Class: ActiveRecord::ConnectionAdapters::FrontBaseAdapter

Inherits:
AbstractAdapter
  • Object
show all
Defined in:
lib/active_record/connection_adapters/frontbase_adapter.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from AbstractAdapter

#raw_connection, #requires_reloading?, #reset_runtime, #supports_count_distinct?, #verify!

Methods included from Quoting

#quoted_date

Methods included from SchemaStatements

#add_index, #add_order_by_for_association_limiting!, #distinct, #dump_schema_information, #index_name, #initialize_schema_information, #table_alias_for, #table_alias_length, #type_to_sql

Constructor Details

#initialize(connection, logger, connection_options, config) ⇒ FrontBaseAdapter

Returns a new instance of FrontBaseAdapter.



248
249
250
251
252
253
254
255
256
257
258
259
260
# File 'lib/active_record/connection_adapters/frontbase_adapter.rb', line 248

def initialize(connection, logger, connection_options, config)
  super(connection, logger)
  @connection_options, @config = connection_options, config
  @transaction_mode = :pessimistic
  
  # Start out in auto-commit mode
  self.rollback_db_transaction
  
  # threaded_connections_test.rb will fail unless we set the session
  # to optimistic locking mode
#         set_pessimistic_transactions
#         execute "SET TRANSACTION ISOLATION LEVEL REPEATABLE READ, READ WRITE, LOCKING OPTIMISTIC"
end

Class Method Details

.compare_versions(v1, v2) ⇒ Object



237
238
239
240
241
242
243
244
245
# File 'lib/active_record/connection_adapters/frontbase_adapter.rb', line 237

def compare_versions(v1, v2)
  v1_seg  = v1.split(".")
  v2_seg  = v2.split(".")
  0.upto([v1_seg.length,v2_seg.length].min) do |i|
    step  = (v1_seg[i].to_i <=> v2_seg[i].to_i)
    return step unless step == 0
  end
  return v1_seg.length <=> v2_seg.length
end

Instance Method Details

#active?Boolean

CONNECTION MANAGEMENT ====================================

Returns:

  • (Boolean)


432
433
434
435
436
# File 'lib/active_record/connection_adapters/frontbase_adapter.rb', line 432

def active?
  true if @connection.status == 1
rescue => e
  false
end

#adapter_nameObject

Returns the human-readable name of the adapter. Use mixed case - one can always use downcase if needed.



264
265
266
# File 'lib/active_record/connection_adapters/frontbase_adapter.rb', line 264

def adapter_name #:nodoc:
  'FrontBase'
end

#add_column(table_name, column_name, type, options = {}) ⇒ Object

Adds a new column to the named table. See TableDefinition#column for details of the options you can use.



789
790
791
792
793
794
# File 'lib/active_record/connection_adapters/frontbase_adapter.rb', line 789

def add_column(table_name, column_name, type, options = {})
  add_column_sql = "ALTER TABLE #{table_name} ADD #{column_name} #{type_to_sql(type, options[:limit])}"
  options[:type] = type
  add_column_options!(add_column_sql, options)
  execute(add_column_sql)
end

#add_column_options!(sql, options) ⇒ Object

:nodoc:



796
797
798
799
800
801
802
803
804
805
# File 'lib/active_record/connection_adapters/frontbase_adapter.rb', line 796

def add_column_options!(sql, options) #:nodoc:
  default_value = quote(options[:default], options[:column])
  if options_include_default?(options)
    if options[:type] == :boolean
      default_value = options[:default] == 0 ? quoted_false : quoted_true
    end
    sql << " DEFAULT #{default_value}"
  end
  sql << " NOT NULL" if options[:null] == false
end

#add_limit_offset!(sql, options) ⇒ Object

:nodoc:



567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
# File 'lib/active_record/connection_adapters/frontbase_adapter.rb', line 567

def add_limit_offset!(sql, options) #:nodoc:
  if limit = options[:limit]
    offset = options[:offset] || 0
  
