2016-06-24 20:27:06 +00:00
|
|
|
[![Build Status](https://travis-ci.org/crystal-lang/crystal-db.svg?branch=master)](https://travis-ci.org/crystal-lang/crystal-db)
|
2016-02-26 03:06:37 +00:00
|
|
|
|
2016-02-26 02:53:23 +00:00
|
|
|
# crystal-db
|
|
|
|
|
2018-11-07 18:44:14 +00:00
|
|
|
Common db api for crystal. You will need to have a specific driver to access a database.
|
2016-02-26 02:53:23 +00:00
|
|
|
|
2016-06-27 17:11:54 +00:00
|
|
|
* [sqlite](https://github.com/crystal-lang/crystal-sqlite3)
|
|
|
|
* [mysql](https://github.com/crystal-lang/crystal-mysql)
|
2016-12-13 11:53:55 +00:00
|
|
|
* [postgres](https://github.com/will/crystal-pg)
|
2018-11-20 13:14:17 +00:00
|
|
|
* [cassandra](https://github.com/kaukas/crystal-cassandra)
|
2016-02-26 02:53:23 +00:00
|
|
|
|
|
|
|
## Installation
|
|
|
|
|
|
|
|
Add this to your application's `shard.yml`:
|
|
|
|
|
|
|
|
```yaml
|
|
|
|
dependencies:
|
|
|
|
db:
|
2016-06-24 20:27:06 +00:00
|
|
|
github: crystal-lang/crystal-db
|
2016-02-26 02:53:23 +00:00
|
|
|
```
|
|
|
|
|
2016-12-21 21:17:45 +00:00
|
|
|
## Documentation
|
|
|
|
|
|
|
|
* [Latest API](http://crystal-lang.github.io/crystal-db/api/latest/)
|
|
|
|
* [Crystal book](https://crystal-lang.org/docs/database/)
|
|
|
|
|
2016-02-26 02:53:23 +00:00
|
|
|
## Usage
|
|
|
|
|
2019-02-26 18:13:28 +00:00
|
|
|
Since this is an abstract db api, its usage is through a concrete database driver.
|
2016-02-26 02:53:23 +00:00
|
|
|
|
|
|
|
```crystal
|
|
|
|
require "db"
|
|
|
|
require "sqlite3"
|
|
|
|
|
|
|
|
DB.open "sqlite3:./file.db" do |db|
|
2016-12-16 18:31:24 +00:00
|
|
|
# When using the pg driver, use $1, $2, etc. instead of ?
|
2017-11-13 13:52:07 +00:00
|
|
|
db.exec "create table contacts (name text, age integer)"
|
2016-02-26 02:53:23 +00:00
|
|
|
db.exec "insert into contacts values (?, ?)", "John Doe", 30
|
|
|
|
|
|
|
|
args = [] of DB::Any
|
|
|
|
args << "Sarah"
|
|
|
|
args << 33
|
|
|
|
db.exec "insert into contacts values (?, ?)", args
|
|
|
|
|
|
|
|
puts "max age:"
|
|
|
|
puts db.scalar "select max(age) from contacts" # => 33
|
|
|
|
|
|
|
|
puts "contacts:"
|
|
|
|
db.query "select name, age from contacts order by age desc" do |rs|
|
|
|
|
puts "#{rs.column_name(0)} (#{rs.column_name(1)})"
|
|
|
|
# => name (age)
|
|
|
|
rs.each do
|
|
|
|
puts "#{rs.read(String)} (#{rs.read(Int32)})"
|
|
|
|
# => Sarah (33)
|
|
|
|
# => John Doe (30)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
```
|
2016-02-26 03:06:37 +00:00
|
|
|
|
2016-02-26 02:53:23 +00:00
|
|
|
## Roadmap
|
|
|
|
|
|
|
|
Issues not yet addressed
|
|
|
|
|
2016-12-14 15:25:32 +00:00
|
|
|
- [x] Support non prepared statements. [#25](https://github.com/crystal-lang/crystal-db/pull/25)
|
2016-12-14 18:21:33 +00:00
|
|
|
- [x] Time data type. (implementation details depends on actual drivers)
|
2016-06-24 13:59:33 +00:00
|
|
|
- [x] Data type extensibility. Allow each driver to extend the data types allowed.
|
2016-12-14 15:25:32 +00:00
|
|
|
- [x] Transactions & nested transactions. [#27](https://github.com/crystal-lang/crystal-db/pull/27)
|
2016-12-07 22:28:15 +00:00
|
|
|
- [x] Connection pool.
|
2016-06-24 13:59:33 +00:00
|
|
|
- [ ] Direct access to `IO` to avoid memory allocation for blobs.
|
2016-02-26 02:53:23 +00:00
|
|
|
|
|
|
|
## Contributing
|
|
|
|
|
2016-06-24 20:27:06 +00:00
|
|
|
1. Fork it ( https://github.com/crystal-lang/crystal-db/fork )
|
2016-02-26 02:53:23 +00:00
|
|
|
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
|
|
|
|
|
|
|
|
- [bcardiff](https://github.com/bcardiff) Brian J. Cardiff - creator, maintainer
|