Class: JDBCHelper::TableWrapper

Inherits:
ObjectWrapper show all
Includes:
Enumerable
Defined in:
lib/jdbc-helper/wrapper/table_wrapper.rb

Overview

table.drop_table!

Instance Attribute Summary

Attributes inherited from ObjectWrapper

#connection, #name

Instance Method Summary collapse

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

#batchJDBCHelper::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

Returns:

  • (Boolean)

Since:

  • 0.4.0



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

Parameters:

  • where (List of Hash/String)

    Filter conditions

Returns:

  • (Fixnum)

    Count of the records.



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.

Parameters:

  • data_hash (Hash)

    Default values

Returns:

Raises:

  • (ArgumentError)

Since:

  • 0.4.5



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

Parameters:

  • where (List of Hash/String)

    Delete filters

Returns:

  • (Fixnum)

    Number of affected records



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

Note:

This operation cannot be undone

Drops the table.

Returns:



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

Parameters:

  • Filter (Hash/String)

    conditions

Returns:

  • (boolean)


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

Parameters:

  • data_hash (Hash) (defaults to: {})

    Column values in Hash

Returns:

  • (Fixnum)

    Number of affected records



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

Note:

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.

Parameters:

  • data_hash (Hash) (defaults to: {})

    Column values in Hash

Returns:

  • (Fixnum)

    Number of affected records



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.

Parameters:

  • criteria (*String/*Symbol)

    Sorting criteria

Returns:

Raises:

  • (ArgumentError)

Since:

  • 0.4.0



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

Note:

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.

Parameters:

  • data_hash (Hash) (defaults to: {})

    Column values in Hash

Returns:

  • (Fixnum)

    Number of affected records



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.

Parameters:

  • fields (*String/*Symbol)

    List of fields to select

Returns:

Since:

  • 0.4.0



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

#sqlString

Returns the select SQL for this wrapper object

Returns:

  • (String)

    Select SQL

Since:

  • 0.4.0



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!

Note:

This operation cannot be undone

Empties the table.

Returns:



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.

Parameters:

  • data_hash_with_where (Hash) (defaults to: {})

    Column values in Hash. :where element of the given hash can (usually should) point to another Hash representing update filters.

Returns:

  • (Fixnum)

    Number of affected records



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.

Parameters:

  • conditions (List of Hash/String)

    Filter conditions

Returns:

Raises:

  • (ArgumentError)

Since:

  • 0.4.0



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