Class: QueryKit::Connection
- Inherits:
-
Object
- Object
- QueryKit::Connection
- Defined in:
- lib/querykit/connection.rb
Overview
Connection class to manage database interactions.
Provides methods for executing queries, managing transactions, and optionally mapping results to model objects.
Instance Attribute Summary collapse
-
#adapter ⇒ Adapter
readonly
The database adapter instance.
Instance Method Summary collapse
-
#delete(table = nil) ⇒ DeleteQuery
Create a new DELETE query builder.
-
#execute_delete(query) ⇒ Object
Execute a delete query and return the number of affected rows.
-
#execute_insert(query) ⇒ Object
Execute an insert query and return the last insert ID.
-
#execute_scalar(query) ⇒ Object
Execute a query and return a scalar value (first column of first row) Useful for aggregate queries like COUNT, SUM, AVG, etc.
-
#execute_update(query) ⇒ Object
Execute an update query and return the number of affected rows.
-
#first(query, model_class = nil) ⇒ Hash, ...
Execute a SELECT query and return the first result.
-
#from(table) ⇒ Query
Create a new query builder for the specified table (alias for query).
-
#get(query, model_class = nil) ⇒ Array<Hash>, Array<Object>
Execute a SELECT query and return all results.
-
#initialize(adapter) ⇒ Connection
constructor
Initialize a new Connection with the given adapter.
-
#insert(table = nil) ⇒ InsertQuery
Create a new INSERT query builder.
-
#query(table = nil) ⇒ Query
Create a new query builder for the specified table.
-
#raw(sql, *bindings, model_class: nil) ⇒ Object
Raw SQL with optional model mapping.
-
#table(table) ⇒ Query
Create a new query builder for the specified table (alias for query).
-
#transaction ⇒ Object
Transaction support.
-
#update(table = nil) ⇒ UpdateQuery
Create a new UPDATE query builder.
Constructor Details
#initialize(adapter) ⇒ Connection
Initialize a new Connection with the given adapter.
33 34 35 |
# File 'lib/querykit/connection.rb', line 33 def initialize(adapter) @adapter = adapter end |
Instance Attribute Details
#adapter ⇒ Adapter (readonly)
Returns the database adapter instance.
24 25 26 |
# File 'lib/querykit/connection.rb', line 24 def adapter @adapter end |
Instance Method Details
#delete(table = nil) ⇒ DeleteQuery
Create a new DELETE query builder.
99 100 101 |
# File 'lib/querykit/connection.rb', line 99 def delete(table = nil) DeleteQuery.new(table) end |
#execute_delete(query) ⇒ Object
Execute a delete query and return the number of affected rows
156 157 158 159 160 |
# File 'lib/querykit/connection.rb', line 156 def execute_delete(query) sql = query.to_sql @adapter.execute(sql, query.bindings) @adapter.affected_rows end |
#execute_insert(query) ⇒ Object
Execute an insert query and return the last insert ID
142 143 144 145 146 |
# File 'lib/querykit/connection.rb', line 142 def execute_insert(query) sql = query.to_sql @adapter.execute(sql, query.bindings) @adapter.last_insert_id end |
#execute_scalar(query) ⇒ Object
Execute a query and return a scalar value (first column of first row) Useful for aggregate queries like COUNT, SUM, AVG, etc.
164 165 166 167 168 |
# File 'lib/querykit/connection.rb', line 164 def execute_scalar(query) result = first(query) return nil if result.nil? result.is_a?(Hash) ? result.values.first : result end |
#execute_update(query) ⇒ Object
Execute an update query and return the number of affected rows
149 150 151 152 153 |
# File 'lib/querykit/connection.rb', line 149 def execute_update(query) sql = query.to_sql @adapter.execute(sql, query.bindings) @adapter.affected_rows end |
#first(query, model_class = nil) ⇒ Hash, ...
Execute a SELECT query and return the first result.
132 133 134 135 136 137 138 139 |
# File 'lib/querykit/connection.rb', line 132 def first(query, model_class = nil) query.limit(1) results = @adapter.execute(query.to_sql, query.bindings) return nil if results.empty? row = results.first model_class ? map_to_model(row, model_class) : row end |
#from(table) ⇒ Query
Create a new query builder for the specified table (alias for query).
55 56 57 |
# File 'lib/querykit/connection.rb', line 55 def from(table) Query.new(table) end |
#get(query, model_class = nil) ⇒ Array<Hash>, Array<Object>
Execute a SELECT query and return all results.
115 116 117 118 119 120 121 |
# File 'lib/querykit/connection.rb', line 115 def get(query, model_class = nil) sql = query.to_sql results = @adapter.execute(sql, query.bindings) return results unless model_class results.map { |row| map_to_model(row, model_class) } end |
#insert(table = nil) ⇒ InsertQuery
Create a new INSERT query builder.
77 78 79 |
# File 'lib/querykit/connection.rb', line 77 def insert(table = nil) InsertQuery.new(table) end |
#query(table = nil) ⇒ Query
Create a new query builder for the specified table.
44 45 46 |
# File 'lib/querykit/connection.rb', line 44 def query(table = nil) Query.new(table) end |
#raw(sql, *bindings, model_class: nil) ⇒ Object
Raw SQL with optional model mapping
171 172 173 174 175 176 |
# File 'lib/querykit/connection.rb', line 171 def raw(sql, *bindings, model_class: nil) results = @adapter.execute(sql, bindings.flatten) return results unless model_class results.map { |row| map_to_model(row, model_class) } end |
#table(table) ⇒ Query
Create a new query builder for the specified table (alias for query).
66 67 68 |
# File 'lib/querykit/connection.rb', line 66 def table(table) Query.new(table) end |
#transaction ⇒ Object
Transaction support
179 180 181 182 183 184 185 186 187 |
# File 'lib/querykit/connection.rb', line 179 def transaction @adapter.begin_transaction result = yield @adapter.commit result rescue => e @adapter.rollback raise e end |
#update(table = nil) ⇒ UpdateQuery
Create a new UPDATE query builder.
88 89 90 |
# File 'lib/querykit/connection.rb', line 88 def update(table = nil) UpdateQuery.new(table) end |