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



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

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

#begin_transactionObject



68
69
70
# File 'lib/tina4/drivers/firebird_driver.rb', line 68

def begin_transaction
  @transaction = @connection.transaction
end

#closeObject



32
33
34
# File 'lib/tina4/drivers/firebird_driver.rb', line 32

def close
  @connection&.close
end

#columns(table_name) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/tina4/drivers/firebird_driver.rb', line 86

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



72
73
74
# File 'lib/tina4/drivers/firebird_driver.rb', line 72

def commit
  @transaction&.commit
end

#connect(connection_string, username: nil, password: nil) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/tina4/drivers/firebird_driver.rb', line 8

def connect(connection_string, username: nil, password: nil)
  require "fb"
  require "uri"
  uri = URI.parse(connection_string)
  host = uri.host
  port = uri.port || 3050
  db_path = uri.path&.sub(/^\//, "")
  db_user = username || uri.user
  db_pass = password || uri.password

  database = if host
               "#{host}/#{port}:#{db_path}"
             else
               db_path || connection_string.sub(/^firebird:\/\//, "")
             end

  opts = { database: database }
  opts[:username] = db_user if db_user
  opts[:password] = db_pass if db_pass
  @connection = Fb::Database.new(**opts).connect
rescue LoadError
  raise "Firebird driver requires the 'fb' gem. Install it with: gem install fb"
end

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



44
45
46
47
48
49
50
# File 'lib/tina4/drivers/firebird_driver.rb', line 44

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

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



36
37
38
39
40
41
42
# File 'lib/tina4/drivers/firebird_driver.rb', line 36

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



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

def last_insert_id
  nil
end

#placeholderObject



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

def placeholder
  "?"
end

#placeholders(count) ⇒ Object



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

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

#rollbackObject



76
77
78
# File 'lib/tina4/drivers/firebird_driver.rb', line 76

def rollback
  @transaction&.rollback
end

#tablesObject



80
81
82
83
84
# File 'lib/tina4/drivers/firebird_driver.rb', line 80

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