145 lines
2.6 KiB
PHP
145 lines
2.6 KiB
PHP
|
<?php
|
||
|
|
||
|
namespace alveolata\database;
|
||
|
|
||
|
// require_once(DIR_ALVEOLATA . '/definitions.php');
|
||
|
require_once(DIR_ALVEOLATA . '/report/functions.php');
|
||
|
require_once(DIR_ALVEOLATA . '/string/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_sqlite {
|
||
|
|
||
|
/**
|
||
|
* @var string
|
||
|
* @author Christian Fraß <frass@greenscale.de>
|
||
|
*/
|
||
|
public $path;
|
||
|
|
||
|
|
||
|
/**
|
||
|
* @var int
|
||
|
* @author Christian Fraß <frass@greenscale.de>
|
||
|
*/
|
||
|
public $verbosity;
|
||
|
|
||
|
|
||
|
/**
|
||
|
* @author Christian Fraß <frass@greenscale.de>
|
||
|
*/
|
||
|
public function __construct(
|
||
|
string $path,
|
||
|
int $verbosity
|
||
|
)
|
||
|
{
|
||
|
$this->path = $path;
|
||
|
$this->verbosity = $verbosity;
|
||
|
}
|
||
|
|
||
|
}
|
||
|
|
||
|
|
||
|
|
||
|
/**
|
||
|
* @author Christian Fraß <frass@greenscale.de>
|
||
|
*/
|
||
|
function sqlite_make(
|
||
|
string $path,
|
||
|
int $verbosity = 0
|
||
|
)
|
||
|
{
|
||
|
return (
|
||
|
new struct_subject_sqlite(
|
||
|
$path,
|
||
|
$verbosity
|
||
|
)
|
||
|
);
|
||
|
}
|
||
|
|
||
|
|
||
|
/**
|
||
|
* @return string
|
||
|
* @author Christian Fraß <frass@greenscale.de>
|
||
|
*/
|
||
|
function sqlite_terminal_autoincrement(
|
||
|
) : string
|
||
|
{
|
||
|
return 'AUTOINCREMENT';
|
||
|
}
|
||
|
|
||
|
|
||
|
/**
|
||
|
* @return string
|
||
|
* @author Christian Fraß <frass@greenscale.de>
|
||
|
*/
|
||
|
function sqlite_boilerplate_field_definition_for_integer_primary_key_with_auto_increment(
|
||
|
) : string
|
||
|
{
|
||
|
return 'INTEGER PRIMARY KEY AUTOINCREMENT';
|
||
|
}
|
||
|
|
||
|
|
||
|
/**
|
||
|
* @param struct_subject_sqlite $subject
|
||
|
* @param string $template
|
||
|
* @param array $arguments
|
||
|
* @return array
|
||
|
* @author Christian Fraß <frass@greenscale.de>
|
||
|
*/
|
||
|
function sqlite_query(
|
||
|
struct_subject_sqlite $subject,
|
||
|
string $template,
|
||
|
array $arguments
|
||
|
) : array
|
||
|
{
|
||
|
$sqlite3 = new \SQLite3($subject->path);
|
||
|
$query = $template;
|
||
|
foreach ($arguments as $key => $value) {
|
||
|
$pattern = sprintf(':%s', $key);
|
||
|
$replacement = \alveolata\sql\format($value, function ($x) {return \SQLite3::escapeString($x);});
|
||
|
$query = str_replace($pattern, $replacement, $query);
|
||
|
}
|
||
|
$report = \alveolata\report\make(
|
||
|
'query',
|
||
|
[
|
||
|
'query' => \alveolata\string\replace(
|
||
|
$query,
|
||
|
[
|
||
|
"\n" => ' ',
|
||
|
"\t" => ' ',
|
||
|
]
|
||
|
),
|
||
|
]
|
||
|
);
|
||
|
\alveolata\log\debug_($report);
|
||
|
$result = $sqlite3->query($query);
|
||
|
if ($result === false) {
|
||
|
$report = \alveolata\report\make(
|
||
|
'query failed',
|
||
|
[
|
||
|
'query' => $query,
|
||
|
]
|
||
|
);
|
||
|
throw (\alveolata\report\as_exception($report));
|
||
|
}
|
||
|
else {
|
||
|
$output = [
|
||
|
'rows' => [],
|
||
|
'id' => $sqlite3->lastInsertRowId(),
|
||
|
'affected' => $sqlite3->changes(),
|
||
|
];
|
||
|
if ($result->numColumns() > 0) {
|
||
|
while ($row = $result->fetchArray(SQLITE3_ASSOC)) {
|
||
|
array_push($output['rows'], $row);
|
||
|
}
|
||
|
}
|
||
|
return $output;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
?>
|