Skip to content

Commit

Permalink
Merge branch 'master' of github.com:linkorb/connector
Browse files Browse the repository at this point in the history
  • Loading branch information
joostfaassen committed Nov 2, 2018
2 parents 1891eec + e97ab41 commit 8be656e
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 36 deletions.
30 changes: 30 additions & 0 deletions examples/url-pdo-sqlite-example.php
@@ -0,0 +1,30 @@
<?php

require_once(__DIR__ . '/../vendor/autoload.php');

$connector = new \Connector\Connector();

$config = $connector->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));
55 changes: 33 additions & 22 deletions src/Config.php
Expand Up @@ -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;
Expand All @@ -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()) {
Expand All @@ -144,6 +156,5 @@ public function validate()
default:
throw new RuntimeException("Unsupported driver: " . $this->getDriver());
}

}
}
39 changes: 25 additions & 14 deletions src/Connector.php
Expand Up @@ -13,7 +13,7 @@ public function registerBackend(BackendInterface $backend)
{
$this->backends[] = $backend;
}

public function getConfig($dsn)
{
if (filter_var($dsn, FILTER_VALIDATE_URL)) {
Expand All @@ -26,26 +26,34 @@ 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) {
throw new RuntimeException("No configuration keys found for cluster: " . $config->getCluster());
}
$this->loadKeys($config, $clusterKeys);
}

if ($config->getServer()) {
$serverKeys = $backend->getKeys('servers/' . $config->getServer());
if (!$serverKeys) {
Expand All @@ -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) {
Expand All @@ -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)) {
Expand All @@ -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)) {
Expand All @@ -127,7 +135,7 @@ public function drop(Config $config)
throw new RuntimeException("Unsupported driver: " . $config->getDriver());
}
}

public function exists(Config $config)
{
switch ($config->getDriver()) {
Expand All @@ -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) {
Expand Down

0 comments on commit 8be656e

Please sign in to comment.