A Web Developer's Diary

How to build H2 database engine yourself and install it in your local Maven repository

7/8/2012

0 Comments

 
If you ever find the need to compile H2 database yourself to install it in your local Maven repository, here are the steps:
 svn checkout http://h2database.googlecode.com/svn/trunk/ h2database-read-only
cd h2database-read-only/h2
./build.sh mavenInstallLocal
Your custom H2 build is now installed in your local Maven repository as version 1.0-SNAPSHOT. Include it as a dependency in your project by adding the appropriate XML to your pom.xml:
<project>
...
<dependencies>
...
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
...
</dependencies>
...
</project>
I had to do these steps in order to test a fix I am preparing for H2 to solve the "SET NAMES" syntax error in MySQL compatibility mode when connecting to H2 using the Quercus PHP MySQL driver.
0 Comments

Lightweight self-contained database: SQLite vs. H2 database vs. MySQL embedded

7/8/2012

1 Comment

 
SQLite is a popular choice for PHP programmers to kick-start their development. It's easy, self-contained, requires no configuration, no server and all the data is kept neatly in just one file. Then later on, the application is commonly migrated to a production DBMS (e.g. MySQL). There have not been any alternatives for SQLite when developing in PHP with MySQL as a target platform. But then again, why would you want an alternative for SQLite? Well, to name a few reasons: MySQL compatibility and support for hash indices. And that's not all! Keep reading to see why.

When looking for alternatives to SQLite, I came across two suitable candidates: H2 database engine and a MySQL embedded version. My criteria were that the alternative should be as easy to use as SQLite and compatible with MySQL, based on two reasons. First, I wanted it to be compatible with MySQL so that I can keep using that easy self-contained database for development even after the application has gone into production. Second, an easy to setup database is great for bringing new developers up to speed with development as fast as possible.

Let's start with the good stuff. Below is a comparison of SQLite, H2 database engine and MySQL embedded version.
Comparison of self-contained PHP development databases.
Compare SQLite H2 database engine MySQL Embedded
Footprint 350KiB ~1MB <2MB
License Public domain Dual: Modified MPL 1.1 / EPL 1.0 (commercial friendly) GPL 2.0 (only commercial friendly if not redistributed)
Self-contained ✔ ✔ ✔
Single file ✔ ✔ ✖
Serverless ✔ ✔ ✖
Server-mode ✖ ✔ ✔
Zero-configuration ✔ ✔ ✔
Transactions ✔ ✔ ✔
Indices ✔ (B-tree, R-tree, full-text) ✔ (B-tree, tree, hash, full-text) ✔ (B-tree, R-tree, hash, full-text)
MySQL compatibility ✖ ✔ (but not 100%) ✔
Compatibility with other DBMS ✖ ✔ MySQL, PostgreSQL, Oracle, MSSQL, DB2, HSQLDB and Derby ✖
Encryption ✖ ✔ ✖
In-memory databases ✔ ✔ ✔ (MEMORY storage engine)
To me it looks like H2 database is the easiest to manage. It requires no server and everything is put neatly in one file, which makes back-upping and sharing databases among developers easier. It even has a nice extra feature over MySQL: encryption of data files (though I don't see a direct need for that during development for me personally).

I have experimented with PHP and H2 before and ran into some limitations (Quercus' MySQL driver doesn't play well yet with H2's MySQL compatibility mode so I have to use the Quercus' PDO driver instead). I haven't tried embedding MySQL yet, which is what I am going to try next.

The embedded version of MySQL has the big benefit of being 100% MySQL compatible, but it also has an uncertainty. I am not sure if it's okay to redistribute my application (containing embedded MySQL) among developers. Another limitation could be that I would have to buy a commercial license for MySQL should I choose to distribute embedded MySQL to customers (vs. customers setting up their own MySQL server). Though, it is okay to use MySQL embedded version for a website hosted by myself (it's not technically distributed in that case).

For now, I have not decided yet whether I will stick with H2 or switch to the embedded version of MySQL. I am going to give both a try and I will keep you updated via this blog!
1 Comment

Run Code Igniter on H2 database engine using PDO and Quercus

7/6/2012

