Class: Tina4::Drivers::MongodbDriver

Inherits:
Object
  • Object
show all
Defined in:
lib/tina4/drivers/mongodb_driver.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#connectionObject (readonly)

Returns the value of attribute connection.



6
7
8
# File 'lib/tina4/drivers/mongodb_driver.rb', line 6

def connection
  @connection
end

#dbObject (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



92
93
94
95
96
97
98
99
# File 'lib/tina4/drivers/mongodb_driver.rb', line 92

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_transactionObject

MongoDB transactions require a replica set — wrap in session if available



102
103
104
# File 'lib/tina4/drivers/mongodb_driver.rb', line 102

def begin_transaction
  # no-op for standalone; transaction support via session handled externally
end

#closeObject



26
27
28
29
30
31
# File 'lib/tina4/drivers/mongodb_driver.rb', line 26

def close
  @client&.close
  @client = nil
  @db = nil
  @connection = nil
end

#columns(table_name) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/tina4/drivers/mongodb_driver.rb', line 118

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

#commitObject



106
107
108
# File 'lib/tina4/drivers/mongodb_driver.rb', line 106

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
24
# 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 one of:\n" \
          "    bundle add mongo     # if your project uses Bundler\n" \
          "    gem install mongo    # bare driver"
  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)



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
# File 'lib/tina4/drivers/mongodb_driver.rb', line 52

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



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/tina4/drivers/mongodb_driver.rb', line 34

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_idObject



79
80
81
# File 'lib/tina4/drivers/mongodb_driver.rb', line 79

def last_insert_id
  @last_insert_id
end

#placeholderObject



83
84
85
# File 'lib/tina4/drivers/mongodb_driver.rb', line 83

def placeholder
  "?"
end

#placeholders(count) ⇒ Object



87
88
89
# File 'lib/tina4/drivers/mongodb_driver.rb', line 87

def placeholders(count)
  (["?"] * count).join(", ")
end

#rollbackObject



110
111
112
# File 'lib/tina4/drivers/mongodb_driver.rb', line 110

def rollback
  # no-op
end

#tablesObject



114
115
116
# File 'lib/tina4/drivers/mongodb_driver.rb', line 114

def tables
  @db.collection_names.reject { |n| n.start_with?("system.") }
end