Modular Sql is a modular query builder that enables a high database compatibility, usage easiness and dynamic construction. It is intended to allow any kind of query in any database, but will, at the moment, only handle relatively simple ones to most common databases.
It actually does a similar job to ActiveRecord, but I believe it may allow a better control in some aspects as it lies somehow between raw queries and that uberuseful gem.
Simple mysql queries (select, insert, update).
Just one where condition (if you need more than one, use #where(all_conditions_together).
result = MdlSql::select.from(:users).where(:role, 2, '=').execute
This will build and execute the following query:
SELECT * FROM users WHERE role = 2
We finally iterate over the results:
results.each do |row| puts row[:username] end
You can read more about the usage of the MysqlResult object @ github.com/brianmario/mysql2usage
When selecting columns use .cols()/.columns(), which can receive a Hash or an Array. In case a Hash is passed, the key will be the alias and the value the actual name of the column (f.e. 'users.username').
result = MdlSql::insert.into(:users).cols(:username, :role).values('admin',2).execute
result = MdlSql::update(:users).set(:role => '1').where(@username = 'default'").execute
Updating follows mysql building way. Remember that you can still use .table() if you feel more comfortable.
#table(table_name, table_alias) (#from, #into):
Select table to use.
#where(first_term, sencond_term, operator):
Where condition. This can be used both passing each separate term (recommended) and introducing the whole comparison as the first_term (i.e. .where('id = 1')). The first way will provide a higher compatibility with other databases, but may not be as useful when needing more than a simple condition.
TODO: expand where to include AND, OR, etc. in order to be able to concatenate many conditions.
gem install mdlsql
If you're going to use mysql, mysql2 is required. Please follow these instructions to install: github.com/brianmario/mysql2#installing (really worth it, even if you're not going to use mdlsql ;) ).
Copyright (C) 2013 MdlSql contributers
This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program. If not, see <www.gnu.org/licenses/>.