Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/sdogruyol/tren
Give your SQL some love
https://github.com/sdogruyol/tren
Last synced: about 1 month ago
JSON representation
Give your SQL some love
- Host: GitHub
- URL: https://github.com/sdogruyol/tren
- Owner: sdogruyol
- License: mit
- Created: 2016-10-08T15:03:49.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2021-08-12T10:08:22.000Z (over 3 years ago)
- Last Synced: 2024-08-01T17:36:12.886Z (4 months ago)
- Language: Crystal
- Homepage:
- Size: 44.9 KB
- Stars: 116
- Watchers: 10
- Forks: 4
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
- awesome-crystal - tren - Give your SQL some love (Misc)
- awesome-crystal - tren - Give your SQL some love (Misc)
README
![Tren Logo](logo.png)
[![Build Status](https://travis-ci.org/sdogruyol/tren.svg?branch=master)](https://travis-ci.org/sdogruyol/tren)
Tren lets you use your SQL as a first class method in your Crystal code.
## Installation
Add this to your application's `shard.yml`:
```yaml
dependencies:
tren:
github: sdogruyol/tren
```## Usage
Create a simple SQL file. The first line must be `-- name: your_method_name(args)`.
```sql
-- name: get_users(name : String, surname : String)SELECT * FROM users WHERE name = '{{ name }}' AND surname = '{{ surname }}'
```Require Tren and load your SQL file. It's going to create a first class method to your SQL.
```crystal
require "tren"Tren.load("/path/to/your/file.sql")
# Or you can load multiple files at once:
Tren.load("./db/**/*.sql")
```### Overloading
```sql
-- name: get_users(name : String, surname : String)SELECT * FROM users WHERE name = '{{ name }}' AND surname = '{{ surname }}'
``````sql
-- name: get_users(name : String, age : Int32)SELECT * FROM users WHERE name = '{{ name }}' AND age = {{ age }}
```#### Meaningful Compiler Errors
Since Tren is creating _native_ Crystal methods, you can see meaningful errors on compile process.
```
in ./your/file: no overload matches 'get_users' with types String, Bool
Overloads are:
- get_users(name : String, surname : String)
- get_users(name : String, age : Int32)get_users("john", true)
```### Prevent SQL Injections
SQL statements are escaped by default. But you are able to make injectable (raw) parameters by writing `!` to the parameter.
```sql
-- name: get_users(name : String, surname : String)SELECT * FROM users WHERE name = '{{! name }}' AND surname = '{{! surname }}'
```### Composing SQLs
You can compose Tren methods easily to be DRY.
```sql
-- name: filter_user(name : String, surname : String)WHERE name = '{{ name }}' AND surname = '{{ surname }}'
```Let's reuse this now:
```sql
-- name: get_users(name : String, surname : String)SELECT * FROM users {{! filter_user(name, surname) }}
```### Escape Character
For example PostgreSQL uses `''` for escaping string. To set your escape character for `postgresql`.
```ruby
Tren.escape_character = "\\'"
```## Contributing
1. Fork it ( https://github.com/sdogruyol/tren/fork )
2. Create your feature branch (git checkout -b my-new-feature)
3. Commit your changes (git commit -am 'Add some feature')
4. Push to the branch (git push origin my-new-feature)
5. Create a new Pull Request## Contributors
- [f](https://github.com/f) Fatih Kadir Akın - creator, maintainer
- [sdogruyol](https://github.com/sdogruyol) Serdar Doğruyol - creator, maintainer_Built on a TREN from Ankara to Istanbul._