Class: Baza::BaseSqlDriver
- Inherits:
-
Object
- Object
- Baza::BaseSqlDriver
- Defined in:
- lib/baza/base_sql_driver.rb
Direct Known Subclasses
Driver::Mysql, Driver::Mysql2, Driver::Sqlite3, Driver::Sqlite3Rhodes, JdbcDriver
Instance Attribute Summary collapse
-
#baza ⇒ Object
readonly
Returns the value of attribute baza.
-
#cols ⇒ Object
Returns the value of attribute cols.
-
#conn ⇒ Object
readonly
Returns the value of attribute conn.
-
#indexes ⇒ Object
Returns the value of attribute indexes.
-
#sep_col ⇒ Object
readonly
Returns the value of attribute sep_col.
-
#sep_table ⇒ Object
readonly
Returns the value of attribute sep_table.
-
#sep_val ⇒ Object
readonly
Returns the value of attribute sep_val.
-
#tables ⇒ Object
Returns the value of attribute tables.
Class Method Summary collapse
Instance Method Summary collapse
-
#esc_col(string) ⇒ Object
(also: #esc_table)
Escapes a string to be used as a column.
- #escape(string) ⇒ Object (also: #esc, #escape_alternative)
-
#initialize(baza) ⇒ BaseSqlDriver
constructor
A new instance of BaseSqlDriver.
- #insert_multi(tablename, arr_hashes, args = nil) ⇒ Object
- #transaction ⇒ Object
Constructor Details
#initialize(baza) ⇒ BaseSqlDriver
Returns a new instance of BaseSqlDriver.
8 9 10 11 12 13 14 |
# File 'lib/baza/base_sql_driver.rb', line 8 def initialize(baza) @baza = baza @sep_table = "`" @sep_col = "`" @sep_val = "'" end |
Instance Attribute Details
#baza ⇒ Object (readonly)
Returns the value of attribute baza.
2 3 4 |
# File 'lib/baza/base_sql_driver.rb', line 2 def baza @baza end |
#cols ⇒ Object
Returns the value of attribute cols.
3 4 5 |
# File 'lib/baza/base_sql_driver.rb', line 3 def cols @cols end |
#conn ⇒ Object (readonly)
Returns the value of attribute conn.
2 3 4 |
# File 'lib/baza/base_sql_driver.rb', line 2 def conn @conn end |
#indexes ⇒ Object
Returns the value of attribute indexes.
3 4 5 |
# File 'lib/baza/base_sql_driver.rb', line 3 def indexes @indexes end |
#sep_col ⇒ Object (readonly)
Returns the value of attribute sep_col.
2 3 4 |
# File 'lib/baza/base_sql_driver.rb', line 2 def sep_col @sep_col end |
#sep_table ⇒ Object (readonly)
Returns the value of attribute sep_table.
2 3 4 |
# File 'lib/baza/base_sql_driver.rb', line 2 def sep_table @sep_table end |
#sep_val ⇒ Object (readonly)
Returns the value of attribute sep_val.
2 3 4 |
# File 'lib/baza/base_sql_driver.rb', line 2 def sep_val @sep_val end |
#tables ⇒ Object
Returns the value of attribute tables.
3 4 5 |
# File 'lib/baza/base_sql_driver.rb', line 3 def tables @tables end |
Class Method Details
.from_object(args) ⇒ Object
5 6 |
# File 'lib/baza/base_sql_driver.rb', line 5 def self.from_object(args) end |
Instance Method Details
#esc_col(string) ⇒ Object Also known as: esc_table
Escapes a string to be used as a column.
32 33 34 35 36 |
# File 'lib/baza/base_sql_driver.rb', line 32 def esc_col(string) string = string.to_s raise "Invalid column-string: #{string}" if string.index(@sep_col) != nil return string end |
#escape(string) ⇒ Object Also known as: esc, escape_alternative
16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/baza/base_sql_driver.rb', line 16 def escape(string) return string.to_s.gsub(/([\0\n\r\032\'\"\\])/) do case $1 when "\0" then "\\0" when "\n" then "\\n" when "\r" then "\\r" when "\032" then "\\Z" else "\\#{$1}" end end end |
#insert_multi(tablename, arr_hashes, args = nil) ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/baza/base_sql_driver.rb', line 51 def insert_multi(tablename, arr_hashes, args = nil) sql = [] if args && args[:return_sql] @baza.transaction do arr_hashes.each do |hash| res = @baza.insert(tablename, hash, args) sql << res if args && args[:return_sql] end end return sql if args && args[:return_sql] return nil end |
#transaction ⇒ Object
40 41 42 43 44 45 46 47 48 49 |
# File 'lib/baza/base_sql_driver.rb', line 40 def transaction query("BEGIN TRANSACTION") begin yield @baza query("COMMIT") rescue => e query("ROLLBACK") end end |