*/ class struct_subject_sqlite { /** * @var string * @author Christian Fraß */ public $path; /** * @var int * @author Christian Fraß */ public $verbosity; /** * @author Christian Fraß */ public function __construct( string $path, int $verbosity ) { $this->path = $path; $this->verbosity = $verbosity; } } /** * @author Christian Fraß */ function sqlite_make( string $path, int $verbosity = 0 ) { return ( new struct_subject_sqlite( $path, $verbosity ) ); } /** * @return string * @author Christian Fraß */ function sqlite_terminal_autoincrement( ) : string { return 'AUTOINCREMENT'; } /** * @return string * @author Christian Fraß */ 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ß */ 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; } } ?>