From ad4a149c32bd42bf374b56c88cdf43b5ee801874 Mon Sep 17 00:00:00 2001 From: Filipp Lepalaan Date: Thu, 15 Jul 2010 23:51:05 +0300 Subject: sqlite support --- MainDb.php | 33 ++++++++++++++++++++++++++------- 1 file changed, 26 insertions(+), 7 deletions(-) (limited to 'MainDb.php') diff --git a/MainDb.php b/MainDb.php index e672651..6417c19 100644 --- a/MainDb.php +++ b/MainDb.php @@ -21,13 +21,20 @@ class MainDb if (!self::$instance) { try { - self::$instance = new PDO( - "{$c['db.driver']}:host={$c['db.host']};dbname={$c['db.name']}", - $c['db.username'], $c['db.password'], array(PDO::ATTR_PERSISTENT => true) - ); - - // always use UTF-8? - self::$instance->query('SET NAMES utf8'); + switch ($c['db.driver']) { + case 'mysql': + self::$instance = new PDO( + "{$c['db.driver']}:host={$c['db.host']};dbname={$c['db.name']}", + $c['db.username'], $c['db.password'], array(PDO::ATTR_PERSISTENT => true) + ); + // always use UTF-8? + self::$instance->query('SET NAMES utf8'); + break; + + case 'sqlite': + self::$instance = new PDO('sqlite:'.$c['db.path']); + break; + } } catch (PDOException $e) { exit(MainApp::error($e->getMessage())); @@ -64,6 +71,13 @@ class MainDb $pdo = self::getInstance(); $stmt = $pdo->prepare($sql); + + if (!$stmt) { + list($ec, $dec, $emsg) = $pdo->errorInfo(); + $error = $emsg ."\n" . print_r(debug_backtrace(), true); + return MainApp::error($error); + } + $result = $stmt->execute($data); if (!$result) { @@ -88,6 +102,11 @@ class MainDb return $stmt; } + // describe statements need the query results + if (preg_match('/^PRAGMA/i', $sql)) { + return $stmt; + } + if (empty($data[':id'])) { $data[':id'] = $pdo->lastInsertId(); } -- cgit v1.2.3