Class: Baza::BaseSqlDriver

Inherits:
Object
  • Object
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

Constructor Details

#initialize(db) ⇒ BaseSqlDriver

Returns a new instance of BaseSqlDriver.



13
14
15
16
17
18
19
20
21
# File 'lib/baza/base_sql_driver.rb', line 13

def initialize(db)
  @db = db

  @sep_database = SEPARATOR_DATABASE
  @sep_table = SEPARATOR_TABLE
  @sep_col = SEPARATOR_COLUMN
  @sep_val = SEPARATOR_VALUE
  @sep_index = SEPARATOR_INDEX
end

Instance Attribute Details

#colsObject

Returns the value of attribute cols.



3
4
5
# File 'lib/baza/base_sql_driver.rb', line 3

def cols
  @cols
end

#connObject (readonly)

Returns the value of attribute conn.



2
3
4
# File 'lib/baza/base_sql_driver.rb', line 2

def conn
  @conn
end

#dbObject (readonly)

Returns the value of attribute db.



2
3
4
# File 'lib/baza/base_sql_driver.rb', line 2

def db
  @db
end

#indexesObject

Returns the value of attribute indexes.



3
4
5
# File 'lib/baza/base_sql_driver.rb', line 3

def indexes
  @indexes
end

#sep_colObject (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_databaseObject (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_indexObject (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_tableObject (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_valObject (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

#tablesObject

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



67
68
69
70
71
# File 'lib/baza/base_sql_driver.rb', line 67

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



77
78
79
80
81
# File 'lib/baza/base_sql_driver.rb', line 77

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



57
58
59
60
61
# File 'lib/baza/base_sql_driver.rb', line 57

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

.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.



216
217
218
219
220
221
222
223
224
225
226
227
228
# File 'lib/baza/base_sql_driver.rb', line 216

def self.sqlval(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



148
149
150
151
152
153
154
155
156
# File 'lib/baza/base_sql_driver.rb', line 148

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”)



171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/baza/base_sql_driver.rb', line 171

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



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



73
74
75
# File 'lib/baza/base_sql_driver.rb', line 73

def escape_database(string)
  self.class.escape_database(string)
end

#escape_index(string) ⇒ Object



83
84
85
# File 'lib/baza/base_sql_driver.rb', line 83

def escape_index(string)
  self.class.escape_index(string)
end

#escape_table(string) ⇒ Object



63
64
65
# File 'lib/baza/base_sql_driver.rb', line 63

def escape_table(string)
  self.class.escape_table(string)
end

#foreign_key_support?Boolean

Returns:

  • (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’)”



105
106
107
108
109
110
111
# File 'lib/baza/base_sql_driver.rb', line 105

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



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/baza/base_sql_driver.rb', line 113

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

#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.



138
139
140
141
142
143
144
145
146
# File 'lib/baza/base_sql_driver.rb', line 138

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”)



162
163
164
165
# File 'lib/baza/base_sql_driver.rb', line 162

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)



188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/baza/base_sql_driver.rb', line 188

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



230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
# File 'lib/baza/base_sql_driver.rb', line 230

def sqlval(val)
  return @conn.sqlval(val) if @conn.respond_to?(:sqlval)

  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

#supports_multiple_databases?Boolean

Returns:

  • (Boolean)


132
133
134
# File 'lib/baza/base_sql_driver.rb', line 132

def supports_multiple_databases?
  false
end

#transactionObject



87
88
89
90
91
92
93
94
95
96
97
# File 'lib/baza/base_sql_driver.rb', line 87

def transaction
  @db.q("BEGIN TRANSACTION")

  begin
    yield @db
    @db.q("COMMIT")
  rescue
    @db.q("ROLLBACK")
    raise
  end
end