4 Comments

 
In my previous post I explained how to setup PHP and H2 database engine using a platform-independent Java stack, a.k.a. JAMP. I managed to get the Code Igniter framework running on JAMP, with some tweaking of the Code Igniter PDO driver. Here are the steps to make it work.
If you would like to skip these steps and jump immediately into some coding action, download the demo.
(Unzip and go into directory jamp-ci-demo/, then type `mvn jetty:run` from the command line. You need Maven installed to execute this command.)
jamp-ci-demo.zip
File Size: 2341 kb
File Type: zip
Download File

First, checkout a copy of JAMP at github (git clone https://github.com/webdevelopersdiary/jamp.git).

Then download Code Igniter and put in the web root (src/main/webapp/).

Because Code Igniter likes pretty urls (ie. /controller_name/method_name) I added support for mod_rewrite to JAMP. It can parse mod_rewrite rules which are located in .htaccess directly in the web root (src/main/webapp/). Add src/main/webbapp/.htaccess with mod_rewrite rules:
<IfModule mod_rewrite.c>
RewriteEngine On

RewriteCond %{REQUEST_URI} ^(system|application).*
RewriteRule ^(.*)$ /index.php?/$1 [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>

<IfModule !mod_rewrite.c>
ErrorDocument 404 /index.php
</IfModule>
Start up the JAMP web server (make sure .htaccess exists before starting the web server or it will have trouble discovering it, then execute mvn jetty:run). Verify Code Igniter is running correctly by going to http://localhost:8080/welcome (the default controller).

If you see a pesky error '$assign_to_config is an undefined variable', go to src/main/webapp/index.php and add
$assign_to_config = array();
under the section 'CUSTOM CONFIG VALUES' (line 110 of index.php) and that should get rid of the error.

Next we configure Code Igniter to connect to the H2 database using PDO. Edit the configuration located at src/main/webapp/application/config/database.php and set these settings:
$db['default']['hostname'] = 'mysql:';
$db['default']['dbdriver'] = 'pdo';
Next, we have to make a slight modification to Code Igniter's PDO driver. Change the method num_rows() from src/main/webapp/system/database/drivers/pdo/pdo_result.php to:

	/**
* Number of rows in the result set
*
* @access public
* @return integer
*/
function num_rows()
{
$sql = $this->result_id->queryString;
if(NULL === $sql) {
$sql = print_r($this->result_id, TRUE);
$quercus_preg = '/\Aresource\(PDOStatement\[(.*)\]\)\z/is';
$php_preg = '/\APDOStatement Object\s*\(.*=>\s*(.*)\)\s*\z/is';
if(preg_match($quercus_preg, $sql)) {
// We're running in Quercus
$sql = preg_replace($quercus_preg, '\1', $sql);
} else if(preg_match($php_preg, $sql)) {
// We're running in PHP
$sql = trim(preg_replace($php_preg, '\1', $sql));
} else {
// No clue about what is running this script!
$sql = NULL;
}
}
if (is_numeric(stripos($sql, 'SELECT')))
{
$count = count($this->result_id->fetchAll());
$this->result_id->execute();
return $count;
}
else
{
return $this->result_id->rowCount();
}
}
Everything should now be in place to connect to the H2 database using Code Igniter's ActiveRecord library. Let's test it! Create a test controller application/controller/test.php:
<?php

class Test extends CI_Controller {
public function index() {
$this->load->database();
$sql = $this->db->query('CREATE TABLE IF NOT EXISTS test (value DATETIME)');
$sql = $this->db->set('value', 'NOW()', FALSE)->insert('test');
var_dump($this->db->get('test')->result());
}
}
Voila! Go to http://localhost:8080/test and enjoy a working Code Igniter on H2 database using ActiveRecord :-)

Note: I used Code Igniter version 2.1.2, Quercus version 4.0.28 and H2 database version 1.3.167.
4 Comments

    Author

    Blog about random challenges of a web developer.

    Archives

    April 2015
    May 2013
    January 2013
    July 2012
    June 2012

    Categories

    All
    Aws
    Cdn
    Code Igniter
    Css
    H2database
    Jamp
    Java
    Javascript
    Magento
    Maven
    Mysql
    Opencl
    Php
    Play-framework
    Quercus
    Scala
    Ubuntu

    RSS Feed

Powered by Create your own unique website with customizable templates.