• Stars
    star
    115
  • Rank 305,916 (Top 7 %)
  • Language
    Crystal
  • License
    MIT License
  • Created about 8 years ago
  • Updated over 3 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Give your SQL some love

Tren Logo

Build Status

Tren lets you use your SQL as a first class method in your Crystal code.

Installation

Add this to your application's shard.yml:

dependencies:
  tren:
    github: sdogruyol/tren

Usage

Create a simple SQL file. The first line must be -- name: your_method_name(args).

-- 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.

require "tren"

Tren.load("/path/to/your/file.sql")

# Or you can load multiple files at once:
Tren.load("./db/**/*.sql")

Overloading

-- name: get_users(name : String, surname : String)

SELECT * FROM users WHERE name = '{{ name }}' AND surname = '{{ surname }}'
-- 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.

-- 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.

-- name: filter_user(name : String, surname : String)

WHERE name = '{{ name }}' AND surname = '{{ surname }}'

Let's reuse this now:

-- 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.

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 Fatih Kadir Akın - creator, maintainer
  • sdogruyol Serdar Doğruyol - creator, maintainer

Built on a TREN from Ankara to Istanbul.

More Repositories

1

awesome-ruby

A curated list of awesome Ruby frameworks, libraries and resources. Inspired by awesome-php & awesome-python.
1,247
star
2

fast-http-server

Super fast, zero configuration command line HTTP Server.
Crystal
148
star
3

cryload

HTTP benchmarking tool written in Crystal
Crystal
121
star
4

aws

Unofficial AWS SDK for Crystal
Crystal
70
star
5

kemal-chat

Sample application to demonstrate how easy it is to build realtime applications with Kemal using WebSocket.
HTML
63
star
6

cloud_backup

Easily dump your database and store it in the cloud.
Ruby
38
star
7

gencleredestek.com

gencleredestek.com website source code
Ruby
36
star
8

crystal-weekly

Crystal Weekly Website
SCSS
30
star
9

friends-of-crystal

Organizations running Crystal in production
HTML
30
star
10

capistrano-kemal

Capistrano integration for Kemal
Ruby
25
star
11

cuba-generator

Application Generator for Cuba framework
Ruby
23
star
12

crystal_for_rubyists

Crystal For Rubyists
HTML
23
star
13

kemal-github-api

Github API routes benchmark for Kemal
Crystal
21
star
14

kemal-pg

Easily add Postgresql database to Kemal
Crystal
21
star
15

crystal-for-rubyists-en

20
star
16

kemal-mysql

Easily add MySQL database to your Kemal application.
Crystal
14
star
17

safran

Safran için Ruby ile yazılmış Command Line okuyucusu.
Ruby
13
star
18

rust-vs-ruby

Sample applications for 'Rust vs Ruby: Building an API' blog post
Rust
11
star
19

kemal-showdown

🏇 benchmark Kemal with Sinatra-like web frameworks
JavaScript
10
star
20

multipart

Multipart Form Data parser for Crystal
Crystal
10
star
21

safran-api

Safran Unofficial API
Ruby
8
star
22

amethyst-bin

Bash script for automatically setting up an Amethyst skeleton project including dependencies
Shell
7
star
23

calculator-tdd

An example of applied TDD with a simple calculator
Ruby
7
star
24

cuba-datamapper-sample

Sample application to show the simplicity and power of Cuba
Ruby
6
star
25

lyk2017-blog

Ruby
6
star
26

kemal-redis

Easily add Redis to Kemal
Crystal
6
star
27

sdogruyol.github.io

serdardogruyol.com source code
SCSS
5
star
28

hn_reader

Hacker News Reader Gui built with Jruby
Ruby
5
star
29

kemal-blog

CSS
4
star
30

russh

Russh: Ruby + SSH. SSH Config Management the Ruby way.
Ruby
4
star
31

kemal-pg-sample

Sample app to demonstrate kemal + postgresql usage
Crystal
4
star
32

critcoin

Crystal
3
star
33

crystal-for-rubyists-cn

Crystal for Rubyists Chinese Version
2
star
34

lyk2015-ruby101

Linux Yaz Kampı 2015 Ruby 101 içeriği
JavaScript
1
star
35

elixir_schizo

Example project from Elixir Sips
Elixir
1
star
36

kemal-multi-path

Crystal
1
star
37

erc20-tracker

Simple ERC20 token tracker
JavaScript
1
star
38

kemalturkiye.org

HTML
1
star
39

homebrew-cryload

Brew tap for Cryload
Ruby
1
star