Class: Tina4::Drivers::PostgresDriver
- Inherits:
-
Object
- Object
- Tina4::Drivers::PostgresDriver
- Defined in:
- lib/tina4/drivers/postgres_driver.rb
Instance Attribute Summary collapse
-
#connection ⇒ Object
readonly
Returns the value of attribute connection.
Instance Method Summary collapse
- #apply_limit(sql, limit, offset = 0) ⇒ Object
- #begin_transaction ⇒ Object
- #close ⇒ Object
- #columns(table_name) ⇒ Object
- #commit ⇒ Object
- #connect(connection_string) ⇒ Object
- #execute(sql, params = []) ⇒ Object
- #execute_query(sql, params = []) ⇒ Object
- #last_insert_id ⇒ Object
- #placeholder ⇒ Object
- #placeholders(count) ⇒ Object
- #rollback ⇒ Object
- #tables ⇒ Object
Instance Attribute Details
#connection ⇒ Object (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
51 52 53 |
# File 'lib/tina4/drivers/postgres_driver.rb', line 51 def apply_limit(sql, limit, offset = 0) "#{sql} LIMIT #{limit} OFFSET #{offset}" end |
#begin_transaction ⇒ Object
55 56 57 |
# File 'lib/tina4/drivers/postgres_driver.rb', line 55 def begin_transaction @connection.exec("BEGIN") end |
#close ⇒ Object
13 14 15 |
# File 'lib/tina4/drivers/postgres_driver.rb', line 13 def close @connection&.close end |
#columns(table_name) ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/tina4/drivers/postgres_driver.rb', line 73 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 |
#commit ⇒ Object
59 60 61 |
# File 'lib/tina4/drivers/postgres_driver.rb', line 59 def commit @connection.exec("COMMIT") end |
#connect(connection_string) ⇒ Object
8 9 10 11 |
# File 'lib/tina4/drivers/postgres_driver.rb', line 8 def connect(connection_string) require "pg" @connection = PG.connect(connection_string) end |
#execute(sql, params = []) ⇒ Object
27 28 29 30 31 32 33 34 |
# File 'lib/tina4/drivers/postgres_driver.rb', line 27 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
17 18 19 20 21 22 23 24 25 |
# File 'lib/tina4/drivers/postgres_driver.rb', line 17 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_id ⇒ Object
36 37 38 39 40 41 |
# File 'lib/tina4/drivers/postgres_driver.rb', line 36 def last_insert_id result = @connection.exec("SELECT lastval()") result.first["lastval"].to_i rescue PG::Error nil end |
#placeholder ⇒ Object
43 44 45 |
# File 'lib/tina4/drivers/postgres_driver.rb', line 43 def placeholder "?" end |
#placeholders(count) ⇒ Object
47 48 49 |
# File 'lib/tina4/drivers/postgres_driver.rb', line 47 def placeholders(count) (1..count).map { |i| "$#{i}" }.join(", ") end |
#rollback ⇒ Object
63 64 65 |
# File 'lib/tina4/drivers/postgres_driver.rb', line 63 def rollback @connection.exec("ROLLBACK") end |
#tables ⇒ Object
67 68 69 70 71 |
# File 'lib/tina4/drivers/postgres_driver.rb', line 67 def tables sql = "SELECT tablename FROM pg_tables WHERE schemaname = 'public'" rows = execute_query(sql) rows.map { |r| r[:tablename] } end |