Class: Baza::BaseSqlDriver

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#bazaObject (readonly)

Returns the value of attribute baza.



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

def baza
  @baza
end

#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

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

#esc_col(string) ⇒ Object Also known as: esc_table

Escapes a string to be used as a column.



32
33
34
35
36
# File 'lib/baza/base_sql_driver.rb', line 32

def esc_col(string)
  string = string.to_s
  raise "Invalid column-string: #{string}" if string.index(@sep_col) != nil
  return string
end

#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)
  return string.to_s.gsub(/([\0\n\r\032\'\"\\])/) do
    case $1
      when "\0" then "\\0"
      when "\n" then "\\n"
      when "\r" then "\\r"
      when "\032" then "\\Z"
      else "\\#{$1}"
    end
  end
end

#insert_multi(tablename, arr_hashes, args = nil) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/baza/base_sql_driver.rb', line 51

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]
  return nil
end

#transactionObject



40
41
42
43
44
45
46
47
48
49
# File 'lib/baza/base_sql_driver.rb', line 40

def transaction
  query("BEGIN TRANSACTION")

  begin
    yield @baza
    query("COMMIT")
  rescue => e
    query("ROLLBACK")
  end
end