baza

A database abstraction layer for Ruby. Also supports JRuby.

Installation

Is fairly painless.

“em install baza

Or in your Gemfile:

“by gem ‘baza’

Connection to a database.

MySQL

“by db = Baza::Db.new(type: :mysql2, host: “localhost”, user: “my_user”, pass: “my_password”, port: 3306, db: “my_database”)

PostgreSQL

“by db = Baza::Db.new(type: :pg, host: “localhost”, user: “my_user”, pass: “my_password”, db: “my_database”)

SQLite3

“by db = Baza::Db.new(type: :sqlite3, path: “/path/to/file.sqlite3”)

Queries

Select

“by db.select(:users, “Kasper”, “age”) do |row| puts “Row: #row” end

name = “Kasper” db.q(“SELECT * FROM users WHERE name = ‘#dbdb.esc(name)’ ORDER BY age”) do |row| puts “Row: #row” end

Inserting

“by db.insert(:users, “Kasper”, age: 27) id = db.last_id

It can also return the ID at the same time

“by id = db.insert(:users, “Kasper”, age: 27, return_id: true)

Inserting multiple rows in one query is also fairly painless:

“by db.insert_multi(:users, [Kasper, age: 27, Christina, age: 25, Charlotte, age: 23])

Update

“by db.update(:users, “Kasper Johansen”, “Kasper”)

Delete

“by db.delete(:users, name: “Kasper”)

Upsert

The following example handels a row that will be inserted with “Kasper”, age: 27 if it doesnt exist or rows with “Kasper” will have their their age updated to 27.

“by db.upsert(:users, “Kasper”, 27)

Structure

Database creation

“by db.databases.create(name: “test-db”)

Database renaming

“by database = db.databases[test-db] database.name = “new-name” database.save!

Listing databases

“by db.databases.list.each do |database| puts “Database: #databasedatabase.name” end

Dropping a database

“by database = db.databases[test-db] database.drop

Listing tables on non-used-database

“by database = db.database[test-db] database.tables.each do |table| puts “TableName: #tabletable.name” puts “Columns: #table.columns.map(&:name)” end

Table creation

“by db.tables.create(:users, { columns: [:id, type: :int, autoincr: true, primarykey: true, :name, type: :varchar], indexes: [:name] })

Table dropping

“by table = db.tables[:users] table.drop

Table listing

“by array_of_tables = db.tables.list

Or you can use blocks:

“by db.tables.list do |table| puts “Table-name: #tabletable.name” end

Table renaming

“by table = db.tables[:users] table.rename(:new_table_name)

Table optimizing

“by table.optimize

Table rows counting

“by table.rows_count

Column listing

“by table = db.tables[users] cols = table.columns

Or a specific column:

“by column = table.column(:id) puts “Column: #columncolumn.name #columncolumn.type(#columncolumn.maxlength)” puts “Default: #columncolumn.default”

Column altering

“by column.change(name: “newname”, type: :varchar, default: “”)

Drop column

“by column.drop

Get an index by name

“by index = table.index(“index_name”)

Rename index

“by index.rename(“new name”)

Dropping an index

“by index.drop

Getting various data from an index

“by puts “Unique: #indexindex.unique?” puts “Primary: #indexindex.primary?” puts “Autoincr: #indexindex.autoincr?” puts “Table: #indexindex.table”

Copying databases

“by db_mysql = Baza::Db.new(type: :mysql, …) db_mysql2 = Baza::Db.new(type: :mysql2, …) db_sqlite = Baza::Db.new(type: :sqlite3, path: …)

db_mysql.copy_to(db_sqlite)

Dumping SQL to an IO

“by db = Baza::Db.new(…) dump = Baza::Dump.new(db: db) str_io = StringIO.new dump.dump(str_io)

Transactions

“by db.transaction do 1000.times do db.insert(:users, name: “Kasper”) end end

Users

Listing users

“by db.users.list do |user| puts “User found: #useruser.name” end

“by root_user = db.users.find_by_name(“root”) root_user.name #=> “root”

Dropping users

“by user.drop

Creating users

“by user = db.users.create(name: “myuser”, host: “localhost”)

Query Buffer

In order to speed things up, but without using transactions directly, you can use a query buffer. This stores various instructions in memory and flushes them every now and then through transactions or intelligent queries (like multi-insertion). The drawback is that it will not be possible to test the queries for errors before a flush is executed and it wont be possible to read results from any of the queries.

It is fairly simple do:

“by db.q_buffer do |buffer| 100_000.times do |count| buffer.insert(:table_name, name: “Kasper #count”)

buffer.query("UPDATE table SET ...")
buffer.query("DELETE FROM table WHERE ...")

end end

Contributing to baza

  • Check out the latest master to make sure the feature hasn’t been implemented or the bug hasn’t been fixed yet.
  • Check out the issue tracker to make sure someone already hasn’t requested it and/or contributed it.
  • Fork the project.
  • Start a feature/bugfix branch.
  • Commit and push until you are happy with your contribution.
  • Make sure to add tests for it. This is important so I don’t break it in a future version unintentionally.
  • Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.

Copyright (c) 2013 Kasper Johansen. See LICENSE.txt for further details.