CQL Toolkit is a powerful library designed to simplify and enhance the management and execution of SQL queries in the Crystal programming language. It provides utilities for building, validating, and executing SQL statements, ensuring better performance and code maintainability.
- Query Builder: Programmatically create complex SQL queries.
- Insert, Update, Delete Operations: Perform CRUD operations with ease.
- Repository Pattern: Manage your data more effectively using
CQL::Repository(T)
. - Active Record Pattern: Work with your data models using
CQL::Record(T)
.
Add this to your application's shard.yml
:
dependencies:
cql:
github: azutoolkit/cql
Then, run the following command to install the dependencies:
shards install
Define the schema for your database tables:
schema = CQL::Schema.define(
:my_database,
adapter: CQL::Adapter::Postgres,
db: DB.open("postgresql://user:password@localhost:5432/database_name")
) do
table :users do
primary_key :id, Int64, auto_increment: true
column :name, String
column :email, String
end
end
With the schema in place, you can start executing queries:
q = CQL::Query.new(schema)
user = q.from(:users).where(id: 1).first(as: User)
puts user.name if user
Insert new records into the database:
q = CQL::Query.new(schema)
q.insert_into(:users, name: "Jane Doe", email: "jane@example.com")
Update existing records:
q = CQL::Query.new(schema)
q.update(:users).set(name: "Jane Smith").where(id: 1)
Delete records from the database:
q = CQL::Query.new(schema)
q.delete_from(:users).where(id: 1)
Utilize the repository pattern for organized data management:
user_repository = CQL::Repository(User, Int64).new(schema, :users)
# Create a new user
user_repository.create(id: 1, name: "Jane Doe", email: "jane@example.com")
# Fetch all users
users = user_repository.all
users.each { |user| puts user.name }
# Find a user by ID
user = user_repository.find!(1)
puts user.name
# Update a user by ID
user_repository.update(1, name: "Jane Smith")
Work with your data using the Active Record pattern:
AcmeDB = CQL::Schema.define(...) do ... end
struct User < CQL::Record(Int64)
db_context schema: AcmeDB, table: :users
# Crystal properties (no macros)
property id : Int64
property name : String
property email : String
end
user = User.find(1)
user.name = "Jane Smith"
user.save
Detailed API documentation is available at CQL Documentation.
Contributions are welcome! To contribute:
- Fork this repository.
- Create your feature branch:
git checkout -b my-new-feature
. - Start Postgres:
docker run --rm -e POSTGRES_DB=spec -e POSTGRES_PASSWORD=password -p 5432:5432 postgres
. - Run specs:
DATABASE_URL="postgres://postgres:password@localhost:5432/spec" crystal spec
. - Commit your changes:
git commit -am 'Add some feature'
. - Push to the branch:
git push origin my-new-feature
. - Create a new Pull Request.
This project is licensed under the MIT License. See the LICENSE file for details.
Thanks to all the contributors who helped in the development of this project.