Class: PartitionGardener::PgConnection
- Inherits:
-
Object
- Object
- PartitionGardener::PgConnection
- Defined in:
- lib/partition_gardener/pg_connection.rb
Defined Under Namespace
Classes: Result
Class Method Summary collapse
Instance Method Summary collapse
- #execute(sql) ⇒ Object
-
#initialize(raw_connection) ⇒ PgConnection
constructor
A new instance of PgConnection.
- #quote(value) ⇒ Object
- #quote_column_name(name) ⇒ Object
- #quote_table_name(name) ⇒ Object
- #table_exists?(table_name) ⇒ Boolean
- #transaction ⇒ Object
Constructor Details
#initialize(raw_connection) ⇒ PgConnection
Returns a new instance of PgConnection.
37 38 39 |
# File 'lib/partition_gardener/pg_connection.rb', line 37 def initialize(raw_connection) @raw_connection = raw_connection end |
Class Method Details
.connect(database_url) ⇒ Object
33 34 35 |
# File 'lib/partition_gardener/pg_connection.rb', line 33 def self.connect(database_url) new(PG.connect(database_url)) end |
Instance Method Details
#execute(sql) ⇒ Object
66 67 68 |
# File 'lib/partition_gardener/pg_connection.rb', line 66 def execute(sql) Result.new(@raw_connection.exec(sql)) end |
#quote(value) ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/partition_gardener/pg_connection.rb', line 41 def quote(value) case value when nil "NULL" when true "TRUE" when false "FALSE" when Numeric value.to_s when Date, Time "'#{@raw_connection.escape_string(value.iso8601)}'" else "'#{@raw_connection.escape_string(value.to_s)}'" end end |
#quote_column_name(name) ⇒ Object
62 63 64 |
# File 'lib/partition_gardener/pg_connection.rb', line 62 def quote_column_name(name) quote_table_name(name) end |
#quote_table_name(name) ⇒ Object
58 59 60 |
# File 'lib/partition_gardener/pg_connection.rb', line 58 def quote_table_name(name) @raw_connection.quote_ident(name.to_s) end |
#table_exists?(table_name) ⇒ Boolean
79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/partition_gardener/pg_connection.rb', line 79 def table_exists?(table_name) sql = <<~SQL SELECT EXISTS ( SELECT 1 FROM pg_catalog.pg_class AS relation JOIN pg_catalog.pg_namespace AS namespace ON namespace.oid = relation.relnamespace WHERE namespace.nspname = ANY (current_schemas(false)) AND relation.relname = $1 AND relation.relkind IN ('r', 'p') ) AS exists SQL result = @raw_connection.exec_params(sql, [table_name.to_s]) result.getvalue(0, 0) == "t" end |
#transaction ⇒ Object
70 71 72 73 74 75 76 77 |
# File 'lib/partition_gardener/pg_connection.rb', line 70 def transaction @raw_connection.exec("BEGIN") yield @raw_connection.exec("COMMIT") rescue @raw_connection.exec("ROLLBACK") raise end |