• Stars
    star
    32
  • Rank 801,539 (Top 16 %)
  • Language
    Crystal
  • License
    MIT License
  • Created over 9 years ago
  • Updated about 8 years ago

Reviews

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

Repository Details

Basic mysql bindings for crystal.

crystal-mysql

Build Status

Fork of https://github.com/farleyknight/crystal-mysql

Basic MySQL bindings for Crystal.

CAUTION: Pre-alpha quality. Don't use for anything serious. Any bug reports and feedback are warmly welcome!

Installation

Add it to your shard.yml

dependencies:
  mysql:
    github: waterlink/crystal-mysql
    version: ~> 0.4

Usage

require "mysql"

Connecting to mysql

# MySQL.connect(host, user, password, database, port, socket, flags = 0)
conn = MySQL.connect("127.0.0.1", "crystal_mysql", "", "crystal_mysql_test", 3306_u16, nil)

Making a query

conn.query(%{SELECT 1})  #=> [[1]]

conn.query(%{CREATE TABLE user (id INT, email VARCHAR(255), name VARCHAR(255))})

conn.query(%{INSERT INTO user(id, email, name) values(1, "[email protected]", "John Smith")})
conn.query(%{INSERT INTO user(id, email, name) values(2, "[email protected]", "Sarah Smith")})

conn.query(%{SELECT * FROM user}) #=> [[1, "[email protected]", "John Smith"], [2, "[email protected]", "Sarah Smith"]]

conn.query(%{DROP TABLE user})

Using higher level Query api

MySQL::Query
  .new(%{SELECT * FROM user WHERE created_at > :from_filter},
       { "from_filter" => 14.days.ago })
  .run(conn)

You can reference parameters in query with symbol-like syntax: :some_symbol_like_syntax or :someSymbolLikeSyntax. And then you can resolve these references with passing a hash as a second argument, which specifies values for these parameters.

By the way all strings get properly escaped, so no SQL injections should be possible (if something is not escaped properly, then it is a bug, and you should probably report it here on github).

You can reference the same symbol multiple times in one query, as well you can use as much symbols as you want.

Making a transaction

other_conn = MySQL.connect("127.0.0.1", "crystal_mysql", "", "crystal_mysql_test", 3306_u16, nil)

conn.transaction do
  conn.query(%{SELECT COUNT(id) FROM user})  #=> 2
  conn.query(%{INSERT INTO user(id, email, name) values(1, "[email protected]", "James Smith")})

  conn.query(%{SELECT COUNT(id) FROM user})  #=> 3
  other_conn.query(%{SELECT COUNT(id) FROM user})  #=> 2
end

conn.query(%{SELECT COUNT(id) FROM user})  #=> 3
other_conn.query(%{SELECT COUNT(id) FROM user})  #=> 3

If block provided for #transaction raises exception, then it will rollback transaction automatically.

You can use #start_transaction, #commit_transaction and #rollback_transaction manually:

begin
  conn.start_transaction
  # .. do stuff with conn ..
  conn.commit_transaction
rescue
  conn.rollback_transaction
end

Nested transactions are possible.

Closing connection

conn.close

Notes

  • This library assumes tinyint is used as boolean type.

High-level API roadmap

High level method Implemented?
MySQL.connect Yes
Connection#initialize Yes
Connection#client_info Yes
Connection#error Yes
Support#escape_string Yes
Connection#connect Yes
Connection#host_info No
Connection#query 72% (usable)
Connection#start_transaction Yes
Connection#commit_transaction Yes
Connection#rollback_transaction Yes
Connection#transaction Yes
Connection#close Yes
Query#to_mysql Yes
Query#run Yes

TODO

  • Support more types: Enum, Set, Geometry, Binary strings
  • Figure out utf-8 (and other collations) support
  • Set up CI for different versions of mysql, ie: 5.5, 5.6, 5.7
  • Set up CI for mac os x
  • Figure out 32bit support?

