Skip to content

Commit

Permalink
Merge pull request #24 from superuserdev/bug/23
Browse files Browse the repository at this point in the history
Fix allowing uppercase in table names
  • Loading branch information
Chris Zuber authored Sep 5, 2017
2 parents 383ab9b + 61db8d5 commit 102c647
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 24 deletions.
44 changes: 27 additions & 17 deletions test.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,12 @@
*/
namespace SuperUserDev\SchemaServer;

const CREDS = './creds.json';
const CREDS = __DIR__ . DIRECTORY_SEPARATOR . 'creds.json';
const MIN_PHP_VERSION = '7.1';
const DB_TEST = false;

if (in_array(PHP_SAPI, ['cli', 'cli-server'])) {
set_include_path(dirname(__DIR__, 2) . PATH_SEPARATOR . get_include_path());
spl_autoload_register();
spl_autoload_extensions('.php');
header('Content-Type: ' . Thing::CONTENT_TYPE);
date_default_timezone_set('America/Los_Angeles');

if (version_compare(PHP_VERSION, MIN_PHP_VERSION, '<')) {
throw new \Exception(sprintf('PHP version %s or greater is required', MIN_PHP_VERSION));
}
const DB_TEST = true;
if (version_compare(PHP_VERSION, MIN_PHP_VERSION, '<')) {
throw new \Exception(sprintf('PHP version %s or greater is required', MIN_PHP_VERSION));
} elseif (in_array(PHP_SAPI, ['cli', 'cli-server'])) {

function exception_handler(\Throwable $e): Void
{
Expand All @@ -45,6 +37,16 @@ function exception_handler(\Throwable $e): Void
], JSON_PRETTY_PRINT);
}

function error_handler(
Int $errno,
String $errstr,
String $errfile,
Int $errline
): Void
{
throw new \ErrorException($errstr, $errno, $errno, $errfile, $errline);
}

function gravatar(String $email, Int $size = 80): String
{
return sprintf(
Expand All @@ -54,12 +56,22 @@ function gravatar(String $email, Int $size = 80): String
);
}

set_include_path(dirname(__DIR__, 2) . PATH_SEPARATOR . get_include_path());
spl_autoload_register();
spl_autoload_extensions('.php');
header('Content-Type: ' . Thing::CONTENT_TYPE);
date_default_timezone_set('America/Los_Angeles');
set_exception_handler(__NAMESPACE__ . '\exception_handler');
set_error_handler(__NAMESPACE__ . '\error_handler', E_ALL);

if (file_exists(CREDS)) {
$creds = $creds = json_decode(file_get_contents(CREDS));
$pdo = Thing::connect($creds->user, $creds->pass ?? '', $creds->dbname ?? $creds->user);
exit(Person::get('029e7b946abd6cff214286dff43a7632', $pdo));
}

if (empty($_POST)) {
if (array_key_exists('id', $_GET) and file_exists(CREDS)) {
$creds = json_decode(file_get_contents(CREDS));
$pdo = Thing::connect($creds->user, $creds->pass ?? '', $creds->dbname ?? $creds->user);
exit(Event::get($_GET['id'], $pdo, [], true));
}

Expand Down Expand Up @@ -110,8 +122,6 @@ function gravatar(String $email, Int $size = 80): String
$event->calcDuration();

if (DB_TEST and file_exists(CREDS)) {
$creds = json_decode(file_get_contents(CREDS));
$pdo = Thing::connect($creds->user, $creds->pass ?? '', $creds->dbname ?? $creds->user);
$event->save($pdo);
exit($event);
} else {
Expand Down
5 changes: 3 additions & 2 deletions thing.php
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,8 @@ final public function setUrl(String $url)
final public function setIdentifier(String $id)
{
$this->_set('identifier', $id);
$scheme = $_SERVER['HTTPS'] ? 'http' : 'https';
$this->_set('@id', "{$scheme}://{$_SERVER['HTTP_HOST']}/{$this::getType()}/{$id}");
$scheme = array_key_exists('HTTPS', $_SERVER) and $_SERVER['HTTPS'] ? 'http' : 'https';
$domain = $_SERVER['HTTP_HOST'] ?? 'localhost';
$this->_set('@id', "{$scheme}://{$domain}/{$this::getType()}/{$id}");
}
}
3 changes: 2 additions & 1 deletion traits/data.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ final protected function _set(String $prop, $value)
*/
final public static function getType(): String
{
return @end(explode('\\', static::class));
$class_path = explode('\\', static::class);
return @end($class_path);
}

/**
Expand Down
12 changes: 9 additions & 3 deletions traits/database.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ function(\stdClass $carry, String $item): \stdClass
);

$sql = sprintf(
'INSERT INTO %s (%s) VALUES (%s);',
'INSERT INTO "%s" (%s) VALUES (%s);',
static::getType(),
join(', ', $params->keys),
join(', ', $params->bindings)
Expand Down Expand Up @@ -154,7 +154,10 @@ final public static function get(
Bool $populate = false
): Thing
{
$sql = sprintf('SELECT * FROM %s WHERE identifier = :id LIMIT 1;', static::getType());
$sql = sprintf(
'SELECT * FROM "%s" WHERE "identifier" = :id LIMIT 1;',
static::getType()
);
$stm = $pdo->prepare($sql);
$stm->execute(['id' => $identifier]);
$obj = $stm->fetch();
Expand Down Expand Up @@ -183,7 +186,10 @@ final public static function get(
*/
final public static function delete(String $identifier, PDO $pdo): Bool
{
$sql = sprintf('DELETE FROM %s WHERE "identifier" = :id LIMIT 1;', static::getType());
$sql = sprintf(
'DELETE FROM "%s" WHERE "identifier" = :id LIMIT 1;',
static::getType()
);
$stm = $pdo->prepare($sql);
$stm->execute(['id' => $identifier]);
return $stm->rowCount() > 0;
Expand Down
2 changes: 1 addition & 1 deletion traits/parse.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ trait Parse
final public static function parseFromArray(Array $data): Thing
{
if (array_key_exists('@type', $data)) {
$type = __NAMESPACE__ . '\\' . $data['@type'];
$type = "\\superuserdev\\SchemaServer\\{$data['@type']}";
return new $type($data);
} else {
throw new \RuntimeException('Missing @type attribute');
Expand Down

0 comments on commit 102c647

Please sign in to comment.