Added function to database object for interpreting parameter values to ones that are database-appropriate. Specifically, to turn true/false into the correct values for a bool column.

This commit is contained in:
Matthew Barbour 2019-06-25 13:58:50 -05:00 committed by Shish
parent cb436cc182
commit 6a6d73168b
2 changed files with 34 additions and 6 deletions

View file

@ -167,6 +167,19 @@ class Database
return $this->engine->scoreql_to_sql($input);
}
public function scoresql_value_prepare($input)
{
if (is_null($this->engine)) {
$this->connect_engine();
}
if($input===true) {
return $this->engine->BOOL_Y;
} else if ($input===false) {
return $this->engine->BOOL_N;
}
return $input;
}
public function get_driver_name(): string
{
if (is_null($this->engine)) {

View file

@ -4,6 +4,9 @@ class DBEngine
/** @var null|string */
public $name = null;
public $BOOL_Y = null;
public $BOOL_N = null;
public function init(PDO $db)
{
}
@ -24,6 +27,9 @@ class MySQL extends DBEngine
/** @var string */
public $name = DatabaseDriver::MYSQL;
public $BOOL_Y = 'Y';
public $BOOL_N = 'N';
public function init(PDO $db)
{
$db->exec("SET NAMES utf8;");
@ -33,8 +39,8 @@ class MySQL extends DBEngine
{
$data = str_replace("SCORE_AIPK", "INTEGER PRIMARY KEY auto_increment", $data);
$data = str_replace("SCORE_INET", "VARCHAR(45)", $data);
$data = str_replace("SCORE_BOOL_Y", "'Y'", $data);
$data = str_replace("SCORE_BOOL_N", "'N'", $data);
$data = str_replace("SCORE_BOOL_Y", "'$this->BOOL_Y'", $data);
$data = str_replace("SCORE_BOOL_N", "'$this->BOOL_N'", $data);
$data = str_replace("SCORE_BOOL", "ENUM('Y', 'N')", $data);
$data = str_replace("SCORE_DATETIME", "DATETIME", $data);
$data = str_replace("SCORE_NOW", "\"1970-01-01\"", $data);
@ -53,9 +59,14 @@ class MySQL extends DBEngine
class PostgreSQL extends DBEngine
{
/** @var string */
public $name = DatabaseDriver::PGSQL;
public $BOOL_Y = 't';
public $BOOL_N = 'f';
public function init(PDO $db)
{
if (array_key_exists('REMOTE_ADDR', $_SERVER)) {
@ -70,8 +81,8 @@ class PostgreSQL extends DBEngine
{
$data = str_replace("SCORE_AIPK", "SERIAL PRIMARY KEY", $data);
$data = str_replace("SCORE_INET", "INET", $data);
$data = str_replace("SCORE_BOOL_Y", "'t'", $data);
$data = str_replace("SCORE_BOOL_N", "'f'", $data);
$data = str_replace("SCORE_BOOL_Y", "'$this->BOOL_Y'", $data);
$data = str_replace("SCORE_BOOL_N", "'$this->BOOL_N'", $data);
$data = str_replace("SCORE_BOOL", "BOOL", $data);
$data = str_replace("SCORE_DATETIME", "TIMESTAMP", $data);
$data = str_replace("SCORE_NOW", "current_timestamp", $data);
@ -138,6 +149,10 @@ class SQLite extends DBEngine
/** @var string */
public $name = DatabaseDriver::SQLITE;
public $BOOL_Y = 'Y';
public $BOOL_N = 'N';
public function init(PDO $db)
{
ini_set('sqlite.assoc_case', 0);
@ -158,8 +173,8 @@ class SQLite extends DBEngine
{
$data = str_replace("SCORE_AIPK", "INTEGER PRIMARY KEY", $data);
$data = str_replace("SCORE_INET", "VARCHAR(45)", $data);
$data = str_replace("SCORE_BOOL_Y", "'Y'", $data);
$data = str_replace("SCORE_BOOL_N", "'N'", $data);
$data = str_replace("SCORE_BOOL_Y", "'$this->BOOL_Y'", $data);
$data = str_replace("SCORE_BOOL_N", "'$this->BOOL_N'", $data);
$data = str_replace("SCORE_BOOL", "CHAR(1)", $data);
$data = str_replace("SCORE_NOW", "\"1970-01-01\"", $data);
$data = str_replace("SCORE_STRNORM", "lower", $data);