Class: Baza::Driver::ActiveRecord
- Inherits:
-
BaseSqlDriver
- Object
- BaseSqlDriver
- Baza::Driver::ActiveRecord
- Defined in:
- lib/baza/driver/active_record.rb
Defined Under Namespace
Classes: Columns, Commands, Databases, ForeignKeys, Indexes, Result, Tables, Users
Constant Summary
Constants inherited from BaseSqlDriver
BaseSqlDriver::SELECT_ARGS_ALLOWED_KEYS, BaseSqlDriver::SEPARATOR_COLUMN, BaseSqlDriver::SEPARATOR_DATABASE, BaseSqlDriver::SEPARATOR_INDEX, BaseSqlDriver::SEPARATOR_TABLE, BaseSqlDriver::SEPARATOR_VALUE
Instance Attribute Summary collapse
-
#cols ⇒ Object
Returns the value of attribute cols.
-
#conn ⇒ Object
readonly
Returns the value of attribute conn.
-
#db ⇒ Object
readonly
Returns the value of attribute db.
-
#driver_type ⇒ Object
readonly
Returns the value of attribute driver_type.
-
#indexes ⇒ Object
Returns the value of attribute indexes.
-
#sep_col ⇒ Object
readonly
Returns the value of attribute sep_col.
-
#sep_table ⇒ Object
readonly
Returns the value of attribute sep_table.
-
#sep_val ⇒ Object
readonly
Returns the value of attribute sep_val.
-
#symbolize ⇒ Object
readonly
Returns the value of attribute symbolize.
-
#tables ⇒ Object
Returns the value of attribute tables.
Attributes inherited from BaseSqlDriver
Class Method Summary collapse
Instance Method Summary collapse
- #close ⇒ Object
- #escape(str) ⇒ Object
- #escape_column(string) ⇒ Object
- #escape_table(string) ⇒ Object
-
#initialize(db) ⇒ ActiveRecord
constructor
A new instance of ActiveRecord.
- #postgres_connection_active? ⇒ Boolean
- #query(sql) ⇒ Object (also: #query_ubuf)
- #save_model!(model, args = {}) ⇒ Object
- #supports_multiple_databases? ⇒ Boolean
- #transaction ⇒ Object
Methods inherited from BaseSqlDriver
#count, #delete, escape, escape_column, #escape_database, escape_database, escape_index, #escape_index, escape_table, #foreign_key_support?, #insert, #insert_multi, quote_column, #quote_column, quote_database, #quote_database, quote_index, #quote_index, quote_table, #quote_table, #quote_value, quote_value, #select, #single, #sql_make_where
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 86 |
# 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 @conn.reconnect! unless postgres_connection_active? @db.opts[:db] ||= query("SELECT current_database()").fetch.values.first end end |
Instance Attribute Details
#cols ⇒ Object
Returns the value of attribute cols.
5 6 7 |
# File 'lib/baza/driver/active_record.rb', line 5 def cols @cols end |
#conn ⇒ Object (readonly)
Returns the value of attribute conn.
4 5 6 |
# File 'lib/baza/driver/active_record.rb', line 4 def conn @conn end |
#db ⇒ Object (readonly)
Returns the value of attribute db.
4 5 6 |
# File 'lib/baza/driver/active_record.rb', line 4 def db @db end |
#driver_type ⇒ Object (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 |
#indexes ⇒ Object
Returns the value of attribute indexes.
5 6 7 |
# File 'lib/baza/driver/active_record.rb', line 5 def indexes @indexes end |
#sep_col ⇒ Object (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_table ⇒ Object (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_val ⇒ Object (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 |
#symbolize ⇒ Object (readonly)
Returns the value of attribute symbolize.
4 5 6 |
# File 'lib/baza/driver/active_record.rb', line 4 def symbolize @symbolize end |
#tables ⇒ Object
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
#close ⇒ Object
116 117 118 119 120 |
# File 'lib/baza/driver/active_record.rb', line 116 def close @conn.close rescue ActiveRecord::ActiveRecordError => e raise e unless e. == "Cannot expire connection, it is not currently leased." end |
#escape(str) ⇒ Object
100 101 102 |
# File 'lib/baza/driver/active_record.rb', line 100 def escape(str) @conn.quote_string(str.to_s) end |
#escape_column(string) ⇒ Object
104 105 106 107 108 |
# File 'lib/baza/driver/active_record.rb', line 104 def escape_column(string) string = string.to_s raise "Invalid column-string: #{string}" if string.include?(@sep_col) string end |
#escape_table(string) ⇒ Object
110 111 112 113 114 |
# File 'lib/baza/driver/active_record.rb', line 110 def escape_table(string) string = string.to_s raise "Invalid column-string: #{string}" if string.include?(@sep_col) string end |
#postgres_connection_active? ⇒ Boolean
88 89 90 91 92 |
# File 'lib/baza/driver/active_record.rb', line 88 def postgres_connection_active? @conn.active? rescue PG::ConnectionBad true end |
#query(sql) ⇒ Object Also known as: query_ubuf
94 95 96 |
# File 'lib/baza/driver/active_record.rb', line 94 def query(sql) @result_constant.new(self, @conn.execute(sql)) end |
#save_model!(model, args = {}) ⇒ Object
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 |
# File 'lib/baza/driver/active_record.rb', line 144 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.compact 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
140 141 142 |
# File 'lib/baza/driver/active_record.rb', line 140 def supports_multiple_databases? conn_name.include?("mysql") || @driver_type == :pg end |
#transaction ⇒ Object
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/baza/driver/active_record.rb', line 122 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 |