Contributing

  1. Fork it ( https://github.com/waterlink/crystal-mysql/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

More Repositories

1

rack-reverse-proxy

A Reverse Proxy for Rack
Ruby
194
star
2

active_record.cr

Active Record pattern implementation for Crystal.
Crystal
191
star
3

Challenge-Build-Your-Own-Array-In-Js

This is a challenge that will allow you to practice your logical, analytical and problem-solving skills. Additionally, by the end of it you’ll have much better command of arrays in javascript.
JavaScript
177
star
4

rspec-json_expectations

Set of matchers and helpers to allow you test your APIs responses like a pro.
Gherkin
138
star
5

spec2.cr

Enhanced `spec` testing library for [Crystal](http://crystal-lang.org/).
Crystal
103
star
6

mocks.cr

General purpose mocking library for Crystal.
Crystal
51
star
7

timecop.cr

Mock with `Time.now` with the power of time travel, time freeze and time scale.
Crystal
19
star
8

refactoring-koans-js

Refactoring Koans to help you learn to refactor code smells in javascript
JavaScript
17
star
9

query.cr

Query abstraction for Crystal Language. Used by active_record.cr library.
Crystal
13
star
10

quick.cr

QuickCheck implementation for Crystal Language
Crystal
11
star
11

kotlin-spring-boot-mvc-starter

This is a starter repository for work with Kotlin on Back-end using Spring Boot 2 MVC, JdbcTemplate, Thymeleaf, Emails with Thymeleaf templates, Spring Security, Feature/UI tests using Fluentlenium, Clean Controller->Service->Repository pattern that is a sweet spot as your starting architecture. Includes a small demo in its source code.
Kotlin
11
star
12

postgres_adapter.cr

Postgres adapter for [active_record.cr](https://github.com/waterlink/active_record.cr). Uses [crystal-pg](https://github.com/will/crystal-pg) driver.
Crystal
9
star
13

spec2-mocks.cr

This library connects spec2.cr and mocks.cr, effectively enabling 'have_received' expectation for spec2.
Crystal
8
star
14

expand.cr

Crystal tool for macro debugging. Allows one to expand macro recursively.
Shell
6
star
15

mysql_adapter.cr

Mysql adapter for [active_record.cr](https://github.com/waterlink/active_record.cr). Uses [crystal-mysql library](https://github.com/waterlink/crystal-mysql)
Crystal
6
star
16

timestamp.cr

Timestamps in crystal-lang. Adds `.from_timestamp` and `#to_timestamp` methods to `Time`
Crystal
5
star
17

quizzykotlin

An example application for my free Getting Started With Kotlin Tutorial
Kotlin
4
star
18

rebecca

Simple database convenience wrapper for Go language.
Go
4
star
19

BuildYourOwnTestingFrameworkPart1

This is a source code for the first part of "Build Your Own Testing Framework" series
JavaScript
4
star
20

singleton.cr

Singleton library for Crystal Language.
Crystal
3
star
21

devpoll

devpoll - small web application for making polls written in http://crystal-lang.org/ (Crystal lang)
Crystal
3
star
22

aop

Very thin AOP gem for Ruby
Ruby
3
star
23

four-cycles-of-tdd-lightning-talk

Slides for my lightning talk: 4 Cycles of Test-Driven Development
HTML
3
star
24

money_tracking

CLI tool for tracking your expenses.
Ruby
2
star
25

restricted_struct

RestrictedStruct gem: create Struct-s with private or protected attributes
Ruby
2
star
26

contracts-rspec

Plugin for contracts.ruby that fixes issues with rspec-mocks.
Ruby
2
star
27

openproject-docker

Let OpenProject run in a docker container
Shell
2
star
28

race-conditions-testing-example

Kotlin
2
star
29

plugged

Library for writing extendable CLI applications for Golang.
Go
2
star
30

tspp_project

C++
2
star
31

messenger-ES6

JavaScript
1
star
32

tdd-talk-ru

TDD talk (version in Russian, for Top-Engineer webinar). | Доклад о TDD, для вебинара Top-Engineer.
HTML
1
star
33

goactor

Thin Actor implementation in Golang
Go
1
star
34

confident.ruby

Be confident and narrative when writing code in ruby
Ruby
1
star
35

secrets

Simple bash script to manage your secrets with symmetric key.
Shell
1
star
36

es-snapshot

This is a small script to make an elasticsearch snapshot. Tested with TDD in Bash in a very esoteric way.
Shell
1
star
37

crystal-blog

Simple blog in crystal using frank and active_record.cr
Crystal
1
star
38

cars-droid

Clojure
1
star
39

ShellTools

My bash shell tools. Unit-tested.
Shell
1
star
40

likes

Give it a list of people and their likings and it will tell what else could these people like. Ruby gem.
Ruby
1
star
41

property-based-testing-talk

HTML
1
star
42

docker-elasticsearch-kubernetes-aws

Docker image for elasticsearch with support for Kubernetes and AWS cloud
Shell
1
star
43

namegen.cr

This library provides facilities for generating random names/nicknames. Written in Crystal-Lang.
Crystal
1
star
44

stomp

Example game engine in Ruby. Uses Component Entity System + World paradigm.
Ruby
1
star
45

elm-todo

Simple ToDo application written in Elm lang. No backend (yet?).
Elm
1
star
46

explorative-tdd-talk

CSS
1
star
47

gas-template

Google Apps Script template for more comfortable development on local machine. Plus CI/CD included.
Shell
1
star
48

cars_api

Ruby
1
star
49

strong_ruby

Just playing around with strong typing in Ruby
Ruby
1
star
50

contracts-non_intrusive

Less intrusive version of Contracts DSL. Allows to use static/dynamic code analysis tools.
Ruby
1
star
51

LoginSignupE2E

Example project for the blog post "Learning TDD with JS: End-to-End Testing". Implements Login and Signup for a web-application using TDD and E2E feature tests.
JavaScript
1
star
52

recorder

Daemon that records all incoming HTTP request and allow to fetch them and put expectations on them. Useful for CLI tools testing.
Go
1
star