# code: # * George Moschovitis # * Thomas Quas # # (c) 2004 Navel, all rights reserved. # $Id$ require 'og' require 'rubygems' require_gem 'flexmock' module Og # = MockDatabase # # A utility object to Mock a Database in test units. # Extends the standard FlexMock object. # #-- # TODO: Factor out common functionality with Database # to avoid code duplication. #++ class MockDatabase < ::FlexMock # Managed class metadata # class ManagedClassMeta # The managed class. attr_accessor :klass # A mapping of the database fields to the object properties. attr_accessor :field_index def initialize(klass = nil) @klass = klass @field_index = {} end end # hash of configuration options. attr_accessor :config # Pool of connections to the backend. attr_accessor :connection_pool # Managed classes. attr_accessor :managed_classes # Initialize the database interface. # def initialize # Initialize FlexMock super @managed_classes = G::SafeHash.new $log.info "Using mock database." if $og_auto_manage_classes # automatically manage classes with properties and metadata. # gmosx: Any idea how to optimize this? classes_to_manage = [] ObjectSpace.each_object(Class) do |c| if c.respond_to?(:__props) and c.__props classes_to_manage << c end end $log.info "Og auto manages the following classes:" $log.info "#{classes_to_manage.inspect}" manage_classes(*classes_to_manage) end end # Shutdown the database interface. # def shutdown end alias_method :close, :shutdown # Register a standard Ruby class as managed. # def manage(klass) return if managed?(klass) or klass.ancestors.include?(Og::Unmanageable) @managed_classes[klass] = ManagedClassMeta.new(klass) # Add standard og methods to the class. convert(klass) # Add helper methods to the class. enchant(klass) if $og_enchant_managed_classes end # Helper method to set multiple managed classes. # def manage_classes(*klasses) for klass in klasses manage(klass) end end # Stop managing a Ruby class # def unmanage(klass) @managed_classes.delete(klass) end # Is this class managed? # def managed?(klass) return @managed_classes.include?(klass) end # Add standard og functionality to the class # def convert(klass) klass.class_eval %{ DBTABLE = "#{Og::Utils.table(klass)}" DBSEQ = "#{Og::Utils.table(klass)}_oids_seq" def to_i() @oid end } end # Enchant a managed class. Add useful DB related methods to the # class and its instances. # def enchant(klass) klass.class_eval %{ def self.save(obj) $og << obj end def self.load(oid_or_name) $og.load(oid_or_name, #{klass}) end def self.[](oid_or_name) $og.load(oid_or_name, #{klass}) end def self.load_all(extra_sql = nil) $og.load_all(#{klass}, extra_sql) end def self.all(extra_sql = nil) $og.load_all(#{klass}, extra_sql) end def self.count(sql = "SELECT COUNT(*) FROM #{klass::DBTABLE}") $og.count(sql, #{klass}) end def self.select(sql) $og.select(sql, #{klass}) end def self.select_one(sql) $og.select_one(sql, #{klass}) end def self.delete(obj_or_oid) $og.delete(obj_or_oid, #{klass}) end def save $og << self return self end alias_method :save!, :save def update_properties(updatesql) $og.pupdate(updatesql, self.oid, #{klass}) end alias_method :pupdate!, :update_properties def delete! $og.delete(@oid, #{klass}) end } end # Automatically wrap connection methods. # def self.wrap_method(method, args) args = args.split(/,/) class_eval %{ def #{method}(#{args.join(", ")}) # nop end } end =begin wrap_method :create_table, "klass" wrap_method :drop_table, "klass" wrap_method :save, "obj"; alias_method :<<, :save; alias_method :put, :save wrap_method :insert, "obj" wrap_method :update, "obj" wrap_method :update_properties, "update_sql, obj_or_oid, klass = nil" wrap_method :pupdate, "update_sql, obj_or_oid, klass = nil" wrap_method :load, "oid, klass"; alias_method :get, :load wrap_method :load_by_oid, "oid, klass" wrap_method :load_by_name, "name, klass" wrap_method :load_all, "klass, extrasql = nil" wrap_method :select, "sql, klass" wrap_method :select_one, "sql, klass" wrap_method :count, "sql, klass = nil" wrap_method :delete, "obj_or_oid, klass = nil" wrap_method :query, "sql" wrap_method :exec, "sql" =end def self.create_db!(config) end def self.drop_db!(config) end end end # module