summaryrefslogtreecommitdiff
path: root/vendor/PicoDb/Database.php
diff options
context:
space:
mode:
authorFrédéric Guillot <contact@fredericguillot.com>2014-01-25 14:56:02 -0500
committerFrédéric Guillot <contact@fredericguillot.com>2014-01-25 14:56:02 -0500
commit9383a15af699ede77142d040b65118e15754a2ca (patch)
treeb550b5adf5bcf8f5a8793c188cc5630f26a27d49 /vendor/PicoDb/Database.php
First commit
Diffstat (limited to 'vendor/PicoDb/Database.php')
-rw-r--r--vendor/PicoDb/Database.php108
1 files changed, 108 insertions, 0 deletions
diff --git a/vendor/PicoDb/Database.php b/vendor/PicoDb/Database.php
new file mode 100644
index 00000000..c3405f72
--- /dev/null
+++ b/vendor/PicoDb/Database.php
@@ -0,0 +1,108 @@
+<?php
+
+namespace PicoDb;
+
+class Database
+{
+ private $logs = array();
+ private $pdo;
+
+
+ public function __construct(array $settings)
+ {
+ if (! isset($settings['driver'])) {
+
+ throw new \LogicException('You must define a database driver.');
+ }
+
+ switch ($settings['driver']) {
+
+ case 'sqlite':
+ require_once __DIR__.'/Drivers/Sqlite.php';
+ $this->pdo = new Sqlite($settings['filename']);
+ break;
+
+ default:
+ throw new \LogicException('This database driver is not supported.');
+ }
+
+ $this->pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
+ }
+
+
+ public function setLogMessage($message)
+ {
+ $this->logs[] = $message;
+ }
+
+
+ public function getLogMessages()
+ {
+ return $this->logs;
+ }
+
+
+ public function getConnection()
+ {
+ return $this->pdo;
+ }
+
+
+ public function escapeIdentifier($value)
+ {
+ return $this->pdo->escapeIdentifier($value);
+ }
+
+
+ public function execute($sql, array $values = array())
+ {
+ try {
+
+ $this->setLogMessage($sql);
+ $this->setLogMessage(implode(', ', $values));
+
+ $rq = $this->pdo->prepare($sql);
+ $rq->execute($values);
+
+ return $rq;
+ }
+ catch (\PDOException $e) {
+
+ if ($this->pdo->inTransaction()) $this->pdo->rollback();
+ $this->setLogMessage($e->getMessage());
+ return false;
+ }
+ }
+
+
+ public function startTransaction()
+ {
+ $this->pdo->beginTransaction();
+ }
+
+
+ public function closeTransaction()
+ {
+ $this->pdo->commit();
+ }
+
+
+ public function cancelTransaction()
+ {
+ $this->pdo->rollback();
+ }
+
+
+ public function table($table_name)
+ {
+ require_once __DIR__.'/Table.php';
+ return new Table($this, $table_name);
+ }
+
+
+ public function schema()
+ {
+ require_once __DIR__.'/Schema.php';
+ return new Schema($this);
+ }
+} \ No newline at end of file