Class: PGlite::Connection
- Inherits:
-
Object
- Object
- PGlite::Connection
- Defined in:
- lib/pglite/connection.rb
Instance Attribute Summary collapse
-
#mount_path ⇒ Object
readonly
Returns the value of attribute mount_path.
Instance Method Summary collapse
- #close ⇒ Object
- #discard_results ⇒ Object
- #escape(str) ⇒ Object
- #exec(sql) ⇒ Object (also: #query, #async_exec, #async_query)
- #exec_params(sql, params) ⇒ Object
- #exec_prepared(name, params) ⇒ Object
- #finished? ⇒ Boolean
- #get_last_result ⇒ Object
- #get_result ⇒ Object
-
#initialize(conn_params = {}) ⇒ Connection
constructor
A new instance of Connection.
- #parameter_status(name) ⇒ Object
- #prepare(name, sql, param_types = nil) ⇒ Object
- #raw_query(sql, params) ⇒ Object
- #reset ⇒ Object
- #send_prepare(name, sql, param_types = nil) ⇒ Object
-
#send_query(sql) ⇒ Object
Async interface for Active Record 8.2+ ===.
- #send_query_params(sql, params) ⇒ Object
- #send_query_prepared(name, params) ⇒ Object
- #server_version ⇒ Object
- #set_client_encoding(_) ⇒ Object
- #set_notice_receiver(&block) ⇒ Object
- #status ⇒ Object
- #transaction_status ⇒ Object
Constructor Details
#initialize(conn_params = {}) ⇒ Connection
Returns a new instance of Connection.
10 11 12 13 14 15 16 17 |
# File 'lib/pglite/connection.rb', line 10 def initialize(conn_params = {}) @mount_path = conn_params[:mount_path] || File.join(Dir.pwd, "tmp", "pglite") @prepared_statements_map = {} @closed = false PGlite.install!(mount_path) rescue => e raise PG::Error, e. end |
Instance Attribute Details
#mount_path ⇒ Object (readonly)
Returns the value of attribute mount_path.
8 9 10 |
# File 'lib/pglite/connection.rb', line 8 def mount_path @mount_path end |
Instance Method Details
#close ⇒ Object
127 128 129 130 |
# File 'lib/pglite/connection.rb', line 127 def close @closed = true nil end |
#discard_results ⇒ Object
103 104 105 |
# File 'lib/pglite/connection.rb', line 103 def discard_results @pending_result = nil end |
#escape(str) ⇒ Object
36 |
# File 'lib/pglite/connection.rb', line 36 def escape(str) = str |
#exec(sql) ⇒ Object Also known as: query, async_exec, async_query
52 53 54 |
# File 'lib/pglite/connection.rb', line 52 def exec(sql) raw_query(sql, []) end |
#exec_params(sql, params) ⇒ Object
60 61 62 63 64 65 |
# File 'lib/pglite/connection.rb', line 60 def exec_params(sql, params) if params.empty? return exec(sql) end raw_query(sql, params) end |
#exec_prepared(name, params) ⇒ Object
67 68 69 70 |
# File 'lib/pglite/connection.rb', line 67 def exec_prepared(name, params) sql = @prepared_statements_map[name] exec_params(sql, params) end |
#finished? ⇒ Boolean
23 |
# File 'lib/pglite/connection.rb', line 23 def finished? = @closed |
#get_last_result ⇒ Object
111 112 113 |
# File 'lib/pglite/connection.rb', line 111 def get_last_result @last_result end |
#get_result ⇒ Object
98 99 100 101 |
# File 'lib/pglite/connection.rb', line 98 def get_result result, @pending_result = @pending_result, nil result end |
#parameter_status(name) ⇒ Object
29 30 31 32 33 34 |
# File 'lib/pglite/connection.rb', line 29 def parameter_status(name) # Rails can query for the current time zone return "UTC" if name.to_s.casecmp?("timezone") nil end |
#prepare(name, sql, param_types = nil) ⇒ Object
72 73 74 |
# File 'lib/pglite/connection.rb', line 72 def prepare(name, sql, param_types = nil) @prepared_statements_map[name] = sql end |
#raw_query(sql, params) ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/pglite/connection.rb', line 38 def raw_query(sql, params) # FIXME: we need to add support for prepared statements at the extension level if params.present? raise ArgumentError, "Prepared statements are not supported in pglite" end raw_res = PGlite.exec_query(sql) result = Result.new(raw_res) @last_result = result result rescue => e raise PG::Error, e. end |
#reset ⇒ Object
115 116 117 |
# File 'lib/pglite/connection.rb', line 115 def reset @prepared_statements_map = {} end |
#send_prepare(name, sql, param_types = nil) ⇒ Object
92 93 94 95 96 |
# File 'lib/pglite/connection.rb', line 92 def send_prepare(name, sql, param_types = nil) prepare(name, sql, param_types) @pending_result = nil nil end |
#send_query(sql) ⇒ Object
Async interface for Active Record 8.2+ ===
77 78 79 80 |
# File 'lib/pglite/connection.rb', line 77 def send_query(sql) @pending_result = raw_query(sql, []) nil end |
#send_query_params(sql, params) ⇒ Object
82 83 84 85 |
# File 'lib/pglite/connection.rb', line 82 def send_query_params(sql, params) @pending_result = raw_query(sql, params) nil end |
#send_query_prepared(name, params) ⇒ Object
87 88 89 90 |
# File 'lib/pglite/connection.rb', line 87 def send_query_prepared(name, params) @pending_result = exec_prepared(name, params) nil end |
#server_version ⇒ Object
119 120 121 122 123 124 125 |
# File 'lib/pglite/connection.rb', line 119 def server_version # The result is formed by multiplying the server's major version number by 10000 and adding the minor version number. # For example, version 10.1 will be returned as 100001, and version 11.0 will be returned as 110000. PGlite.database_version.match(/PostgreSQL\s(\d+)\.(\d+)/)&.then do _1[1].to_i * 10_000 + _1[2].to_i end end |
#set_client_encoding(_) ⇒ Object
19 20 21 |
# File 'lib/pglite/connection.rb', line 19 def set_client_encoding(_) # make no-op for now end |
#set_notice_receiver(&block) ⇒ Object
107 108 109 |
# File 'lib/pglite/connection.rb', line 107 def set_notice_receiver(&block) nil end |
#status ⇒ Object
132 |
# File 'lib/pglite/connection.rb', line 132 def status = PG::CONNECTION_OK |
#transaction_status ⇒ Object
25 26 27 |
# File 'lib/pglite/connection.rb', line 25 def transaction_status PG::PQTRANS_IDLE end |