Class: Trix51::Database

Inherits:
Trix51
  • Object
show all
Defined in:
lib/trix51db.rb

Overview

Class for managing a single Trix51 based database, allowing access to tables.

Constant Summary

Constants inherited from Trix51

KEY_JOIN, META_PREFIX, RECORD_PREFIX, SEQ_PREFIX

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Trix51

add_connection, add_helper, class_to_database, class_to_table, connections, create_helper_code, debug, defer_classref, defer_classref=, error, fatal, info, table_to_database, warn

Constructor Details

#initialize(*args) ⇒ Database

db = Trix51::Database.new( path: ‘./data’, database: ‘animals.t51’ )



182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/trix51db.rb', line 182

def initialize( *args )

	@meta_prefix = Trix51::META_PREFIX
	@tables = {}
	@args = {
		:path => './trix',
		:database => 'database.db'
	}

	args.each { |key|
		key.each_pair { |k,v|
			@args[k] = v
		}
	}
	
	if not Dir.exist?( @args[:path] ) then
	  Dir.mkdir( @args[:path] )
	end
	
	# now create / open the dbm file
	begin
	  @dbm = GDBM.new( "#{@args[:path]}/#{@args[:database]}" )
	rescue => e
	    raise Trix51::DatabaseError, "Unable to open #{@args[:path]}/#{@args[:database]}: #{e.to_s}"
	end
	
	Trix51.add_connection( self )
	
	list = self.tables
	list.each do |tag|
	  tagref = self.get_table( tag )
	  Trix51.add_helper( tagref.classname, self, tagref )
	end

end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(methodid) ⇒ Object

:nodoc:



313
314
315
316
317
318
# File 'lib/trix51db.rb', line 313

def method_missing( methodid ) #:nodoc:
  if self.tables.include?( methodid ) then
    return self.get_table( methodid )
  end
  raise Trix51::NotFoundError, "Table #{methodid.to_s } does not exist"
end

Instance Attribute Details

#dbmObject

Database accessor



326
327
328
# File 'lib/trix51db.rb', line 326

def dbm
  @dbm
end

Instance Method Details

#closeObject

Close the database, cleaning up a persisting indexes to disk.



219
220
221
222
223
224
225
# File 'lib/trix51db.rb', line 219

def close
  return if @dbm.closed?
  @tables.each_value do |tblref|
    tblref.indices_save( @args[:path] )
  end
  @dbm.close
end

#create_table(tablename, args, classname = 'Trix51::Tuple') ⇒ Object

animal = db.create_table(

:animal,
{

id: { datatype: ‘integer’, key: true, autoincrement: true }, species_id: { datatype: ‘integer’, required: true, indexed: true }, species: { datatype: ‘record’, source: :species_id, table: :species, dest: :id }, name: { datatype: ‘string’, required: true, unique: true }, created_date: { datatype: ‘datetime’, default: ‘#Time.new.to_s’ }, number_of_legs: { datatype: ‘integer’, default: 4 }, number_of_eyes: { datatype: ‘integer’, default: 2 }, legs_and_eyes: { datatype: ‘integer’, calculation: ‘number_of_legs + number_of_eyes’ }

  },
  'Animal'
)

Field types

Valid field types are ‘string’, ‘integer’, ‘datetime’, ‘float’, ‘boolean’, ‘record’, ‘resultset’

The ‘calculation:’ clause fields are evaluated based on other fields in the table.



280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
# File 'lib/trix51db.rb', line 280

def create_table( tablename, args, classname = 'Trix51::Tuple' )
  
  mref = @dbm[ Trix51::META_PREFIX+tablename.to_s ]
  
  if not mref.nil? then
    raise Trix51::TableExistsError, "Attempt to create a table that exists..."
  end
                      
  # create it
  @dbm[	Trix51::META_PREFIX+tablename.to_s ] = Marshal.dump(
    tablename: tablename,
    structure: args,
    classname: classname
  )
  @tables[tablename] = Trix51::Table.new( tablename, @dbm )
	
         Trix51.add_helper(  @tables[tablename].classname, self,  @tables[tablename] )	  
  
  return @tables[tablename]
  
end

#get_table(tablename) ⇒ Object

Example

animal = db.get_table( :animal )

Note: you can also use the alias db.animal to access it.



235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
# File 'lib/trix51db.rb', line 235

def get_table( tablename )
  
    # do we have a cached object?
    mref = @dbm[ Trix51::META_PREFIX+tablename.to_s ]
  
    if @tables[tablename].nil? then
      mref = @dbm[ Trix51::META_PREFIX+tablename.to_s ]
      raise Trix51::NotFoundError, "Table #{tablename.to_s} does not exist." if mref.nil?
      @tables[tablename] = Trix51::Table.new( tablename.to_s, @dbm )
      @tables[tablename].indices_load( @args[:path] )
    end
    
    return @tables[tablename]
    
end

#has_table?(table) ⇒ Boolean

Return true or false if a given table exists.

Returns:

  • (Boolean)


321
322
323
# File 'lib/trix51db.rb', line 321

def has_table?( table ) 
  return (self.tables.include?( table ))
end

#tablesObject

Provides a list of the tables in the database.



303
304
305
306
307
308
309
310
311
# File 'lib/trix51db.rb', line 303

def tables
  list = []
  @dbm.each_key do |k|
    if k.match( Trix51::META_PREFIX )
      list.push( k.gsub( Trix51::META_PREFIX, '' ).to_sym )
    end
  end
  return list
end