Class: Baza::Driver::Pg::Databases
- Inherits:
-
Object
- Object
- Baza::Driver::Pg::Databases
- Defined in:
- lib/baza/driver/pg/databases.rb
Instance Method Summary collapse
- #[](name) ⇒ Object
- #create(args) ⇒ Object
- #current_database ⇒ Object
- #current_database_name ⇒ Object
-
#initialize(args) ⇒ Databases
constructor
A new instance of Databases.
- #list(args = {}) ⇒ Object
- #with_database(name) ⇒ Object
Constructor Details
#initialize(args) ⇒ Databases
Returns a new instance of Databases.
2 3 4 |
# File 'lib/baza/driver/pg/databases.rb', line 2 def initialize(args) @db = args.fetch(:db) end |
Instance Method Details
#[](name) ⇒ Object
29 30 31 32 33 |
# File 'lib/baza/driver/pg/databases.rb', line 29 def [](name) database = list(name: name).first raise Baza::Errors::DatabaseNotFound unless database database end |
#create(args) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/baza/driver/pg/databases.rb', line 14 def create(args) if args[:if_not_exists] begin __send__(:[], args.fetch(:name).to_s) return true # rubocop:disable Lint/HandleExceptions rescue Baza::Errors::DatabaseNotFound # rubocop:enable Lint/HandleExceptions end end @db.query("CREATE DATABASE #{@db.sep_database}#{@db.escape_table(args.fetch(:name))}#{@db.sep_database}") true end |
#current_database ⇒ Object
6 7 8 |
# File 'lib/baza/driver/pg/databases.rb', line 6 def current_database @db.databases[current_database_name] end |
#current_database_name ⇒ Object
10 11 12 |
# File 'lib/baza/driver/pg/databases.rb', line 10 def current_database_name @db.query("SELECT current_database()").fetch.values.first end |
#list(args = {}) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/baza/driver/pg/databases.rb', line 35 def list(args = {}) where_args = {} where_args[:datname] = args.fetch(:name) if args[:name] database_list = [] unless block_given? @db.select(:pg_database, where_args) do |database_data| database = Baza::Driver::Pg::Database.new( db: @db, driver: @db.driver, name: database_data.fetch(:datname) ) if database_list database_list << database else yield database end end database_list end |
#with_database(name) ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/baza/driver/pg/databases.rb', line 57 def with_database(name) if @db.opts[:db].to_s == name yield if block_given? return self end previous_db_name = @db.current_database_name @db.opts[:db] = name @db.driver.reconnect if block_given? begin yield ensure @db.opts[:db] = previous_db_name @db.driver.reconnect end end self end |