This repository has been archived by the owner on May 16, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SQLiteTableStructureUpdater.php
357 lines (324 loc) · 12 KB
/
SQLiteTableStructureUpdater.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
<?php
// SQLite Table Structure Updater
namespace Attogram;
define('__STSU__','0.1.0');
//////////////////////////////////////////////////////////
class stsu_utils {
public $debug;
protected $timer;
public $timer_results;
public function debug( $msg ) {
if( !$this->debug ) { return; }
print '<p class="debug">' . print_r($msg,1) . '</p>';
}
public function notice( $msg ) {
print '<p class="notice">' . print_r($msg,1) . '</p>';
}
public function error( $msg ) {
print '<p class="error">' . print_r($msg,1) . '</p>';
}
function start_timer( $name ) {
$this->timer[$name] = microtime(1);
}
function end_timer( $name ) {
if( !isset($this->timer[$name]) ) {
$this->timer_results[$name] = 0;
return;
}
$result = microtime(1) - $this->timer[$name];
if( isset($this->timer_results[$name]) ) {
$this->timer_results[$name] += $result;
return;
}
$this->timer_results[$name] = $result;
}
}
//////////////////////////////////////////////////////////
class stsu_database EXTENDS stsu_utils {
protected $db;
protected $database_file;
protected $last_insert_id;
protected $last_error;
protected function open_database() {
$this->debug('open_database: ' . $this->database_file);
if( !in_array('sqlite', \PDO::getAvailableDrivers() ) ) {
$this->error('open_database: ERROR: no sqlite Driver');
return $this->db = FALSE;
}
try {
return $this->db = new \PDO('sqlite:'. $this->database_file);
} catch(\PDOException $e) {
$this->error('open_database: ' . $this->database_file . ' ERROR: '. $e->getMessage());
return $this->db = FALSE;
}
}
protected function query_as_array( $sql, $bind=array() ) {
$this->debug( $this->normalize_sql($sql) );
if( !$this->database_loaded() ) {
return array();
}
$this->start_timer('query_as_array');
$statement = $this->db->prepare($sql);
if( !$statement ) {
$this->error('query_as_array(): ERROR PREPARE');
$this->end_timer('query_as_array');
return array();
}
while( $xbind = each($bind) ) {
$this->debug('query_as_array(): bindParam '. $xbind[0] .' = ' . $xbind[1]);
$statement->bindParam( $xbind[0], $xbind[1]);
}
if( !$statement->execute() ) {
$this->error('ERROR EXECUTE: '.print_r($this->db->errorInfo(),1));
$this->end_timer('query_as_array');
return array();
}
$response = $statement->fetchAll(\PDO::FETCH_ASSOC);
if( !$response && $this->db->errorCode() != '00000') {
$this->error('query_as_array(): ERROR FETCH: '.print_r($this->db->errorInfo(),1));
$response = array();
}
$this->end_timer('query_as_array');
return $response;
}
protected function query_as_bool( $sql, $bind=array() ) {
$this->debug( $this->normalize_sql($sql) );
if( !$this->database_loaded() ) {
return FALSE;
}
$this->start_timer('query_as_bool');
$this->last_error = FALSE;
$statement = $this->db->prepare($sql);
if( !$statement ) {
$this->last_error = $this->db->errorInfo();
$this->error('ERROR: ' . print_r($this->last_error,1) );
$this->end_timer('query_as_bool');
return FALSE;
}
while( $xbind = each($bind) ) {
$statement->bindParam( $xbind[0], $xbind[1] );
}
if( !$statement->execute() ) {
$this->last_error = $this->db->errorInfo();
if( $this->last_error[0] == '00000' ) { // no error
$this->end_timer('query_as_bool');
return TRUE;
}
$this->error('query_as_bool: prepare failed: ' . print_r($this->last_error,1) );
$this->end_timer('query_as_bool');
return FALSE;
}
$this->last_error = $this->db->errorInfo();
$this->end_timer('query_as_bool');
return TRUE;
}
protected function vacuum() {
$this->start_timer('vacuum');
if( $this->query_as_bool('VACUUM') ) {
$this->end_timer('vacuum');
return TRUE;
}
$this->end_timer('vacuum');
$this->error('FAILED to VACUUM');
return FALSE;
}
protected function begin_transaction() {
if( $this->query_as_bool('BEGIN TRANSACTION') ) {
return TRUE;
}
$this->error('FAILED to BEGIN TRANSACTION');
return FALSE;
}
protected function commit() {
if( $this->query_as_bool('COMMIT') ) {
return TRUE;
}
$this->error('FAILED to COMMIT');
return FALSE;
}
}
//////////////////////////////////////////////////////////
class SQLiteTableStructureUpdater extends stsu_database {
protected $tables_current;
protected $sql_current;
protected $sql_new;
public function __construct() {
$this->start_timer('page');
$this->debug('__construct()');
}
public function set_database_file( $file ) {
$this->debug("set_database_file($file)");
$this->database_file = $file;
$this->tables_current = array();
$this->sql_current = array();
$this->set_table_info();
return $this->database_loaded();
}
public function set_new_structures( $tables = array() ) {
$this->debug('set_new_structures()');
if( !$tables || !is_array($tables) ) {
$this->error('$tables array is invalid');
return FALSE;
}
$errors = 0;
$count = 0;
while( list($table_name,$table_sql) = each($tables) ) {
$count++;
if( !$table_name || !is_string($table_name) ) {
$this->error("#$count - Invalid table name");
$errors++;
continue;
}
if( !$table_sql || !is_string($table_sql) ) {
$this->error("#$count - Invalid table sql");
$errors++;
continue;
}
$this->set_new_structure( $table_name, $table_sql );
}
return $errors ? FALSE : TRUE;
}
public function set_new_structure( $table_name, $sql ) {
$this->debug("set_new_structure($table_name, $sql)");
$sql = $this->normalize_sql($sql);
$this->sql_new[$table_name] = $sql;
}
public function update() {
$this->debug("update()");
$this->start_timer('update');
$to_update = array();
foreach( array_keys($this->sql_new) as $name ) {
$old = $this->normalize_sql( @$this->sql_current[$name] );
$new = $this->normalize_sql( @$this->sql_new[$name] );
$this->debug("$name: OLD: $old");
$this->debug("$name: NEW: $new");
if( $old == $new ) {
continue;
}
$this->debug('Needs updating: ' . $name);
$to_update[] = $name;
}
if( !$to_update ) {
$this->notice(
'OK: ' . sizeof($this->sql_new) . ' tables up-to-date'
);
$this->end_timer('update');
return TRUE;
}
$this->notice(
sizeof($to_update) . ' tables to update: '
. implode($to_update,', ')
);
foreach( $to_update as $table_name ) {
$this->update_table($table_name);
}
$this->end_timer('update');
return TRUE;
}
public function database_loaded() {
if( !$this->db ) {
$this->open_database();
}
if( !$this->db ) {
return FALSE;
}
return TRUE;
}
protected function update_table( $table_name ) {
$this->debug("update_table($table_name)");
$tmp_name = '_STSU_TMP_' . $table_name;
$backup_name = '_STSU_BACKUP_' . $table_name;
$this->query_as_bool("DROP TABLE IF EXISTS '$tmp_name'");
$this->query_as_bool("DROP TABLE IF EXISTS '$backup_name'");
$this->begin_transaction();
$sql = $this->sql_new[$table_name];
$sql = str_ireplace(
"CREATE TABLE '$table_name'",
"CREATE TABLE '$tmp_name'",
$sql
);
if( !$this->query_as_bool($sql) ) {
$this->error('ERROR: can not create tmp table:<br />' . $sql );
return FALSE;
}
// Get Columns of new table
$this->set_table_column_info($tmp_name);
$new_cols = $this->tables_current[$tmp_name];
// Only use Columns both in new and old tables
$cols = array();
foreach( $new_cols as $new_col ) {
if( isset( $this->tables_current[$table_name][$new_col['name']] ) ) {
$cols[] = $new_col['name'];
}
}
if( !$cols ) {
$this->debug('Nothing to insert into table: ' . $table_name);
$new_size = 0;
} else {
$old_size = $this->get_table_size($table_name);
$cols = implode( $cols, ', ');
$sql = "INSERT INTO '$tmp_name' ( $cols ) SELECT $cols FROM $table_name";
if( !$this->query_as_bool($sql) ) {
$this->error('ERROR: can not insert into tmp table: ' . $tmp_name
. '<br />' . $sql);
return FALSE;
}
$new_size = $this->get_table_size($tmp_name);
if( $new_size == $old_size ) {
$this->debug("Inserted OK: $new_size rows into $tmp_name");
} else {
$this->error("ERROR: Inserted new $new_size rows, from $old_size old rows");
}
if( !$this->query_as_bool("ALTER TABLE $table_name RENAME TO $backup_name") ) {
$this->error('ERROR: can not rename '.$table_name.' to '.$backup_name );
return FALSE;
}
}
if( !$this->query_as_bool("ALTER TABLE $tmp_name RENAME TO $table_name") ) {
$this->error('ERROR: can not rename '.$tmp_name.' to '.$backup_name );
return FALSE;
}
$this->commit();
$this->notice('OK: Table Structure Updated: ' . $table_name
. ': +' . number_format($new_size) . ' rows');
$this->query_as_bool("DROP TABLE IF EXISTS '$tmp_name'");
$this->query_as_bool("DROP TABLE IF EXISTS '$backup_name'");
$this->vacuum();
}
protected function set_table_info() {
$this->debug('set_table_info()');
$tables = $this->query_as_array("
SELECT name, sql
FROM sqlite_master
WHERE type = 'table'");
foreach($tables as $table) {
if( preg_match('/^_STSU_/', $table['name']) ) {
continue; // tmp and backup tables
}
$this->sql_current[$table['name']]
= $this->normalize_sql($table['sql']);
$this->set_table_column_info($table['name']);
}
}
protected function set_table_column_info( $table_name ) {
$this->debug("set_table_column_info($table_name)");
$columns = $this->query_as_array("PRAGMA table_info( $table_name )");
foreach($columns as $column) {
$this->tables_current[$table_name][$column['name']] = $column;
}
}
protected function normalize_sql( $sql ) {
$sql = preg_replace('/\s+/', ' ', $sql); // remove all excessive spaces and control chars
$sql = str_replace('"', "'", $sql); // use only single quote '
$sql = str_ireplace('CREATE TABLE IF NOT EXISTS', 'CREATE TABLE', $sql); // standard create syntax
return trim($sql);
}
protected function get_table_size( $table_name ) {
$size = $this->query_as_array('SELECT count(rowid) AS count FROM ' . $table_name);
if( isset($size[0]['count']) ) {
return $size[0]['count'];
}
$this->error('Can not get table size: ' . $table_name);
return 0;
}
}