README.md in baza-0.0.20 vs README.md in baza-0.0.21

- old
+ new

@@ -1,8 +1,8 @@ [![Code Climate](https://codeclimate.com/github/kaspernj/baza/badges/gpa.svg)](https://codeclimate.com/github/kaspernj/baza) [![Test Coverage](https://codeclimate.com/github/kaspernj/baza/badges/coverage.svg)](https://codeclimate.com/github/kaspernj/baza) -[![Build Status](https://img.shields.io/shippable/540e7b993479c5ea8f9ec1fe.svg)](https://app.shippable.com/projects/540e7b993479c5ea8f9ec1fe/builds/latest) +[![Build Status](https://travis-ci.org/kaspernj/baza.svg?branch=master)](https://travis-ci.org/kaspernj/baza) # baza A database abstraction layer for Ruby. Also supports JRuby. @@ -20,13 +20,18 @@ ## Connection to a database. ### MySQL ```ruby -db = Baza::Db.new(type: :mysql, subtype: :mysql2, host: "localhost", user: "my_user", pass: "my_password", port: 3306, db: "my_database") +db = Baza::Db.new(type: :mysql2, host: "localhost", user: "my_user", pass: "my_password", port: 3306, db: "my_database") ``` +### PostgreSQL +```ruby +db = Baza::Db.new(type: :pg, host: "localhost", user: "my_user", pass: "my_password", db: "my_database") +``` + ### SQLite3 ```ruby db = Baza::Db.new(type: :sqlite3, path: "/path/to/file.sqlite3") ``` @@ -80,10 +85,38 @@ db.upsert(:users, {name: "Kasper"}, {age: 27}) ``` ## Structure +### Database creation +```ruby +db.databases.create(name: "test-db") +``` + +### Database renaming +```ruby +database = db.databases["test-db"] +database.name = "new-name" +database.save! +``` + +### Listing databases +```ruby +db.databases.list.each do |database| + puts "Database: #{database.name}" +end +``` + +### Listing tables on non-used-database +```ruby +database = db.database["test-db"] +database.tables.each do |table| + puts "TableName: #{table.name}" + puts "Columns: #{table.columns.map(&:name)}" +end +``` + ### Table creation ```ruby db.tables.create(:users, { columns: [ {name: :id, type: :int, autoincr: true, primarykey: true}, @@ -129,11 +162,11 @@ table.rows_count ``` ### Column listing ```ruby -table = db.tables[:users] +table = db.tables["users"] cols = table.columns ``` Or a specific column: ```ruby @@ -169,10 +202,11 @@ ### Getting various data from an index ```ruby puts "Unique: #{index.unique?}" puts "Primary: #{index.primary?}" +puts "Autoincr: #{index.autoincr?}" puts "Table: #{index.table}" ``` ## Copying databases ```ruby @@ -196,9 +230,33 @@ db.transaction do 1000.times do db.insert(:users, name: "Kasper") end end +``` + +## Users + +### Listing users +```ruby +db.users.list do |user| + puts "User found: #{user.name}" +end +``` + +```ruby +root_user = db.users.find_by_name("root") +root_user.name #=> "root" +``` + +### Dropping users +```ruby +user.drop +``` + +### Creating users +```ruby +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.