Class: JDBCHelper::TableWrapper

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

Overview

conn.table(‘test.data’).drop_table!

Instance Attribute Summary

Attributes inherited from ObjectWrapper

#connection, #name

Instance Method Summary collapse

Methods inherited from ObjectWrapper

#initialize

Constructor Details

This class inherits a constructor from JDBCHelper::ObjectWrapper

Instance Method Details

#count(where = nil) ⇒ Fixnum

Retrieves the count of the table

Returns:

  • (Fixnum)

    Count of the records.



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

Parameters:

  • where (Hash) (defaults to: nil)

    Delete filters

Returns:

  • (Fixnum)

    Number of affected records



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

Note:

This operation cannot be undone

Drops the table.

Returns:

  • (Fixnum)

    executeUpdate return value



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

Returns:

  • (boolean)


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

Parameters:

  • data_hash (Hash)

    Column values in Hash

Returns:

  • (Fixnum)

    Number of affected records



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

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)

    Column values in Hash

Returns:

  • (Fixnum)

    Number of affected records



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

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)

    Column values in Hash

Returns:

  • (Fixnum)

    Number of affected records



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

Parameters:

  • where (Hash/String) (defaults to: nil)

    Select filters

Yields:

Returns:

  • (Array)

    Array is returned if block is not given



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

Note:

This operation cannot be undone

Empties the table.

Returns:

  • (Fixnum)

    executeUpdate return value



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.

Parameters:

  • data_hash_with_where (Hash)

    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



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