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.
We would like to thank Deep 6 AI for their support on this project.
We would like to thank Brian Dolan, Ben Albrecht, Brad Chamberlain and all Chapel Community for all motivations, suggestions and help.
On Ubuntu do:
sudo apt-get install libpq-dev
make pgsql
./expq
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");
}
}
On Ubuntu do:
sudo apt-get install libmysqlclient-dev
bash mysql_config --cflags --libs
and edit Makefile.make mysql_ex1
cd bin
./mysql_ex1
Do similar steps to compile mysql_ex2.
use DatabaseCommunicator;
use DatabaseCommunicator.QueryBuilder; // for Statement class
use MySQL;
proc main() throws {
var conHandler = ConnectionHandler.ConnectionHandlerWithString(MySQLConnection, "localhost;testdb;username;password");
var cursor = conHandler.cursor();
var createStmt = "CREATE TABLE CONTACTS (id INT PRIMARY KEY, name VARCHAR(10));";
cursor.execute(new Statement(createStmt));
cursor.execute(new Statement("INSERT INTO CONTACTS VALUES (6, 'B');"));
var stmt: Statement = new Statement("SELECT * FROM CONTACTS WHERE name = ?1", true);
stmt.setValue(1, "B");
cursor.execute(stmt);
for row in cursor.fetchall() {
writeln(row![0], "\t", row![1]);
}
cursor.close();
conHandler.commit();
conHandler.close();
}
If using a TOML configuration file to specify connection parameters:
use DatabaseCommunicator;
use DatabaseCommunicator.QueryBuilder; // for Statement class
use MySQL;
proc main() throws {
var conHandler = ConnectionHandler.ConnectionHandlerWithConfig(MySQLConnection, "dbinfo.toml");
var cursor = conHandler.cursor();
var createStmt = "CREATE TABLE CONTACTS (id INT PRIMARY KEY, name VARCHAR(10));";
cursor.execute(new Statement(createStmt));
cursor.execute(new Statement("INSERT INTO CONTACTS VALUES (6, 'B');"));
var stmt: Statement = new Statement("SELECT * FROM CONTACTS WHERE name = ?1", true);
stmt.setValue(1, "B");
cursor.execute(stmt);
for row in cursor.fetchall() {
writeln(row![0], "\t", row![1]);
}
cursor.close();
conHandler.commit();
conHandler.close();
}
On Ubuntu do:
sudo apt-get install sqlite3 libsqlite3-dev
make sqlitex
./sqlitex
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");
}
}
Numsuch numerical and Machine Learning library for Chapel Language.
This library is very alpha and incomplete. Please, consider that we are in early stage and many functions and features are not implemented yet.