From aeeb28366cf34d922f702c92a2cffd49fdd41b61 Mon Sep 17 00:00:00 2001 From: Hongliang Date: Mon, 6 Feb 2017 16:26:20 +0100 Subject: [PATCH 1/2] sqlite support --- examples/url-pdo-sqlite-example.php | 30 ++++++++++++++++++++ src/Config.php | 43 ++++++++++++++--------------- src/Connector.php | 37 +++++++++++++++---------- 3 files changed, 74 insertions(+), 36 deletions(-) create mode 100644 examples/url-pdo-sqlite-example.php diff --git a/examples/url-pdo-sqlite-example.php b/examples/url-pdo-sqlite-example.php new file mode 100644 index 0000000..037c3d0 --- /dev/null +++ b/examples/url-pdo-sqlite-example.php @@ -0,0 +1,30 @@ +getConfig('sqlite://file/tmp/test.sqlite3'); +if (!$connector->exists($config)) { + echo "Creating database\n"; + $connector->create($config); +} + +$pdo = $connector->getPdo($config); +$pdo->exec( + "CREATE TABLE IF NOT EXISTS message ( + id INTEGER PRIMARY KEY, + title TEXT, + message TEXT, + time INTEGER)" +); + +$stmt = $pdo->prepare( + "INSERT INTO message (title, message, time) + VALUES (:title, :message, :time)" +); +$stmt->execute(['title' => 'test', 'message' => 'wola', 'time' => time()]); + +$stmt = $pdo->prepare("SELECT * FROM message ORDER BY time DESC"); +$stmt->execute(); +print_r($stmt->fetchAll(PDO::FETCH_ASSOC)); diff --git a/src/Config.php b/src/Config.php index dabec3e..6de3d35 100644 --- a/src/Config.php +++ b/src/Config.php @@ -15,91 +15,91 @@ class Config protected $address; protected $port = 3306; protected $properties = []; - + public function getName() { return $this->name; } - + public function setName($name) { $this->name = $name; return $this; } - + public function getPort() { return $this->port; } - + public function setPort($port) { $this->port = $port; return $this; } - + public function getServer() { return $this->server; } - + public function setServer($server) { $this->server = $server; return $this; } - + public function getCluster() { return $this->cluster; } - + public function setCluster($cluster) { $this->cluster = $cluster; return $this; } - - + + public function getAddress() { return $this->address; } - + public function setAddress($address) { $this->address = $address; return $this; } - - + + public function getPassword() { return $this->password; } - + public function setPassword($password) { $this->password = $password; return $this; } - + public function getUsername() { return $this->username; } - + public function setUsername($username) { $this->username = $username; return $this; } - + public function getDriver() { return $this->driver; } - + public function setDriver($driver) { $this->driver = $driver; @@ -110,14 +110,14 @@ public function setDriver($driver) } return $this; } - - + + public function setProperty($key, $value) { $this->properties[$key] = $value; return $this; } - + public function validate() { if (!$this->getName()) { @@ -144,6 +144,5 @@ public function validate() default: throw new RuntimeException("Unsupported driver: " . $this->getDriver()); } - } } diff --git a/src/Connector.php b/src/Connector.php index adfebb1..6ce62c1 100644 --- a/src/Connector.php +++ b/src/Connector.php @@ -13,7 +13,7 @@ public function registerBackend(BackendInterface $backend) { $this->backends[] = $backend; } - + public function getConfig($dsn) { if (filter_var($dsn, FILTER_VALIDATE_URL)) { @@ -26,18 +26,24 @@ public function getConfig($dsn) if ($port) { $config->setPort($port); } - $config->setName(substr(parse_url($dsn, PHP_URL_PATH), 1)); + $urlPath = parse_url($dsn, PHP_URL_PATH); + if (parse_url($dsn, PHP_URL_SCHEME) == 'sqlite') { + $config->setName($urlPath); + } else { + $config->setName(substr($urlPath, 1)); + } + // TODO: support further resolving cluster/server settings // from backends based on address/host/server? return $config; } - + foreach ($this->backends as $backend) { $dsnKeys = $backend->getKeys($dsn); if ($dsnKeys) { $config = new Config(); $this->loadKeys($config, $dsnKeys); - + if ($config->getCluster()) { $clusterKeys = $backend->getKeys('clusters/' . $config->getCluster()); if (!$clusterKeys) { @@ -45,7 +51,7 @@ public function getConfig($dsn) } $this->loadKeys($config, $clusterKeys); } - + if ($config->getServer()) { $serverKeys = $backend->getKeys('servers/' . $config->getServer()); if (!$serverKeys) { @@ -58,7 +64,7 @@ public function getConfig($dsn) } throw new RuntimeException("Can't resolve DSN `" . $dsn . '`'); } - + public function getPdoDsn(Config $config, $mode = 'db') { switch ($mode) { @@ -81,14 +87,14 @@ public function getPdoDsn(Config $config, $mode = 'db') } break; case 'sqlite': - $pdoDsn .= $config->getName() . ';'; + $pdoDsn .= $config->getName(); break; default: throw new RuntimeException("Unsupported driver: " . $config->getDriver()); } return $pdoDsn; } - + public function create(Config $config) { if ($this->exists($config)) { @@ -107,7 +113,7 @@ public function create(Config $config) throw new RuntimeException("Unsupported driver: " . $config->getDriver()); } } - + public function drop(Config $config) { if (!$this->exists($config)) { @@ -126,7 +132,7 @@ public function drop(Config $config) throw new RuntimeException("Unsupported driver: " . $config->getDriver()); } } - + public function exists(Config $config) { switch ($config->getDriver()) { @@ -142,19 +148,22 @@ public function exists(Config $config) ] ); return (bool) $stmt->fetchColumn(); + case 'sqlite': + return $this->getPdo($config, 'server'); + break; default: throw new RuntimeException("Unsupported driver"); } } - + public function getPdo(Config $config, $mode = 'db') { $pdoDsn = $this->getPdoDsn($config, $mode); $config->validate(); // throws if invalid - $pdo = new PDO($pdoDsn, $config->getUsername(), $config->getPassword()); - return $pdo; + + return new PDO($pdoDsn, $config->getUsername(), $config->getPassword()); } - + public function loadKeys(Config $config, $keys) { if (!$keys) { From e97ab41983afd552dc207d2766244885d6676912 Mon Sep 17 00:00:00 2001 From: Hongliang Date: Wed, 8 Feb 2017 15:06:32 +0100 Subject: [PATCH 2/2] sqlite: separate name and fileName in config --- src/Config.php | 12 ++++++++++++ src/Connector.php | 6 ++++-- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/src/Config.php b/src/Config.php index 6de3d35..a122cbf 100644 --- a/src/Config.php +++ b/src/Config.php @@ -15,6 +15,7 @@ class Config protected $address; protected $port = 3306; protected $properties = []; + protected $fileName; public function getName() { @@ -111,6 +112,17 @@ public function setDriver($driver) return $this; } + public function getFileName() + { + return $this->fileName; + } + + public function setFileName($fileName) + { + $this->fileName = $fileName; + + return $this; + } public function setProperty($key, $value) { diff --git a/src/Connector.php b/src/Connector.php index 6ce62c1..314d25e 100644 --- a/src/Connector.php +++ b/src/Connector.php @@ -28,7 +28,9 @@ public function getConfig($dsn) } $urlPath = parse_url($dsn, PHP_URL_PATH); if (parse_url($dsn, PHP_URL_SCHEME) == 'sqlite') { - $config->setName($urlPath); + $i = pathinfo($urlPath); + $config->setName($i['filename']); + $config->setFileName($urlPath); } else { $config->setName(substr($urlPath, 1)); } @@ -87,7 +89,7 @@ public function getPdoDsn(Config $config, $mode = 'db') } break; case 'sqlite': - $pdoDsn .= $config->getName(); + $pdoDsn .= $config->getFileName(); break; default: throw new RuntimeException("Unsupported driver: " . $config->getDriver());