Class: JunglePath::DBAccess::IO::DB
- Inherits:
-
Object
- Object
- JunglePath::DBAccess::IO::DB
- Defined in:
- lib/jungle_path/db_access/io/db.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#base ⇒ Object
readonly
Returns the value of attribute base.
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#copy ⇒ Object
readonly
Returns the value of attribute copy.
-
#database_name ⇒ Object
readonly
Returns the value of attribute database_name.
-
#delete ⇒ Object
readonly
Returns the value of attribute delete.
-
#insert ⇒ Object
readonly
Returns the value of attribute insert.
-
#postgresql ⇒ Object
readonly
Returns the value of attribute postgresql.
-
#schema ⇒ Object
readonly
Returns the value of attribute schema.
-
#select ⇒ Object
readonly
Returns the value of attribute select.
-
#update ⇒ Object
readonly
Returns the value of attribute update.
Instance Method Summary collapse
- #copy_table_data(from_table, to_table) ⇒ Object
- #create_table_like(from_table, to_table) ⇒ Object
- #drop_table?(table_name) ⇒ Boolean
- #get_max_id_for_table(table_name, id_column_name = :id) ⇒ Object
-
#initialize(config, logger = nil) ⇒ DB
constructor
A new instance of DB.
- #rename_table(table_name, new_table_name) ⇒ Object
- #reset_sequence_for_table(table_name) ⇒ Object
- #table_exists?(table_name) ⇒ Boolean
- #transaction ⇒ Object
Constructor Details
#initialize(config, logger = nil) ⇒ DB
Returns a new instance of DB.
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/jungle_path/db_access/io/db.rb', line 25 def initialize(config, logger=nil) @logger = logger @config = config @postgresql = config @db = JunglePath::DBAccess::IO.connection( database_type: config.type, user_name: config.user_name, database_name: config.name, host: config.host, extensions: config.extensions, password: config.password, port: config.port, options: config. ) @select = JunglePath::DBAccess::IO::Select.new @db, @logger @insert = JunglePath::DBAccess::IO::Insert.new @db, @logger @update = JunglePath::DBAccess::IO::Update.new @db, @logger @delete = JunglePath::DBAccess::IO::Delete.new @db, @logger @schema = JunglePath::DBAccess::IO::Schema.new @db, @logger @copy = JunglePath::DBAccess::IO::Copy.new @db, @logger @base = @db @database_name = config.name end |
Instance Attribute Details
#base ⇒ Object (readonly)
Returns the value of attribute base.
20 21 22 |
# File 'lib/jungle_path/db_access/io/db.rb', line 20 def base @base end |
#config ⇒ Object (readonly)
Returns the value of attribute config.
22 23 24 |
# File 'lib/jungle_path/db_access/io/db.rb', line 22 def config @config end |
#copy ⇒ Object (readonly)
Returns the value of attribute copy.
19 20 21 |
# File 'lib/jungle_path/db_access/io/db.rb', line 19 def copy @copy end |
#database_name ⇒ Object (readonly)
Returns the value of attribute database_name.
21 22 23 |
# File 'lib/jungle_path/db_access/io/db.rb', line 21 def database_name @database_name end |
#delete ⇒ Object (readonly)
Returns the value of attribute delete.
17 18 19 |
# File 'lib/jungle_path/db_access/io/db.rb', line 17 def delete @delete end |
#insert ⇒ Object (readonly)
Returns the value of attribute insert.
15 16 17 |
# File 'lib/jungle_path/db_access/io/db.rb', line 15 def insert @insert end |
#postgresql ⇒ Object (readonly)
Returns the value of attribute postgresql.
23 24 25 |
# File 'lib/jungle_path/db_access/io/db.rb', line 23 def postgresql @postgresql end |
#schema ⇒ Object (readonly)
Returns the value of attribute schema.
18 19 20 |
# File 'lib/jungle_path/db_access/io/db.rb', line 18 def schema @schema end |
#select ⇒ Object (readonly)
Returns the value of attribute select.
14 15 16 |
# File 'lib/jungle_path/db_access/io/db.rb', line 14 def select @select end |
#update ⇒ Object (readonly)
Returns the value of attribute update.
16 17 18 |
# File 'lib/jungle_path/db_access/io/db.rb', line 16 def update @update end |
Instance Method Details
#copy_table_data(from_table, to_table) ⇒ Object
65 66 67 |
# File 'lib/jungle_path/db_access/io/db.rb', line 65 def copy_table_data(from_table, to_table) JunglePath::DBAccess::Meta::Table.copy_data(self, from_table, to_table) end |
#create_table_like(from_table, to_table) ⇒ Object
61 62 63 |
# File 'lib/jungle_path/db_access/io/db.rb', line 61 def create_table_like(from_table, to_table) JunglePath::DBAccess::Meta::Table.create_like(self, from_table, to_table) end |
#drop_table?(table_name) ⇒ Boolean
49 50 51 |
# File 'lib/jungle_path/db_access/io/db.rb', line 49 def drop_table? table_name JunglePath::DBAccess::Meta::Table.drop? self, table_name end |
#get_max_id_for_table(table_name, id_column_name = :id) ⇒ Object
75 76 77 78 79 |
# File 'lib/jungle_path/db_access/io/db.rb', line 75 def get_max_id_for_table(table_name, id_column_name=:id) ds = @db["select max(#{id_column_name}) as max_id from \"#{table_name}\""] result = ds.first max_id = result[:max_id] end |
#rename_table(table_name, new_table_name) ⇒ Object
57 58 59 |
# File 'lib/jungle_path/db_access/io/db.rb', line 57 def rename_table table_name, new_table_name JunglePath::DBAccess::Meta::Table.rename_table self, table_name, new_table_name end |
#reset_sequence_for_table(table_name) ⇒ Object
69 70 71 72 73 |
# File 'lib/jungle_path/db_access/io/db.rb', line 69 def reset_sequence_for_table(table_name) #max = @db[table_name.to_sym].max(:id) + 1 #@db.run "alter sequence #{table_name}_id_seq restart with #{max}" @db.run "select setval('#{table_name}_id_seq', (select max(id)+1 from \"#{table_name}\"), false)" end |
#table_exists?(table_name) ⇒ Boolean
53 54 55 |
# File 'lib/jungle_path/db_access/io/db.rb', line 53 def table_exists? table_name JunglePath::DBAccess::Meta::Table.exists? self, table_name end |
#transaction ⇒ Object
81 82 83 84 85 |
# File 'lib/jungle_path/db_access/io/db.rb', line 81 def transaction @db.transaction do yield end end |