2024.8.23
Loading...
Searching...
No Matches
db_sqlite.inc.php
Go to the documentation of this file.
1<?php
2
3/**
4* WINBINDER - The native Windows binding for PHP for PHP
5*
6* Copyright � Hypervisual - see LICENSE.TXT for details
7* Authors: Rubem Pechansky and Hans Rebel
8*
9* Database wrapper functions for WinBinder (SQLite-specific)
10* version 2b
11*/
12
13if(PHP_VERSION < "5")
14 if(!extension_loaded('sqlite'))
15 if(!@dl('php_sqlite.dll')) {
16 wb_message_box(null, "SQLite extension could not be loaded.", "Error", WBC_STOP);
17 trigger_error("SQLite extension could not be loaded.\n", E_USER_ERROR);
18 }
19
20
21// -------------------------------------------------------------------- CONSTANTS
22define("FETCH_BOTH", SQLITE_BOTH);
23define("FETCH_NUM", SQLITE_NUM);
24define("FETCH_ASSOC", SQLITE_ASSOC);
25// -------------------------------------------------------------------- CONSTANTS
26define("DB_SQLITE_WRAP", "db_v2b");
27// ----------------------------------------------------------- DATABASE VERSION CHECK
28
29/**
30* raw_get_db_version()
31* Returns the version of the database library.
32*
33* @return string
34*/
36{
37 return sqlite_libversion();
38}
39
41 die(" db_common.inc.php has different version number than db_mysql.inc.php ");
42}
43// ----------------------------------------------------------- DATABASE FUNCTIONS
44/**
45* raw_db_open_database()
46* Opens and connects a database. Create the database if it does not exist.
47*
48* @param $database
49* @param string $server
50* @param string $username
51* @param string $password
52* @return resource or FALSE
53*/
54function raw_db_open_database($database, $path = "", $u = null, $p = null)
55{
56 global $g_current_db;
57
58 if (!$path) {
59 $path = pathinfo(__FILE__);
60 $path = $path["dirname"] . "/";
61 }
62
63 if (!file_exists($database))
64
65 $database = $path . "sqlite_" . $database . ".db";
66
67 $g_current_db = sqlite_open($database, 0666, $sql_error);
68 if (!$g_current_db) {
69 trigger_error(__FUNCTION__ . $sql_error);
70 return false;
71 } else return $g_current_db;
72}
73
74/**
75* raw_db_list_database_tables()
76* Returns an array with the list of tables of the current database.
77*
78* @return array or FALSE
79*/
81{
82 global $g_current_db;
83
84 $tables = array();
85 $sql = "SELECT name FROM sqlite_master WHERE (type = 'table')";
86 $res = sqlite_query($g_current_db, $sql);
87 if ($res) {
88 while (sqlite_has_more($res)) {
89 $tables[] = sqlite_fetch_single($res);
90 }
91 } else return false;
92 return $tables;
93}
94
95/**
96* raw_db_close_database()
97*
98* @return bool
99*/
101{
102 global $g_current_db;
103
104 sqlite_close($g_current_db);
105 return true;
106}
107// -------------------------------------------------------------- TABLE FUNCTIONS
108/**
109* raw_db_table_exists()
110*
111* @param $tablename
112* @return bool
113*/
114function raw_db_table_exists($tablename)
115{
116 global $g_current_db;
117
118 $sql = "SELECT name FROM sqlite_master WHERE (type = 'table' AND name ='$tablename')";
119 $res = sqlite_query($g_current_db, $sql);
120 $count = intval(sqlite_fetch_array($res));
121 return $count > 0;
122}
123
124/**
125* raw_db_rename_table()
126*
127* @param $tablename
128* @param $newname
129* @return bool
130*/
131function raw_db_rename_table($tablename, $newname)
132{
133 global $g_lasttable;
134
135 if (!$tablename)
136 $tablename = $g_lasttable;
137 $g_lasttable = $newname;
138 return __alter_table($tablename, "rename $tablename $newname");
139}
140
141/**
142* raw_db_list_table_fields_def()
143* lists fieldnames or fieldattributes according type
144*
145* @param $tablename
146* @param boolean $type
147* @return array or FALSE
148*/
149function raw_db_list_table_fields_def($tablename, $type = false)
150{
151 $result = db_query("SELECT sql FROM sqlite_master WHERE (tbl_name = '" . $tablename . "');");
152 if ($result === false) return false;
153
154 $all = db_fetch_array($result);
155 $origsql = trim(preg_replace("/[\s]+/", " ", str_replace(",", ", ", preg_replace("/[\‍(]/", "( ", $all[0], 1))));
156 $origsql = substr($origsql, 0, strlen($origsql)-1);
157 $oldcols = preg_split("/[,]+/", substr(trim($origsql), strpos(trim($origsql), '(') + 1), -1, PREG_SPLIT_NO_EMPTY);
158
159 $colnames = array();
160 $coltype = array();
161 for($i = 0;$i < sizeof($oldcols);$i++) {
162 $colparts = preg_split("/[\s]+/", $oldcols[$i], -1, PREG_SPLIT_NO_EMPTY);
163 $colnames[] = $colparts[0];
164 $coltype[] = implode(" ", array_slice($colparts, 1));
165 }
166
167 return ($type ? $coltype : $colnames);
168}
169// -------------------------------------------------------------- FIELD FUNCTIONS
170/**
171* raw_db_create_field()
172*
173* @param $tablename
174* @param $field
175* @param $type
176* @return bool
177*/
178function raw_db_create_field($tablename, $field, $type)
179{
180 global $g_lasttable;
181
182 if (!$tablename)
183 $tablename = $g_lasttable;
184 $g_lasttable = $tablename;
185
186 return __alter_table($tablename, "ADD $field $type");
187}
188
189/**
190* raw_db_delete_field()
191*
192* @param $tablename
193* @param $field
194* @return bool
195*/
196function raw_db_delete_field($tablename, $field)
197{
198 global $g_lasttable;
199
200 if (!$tablename)
201 $tablename = $g_lasttable;
202 $g_lasttable = $tablename;
203
204 return __alter_table($tablename, "DROP $field");
205}
206
207/**
208* raw_db_rename_field()
209*
210* @param $tablename
211* @param $field
212* @param $newname
213* @param $type
214* @return bool
215*/
216function raw_db_rename_field($tablename, $field, $newname, $type)
217{
218 global $g_lasttable;
219
220 if (!$tablename)
221 $tablename = $g_lasttable;
222 $g_lasttable = $tablename;
223
224 return __alter_table($tablename, "CHANGE $field $newname $type");
225}
226
227/**
228* raw_db_edit_field()
229*
230* @param $tablename
231* @param $field
232* @param $type
233* @return bool
234*/
235function raw_db_edit_field($tablename, $field, $type)
236{
237 global $g_lasttable;
238
239 if (!$tablename)
240 $tablename = $g_lasttable;
241 $g_lasttable = $tablename;
242
243 return __alter_table($tablename, "CHANGE $field $field $type");
244}
245// ---------------------------------------------------------------- SQL FUNCTIONS
246/**
247* raw_db_query()
248* queries the database with SQL
249*
250* @param string $query
251* @return resource on success for SELECT,SHOW,DESCRIBE ans EXPLAIN
252* TRUE on success for UPDATE, DELETE, DROP etc
253* FALSE on errors
254*/
255function raw_db_query($query)
256{
257 global $g_current_db;
258
259 return sqlite_query($g_current_db, $query);
260}
261
262/**
263* raw_db_fetch_array()
264* get the value of SQL-query, row by row
265*
266* @param $result
267* @param unknown $type
268* @return array of row, FALSE if no more rows
269*/
270function raw_db_fetch_array($result, $type = SQLITE_BOTH)
271{
272 return sqlite_fetch_array($result, $type);
273}
274
275/**
276* raw_db_free_result()
277*
278* @param $result
279* @return bool
280*/
282{
283 // Not required in SQLite
284 return true;
285}
286
287/**
288* raw_db_escape_string()
289*
290* @param $str
291* @return escaped string
292*/
294{
295 return sqlite_escape_string($str);
296}
297// ------------------------------------------------------------ PRIVATE FUNCTIONS
298/**
299* __alter_table()
300* This function implements a subset of commands from ALTER TABLE
301* Adapted from http://code.jenseng.com/db/
302*
303* @param $table
304* @param $alterdefs string for ALTER TABLE
305* @return bool
306*/
307function __alter_table($table, $alterdefs)
308{
309 global $g_current_db;
310
311 $sql = "SELECT sql,name,type FROM sqlite_master WHERE tbl_name = '" . $table . "' ORDER BY type DESC";
312 $result = sqlite_query($g_current_db, $sql);
313
314 if (($result === false) || (sqlite_num_rows($result) <= 0)) {
315 trigger_error('no such table: ' . $table, E_USER_WARNING);
316 return false;
317 }
318 // ------------------------------------- Build the queries
319 $row = sqlite_fetch_array($result);
320 $tmpname = 't' . time();
321 $origsql = trim(preg_replace("/[\s]+/", " ", str_replace(",", ", ", preg_replace("/[\‍(]/", "( ", $row['sql'], 1))));
322 $createtemptableSQL = 'CREATE TEMPORARY ' . substr(trim(preg_replace("'" . $table . "'", $tmpname, $origsql, 1)), 6);
323 $origsql = substr($origsql, 0, strlen($origsql)-1); // chops the ) at end
324 $createindexsql = array();
325 $i = 0;
326 $defs = preg_split("/[,]+/", $alterdefs, -1, PREG_SPLIT_NO_EMPTY);
327 $prevword = $table;
328 $oldcols = preg_split("/[,]+/", substr(trim($createtemptableSQL), strpos(trim($createtemptableSQL), '(') + 1), -1, PREG_SPLIT_NO_EMPTY);
329 $oldcols = preg_split("/[,]+/", substr(trim($origsql), strpos(trim($origsql), '(') + 1), -1, PREG_SPLIT_NO_EMPTY);
330 $newcols = array();
331
332 for($i = 0;$i < sizeof($oldcols);$i++) {
333 $colparts = preg_split("/[\s]+/", $oldcols[$i], -1, PREG_SPLIT_NO_EMPTY);
334 $oldcols[$i] = $colparts[0];
335 $newcols[$colparts[0]] = $colparts[0];
336 }
337
338 $newcolumns = '';
339 $oldcolumns = '';
340 reset($newcols);
341
342 while (list($key, $val) = each($newcols)) {
343 $newcolumns .= ($newcolumns?', ':'') . $val;
344 $oldcolumns .= ($oldcolumns?', ':'') . $key;
345 }
346
347 $copytotempsql = 'INSERT INTO ' . $tmpname . '(' . $newcolumns . ') SELECT ' . $oldcolumns . ' FROM ' . $table;
348 $dropoldsql = 'DROP TABLE ' . $table;
349 $createtesttableSQL = $createtemptableSQL;
350
351 $newname = "";
352
353 foreach($defs as $def) {
354 $defparts = preg_split("/[\s]+/", $def, -1, PREG_SPLIT_NO_EMPTY);
355 $action = strtolower($defparts[0]);
356
357 switch ($action) {
358 case 'add':
359
360 if (sizeof($defparts) <= 2) {
361 /**
362 * * mySQL gives no such user_warning
363 * trigger_error('near "' . $defparts[0] . ($defparts[1]?' ' . $defparts[1]:'') . '": SQLITE syntax error', E_USER_WARNING);
364 *
365 * //
366 */
367 return false;
368 }
369 $createtesttableSQL = substr($createtesttableSQL, 0, strlen($createtesttableSQL)-1) . ',';
370 for($i = 1;$i < sizeof($defparts);$i++)
371 $createtesttableSQL .= ' ' . $defparts[$i];
372 $createtesttableSQL .= ')';
373 break;
374
375 case 'change':
376
377 if (sizeof($defparts) <= 2) {
378 trigger_error('near "' . $defparts[0] . ($defparts[1]?' ' . $defparts[1]:'') . ($defparts[2]?' ' . $defparts[2]:'') . '": SQLITE syntax error', E_USER_WARNING);
379 return false;
380 }
381 if ($severpos = strpos($createtesttableSQL, ' ' . $defparts[1] . ' ')) {
382 if ($newcols[$defparts[1]] != $defparts[1]) {
383 trigger_error('unknown column "' . $defparts[1] . '" in "' . $table . '"', E_USER_WARNING);
384 return false;
385 }
386 $newcols[$defparts[1]] = $defparts[2];
387 $nextcommapos = strpos($createtesttableSQL, ',', $severpos);
388 $insertval = '';
389 for($i = 2;$i < sizeof($defparts);$i++)
390 $insertval .= ' ' . $defparts[$i];
391 if ($nextcommapos)
392 $createtesttableSQL = substr($createtesttableSQL, 0, $severpos) . $insertval . substr($createtesttableSQL, $nextcommapos);
393 else
394 $createtesttableSQL = substr($createtesttableSQL, 0, $severpos - (strpos($createtesttableSQL, ',')?0:1)) . $insertval . ')';
395 } else {
396 trigger_error('unknown column "' . $defparts[1] . '" in "' . $table . '"', E_USER_WARNING);
397 return false;
398 }
399 break;
400
401 case 'drop';
402
403 if (sizeof($defparts) < 2) {
404 trigger_error('near "' . $defparts[0] . ($defparts[1]?' ' . $defparts[1]:'') . '": SQLITE syntax error', E_USER_WARNING);
405 return false;
406 }
407 /**
408 * if ($severpos = strpos($createtesttableSQL, ' ' . $defparts[1] . ' ')) {
409 * could end with , or ) if no type!!!!
410 *
411 * //
412 */
413 if (($severpos = strpos($createtesttableSQL, ' ' . $defparts[1] . ' ')) || ($severpos = strpos($createtesttableSQL, ' ' . $defparts[1] . ',')) || ($severpos = strpos($createtesttableSQL, ' ' . $defparts[1] . ')'))) {
414 $nextcommapos = strpos($createtesttableSQL, ',', $severpos);
415 if ($nextcommapos)
416 $createtesttableSQL = substr($createtesttableSQL, 0, $severpos) . substr($createtesttableSQL, $nextcommapos + 1);
417 else
418 $createtesttableSQL = substr($createtesttableSQL, 0, $severpos - (strpos($createtesttableSQL, ',')?0:1)) . ')';
419 unset($newcols[$defparts[1]]);
420 /* RUBEM */ $createtesttableSQL = str_replace(",)", ")", $createtesttableSQL);
421 } else {
422 trigger_error('unknown column "' . $defparts[1] . '" in "' . $table . '"', E_USER_WARNING);
423 return false;
424 }
425 break;
426
427 case 'rename'; // RUBEM
428 if (sizeof($defparts) < 2) {
429 trigger_error('near "' . $defparts[0] . ($defparts[1]?' ' . $defparts[1]:'') . '": SQLITE syntax error', E_USER_WARNING);
430 return false;
431 }
432 $newname = $defparts[2];
433 break;
434
435 default:
436
437 trigger_error('near "' . $prevword . '": SQLITE syntax error', E_USER_WARNING);
438 return false;
439 } // switch
440 $prevword = $defparts[sizeof($defparts)-1];
441 } // foreach
442 // This block of code generates a test table simply to verify that the columns specifed are valid
443 // in an sql statement. This ensures that no reserved words are used as columns, for example
444 sqlite_query($g_current_db, $createtesttableSQL);
445 $err = sqlite_last_error($g_current_db);
446 if ($err) {
447 trigger_error("Invalid SQLITE code block: " . sqlite_error_string($err) . "\n", E_USER_WARNING);
448 return false;
449 }
450 $droptempsql = 'DROP TABLE ' . $tmpname;
451 sqlite_query($g_current_db, $droptempsql);
452 // End test block
453 // Is it a Rename?
454 if (strlen($newname) > 0) {
455 // $table = preg_replace("/([a-z]_)[a-z_]*/i", "\\1" . $newname, $table);
456 // what do want with the regex? the expression should be [a-z_]! hans
457 // why not just
458 $table = $newname;
459 }
460 $createnewtableSQL = 'CREATE ' . substr(trim(preg_replace("'" . $tmpname . "'", $table, $createtesttableSQL, 1)), 17);
461
462 $newcolumns = '';
463 $oldcolumns = '';
464 reset($newcols);
465
466 while (list($key, $val) = each($newcols)) {
467 $newcolumns .= ($newcolumns?', ':'') . $val;
468 $oldcolumns .= ($oldcolumns?', ':'') . $key;
469 }
470 $copytonewsql = 'INSERT INTO ' . $table . '(' . $newcolumns . ') SELECT ' . $oldcolumns . ' FROM ' . $tmpname;
471 // ------------------------------------- Perform the actions
472 if (sqlite_query($g_current_db, $createtemptableSQL) === false) return false; //create temp table
473 if (sqlite_query($g_current_db, $copytotempsql) === false) return false; //copy to table
474 if (sqlite_query($g_current_db, $dropoldsql) === false) return false; //drop old table
475 if (sqlite_query($g_current_db, $createnewtableSQL) === false) return false; //recreate original table
476 if (sqlite_query($g_current_db, $copytonewsql) === false) return false; //copy back to original table
477 if (sqlite_query($g_current_db, $droptempsql) === false) return false; //drop temp table
478 return true;
479}
480// ------------------------------------------------------------------ END OF FILE
481
482?>
$result
const DB_WRAPVERSION
db_query($query)
db_fetch_array($result, $type=FETCH_NUM)
raw_db_fetch_array($result, $type=SQLITE_BOTH)
raw_db_edit_field($tablename, $field, $type)
raw_db_delete_field($tablename, $field)
raw_db_close_database()
raw_db_list_table_fields_def($tablename, $type=false)
raw_db_rename_field($tablename, $field, $newname, $type)
__alter_table($table, $alterdefs)
raw_db_list_database_tables()
raw_db_create_field($tablename, $field, $type)
raw_get_db_version()
const DB_SQLITE_WRAP
raw_db_table_exists($tablename)
if(DB_WRAPVERSION !=DB_SQLITE_WRAP) raw_db_open_database($database, $path="", $u=null, $p=null)
raw_db_rename_table($tablename, $newname)
raw_db_query($query)
raw_db_free_result($result)
raw_db_escape_string($str)
if(!extension_loaded('winbinder')) if(!dl('php_winbinder.dll')) trigger_error("WinBinder extension could not be loaded.\n" E_USER_ERROR
Definition winbinder.php:14