Class: MessageStore::Postgres::Session
- Inherits:
-
Object
- Object
- MessageStore::Postgres::Session
- Includes:
- Dependency, Log::Dependency, Settings::Setting
- Defined in:
- lib/message_store/postgres/session.rb
Defined Under Namespace
Modules: LogText
Constant Summary collapse
- Error =
Class.new(RuntimeError)
Instance Attribute Summary collapse
-
#connection ⇒ Object
Returns the value of attribute connection.
-
#executed_time ⇒ Object
Returns the value of attribute executed_time.
Class Method Summary collapse
- .build(settings: nil) ⇒ Object
- .build_connection(instance) ⇒ Object
- .configure(receiver, session: nil, settings: nil, attr_name: nil) ⇒ Object
- .logger ⇒ Object
- .settings ⇒ Object
Instance Method Summary collapse
- #close ⇒ Object
- #connected? ⇒ Boolean (also: #open?)
- #escape(data) ⇒ Object
- #execute(sql_command, params = nil) ⇒ Object
- #executed_time_elapsed_milliseconds ⇒ Object
- #open ⇒ Object (also: #connect)
- #reset ⇒ Object
- #settings ⇒ Object
- #transaction(&blk) ⇒ Object
Instance Attribute Details
#connection ⇒ Object
Returns the value of attribute connection.
21 22 23 |
# File 'lib/message_store/postgres/session.rb', line 21 def connection @connection end |
#executed_time ⇒ Object
Returns the value of attribute executed_time.
22 23 24 |
# File 'lib/message_store/postgres/session.rb', line 22 def executed_time @executed_time end |
Class Method Details
.build(settings: nil) ⇒ Object
24 25 26 27 28 29 30 31 32 33 |
# File 'lib/message_store/postgres/session.rb', line 24 def self.build(settings: nil) instance = new settings ||= Settings.instance settings.set(instance) Clock::UTC.configure(instance) instance end |
.build_connection(instance) ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/message_store/postgres/session.rb', line 67 def self.build_connection(instance) settings = instance.settings logger.trace(tag: :session) { "Building new connection to database (Settings: #{LogText.settings(settings).inspect})" } connection = PG::Connection.open(settings) connection.type_map_for_results = PG::BasicTypeMapForResults.new(connection) logger.debug(tag: :session) { "Built new connection to database (Settings: #{LogText.settings(settings).inspect})" } connection end |
.configure(receiver, session: nil, settings: nil, attr_name: nil) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/message_store/postgres/session.rb', line 35 def self.configure(receiver, session: nil, settings: nil, attr_name: nil) attr_name ||= :session if session != nil && settings != nil error_msg = "Session configured with both settings and session arguments. Use one or the other, but not both." logger.error(tag: :session) { error_msg } raise Error, error_msg end instance = session || build(settings: settings) receiver.public_send "#{attr_name}=", instance instance end |
.logger ⇒ Object
159 160 161 |
# File 'lib/message_store/postgres/session.rb', line 159 def self.logger @logger ||= Log.get self end |
Instance Method Details
#close ⇒ Object
93 94 95 96 97 98 99 100 |
# File 'lib/message_store/postgres/session.rb', line 93 def close if connection.nil? return end connection.close self.connection = nil end |
#connected? ⇒ Boolean Also known as: open?
79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/message_store/postgres/session.rb', line 79 def connected? return false if connection.nil? status = PG::CONNECTION_OK begin status = connection.status rescue PG::ConnectionBad status = nil end status == PG::CONNECTION_OK end |
#escape(data) ⇒ Object
142 143 144 145 146 147 148 |
# File 'lib/message_store/postgres/session.rb', line 142 def escape(data) connection = connect escaped_data = connection.escape(data) escaped_data end |
#execute(sql_command, params = nil) ⇒ Object
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/message_store/postgres/session.rb', line 106 def execute(sql_command, params=nil) logger.trace(tag: :session) { "Executing SQL command" } logger.trace(tag: :sql) { sql_command } logger.trace(tag: :data) { params.pretty_inspect } unless connected? connect end if params.nil? connection.exec(sql_command).tap do self.executed_time = clock.now logger.debug(tag: :session) { "Executed SQL command (no params)" } end else connection.exec_params(sql_command, params).tap do self.executed_time = clock.now logger.debug(tag: :session) { "Executed SQL command with params" } end end end |
#executed_time_elapsed_milliseconds ⇒ Object
128 129 130 131 132 |
# File 'lib/message_store/postgres/session.rb', line 128 def executed_time_elapsed_milliseconds return nil if executed_time.nil? (clock.now - executed_time) * 1000 end |
#open ⇒ Object Also known as: connect
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/message_store/postgres/session.rb', line 49 def open logger.trace(tag: :session) { "Connecting to database" } if connected? logger.debug(tag: :session) { "Already connected. A new connection will not be built." } return connection end logger.debug(tag: :session) { "Not connected. A new connection will be built." } connection = self.class.build_connection(self) self.connection = connection logger.debug(tag: :session) { "Connected to database" } connection end |
#reset ⇒ Object
102 103 104 |
# File 'lib/message_store/postgres/session.rb', line 102 def reset connection.reset end |
#settings ⇒ Object
150 151 152 153 154 155 156 157 |
# File 'lib/message_store/postgres/session.rb', line 150 def settings settings = {} self.class.settings.each do |s| val = public_send(s) settings[s] = val unless val.nil? end settings end |
#transaction(&blk) ⇒ Object
134 135 136 137 138 139 140 |
# File 'lib/message_store/postgres/session.rb', line 134 def transaction(&blk) unless connected? connect end connection.transaction(&blk) end |