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..a122cbf 100644 --- a/src/Config.php +++ b/src/Config.php @@ -15,91 +15,92 @@ class Config protected $address; protected $port = 3306; protected $properties = []; - + protected $fileName; + 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 +111,25 @@ 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) { $this->properties[$key] = $value; return $this; } - + public function validate() { if (!$this->getName()) { @@ -144,6 +156,5 @@ public function validate() default: throw new RuntimeException("Unsupported driver: " . $this->getDriver()); } - } } diff --git a/src/Connector.php b/src/Connector.php index 41e60f3..948b6f2 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,26 @@ 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') { + $i = pathinfo($urlPath); + $config->setName($i['filename']); + $config->setFileName($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 +53,7 @@ public function getConfig($dsn) } $this->loadKeys($config, $clusterKeys); } - + if ($config->getServer()) { $serverKeys = $backend->getKeys('servers/' . $config->getServer()); if (!$serverKeys) { @@ -58,7 +66,7 @@ public function getConfig($dsn) } throw new RuntimeException("Can't resolve DSN `" . $dsn . '`'); } - + public function getPdoDsn(Config $config, $mode = 'db') { switch ($mode) { @@ -82,14 +90,14 @@ public function getPdoDsn(Config $config, $mode = 'db') $pdoDsn .= 'charset=utf8;'; break; case 'sqlite': - $pdoDsn .= $config->getName() . ';'; + $pdoDsn .= $config->getFileName(); break; default: throw new RuntimeException("Unsupported driver: " . $config->getDriver()); } return $pdoDsn; } - + public function create(Config $config) { if ($this->exists($config)) { @@ -108,7 +116,7 @@ public function create(Config $config) throw new RuntimeException("Unsupported driver: " . $config->getDriver()); } } - + public function drop(Config $config) { if (!$this->exists($config)) { @@ -127,7 +135,7 @@ public function drop(Config $config) throw new RuntimeException("Unsupported driver: " . $config->getDriver()); } } - + public function exists(Config $config) { switch ($config->getDriver()) { @@ -143,19 +151,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) {