Class: Tina4::Drivers::FirebirdDriver

Inherits:
Object
  • Object
show all
Defined in:
lib/tina4/drivers/firebird_driver.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#connectionObject (readonly)

Returns the value of attribute connection.



6
7
8
# File 'lib/tina4/drivers/firebird_driver.rb', line 6

def connection
  @connection
end

Instance Method Details

#apply_limit(sql, limit, offset = 0) ⇒ Object



48
49
50
# File 'lib/tina4/drivers/firebird_driver.rb', line 48

def apply_limit(sql, limit, offset = 0)
  "SELECT FIRST #{limit} SKIP #{offset} * FROM (#{sql})"
end

#begin_transactionObject



52
53
54
# File 'lib/tina4/drivers/firebird_driver.rb', line 52

def begin_transaction
  @transaction = @connection.transaction
end

#closeObject



16
17
18
# File 'lib/tina4/drivers/firebird_driver.rb', line 16

def close
  @connection&.close
end

#columns(table_name) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/tina4/drivers/firebird_driver.rb', line 70

def columns(table_name)
  sql = "SELECT RF.RDB\$FIELD_NAME, F.RDB\$FIELD_TYPE, RF.RDB\$NULL_FLAG, RF.RDB\$DEFAULT_SOURCE " \
        "FROM RDB\$RELATION_FIELDS RF " \
        "JOIN RDB\$FIELDS F ON RF.RDB\$FIELD_SOURCE = F.RDB\$FIELD_NAME " \
        "WHERE RF.RDB\$RELATION_NAME = ?"
  rows = execute_query(sql, [table_name.upcase])
  rows.map do |r|
    {
      name: (r["RDB\$FIELD_NAME"] || r["rdb\$field_name"] || "").strip,
      type: r["RDB\$FIELD_TYPE"] || r["rdb\$field_type"],
      nullable: (r["RDB\$NULL_FLAG"] || r["rdb\$null_flag"]).nil?,
      default: r["RDB\$DEFAULT_SOURCE"] || r["rdb\$default_source"],
      primary_key: false
    }
  end
end

#commitObject



56
57
58
# File 'lib/tina4/drivers/firebird_driver.rb', line 56

def commit
  @transaction&.commit
end

#connect(connection_string) ⇒ Object



8
9
10
11
12
13
14
# File 'lib/tina4/drivers/firebird_driver.rb', line 8

def connect(connection_string)
  require "fb"
  db_path = connection_string.sub(/^firebird:\/\//, "")
  @connection = Fb::Database.new(database: db_path).connect
rescue LoadError
  raise "Firebird driver requires the 'fb' gem. Install it with: gem install fb"
end

#execute(sql, params = []) ⇒ Object



28
29
30
31
32
33
34
# File 'lib/tina4/drivers/firebird_driver.rb', line 28

def execute(sql, params = [])
  if params.empty?
    @connection.execute(sql)
  else
    @connection.execute(sql, *params)
  end
end

#execute_query(sql, params = []) ⇒ Object



20
21
22
23
24
25
26
# File 'lib/tina4/drivers/firebird_driver.rb', line 20

def execute_query(sql, params = [])
  if params.empty?
    @connection.query(:hash, sql)
  else
    @connection.query(:hash, sql, *params)
  end.map { |row| stringify_keys(row) }
end

#last_insert_idObject



36
37
38
# File 'lib/tina4/drivers/firebird_driver.rb', line 36

def last_insert_id
  nil
end

#placeholderObject



40
41
42
# File 'lib/tina4/drivers/firebird_driver.rb', line 40

def placeholder
  "?"
end

#placeholders(count) ⇒ Object



44
45
46
# File 'lib/tina4/drivers/firebird_driver.rb', line 44

def placeholders(count)
  (["?"] * count).join(", ")
end

#rollbackObject



60
61
62
# File 'lib/tina4/drivers/firebird_driver.rb', line 60

def rollback
  @transaction&.rollback
end

#tablesObject



64
65
66
67
68
# File 'lib/tina4/drivers/firebird_driver.rb', line 64

def tables
  sql = "SELECT RDB\$RELATION_NAME FROM RDB\$RELATIONS WHERE RDB\$SYSTEM_FLAG = 0 AND RDB\$VIEW_BLR IS NULL"
  rows = execute_query(sql)
  rows.map { |r| (r["RDB\$RELATION_NAME"] || r["rdb\$relation_name"] || "").strip }
end