Class: JDBCHelper::TableWrapper
- Inherits:
-
ObjectWrapper
- Object
- ObjectWrapper
- JDBCHelper::TableWrapper
- Includes:
- Enumerable
- Defined in:
- lib/jdbc-helper/wrapper/table_wrapper.rb
Overview
table.drop_table!
Instance Attribute Summary
Attributes inherited from ObjectWrapper
Instance Method Summary collapse
-
#batch ⇒ JDBCHelper::Connection::ResultSetEnumerator
Returns a new TableWrapper object whose subsequent inserts, updates, and deletes are added to batch for JDBC batch-execution.
-
#batch? ⇒ Boolean
Returns if the subsequent updates for this wrapper will be batched.
-
#count(*where) ⇒ Fixnum
Retrieves the count of the table.
-
#default(data_hash) ⇒ JDBCHelper::TableWrapper
Returns a new TableWrapper object with default values, which will be applied to the subsequent inserts and updates.
-
#delete(*where) ⇒ Fixnum
Deletes records matching given condtion.
-
#drop! ⇒ JDBCHelper::TableWrapper
Drops the table.
-
#each(&block) ⇒ JDBCHelper::Connection::ResultSetEnumerator
Executes a select SQL for the table and returns an Enumerable object, or yields each row if block is given.
-
#empty?(*where) ⇒ boolean
Sees if the table is empty.
-
#initialize(connection, table_name) ⇒ TableWrapper
constructor
A new instance of TableWrapper.
-
#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.
-
#order(*criteria, &block) ⇒ JDBCHelper::TableWrapper
Returns a new TableWrapper object which can be used to execute a select statement for the table with the given sorting criteria.
-
#replace(data_hash = {}) ⇒ Fixnum
Replaces a record in the table with the new one with the same unique key.
-
#select(*fields, &block) ⇒ JDBCHelper::TableWrapper
Returns a new TableWrapper object which can be used to execute a select statement for the table selecting only the specified fields.
-
#sql ⇒ String
Returns the select SQL for this wrapper object.
-
#truncate! ⇒ JDBCHelper::TableWrapper
(also: #truncate_table!, #drop_table!)
Empties the table.
-
#update(data_hash_with_where = {}) ⇒ Fixnum
Executes update with the given hash.
-
#where(*conditions, &block) ⇒ JDBCHelper::TableWrapper
Returns a new TableWrapper object which can be used to execute a select statement for the table with the specified filter conditions.
Constructor Details
#initialize(connection, table_name) ⇒ TableWrapper
Returns a new instance of TableWrapper.
221 222 223 224 225 226 |
# File 'lib/jdbc-helper/wrapper/table_wrapper.rb', line 221 def initialize connection, table_name super connection, table_name @update_method = :update @query_default = {} @query_where = [] end |
Instance Method Details
#batch ⇒ JDBCHelper::Connection::ResultSetEnumerator
Returns a new TableWrapper object whose subsequent inserts, updates, and deletes are added to batch for JDBC batch-execution. The actual execution is deferred until JDBCHelper::Connection#execute_batch method is called. Self is returned when batch is called more than once.
193 194 195 196 197 198 199 200 201 |
# File 'lib/jdbc-helper/wrapper/table_wrapper.rb', line 193 def batch if batch? self else obj = self.dup obj.instance_variable_set :@update_method, :add_batch obj end end |
#batch? ⇒ Boolean
Returns if the subsequent updates for this wrapper will be batched
206 207 208 |
# File 'lib/jdbc-helper/wrapper/table_wrapper.rb', line 206 def batch? @update_method == :add_batch end |
#count(*where) ⇒ Fixnum
Retrieves the count of the table
52 53 54 |
# File 'lib/jdbc-helper/wrapper/table_wrapper.rb', line 52 def count *where @connection.query(JDBCHelper::SQL.count(name, @query_where + where))[0][0].to_i end |
#default(data_hash) ⇒ JDBCHelper::TableWrapper
Returns a new TableWrapper object with default values, which will be applied to the subsequent inserts and updates.
171 172 173 174 175 176 177 |
# File 'lib/jdbc-helper/wrapper/table_wrapper.rb', line 171 def default data_hash raise ArgumentError.new("Hash required") unless data_hash.kind_of? Hash obj = self.dup obj.instance_variable_set :@query_default, @query_default.merge(data_hash) obj end |
#delete(*where) ⇒ Fixnum
Deletes records matching given condtion
102 103 104 |
# File 'lib/jdbc-helper/wrapper/table_wrapper.rb', line 102 def delete *where @connection.send @update_method, JDBCHelper::SQL.delete(name, @query_where + where) end |
#drop! ⇒ JDBCHelper::TableWrapper
This operation cannot be undone
Drops the table.
118 119 120 121 |
# File 'lib/jdbc-helper/wrapper/table_wrapper.rb', line 118 def drop! @connection.update(JDBCHelper::SQL.check "drop table #{name}") self end |
#each(&block) ⇒ JDBCHelper::Connection::ResultSetEnumerator
Executes a select SQL for the table and returns an Enumerable object, or yields each row if block is given.
183 184 185 |
# File 'lib/jdbc-helper/wrapper/table_wrapper.rb', line 183 def each &block @connection.enumerate sql, &block end |
#empty?(*where) ⇒ boolean
Sees if the table is empty
59 60 61 |
# File 'lib/jdbc-helper/wrapper/table_wrapper.rb', line 59 def empty? *where count(*where) == 0 end |
#insert(data_hash = {}) ⇒ Fixnum
Inserts a record into the table with the given hash
66 67 68 |
# File 'lib/jdbc-helper/wrapper/table_wrapper.rb', line 66 def insert data_hash = {} @connection.send @update_method, JDBCHelper::SQL.insert(name, @query_default.merge(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.
75 76 77 |
# File 'lib/jdbc-helper/wrapper/table_wrapper.rb', line 75 def insert_ignore data_hash = {} @connection.send @update_method, JDBCHelper::SQL.insert_ignore(name, @query_default.merge(data_hash)) end |
#order(*criteria, &block) ⇒ JDBCHelper::TableWrapper
Returns a new TableWrapper object which can be used to execute a select statement for the table with the given sorting criteria. If a block is given, executes the select statement and yields each row to the block.
159 160 161 162 163 164 |
# File 'lib/jdbc-helper/wrapper/table_wrapper.rb', line 159 def order *criteria, &block raise ArgumentError.new("Wrong number of arguments") if criteria.empty? obj = self.dup obj.instance_variable_set :@query_order, criteria ret obj, &block 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.
83 84 85 |
# File 'lib/jdbc-helper/wrapper/table_wrapper.rb', line 83 def replace data_hash = {} @connection.send @update_method, JDBCHelper::SQL.replace(name, @query_default.merge(data_hash)) end |
#select(*fields, &block) ⇒ JDBCHelper::TableWrapper
Returns a new TableWrapper object which can be used to execute a select statement for the table selecting only the specified fields. If a block is given, executes the select statement and yields each row to the block.
133 134 135 136 137 |
# File 'lib/jdbc-helper/wrapper/table_wrapper.rb', line 133 def select *fields, &block obj = self.dup obj.instance_variable_set :@query_select, fields unless fields.empty? ret obj, &block end |
#sql ⇒ String
Returns the select SQL for this wrapper object
213 214 215 216 217 218 219 |
# File 'lib/jdbc-helper/wrapper/table_wrapper.rb', line 213 def sql JDBCHelper::SQL.select( name, :select => @query_select, :where => @query_where, :order => @query_order) end |
#truncate! ⇒ JDBCHelper::TableWrapper Also known as: truncate_table!, drop_table!
This operation cannot be undone
Empties the table.
109 110 111 112 |
# File 'lib/jdbc-helper/wrapper/table_wrapper.rb', line 109 def truncate! @connection.update(JDBCHelper::SQL.check "truncate table #{name}") self 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.
92 93 94 95 96 97 |
# File 'lib/jdbc-helper/wrapper/table_wrapper.rb', line 92 def update data_hash_with_where = {} where_ext = data_hash_with_where.delete(:where) where_ext = [where_ext] unless where_ext.is_a? Array @connection.send @update_method, JDBCHelper::SQL.update(name, @query_default.merge(data_hash_with_where), @query_where + where_ext.compact) end |
#where(*conditions, &block) ⇒ JDBCHelper::TableWrapper
Returns a new TableWrapper object which can be used to execute a select statement for the table with the specified filter conditions. If a block is given, executes the select statement and yields each row to the block.
145 146 147 148 149 150 151 |
# File 'lib/jdbc-helper/wrapper/table_wrapper.rb', line 145 def where *conditions, &block raise ArgumentError.new("Wrong number of arguments") if conditions.empty? obj = self.dup obj.instance_variable_set :@query_where, @query_where + conditions ret obj, &block end |