Module: BlackStack::PostgreSQL
- Defined in:
- lib/blackstack-db/postgresql.rb
Constant Summary collapse
- @@db_url =
database connection parameters
nil- @@db_port =
nil- @@db_name =
nil- @@db_user =
nil- @@db_password =
nil
Class Method Summary collapse
-
.connect ⇒ Object
create database connection.
-
.connection_string ⇒ Object
return the connection string for a postgresql database.
- .db_name ⇒ Object
- .db_password ⇒ Object
- .db_port ⇒ Object
-
.db_url ⇒ Object
database connection getters.
- .db_user ⇒ Object
-
.guid ⇒ Object
return a postgresql uuid.
-
.now ⇒ Object
return current datetime with format ‘%Y-%m-%d %H:%M:%S %Z`, using the timezone of the database (`select current_setting(’TIMEZONE’)‘) TODO: I am hardcoding the value of `tz` because for any reason `SELECT current_setting(’TIMEZONE’)‘ returns `UTC` instead of `America/Argentina/Buenos_Aires` when I run it from Ruby.
- .seconds_ago(n) ⇒ Object
- .set_db_params(h) ⇒ Object
Class Method Details
.connect ⇒ Object
create database connection
78 79 80 81 |
# File 'lib/blackstack-db/postgresql.rb', line 78 def self.connect BlackStack::set_db_type(BlackStack::TYPE_POSTGRESQL) Sequel.connect(BlackStack::PostgreSQL.connection_string) end |
.connection_string ⇒ Object
return the connection string for a postgresql database
11 12 13 |
# File 'lib/blackstack-db/postgresql.rb', line 11 def self.connection_string "postgresql://#{@@db_user}:#{@@db_password}@#{@@db_url}:#{@@db_port}/#{@@db_name}" end |
.db_name ⇒ Object
22 23 24 |
# File 'lib/blackstack-db/postgresql.rb', line 22 def self.db_name @@db_name end |
.db_password ⇒ Object
28 29 30 |
# File 'lib/blackstack-db/postgresql.rb', line 28 def self.db_password @@db_password end |
.db_port ⇒ Object
19 20 21 |
# File 'lib/blackstack-db/postgresql.rb', line 19 def self.db_port @@db_port end |
.db_url ⇒ Object
database connection getters
16 17 18 |
# File 'lib/blackstack-db/postgresql.rb', line 16 def self.db_url @@db_url end |
.db_user ⇒ Object
25 26 27 |
# File 'lib/blackstack-db/postgresql.rb', line 25 def self.db_user @@db_user end |
.guid ⇒ Object
return a postgresql uuid
84 85 86 |
# File 'lib/blackstack-db/postgresql.rb', line 84 def self.guid() DB['SELECT uuid_generate_v4() AS id'].first[:id] end |
.now ⇒ Object
return current datetime with format ‘%Y-%m-%d %H:%M:%S %Z`, using the timezone of the database (`select current_setting(’TIMEZONE’)‘) TODO: I am hardcoding the value of `tz` because for any reason `SELECT current_setting(’TIMEZONE’)‘ returns `UTC` instead of `America/Argentina/Buenos_Aires` when I run it from Ruby. Be sure your database is ALWAYS configured with the correct timezone.
91 92 93 94 |
# File 'lib/blackstack-db/postgresql.rb', line 91 def self.now() tz = 'America/Argentina/Buenos_Aires' #DB["SELECT current_setting('TIMEZONE') AS tz"].first[:tz] DB["SELECT current_timestamp at TIME ZONE '#{tz}' AS now"].first[:now] end |
.seconds_ago(n) ⇒ Object
96 97 98 99 |
# File 'lib/blackstack-db/postgresql.rb', line 96 def self.seconds_ago(n) tz = 'America/Argentina/Buenos_Aires' #DB["SELECT current_setting('TIMEZONE') AS tz"].first[:tz] DB["SELECT (current_timestamp - interval '#{n} seconds') at TIME ZONE '#{tz}' AS now"].first[:now] end |
.set_db_params(h) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/blackstack-db/postgresql.rb', line 32 def self.set_db_params(h) # validate: the parameter h is requred raise "The parameter h is required." if h.nil? # validate: the parameter h must be a hash raise "The parameter h must be a hash" unless h.is_a?(Hash) # validate: the :db_url key is required raise 'The key :db_url is required' unless h.has_key?(:db_url) # validate: the :db_port key is required raise 'The key :db_port is required' unless h.has_key?(:db_port) # validate: the db_name key is required raise 'The key :db_name is required' unless h.has_key?(:db_name) # validate: the db_user key is required raise 'The key :db_user is required' unless h.has_key?(:db_user) # validate: the db_password key is required raise 'The key :db_password is required' unless h.has_key?(:db_password) # validate: the :db_url key must be a string raise 'The key :db_url must be a string' unless h[:db_url].is_a?(String) # validate: the :db_port key must be an integer, or a string that can be converted to an integer raise 'The key :db_port must be an integer' unless h[:db_port].is_a?(Integer) || (h[:db_port].is_a?(String) && h[:db_port].to_i.to_s == h[:db_port]) # validate: the :db_name key must be a string raise 'The key :db_name must be a string' unless h[:db_name].is_a?(String) # validate: the :db_user key must be a string raise 'The key :db_user must be a string' unless h[:db_user].is_a?(String) # validate: the :db_password key must be a string raise 'The key :db_password must be a string' unless h[:db_password].is_a?(String) # map values @@db_url = h[:db_url] @@db_port = h[:db_port].to_i @@db_name = h[:db_name] @@db_user = h[:db_user] @@db_password = h[:db_password] end |