Class: Flounder::Domain
- Inherits:
-
Object
- Object
- Flounder::Domain
- Extended by:
- Forwardable
- Defined in:
- lib/flounder/domain.rb
Defined Under Namespace
Classes: NilDevice
Instance Attribute Summary collapse
-
#connection_pool ⇒ Object
readonly
Returns the value of attribute connection_pool.
-
#logger ⇒ Object
Returns the value of attribute logger.
-
#savepoints ⇒ Object
readonly
Returns the value of attribute savepoints.
Instance Method Summary collapse
-
#[](name) ⇒ Object
Returns the entity with the given plural name.
-
#by_oid(oid) ⇒ Object
Returns an entity by table oid.
-
#entities ⇒ Object
Returns all entities as an array.
-
#entity(plural, singular, table_name) ⇒ Object
Define a database entity and alias it to plural and singular names that will be used in the code.
-
#expr(&block) ⇒ Object
Builds an SQL expression.
- #has_entity?(name) ⇒ Boolean
-
#initialize(connection_pool) ⇒ Domain
constructor
A new instance of Domain.
- #log_bm(measure) ⇒ Object
-
#log_sql(sql) ⇒ Object
Logs sql statements that are prepared for execution.
-
#reset_stats ⇒ Object
Resets this threads stats.
-
#stats ⇒ Aggregate
Returns an aggregate of all query wall clock times.
- #transaction(&block) ⇒ Object
Constructor Details
#initialize(connection_pool) ⇒ Domain
Returns a new instance of Domain.
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/flounder/domain.rb', line 18 def initialize connection_pool @connection_pool = connection_pool # maps from plural/singular names to entities in this domain @plural = {} @singular = {} # Maps table names to entities @entity_by_table_name = {} # maps OIDs of entities to entities @oids_entity_map = Hash.new { |hash, oid| hash[oid] = entity_from_oid(oid) } @logger = Logger.new(NilDevice.new) @savepoints = Hash.new(0) end |
Instance Attribute Details
#connection_pool ⇒ Object (readonly)
Returns the value of attribute connection_pool.
39 40 41 |
# File 'lib/flounder/domain.rb', line 39 def connection_pool @connection_pool end |
#logger ⇒ Object
Returns the value of attribute logger.
40 41 42 |
# File 'lib/flounder/domain.rb', line 40 def logger @logger end |
#savepoints ⇒ Object (readonly)
Returns the value of attribute savepoints.
38 39 40 |
# File 'lib/flounder/domain.rb', line 38 def savepoints @savepoints end |
Instance Method Details
#[](name) ⇒ Object
Returns the entity with the given plural name.
84 85 86 87 88 89 |
# File 'lib/flounder/domain.rb', line 84 def [] name raise NoSuchEntity, "No such entity #{name.inspect} in this domain." \ unless @plural.has_key?(name) @plural.fetch(name) end |
#by_oid(oid) ⇒ Object
Returns an entity by table oid.
129 130 131 132 |
# File 'lib/flounder/domain.rb', line 129 def by_oid oid return nil if oid==0 # computed fields @oids_entity_map[oid] end |
#entities ⇒ Object
Returns all entities as an array.
93 94 95 |
# File 'lib/flounder/domain.rb', line 93 def entities @plural.values end |
#entity(plural, singular, table_name) ⇒ Object
Define a database entity and alias it to plural and singular names that will be used in the code.
113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/flounder/domain.rb', line 113 def entity plural, singular, table_name entity = Entity.new(self, plural, singular, table_name). tap { |e| yield e if block_given? } raise ArgumentError, "Table #{table_name} was already associated with an entity." \ if @entity_by_table_name.has_key?(table_name) @plural[plural] = entity @singular[singular] = entity @entity_by_table_name[table_name] = entity entity end |
#expr(&block) ⇒ Object
Builds an SQL expression.
domain.expr { concat('1', '2') }
61 62 63 64 |
# File 'lib/flounder/domain.rb', line 61 def expr &block builder = Expression::Builder.new(self) builder.call(&block) end |
#has_entity?(name) ⇒ Boolean
96 97 98 |
# File 'lib/flounder/domain.rb', line 96 def has_entity? name @plural.has_key? name end |
#log_bm(measure) ⇒ Object
105 106 107 108 |
# File 'lib/flounder/domain.rb', line 105 def log_bm measure stats << measure.total * 1_000 @logger.info "Query took #{measure}." end |
#log_sql(sql) ⇒ Object
Logs sql statements that are prepared for execution.
102 103 104 |
# File 'lib/flounder/domain.rb', line 102 def log_sql sql @logger.info sql end |
#reset_stats ⇒ Object
Resets this threads stats.
78 79 80 |
# File 'lib/flounder/domain.rb', line 78 def reset_stats Thread.current[:__flounder_stats] = nil end |
#stats ⇒ Aggregate
Returns an aggregate of all query wall clock times. Please see github.com/josephruscio/aggregate for more information on this object.
72 73 74 |
# File 'lib/flounder/domain.rb', line 72 def stats Thread.current[:__flounder_stats] ||= Aggregate.new end |
#transaction(&block) ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/flounder/domain.rb', line 45 def transaction &block with_connection do |conn| if in_transaction?(conn) savepoint(conn) { block.call(conn) } else conn.transaction do savepoint(conn) { block.call(conn) } end end end end |