Code Monkey home page Code Monkey logo

crystal-db's Introduction

Build Status

crystal-db

Common db api for crystal. You will need to have a specific driver to access a database.

Installation

If you are creating a shard that will work with any driver, then add crystal-db as a dependency in shard.yml:

dependencies:
  db:
    github: crystal-lang/crystal-db

If you are creating an application that will work with some specific driver(s), then add them in shard.yml:

dependencies:
  sqlite3:
    github: crystal-lang/crystal-sqlite3

crystal-db itself will be a nested dependency if drivers are included.

Note: Multiple drivers can be included in the same application.

Documentation

Usage

This shard only provides an abstract database API. In order to use it, a specific driver for the intended database has to be required as well:

The following example uses SQLite where ? indicates the arguments. If PostgreSQL is used $1, $2, etc. should be used. crystal-db does not interpret the statements.

require "db"
require "sqlite3"

DB.open "sqlite3:./file.db" do |db|
  # When using the pg driver, use $1, $2, etc. instead of ?
  db.exec "create table contacts (name text, age integer)"
  db.exec "insert into contacts values (?, ?)", "John Doe", 30

  args = [] of DB::Any
  args << "Sarah"
  args << 33
  db.exec "insert into contacts values (?, ?)", args: 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

Roadmap

Issues not yet addressed:

  • Support non prepared statements. #25
  • Time data type. (implementation details depends on actual drivers)
  • Data type extensibility. Allow each driver to extend the data types allowed.
  • Transactions & nested transactions. #27
  • Connection pool.
  • Logging
  • Direct access to IO to avoid memory allocation for blobs.

Contributing

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

  • bcardiff Brian J. Cardiff - creator, maintainer

crystal-db's People

Contributors

bcardiff avatar crisward avatar exts avatar fridgerator avatar ggiraldez avatar greenbigfrog avatar heaven31415 avatar hinrik avatar jgaskins avatar jkthorne avatar jwoertink avatar kaukas avatar nephos avatar nickbclifford avatar paulcsmith avatar robdavid avatar rx14 avatar spalladino avatar straight-shoota avatar vtambourine avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.