Class: Lafcadio::ObjectStore::DbBridge
- Inherits:
-
Object
- Object
- Lafcadio::ObjectStore::DbBridge
show all
- Defined in:
- lib/lafcadio/objectStore.rb
Overview
Defined Under Namespace
Classes: RollbackError, Transaction
Constant Summary
collapse
- @@last_pk_id_inserted =
nil
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
Returns a new instance of DbBridge.
547
548
549
550
551
552
553
|
# File 'lib/lafcadio/objectStore.rb', line 547
def initialize
@db_conn = DbConnection.get_db_connection
@transaction = nil
ObjectSpace.define_finalizer( self, proc { |id|
DbConnection.get_db_connection.disconnect
} )
end
|
Class Method Details
._load(aString) ⇒ Object
526
527
528
529
530
531
532
533
534
535
536
|
# File 'lib/lafcadio/objectStore.rb', line 526
def self._load(aString)
aString =~ /db_conn:/
db_conn_str = $'
begin
db_conn = Marshal.load db_conn_str
rescue TypeError
db_conn = nil
end
DbConnection.set_db_connection db_conn
new
end
|
.last_inserted_pk_id_pair(domain_class) ⇒ Object
538
539
540
541
542
543
544
545
|
# File 'lib/lafcadio/objectStore.rb', line 538
def self.last_inserted_pk_id_pair( domain_class )
case ObjectStore.db_type
when 'Mysql'
[ 'last_insert_id()', 'last_insert_id()' ]
when 'Pg'
[ "currval('#{ domain_class.postgres_pk_id_seq }')", 'currval' ]
end
end
|
Instance Method Details
#_dump(aDepth) ⇒ Object
555
556
557
558
|
# File 'lib/lafcadio/objectStore.rb', line 555
def _dump(aDepth)
dbDump = @dbh.respond_to?( '_dump' ) ? @dbh._dump : @dbh.class.to_s
"dbh:#{dbDump}"
end
|
#commit(db_object) ⇒ Object
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
|
# File 'lib/lafcadio/objectStore.rb', line 560
def commit(db_object)
statements_and_binds = ObjectStore::CommitSqlStatementsAndBinds.new(
db_object
)
statements_and_binds.each do |sql, binds|
maybe_log sql
@db_conn.do( sql, *binds )
end
if statements_and_binds[0].first =~ /insert/
@@last_pk_id_inserted = get_last_pk_id_inserted(
db_object.domain_class
)
end
@db_conn.do( 'commit' ) unless @transaction
end
|
#get_last_pk_id_inserted(domain_class) ⇒ Object
576
577
578
579
580
581
582
583
584
585
586
587
588
|
# File 'lib/lafcadio/objectStore.rb', line 576
def get_last_pk_id_inserted( domain_class )
pair = self.class.last_inserted_pk_id_pair( domain_class )
sql = 'select ' + pair.first
begin
select_all( sql ).first[pair.last].to_i
rescue RuntimeError
error_msg =
"The field \"" + domain_class.sql_primary_key_name +
"\" can\'t be found in the table \"" +
domain_class.table_name + "\"."
raise FieldMatchError, error_msg, caller
end
end
|
#group_query(query) ⇒ Object
590
591
592
|
# File 'lib/lafcadio/objectStore.rb', line 590
def group_query( query )
select_all( query.to_sql ).map { |row| query.result_row( row ) }
end
|
#last_pk_id_inserted ⇒ Object
594
|
# File 'lib/lafcadio/objectStore.rb', line 594
def last_pk_id_inserted; @@last_pk_id_inserted; end
|
#maybe_log(sql) ⇒ Object
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
|
# File 'lib/lafcadio/objectStore.rb', line 596
def maybe_log(sql)
config = LafcadioConfig.new
if config['logSql'] == 'y'
sqllog = Log4r::Logger['sql'] || Log4r::Logger.new( 'sql' )
if sqllog.outputters.empty?
filename = File.join(
config['logdir'], config['sqlLogFile'] || 'sql'
)
outputter = Log4r::FileOutputter.new(
'outputter', { :filename => filename }
)
sqllog.outputters = outputter
end
sqllog.info sql
end
end
|
#select_all(sql) ⇒ Object
613
614
615
616
617
618
619
620
|
# File 'lib/lafcadio/objectStore.rb', line 613
def select_all(sql)
maybe_log sql
begin
@db_conn.select_all( sql )
rescue DBI::DatabaseError => e
raise $!.to_s + ": #{ e.errstr }"
end
end
|
#select_dobjs(query) ⇒ Object
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
|
# File 'lib/lafcadio/objectStore.rb', line 622
def select_dobjs(query)
domain_class = query.domain_class
select_all( query.to_sql( ObjectStore.db_type ) ).collect { |row_hash|
dobj = domain_class.new(
SqlToRubyValues.new( domain_class, row_hash )
)
if query.include
query.include.each do |include_sym|
field = domain_class.field include_sym
included_dclass = field.linked_type
if dobj.send( field.name )
dobj.send( field.name ).db_object = included_dclass.new(
SqlToRubyValues.new( included_dclass, row_hash )
)
end
end
end
dobj
}
end
|
#transaction(action) ⇒ Object
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
|
# File 'lib/lafcadio/objectStore.rb', line 643
def transaction( action )
@transaction = Transaction.new @db_conn
@transaction.commit
begin
action.call @transaction
@transaction.commit
rescue RollbackError
rescue
err_to_raise = $!
@transaction.rollback false
raise err_to_raise
end
@transaction = nil
end
|