# Here is the full syntax FrontBase supports:
# (from [email protected])
# 
#       TOP <limit - unsigned integer>
#       TOP ( <offset expr>, <limit expr>)
  
    # "TOP 0" is not allowed, so we have
    # to use a cheap trick.
    if limit.zero?
      case sql
      when /WHERE/i
        sql.sub!(/WHERE/i, 'WHERE 0 = 1 AND ')
      when /ORDER\s+BY/i
        sql.sub!(/ORDER\s+BY/i, 'WHERE 0 = 1 ORDER BY')
      else
        sql << 'WHERE 0 = 1'
      end
    else
      if offset.zero?
        sql.replace sql.gsub("SELECT ","SELECT TOP #{limit} ")
      else
        sql.replace sql.gsub("SELECT ","SELECT TOP(#{offset},#{limit}) ")
      end
    end
  end
end

#begin_db_transactionObject

:nodoc:



551
552
553
# File 'lib/active_record/connection_adapters/frontbase_adapter.rb', line 551

def begin_db_transaction #:nodoc:
  execute "SET COMMIT FALSE" rescue nil
end

#change_column(table_name, column_name, type, options = {}) ⇒ Object

:nodoc:



826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
# File 'lib/active_record/connection_adapters/frontbase_adapter.rb', line 826

