ibruby 0.5.1-mswin32

Sign up to get free protection for your applications and to get access to all the features.
data/doc/README ADDED
@@ -0,0 +1,463 @@
1
+ = IBRuby Version 0.5
2
+ IBRuby is an extension for the Ruby programming language that provides access
3
+ to the InterBase RDBMS from CodeGear (formally Borland). The IBRuby library is released under the Mozilla Public Licence version 1.1 and is free for commercial use.
4
+
5
+ ---
6
+
7
+ -- IBRuby 0.5.1 notes - Dec 2006
8
+
9
+ Thanks to the work of Peter Wood, providing support for InterBase was relatively straight forward. This release adds direct support for InterBase and is compiled against InterBase 2007 (aka IB 8). It includes direct support for Boolean data types and SQL Roles.
10
+
11
+ I migrated to 0.5.1 because of changes I needed to make to the source code to make it link under Visual C++ 6.0 (weird, weird problems) and updating of the src.rb file to remove Firebird-isms.
12
+
13
+ --- FireRuby 0.4.x notes
14
+
15
+ == Enhancements & Alterations
16
+
17
+ This release has been brought about as a direct result of efforts to get the
18
+ library working on a 64 bit platform. This process exposed some code of
19
+ questionable quality. All of the issues raised in getting a 64 bit build have
20
+ been addressed and it's only fair that the improvements obtained be passed on
21
+ to all platforms. Functionally nothing has changed since the last release and
22
+ the new version should replace the old with no problems.
23
+
24
+ === Bug Fixes
25
+
26
+ A number of bug fixes were implemented as part of the effort to get the library
27
+ working on a 64 bit platform. None of these had been raised as bugs against the
28
+ library so I have nothing specific to tick off.
29
+
30
+ === Backward Compatibility
31
+
32
+ Version 0.4.0 of the library made fundamental changes to functionality that was
33
+ available in earlier versions of the library. To help accommodate this change
34
+ elements were added to support backward compatibility. The relevant details are
35
+ listed here...
36
+
37
+ - Row objects were switched from keying on column names to keying on column
38
+ aliases. To revert to the older functionality assign a value of true to the
39
+ $FireRubySettings[:ALIAS_KEYS] global setting.
40
+
41
+ - DATE columns were switched from being returned as Time objects to be returned
42
+ as Date objects. To revert to the older functionality assign a value of true
43
+ to the $FireRubySettings[:DATE_AS_DATE] global setting.
44
+
45
+ One other point to note is that, as of version 0.4.0, Enumerable is included in
46
+ the Row and ResultSet classes.
47
+
48
+ === Issues
49
+
50
+ Nothing is perfect so this section outlines those issues that are known to
51
+ exist as of this release.
52
+
53
+ - The service manager functionality does not appear to work on the Mac OS X
54
+ platform. I don't believe that this is a problem in the FireRuby code as I
55
+ have tested the Firebird gbak utility with the -service option and it gives
56
+ the same result. If anyone knows this to be untrue or of a work around let me
57
+ know.
58
+
59
+ - The library currently does not support array columns. This may be implemented
60
+ for a later release depending on demand. No-one has asked for this so far so
61
+ I'm starting to think that people don't make much use of array columns.
62
+
63
+ - The library can be a bit touchy if you don't clean up after yourself. This
64
+ can result in a segmentation violation whenever your program stops if you've
65
+ left a ResultSet or Statement object unclosed. Check through your code to
66
+ insure that this isn't the case before contacting me about problems in this
67
+ line.
68
+
69
+ - The unit tests are currently set up on the assumption that the password for
70
+ your sysdba account is 'masterkey'. If this is not the case, or if you wish
71
+ to use an alternative user for testing, edit the TestSetup.rb file in the
72
+ unit test directory and update the entries there as appropriate. I should also
73
+ note that you may need to alter permissions on the test directory to run the
74
+ actual unit tests on Linux/Unix.
75
+
76
+ ---
77
+
78
+ == Credit Where Credit Is Due
79
+
80
+ Over its lifetime the FireRuby library has benefitted from input provided by a
81
+ number of individuals. This section acknowledges these inputs...
82
+
83
+ <b>Ken Kunz</b>: Ken has been a strong supporter of the library from early on and
84
+ has contributed through feedback, testing and suggestions. For some time he
85
+ produced and tested the Linux builds of the library.
86
+
87
+ <b>David Walthour</b>: David basically executed all of the work to generate the
88
+ 64 bit version of the library, along the way exposing some flaws in the code
89
+ (amazing what a bit of peer review can find!). David produced the 64 bit version
90
+ of the library gem.
91
+
92
+ <b>John Wood</b>: John currently builds and tests the Mac OS X version of the
93
+ library.
94
+
95
+ <b>Art Federov</b>: Art provided input on handling and testing character sets.
96
+
97
+ ---
98
+
99
+ == Installation & Usage
100
+
101
+ The library is provided as a gem and built for use with Ruby 1.8+. Testing
102
+ against an earlier release of Ruby has not been performed. Installation requires
103
+ the Ruby Gems package to be installed. Assuming that these installation criteria
104
+ have been met the library can be installed on Windows by executing a command
105
+ such as the following...
106
+
107
+ gem install fireruby-0.4.1-mswin32.gem
108
+
109
+ On the Mac OS X platform you may require super user privilege if your Ruby is
110
+ installed to the default location (i.e. /usr/local/lib). In this case you can
111
+ use the sudo command to make the installation like this...
112
+
113
+ sudo gem install fireruby-0.4.1-powerpc-darwin.gem
114
+
115
+ Once the gem installation is complete the FireRuby functionality can be accessed
116
+ in code with the usual gem style requires...
117
+
118
+ require 'rubygems'
119
+ require 'fireruby'
120
+
121
+ === Build Details
122
+
123
+ The FireRuby library is a Ruby extension written in C. The avoid build issues
124
+ binary versions are provided for a number of platforms, including...
125
+
126
+ - Windows: Built against a version of Ruby installed using the one-click
127
+ installer and using the freely available Microsoft development tools. This
128
+ version was compiled against version 1.5.2 of Firebird.
129
+
130
+ - Linux: Built on Ubuntu Linux (Breezy Badger) using a version of Ruby 1.8.2
131
+ installed via the Synaptic package manager. This package manager was also
132
+ used to make an installation of Firebird.
133
+
134
+ - Mac OS X: Build on version 10.4.3 of OS X against the 1.8.2 version of Ruby
135
+ that comes with the operating system. A framework installation of Firebird
136
+ version 1.5.1 was used to make the build.
137
+
138
+ Its possible to try and build the library on other platforms so I'll provide a
139
+ few details as to how to go about doing this. The first step is to download the
140
+ CVS tar ball from the Ruby Forge site and expand it into a local directory. This
141
+ will create a directory called fireruby. Change into this directory and then
142
+ into the fireruby/src subdirectory.
143
+
144
+ This directory contains a file called extconf.rb that is used to create the
145
+ make file used to build the library. The make file is created by executing this
146
+ file but before you do there are a number of parameters that you should be
147
+ aware of. The main one of these is --with-firebird-dir. This parameter is used
148
+ to indicate the whereabouts of the Firebird headers and libraries. The following
149
+ is an example of how this might be used...
150
+
151
+ ruby extconf.rb --with-firebird-dir=/usr/local/firebird
152
+
153
+ You may need to customise the path for your own Firebird installation. The path
154
+ specified should be a directory that contains subdirectories called 'lib' and
155
+ 'include'. The lib subdirectory should contain the fbclient shared library and
156
+ include should contain the ibase.h header file.
157
+
158
+ A note for Windows users. The library requires the free Microsoft C++ compiler,
159
+ the Windows SDK, the .NET SDK and nmake to build. If you have all of these
160
+ and Firebird installed to default locations then you can create a make file
161
+ using the mkmf.bat batch file in the src directory.
162
+
163
+ Once you have the make file you can attempt a library build using either make
164
+ (on Unix/Linux) or nmake (on Windows). If it builds successfully you can move
165
+ on to creating a gem file for installation. To do this, change into the ../gem
166
+ directory. In this directory you can do the following (on Windows)...
167
+
168
+ make_gem
169
+
170
+ ...or the following (on Unix/Linux)...
171
+
172
+ ruby make_gem.rb
173
+
174
+ This will create the gem file in the main fireruby directory. Install this and
175
+ execute the unit tests to check whether you're version is working.
176
+
177
+ === So How Do I Use It?
178
+
179
+ This section will provide some examples of usage for the the FireRuby classes.
180
+ Throughout the code the following set of assumptions are made.
181
+
182
+ - The user name and password that will be employed to attach to the database
183
+ are 'sysdba' and 'masterkey' respectively (the Firebird defaults).
184
+
185
+ - The databases attached to will all be local (i.e. they will all reside on the
186
+ same machine) as the test code.
187
+
188
+ A database, from the Firebird perspective, is made up of one or more files. From
189
+ a FireRuby perspective a user interaction with a database starts through the
190
+ Database class. This class provides facilities that allow for creating, dropping
191
+ and connecting to database instances. For example, to obtain a connection to a
192
+ database you would use something like the following...
193
+
194
+ require 'rubygems'
195
+ require 'fireruby'
196
+
197
+ include FireRuby
198
+
199
+ db = Database.new('./test.fdb')
200
+ c = db.connect('sysdba', 'masterkey')
201
+
202
+ This example starts by requiring the necessary files and including the FireRuby
203
+ module locally - later examples will not detail these lines but they are always
204
+ required to use the FireRuby code.
205
+
206
+ The first line of code after the include creates a new database object. This
207
+ process does not actually create the database file (see the Database#create
208
+ method API documentation if that is what you want to do), it simple creates an
209
+ abstract reference to a database. In creating the Database object we had to
210
+ provide a database specification string which identifies the database we want to
211
+ access. In this case we are specifying a database in the current working
212
+ directory called 'test.fdb'. See the Firebird documentation for details on the
213
+ structure of more complex database specifications.
214
+
215
+ The last line of code in the example given above opens a connection to the
216
+ database. In doing this we had to provide two parameters, the database user
217
+ name and password. These are required to gain access to the database.
218
+
219
+ A connection represents a conduit to a database and obtaining a connection is a
220
+ prerequisite to working with the database. The FireRuby library support having
221
+ multiple connections, to one or more databases, using one or more users, active
222
+ simultaneously. FireRuby represents a database connection through objects of the
223
+ Connection class. This class provides functionality to determine the current
224
+ state a database connection (open or closed) and for closing the connection.
225
+ Connections take up resources, both locally and on the database server and
226
+ should be explicitly closed when they are no longer required.
227
+
228
+ The connection class also provides a set of conveniences methods to allow for
229
+ the execution of SQL against a database. These methods, execute_immediate and
230
+ execute, represently two slightly different approaches to executing SQL against
231
+ the database. Refer to the API documentation for more information.
232
+
233
+ An advantage of using a relational database management system like Firebird is
234
+ that it provides transactions. A transaction represents a block of work that is
235
+ either all completed successful or none of it is applied. From the perspective
236
+ of the database this means that a series of steps that make changes to the
237
+ tables in the database can be wrapped in a transaction to insure that they
238
+ either all complete or that none of the changes are applied.
239
+
240
+ The FireRuby library represents a database transaction through instances of the
241
+ Transaction class. There are two ways of obtaining a Transaction using the
242
+ library, both requiring you to have an open database connection. The first way
243
+ is to construct a new Transaction object like so...
244
+
245
+ tx = Transaction.new(connection)
246
+
247
+ The Transaction constructor takes a single parameter which must be either a
248
+ Connection object or an array of Connection objects. If you pass an array of
249
+ Connection objects to this constructor then the Transaction created will apply
250
+ across all of the databases that the connections refer to, allowing you to
251
+ have transactional control of work that must utilise more than one database. The
252
+ second way to obtain a transaction is to simply request one from a Connection
253
+ object, like so.
254
+
255
+ tx = connection.start_transaction
256
+
257
+ In this case the transaction will only ever apply to one database, the one that
258
+ the connection relates to. This method also accepts a block, taking a single
259
+ parameter. The parameter passed to the block will be the transaction created.
260
+ In this case the lifetime of the transaction is delimited by the block. If the
261
+ block completes successfully then the work of the transaction will be committed
262
+ to the database. If the block raises an exception then the transactional work
263
+ will be rolled back.
264
+
265
+ When the block of work associated with a transaction is complete the user must
266
+ instruct the system to either apply the changes implemented by the work or to
267
+ discard them. This can be done by calling the commit or rollback methods of the
268
+ Transaction class respectively. Once a transaction has been committed or rolled
269
+ back it can no longer be used and should be discarded. Note that attempts to
270
+ close a connection that has an active transaction against it will fail, so one
271
+ of the commit or rollback methods should be explictly called in code. The
272
+ block technique detailed above helps protect against the failure to do this and
273
+ is a useful technique.
274
+
275
+ The Transaction object provides a number of other informational and utility
276
+ methods. Check the API documentation for this class for more information.
277
+
278
+ So we've looked at connections and transactions, but how do we actually do
279
+ something practical with the database. Well there are a number of possible
280
+ approaches that we can take to this. Both the Connection and Transaction classes
281
+ have convenience method for the execution of SQL statements and these are useful
282
+ for quick SQL. Where you want something that you can repeatedly reuse and,
283
+ optionally, pass parameters to then you need the Statement class.
284
+
285
+ The Statement class represents a SQL statement that has been validated and
286
+ prepared for execution. Here's an example of creating a SQL statement...
287
+
288
+ s = Statement.new(cxn, tx, 'SELECT * FROM MY_TABLE', 3)
289
+
290
+ In this example we have created a Statement object that wraps a SQL select from
291
+ a table called MY_TABLE. The first parameter to the constructor is a Connection
292
+ object and the second is a Transaction, both mandatory. You may be thinking
293
+ 'why do I need a transaction here, I'm not changing anything?'. This is true
294
+ (well sort of) but it's a requirement of the underlying database system. This
295
+ is also the case for the final parameter to the constructor. The value 3 is
296
+ the SQL dialect to be used with the Statement. This exists for reason arising
297
+ from the move from closed source Interbase to open source Firebird. The
298
+ parameter should be given a value of between 1 and 3. If you're not sure what
299
+ this is and you're only using Firebird it's probably safe to use a value of
300
+ 3 here. Other values are for backward compatibility. Consult the Firebird and
301
+ Interbase documentation for more details.
302
+
303
+ Anyway, now that we have our Statement how do we use it? Well, the answer is
304
+ that we call once of the Statement objects execute methods. The one to be called
305
+ depends on whether the Statement requires parameters or not. What are parameters
306
+ you ask? Well, look at the following...
307
+
308
+ s = Statement.new(cxn, tx, 'SELECT * FROM MY_TABLE WHERE MYID = ?', 3)
309
+
310
+ Note that the SQL select for this Statement contains a '?'. This is a position
311
+ holder for a value that the statement expects to be provided later. A Statement
312
+ that wraps such a piece of SQL must be provided with the necessary parameters
313
+ to execute properly. Where a Statement object represents SQL that requires a
314
+ parameter then the execute_for method must be called, like this...
315
+
316
+ s.execute_for([25])
317
+
318
+ This code executes the SQL substituting the parameters from the array of data
319
+ passed to the function call. If a Statement object represents SQL that does not
320
+ require parameter values a call to the execute method will suffice, such as the
321
+ following...
322
+
323
+ s.execute
324
+
325
+ The execute methods for the Statement class, as with all of the execute methods
326
+ for the FireRuby library, have three potential return values. They will either
327
+ return an Integer, a ResultSet object or nil. A ResultSet object will only be
328
+ returned for SQL statements that constitute a query, irrespective of whether
329
+ that query returns any data. For all other SQL statements (inserts, updates and
330
+ deletes) the execute method will return a count of the number of rows affected
331
+ by the statement execution. For any other SQL statements the various execute
332
+ methods will return nil.
333
+
334
+ A ResultSet object represents a handle by which the data retrieved for a SQL
335
+ query can be accessed. While it's possible to obtain a ResultSet from one of the
336
+ execute methods on the Connection, Transaction or Statement classes it is more
337
+ efficient to create one directly. The constructor for the ResultSet class
338
+ accepts the same arguments as the constructor for the Statement class but will
339
+ throw an exception if the SQL statement specified is not a query.
340
+
341
+ Once we have obtained a ResultSet we can extract the rows of data for a query
342
+ from it. To fetch a row of data from a ResultSet object you call the fetch
343
+ method, like the following...
344
+
345
+ row = r.fetch
346
+
347
+ This fetches a single row of data for a query represented as a Row object (which
348
+ will be covered shortly). The ResultSet class also provides for iteration across
349
+ the contents of a result set by providing an each method. The block to the each
350
+ method will be passed the data for the ResultSet, a row at a time.
351
+
352
+ It should be noted that both the Statement and ResultSet objects hold resources
353
+ while they are active. They both possess close methods and these should be
354
+ explicitly called to release the associated resources. The exception to this
355
+ rule is for ResultSets. If you select all of the rows from a ResultSet then the
356
+ resources for the ResultSet are automatically released. It is still safe to call
357
+ close on such a ResultSet as this will not cause errors.
358
+
359
+ Okay, so you've gotten a row of data in the form of a Row object from your
360
+ ResultSet, how do we get the data out of it? Well, there are a number of ways
361
+ of doing this. You can treat the Row object like an array and dereference the
362
+ columns of data within the row like this...
363
+
364
+ value = row[1]
365
+
366
+ The index specified to the array dereference operator specifies the column that
367
+ you want the data for. Column indices start at 0. Alternatively you can treat
368
+ the Row object like a read only Hash object and use the column name to access
369
+ the data, like this...
370
+
371
+ value = row['MYID']
372
+
373
+ This is beneficial as it frees you from the constraint of knowing the ordering
374
+ of the columns within the row. For more information of the Row class please
375
+ consult the API documentation.
376
+
377
+ That covers the bulk of the SQL classes provided by the FireRuby library. The
378
+ two which haven't been touched upon are the Generator class and the Blob class.
379
+
380
+ The Generator class is a wrapper around the Firebird generator facility. A
381
+ generator, also known as a sequence, provides a means of creating a list of
382
+ numeric values in a way that is guaranteed to be thread and process safe. Used
383
+ properly generators can be employed to create unique sequences that make perfect
384
+ table keys. Consult the API documentation for more details on the Generator
385
+ class.
386
+
387
+ The Blob class is returned as part of the Row object data obtained from a
388
+ ResultSet. The class wraps the concept of a binary large object stored in the
389
+ database. Consult the API documentation for further information.
390
+
391
+ === Errors
392
+
393
+ Whenever a problem occurs within a FireRuby library class then it is likely that
394
+ a FireRubyException will be thrown. The FireRubyException class is the error
395
+ class used by the FireRuby library whenever it hits trouble. The class provides
396
+ a means of finding out a little more about what exactly has gone wrong. Again,
397
+ consult the API documentation for more details.
398
+
399
+ === Firebird Service Manager
400
+
401
+ The FireRuby library provides a set of class that provide for an interaction
402
+ with the Firebird service manager. This interaction allows for the execution of
403
+ tasks, such as the backing up of a database, on the database server. To execute
404
+ such tasks against the service manager for a Firebird instance you first need
405
+ to obtain a ServiceManager class instance. This can be done as follows...
406
+
407
+ sm = ServiceManager.new('localhost')
408
+
409
+ The constructor for the ServiceManager class takes a single parameter that is
410
+ the host name of the server running the Firebird instance. In the example above
411
+ this would be a local machine but could be any machine that can be reached over
412
+ the network (NOTE: although Firebird supports a number of underlying transport
413
+ protocols in accessing a service manager currently only TCP/IP is supported for
414
+ the FireRuby library).
415
+
416
+ The next step in executing service manager tasks involves connecting your
417
+ ServiceManager object to the service manager for a Firebird instance. To do this
418
+ you must supply a user name and password. The user name and password used must
419
+ be a user that exists on the Firebird instance. The user you connect as can
420
+ affect the access to services that you receive. For example, to connect as the
421
+ database administrator you might do the following...
422
+
423
+ sm.connect('sysdba', 'masterkey')
424
+
425
+ Assuming that this succeeds you are now ready to execute tasks through your
426
+ ServiceManager object. Within the FireRuby library individual task are broken
427
+ out into separate classes. For this release (0.4.1) there are four task classes
428
+ provided in the library - Backup, Restore, AddUser and RemoveUser. I think the
429
+ class names are relatively self explanatory but if you want more information
430
+ consult the API documentation for a class.
431
+
432
+ To use the task classes you construct a class instance, configure it as you may
433
+ need and then execute it. Here's an example of going through this procedure to
434
+ create a database backup...
435
+
436
+ b = Backup.new('c:\database\work.fdb', 'c:\temp\work.bak')
437
+ b.metadata_only = true
438
+ b.execute(sm)
439
+
440
+ The first list creates the new Backup object. The first parameter passed to this
441
+ call is the path and name of the primary file of the database to be backed up
442
+ (NOTE: All paths are relative to the database server). The second parameter is
443
+ the path and name of the database backup file to be created. The second line
444
+ sets an attribute on the class to indicate that only the metadata (i.e. it's
445
+ schema but not it's data) for the specified database should be included in the
446
+ backup. The final line begins the execution of the backup task on the database
447
+ server. This will block until completion.
448
+
449
+ Its also possible to execute a batch of tasks against a service manager. To do
450
+ this you would accumulate the tasks to be executed and then pass them all at the
451
+ same time to the ServiceManager#execute method, like so...
452
+
453
+ t = Array.new
454
+ t.push(Backup.new('c:\database\work.fdb', 'c:\temp\work.bak'))
455
+ ...
456
+ # Create more tasks here and add them to the array.
457
+
458
+ sm.execute(*t)
459
+
460
+ The tasks will be executed in the order they are specified to the ServiceManager
461
+ object. For the example above this would mean in the order they were added to
462
+ the array. For more details on the ServiceManager class and the various task
463
+ classes please consult the API documentation.