ben-albrecht / cdo

Chapel Data Object

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

cdo

Chapel Data Object

Chapel Data Object is a library that helps to connect to databases like Mysql,Postgres and Sqlite using a common API in Chapel language.

The module Cdo provides a simple way to hide the complexities that each database connector library has.

Cdo with Postgres

  1. Have latest Chapel compiler version installed.
  2. Install libpq.

On Ubuntu do:

sudo apt-get install libpq-dev
  1. Go to example/expq.chpl and inform database host, user, password.
  2. Go to repository folder and compile:
make pgsql
  1. Run the example:
./expq

Code example

module Main{
use Cdo;
use Postgres;

proc main(){
//Open connection to Postgres database. Parametrs are host,username, database, password

var con = PgConnectionFactory("localhost", "postgres", "teste", "password");


//Open a cursor
var cursor = con.cursor();
//Queries from database
cursor.query("SELECT * FROM public.contacts");
//Get one row.
var res:Row = cursor.fetchone();
while(res!=nil){
//print the results.
writeln(res);
//get the next row one.
res = cursor.fetchone();
}

// Queries passing tuple to formated query string.
cursor.query("SELECT %s, %s FROM public.contacts",("email","name"));
        
// iterate over all rows
for row in cursor{
//get row data by column name and print it.
writeln("name = ", row["name"]," email = ", row["email"] );
}

cursor.query("SELECT * FROM public.contacts");

// iterate over all rows
for row in cursor{
//get row data by column number and print it.
writeln("name = ", row[1]," email =", row[3] );
}

cursor.close();
con.close();
writeln("end");
}
}

Cdo with Mysql

  1. Have latest Chapel compiler version installed.
  2. Install libmysqlclient.

On Ubuntu do:

sudo apt-get install libmysqlclient-dev
  1. Go to example/expq.chpl and inform database host, user, password.
  2. Verify the mysql library path with bash mysql_config --cflags --libs and edit Makefile.
  3. Go to repository folder and compile:
make mysqlex
  1. Run the example:
./mysqlex

Code example

module Main{
use Cdo;
use Mysql;

proc main(){
//Open connection to Mysql database. Parametrs are host,username, database, password

var con = MysqlConnectionFactory("localhost", "root", "teste", "root");


//Open a cursor
var cursor = con.cursor();
//Queries from database
cursor.query("SELECT * FROM contacts");
//Get one row.
var res:Row = cursor.fetchone();
while(res!=nil){
//print the results.
writeln(res);
//get the next row one.
res = cursor.fetchone();
}

// Queries passing tuple to formated query string.
cursor.query("SELECT %s, %s FROM contacts",("email","name"));
        
// iterate over all rows
for row in cursor{
//get row data by column name and print it.
writeln("name = ", row["name"]," email = ", row["email"] );
}

cursor.query("SELECT * FROM contacts");

// iterate over all rows
for row in cursor{
//get row data by column number and print it.
writeln("name = ", row[1] );
}


cursor.close();
con.close();
writeln("end");
}
}

Cdo with Sqlite

  1. Have lastest Chapel compiler version installed.
  2. Install libsqlite3-dev.

On Ubuntu do:

sudo apt-get install sqlite3 libsqlite3-dev
  1. Go to example/exsqlite.chpl and inform database file.
  2. Go to repository folder and compile:
make sqlitex
  1. Run the example:
./sqlitex

Code example

module Main{
use Cdo;
use Sqlite;

proc main(){
//Open connection to SQlite database. Parametrs is the file name.

var con = SqliteConnectionFactory("teste.db");

//Open a cursor
var cursor = con.cursor();
//Queries from database
cursor.query("SELECT * FROM contacts");
//Get one row using while.
var res:Row = cursor.fetchone();
while(res!=nil){
//print the results.
writeln(res);
//get the next row one.
res = cursor.fetchone();
}

// Queries passing tuple to formated query string.
cursor.query("SELECT %s, %s FROM contacts",("email","name"));
        
// iterate over all rows
for row in cursor{
//get row data by column name and print it.
writeln("name = ", row["name"]," email = ", row["email"] );
}

cursor.query("SELECT * FROM contacts");

// iterate over all rows
for row in cursor{
//get row data by column number and print it.
writeln("name = ", row[1] );
}

cursor.close();
con.close();
writeln("end");
}
}

Warning

This library is very alpha and incomplete. Please, consider that we are in early stage and many functions and features are not implemented yet.

About

Chapel Data Object

License:Apache License 2.0


Languages

Language:Chapel 97.6%Language:C 1.9%Language:Makefile 0.5%