All Projects → piisalie → sql_capsule

piisalie / sql_capsule

Licence: MIT license
Less ORM, more SQL

Programming Languages

ruby
36898 projects - #4 most used programming language
shell
77523 projects

Projects that are alternatives of or similar to sql capsule

viup
A V wrapper for the cross-platform UI library, IUP.
Stars: ✭ 36 (+33.33%)
Mutual labels:  wrapper
google-pubsub-emulator
Google PubSub Emulator wrapper to nodejs
Stars: ✭ 28 (+3.7%)
Mutual labels:  wrapper
python-qnapstats
Python API for obtaining QNAP NAS system stats
Stars: ✭ 45 (+66.67%)
Mutual labels:  wrapper
adl
🍿 anime-downloader + trackma wrapper
Stars: ✭ 91 (+237.04%)
Mutual labels:  wrapper
confirm-before
✅ Are you sure? No? Confirm Before - Sanity check for your shell commands
Stars: ✭ 16 (-40.74%)
Mutual labels:  wrapper
libssh2.nim
Nim wrapper for libssh2
Stars: ✭ 25 (-7.41%)
Mutual labels:  wrapper
node-atol-wrapper
Node.js обертка для ДТО версии 10 компании АТОЛ
Stars: ✭ 28 (+3.7%)
Mutual labels:  wrapper
node-aplay
🎵 ALSA aplay wrapper for Node.js
Stars: ✭ 30 (+11.11%)
Mutual labels:  wrapper
jodit-vue
Vue wrapper for Jodit Editor
Stars: ✭ 60 (+122.22%)
Mutual labels:  wrapper
careful rm
A safe wrapper for rm that adds useful warnings and an optional recycle/trash mode
Stars: ✭ 22 (-18.52%)
Mutual labels:  wrapper
nanoleaf-aurora
A java wrapper for the Nanoleaf Aurora API
Stars: ✭ 19 (-29.63%)
Mutual labels:  wrapper
node-freshdesk-api
Node wrapper for Freshdesk v2 API
Stars: ✭ 24 (-11.11%)
Mutual labels:  wrapper
dokuwiki-plugin-bootswrapper
Bootstrap Wrapper for DokuWiki
Stars: ✭ 33 (+22.22%)
Mutual labels:  wrapper
Xamarin-iOS
PSPDFKit for iOS wrapper for the Xamarin platform.
Stars: ✭ 14 (-48.15%)
Mutual labels:  wrapper
dokan-delphi
Dokan Delphi Wrapper
Stars: ✭ 48 (+77.78%)
Mutual labels:  wrapper
py-SMART
Wrapper for smartctl (smartmontools)
Stars: ✭ 42 (+55.56%)
Mutual labels:  wrapper
dbots.js
A stats poster and API wrapper for all botlists.
Stars: ✭ 18 (-33.33%)
Mutual labels:  wrapper
igdb
IGDB PHP API Wrapper
Stars: ✭ 20 (-25.93%)
Mutual labels:  wrapper
angsd-wrapper
Utilities for analyzing next generation sequencing data.
Stars: ✭ 13 (-51.85%)
Mutual labels:  wrapper
haskell-libui
Haskell bindings to the libui C library.
Stars: ✭ 45 (+66.67%)
Mutual labels:  wrapper

SQLCapsule

SQLCapsule is the culmination of many of my thoughts and concerns surrounding ORMs and how we use Ruby to interact with databases. The goal is to be a small and easy to understand tool to help you talk to your database without the baggage of a using a full fledged ORM. SQLCapsule is reminiscent of the repository pattern (though you may use it however you like) and works by registering and naming SQL queries for later use.

SQLCapsule aims to provide helpful errors, and to assist you along the way to building an application specific PostgreSQL interaction layer.

Installation

Add this line to your application's Gemfile:

gem 'sql_capsule'

And then execute:

$ bundle

Or install it yourself as:

$ gem install sql_capsule

Usage

Initialize a wrapper using a PG connection object:

user_database = SQLCapsule.wrap(pg_connection)

Once you have a wrapper you can register bits of SQL. The method signature is: query_name, raw_sql, *arguments

query = "SELECT * FROM users_table WHERE id = $1;"
user_database.register(:find_user, query, :id)

If you try and register a SQL statement using $1 without defining an argument name it will raise an error.

user_database.register :find_user, "SELECT * FROM users WHERE id = $1;"
  # SQLCapsule::Query::ArgumentCountMismatchError: Argument count mismatch
  # 0 arguments provided for
  # SQL: SELECT * FROM users WHERE id = $1;
  # Args:[]

Likewise, if you try and register an argument without defining its use within the SQL it will also raise an error.

user_database.register :find_user, "SELECT * FROM users;", :id
  # SQLCapsule::Query::ArgumentCountMismatchError: Argument count mismatch
  # 1 arguments provided for
  # SQL: SELECT * FROM users;
  # Args:[:id]

Arguments are used in order, so if you register :id, :name then $1 will correspond with :id and $2 will correspond with :name. SQLCapsule does not attempt to verify your numbering. :-)

It is also possible to register a query with a block to handle the resulting rows:

query = "SELECT * FROM users_table WHERE id = $1;", :id
user_database.register(:find_user, query, :id) { |row| row.merge('preprocessed' => true) }
user_database.run(:find_user, id: 1) => [ { 'name' => 'John', 'age' => 20, 'id' => 3, 'preprocessed' => true} ]

Any registered query can be called like:

user_database.run :find_user, id: 3  # => [ { 'name' => 'John', 'age' => 20, 'id' => 3 } ]

Or with a block:

user_database.run(:find_user, id: 3) { |user| user.merge('loaded' => true) }
  # => [ { 'name' => 'John', 'age' => 20, 'id' => 3, 'loaded' => true } ]

Run checks for required keywords when called and will throw an error if missing one:

user_database.run :find_user
  # => SQLCapsule::QueryGroup::MissingKeywordArgumentError: Missing query argument: id

The result of a query will return in the form of an array of hashes, where each item in the array corresponds to result row, and key/value pairs in the hash correspond with column/value pairs in a resulting row.

user_database.register :find_adult_users, "SELECT * FROM users WHERE age >= 18;"
user_database.run :find_adult_users  # => [ { 'name' => 'John', 'age' => 20 }, { 'name' =>  'Anne', 'age' =>  23 } ]

Complex Queries

One thing about SQL and relational databases is that returning tables with identical column names is a perfectly normal and sane thing to do. SQLCapsule enforces the use of AS to alias column names and will raise an error when duplicate column names result from a query (like a join)

query = 'SELECT * FROM widgets LEFT JOIN orders on widgets.id=orders.widget_id;'
widget_database.register :join_widgets, query
widget_database.run :join_widgets
  # SQLCapsule::Wrapper::DuplicateColumnNamesError: Error duplicate column names in resulting table: ["name", "price", "id", "widget_id", "amount", "id"]
  # This usually happens when using a `JOIN` with a `SELECT *`
  # You may need use `AS` to name your columns.
  # QUERY: SELECT * FROM widgets LEFT JOIN orders on widgets.id=orders.widget_id;

Development

After checking out the repo, run bin/setup to install dependencies / setup the testing database. Then, run rake test to run the tests.

You can also run bin/console for an interactive prompt that will allow you to experiment. Once in the console @wrapper is already setup for use and includes a registered query: @wrapper.run :all_widgets

To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and tags, and push the .gem file to rubygems.org.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/piisalie/sql_capsule. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.

License

The gem is available as open source under the terms of the MIT License.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].