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



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

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

#begin_transactionObject



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

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



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

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



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

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



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

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
43
# File 'lib/tina4/drivers/firebird_driver.rb', line 36

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

#last_insert_idObject



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

def last_insert_id
  nil
end

#placeholderObject



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

def placeholder
  "?"
end

#placeholders(count) ⇒ Object



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

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

#rollbackObject



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

def rollback
  @transaction&.rollback
end

#tablesObject



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

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