Class: Baza::BaseSqlDriver

Inherits:
Object
  • Object
show all
Defined in:
lib/baza/base_sql_driver.rb

Constant Summary collapse

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.



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

#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

.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



118
119
120
121
122
123
124
125
126
# File 'lib/baza/base_sql_driver.rb', line 118

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



141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/baza/base_sql_driver.rb', line 141

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



22
23
24
25
26
27
28
29
30
31
32
# File 'lib/baza/base_sql_driver.rb', line 22

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.



38
39
40
41
42
# File 'lib/baza/base_sql_driver.rb', line 38

def escape_column(string)
  string = string.to_s
  raise "Invalid column-string: #{string}" if string.include?(@sep_col)
  string
end

#escape_database(string) ⇒ Object



50
51
52
53
54
# File 'lib/baza/base_sql_driver.rb', line 50

def escape_database(string)
  string = string.to_s
  raise "Invalid database-string: #{string}" if string.include?(@sep_database)
  string
end

#escape_index(string) ⇒ Object



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

def escape_index(string)
  string = string.to_s
  raise "Invalid index-string: #{string}" if string.include?(@sep_index)
  string
end

#escape_table(string) ⇒ Object



44
45
46
47
48
# File 'lib/baza/base_sql_driver.rb', line 44

def escape_table(string)
  string = string.to_s
  raise "Invalid table-string: #{string}" if string.include?(@sep_table)
  string
end

#foreign_key_support?Boolean

Returns:

  • (Boolean)


18
19
20
# File 'lib/baza/base_sql_driver.rb', line 18

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



80
81
82
83
84
85
86
# File 'lib/baza/base_sql_driver.rb', line 80

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



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

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



108
109
110
111
112
113
114
115
116
# File 'lib/baza/base_sql_driver.rb', line 108

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



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

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)



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/baza/base_sql_driver.rb', line 158

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.



186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/baza/base_sql_driver.rb', line 186

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

Returns:

  • (Boolean)


102
103
104
# File 'lib/baza/base_sql_driver.rb', line 102

def supports_multiple_databases?
  false
end

#transactionObject



62
63
64
65
66
67
68
69
70
71
72
# File 'lib/baza/base_sql_driver.rb', line 62

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

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