Class: Baza::Driver::ActiveRecord

Inherits:
BaseSqlDriver 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

Methods inherited from BaseSqlDriver

#insert, #insert_multi

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
63
64
65
66
67
68
69
70
71
72
# File 'lib/baza/drivers/active_record.rb', line 32

def initialize(baza)
  @baza = baza
  @conn = @baza.opts.fetch(:conn)

  raise "No conn given" unless @conn

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

  if conn_name.include?("mysql2")
    require_relative "mysql2"
    require_relative "mysql2/result"

    @sep_table = "`"
    @sep_col = "`"
    @sep_val = "'"
    @driver_type = :mysql2
    @result_constant = Baza::Driver::Mysql2::Result
  elsif conn_name.include?("mysql")
    require_relative "mysql"
    require_relative "mysql/result"

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

  if conn_name.include?("mysql")
    @baza.opts[:db] ||= query("SELECT DATABASE()").fetch.fetch(:"DATABASE()")
  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

#driver_typeObject (readonly)

Returns the value of attribute driver_type.



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

def driver_type
  @driver_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

  nil
end

Instance Method Details

#closeObject



96
97
98
# File 'lib/baza/drivers/active_record.rb', line 96

def close
  @conn.close
end

#escape(str) ⇒ Object



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

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

#escape_column(string) ⇒ Object



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

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

#escape_table(string) ⇒ Object



90
91
92
93
94
# File 'lib/baza/drivers/active_record.rb', line 90

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

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



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

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

#supports_multiple_databases?Boolean

Returns:

  • (Boolean)


118
119
120
# File 'lib/baza/drivers/active_record.rb', line 118

def supports_multiple_databases?
  conn_name.include?("mysql")
end

#transactionObject



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/baza/drivers/active_record.rb', line 100

def transaction
  if @driver_type == :mysql || @driver_type == :mysql2
    query("START TRANSACTION")
  elsif @driver_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