11<?php
2+ /**
3+ * Attogram Database
4+ * @see https://github.com/attogram/database
5+ */
26declare (strict_types = 1 );
37
48namespace Attogram \Database ;
1519class Database
1620{
1721 /** @var string */
18- const VERSION = '1.0.0-pre.4 ' ;
22+ const VERSION = '1.1.0 ' ;
1923
2024 /** @var bool */
2125 private $ connected = false ;
2226
23- /** @var string */
24- private $ createTables ;
27+ /** @var array */
28+ private $ createTables = [] ;
2529
2630 /** @var string */
27- private $ databaseFile ;
31+ private $ databaseFile = ' database.sqlite ' ;
2832
2933 /** @var PDO */
3034 private $ pdo ;
@@ -38,11 +42,20 @@ public function setDatabaseFile(string $databaseFile)
3842 }
3943
4044 /**
41- * @param string $createTables
45+ * @param string|array $createTables
4246 */
43- public function setCreateTables (string $ createTables )
47+ public function setCreateTables ($ createTables )
4448 {
45- $ this ->createTables = $ createTables ;
49+ if (empty ($ createTables )) {
50+ return ;
51+ }
52+ if (is_string ($ createTables )) {
53+ $ this ->createTables [] = $ createTables ;
54+ return ;
55+ }
56+ if (is_array ($ createTables )) {
57+ $ this ->createTables = array_merge ($ this ->createTables , $ createTables );
58+ }
4659 }
4760
4861 /**
@@ -70,19 +83,29 @@ public function connect()
7083 $ this ->pdo = new PDO ('sqlite: ' . $ this ->databaseFile );
7184 $ this ->connected = true ;
7285 if ($ doCreateTables && !empty ($ this ->createTables )) {
73- $ this ->raw ( $ this -> createTables );
86+ $ this ->createTables ( );
7487 }
7588 }
7689
7790 /**
78- * SQL query, returns results in an array
91+ * @throws Exception
92+ */
93+ private function createTables ()
94+ {
95+ foreach ($ this ->createTables as $ table ) {
96+ $ this ->raw ($ table );
97+ }
98+ }
99+
100+ /**
101+ * SQL query, return results as an array
79102 *
80103 * @param string $sql
81104 * @param array $bind
82105 * @return array
83106 * @throws Exception
84107 */
85- public function query (string $ sql , array $ bind = []) : array
108+ public function query (string $ sql , array $ bind = []): array
86109 {
87110 if (!$ this ->connected ) {
88111 $ this ->connect ();
@@ -107,9 +130,10 @@ public function query(string $sql, array $bind = []) :array
107130 *
108131 * @param string $sql
109132 * @param array $bind
133+ * @return bool
110134 * @throws Exception
111135 */
112- public function raw (string $ sql , array $ bind = [])
136+ public function raw (string $ sql , array $ bind = []): bool
113137 {
114138 if (!$ this ->connected ) {
115139 $ this ->connect ();
@@ -121,7 +145,11 @@ public function raw(string $sql, array $bind = [])
121145 $ result = $ statement ->execute ($ bind );
122146 if (!$ result && ($ this ->pdo ->errorCode () != '00000 ' )) {
123147 $ this ->pdoFail ('raw: execute statement failed ' );
148+
149+ return false ;
124150 }
151+
152+ return true ;
125153 }
126154
127155 /**
0 commit comments