Class: ActiveRecord::ConnectionAdapters::MongoAdapter
- Inherits:
-
AbstractAdapter
- Object
- AbstractAdapter
- ActiveRecord::ConnectionAdapters::MongoAdapter
- Defined in:
- lib/active_record/connection_adapters/mongo_adapter.rb
Instance Method Summary collapse
- #delete_sql(sql, name = nil) ⇒ Object
-
#initialize(connection, logger, config) ⇒ MongoAdapter
constructor
A new instance of MongoAdapter.
- #insert_sql(sql, name = nil, pk = nil, id_value = nil, sequence_name = nil) ⇒ Object
- #select(sql, name = nil) ⇒ Object
- #supports_count_distinct? ⇒ Boolean
- #update_sql(sql, name = nil) ⇒ Object
Constructor Details
#initialize(connection, logger, config) ⇒ MongoAdapter
Returns a new instance of MongoAdapter.
25 26 27 28 |
# File 'lib/active_record/connection_adapters/mongo_adapter.rb', line 25 def initialize(connection, logger, config) super(connection, logger) @config = config end |
Instance Method Details
#delete_sql(sql, name = nil) ⇒ Object
98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/active_record/connection_adapters/mongo_adapter.rb', line 98 def delete_sql(sql, name = nil) log(sql, name) do parsed_sql = ActiveMongo::SQLParser.new(sql).parse table = parsed_sql[:table] coll = @connection.collection(table) selector = query2selector(parsed_sql) coll.remove(selector) end # XXX: 1 end |
#insert_sql(sql, name = nil, pk = nil, id_value = nil, sequence_name = nil) ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/active_record/connection_adapters/mongo_adapter.rb', line 64 def insert_sql(sql, name = nil, pk = nil, id_value = nil, sequence_name = nil) log(sql, name) do parsed_sql = ActiveMongo::SQLParser.new(sql).parse table, column_list, value_list = parsed_sql.values_at(:table, :column_list, :value_list) doc = {} column_list.zip(value_list).each {|k, v| doc[k] = v } coll = @connection.collection(table) coll.insert(doc).to_s end end |
#select(sql, name = nil) ⇒ Object
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/active_record/connection_adapters/mongo_adapter.rb', line 34 def select(sql, name = nil) log(sql, name) do parsed_sql = ActiveMongo::SQLParser.new(sql).parse table = parsed_sql[:table] coll = @connection.collection(table) count = parsed_sql[:count] distinct = parsed_sql[:distinct] selector = query2selector(parsed_sql) opts = query2opts(parsed_sql) if count and selector.empty? and opts.empty? [{count => coll.count}] elsif distinct coll.distinct(distinct, selector).map do |row| {distinct => row} end else rows = [] coll.find(selector, opts).each do |row| row['id'] = row['_id'].to_s if row rows << row end count ? [{count => rows.length}] : rows end end end |
#supports_count_distinct? ⇒ Boolean
30 31 32 |
# File 'lib/active_record/connection_adapters/mongo_adapter.rb', line 30 def supports_count_distinct? false end |
#update_sql(sql, name = nil) ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/active_record/connection_adapters/mongo_adapter.rb', line 77 def update_sql(sql, name = nil) log(sql, name) do parsed_sql = ActiveMongo::SQLParser.new(sql).parse table = parsed_sql[:table] coll = @connection.collection(table) selector = query2selector(parsed_sql) set_clause_list = parsed_sql[:set_clause_list] doc = {} set_clause_list.each do |k, v| k = k.split('.').last doc[k] = v end coll.update(selector, {'$set' => doc}, :multi => true) end # XXX: 1 end |