sql-thumbMore and more people prefer textual open-format storage that’s why Microsoft Office has turned to XML. But for simple structure, XML is too much verbose and cannot be easily queried by end-user that’s why RebDB by Dobeash Software presented here may be a perfect solution (for simpler textual database usage see Rebol Mini-Text Database).

According to its own author’s claim:

The entire distribution weighs in at less than 20Kb of highly optimized and tuned REBOL/Base syntax. It can blaze through millions of rows a second!

Quite impressive isn’t it ? Well I didn’t check the performance and the “pseudo-relational” in the title is a bit over-hyped - as far as I can see, you are able to make SQL queries on a table but not make relations between tables - but if you really need a full-blown relational portable database engine with Rebol you can use SQLite Driver from the same author (if you need MySQL instead, we have already seen how).

I tested its usage and it quites suits me. So here’s how to get started with RebDB.

Go to download page and download it from the link there, unzip it somewhere (for example in rebol’s lib sub-directory). You should get this list of files:
rebdb-files
which are described here:

Script Description
db.r Defines the main workings of RebDB in a single context named db. A number of accessor functions, all prefixed with db-, are exported to the global context for easy reference.
db-client.r Implements the db-request function used by remote clients, and provides the sql function for easy database access.
SQL.r Starts up a console SQL client.
login.sql Used by the SQL client to initialize various settings.

As mentioned in Quick Start Guide, launch Rebol Console and type:


do %lib/RebDB-203/SQL.r

You should then get this SQL prompt:


login.sql ran in 0:00 second(s)
SQL>

Let’s create a Contact table (Rebol is case-insensitive by default Contact or contact is the same):


create Contact [id First-Name Last-Name Email]

Insert some datas by copying and pasting the two lines below:


insert Contact values [1 "John" "Doe" "email hidden; JavaScript is required"]
insert Contact values [2 "Jane" "Doe" "email hidden; JavaScript is required"]

rebdb-insert

When you’ve finished you must commit to save your datas on disk:


commit Contact

This generates 2 files:

  1. Contact.ctl for the header
  2. Contact.data for the data rows

rebdb-header
rebdb-datas
The storage format is quite nice for a human. And you can easily import datas from any other storage and query it with RebDB with SQL statements like this


select * from Contact

Add one more row:


insert Contact values [3 "Carl" "Sassenrath" "email hidden; JavaScript is required"]

And let’s use a SQL Where clause (as you can see you can use the full range of Rebol’s Expression like the find function):


select * from contact where [find email "rebol"]

The output will be:
rebdb-select

You can play further on your own with sort, update, delete and drop and more by reading the SQL Guide and for administration the DB Guide (for example a nice feature is automatic Replay from log file at restart when you crash before commit).

SQL.r is just a Console Client which uses db.r and db-client.r. But you could directly use the DB Engine in your Rebol’s program for example if you need bulk loading as explained in the DB Guide.

So type Exit at the SQL Prompt, this will close Rebol Console. Launch Rebol Console again and type:


 do %lib/RebDB-203/db.r

RebDB functions can be listed with Rebol’s integrated Help Engine with


 help db-

or


? db-

rebdb-api

For example, try db-select with


db-select * Contact

If you want to use the Where clause, you should use the /where refinement for db-select function:


db-select/where * Contact [id < 5]

The ouput will be a linear Rebol’s block:


>>  db-select/where * Contact [id < 5]
== [1 "John" "Doe" "email hidden; JavaScript is required"
2 "Jane" "Doe" "email hidden; JavaScript is required"
3 "Carl" "Sassenrath" "email hidden; JavaScript is required"]

If you prefer to use SQL syntax, add this:


do %lib/RebDB-203/db-client.r

you can then transform SQL syntax into db command:


sql "select * from contact"

outputs:


>> sql "select * from contact"
== [1 "John" "Doe" "email hidden; JavaScript is required" 2 "Jane" "Doe" "
email hidden; JavaScript is required" 3 "Carl" "Sassenrath" "TheOne@rebol.
com"]

So that you can easily iterate through it with the usual foreach Rebol iterator like this for example:


foreach person  sql "select * from contact where [id < 5]" [
probe person
]

will output:


1
"John"
"Doe"
"email hidden; JavaScript is required"
2
"Jane"
"Doe"
"email hidden; JavaScript is required"
3
"Carl"
"Sassenrath"
"email hidden; JavaScript is required"

or you can use the variant syntax:


foreach [id first-name last-name email] sql "select * from contact where [id < 5]"  [
print [first-name last-name email]
]

which would output:


John Doe email hidden; JavaScript is required
Jane Doe email hidden; JavaScript is required
Carl Sassenrath email hidden; JavaScript is required

So if you need to export your database to csv format (to import it elsewhere in Outlook for example), this is quite easy to do by using the basic rebol write function:


delete %contact.csv
foreach [id first-name last-name email] sql "select * from contact where [id < 5]" [
  write/lines/append %contact.csv rejoin [first-name "," last-name "," email]
]

rebdb-export-to-csv

By combining this SQL Engine with Rebol’s Block and Expressions you have the power of Microsoft .NET SQL/LINQ (Language-Integrated Query) in Rebol 500 Ko light weight ;)

Note: another interesting library is SQL Protocol by “Marco” but I didn’t try it yet.

2 people like this post.
Bookmark and Share