113 lines
1.4 KiB
PHP
113 lines
1.4 KiB
PHP
|
<?php
|
||
|
|
||
|
namespace rosavox\helpers\sqlite;
|
||
|
|
||
|
|
||
|
/**
|
||
|
*/
|
||
|
function placeholder(
|
||
|
string $name
|
||
|
) : string
|
||
|
{
|
||
|
return (':' . $name);
|
||
|
}
|
||
|
|
||
|
|
||
|
/**
|
||
|
*/
|
||
|
function map_type(string $type) : string
|
||
|
{
|
||
|
switch ($type)
|
||
|
{
|
||
|
case 'bool':
|
||
|
{
|
||
|
return 'INTEGER';
|
||
|
break;
|
||
|
}
|
||
|
case 'integer':
|
||
|
{
|
||
|
return 'INTEGER';
|
||
|
break;
|
||
|
}
|
||
|
case 'string':
|
||
|
{
|
||
|
return 'TEXT';
|
||
|
break;
|
||
|
}
|
||
|
default:
|
||
|
{
|
||
|
throw (new \Exception('unhandled type: ' . $type));
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
|
||
|
/**
|
||
|
*/
|
||
|
function format_value($value) : string
|
||
|
{
|
||
|
if ($value === null)
|
||
|
{
|
||
|
return 'NULL';
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
$type = \gettype($value);
|
||
|
switch ($type)
|
||
|
{
|
||
|
case 'bool':
|
||
|
{
|
||
|
return ($value ? '1' : '0');
|
||
|
break;
|
||
|
}
|
||
|
case 'int':
|
||
|
{
|
||
|
return \sprintf('%u', $value);
|
||
|
break;
|
||
|
}
|
||
|
case 'string':
|
||
|
{
|
||
|
return \sprintf("'%s'", $value);
|
||
|
break;
|
||
|
}
|
||
|
default:
|
||
|
{
|
||
|
throw (new \Exception('unhandled type: ' . $type));
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
|
||
|
/**
|
||
|
*/
|
||
|
function query(string $path, string $template, array $arguments) : array
|
||
|
{
|
||
|
/*
|
||
|
\error_log(
|
||
|
\json_encode(
|
||
|
[
|
||
|
'template' => $template,
|
||
|
'arguments' => $arguments,
|
||
|
]
|
||
|
)
|
||
|
);
|
||
|
*/
|
||
|
$connection = new \SQLite3($path);
|
||
|
$statement = $connection->prepare($template);
|
||
|
foreach ($arguments as $key => $value)
|
||
|
{
|
||
|
$statement->bindValue(placeholder($key), $value);
|
||
|
}
|
||
|
$result = $statement->execute();
|
||
|
$last_insert_id = $connection->lastInsertRowID();
|
||
|
return [
|
||
|
'result' => $result,
|
||
|
'last_insert_id' => $last_insert_id,
|
||
|
];
|
||
|
}
|
||
|
|
||
|
?>
|