Class: Patriot::Util::DBClient::SQLite3Client
- Inherits:
-
Base
- Object
- Base
- Patriot::Util::DBClient::SQLite3Client
- Defined in:
- lib/patriot/util/db_client/sqlite3_client.rb
Overview
NOT thread safe
Instance Method Summary collapse
- #build_insert_query(tbl, value, option = {}) ⇒ Object
- #close ⇒ Object
- #do_insert(query) ⇒ Object
- #do_select(query) ⇒ Object
- #do_update(query) ⇒ Object
-
#initialize(dbconf) ⇒ SQLite3Client
constructor
A new instance of SQLite3Client.
- #quote(v) ⇒ Object
Constructor Details
#initialize(dbconf) ⇒ SQLite3Client
Returns a new instance of SQLite3Client.
15 16 17 18 19 |
# File 'lib/patriot/util/db_client/sqlite3_client.rb', line 15 def initialize(dbconf) db = dbconf[:database] db = File.join($home, db) unless db.start_with?("/") @connection = SQLite3::Database.new(dbconf[:database], :results_as_hash => true) end |
Instance Method Details
#build_insert_query(tbl, value, option = {}) ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/patriot/util/db_client/sqlite3_client.rb', line 27 def build_insert_query(tbl, value, option = {}) option = {:ignore => false}.merge(option) cols, vals = [], [] value.each do |c,v| cols << c vals << quote(v) end if option[:ignore] return "INSERT OR IGNORE INTO #{tbl} (#{cols.join(',')}) VALUES (#{vals.join(',')})" else return "INSERT INTO #{tbl} (#{cols.join(',')}) VALUES (#{vals.join(',')})" end end |
#close ⇒ Object
54 55 56 |
# File 'lib/patriot/util/db_client/sqlite3_client.rb', line 54 def close() @connection.close unless @connection.nil? end |
#do_insert(query) ⇒ Object
42 43 44 45 |
# File 'lib/patriot/util/db_client/sqlite3_client.rb', line 42 def do_insert(query) @connection.execute(query) return @connection.last_insert_row_id end |
#do_select(query) ⇒ Object
22 23 24 |
# File 'lib/patriot/util/db_client/sqlite3_client.rb', line 22 def do_select(query) return @connection.execute(query).map{|r| HashRecord.new(r)} end |
#do_update(query) ⇒ Object
48 49 50 51 |
# File 'lib/patriot/util/db_client/sqlite3_client.rb', line 48 def do_update(query) @connection.execute(query) return @connection.changes end |
#quote(v) ⇒ Object
59 60 61 62 63 |
# File 'lib/patriot/util/db_client/sqlite3_client.rb', line 59 def quote(v) return 'NULL' if v.nil? return "'#{v.to_s}'" if v.is_a?(DateTime) || v.is_a?(Time) return (v.is_a?(String) ? "'#{v.gsub(/'/,"''")}'" : v) end |