posted 4 years ago in News category by Esen Sagynov
Well, the title speaks for itself. But I will give you some more details on CUBRID and CI engagement.
At the beginning of this month I have decided to work on a CUBRID Driver for CodeIgniter, a popular PHP Framework used by thousands of Web developers. Lucky I am, the framework has very intuitive structure that I could quickly spot the classes required to be inherited and coded for CUBRID.
I found other database drivers in the /system/database/drivers directory. Since CUBRID supports over 90% of MySQL SQL syntax, there was no need to write the code from ground up. I just worked over the MySQL Driver which has already been developed for CI. It took only one day to have the working version of the CUBRID Driver. Couple days later I performed tests (*sigh* CodeIgniter developers said there were no unit tests for the whole framework, including the database drivers, so I had to create my own). Once the stability was confirmed committed the changes to their repository.
Below I will list which parts of the MySQL code have been changed. This will be of a big help to those of you who would like to port MySQL based applications to CUBRID.
- Database name at connection time
Unlike in MySQL, a connection in CUBRID is established per database, i.e. the connection string should indicate the database name you would like to connect to. This eliminates the necessity to "select a database" after the connection. You do not need to do this in CUBRID.
$conn = cubrid_connect($this->hostname, $this->port, $this->database, $this->username, $this->password);
- Broker port
The default port for the CUBRID Broker used for client-broker communication is 33000.
- Auto commit
By default auto_commit parameter in CUBRID is set to OFF, which means users need to manually commit the transactions. This allows users to execute the code in so called dry mode when you obtain the changes but they are not reflected in the database. Useful in the development environment. You can change its value either in your CI database.php configuration file as shown below or in the CUBRID Broker parameters.
$db['default']['hostname'] = 'localhost'; $db['default']['port'] = '33000'; $db['default']['username'] = 'dba'; $db['default']['password'] = 'ci_pass'; $db['default']['database'] = 'ci_demo'; $db['default']['auto_commit'] = TRUE; $db['default']['dbdriver'] = 'cubrid'; $db['default']['dbprefix'] = 'tbl_';
- Quoting identifiers
Sometimes you may want to create a column with a name same as one of the reserved words of the database system. To bypass this, in MySQL you quote the identifiers (table/column names) with a backtick character ( 'time' ) where time is a reserved word. In MSSQL you use square brackets ( [time] ). In Oracle you use double quotes ( "time" ).
In CUBRID you can use all three: backticks, square brackets or double quotes. Double quotes are permitted to be used as an escape character for identifiers by default as the ansi_quotes parameter in CUBRID configuration file (cubrid.conf) is set to no. If ansi_quotes is set to yes, then double quotes will be used to wrap strings just like a single quote.
Identifier escaping String wrapping ansi_quotes = NO (default) backtick 'time'
square brackets [time]
double quotes "time"
single quote 'normal string' ansi_quotes = YES backtick 'time'
square brackets [time]
single quote 'normal string'
double quotes "normal string"
- Optimize, Repair and Backup
These functions are not supported in SQL mode. To perform database optimization, compacting, checking for errors, backup/restore, load/unload, there is a great database administration tool - CUBRID Manager.
Besides these dissimilarities, almost entire SQL queries have not been altered, which proves again how much CUBRID is compatible with MySQL and how much it is easy to port applications written for MySQL to CUBRID. With this example I wanted to show you that porting applications to CUBRID, even as big as CodeIgniter, is very easy and takes only several days. So, if you think of porting something, do not hesitate. It will not be time consuming. And yes, if you need help, let us know in the CUBRID forum. We will be glad to help you.
Regarding the CUBRID Driver source, as you might already know, CodeIgniter has officially moved to GitHub. So I have already committed the CUBRID Driver source code there. I hope CI will soon roll out the new version of its framework which will come with native CUBRID database support.
UPDATE: I have just learnt that CUBRID support will come in CodeIgniter 2.1.0. Let's keep fingers crossed :).
Though, if you do not want to wait for the release, go ahead and fork their develop branch and test out CI with CUBRID. Let me know if you find any bugs. You can report them in our CUBRID Forum.
P.S. There is a quick tutorial how to setup CUBRID with CI posted in CodeIgniter forum.