Class: Baza::BaseSqlDriver
- Inherits:
-
Object
- Object
- Baza::BaseSqlDriver
- Defined in:
- lib/baza/base_sql_driver.rb
Direct Known Subclasses
Driver::ActiveRecord, Driver::Pg, Driver::Sqlite3, Driver::Sqlite3Rhodes, JdbcDriver, MysqlBaseDriver
Constant Summary collapse
- SELECT_ARGS_ALLOWED_KEYS =
[:limit, :limit_from, :limit_to].freeze
Instance Attribute Summary collapse
-
#cols ⇒ Object
Returns the value of attribute cols.
-
#conn ⇒ Object
readonly
Returns the value of attribute conn.
-
#db ⇒ Object
readonly
Returns the value of attribute db.
-
#indexes ⇒ Object
Returns the value of attribute indexes.
-
#sep_col ⇒ Object
readonly
Returns the value of attribute sep_col.
-
#sep_database ⇒ Object
readonly
Returns the value of attribute sep_database.
-
#sep_index ⇒ Object
readonly
Returns the value of attribute sep_index.
-
#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
- #count(tablename, arr_terms = nil) ⇒ Object
-
#delete(tablename, arr_terms, args = nil) ⇒ Object
Deletes rows from the database.
- #escape(string) ⇒ Object (also: #esc, #escape_alternative)
-
#escape_column(string) ⇒ Object
Escapes a string to be used as a column.
- #escape_database(string) ⇒ Object
- #escape_index(string) ⇒ Object
- #escape_table(string) ⇒ Object
-
#initialize(db) ⇒ BaseSqlDriver
constructor
A new instance of BaseSqlDriver.
-
#insert(table_name, data, args = {}) ⇒ Object
Simply inserts data into a table.
- #insert_multi(tablename, arr_hashes, args = {}) ⇒ Object
-
#select(tablename, arr_terms = nil, args = nil, &block) ⇒ Object
Makes a select from the given arguments: table-name, where-terms and other arguments as limits and orders.
-
#single(tablename, terms = nil, args = {}) ⇒ Object
Returns a single row from a database.
-
#sql_make_where(arr_terms, _driver = nil) ⇒ Object
Internally used to generate SQL.
-
#sqlval(val) ⇒ Object
Returns the correct SQL-value for the given value.
- #supports_multiple_databases? ⇒ Boolean
- #transaction ⇒ Object
Constructor Details
#initialize(db) ⇒ BaseSqlDriver
Returns a new instance of BaseSqlDriver.
8 9 10 11 12 13 14 15 16 |
# File 'lib/baza/base_sql_driver.rb', line 8 def initialize(db) @db = db @sep_database = "`" @sep_table = "`" @sep_col = "`" @sep_val = "'" @sep_index = "`" end |
Instance Attribute Details
#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 |
#db ⇒ Object (readonly)
Returns the value of attribute db.
2 3 4 |
# File 'lib/baza/base_sql_driver.rb', line 2 def db @db 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_database ⇒ Object (readonly)
Returns the value of attribute sep_database.
2 3 4 |
# File 'lib/baza/base_sql_driver.rb', line 2 def sep_database @sep_database end |
#sep_index ⇒ Object (readonly)
Returns the value of attribute sep_index.
2 3 4 |
# File 'lib/baza/base_sql_driver.rb', line 2 def sep_index @sep_index 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
#count(tablename, arr_terms = nil) ⇒ Object
186 187 188 189 190 191 192 193 194 |
# File 'lib/baza/base_sql_driver.rb', line 186 def count(tablename, arr_terms = nil) sql = "SELECT COUNT(*) AS count FROM #{@sep_table}#{tablename}#{@sep_table}" if !arr_terms.nil? && !arr_terms.empty? sql << " WHERE #{sql_make_where(arr_terms)}" end query(sql).fetch.fetch(:count).to_i end |
#delete(tablename, arr_terms, args = nil) ⇒ Object
Deletes rows from the database.
Examples
db.delete(:users, “Doe”)
209 210 211 212 213 214 215 216 217 218 219 220 |
# File 'lib/baza/base_sql_driver.rb', line 209 def delete(tablename, arr_terms, args = nil) sql = "DELETE FROM #{@sep_table}#{tablename}#{@sep_table}" if !arr_terms.nil? && !arr_terms.empty? sql << " WHERE #{sql_make_where(arr_terms)}" end return sql if args && args[:return_sql] query(sql) nil end |
#escape(string) ⇒ Object Also known as: esc, escape_alternative
18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/baza/base_sql_driver.rb', line 18 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
Escapes a string to be used as a column.
34 35 36 37 38 |
# File 'lib/baza/base_sql_driver.rb', line 34 def escape_column(string) string = string.to_s raise "Invalid column-string: #{string}" if string.include?(@sep_col) string end |
#escape_database(string) ⇒ Object
46 47 48 49 50 |
# File 'lib/baza/base_sql_driver.rb', line 46 def escape_database(string) string = string.to_s raise "Invalid database-string: #{string}" if string.include?(@sep_database) string end |
#escape_index(string) ⇒ Object
52 53 54 55 56 |
# File 'lib/baza/base_sql_driver.rb', line 52 def escape_index(string) string = string.to_s raise "Invalid index-string: #{string}" if string.include?(@sep_index) string end |
#escape_table(string) ⇒ Object
40 41 42 43 44 |
# File 'lib/baza/base_sql_driver.rb', line 40 def escape_table(string) string = string.to_s raise "Invalid table-string: #{string}" if string.include?(@sep_table) string end |
#insert(table_name, data, args = {}) ⇒ 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’)”
76 77 78 79 80 81 82 |
# File 'lib/baza/base_sql_driver.rb', line 76 def insert(table_name, data, args = {}) Baza::SqlQueries::GenericInsert.new({ db: @db, table_name: table_name, data: data }.merge(args)).execute end |
#insert_multi(tablename, arr_hashes, args = {}) ⇒ Object
84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/baza/base_sql_driver.rb', line 84 def insert_multi(tablename, arr_hashes, args = {}) sql = [] if args && args[:return_sql] @db.transaction do arr_hashes.each do |hash| res = @db.insert(tablename, hash, args) sql << res if args && args[:return_sql] end end return sql if args && args[:return_sql] nil end |
#select(tablename, arr_terms = nil, args = nil, &block) ⇒ Object
Makes a select from the given arguments: table-name, where-terms and other arguments as limits and orders. Also takes a block to avoid raping of memory.
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 |
# File 'lib/baza/base_sql_driver.rb', line 104 def select(tablename, arr_terms = nil, args = nil, &block) # Set up vars. sql = "" args_q = nil select_sql = "*" # Give 'cloned_ubuf' argument to 'q'-method. args_q = {cloned_ubuf: true} if args && args[:cloned_ubuf] # Set up IDQuery-stuff if that is given in arguments. if args && args[:idquery] if args.fetch(:idquery) == true select_sql = "#{sep_col}id#{sep_col}" col = :id else select_sql = "#{sep_col}#{escape_column(args.fetch(:idquery))}#{sep_col}" col = args.fetch(:idquery) end end sql = "SELECT #{select_sql} FROM" if tablename.is_a?(Array) sql << " #{@sep_table}#{tablename.first}#{@sep_table}.#{@sep_table}#{tablename.last}#{@sep_table}" else sql << " #{@sep_table}#{tablename}#{@sep_table}" end if !arr_terms.nil? && !arr_terms.empty? sql << " WHERE #{sql_make_where(arr_terms)}" end unless args.nil? if args[:orderby] sql << " ORDER BY" if args.fetch(:orderby).is_a?(Array) first = true args.fetch(:orderby).each do |order_by| sql << "," unless first first = false if first sql << " #{sep_col}#{escape_column(order_by)}#{sep_col}" end else sql << " #{sep_col}#{escape_column(args.fetch(:orderby))}#{sep_col}" end end sql << " LIMIT #{args[:limit]}" if args[:limit] if args[:limit_from] && args[:limit_to] begin Float(args[:limit_from]) rescue raise "'limit_from' was not numeric: '#{args.fetch(:limit_from)}'." end begin Float(args[:limit_to]) rescue raise "'limit_to' was not numeric: '#{args[:limit_to]}'." end sql << " LIMIT #{args.fetch(:limit_from)}, #{args.fetch(:limit_to)}" end end # Do IDQuery if given in arguments. if args && args[:idquery] res = Baza::Idquery.new(db: @db, table: tablename, query: sql, col: col, &block) else res = @db.q(sql, args_q, &block) end # Return result if a block wasnt given. if block return nil else return res end end |
#single(tablename, terms = nil, args = {}) ⇒ Object
Returns a single row from a database.
Examples
row = db.single(:users, lastname: “Doe”)
200 201 202 203 |
# File 'lib/baza/base_sql_driver.rb', line 200 def single(tablename, terms = nil, args = {}) # Experienced very weird memory leak if this was not done by block. Maybe bug in Ruby 1.9.2? - knj select(tablename, terms, args.merge(limit: 1)).fetch end |
#sql_make_where(arr_terms, _driver = nil) ⇒ Object
Internally used to generate SQL.
Examples
sql = db.sql_make_where(“Doe”, driver_obj)
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 |
# File 'lib/baza/base_sql_driver.rb', line 226 def sql_make_where(arr_terms, _driver = nil) sql = "" first = true arr_terms.each do |key, value| if first first = false else sql << " AND " end if value.is_a?(Array) raise "Array for column '#{key}' was empty." if value.empty? values = value.map { |v| "'#{escape(v)}'" }.join(",") sql << "#{@sep_col}#{key}#{@sep_col} IN (#{values})" elsif value.is_a?(Hash) raise "Dont know how to handle hash." else sql << "#{@sep_col}#{key}#{@sep_col} = #{sqlval(value)}" end end sql end |
#sqlval(val) ⇒ Object
Returns the correct SQL-value for the given value. If it is a number, then just the raw number as a string will be returned. nil’s will be NULL and strings will have quotes and will be escaped.
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 |
# File 'lib/baza/base_sql_driver.rb', line 254 def sqlval(val) return @conn.sqlval(val) if @conn.respond_to?(:sqlval) if val.is_a?(Fixnum) || val.is_a?(Integer) val.to_s elsif val == nil "NULL" elsif val.is_a?(Date) "#{@sep_val}#{Datet.in(val).dbstr(time: false)}#{@sep_val}" elsif val.is_a?(Time) || val.is_a?(DateTime) || val.is_a?(Datet) "#{@sep_val}#{Datet.in(val).dbstr}#{@sep_val}" else "#{@sep_val}#{escape(val)}#{@sep_val}" end end |
#supports_multiple_databases? ⇒ Boolean
98 99 100 |
# File 'lib/baza/base_sql_driver.rb', line 98 def supports_multiple_databases? false end |
#transaction ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/baza/base_sql_driver.rb', line 58 def transaction @db.q("BEGIN TRANSACTION") begin yield @db @db.q("COMMIT") rescue @db.q("ROLLBACK") raise end end |