README.md in baza-0.0.14 vs README.md in baza-0.0.15
- old
+ new
@@ -69,11 +69,11 @@
db.update(:users, {name: "Kasper Johansen"}, {name: "Kasper"})
```
### Delete
```ruby
-db.delete(:users, {name: "Kasper"})
+db.delete(:users, name: "Kasper")
```
### Upsert
The following example handels a row that will be inserted with {name: "Kasper", age: 27} if it doesnt exist or rows with {name: "Kasper"} will have their their age updated to 27.
```ruby
@@ -83,15 +83,15 @@
## Structure
### Table creation
```ruby
db.tables.create(:users, {
- :columns => [
- {:name => :id, :type => :int, :autoincr => true, :primarykey => true},
- {:name => :name, :type => :varchar}
+ columns: [
+ {name: :id, type: :int, autoincr: true, primarykey: true},
+ {name: :name, type: :varchar}
],
- :indexes => [
+ indexes: [
:name
]
})
```
@@ -117,52 +117,97 @@
```ruby
table = db.tables[:users]
table.rename(:new_table_name)
```
+### Table optimizing
+```ruby
+table.optimize
+```
+
+### Table rows counting
+```ruby
+table.rows_count
+```
+
### Column listing
```ruby
table = db.tables[:users]
cols = table.columns
```
Or a specific column:
```ruby
-col = table.column(:id)
-puts "Column: #{col.name} #{col.type}(#{col.maxlength})"
+column = table.column(:id)
+puts "Column: #{column.name} #{column.type}(#{column.maxlength})"
+puts "Default: #{column.default}"
```
+### Column altering
+```ruby
+column.change(name: "newname", type: :varchar, default: "")
+```
+
+### Drop column
+```ruby
+column.drop
+```
+
+### Get an index by name
+```ruby
+index = table.index("index_name")
+```
+
+### Rename index
+```ruby
+index.rename("new name")
+```
+
+### Dropping an index
+```ruby
+index.drop
+```
+
+### Getting various data from an index
+```ruby
+puts "Unique: #{index.unique?}"
+puts "Primary: #{index.primary?}"
+puts "Table: #{index.table}"
+```
+
## Copying databases
```ruby
-db_mysql = Baza::Db.new(:type => :mysql, :subtype => :mysql2, ...)
-db_sqlite = Baza::Db.new(:type => :sqlite3, :path => ...)
+db_mysql = Baza::Db.new(type: :mysql, subtype: :mysql2, ...)
+db_sqlite = Baza::Db.new(type: :sqlite3, path: ...)
db_mysql.copy_to(db_sqlite)
```
## Dumping SQL to an IO
```ruby
db = Baza::Db.new(...)
-dump = Baza::Dump.new(:db => db)
+dump = Baza::Dump.new(db: db)
str_io = StringIO.new
dump.dump(str_io)
```
## Transactions
```ruby
db.transaction do
1000.times do
- db.insert(:users, {:name => "Kasper"})
+ db.insert(:users, name: "Kasper")
end
end
```
## 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 form any of the queries. It is fairly simple do however:
+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:
```ruby
db.q_buffer do |buffer|
100_000.times do |count|
- buffer.insert(:table_name, {:name => "Kasper #{count}"})
+ buffer.insert(:table_name, name: "Kasper #{count}")
buffer.query("UPDATE table SET ...")
buffer.query("DELETE FROM table WHERE ...")
end
end