Class: Tina4::Drivers::PostgresDriver

Inherits:
Object
  • Object
show all
Defined in:
lib/tina4/drivers/postgres_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/postgres_driver.rb', line 6

def connection
  @connection
end

Instance Method Details

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



58
59
60
# File 'lib/tina4/drivers/postgres_driver.rb', line 58

def apply_limit(sql, limit, offset = 0)
  "#{sql} LIMIT #{limit} OFFSET #{offset}"
end

#begin_transactionObject



62
63
64
# File 'lib/tina4/drivers/postgres_driver.rb', line 62

def begin_transaction
  @connection.exec("BEGIN")
end

#closeObject



20
21
22
# File 'lib/tina4/drivers/postgres_driver.rb', line 20

def close
  @connection&.close
end

#columns(table_name) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/tina4/drivers/postgres_driver.rb', line 80

def columns(table_name)
  sql = "SELECT column_name, data_type, is_nullable, column_default FROM information_schema.columns WHERE table_name = $1"
  rows = execute_query(sql, [table_name])
  rows.map do |r|
    {
      name: r[:column_name],
      type: r[:data_type],
      nullable: r[:is_nullable] == "YES",
      default: r[:column_default],
      primary_key: false
    }
  end
end

#commitObject



66
67
68
# File 'lib/tina4/drivers/postgres_driver.rb', line 66

def commit
  @connection.exec("COMMIT")
end

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



8
9
10
11
12
13
14
15
16
17
18
# File 'lib/tina4/drivers/postgres_driver.rb', line 8

def connect(connection_string, username: nil, password: nil)
  require "pg"
  url = connection_string
  if username || password
    uri = URI.parse(url)
    uri.user = username if username
    uri.password = password if password
    url = uri.to_s
  end
  @connection = PG.connect(url)
end

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



34
35
36
37
38
39
40
41
# File 'lib/tina4/drivers/postgres_driver.rb', line 34

def execute(sql, params = [])
  converted_sql = convert_placeholders(sql)
  if params.empty?
    @connection.exec(converted_sql)
  else
    @connection.exec_params(converted_sql, params)
  end
end

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



24
25
26
27
28
29
30
31
32
# File 'lib/tina4/drivers/postgres_driver.rb', line 24

def execute_query(sql, params = [])
  converted_sql = convert_placeholders(sql)
  result = if params.empty?
             @connection.exec(converted_sql)
           else
             @connection.exec_params(converted_sql, params)
           end
  result.map { |row| symbolize_keys(row) }
end

#last_insert_idObject



43
44
45
46
47
48
# File 'lib/tina4/drivers/postgres_driver.rb', line 43

def last_insert_id
  result = @connection.exec("SELECT lastval()")
  result.first["lastval"].to_i
rescue PG::Error
  nil
end

#placeholderObject



50
51
52
# File 'lib/tina4/drivers/postgres_driver.rb', line 50

def placeholder
  "?"
end

#placeholders(count) ⇒ Object



54
55
56
# File 'lib/tina4/drivers/postgres_driver.rb', line 54

def placeholders(count)
  (1..count).map { |i| "$#{i}" }.join(", ")
end

#rollbackObject



70
71
72
# File 'lib/tina4/drivers/postgres_driver.rb', line 70

def rollback
  @connection.exec("ROLLBACK")
end

#tablesObject



74
75
76
77
78
# File 'lib/tina4/drivers/postgres_driver.rb', line 74

def tables
  sql = "SELECT tablename FROM pg_tables WHERE schemaname = 'public'"
  rows = execute_query(sql)
  rows.map { |r| r[:tablename] }
end