PHP 5, SQLite in Factory?
In the previous article we introduced the concept of factory in PHP 5. In this one i'm gonna show out these techniques reported to a database.
XII. SQLite?
After a while I said let's try to create another product for May's factory described in the previous tutorial.
I would have liked to have the opportunity to loging errors and debug messages in a database table.
And as I haven't had the opportunity to work with SQLite, I decided that it is the type of database i shoul use it.
The first conclusion?
If i want to use the SQLite command line client and then access the database created with this client from PHP 5.0 i should use SQLite 2.8.15 and not SQLite 3.0.8. Databases created with SQLite 3.0.8 will be available in PHP 5.1.
So, using the SQLite client, version 2.8.15, I got to work, and created a new database:
$ sqlite logger.db
I learned that I can still write SQL and then import it into a SQLite database.
dump.sql:
CODE:
BEGIN TRANSACTION;
CREATE TABLE logger (id INTEGER PRIMARY KEY,message TEXT,timeEnter DATE);
CREATE TRIGGER insert_logger_timeEnter AFTER INSERT ON logger
BEGIN
UPDATE logger SET timeEnter = DATETIME('NOW') WHERE rowid = new.rowid;
END;
COMMIT; Ordinary things here:
A table with the name logger that contains a field id integer primary key, a message field, where I will take messages and a timeEnter obvious that I will keep the date of the new message.
In addition, I created a trigger, so every insert i'l dol, timeEnter field will update itself(do not need to tell him again to insert into logger (timeEnter) values DATETIME ('NOW')
It will be enough to update the three fields). We have imported dump.sql file with the command:
$ sqlite logger.db < dump.sql
Binary (. exe file on Windows) should be in the PATH to get sqlite command at the prompt.
XIII. A new product, SQLite
Given that you have to implement interface methods and Trace, following the example of FileTrace I wrote pretty quickly and easily SQLiteTrace.php:
CODE:
<?php
// works with sqlite 2.8.15 on php 5.0!
/*
----------> SQL DUMP
BEGIN TRANSACTION;
CREATE TABLE logger (id INTEGER PRIMARY KEY,message TEXT,timeEnter DATE);
CREATE TRIGGER insert_logger_timeEnter AFTER INSERT ON logger
BEGIN
UPDATE logger SET timeEnter = DATETIME('NOW') WHERE rowid = new.rowid;
END;
COMMIT;
<---------- SQL DUMP
*/
include_once('ITrace.php');
/**
* It logs the trace to a sqlite db
*
* @todo: implement a custom Exception class
* @package test.factory.trace
* @access public
*/
class SQLiteTrace implements Trace {
/**
* It`s the db handler
* @var handler
* @access private
*/
private $handler;
/**
* debug
* @var bool
* @access private
*/
private $debug;
/**
* The construnctor
* it opens the db handler
*
* @return void
* @access public
*/
public function __construct() {
$dbname = 'logger.db';
$this->handler = @sqlite_open($dbname, 0666, $sqliteerror);
if($this->handler===false) {
throw new Exception($sqliteerror);
}
}
/**
* Sets the debug
* @param bool debug
* @return void
* @access public
*/
public function setDebug($debug) {
$this->debug = $debug;
}
/**
* It writes a debug message
* only if debug is true
* @param string message
* @return void
* @access public
*/
public function debug($message) {
if($this->debug) {
// only insert if debug is true
sqlite_query($this->handler, "INSERT INTO logger (message) VALUES
(' DEBUG >>>> ".$message."')");
}
}
/**
* It writes an error message
*
* @param string message
* @return void
* @access public
*/
public function error($message) {
// always log errors
sqlite_query($this->handler, "INSERT INTO logger (message) VALUES
(' ERROR >>>> ".$message."')");
}
/**
* Is the Destructor
* just close the handler
* @access public
* return void
*/
public function __destruct() {
@sqlite_close($this->handler);
}
}
?> XIV. A new creator
I managed to rewrite TraceFactory.php as follows:
CODE:
<?php
/**
* Is the factory
*
* @version 0.3
* @access public
* @package test.factory.trace
*/
class TraceFactory {
/**
* It trys to get a trace
*
* @access public
* @return object Trace, a trace instance
*/
public static function &getTrace() {
try {
include_once('FileTrace.php');
return new FileTrace();
}
catch (Exception $ex) {
try {
include_once('SQLiteTrace.php');
$t_sqlite = new SQLiteTrace();
$t_sqlite->error("Could not instantiate FileTrace:\n".$ex->getMessage());
return $t_sqlite;
}
catch(Exception $sqlite_ex) {
include_once('StdoutTrace.php');
$t = new StdoutTrace();
$t->error("Could not instantiate SQLiteTrace:\n".$sqlite_ex->getMessage());
return $t;
}
}
}
}
?> Even if i don't like too much the block:
CODE:
<?php
try {
}
catch (Exception $ex) {
try {
}
catch(Exception $sqlite_ex) {
}
?> My Tracer is running with 3 different methods to log messages:
- in a file
- in a database or
- directly on the console (not going to see this stuff too soon, however).
XV. Shortcomings
At the moment, frankly, I can not figure out where I could implement this example.
I would like to continue, calling into question another process: observe, more than I would like to have several levels of logging (debug / info / error / alert / emergency).
GetTrace method does not seem too flexible, such as if I wanted to add yet another product out there?
Perhaps we should rewrite this:
CODE:
<?php
/**
* Is the factory
*
* @version 0.4
* @access public
* @package test.factory.trace
*/
class TraceFactory {
/**
* It trys to get a trace
*
* @param string driver, it can be File or SQLite
* @access public
* @return object Trace, a trace instance
*/
public static function &getTrace($driver='') {
$drivers = array('File','SQLite');
if(!in_array($driver, $drivers)) {
include_once('StdoutTrace.php');
return new StdoutTrace();
}
try {
$rs = $driver.'Trace';
include_once($rs.'.php');
return new $rs();
}
catch (Exception $ex) {
include_once('StdoutTrace.php');
$t = new StdoutTrace();
$t->error("Could not instantiate ".$driver.":\n".$ex->getMessage());
return $t;
}
}
}
?> and the next test, I would runTrace.php log messages in text file in the first part, the SQLite database in the middle and finally I show and something in the console:
CODE:
<?php
include_once('TraceFactory.php');
$t = &TraceFactory::getTrace('File');
$t->error("O eroare de test!");
$t->setDebug(true);
$t->debug("Debug is true");
for($i=1;$i<10;$i++) {
$t->debug("Tha lup :: ".$i);
}
unset($t);
$t = &TraceFactory::getTrace('SQLite');
$t->error("Ceva de test!");
$t->setDebug(true);
$t->debug("True Debug");
for($i=1;$i<10;$i++) {
$t->debug("Za I :: ".$i);
}
$t->error("FOO IS BAR MUST DIE !!!");
unset($t);
$t = &TraceFactory::getTrace('Unknow_driver');
$t->error("Ajunge in consola!");
$t->setDebug(true);
$t->debug("True Debug a fos setat");
for($i=1;$i<10;$i++) {
$t->debug("I-ul :: ".$i);
}
$t->error("Fatal!!!");
?> It remains, however, discussed the different levels of logging and the introduction of an observer to notify me if an error occurs in the system.
In the meantime, i'll stay with PEAR:: Log, but I still keep this article by introducing components above, which means an item of log messages as complete and as used in production (even if you do not see that SQLite is not so fast:)).
There is a similar article, click here for list.
| There are no comments on this article. Be the first to say your opinion. |






