Class: Baza::Driver::ActiveRecord

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

Defined Under Namespace

Classes: Columns, Indexes, Result, Tables

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(baza) ⇒ ActiveRecord

Returns a new instance of ActiveRecord.



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/baza/drivers/active_record.rb', line 32

def initialize(baza)
  @baza = baza
  @conn = @baza.opts[:conn]

  raise 'No conn given' unless @conn

  conn_name = @conn.class.name.to_s.downcase

  if conn_name.include?("mysql2")
    @sep_table = "`"
    @sep_col = "`"
    @sep_val = "'"
    @conn_type = :mysql2
    @result_constant = Baza::Driver::Mysql2::Result
  elsif conn_name.include?("mysql")
    @sep_table = "`"
    @sep_col = "`"
    @sep_val = "'"
    @conn_type = :mysql
    @result_constant = Baza::Driver::Mysql::Result unless RUBY_PLATFORM == 'java'
  elsif conn_name.include?("sqlite")
    @sep_table = "`"
    @sep_col = "`"
    @sep_val = "'"
    @conn_type = :sqlite3
  else
    raise "Unknown type: '#{conn_name}'."
  end

  @result_constant ||= Baza::Driver::ActiveRecord::Result
end

Instance Attribute Details

#bazaObject (readonly)

Returns the value of attribute baza.



9
10
11
# File 'lib/baza/drivers/active_record.rb', line 9

def baza
  @baza
end

#colsObject

Returns the value of attribute cols.



10
11
12
# File 'lib/baza/drivers/active_record.rb', line 10

def cols
  @cols
end

#connObject (readonly)

Returns the value of attribute conn.



9
10
11
# File 'lib/baza/drivers/active_record.rb', line 9

def conn
  @conn
end

#conn_typeObject (readonly)

Returns the value of attribute conn_type.



9
10
11
# File 'lib/baza/drivers/active_record.rb', line 9

def conn_type
  @conn_type
end

#indexesObject

Returns the value of attribute indexes.



10
11
12
# File 'lib/baza/drivers/active_record.rb', line 10

def indexes
  @indexes
end

#sep_colObject (readonly)

Returns the value of attribute sep_col.



9
10
11
# File 'lib/baza/drivers/active_record.rb', line 9

def sep_col
  @sep_col
end

#sep_tableObject (readonly)

Returns the value of attribute sep_table.



9
10
11
# File 'lib/baza/drivers/active_record.rb', line 9

def sep_table
  @sep_table
end

#sep_valObject (readonly)

Returns the value of attribute sep_val.



9
10
11
# File 'lib/baza/drivers/active_record.rb', line 9

def sep_val
  @sep_val
end

#symbolizeObject (readonly)

Returns the value of attribute symbolize.



9
10
11
# File 'lib/baza/drivers/active_record.rb', line 9

def symbolize
  @symbolize
end

#tablesObject

Returns the value of attribute tables.



10
11
12
# File 'lib/baza/drivers/active_record.rb', line 10

def tables
  @tables
end

Class Method Details

.from_object(args) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/baza/drivers/active_record.rb', line 12

def self.from_object(args)
  if args[:object].class.name.include?("ActiveRecord::ConnectionAdapters")
    if args[:object].class.name.include?("ConnectionPool")
      object_to_use = args[:object].connection
    else
      object_to_use = args[:object]
    end

    return {
      type: :success,
      args: {
        type: :active_record,
        conn: object_to_use
      }
    }
  end

  return nil
end

Instance Method Details

#closeObject



86
87
88
# File 'lib/baza/drivers/active_record.rb', line 86

def close
  @conn.close
end

#esc_col(string) ⇒ Object



74
75
76
77
78
# File 'lib/baza/drivers/active_record.rb', line 74

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

#esc_table(string) ⇒ Object



80
81
82
83
84
# File 'lib/baza/drivers/active_record.rb', line 80

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

#escape(str) ⇒ Object



70
71
72
# File 'lib/baza/drivers/active_record.rb', line 70

def escape(str)
  @conn.quote_string(str.to_s)
end

#query(sql) ⇒ Object Also known as: query_ubuf



64
65
66
# File 'lib/baza/drivers/active_record.rb', line 64

def query(sql)
  @result_constant.new(self, @conn.execute(sql))
end

#transactionObject



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/baza/drivers/active_record.rb', line 90

def transaction
  if @conn_type == :mysql || @conn_type == :mysql2
    query("START TRANSACTION")
  elsif @conn_type == :sqlite3
    query("BEGIN TRANSACTION")
  else
    raise "Don't know how to start transaction"
  end

  begin
    yield @baza
    query("COMMIT")
  rescue
    query("ROLLBACK")
    raise
  end
end