Class: JDBCHelper::TableWrapper
- Inherits:
-
ObjectWrapper
- Object
- ObjectWrapper
- JDBCHelper::TableWrapper
- Defined in:
- lib/jdbc-helper/wrapper/table_wrapper.rb
Overview
conn.table(‘test.data’).drop_table!
Instance Attribute Summary
Attributes inherited from ObjectWrapper
Instance Method Summary collapse
-
#count(where = nil) ⇒ Fixnum
Retrieves the count of the table.
-
#delete(where = nil) ⇒ Fixnum
Deletes records matching given condtion.
-
#drop_table! ⇒ Fixnum
Drops the table.
-
#empty? ⇒ boolean
Sees if the table is empty.
-
#insert(data_hash) ⇒ Fixnum
Inserts a record into the table with the given hash.
-
#insert_ignore(data_hash) ⇒ Fixnum
Inserts a record into the table with the given hash.
-
#replace(data_hash) ⇒ Fixnum
Replaces a record in the table with the new one with the same unique key.
-
#select(where = nil) {|JDBCHelper::Connection::Row| ... } ⇒ Array
Select * with optional conditions.
-
#truncate_table! ⇒ Fixnum
Empties the table.
-
#update(data_hash_with_where) ⇒ Fixnum
Executes update with the given hash.
Methods inherited from ObjectWrapper
Constructor Details
This class inherits a constructor from JDBCHelper::ObjectWrapper
Instance Method Details
#count(where = nil) ⇒ Fixnum
Retrieves the count of the table
29 30 31 |
# File 'lib/jdbc-helper/wrapper/table_wrapper.rb', line 29 def count(where = nil) @connection.query(JDBCHelper::SQL.count name, where)[0][0].to_i end |
#delete(where = nil) ⇒ Fixnum
Deletes records matching given condtion
83 84 85 |
# File 'lib/jdbc-helper/wrapper/table_wrapper.rb', line 83 def delete where = nil @connection.update(JDBCHelper::SQL.delete name, where) end |
#drop_table! ⇒ Fixnum
This operation cannot be undone
Drops the table.
97 98 99 |
# File 'lib/jdbc-helper/wrapper/table_wrapper.rb', line 97 def drop_table! @connection.update(JDBCHelper::SQL.check "drop table #{name}") end |
#empty? ⇒ boolean
Sees if the table is empty
35 36 37 |
# File 'lib/jdbc-helper/wrapper/table_wrapper.rb', line 35 def empty? count == 0 end |
#insert(data_hash) ⇒ Fixnum
Inserts a record into the table with the given hash
50 51 52 |
# File 'lib/jdbc-helper/wrapper/table_wrapper.rb', line 50 def insert data_hash @connection.update(JDBCHelper::SQL.insert name, data_hash) end |
#insert_ignore(data_hash) ⇒ Fixnum
This is not SQL standard. Only works if the database supports insert ignore syntax.
Inserts a record into the table with the given hash. Skip insertion when duplicate record is found.
59 60 61 |
# File 'lib/jdbc-helper/wrapper/table_wrapper.rb', line 59 def insert_ignore data_hash @connection.update(JDBCHelper::SQL.insert_ignore name, data_hash) end |
#replace(data_hash) ⇒ Fixnum
This is not SQL standard. Only works if the database supports replace syntax.
Replaces a record in the table with the new one with the same unique key.
67 68 69 |
# File 'lib/jdbc-helper/wrapper/table_wrapper.rb', line 67 def replace data_hash @connection.update(JDBCHelper::SQL.replace name, data_hash) end |
#select(where = nil) {|JDBCHelper::Connection::Row| ... } ⇒ Array
Select * with optional conditions
43 44 45 |
# File 'lib/jdbc-helper/wrapper/table_wrapper.rb', line 43 def select where = nil, &block @connection.query(JDBCHelper::SQL.select(name, where), &block) end |
#truncate_table! ⇒ Fixnum
This operation cannot be undone
Empties the table.
90 91 92 |
# File 'lib/jdbc-helper/wrapper/table_wrapper.rb', line 90 def truncate_table! @connection.update(JDBCHelper::SQL.check "truncate table #{name}") end |
#update(data_hash_with_where) ⇒ Fixnum
Executes update with the given hash. :where element of the hash is taken out to generate where clause of the update SQL.
76 77 78 |
# File 'lib/jdbc-helper/wrapper/table_wrapper.rb', line 76 def update data_hash_with_where @connection.update(JDBCHelper::SQL.update name, data_hash_with_where) end |