Class: Dexter::Connection
- Inherits:
-
Object
- Object
- Dexter::Connection
- Includes:
- Logging
- Defined in:
- lib/dexter/connection.rb
Constant Summary
Constants included from Logging
Instance Method Summary collapse
- #execute(query, pretty: true, params: [], use_exec: false) ⇒ Object
-
#initialize(dbname:, host:, port:, username:, log_sql:) ⇒ Connection
constructor
A new instance of Connection.
- #quote_ident(value) ⇒ Object
- #server_version_num ⇒ Object
- #setup(enable_hypopg) ⇒ Object
Methods included from Logging
Constructor Details
#initialize(dbname:, host:, port:, username:, log_sql:) ⇒ Connection
Returns a new instance of Connection.
5 6 7 8 9 10 11 12 |
# File 'lib/dexter/connection.rb', line 5 def initialize(dbname:, host:, port:, username:, log_sql:) @dbname = dbname @host = host @port = port @username = username @log_sql = log_sql @mutex = Mutex.new end |
Instance Method Details
#execute(query, pretty: true, params: [], use_exec: false) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/dexter/connection.rb', line 24 def execute(query, pretty: true, params: [], use_exec: false) # use exec_params instead of exec when possible for security # # Unlike PQexec, PQexecParams allows at most one SQL command in the given string. # (There can be semicolons in it, but not more than one nonempty command.) # This is a limitation of the underlying protocol, but has some usefulness # as an extra defense against SQL-injection attacks. # https://www.postgresql.org/docs/current/static/libpq-exec.html query = squish(query) if pretty log colorize("[sql] #{query}#{params.any? ? " /*#{params.to_json}*/" : ""}", :cyan) if @log_sql @mutex.synchronize do if use_exec conn.exec("#{query} /*dexter*/").to_a else conn.exec_params("#{query} /*dexter*/", params).to_a end end end |
#quote_ident(value) ⇒ Object
44 45 46 |
# File 'lib/dexter/connection.rb', line 44 def quote_ident(value) value.split(".").map { |v| conn.quote_ident(v) }.join(".") end |
#server_version_num ⇒ Object
48 49 50 |
# File 'lib/dexter/connection.rb', line 48 def server_version_num @server_version_num ||= execute("SHOW server_version_num").first["server_version_num"].to_i end |
#setup(enable_hypopg) ⇒ Object
14 15 16 17 18 19 20 21 22 |
# File 'lib/dexter/connection.rb', line 14 def setup(enable_hypopg) if server_version_num < 130000 raise Error, "This version of Dexter requires Postgres 13+" end check_extension(enable_hypopg) execute("SET lock_timeout = '5s'") end |