Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/azutoolkit/cql
CQL Toolkit is a comprehensive library designed to simplify and enhance the management and execution of SQL queries in Crystal. This toolkit provides utilities for building, validating, and executing SQL statements with ease, ensuring better performance and code maintainability.
https://github.com/azutoolkit/cql
crystal crystalang db mapper query-builder query-dsl sql
Last synced: 4 days ago
JSON representation
CQL Toolkit is a comprehensive library designed to simplify and enhance the management and execution of SQL queries in Crystal. This toolkit provides utilities for building, validating, and executing SQL statements with ease, ensuring better performance and code maintainability.
- Host: GitHub
- URL: https://github.com/azutoolkit/cql
- Owner: azutoolkit
- License: mit
- Created: 2024-07-12T23:01:45.000Z (4 months ago)
- Default Branch: master
- Last Pushed: 2024-09-28T19:05:56.000Z (about 1 month ago)
- Last Synced: 2024-10-25T01:20:05.889Z (12 days ago)
- Topics: crystal, crystalang, db, mapper, query-builder, query-dsl, sql
- Language: Crystal
- Homepage: https://azutopia.gitbook.io/cql
- Size: 2.79 MB
- Stars: 14
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[![Crystal CI](https://github.com/azutoolkit/cql/actions/workflows/crystal.yml/badge.svg)](https://github.com/azutoolkit/cql/actions/workflows/crystal.yml)
[![Codacy Badge](https://app.codacy.com/project/badge/Grade/a85e29e6b78849c28fb813397cc3eb1a)](https://app.codacy.com/gh/azutoolkit/cql/dashboard?utm_source=gh&utm_medium=referral&utm_content=&utm_campaign=Badge_grade)# CQL Toolkit
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.
## Table of Contents
- [CQL Toolkit](#cql-toolkit)
- [Table of Contents](#table-of-contents)
- [Features](#features)
- [Installation](#installation)
- [Getting Started](#getting-started)
- [1. Define a Schema](#1-define-a-schema)
- [2. Executing Queries](#2-executing-queries)
- [3. Inserting Data](#3-inserting-data)
- [4. Updating Data](#4-updating-data)
- [5. Deleting Data](#5-deleting-data)
- [6. Using the Repository Pattern](#6-using-the-repository-pattern)
- [7. Active Record Pattern](#7-active-record-pattern)
- [Documentation](#documentation)
- [Contributing](#contributing)
- [License](#license)
- [Acknowledgments](#acknowledgments)## Features
- **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)`.## Installation
Add this to your application's `shard.yml`:
```yaml
dependencies:
cql:
github: azutoolkit/cql
```Then, run the following command to install the dependencies:
```bash
shards install
```## Getting Started
### 1. Define a Schema
Define the schema for your database tables:
```crystal
schema = CQL::Schema.define(
:my_database,
adapter: CQL::Adapter::Postgres,
db: DB.open("postgresql://user:password@localhost:5432/database_name")
) dotable :users do
primary :id
varchar :name, size: 150
varchar :email, size: 150
end
end
```### 2. Executing Queries
With the schema in place, you can start executing queries:
```crystal
q = CQL::Query.new(schema)
user = q.from(:users).where(id: 1).first(as: User)puts user.name if user
```### 3. Inserting Data
Insert new records into the database:
```crystal
i = CQL::Insert.new(schema)
i.into(:users, name: "Jane Doe", email: "[email protected]")
```### 4. Updating Data
Update existing records:
```crystal
u = CQL::Update.new(schema)
u.table(:users).set(name: "Jane Smith").where(id: 1)
```### 5. Deleting Data
Delete records from the database:
```crystal
d = CQL::Delete.new(schema)d.from(:users).where(id: 1)
```### 6. Using the Repository Pattern
Utilize the repository pattern for organized data management:
```crystal
user_repository = CQL::Repository(User, Int64).new(schema, :users)# Create a new user
user_repository.create(id: 1, name: "Jane Doe", email: "[email protected]")# 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")
```### 7. Active Record Pattern
Work with your data using the Active Record pattern:
```crystal
AcmeDB = CQL::Schema.define(...) do ... endstruct User < CQL::Record(Int64)
db_context schema: AcmeDB, table: :users# Crystal properties (no macros)
property id : Int64
property name : String
property email : String
enduser = User.find(1)
user.name = "Jane Smith"
user.save
```## Documentation
Detailed API documentation is available at [CQL Documentation](https://azutopia.gitbook.io/cql/).
## Contributing
Contributions are welcome! To contribute:
1. Fork this repository.
2. Create your feature branch: `git checkout -b my-new-feature`.
3. Start Postgres: `docker run --rm -e POSTGRES_DB=spec -e POSTGRES_PASSWORD=password -p 5432:5432 postgres`.
4. Run specs: `DATABASE_URL="postgres://postgres:password@localhost:5432/spec" crystal spec`.
5. Commit your changes: `git commit -am 'Add some feature'`.
6. Push to the branch: `git push origin my-new-feature`.
7. Create a new Pull Request.## License
This project is licensed under the MIT License. See the [LICENSE](./LICENSE) file for details.
## Acknowledgments
Thanks to all the contributors who helped in the development of this project.