def change_column(table_name, column_name, type, options = {}) #:nodoc:
  change_column_sql = %( ALTER COLUMN "#{table_name}"."#{column_name}" TO #{type_to_sql(type, options[:limit])} )
  execute(change_column_sql)
  change_column_sql = %( ALTER TABLE "#{table_name}" ALTER COLUMN "#{column_name}" )

  if options_include_default?(options)
    default_value = quote(options[:default], options[:column])
    if type == :boolean
      default_value = options[:default] == 0 ? quoted_false : quoted_true
    end
    change_column_sql << " SET DEFAULT #{default_value}"
  end

  execute(change_column_sql)
  
#         change_column_sql = "ALTER TABLE #{table_name} CHANGE #{column_name} #{column_name} #{type_to_sql(type, options[:limit])}"
#         add_column_options!(change_column_sql, options)
#         execute(change_column_sql)
end

#change_column_default(table_name, column_name, default) ⇒ Object

:nodoc:



822
823
824
# File 'lib/active_record/connection_adapters/frontbase_adapter.rb', line 822

def change_column_default(table_name, column_name, default) #:nodoc:
  execute "ALTER TABLE #{table_name} ALTER COLUMN #{column_name} SET DEFAULT #{default}" if default != "NULL"
end

#classes_for_table_name(table) ⇒ Object



625
626
627
# File 'lib/active_record/connection_adapters/frontbase_adapter.rb', line 625

def classes_for_table_name(table)
  ActiveRecord::Base.send(:subclasses).select {|klass| klass.table_name == table}
end

#columns(table_name, name = nil) ⇒ Object

:nodoc:



713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
# File 'lib/active_record/connection_adapters/frontbase_adapter.rb', line 713

def columns(table_name, name = nil)#:nodoc:
  sql = <<-SQL
    SELECT   "TABLE_NAME", "COLUMN_NAME", ORDINAL_POSITION, IS_NULLABLE, COLUMN_DEFAULT, 
             DATA_TYPE, DATA_TYPE_CODE, CHARACTER_MAXIMUM_LENGTH, NUMERIC_PRECISION, 
             NUMERIC_PRECISION_RADIX, NUMERIC_SCALE, DATETIME_PRECISION, DATETIME_PRECISION_LEADING 
    FROM     INFORMATION_SCHEMA.TABLES               T0, 
             INFORMATION_SCHEMA.COLUMNS              T1, 
             INFORMATION_SCHEMA.DATA_TYPE_DESCRIPTOR T3 
    WHERE    "TABLE_NAME" = '#{table_name}' 
      AND    T0.TABLE_PK  = T1.TABLE_PK 
      AND    T0.TABLE_PK  = T3.TABLE_OR_DOMAIN_PK 
      AND    T1.COLUMN_PK = T3.COLUMN_NAME_PK 
    ORDER BY T1.ORDINAL_POSITION
  SQL

  rawresults = query(sql,name)
  columns = []
  rawresults.each do |field|
    args = [base       = field[0],
            name       = field[1],
            typecode   = field[6],
            typestring = field[5],
            limit      = field[7],
            precision  = field[8],
            scale      = field[9],
            default    = field[4],
            nullable   = field[3]]
    columns << FrontBaseColumn.new(*args)
   end
  columns
end

#commit_db_transactionObject

:nodoc:



555
556
557
558
559
# File 'lib/active_record/connection_adapters/frontbase_adapter.rb', line 555

def commit_db_transaction #:nodoc:
  execute "COMMIT"
ensure
  execute "SET COMMIT TRUE"
end

#create_database(name) ⇒ Object

:nodoc:



652
653
654
# File 'lib/active_record/connection_adapters/frontbase_adapter.rb', line 652

def create_database(name) #:nodoc:
  execute "CREATE DATABASE #{name}"
end

#create_table(name, options = {}) ⇒ Object



745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
# File 'lib/active_record/connection_adapters/frontbase_adapter.rb', line 745

def create_table(name, options = {})
  table_definition = TableDefinition.new(self)
  table_definition.primary_key(options[:primary_key] || "id") unless options[:id] == false

  yield table_definition

  if options[:force]
    drop_table(name) rescue nil
  end

  create_sql = "CREATE#{' TEMPORARY' if options[:temporary]} TABLE "
  create_sql << "#{name} ("
  create_sql << table_definition.to_sql
  create_sql << ") #{options[:options]}"
  begin_db_transaction
  execute create_sql
  commit_db_transaction
  rescue ActiveRecord::StatementInvalid => e
    raise e unless e.message.match(/Table name - \w* - exists/)
end

#current_databaseObject



660
661
662
# File 'lib/active_record/connection_adapters/frontbase_adapter.rb', line 660

def current_database
  select_value('SELECT "CATALOG_NAME" FROM INFORMATION_SCHEMA.CATALOGS').downcase
end

#default_sequence_name(table, column) ⇒ Object



611
612
613
# File 'lib/active_record/connection_adapters/frontbase_adapter.rb', line 611

def default_sequence_name(table, column)
  table
end

#disconnect!Object

Close this connection



444
445
446
447
# File 'lib/active_record/connection_adapters/frontbase_adapter.rb', line 444

def disconnect!
  @connection.close rescue nil
  @active = false
end

#drop_database(name) ⇒ Object

:nodoc:



656
657
658
# File 'lib/active_record/connection_adapters/frontbase_adapter.rb', line 656

def drop_database(name) #:nodoc:
  execute "DROP DATABASE #{name}"
end

#drop_table(name, options = {}) ⇒ Object

Drops a table from the database.



781
782
783
784
785
# File 'lib/active_record/connection_adapters/frontbase_adapter.rb', line 781

def drop_table(name, options = {})
  execute "DROP TABLE #{name} RESTRICT"
rescue ActiveRecord::StatementInvalid => e
  raise e unless e.message.match(/Referenced TABLE - \w* - does not exist/)
end

#execute(sql, name = nil) ⇒ Object

:nodoc:



511
512
513
514
515
516
517
518
519
520
# File 'lib/active_record/connection_adapters/frontbase_adapter.rb', line 511

def execute(sql, name = nil) #:nodoc:
  fbsql = cleanup_fb_sql(sql)
  puts "SQL(execute) -> #{fbsql}"  if FB_TRACE
  log(fbsql, name) { @connection.query(fbsql) }
rescue ActiveRecord::StatementInvalid => e
  if e.message.scan(/Table name - \w* - exists/).empty?
    puts "FB Exception: #{e.inspect}" if FB_TRACE
    raise e
  end
end

#indexes(table_name, name = nil) ⇒ Object

:nodoc:



674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
# File 'lib/active_record/connection_adapters/frontbase_adapter.rb', line 674

def indexes(table_name, name = nil)#:nodoc:
  indexes = []
  current_index = nil
  sql = <<-SQL
    SELECT   INDEX_NAME, T2.ORDINAL_POSITION, INDEX_COLUMN_COUNT, INDEX_TYPE, 
             "COLUMN_NAME", IS_NULLABLE 
    FROM     INFORMATION_SCHEMA.TABLES             AS T0, 
             INFORMATION_SCHEMA.INDEXES            AS T1, 
             INFORMATION_SCHEMA.INDEX_COLUMN_USAGE AS T2, 
             INFORMATION_SCHEMA.COLUMNS            AS T3 
    WHERE    T0."TABLE_NAME" = '#{table_name}' 
      AND    INDEX_TYPE <> 0 
      AND    T0.TABLE_PK   = T1.TABLE_PK 
      AND    T0.TABLE_PK   = T2.TABLE_PK 
      AND    T0.TABLE_PK   = T3.TABLE_PK 
      AND    T1.INDEXES_PK = T2.INDEX_PK 
      AND    T2.COLUMN_PK  = T3.COLUMN_PK 
    ORDER BY INDEX_NAME, T2.ORDINAL_POSITION
  SQL

  columns = []
  query(sql).each do |row|
    index_name   = row[0]
    ord_position = row[1]
    ndx_colcount = row[2]
    index_type   = row[3]
    column_name  = row[4]
    
    is_unique = index_type == 1
    
    columns << column_name
    if ord_position == ndx_colcount
      indexes << IndexDefinition.new(table_name, index_name, is_unique , columns)
      columns = []
    end
  end
  indexes
end

#insert(sql, name = nil, pk = nil, id_value = nil, sequence_name = nil) ⇒ Object

Returns the last auto-generated ID from the affected table.



523
524
525
526
527
# File 'lib/active_record/connection_adapters/frontbase_adapter.rb', line 523

def insert(sql, name = nil, pk = nil, id_value = nil, sequence_name = nil) #:nodoc:
  puts "SQL -> #{sql.inspect}"  if FB_TRACE
  execute(sql, name)
  id_value || pk
end

#native_database_typesObject

:nodoc:



274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
# File 'lib/active_record/connection_adapters/frontbase_adapter.rb', line 274

def native_database_types #:nodoc:
  {
    :primary_key    => "INTEGER DEFAULT UNIQUE PRIMARY KEY",
    :string         => { :name => "VARCHAR", :limit => 255 },
    :text           => { :name => "CLOB" },
    :integer        => { :name => "INTEGER" },
    :float          => { :name => "FLOAT" },
    :decimal        => { :name => "DECIMAL" },
    :datetime       => { :name => "TIMESTAMP" },
    :timestamp      => { :name => "TIMESTAMP" },
    :time           => { :name => "TIME" },
    :date           => { :name => "DATE" },
    :binary         => { :name => "BLOB" },
    :boolean        => { :name => "BOOLEAN" },
    :twelvebytekey  => { :name => "BYTE", :limit => 12}
  }
end

#next_sequence_value(sequence_name) ⇒ Object

Returns the next sequence value from a sequence generator. Not generally called directly; used by ActiveRecord to get the next primary key value when inserting a new database record (see #prefetch_primary_key?).



605
606
607
608
609
# File 'lib/active_record/connection_adapters/frontbase_adapter.rb', line 605

def next_sequence_value(sequence_name)
  unique = select_value("SELECT UNIQUE FROM #{sequence_name}","Next Sequence Value")
  # The test cases cannot handle a zero primary key
  unique.zero? ? select_value("SELECT UNIQUE FROM #{sequence_name}","Next Sequence Value") : unique
end

#prefetch_primary_key?(table_name = nil) ⇒ Boolean

Returns:

  • (Boolean)


598
599
600
# File 'lib/active_record/connection_adapters/frontbase_adapter.rb', line 598

def prefetch_primary_key?(table_name = nil)
  true
end

#query(sql, name = nil) ⇒ Object

:nodoc:



502
503
504
505
506
507
508
509
# File 'lib/active_record/connection_adapters/frontbase_adapter.rb', line 502

def query(sql, name = nil) #:nodoc:
  fbsql = cleanup_fb_sql(sql)
  puts "SQL(query) -> #{fbsql}"  if FB_TRACE
  log(fbsql, name) { @connection.query(fbsql) }
rescue => e
  puts "FB Exception: #{e.inspect}" if FB_TRACE
  raise e
end

#quote(value, column = nil) ⇒ Object

Quotes the column value to help prevent SQL injection attacks.



297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
# File 'lib/active_record/connection_adapters/frontbase_adapter.rb', line 297

def quote(value, column = nil)
  return value.quoted_id if value.respond_to?(:quoted_id)

  retvalue = "<INVALID>"

  puts "quote(#{value.inspect}(#{value.class}),#{column.type.inspect})" if FB_TRACE
  # If a column was passed in, use column type information
  unless value.nil?
    if column
      retvalue = case column.type
        when :string
          if value.kind_of?(String)
            "'#{quote_string(value.to_s)}'" # ' (for ruby-mode)
          else
            "'#{quote_string(value.to_yaml)}'"
          end
        when :integer
          if value.kind_of?(TrueClass)
            '1'
          elsif value.kind_of?(FalseClass)
            '0'
          else
             value.to_i.to_s
          end
        when :float
          value.to_f.to_s
        when :decimal
          value.to_d.to_s("F")
        when :datetime, :timestamp
          "TIMESTAMP '#{value.strftime("%Y-%m-%d %H:%M:%S")}'"
        when :time
          "TIME '#{value.strftime("%H:%M:%S")}'"
        when :date
          "DATE '#{value.strftime("%Y-%m-%d")}'"
        when :twelvebytekey
          value = value.to_s.unpack("H*").first unless value.kind_of?(TwelveByteKey)
          "X'#{value.to_s}'"
        when :boolean
          value = quoted_true if value.kind_of?(TrueClass)
          value = quoted_false if value.kind_of?(FalseClass)
          value
        when :binary
          blob_handle = @connection.create_blob(value.to_s)
          puts "SQL -> Insert #{value.to_s.length} byte blob as #{retvalue}" if FB_TRACE
          blob_handle.handle
        when :text
          if value.kind_of?(String)
            clobdata = value.to_s # ' (for ruby-mode)
          else
            clobdata = value.to_yaml
          end
          clob_handle = @connection.create_clob(clobdata)
          puts "SQL -> Insert #{value.to_s.length} byte clob as #{retvalue}" if FB_TRACE
          clob_handle.handle
        else
          raise "*** UNKNOWN TYPE: #{column.type.inspect}"
      end # case
    # Since we don't have column type info, make a best guess based
    # on the Ruby class of the value
    else
      retvalue = case value
        when ActiveRecord::ConnectionAdapters::TwelveByteKey
          s = value.unpack("H*").first
          "X'#{s}'"
        when String
          if column && column.type == :binary
            s = value.unpack("H*").first
            "X'#{s}'"
          elsif column && [:integer, :float, :decimal].include?(column.type) 
            value.to_s
          else
            "'#{quote_string(value)}'" # ' (for ruby-mode)
          end
        when NilClass
          "NULL"
        when TrueClass
          (column && column.type == :integer ? '1' : quoted_true)
        when FalseClass
          (column && column.type == :integer ? '0' : quoted_false)
        when Float, Fixnum, Bignum, BigDecimal
          value.to_s
        when Time, Date, DateTime
          if column
            case column.type
              when :date
                "DATE '#{value.strftime("%Y-%m-%d")}'"
              when :time
                "TIME '#{value.strftime("%H:%M:%S")}'"
              when :timestamp
                "TIMESTAMP '#{value.strftime("%Y-%m-%d %H:%M:%S")}'"
            else
              raise NotImplementedError, "Unknown column type!"
            end # case
          else # Column wasn't passed in, so try to guess the right type
            if value.kind_of? Date
              "DATE '#{value.strftime("%Y-%m-%d")}'"
            else
              if [:hour, :min, :sec].all? {|part| value.send(:part).zero? }
                "TIME '#{value.strftime("%H:%M:%S")}'"
              else
                "TIMESTAMP '#{quoted_date(value)}'"
              end
            end 
          end #if column
        else 
          "'#{quote_string(value.to_yaml)}'"
      end #case
    end
  else
    retvalue = "NULL"
  end
   
  retvalue
end

#quote_column_name(name) ⇒ Object

:nodoc:



417
418
419
# File 'lib/active_record/connection_adapters/frontbase_adapter.rb', line 417

def quote_column_name(name) #:nodoc:
  %( "#{name}" )
end

#quote_string(s) ⇒ Object

Quotes a string, escaping any ‘ (single quote) characters.



413
414
415
# File 'lib/active_record/connection_adapters/frontbase_adapter.rb', line 413

def quote_string(s)
  s.gsub(/'/, "''") # ' (for ruby-mode)
end

#quoted_falseObject



425
426
427
# File 'lib/active_record/connection_adapters/frontbase_adapter.rb', line 425

def quoted_false
  "false"
end

#quoted_trueObject



421
422
423
# File 'lib/active_record/connection_adapters/frontbase_adapter.rb', line 421

def quoted_true
  "true"
end

#reconnect!Object



438
439
440
441
# File 'lib/active_record/connection_adapters/frontbase_adapter.rb', line 438

def reconnect!
  @connection.close rescue nil
  @connection = FBSQL_Connect.connect(*@connection_options.first(7))
end

#recreate_database(name) ⇒ Object

:nodoc:



647
648
649
650
# File 'lib/active_record/connection_adapters/frontbase_adapter.rb', line 647

def recreate_database(name) #:nodoc:
  drop_database(name)
  create_database(name)
end

#remove_column(table_name, column_name) ⇒ Object

Removes the column from the table definition.

Examples
remove_column(:suppliers, :qualification)


810
811
812
# File 'lib/active_record/connection_adapters/frontbase_adapter.rb', line 810

def remove_column(table_name, column_name)
  execute "ALTER TABLE #{table_name} DROP #{column_name} RESTRICT"
end

#remove_index(table_name, options = {}) ⇒ Object

:nodoc:



814
815
816
817
818
819
820
# File 'lib/active_record/connection_adapters/frontbase_adapter.rb', line 814

def remove_index(table_name, options = {}) #:nodoc:
  if options[:unique]
    execute "ALTER TABLE #{table_name} DROP CONSTRAINT #{quote_column_name(index_name(table_name, options))} RESTRICT"
  else
    execute "DROP INDEX #{quote_column_name(index_name(table_name, options))}"
  end
end

#rename_column(table_name, column_name, new_column_name) ⇒ Object

:nodoc:



846
847
848
# File 'lib/active_record/connection_adapters/frontbase_adapter.rb', line 846

def rename_column(table_name, column_name, new_column_name) #:nodoc:
  execute %( ALTER COLUMN NAME "#{table_name}"."#{column_name}" TO "#{new_column_name}" )
end

#rename_table(name, new_name) ⇒ Object



766
767
768
769
770
771
772
773
774
775
776
777
778
# File 'lib/active_record/connection_adapters/frontbase_adapter.rb', line 766

def rename_table(name, new_name)
  columns = columns(name)
  pkcol = columns.find {|c| c.fb_autogen}
  execute "ALTER TABLE NAME #{name} TO #{new_name}"
  if pkcol
    change_column_default(new_name,pkcol.name,"UNIQUE")
    begin_db_transaction
    mpk = select_value("SELECT MAX(#{pkcol.name}) FROM #{new_name}")
    mpk = 0 if mpk.nil?
    execute "SET UNIQUE=#{mpk} FOR #{new_name}"
    commit_db_transaction
  end
end

#reset_pk_sequence!(table, pk = nil, sequence = nil) ⇒ Object



629
630
631
632
633
634
635
636
637
# File 'lib/active_record/connection_adapters/frontbase_adapter.rb', line 629

def reset_pk_sequence!(table, pk = nil, sequence = nil)
  klasses = classes_for_table_name(table)
  klass   = klasses.nil? ? nil : klasses.first
  pk      = klass.primary_key unless klass.nil?
  if pk && klass.columns_hash[pk].type == :integer
    mpk = select_value("SELECT MAX(#{pk}) FROM #{table}")
    execute("SET UNIQUE FOR #{klass.table_name}(#{pk})")
  end
end

#reset_sequence!(table, column, sequence = nil) ⇒ Object

Set the sequence to the max value of the table’s column.



616
617
618
619
620
621
622
623
# File 'lib/active_record/connection_adapters/frontbase_adapter.rb', line 616

def reset_sequence!(table, column, sequence = nil)
  klasses = classes_for_table_name(table)
  klass   = klasses.nil? ? nil : klasses.first
  pk      = klass.primary_key unless klass.nil?
  if pk && klass.columns_hash[pk].type == :integer
    execute("SET UNIQUE FOR #{klass.table_name}(#{pk})")
  end
end

#rollback_db_transactionObject

:nodoc:



561
562
563
564
565
# File 'lib/active_record/connection_adapters/frontbase_adapter.rb', line 561

def rollback_db_transaction #:nodoc:
  execute "ROLLBACK"
ensure
  execute "SET COMMIT TRUE"
end

#select_all(sql, name = nil) ⇒ Object

Returns an array of record hashes with the column names as keys and column values as values.



453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
# File 'lib/active_record/connection_adapters/frontbase_adapter.rb', line 453

def select_all(sql, name = nil) #:nodoc:
  fbsql        = cleanup_fb_sql(sql)
  return_value = []
  fbresult     = execute(sql, name)
  puts "select_all SQL -> #{fbsql}" if FB_TRACE
  columns = fbresult.columns

  fbresult.each do |row|
    puts "SQL <- #{row.inspect}"  if FB_TRACE
    hashed_row = {}    
    colnum     = 0
    row.each do |col|
      hashed_row[columns[colnum]] = col
      if col.kind_of?(FBSQL_LOB)
        hashed_row[columns[colnum]] = col.read
      end
      colnum += 1
    end
    puts "raw row: #{hashed_row.inspect}" if FB_TRACE
    return_value << hashed_row
  end
  return_value
end

#select_one(sql, name = nil) ⇒ Object

:nodoc:



477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
# File 'lib/active_record/connection_adapters/frontbase_adapter.rb', line 477

def select_one(sql, name = nil) #:nodoc:
  fbsql        = cleanup_fb_sql(sql)
  return_value = []
  fbresult     = execute(fbsql, name)
  puts "SQL -> #{fbsql}"  if FB_TRACE
  columns = fbresult.columns
  
  fbresult.each do |row|
    puts "SQL <- #{row.inspect}"  if FB_TRACE
    hashed_row = {}    
    colnum     = 0
    row.each do |col|
      hashed_row[columns[colnum]] = col
      if col.kind_of?(FBSQL_LOB)
        hashed_row[columns[colnum]] = col.read
      end
      colnum += 1
    end
    return_value << hashed_row
    break
  end
  fbresult.clear
  return_value.first
end

#set_optimistic_transactionsObject



544
545
546
547
548
549
# File 'lib/active_record/connection_adapters/frontbase_adapter.rb', line 544

def set_optimistic_transactions
  if @transaction_mode == :pessimistic
    execute "SET TRANSACTION ISOLATION LEVEL REPEATABLE READ, READ WRITE, LOCKING OPTIMISTIC"
    @transaction_mode = :optimistic
  end
end

#set_pessimistic_transactionsObject



537
538
539
540
541
542
# File 'lib/active_record/connection_adapters/frontbase_adapter.rb', line 537

def set_pessimistic_transactions
  if @transaction_mode == :optimistic
    execute "SET TRANSACTION ISOLATION LEVEL SERIALIZABLE, LOCKING PESSIMISTIC, READ WRITE"
    @transaction_mode = :pessimistic
  end
end

#structure_dumpObject

SCHEMA STATEMENTS ========================================



641
642
643
644
645
# File 'lib/active_record/connection_adapters/frontbase_adapter.rb', line 641

def structure_dump #:nodoc:
  select_all("SHOW TABLES").inject('') do |structure, table|
    structure << select_one("SHOW CREATE TABLE #{table.to_a.first.last}")["Create Table"] << ";\n\n"
  end
end

#supports_migrations?Boolean

Does this adapter support migrations? Backend specific, as the abstract adapter always returns false.

Returns:

  • (Boolean)


270
271
272
# File 'lib/active_record/connection_adapters/frontbase_adapter.rb', line 270

def supports_migrations? #:nodoc:
  true
end

#tables(name = nil) ⇒ Object

:nodoc:



664
665
666
667
668
669
670
671
672
# File 'lib/active_record/connection_adapters/frontbase_adapter.rb', line 664

def tables(name = nil) #:nodoc:
  select_values(<<-SQL, nil)
    SELECT "TABLE_NAME" 
    FROM   INFORMATION_SCHEMA.TABLES   AS T0,
           INFORMATION_SCHEMA.SCHEMATA AS T1 
    WHERE  T0.SCHEMA_PK  = T1.SCHEMA_PK 
    AND    "SCHEMA_NAME" = CURRENT_SCHEMA
  SQL
end

#update(sql, name = nil) ⇒ Object Also known as: delete

Executes the update statement and returns the number of rows affected.



530
531
532
533
# File 'lib/active_record/connection_adapters/frontbase_adapter.rb', line 530

def update(sql, name = nil) #:nodoc:
  puts "SQL -> #{sql.inspect}"  if FB_TRACE
  execute(sql, name).num_rows
end