2018-10-09 21:30:00 +02:00
|
|
|
<?php
|
|
|
|
|
2019-04-04 19:23:03 +02:00
|
|
|
require_once( __DIR__ . "/class.sql.conversions.php" );
|
2019-02-04 22:42:12 +01:00
|
|
|
|
2018-10-09 21:30:00 +02:00
|
|
|
class sql {
|
|
|
|
|
2019-04-11 01:31:28 +02:00
|
|
|
const DB_TYPES = array(
|
|
|
|
|
|
|
|
"MySQL" => "mysql",
|
|
|
|
"PostgresSQL" => "pgsql",
|
|
|
|
"SQLite" => "sqlite",
|
|
|
|
);
|
|
|
|
|
2018-10-09 21:30:00 +02:00
|
|
|
public $connection = null;
|
2019-04-04 14:18:08 +02:00
|
|
|
public $conversions = null;
|
2019-02-04 22:42:12 +01:00
|
|
|
public $identifier_character = null;
|
|
|
|
protected static $instance = null;
|
2018-10-09 21:30:00 +02:00
|
|
|
|
|
|
|
public function __construct() {
|
|
|
|
|
2019-04-04 14:18:08 +02:00
|
|
|
$this->conversions = new sql_conversions();
|
2019-02-04 22:42:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function close() {
|
|
|
|
|
|
|
|
$this->connection = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function connect() {
|
|
|
|
|
|
|
|
if( $this->connection == null ) {
|
|
|
|
|
|
|
|
$host = DBHOST;
|
|
|
|
$dbname = DBNAME;
|
|
|
|
$dbtype = DBTYPE;
|
|
|
|
$username = DBUSER;
|
|
|
|
$password = DBPASS;
|
|
|
|
|
|
|
|
$this->connection = new PDO( "{$dbtype}:host={$host};dbname={$dbname}", $username, $password );
|
|
|
|
}
|
|
|
|
|
|
|
|
return( $this->connection );
|
|
|
|
}
|
|
|
|
|
2019-04-04 19:23:03 +02:00
|
|
|
public function create_table( $table_name, $fields=array(), $attributes=array() ) {
|
2019-04-04 14:18:08 +02:00
|
|
|
|
2019-04-04 19:23:03 +02:00
|
|
|
$query = $this->conversions->table( $table_name, $fields, $attributes );
|
|
|
|
//$this->query( $query, array(), array(), null, "rowCount" );
|
|
|
|
}
|
|
|
|
|
2019-04-12 15:45:42 +02:00
|
|
|
public function create_default_tables() {
|
|
|
|
|
2019-04-12 18:11:27 +02:00
|
|
|
$result = $this->create_tables(
|
2019-04-12 15:45:42 +02:00
|
|
|
array(
|
|
|
|
"active" => array(
|
|
|
|
"fields" => array(
|
|
|
|
"username" => "string",
|
|
|
|
"path" => "text",
|
|
|
|
"position" => "string",
|
|
|
|
"focused" => "string"
|
|
|
|
),
|
|
|
|
"attributes" => array(
|
|
|
|
"username" => array( "not null", "unique" ),
|
|
|
|
"path" => array( "not null", "unique" ),
|
|
|
|
"focused" => array( "not null" ),
|
|
|
|
)
|
|
|
|
),
|
|
|
|
"options" => array(
|
|
|
|
"fields" => array(
|
|
|
|
"id" => "int",
|
|
|
|
"name" => "string",
|
|
|
|
"value" => "text",
|
|
|
|
),
|
|
|
|
"attributes" => array(
|
|
|
|
"id" => array( "id" ),
|
|
|
|
"name" => array( "not null", "unique" ),
|
|
|
|
"value" => array( "not null" ),
|
|
|
|
)
|
|
|
|
),
|
|
|
|
"projects" => array(
|
|
|
|
"fields" => array(
|
|
|
|
"id" => "int",
|
|
|
|
"name" => "string",
|
|
|
|
"path" => "text",
|
|
|
|
"owner" => "string",
|
|
|
|
"access" => "string",
|
|
|
|
),
|
|
|
|
"attributes" => array(
|
|
|
|
|
|
|
|
"id" => array( "id" ),
|
|
|
|
"name" => array( "not null" ),
|
|
|
|
"path" => array( "not null", "unique" ),
|
|
|
|
"owner" => array( "not null", "unique" ),
|
|
|
|
"access" => array(),
|
|
|
|
)
|
|
|
|
),
|
|
|
|
"users" => array(
|
|
|
|
"fields" => array(
|
|
|
|
"id" => "int",
|
|
|
|
"first_name" => "string",
|
|
|
|
"last_name" => "string",
|
|
|
|
"username" => "string",
|
|
|
|
"password" => "text",
|
|
|
|
"email" => "string",
|
|
|
|
"project" => "string",
|
|
|
|
"access" => "string",
|
|
|
|
"groups" => "string",
|
|
|
|
"token" => "string",
|
|
|
|
),
|
|
|
|
"attributes" => array(
|
|
|
|
"id" => array( "id" ),
|
|
|
|
"username" => array( "not null", "unique" ),
|
|
|
|
"password" => array( "not null" ),
|
|
|
|
"access" => array( "not null" ),
|
|
|
|
)
|
|
|
|
),
|
|
|
|
"user_options" => array(
|
|
|
|
"fields" => array(
|
|
|
|
"id" => "int",
|
|
|
|
"name" => "string",
|
|
|
|
"username" => "string",
|
|
|
|
"value" => "text",
|
|
|
|
),
|
|
|
|
"attributes" => array(
|
|
|
|
"id" => array( "id" ),
|
|
|
|
"name" => array( "not null", "unique" ),
|
|
|
|
"username" => array( "not null", "unique" ),
|
|
|
|
"value" => array( "not null" ),
|
|
|
|
)
|
|
|
|
),
|
|
|
|
)
|
|
|
|
);
|
2019-04-12 18:11:27 +02:00
|
|
|
|
|
|
|
return $result;
|
2019-04-12 15:45:42 +02:00
|
|
|
}
|
|
|
|
|
2019-04-04 19:23:03 +02:00
|
|
|
public function create_tables( $table ) {
|
2019-04-04 14:18:08 +02:00
|
|
|
|
2019-04-04 19:23:03 +02:00
|
|
|
/**
|
|
|
|
Tables layout
|
|
|
|
array(
|
2019-04-04 14:18:08 +02:00
|
|
|
|
2019-04-04 19:23:03 +02:00
|
|
|
"table_name" => array(
|
2019-04-04 14:18:08 +02:00
|
|
|
|
2019-04-04 19:23:03 +02:00
|
|
|
"fields" => array(
|
2019-04-04 14:18:08 +02:00
|
|
|
|
2019-04-04 19:23:03 +02:00
|
|
|
"id" => "int",
|
|
|
|
"test_field" => "string"
|
|
|
|
),
|
|
|
|
"attributes" => array(
|
2019-04-04 14:18:08 +02:00
|
|
|
|
2019-04-04 19:23:03 +02:00
|
|
|
"id" => array( "id" ),
|
|
|
|
"test_field" => array( "not null" ),
|
|
|
|
)
|
|
|
|
),
|
|
|
|
"table2_name" => array(
|
|
|
|
|
|
|
|
"fields" => array(
|
2019-04-04 14:18:08 +02:00
|
|
|
|
2019-04-04 19:23:03 +02:00
|
|
|
"id" => "int",
|
|
|
|
"test_field" => "string"
|
|
|
|
),
|
|
|
|
"attributes" => array(
|
2019-04-04 14:18:08 +02:00
|
|
|
|
2019-04-04 19:23:03 +02:00
|
|
|
"id" => array( "id" ),
|
|
|
|
"test_field" => array( "not null" ),
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
*/
|
2019-04-04 14:18:08 +02:00
|
|
|
|
2019-04-04 19:23:03 +02:00
|
|
|
$query = $this->conversions->tables( $table );
|
2019-04-11 01:31:28 +02:00
|
|
|
$connection = $this->connect();
|
|
|
|
$result = $connection->exec( $query );
|
2019-04-12 00:26:55 +02:00
|
|
|
$error = $connection->errorInfo();
|
2019-04-11 01:31:28 +02:00
|
|
|
//echo var_dump( $query, $result, $connection->errorInfo() ) . "<br>";
|
2019-04-12 00:26:55 +02:00
|
|
|
|
|
|
|
if ( $result === false || ! $error[0] == "00000" ) {
|
|
|
|
|
2019-04-30 18:56:08 +02:00
|
|
|
return $error;
|
2019-04-12 18:11:27 +02:00
|
|
|
} else {
|
|
|
|
|
|
|
|
return true;
|
2019-04-12 00:26:55 +02:00
|
|
|
}
|
2019-04-04 14:18:08 +02:00
|
|
|
}
|
|
|
|
|
2019-02-04 22:42:12 +01:00
|
|
|
public static function escape_identifier( $i ) {
|
|
|
|
|
|
|
|
$i = preg_replace('/[^A-Za-z0-9_]+/', '', $i );
|
2019-04-04 19:23:03 +02:00
|
|
|
return $i;
|
2018-10-09 21:30:00 +02:00
|
|
|
}
|
|
|
|
|
2019-02-04 22:42:12 +01:00
|
|
|
public static function is_not_error( $i ) {
|
2018-11-10 06:41:28 +01:00
|
|
|
|
|
|
|
$return = false;
|
2019-02-04 22:42:12 +01:00
|
|
|
$result = json_decode( $i );
|
2018-11-10 06:41:28 +01:00
|
|
|
|
2019-02-04 22:42:12 +01:00
|
|
|
if ( json_last_error() !== JSON_ERROR_NONE || ( ! $i == NULL && ! $i["status"] == "error" ) ) {
|
2018-11-10 06:41:28 +01:00
|
|
|
|
|
|
|
$return = true;
|
|
|
|
}
|
|
|
|
return( $return );
|
|
|
|
}
|
|
|
|
|
2019-02-04 22:42:12 +01:00
|
|
|
public static function get_instance() {
|
2018-10-09 21:30:00 +02:00
|
|
|
|
2019-02-04 22:42:12 +01:00
|
|
|
// If the single instance hasn't been set, set it now.
|
|
|
|
if ( null == self::$instance ) {
|
|
|
|
|
|
|
|
self::$instance = new self;
|
|
|
|
}
|
2018-10-09 21:30:00 +02:00
|
|
|
|
2019-02-04 22:42:12 +01:00
|
|
|
return self::$instance;
|
2018-10-09 21:30:00 +02:00
|
|
|
}
|
|
|
|
|
2019-04-08 02:58:53 +02:00
|
|
|
public function select( $table, $fields=array(), $where=array() ) {
|
|
|
|
|
|
|
|
$array = $this->conversions->select( $table, $fields, $where );
|
|
|
|
$query = $array[0];
|
|
|
|
$bind_vars = $array[1];
|
|
|
|
$result = $this->query( $query, $bind_vars, array() );
|
|
|
|
//echo var_dump( $query, $bind_vars ) . "<br>";
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function update( $table, $fields=array(), $where=array() ) {
|
|
|
|
|
|
|
|
$query = $this->conversions->update( $table, $fields, $where );
|
|
|
|
//echo var_dump( $query ) . "<br>";
|
|
|
|
}
|
|
|
|
|
2019-04-15 20:38:54 +02:00
|
|
|
public function query( $query, $bind_variables, $default, $action='fetchAll', $show_errors=false ) {
|
2018-10-09 21:30:00 +02:00
|
|
|
|
2019-02-04 22:42:12 +01:00
|
|
|
$connection = $this->connect();
|
|
|
|
$statement = $connection->prepare( $query );
|
|
|
|
$statement->execute( $bind_variables );
|
|
|
|
|
|
|
|
switch( $action ) {
|
|
|
|
|
|
|
|
case( 'rowCount' ):
|
|
|
|
|
|
|
|
$return = $statement->rowCount();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case( 'fetchAll' ):
|
|
|
|
|
|
|
|
$return = $statement->fetchAll( \PDO::FETCH_ASSOC );
|
|
|
|
break;
|
|
|
|
|
|
|
|
case( 'fetchColumn' ):
|
|
|
|
|
|
|
|
$return = $statement->fetchColumn();
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
|
|
|
$return = $statement->fetchAll( \PDO::FETCH_ASSOC );
|
|
|
|
break;
|
|
|
|
}
|
2018-10-09 21:30:00 +02:00
|
|
|
|
2019-02-04 22:42:12 +01:00
|
|
|
$error = $statement->errorInfo();
|
2019-04-11 01:31:28 +02:00
|
|
|
|
2019-02-04 22:42:12 +01:00
|
|
|
if( ! $error[0] == "00000" ) {
|
2018-10-09 21:30:00 +02:00
|
|
|
|
Changed $path to __DIR__ for config location, Updated auto reload variables, Removed unload listener for auto reload, Changed project default to array so that if no projects exist the program does not crash, Updated autosave to use let instead of vars, Fixed capitalization for sideExpanded variable, Added try catch to pdo initialization on install, Added more error checks on install, Removed password function on install query, Changed default settings array, Added loading div to user delete, Updated queries that threw errors when a default value was zero, Added blank username and password check,
2019-02-09 22:14:27 +01:00
|
|
|
echo var_export( $error );
|
|
|
|
echo var_export( $return );
|
2019-02-04 22:42:12 +01:00
|
|
|
$return = $default;
|
2018-10-09 21:30:00 +02:00
|
|
|
}
|
|
|
|
|
2019-04-15 20:38:54 +02:00
|
|
|
if( $show_errors ) {
|
|
|
|
|
|
|
|
$return = json_encode( $error );
|
|
|
|
}
|
|
|
|
|
2019-04-08 02:58:53 +02:00
|
|
|
//echo var_dump( $error, $return );
|
|
|
|
|
2019-02-04 22:42:12 +01:00
|
|
|
$this->close();
|
2018-10-09 21:30:00 +02:00
|
|
|
return( $return );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
?>
|