Class: Tina4::Drivers::MongodbDriver
- Inherits:
-
Object
- Object
- Tina4::Drivers::MongodbDriver
- Defined in:
- lib/tina4/drivers/mongodb_driver.rb
Instance Attribute Summary collapse
-
#connection ⇒ Object
readonly
Returns the value of attribute connection.
-
#db ⇒ Object
readonly
Returns the value of attribute db.
Instance Method Summary collapse
-
#apply_limit(sql, limit, offset = 0) ⇒ Object
MongoDB has no LIMIT clause — ignore; already handled in execute_query.
-
#begin_transaction ⇒ Object
MongoDB transactions require a replica set — wrap in session if available.
- #close ⇒ Object
- #columns(table_name) ⇒ Object
- #commit ⇒ Object
- #connect(connection_string, username: nil, password: nil) ⇒ Object
-
#execute(sql, params = []) ⇒ Object
Execute a DML statement (INSERT, UPDATE, DELETE, CREATE).
-
#execute_query(sql, params = []) ⇒ Object
Execute a query (SELECT-like) and return array of symbol-keyed hashes.
- #last_insert_id ⇒ Object
- #placeholder ⇒ Object
- #placeholders(count) ⇒ Object
- #rollback ⇒ Object
- #tables ⇒ Object
Instance Attribute Details
#connection ⇒ Object (readonly)
Returns the value of attribute connection.
6 7 8 |
# File 'lib/tina4/drivers/mongodb_driver.rb', line 6 def connection @connection end |
#db ⇒ Object (readonly)
Returns the value of attribute db.
6 7 8 |
# File 'lib/tina4/drivers/mongodb_driver.rb', line 6 def db @db end |
Instance Method Details
#apply_limit(sql, limit, offset = 0) ⇒ Object
MongoDB has no LIMIT clause — ignore; already handled in execute_query
91 92 93 94 95 96 97 98 |
# File 'lib/tina4/drivers/mongodb_driver.rb', line 91 def apply_limit(sql, limit, offset = 0) sql_up = sql.upcase return sql if sql_up.include?("LIMIT") modified = sql.dup modified += " LIMIT #{limit}" if limit && limit > 0 modified += " OFFSET #{offset}" if offset && offset > 0 modified end |
#begin_transaction ⇒ Object
MongoDB transactions require a replica set — wrap in session if available
101 102 103 |
# File 'lib/tina4/drivers/mongodb_driver.rb', line 101 def begin_transaction # no-op for standalone; transaction support via session handled externally end |
#close ⇒ Object
25 26 27 28 29 30 |
# File 'lib/tina4/drivers/mongodb_driver.rb', line 25 def close @client&.close @client = nil @db = nil @connection = nil end |
#columns(table_name) ⇒ Object
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/tina4/drivers/mongodb_driver.rb', line 117 def columns(table_name) collection = @db[table_name.to_s] sample = collection.find.limit(1).first return [] unless sample sample.keys.map do |key| { name: key, type: sample[key].class.name, nullable: true, default: nil, primary_key: key == "_id" } end end |
#commit ⇒ Object
105 106 107 |
# File 'lib/tina4/drivers/mongodb_driver.rb', line 105 def commit # no-op end |
#connect(connection_string, username: nil, password: nil) ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/tina4/drivers/mongodb_driver.rb', line 8 def connect(connection_string, username: nil, password: nil) begin require "mongo" rescue LoadError raise LoadError, "The 'mongo' gem is required for MongoDB connections. " \ "Install: gem install mongo" end uri = build_uri(connection_string, username, password) @db_name = extract_db_name(connection_string) @client = Mongo::Client.new(uri) @db = @client.use(@db_name) @connection = @db @last_insert_id = nil end |
#execute(sql, params = []) ⇒ Object
Execute a DML statement (INSERT, UPDATE, DELETE, CREATE)
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 |
# File 'lib/tina4/drivers/mongodb_driver.rb', line 51 def execute(sql, params = []) parsed = parse_sql(sql, params) collection = @db[parsed[:collection]] case parsed[:operation] when :insert result = collection.insert_one(parsed[:document]) @last_insert_id = result.inserted_id.to_s result when :update collection.update_many(parsed[:filter] || {}, { "$set" => parsed[:updates] }) when :delete collection.delete_many(parsed[:filter] || {}) when :create_collection begin @db.command(create: parsed[:collection].to_s) rescue Mongo::Error::OperationFailure # Collection already exists — ignore end nil when :find execute_query(sql, params) else nil end end |
#execute_query(sql, params = []) ⇒ Object
Execute a query (SELECT-like) and return array of symbol-keyed hashes
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/tina4/drivers/mongodb_driver.rb', line 33 def execute_query(sql, params = []) parsed = parse_sql(sql, params) collection = @db[parsed[:collection]] case parsed[:operation] when :find cursor = collection.find(parsed[:filter] || {}) cursor = cursor.projection(parsed[:projection]) if parsed[:projection] && !parsed[:projection].empty? cursor = cursor.sort(parsed[:sort]) if parsed[:sort] && !parsed[:sort].empty? cursor = cursor.skip(parsed[:skip]) if parsed[:skip] && parsed[:skip] > 0 cursor = cursor.limit(parsed[:limit]) if parsed[:limit] && parsed[:limit] > 0 cursor.map { |doc| mongo_doc_to_hash(doc) } else [] end end |
#last_insert_id ⇒ Object
78 79 80 |
# File 'lib/tina4/drivers/mongodb_driver.rb', line 78 def last_insert_id @last_insert_id end |
#placeholder ⇒ Object
82 83 84 |
# File 'lib/tina4/drivers/mongodb_driver.rb', line 82 def placeholder "?" end |
#placeholders(count) ⇒ Object
86 87 88 |
# File 'lib/tina4/drivers/mongodb_driver.rb', line 86 def placeholders(count) (["?"] * count).join(", ") end |
#rollback ⇒ Object
109 110 111 |
# File 'lib/tina4/drivers/mongodb_driver.rb', line 109 def rollback # no-op end |
#tables ⇒ Object
113 114 115 |
# File 'lib/tina4/drivers/mongodb_driver.rb', line 113 def tables @db.collection_names.reject { |n| n.start_with?("system.") } end |