Class: Baza::Driver::ActiveRecord

Inherits:
BaseSqlDriver show all
Defined in:
lib/baza/driver/active_record.rb

Defined Under Namespace

Classes: Columns, Commands, Databases, Indexes, Result, Tables, Users

Constant Summary

Constants inherited from BaseSqlDriver

BaseSqlDriver::SELECT_ARGS_ALLOWED_KEYS

Instance Attribute Summary collapse

Attributes inherited from BaseSqlDriver

#sep_database, #sep_index

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BaseSqlDriver

#count, #delete, #escape_database, #escape_index, #insert, #insert_multi, #select, #single, #sql_make_where, #sqlval

Constructor Details

#initialize(db) ⇒ ActiveRecord

Returns a new instance of ActiveRecord.



27
28
29
30
31
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
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/baza/driver/active_record.rb', line 27

def initialize(db)
  @db = db
  @conn = @db.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_database = "`"
    @sep_table = "`"
    @sep_col = "`"
    @sep_val = "'"
    @sep_index = "`"
    @driver_type = :mysql2
    @result_constant = Baza::Driver::Mysql2::Result
  elsif conn_name.include?("mysql")
    unless RUBY_PLATFORM == "java"
      require_relative "mysql"
      require_relative "mysql/result"
      @result_constant = Baza::Driver::Mysql::Result
    end

    @sep_database = "`"
    @sep_table = "`"
    @sep_col = "`"
    @sep_val = "'"
    @sep_index = "`"
    @driver_type = :mysql
  elsif conn_name.include?("sqlite")
    @sep_database = "`"
    @sep_table = "`"
    @sep_col = "`"
    @sep_val = "'"
    @sep_index = "`"
    @driver_type = :sqlite3
  elsif conn_name.include?("postgresqladapter")
    @sep_database = '"'
    @sep_table = '"'
    @sep_col = '"'
    @sep_index = '"'
    @sep_val = "'"
    @driver_type = :pg
    @result_constant = Baza::Driver::Pg::Result
  else
    raise "Unknown type: '#{conn_name}'"
  end

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

  if conn_name.include?("mysql")
    @db.opts[:db] ||= query("SELECT DATABASE()").fetch.fetch(:"DATABASE()")
  elsif @driver_type == :pg
    @db.opts[:db] ||= query("SELECT current_database()").fetch.values.first
  end
end

Instance Attribute Details

#colsObject

Returns the value of attribute cols.



5
6
7
# File 'lib/baza/driver/active_record.rb', line 5

def cols
  @cols
end

#connObject (readonly)

Returns the value of attribute conn.



4
5
6
# File 'lib/baza/driver/active_record.rb', line 4

def conn
  @conn
end

#dbObject (readonly)

Returns the value of attribute db.



4
5
6
# File 'lib/baza/driver/active_record.rb', line 4

def db
  @db
end

#driver_typeObject (readonly)

Returns the value of attribute driver_type.



4
5
6
# File 'lib/baza/driver/active_record.rb', line 4

def driver_type
  @driver_type
end

#indexesObject

Returns the value of attribute indexes.



5
6
7
# File 'lib/baza/driver/active_record.rb', line 5

def indexes
  @indexes
end

#sep_colObject (readonly)

Returns the value of attribute sep_col.



4
5
6
# File 'lib/baza/driver/active_record.rb', line 4

def sep_col
  @sep_col
end

#sep_tableObject (readonly)

Returns the value of attribute sep_table.



4
5
6
# File 'lib/baza/driver/active_record.rb', line 4

def sep_table
  @sep_table
end

#sep_valObject (readonly)

Returns the value of attribute sep_val.



4
5
6
# File 'lib/baza/driver/active_record.rb', line 4

def sep_val
  @sep_val
end

#symbolizeObject (readonly)

Returns the value of attribute symbolize.



4
5
6
# File 'lib/baza/driver/active_record.rb', line 4

def symbolize
  @symbolize
end

#tablesObject

Returns the value of attribute tables.



5
6
7
# File 'lib/baza/driver/active_record.rb', line 5

def tables
  @tables
end

Class Method Details

.from_object(args) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/baza/driver/active_record.rb', line 7

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



109
110
111
# File 'lib/baza/driver/active_record.rb', line 109

def close
  @conn.close
end

#escape(str) ⇒ Object



93
94
95
# File 'lib/baza/driver/active_record.rb', line 93

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

#escape_column(string) ⇒ Object



97
98
99
100
101
# File 'lib/baza/driver/active_record.rb', line 97

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

#escape_table(string) ⇒ Object



103
104
105
106
107
# File 'lib/baza/driver/active_record.rb', line 103

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



87
88
89
# File 'lib/baza/driver/active_record.rb', line 87

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

#save_model!(model, args = {}) ⇒ Object

Raises:

  • (ActiveRecord::InvalidRecord)


135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/baza/driver/active_record.rb', line 135

def save_model!(model, args = {})
  raise ActiveRecord::InvalidRecord, model if (!args.key?(:validate) || args[:validate]) && !model.valid?

  attributes = {}
  model.changes.each do |column_name, value_array|
    attributes[column_name.to_s] = value_array.last
  end

  attributes = attributes.delete_if { |_key, value| value.nil? } if model.new_record?

  table_name = model.class.table_name

  if model.new_record?
    if args[:update_on_duplicate_key]
      id = @db.upsert_duplicate_key(table_name, attributes, {}, return_id: true)
    else
      id = @db.insert(table_name, attributes, return_id: true)
    end

    if id && id.to_i > 0
      model.id = id
    else
      raise "Invalid ID: #{id}" unless id.to_i > 0
    end
  else
    @db.update(table_name, attributes, id: model.id)
  end

  model.reload
end

#supports_multiple_databases?Boolean

Returns:

  • (Boolean)


131
132
133
# File 'lib/baza/driver/active_record.rb', line 131

def supports_multiple_databases?
  conn_name.include?("mysql") || @driver_type == :pg
end

#transactionObject



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

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

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