API Reference
Global Configuration
QueryKit.configure { |config| ... }
Configure QueryKit with a block.
QueryKit.configure do |config|
config.adapter = :sqlite
config.connection_options = { database: 'app.db' }
end
QueryKit.setup(adapter, options)
Shorthand configuration.
QueryKit.setup(:sqlite, database: 'app.db')
QueryKit.setup(:postgresql, host: 'localhost', dbname: 'mydb', user: 'postgres', password: 'pass')
QueryKit.setup(:mysql, host: 'localhost', database: 'mydb', username: 'root', password: 'pass')
QueryKit.connection
Get the global connection. Raises QueryKit::ConfigurationError if not configured.
QueryKit.reset!
Reset configuration and connection (useful for testing).
QueryKit.connect(adapter, options)
Create a new connection without global configuration.
Query Builder
SELECT
query(table)/from(table)/table(table)- Start queryselect(*columns)- Select columnsdistinct- Add DISTINCTwhere(column, operator, value)- WHERE conditionwhere(column, value)- WHERE with = operatorwhere(hash)- WHERE with hashor_where(column, operator, value)- OR WHEREwhere_in(column, values)- WHERE INwhere_not_in(column, values)- WHERE NOT INwhere_null(column)- WHERE column IS NULLwhere_not_null(column)- WHERE column IS NOT NULLwhere_between(column, min, max)- WHERE BETWEENwhere_exists(subquery)- WHERE EXISTS (accepts Query or String)where_not_exists(subquery)- WHERE NOT EXISTS (accepts Query or String)where_raw(sql, *bindings)- Raw WHERE clause (Security: Use placeholders?for values, never string interpolation)join(table, column1, operator, column2)- INNER JOINleft_join(...)- LEFT JOINright_join(...)- RIGHT JOINcross_join(table)- CROSS JOINcount(column = '*')- Count aggregate (use with execute_scalar)avg(column)- Average aggregatesum(column)- Sum aggregatemin(column)- Minimum aggregatemax(column)- Maximum aggregateunion(query)- Combine with UNION (removes duplicates)union_all(query)- Combine with UNION ALL (keeps duplicates)order_by(column, direction = 'ASC')- ORDER BYorder_by_desc(column)- ORDER BY DESCgroup_by(*columns)- GROUP BYhaving(column, operator, value)- HAVINGlimit(count)/take(count)- LIMIToffset(count)/skip(count)- OFFSETpage(page_number, per_page)- Pagination helperto_sql- Get SQL stringbindings- Get bindings array
INSERT
insert(table)/into(table)- Start insertvalues(hash)- Single recordvalues(array_of_hashes)- Multiple recordsto_sql- Get SQL stringbindings- Get bindings array
UPDATE
update(table)- Start updateset(hash)- Set valueswhere(...)- Add WHERE conditions (same as SELECT)to_sql- Get SQL stringbindings- Get bindings array
DELETE
delete(table)/from(table)- Start deletewhere(...)- Add WHERE conditions (same as SELECT)to_sql- Get SQL stringbindings- Get bindings array
Connection
get(query, model_class = nil)- Execute SELECT, returns arrayfirst(query, model_class = nil)- Execute SELECT, returns first result or nilexecute_insert(query)- Execute INSERT, returns last_insert_idexecute_update(query)- Execute UPDATE, returns affected_rowsexecute_delete(query)- Execute DELETE, returns affected_rowsexecute_scalar(query)- Execute query, returns first column of first row (for aggregates)raw(sql, *bindings, model_class: nil)- Execute raw SQL (Security: Always use placeholders?for values)transaction { ... }- Execute block in transactionquery(table)- Start SELECT queryinsert(table)- Start INSERT queryupdate(table)- Start UPDATE querydelete(table)- Start DELETE query
Repository
Class Methods
table(name)- Set table namemodel(klass)- Set model class
Instance Methods
find(id)- Find by primary key (id column)find_by(column, value)- Find by any columnall- Get all recordswhere(column, operator, value)- WHERE querywhere(column, value)- WHERE with = operatorwhere_in(column, values)- WHERE INwhere_not_in(column, values)- WHERE NOT INfirst- Get first recordcount- Count recordsexists?(id = nil)- Check if records exist (or specific ID)create(attributes)/insert(attributes)- Insert record, returns IDupdate(id, attributes)- Update record, returns affected_rowsdelete(id)/destroy(id)- Delete record, returns affected_rowsdelete_where(conditions)- Bulk delete, returns affected_rowsexecute(query)- Execute custom query with model mappingexecute_first(query)- Execute custom query, return first resulttransaction { ... }- Execute block in transaction
Adapters
Three built-in adapters:
SQLiteAdapter
QueryKit.connect(:sqlite, 'database.db')
QueryKit.connect(:sqlite, ':memory:')
PostgreSQLAdapter
QueryKit.connect(:postgresql, host: 'localhost', dbname: 'mydb', user: 'postgres', password: 'pass')
MySQLAdapter
QueryKit.connect(:mysql, host: 'localhost', database: 'mydb', username: 'root', password: 'pass')
Return Values
- SELECT: Array of Hashes (or Models if model_class provided)
- INSERT: Integer (last_insert_id)
- UPDATE: Integer (affected_rows)
- DELETE: Integer (affected_rows)
- first(): Hash/Model or nil
- raw(): Array of Hashes (or Models if model_class provided)
Exceptions
ArgumentError- Invalid arguments (no table, no values, unknown adapter)QueryKit::ConfigurationError- AccessingQueryKit.connectionwithout configuration- Database-specific exceptions from drivers (sqlite3, pg, mysql2)