189 lines
3.3 KiB
PHP
189 lines
3.3 KiB
PHP
|
<?php
|
||
|
|
||
|
namespace alveolata\database;
|
||
|
|
||
|
// require_once(DIR_ALVEOLATA . '/definitions.php');
|
||
|
require_once(DIR_ALVEOLATA . '/report/functions.php');
|
||
|
require_once(DIR_ALVEOLATA . '/log/functions.php');
|
||
|
require_once(DIR_ALVEOLATA . '/sql/functions.php');
|
||
|
|
||
|
|
||
|
/**
|
||
|
* @author Christian Fraß <frass@greenscale.de>
|
||
|
*/
|
||
|
class struct_subject_mysql {
|
||
|
|
||
|
/**
|
||
|
* @var string
|
||
|
* @author Christian Fraß <frass@greenscale.de>
|
||
|
*/
|
||
|
public $host;
|
||
|
|
||
|
|
||
|
/**
|
||
|
* @var int
|
||
|
* @author Christian Fraß <frass@greenscale.de>
|
||
|
*/
|
||
|
public $port;
|
||
|
|
||
|
|
||
|
/**
|
||
|
* @var string
|
||
|
* @author Christian Fraß <frass@greenscale.de>
|
||
|
*/
|
||
|
public $schema;
|
||
|
|
||
|
|
||
|
/**
|
||
|
* @var string
|
||
|
* @author Christian Fraß <frass@greenscale.de>
|
||
|
*/
|
||
|
public $username;
|
||
|
|
||
|
|
||
|
/**
|
||
|
* @var string
|
||
|
* @author Christian Fraß <frass@greenscale.de>
|
||
|
*/
|
||
|
public $password;
|
||
|
|
||
|
|
||
|
|
||
|
/**
|
||
|
* @author Christian Fraß <frass@greenscale.de>
|
||
|
*/
|
||
|
public function __construct(
|
||
|
string $host,
|
||
|
int $port,
|
||
|
string $schema,
|
||
|
string $username,
|
||
|
string $password
|
||
|
)
|
||
|
{
|
||
|
$this->host = $host;
|
||
|
$this->port = $port;
|
||
|
$this->schema = $schema;
|
||
|
$this->username = $username;
|
||
|
$this->password = $password;
|
||
|
}
|
||
|
|
||
|
}
|
||
|
|
||
|
|
||
|
/**
|
||
|
* @author Christian Fraß <frass@greenscale.de>
|
||
|
*/
|
||
|
function mysql_make(
|
||
|
string $host,
|
||
|
int $port,
|
||
|
string $schema,
|
||
|
string $username,
|
||
|
string $password
|
||
|
) : struct_subject_mysql
|
||
|
{
|
||
|
return (
|
||
|
new struct_subject_mysql(
|
||
|
$host,
|
||
|
$port,
|
||
|
$schema,
|
||
|
$username,
|
||
|
$password
|
||
|
)
|
||
|
);
|
||
|
}
|
||
|
|
||
|
|
||
|
/**
|
||
|
* @return string
|
||
|
* @author Christian Fraß <frass@greenscale.de>
|
||
|
*/
|
||
|
function mysql_terminal_autoincrement(
|
||
|
) : string
|
||
|
{
|
||
|
return 'AUTO_INCREMENT';
|
||
|
}
|
||
|
|
||
|
|
||
|
/**
|
||
|
* @return string
|
||
|
* @author Christian Fraß <frass@greenscale.de>
|
||
|
*/
|
||
|
function mysql_boilerplate_field_definition_for_integer_primary_key_with_auto_increment(
|
||
|
) : string
|
||
|
{
|
||
|
return 'INTEGER PRIMARY KEY AUTO_INCREMENT';
|
||
|
}
|
||
|
|
||
|
|
||
|
/**
|
||
|
* @param struct_subject_mysql $subject
|
||
|
* @param string $template
|
||
|
* @param array $arguments
|
||
|
* @return array
|
||
|
* @author Christian Fraß <frass@greenscale.de>
|
||
|
*/
|
||
|
function mysql_query(
|
||
|
struct_subject_mysql $subject,
|
||
|
string $template,
|
||
|
array $arguments
|
||
|
) : array
|
||
|
{
|
||
|
$connection = \mysqli_connect(
|
||
|
$subject->host,
|
||
|
$subject->username,
|
||
|
$subject->password,
|
||
|
$subject->schema,
|
||
|
$subject->port
|
||
|
);
|
||
|
\mysqli_options($connection, MYSQLI_OPT_INT_AND_FLOAT_NATIVE, true);
|
||
|
\mysqli_set_charset($connection, 'utf8');
|
||
|
\mysqli_query($connection, 'set names \'utf8\'');
|
||
|
$query = $template;
|
||
|
foreach ($arguments as $key => $value) {
|
||
|
$pattern = sprintf(':%s', $key);
|
||
|
$replacement = \alveolata\sql\format(
|
||
|
$value,
|
||
|
function ($x) use ($connection) {
|
||
|
return \mysqli_real_escape_string($connection, $x);
|
||
|
}
|
||
|
);
|
||
|
$query = str_replace($pattern, $replacement, $query);
|
||
|
}
|
||
|
$report = \alveolata\report\make(
|
||
|
'query',
|
||
|
[
|
||
|
'query' => $query,
|
||
|
]
|
||
|
);
|
||
|
\alveolata\log\debug_($report);
|
||
|
$result = \mysqli_query($connection, $query);
|
||
|
if ($result === false) {
|
||
|
$report = \alveolata\report\make(
|
||
|
'query failed',
|
||
|
[
|
||
|
'query' => $query,
|
||
|
'reason' => \mysqli_error($connection),
|
||
|
]
|
||
|
);
|
||
|
throw (\alveolata\report\as_exception($report));
|
||
|
}
|
||
|
else if ($result === true) {
|
||
|
$output = [
|
||
|
'rows' => [],
|
||
|
'id' => mysqli_insert_id($connection),
|
||
|
'affected' => \mysqli_affected_rows($connection),
|
||
|
];
|
||
|
return $output;
|
||
|
}
|
||
|
else {
|
||
|
$output = [
|
||
|
'rows' => $result->fetch_all(MYSQLI_ASSOC),
|
||
|
'id' => 0,
|
||
|
'affected' => 0,
|
||
|
];
|
||
|
return $output;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
?>
|