Note: This website is archived. For up-to-date information about D projects and development, please visit wiki.dlang.org.

Welcome to libddbc

This project tries to provide JDBC like APIs to database connectivity. Basic stuff for ODBC, sqlite and postgresql support has been ported using phobos and D2. ODBC driver has been based on http://libodbcxx.sourceforge.net/, while postgresql and sqlite driver has been based on jdbc drivers.

To build the library: Follow the steps below:

1) Install Pre-requisites like dmd, git and scons
2) Create a directory for code e.g libddbc.
3) Change into the directory and get the source code with the command: 'git clone http://git.dsource.org/projects/ddbc/'
4) This will download source code into a directory ddbc in the directory created earlier
5) Change to directory ddbc on a terminal.
6) Fire command 'scons' in the directory ddbc. This will compile the code and create a bin directory
7) Fire command 'sudo scons install' to install the static library and headers. By default library is installed to the path '/usr/local/lib' and header files are installed into the directory '/usr/local/include'.

In order to compile the code for a 32 bit platform, remove the flag '-m64' from file SConstruct in the directory where source code has been downloaded. Also for any changes to the installation path, make the edits in the same file.

The following test program works with the above library as a sample.

module TestDDBC;

import ddbc.sql._;
import std.datetime;
import std.conv;
import ddbc.util.String;
import std.stdio;

int odbc_main() {
	Connection conn = DriverManager.getConnection("DSN=test;user=mandeep;password=mandeep", DriverManager.DriverType.ODBC);
	if(conn !is null) {
		writeln("got connection");
	}
	else {
		writeln("no connection");
	}
	scope(exit) conn.close();
	testMetaData(conn.getMetaData());
	testStatement(conn);
	testPreparedStatement(conn);
	testCallableStatement(conn);

	conn.close();
	return 0;
}

int pg_main() {
	Connection conn = DriverManager.getConnection("dbname=test;user=mandeep;password=mandeep", DriverManager.DriverType.PostgreSQL);
	scope(exit) conn.close();
	if(conn !is null) {
		writeln("connection successfull");
	}
	else {
		writeln("no connection");
		return 1;
	}
	testStatement(conn);
	testPreparedStatement(conn);
	testCallableStatement(conn);
	return 0;
}

int sqlite_main() {
	Connection conn = DriverManager.getConnection("file=/home/mandeep/Documents/test", DriverManager.DriverType.SQLite);
	scope(exit) conn.close();
	if(conn !is null) {
		writeln("connection successfull");
	}
	else {
		writeln("no connection");
		return 1;
	}
	testStatement(conn);
	testPreparedStatement(conn);
	//testCallableStatement(conn);
	return 0;
}


public void testMetaData(DatabaseMetaData metaData) {
	writeln("\n Test MetaData");

}

public void testStatement(Connection conn) {
	writeln("\n Test Statement  ");
	Statement st = conn.createStatement();
	writeln("\n Statement created ");
	scope(exit) st.close();
	if(st is null)
		writeln("statement could not be created");
	int intVar = 2;
	writeln("\n Executing query");
	ResultSet rs = st.executeQuery("select intVar, floatVar, doubleVar, longVar, boolVar, stringVar, dateVar, timeVar, dateTimeVar from testTable where intVar="~to!String(intVar));
	writeln("\n Executed query");
	scope(exit) rs.close();
	writeln("\n Printing Result");
	printResultSet(rs);
	writeln("\n Printed Result");
}
public void testPreparedStatement(Connection conn) {
	writeln("\n Test Prepared Statement  ");
	PreparedStatement st = conn.prepareStatement("select intVar, floatVar,doubleVar, longVar, boolVar, stringVar, dateVar, timeVar, dateTimeVar from testTable where stringVar=?");
	scope(exit) st.close();
	String stringVar = "String 1";
	st.setString(1,stringVar);
	ResultSet rs = st.executeQuery();
	scope(exit) rs.close();
	printResultSet(rs);
}
public void testCallableStatement(Connection conn) {
	writeln("\n Test Callable Statement ");
	String outVar;
	CallableStatement cst = conn.prepareCall("{call testCall(?,?)}");
	scope(exit) cst.close();
	cst.registerOutParameter(2, Types.VARCHAR);
	
	(cast(PreparedStatement)cst).setInt(1, 2);
	cst.execute();
	
	outVar = cst.getString(2);
	writeln("Out Variable value ", outVar);
}
public void printResultSet(ResultSet rs) {
	while(rs.next()) {
		int intVar = rs.getInt("intVar");
		String stringVar = rs.getString("stringVar");
		bool boolVar = rs.getBool("boolVar");
		float floatVar = rs.getFloat("floatVar");
		double doubleVar = rs.getDouble("doubleVar");
		long longVar = rs.getLong("longVar");
		Date dateVar = rs.getDate("dateVar");
		TimeOfDay timeVar = rs.getTime("timeVar");
		DateTime dateTimeVar = rs.getTimestamp("DateTimeVar");
		writeln("intVar ", intVar, "floatVar ", floatVar,"doubleVar ", doubleVar, "longVar ", longVar,"boolVar ", boolVar, "stringVar ", stringVar, "dateVar ", dateVar.year,"-", dateVar.month,"-", dateVar.day, "timeVar ", timeVar.hour,":",timeVar.minute);
		writeln("intVar ", intVar, "floatVar ", floatVar, "longVar ", longVar,"boolVar ", boolVar, "stringVar ", stringVar, "dateVar ", dateVar.year,"-", dateVar.month,"-", dateVar.day, "dateTimeVar ", dateTimeVar.year,"-", dateTimeVar.month,"-", dateTimeVar.day," ",  dateTimeVar.hour,":",dateTimeVar.minute);
	}
}

int main() {
	odbc_main();
	pg_main();
	sqlite_main();
	return 0;
}

Table structure for the database was created with

create table testTable(intVar int, floatVar float,doubleVar double, longVar BIGINT,boolVar Boolean, stringVar VARCHAR(100), dateVar date, timeVar time, dateTimeVar datetime) ;
insert into testTable values(1, 1.15, 200000.0455,9223372036854775800,TRUE, 'String 1','2010-10-18','10:15','2010-10-18 10:15');
insert into testTable values(2, 2.15, 4034340.0455,122234036854775800,FALSE, 'String 2','2011-03-12','11:15','2011-03-11 11:15');

create procedure testCall(IN intV int, OUT strV VARCHAR(100)) proc body follows

In order to compile your program with ddbc, include the headers path (/usr/local/include) in your includes (-I with dmd) and then link the static library(libddbc) from the path (/usr/local/lib) with appropriate flags (-L-L/usr/local/lib) and (-L-lddbc)

Starting Points

For a complete list of local wiki pages, see TitleIndex.