Class: DB::Postgres::Connection
- Inherits:
-
Async::Pool::Resource
- Object
- Async::Pool::Resource
- DB::Postgres::Connection
- Defined in:
- lib/db/postgres/connection.rb
Overview
A high-level database connection that implements the standardized connection interface. This class provides a bridge between the underlying native PostgreSQL interface and the DB gem’s unified connection API.
Constant Summary collapse
- FEATURES =
DB::Features.new( alter_column_type: true, using_clause: true, conditional_operations: true, transactional_schema: true, batch_alter_table: true, concurrent_schema: true, serial_columns: true, )
Instance Method Summary collapse
-
#append_identifier(value, buffer = String.new) ⇒ Object
Append an escaped identifier to the buffer.
-
#append_literal(value, buffer = String.new) ⇒ Object
Append a literal value to the buffer with appropriate formatting.
-
#append_string(value, buffer = String.new) ⇒ Object
Append an escaped string value to the buffer.
-
#close ⇒ Object
Close the database connection and release resources.
-
#features ⇒ Object
Database feature detection for migration and query building.
-
#initialize(**options) ⇒ Connection
constructor
Initialize a new database connection.
-
#key_column(name = "id", primary: true, null: false) ⇒ Object
Generate a key column definition for table creation.
-
#next_result ⇒ Object
Get the next result set from a multi-result query.
-
#send_query(statement) ⇒ Object
Send a query to the database server.
-
#status ⇒ Object
Get the current connection status.
-
#types ⇒ Object
Get the type mapping for database types.
Constructor Details
#initialize(**options) ⇒ Connection
Initialize a new database connection.
17 18 19 20 21 |
# File 'lib/db/postgres/connection.rb', line 17 def initialize(**) @native = Native::Connection.connect(**) super() end |
Instance Method Details
#append_identifier(value, buffer = String.new) ⇒ Object
Append an escaped identifier to the buffer.
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/db/postgres/connection.rb', line 76 def append_identifier(value, buffer = String.new) case value when Array first = true value.each do |part| buffer << "." unless first first = false buffer << @native.escape_identifier(part) end else buffer << @native.escape_identifier(value) end return buffer end |
#append_literal(value, buffer = String.new) ⇒ Object
Append a literal value to the buffer with appropriate formatting.
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/db/postgres/connection.rb', line 53 def append_literal(value, buffer = String.new) case value when Time, DateTime, Date append_string(value.iso8601, buffer) when Numeric buffer << value.to_s when TrueClass buffer << "TRUE" when FalseClass buffer << "FALSE" when nil buffer << "NULL" else append_string(value, buffer) end return buffer end |
#append_string(value, buffer = String.new) ⇒ Object
Append an escaped string value to the buffer.
43 44 45 46 47 |
# File 'lib/db/postgres/connection.rb', line 43 def append_string(value, buffer = String.new) buffer << @native.escape_literal(value) return buffer end |
#close ⇒ Object
Close the database connection and release resources.
24 25 26 27 28 29 30 31 |
# File 'lib/db/postgres/connection.rb', line 24 def close if @native @native&.close @native = nil end super end |
#features ⇒ Object
Database feature detection for migration and query building.
150 151 152 |
# File 'lib/db/postgres/connection.rb', line 150 def features FEATURES end |
#key_column(name = "id", primary: true, null: false) ⇒ Object
Generate a key column definition for table creation.
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/db/postgres/connection.rb', line 98 def key_column(name = "id", primary: true, null: false) buffer = String.new append_identifier(name, buffer) if primary buffer << " BIGSERIAL" else buffer << " BIGINT" end if primary buffer << " PRIMARY KEY" elsif !null buffer << " NOT NULL" end return buffer end |
#next_result ⇒ Object
Get the next result set from a multi-result query.
134 135 136 |
# File 'lib/db/postgres/connection.rb', line 134 def next_result @native.next_result end |
#send_query(statement) ⇒ Object
Send a query to the database server.
126 127 128 129 130 |
# File 'lib/db/postgres/connection.rb', line 126 def send_query(statement) @native.discard_results @native.send_query(statement) end |
#status ⇒ Object
Get the current connection status.
120 121 122 |
# File 'lib/db/postgres/connection.rb', line 120 def status @native.status end |
#types ⇒ Object
Get the type mapping for database types.
35 36 37 |
# File 'lib/db/postgres/connection.rb', line 35 def types @native.types end |