Class: Patriot::Util::DBClient::Base Abstract
- Inherits:
-
Object
- Object
- Patriot::Util::DBClient::Base
- Defined in:
- lib/patriot/util/db_client/base.rb
Overview
base class for DB connection
Instance Method Summary collapse
-
#build_insert_query(tbl, value, opts = {}) ⇒ String
create a insert statement for given arguments.
-
#close ⇒ Object
close this connection.
-
#cond_exp(cond) ⇒ Object
get an expression of where clause.
-
#delete(tbl, cond) ⇒ Integer
delete records from a table.
-
#do_insert(query) ⇒ Integer
The method which issues an insert statement.
-
#do_select(query) ⇒ Array
The method which issues a select statement.
-
#do_update(query) ⇒ Integer
The method which issues a delete/update statement.
-
#execute_statement(stmt, type = :select) ⇒ Array, Integer
execute a given statment with this connection.
-
#insert(tbl, value, opts = {}) ⇒ Integer
insert value into a given table.
-
#quote(v) ⇒ Object
quote a given value to use in SQL.
-
#select(tbl, cond = {}, opts = {}) ⇒ Array
select records from a table.
-
#update(tbl, new_val, cond) ⇒ Integer
update records of a table.
-
#value_exp(val) ⇒ Object
get an expression used as values in insert statements.
Instance Method Details
#build_insert_query(tbl, value, opts = {}) ⇒ String
create a insert statement for given arguments. Subclasses can override this method if necessary
47 48 49 50 51 52 53 54 55 |
# File 'lib/patriot/util/db_client/base.rb', line 47 def build_insert_query(tbl, value, opts = {}) opts = {:ignore => false}.merge(opts) cols, vals = [], [] value.each do |c,v| cols << c vals << quote(v) end return "INSERT INTO #{tbl} (#{cols.join(',')}) VALUES (#{vals.join(',')})" end |
#close ⇒ Object
close this connection
8 9 10 |
# File 'lib/patriot/util/db_client/base.rb', line 8 def close() raise NotImplementedError end |
#cond_exp(cond) ⇒ Object
get an expression of where clause
126 127 128 129 130 131 |
# File 'lib/patriot/util/db_client/base.rb', line 126 def cond_exp(cond) cond = cond.map{|k,v| v.nil? ? "#{k} IS NULL" : "#{k} = #{quote(v)}"}.join(' AND ') if cond.is_a?(Hash) raise "illegal type of cond : #{cond.class}" unless cond.is_a?(String) cond = "WHERE #{cond}" unless cond == "" return cond end |
#delete(tbl, cond) ⇒ Integer
delete records from a table
95 96 97 98 |
# File 'lib/patriot/util/db_client/base.rb', line 95 def delete(tbl, cond) query = "DELETE FROM #{tbl} #{cond_exp(cond)}" return execute_statement(query, :update) end |
#do_insert(query) ⇒ Integer
The method which issues an insert statement
60 61 62 |
# File 'lib/patriot/util/db_client/base.rb', line 60 def do_insert(query) raise NotImplementedError end |
#do_select(query) ⇒ Array
The method which issues a select statement
85 86 87 |
# File 'lib/patriot/util/db_client/base.rb', line 85 def do_select(query) raise NotImplementedError end |
#do_update(query) ⇒ Integer
The method which issues a delete/update statement
115 116 117 |
# File 'lib/patriot/util/db_client/base.rb', line 115 def do_update(query) raise NotImplementedError end |
#execute_statement(stmt, type = :select) ⇒ Array, Integer
execute a given statment with this connection.
18 19 20 21 22 23 24 25 |
# File 'lib/patriot/util/db_client/base.rb', line 18 def execute_statement(stmt, type = :select) method = "do_#{type}".to_sym begin return self.send(method, stmt) rescue => e raise "Failed to execute #{stmt} : Caused by (#{e})" end end |
#insert(tbl, value, opts = {}) ⇒ Integer
insert value into a given table
35 36 37 38 |
# File 'lib/patriot/util/db_client/base.rb', line 35 def insert(tbl, value, opts = {}) query = build_insert_query(tbl, value, opts) return execute_statement(query, :insert) end |
#quote(v) ⇒ Object
quote a given value to use in SQL
134 135 136 |
# File 'lib/patriot/util/db_client/base.rb', line 134 def quote(v) raise NotImplementedError end |
#select(tbl, cond = {}, opts = {}) ⇒ Array
select records from a table
74 75 76 77 78 79 80 |
# File 'lib/patriot/util/db_client/base.rb', line 74 def select(tbl, cond = {}, opts = {}) opts = {:items => ['*']}.merge(opts) query = "SELECT #{opts[:items].join(', ')} FROM #{tbl} #{cond_exp(cond)}" query = "#{query} LIMIT #{opts[:limit]}" if opts.has_key?(:limit) query = "#{query} OFFSET #{opts[:offset]}" if opts.has_key?(:offset) return execute_statement(query, :select) end |
#update(tbl, new_val, cond) ⇒ Integer
update records of a table
107 108 109 110 |
# File 'lib/patriot/util/db_client/base.rb', line 107 def update(tbl, new_val, cond) query = "UPDATE #{tbl} SET #{value_exp(new_val)} #{cond_exp(cond)}" return execute_statement(query, :update) end |
#value_exp(val) ⇒ Object
get an expression used as values in insert statements
120 121 122 123 |
# File 'lib/patriot/util/db_client/base.rb', line 120 def value_exp(val) raise "illegal type of value_exp #{val.class}" unless val.is_a?(Hash) return val.map{|k,v| v.nil? ? "#{k} = NULL" : "#{k} = #{quote(v)}"}.join(",") end |