Class: Lafcadio::ObjectStore::DbBridge

Inherits:
Object
  • Object
show all
Defined in:
lib/lafcadio/objectStore.rb

Overview

:nodoc:

Defined Under Namespace

Classes: Transaction

Constant Summary collapse

@@last_pk_id_inserted =
nil

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeDbBridge

Returns a new instance of DbBridge.



581
582
583
584
# File 'lib/lafcadio/objectStore.rb', line 581

def initialize
  @db_conn = DbConnection.get_db_connection
  @transaction = nil
end

Class Method Details

._load(aString) ⇒ Object



560
561
562
563
564
565
566
567
568
569
570
# File 'lib/lafcadio/objectStore.rb', line 560

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



572
573
574
575
576
577
578
579
# File 'lib/lafcadio/objectStore.rb', line 572

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



586
587
588
589
# File 'lib/lafcadio/objectStore.rb', line 586

def _dump(aDepth)
  dbDump = @dbh.respond_to?( '_dump' ) ? @dbh._dump : @dbh.class.to_s
  "dbh:#{dbDump}"
end

#commit(db_object) ⇒ Object



591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
# File 'lib/lafcadio/objectStore.rb', line 591

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



607
608
609
610
611
612
613
614
615
616
617
618
619
# File 'lib/lafcadio/objectStore.rb', line 607

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



621
622
623
# File 'lib/lafcadio/objectStore.rb', line 621

def group_query( query )
  select_all( query.to_sql ).map { |row| query.result_row( row ) }
end

#last_pk_id_insertedObject



625
# File 'lib/lafcadio/objectStore.rb', line 625

def last_pk_id_inserted; @@last_pk_id_inserted; end

#maybe_log(sql) ⇒ Object



627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
# File 'lib/lafcadio/objectStore.rb', line 627

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

#rollbackObject



644
# File 'lib/lafcadio/objectStore.rb', line 644

def rollback; @transaction.rollback( false ) if @transaction; end

#select_all(sql) ⇒ Object



646
647
648
649
650
651
652
653
# File 'lib/lafcadio/objectStore.rb', line 646

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



655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
# File 'lib/lafcadio/objectStore.rb', line 655

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



676
677
678
679
680
681
682
683
684
685
686
687
688
# File 'lib/lafcadio/objectStore.rb', line 676

def transaction( action )
  @transaction = Transaction.new @db_conn
  @transaction.commit
  begin
    action.call @transaction
    @transaction.commit
  rescue
    err_to_raise = $!
    @transaction.rollback false
    raise err_to_raise
  end
  @transaction = nil
end

#transactional_cloneObject



690
# File 'lib/lafcadio/objectStore.rb', line 690

def transactional_clone; clone; end