Class: Baza::BaseSqlDriver
- Inherits:
-
Object
- Object
- Baza::BaseSqlDriver
- Defined in:
- lib/baza/base_sql_driver.rb
Direct Known Subclasses
Driver::ActiveRecord, Driver::Sqlite3, Driver::Sqlite3Rhodes, JdbcDriver, MysqlBaseDriver
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
- #escape(string) ⇒ Object (also: #esc, #escape_alternative)
-
#escape_column(string) ⇒ Object
(also: #escape_table, #escape_database)
Escapes a string to be used as a column.
-
#initialize(baza) ⇒ BaseSqlDriver
constructor
A new instance of BaseSqlDriver.
-
#insert(tablename, arr_insert, args = nil) ⇒ Object
Simply inserts data into a table.
- #insert_multi(tablename, arr_hashes, args = nil) ⇒ Object
- #supports_multiple_databases? ⇒ Boolean
- #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
#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) string.to_s.gsub(/([\0\n\r\032\'\"\\])/) do case Regexp.last_match(1) when "\0" then "\\0" when "\n" then "\\n" when "\r" then "\\r" when "\032" then "\\Z" else "\\#{Regexp.last_match(1)}" end end end |
#escape_column(string) ⇒ Object Also known as: escape_table, escape_database
Escapes a string to be used as a column.
32 33 34 35 36 |
# File 'lib/baza/base_sql_driver.rb', line 32 def escape_column(string) string = string.to_s raise "Invalid column-string: #{string}" unless string.index(@sep_col).nil? string end |
#insert(tablename, arr_insert, args = nil) ⇒ Object
Simply inserts data into a table.
Examples
db.insert(:users, name: “John”, lastname: “Doe”) id = db.insert(:users, “John”, lastname: “Doe”, return_id: true) sql = db.insert(:users, “John”, lastname: “Doe”, return_sql: true) #=> “INSERT INTO ‘users` (`name`, `lastname`) VALUES (’John’, ‘Doe’)”
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/baza/base_sql_driver.rb', line 57 def insert(tablename, arr_insert, args = nil) sql = "INSERT INTO #{@sep_table}#{escape_table(tablename)}#{@sep_table}" if !arr_insert || arr_insert.empty? # This is the correct syntax for inserting a blank row in MySQL. if @baza.opts.fetch(:type).to_s.include?("mysql") sql << " VALUES ()" elsif @baza.opts.fetch(:type).to_s.include?("sqlite3") sql << " DEFAULT VALUES" else raise "Unknown database-type: '#{@baza.opts.fetch(:type)}'." end else sql << " (" first = true arr_insert.each_key do |key| if first first = false else sql << ", " end sql << "#{@baza.sep_col}#{@baza.escape_column(key)}#{@baza.sep_col}" end sql << ") VALUES (" first = true arr_insert.each_value do |value| if first first = false else sql << ", " end sql << @baza.sqlval(value) end sql << ")" end return sql if args && args[:return_sql] @baza.query(sql) return @baza.last_id if args && args[:return_id] nil end |
#insert_multi(tablename, arr_hashes, args = nil) ⇒ Object
106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/baza/base_sql_driver.rb', line 106 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] nil end |
#supports_multiple_databases? ⇒ Boolean
120 121 122 |
# File 'lib/baza/base_sql_driver.rb', line 120 def supports_multiple_databases? false end |
#transaction ⇒ Object
40 41 42 43 44 45 46 47 48 49 |
# File 'lib/baza/base_sql_driver.rb', line 40 def transaction @baza.q("BEGIN TRANSACTION") begin yield @baza @baza.q("COMMIT") rescue => e @baza.q("ROLLBACK") end end |