Class: Baza::BaseSqlDriver
- Inherits:
-
Object
- Object
- Baza::BaseSqlDriver
show all
- Defined in:
- lib/baza/base_sql_driver.rb
Constant Summary
collapse
- SEPARATOR_DATABASE =
"`".freeze
- SEPARATOR_TABLE =
"`".freeze
- SEPARATOR_COLUMN =
"`".freeze
- SEPARATOR_VALUE =
"'".freeze
- SEPARATOR_INDEX =
"`".freeze
- SELECT_ARGS_ALLOWED_KEYS =
[:limit, :limit_from, :limit_to].freeze
Instance Attribute Summary collapse
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
-
#escape_database(string) ⇒ Object
-
#escape_index(string) ⇒ Object
-
#escape_table(string) ⇒ Object
-
#foreign_key_support? ⇒ Boolean
-
#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
-
#quote_column(column_name) ⇒ Object
-
#quote_database(database_name) ⇒ Object
-
#quote_index(index_name) ⇒ Object
-
#quote_table(table_name) ⇒ Object
-
#quote_value(val) ⇒ Object
-
#select(table_name, 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.
-
#supports_multiple_databases? ⇒ Boolean
-
#transaction ⇒ Object
Constructor Details
Returns a new instance of BaseSqlDriver.
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
Returns the value of attribute conn.
2
3
4
|
# File 'lib/baza/base_sql_driver.rb', line 2
def conn
@conn
end
|
#db ⇒ Object
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
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
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
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
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
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
.escape(string) ⇒ Object
27
28
29
30
31
32
33
34
35
36
37
|
# File 'lib/baza/base_sql_driver.rb', line 27
def self.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.
47
48
49
50
51
|
# File 'lib/baza/base_sql_driver.rb', line 47
def self.escape_column(string)
string = string.to_s
raise "Invalid column-string: #{string}" if string.include?(SEPARATOR_COLUMN)
string
end
|
.escape_database(string) ⇒ Object
83
84
85
86
87
|
# File 'lib/baza/base_sql_driver.rb', line 83
def self.escape_database(string)
string = string.to_s
raise "Invalid database-string: #{string}" if string.include?(SEPARATOR_DATABASE)
string
end
|
.escape_index(string) ⇒ Object
101
102
103
104
105
|
# File 'lib/baza/base_sql_driver.rb', line 101
def self.escape_index(string)
string = string.to_s
raise "Invalid index-string: #{string}" if string.include?(SEPARATOR_INDEX)
string
end
|
.escape_table(string) ⇒ Object
65
66
67
68
69
|
# File 'lib/baza/base_sql_driver.rb', line 65
def self.escape_table(string)
string = string.to_s
raise "Invalid table-string: #{string}" if string.include?(SEPARATOR_TABLE)
string
end
|
.from_object(_args) ⇒ Object
11
|
# File 'lib/baza/base_sql_driver.rb', line 11
def self.from_object(_args); end
|
.quote_column(column_name) ⇒ Object
57
58
59
|
# File 'lib/baza/base_sql_driver.rb', line 57
def self.quote_column(column_name)
"#{SEPARATOR_COLUMN}#{escape_column(column_name)}#{SEPARATOR_COLUMN}"
end
|
.quote_database(database_name) ⇒ Object
.quote_index(index_name) ⇒ Object
111
112
113
|
# File 'lib/baza/base_sql_driver.rb', line 111
def self.quote_index(index_name)
"#{SEPARATOR_INDEX}#{escape_index(index_name)}#{SEPARATOR_INDEX}"
end
|
.quote_table(table_name) ⇒ Object
75
76
77
|
# File 'lib/baza/base_sql_driver.rb', line 75
def self.quote_table(table_name)
"#{SEPARATOR_TABLE}#{escape_table(table_name)}#{SEPARATOR_TABLE}"
end
|
.quote_value(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.
248
249
250
251
252
253
254
255
256
257
258
259
260
|
# File 'lib/baza/base_sql_driver.rb', line 248
def self.quote_value(val)
if val.class.name == "Fixnum" || val.is_a?(Integer)
val.to_s
elsif val == nil
"NULL"
elsif val.is_a?(Date)
"#{SEPARATOR_VALUE}#{Datet.in(val).dbstr(time: false)}#{SEPARATOR_VALUE}"
elsif val.is_a?(Time) || val.is_a?(DateTime) || val.is_a?(Datet)
"#{SEPARATOR_VALUE}#{Datet.in(val).dbstr}#{SEPARATOR_VALUE}"
else
"#{SEPARATOR_VALUE}#{escape(val)}#{SEPARATOR_VALUE}"
end
end
|
Instance Method Details
#count(tablename, arr_terms = nil) ⇒ Object
180
181
182
183
184
185
186
187
188
|
# File 'lib/baza/base_sql_driver.rb', line 180
def count(tablename, arr_terms = nil)
sql = "SELECT COUNT(*) AS count FROM #{quote_table(tablename)}"
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”)
203
204
205
206
207
208
209
210
211
212
213
214
|
# File 'lib/baza/base_sql_driver.rb', line 203
def delete(tablename, arr_terms, args = nil)
sql = "DELETE FROM #{quote_table(tablename)}"
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
39
40
41
|
# File 'lib/baza/base_sql_driver.rb', line 39
def escape(string)
self.class.escape(string)
end
|
#escape_column(string) ⇒ Object
53
54
55
|
# File 'lib/baza/base_sql_driver.rb', line 53
def escape_column(string)
self.class.escape_column(string)
end
|
#escape_database(string) ⇒ Object
89
90
91
|
# File 'lib/baza/base_sql_driver.rb', line 89
def escape_database(string)
self.class.escape_database(string)
end
|
#escape_index(string) ⇒ Object
107
108
109
|
# File 'lib/baza/base_sql_driver.rb', line 107
def escape_index(string)
self.class.escape_index(string)
end
|
#escape_table(string) ⇒ Object
71
72
73
|
# File 'lib/baza/base_sql_driver.rb', line 71
def escape_table(string)
self.class.escape_table(string)
end
|
#foreign_key_support? ⇒ Boolean
23
24
25
|
# File 'lib/baza/base_sql_driver.rb', line 23
def foreign_key_support?
true
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’)”
137
138
139
140
141
142
143
|
# File 'lib/baza/base_sql_driver.rb', line 137
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
|
# File 'lib/baza/base_sql_driver.rb', line 145
def insert_multi(tablename, arr_hashes, args = {})
sql = [] if args && args[:return_sql]
if args && args[:return_sql]
arr_hashes.each do |hash|
sql << @db.insert(tablename, hash, args)
end
else
@db.transaction do
arr_hashes.each do |hash|
@db.insert(tablename, hash, args)
end
end
end
return sql if args && args[:return_sql]
nil
end
|
#quote_column(column_name) ⇒ Object
61
62
63
|
# File 'lib/baza/base_sql_driver.rb', line 61
def quote_column(column_name)
"#{sep_col}#{escape_column(column_name)}#{sep_col}"
end
|
#quote_database(database_name) ⇒ Object
97
98
99
|
# File 'lib/baza/base_sql_driver.rb', line 97
def quote_database(database_name)
"#{sep_database}#{escape_database(database_name)}#{sep_database}"
end
|
#quote_index(index_name) ⇒ Object
115
116
117
|
# File 'lib/baza/base_sql_driver.rb', line 115
def quote_index(index_name)
"#{sep_index}#{escape_index(index_name)}#{sep_index}"
end
|
#quote_table(table_name) ⇒ Object
79
80
81
|
# File 'lib/baza/base_sql_driver.rb', line 79
def quote_table(table_name)
"#{sep_table}#{escape_table(table_name)}#{sep_table}"
end
|
#quote_value(val) ⇒ Object
262
263
264
265
266
267
268
269
270
271
272
273
274
|
# File 'lib/baza/base_sql_driver.rb', line 262
def quote_value(val)
if val.class.name == "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
|
#select(table_name, 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.
170
171
172
173
174
175
176
177
178
|
# File 'lib/baza/base_sql_driver.rb', line 170
def select(table_name, terms = nil, args = nil, &block)
Baza::Commands::Select.new(
args: args,
block: block,
db: @db,
table_name: table_name,
terms: terms
).execute
end
|
#single(tablename, terms = nil, args = {}) ⇒ Object
Returns a single row from a database.
Examples
row = db.single(:users, lastname: “Doe”)
194
195
196
197
|
# File 'lib/baza/base_sql_driver.rb', line 194
def single(tablename, terms = nil, args = {})
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)
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
|
# File 'lib/baza/base_sql_driver.rb', line 220
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 << "#{quote_column(key)} IN (#{values})"
elsif value.is_a?(Hash)
raise "Dont know how to handle hash."
else
sql << "#{quote_column(key)} = #{quote_value(value)}"
end
end
sql
end
|
#supports_multiple_databases? ⇒ Boolean
164
165
166
|
# File 'lib/baza/base_sql_driver.rb', line 164
def supports_multiple_databases?
false
end
|
#transaction ⇒ Object
119
120
121
122
123
124
125
126
127
128
129
|
# File 'lib/baza/base_sql_driver.rb', line 119
def transaction
@db.q("BEGIN TRANSACTION")
begin
yield @db
@db.q("COMMIT")
rescue
@db.q("ROLLBACK")
raise
end
end
|