18define(
"DB_WRAPVERSION",
"db_v2b");
20if (!defined(
"APPPREFIX")) define(
"APPPREFIX",
"");
21if (!defined(
"DB_DATABASE")) define(
"DB_DATABASE",
"SQLite");
26include
$_mainpath .
"db_" . strtolower(DB_DATABASE) .
".inc.php";
37 switch (strtolower($info)) {
95 $prefixlen = strlen(trim(APPPREFIX));
97 foreach($tables as $table) {
98 if (!(stristr(substr($table, 0, $prefixlen), APPPREFIX) ===
false)) {
99 $tmp_tabs[] = substr($table, strlen(APPPREFIX));
102 if ($tmp_tabs ==
"") {
106 }
else return $tables;
129 $tablename = $g_lasttable;
130 $g_lasttable = $tablename;
144function db_create_table($tablename, $fieldnames, $fieldattrib, $idfield =
"id", $valarray =
null)
148 if ($tablename ==
null || $tablename ==
"")
149 $tablename = $g_lasttable;
150 $g_lasttable = $tablename;
155 if (is_string($fieldnames))
156 $fieldnames = preg_split(
"/[\r\n,]/", $fieldnames);
157 if (is_string($fieldattrib))
158 $fieldattrib = preg_split(
"/[\r\n,]/", $fieldattrib);
159 $attribs = count($fieldattrib);
160 if (count($fieldnames) != $attribs) {
161 trigger_error(__FUNCTION__ .
": both arrays must be same length.");
164 $sql =
"CREATE TABLE " . APPPREFIX .
"$tablename (";
165 $sql .=
"$idfield int(11) NOT NULL PRIMARY KEY ";
169 for($i = 0; $i < $attribs; $i++)
170 $sql .= $fieldnames[$i] .
" " . $fieldattrib[$i] . ($i < $attribs - 1 ?
", " :
"");
176 trigger_error(__FUNCTION__ .
": could not create table $tablename.");
181 foreach($valarray as $values) {
201 $tablename = $g_lasttable;
202 $g_lasttable = $tablename;
204 if ($tablename ==
null || $tablename ==
"")
233 $tablename = $g_lasttable;
234 $g_lasttable = $tablename;
304function db_create_record($tablename, $fieldnames =
null, $fieldvalues =
null, $idfield =
"id")
309 $tablename = $g_lasttable;
310 $g_lasttable = $tablename;
314 array_shift($fieldnames);
317 $fieldvalues = array_fill(0, count($fieldnames), 0);
321 $sql =
"SELECT max($idfield) FROM " . APPPREFIX . $tablename;
327 $newid = $newid[0] + 1;
329 $names = is_string($fieldnames) ? preg_split(
"/[\r\n]/", $fieldnames) : $fieldnames;
330 $values = is_string($fieldvalues) ? preg_split(
"/[\r\n]/", $fieldvalues) : $fieldvalues;
331 if (count($names) != count($values)) {
332 trigger_error(__FUNCTION__ .
": both arrays must be same length.\n");
336 $nfields = count($names);
337 $fieldnames = $names;
338 $fieldvalues = $values;
339 for($i = 0, $names =
""; $i < $nfields; $i++)
340 $names .= $fieldnames[$i] . ($i < $nfields - 1 ?
", " :
"");
341 for($i = 0, $values =
""; $i < $nfields; $i++)
342 $values .=
"'" .
db_escape_string($fieldvalues[$i]) .
"'" . ($i < $nfields - 1 ?
", " :
"");
344 $sql =
"INSERT INTO " . APPPREFIX . $tablename .
" ($idfield, $names) VALUES ($newid, $values)";
348 trigger_error(__FUNCTION__ .
": could not create new record in table $tablename.");
367 if ($idarray ==
null || $idarray <= 0)
369 if (!is_array($idarray))
370 $idarray = array($idarray);
373 $tablename = $g_lasttable;
374 $g_lasttable = $tablename;
376 foreach($idarray as $item) {
378 $sql =
"DELETE FROM " . APPPREFIX . $tablename .
" WHERE $idfield = " . $item;
381 trigger_error(__FUNCTION__ .
": could not delete record $id in table $tablename.");
400function db_edit_record($tablename, $id = 0, $fieldnames =
null, $fieldvalues =
null, $idfield =
"id")
404 if ($id ==
null || $id <= 0) {
408 $tablename = $g_lasttable;
409 $g_lasttable = $tablename;
413 array_shift($fieldnames);
416 $fieldvalues = array_fill(0, count($fieldnames), 0);
420 $names = is_string($fieldnames) ? preg_split(
"/[\r\n]/", $fieldnames) : $fieldnames;
421 $values = is_string($fieldvalues) ? preg_split(
"/[\r\n]/", $fieldvalues) : $fieldvalues;
423 if (count($names) != count($values)) {
424 trigger_error(__FUNCTION__ .
": both arrays must be same length.\n");
428 $nfields = count($names);
429 for($i = 0, $str =
""; $i < $nfields; $i++) {
431 ($i < $nfields - 1 ?
", " :
"");
434 $sql =
"UPDATE " . APPPREFIX .
"$tablename SET $str WHERE $idfield=$id";
438 trigger_error(__FUNCTION__ .
": could not edit record $id in table $tablename.");
462 $tablename = $g_lasttable;
463 $g_lasttable = $tablename;
464 $table = APPPREFIX .
"$tablename";
468 trigger_error(__FUNCTION__ .
": could not read record $id1 in table $tablename.");
472 $fieldvalues1 = array_values($a);
473 $fieldnames1 = array_keys($a);
474 array_shift($fieldvalues1);
475 array_shift($fieldnames1);
479 trigger_error(__FUNCTION__ .
": could not read record $id2 in table $tablename.");
483 $fieldvalues2 = array_values($a);
484 $fieldnames2 = array_keys($a);
485 array_shift($fieldvalues2);
486 array_shift($fieldnames2);
488 if (
db_edit_record($tablename, $id1, $fieldnames2, $fieldvalues2, $idfield) ===
false)
return false;
489 if (
db_edit_record($tablename, $id2, $fieldnames1, $fieldvalues1, $idfield) ===
false)
return false;
493 if (
db_edit_record($tablename, $id1, array($idfield), array($unique), $idfield) ===
false)
return false;
494 if (
db_edit_record($tablename, $id2, array($idfield), array($id1), $idfield) ===
false)
return false;
495 if (
db_edit_record($tablename, $unique, array($idfield), array($id2), $idfield) ===
false)
return false;
535function db_get_data($tablename, $id =
null, $col =
null, $where =
"", $result_type =
FETCH_NUM, $idfield =
"id", $orderby =
"")
540 $tablename = $g_lasttable;
541 $g_lasttable = $tablename;
544 $col = implode(
",", $col);
545 if ($col ===
null || $col ===
"")
549 if (is_string($id) && strstr($id,
",")) {
550 $id = explode(
",", $id);
554 for($i = 0; $i < count($id); $i++)
555 $idcond .=
"$idfield = '{$id[$i]}'" . ($i < count($id) - 1 ?
" OR " :
"");
557 $idcond =
"$idfield = '$id'";
559 $condition = $where ?
" WHERE ($where) AND ($idcond)" :
" WHERE ($idcond)";
561 $condition = $where ?
" WHERE ($where)" :
"";
563 $orderby = $orderby ?
" ORDER BY $orderby" :
"";
565 $sql =
"SELECT $col FROM " . APPPREFIX . $tablename . $condition . $orderby;
573 if (count($row) == 1)
574 $row = array_shift($row);
579 if (!is_array($array))
582 switch (count($array)) {
589 $elem = array_shift($test);
592 if (is_scalar($elem))
615 return array_search($id, $data);
633 if (!is_scalar($index)) {
634 trigger_error(__FUNCTION__ .
": index must be an integer");
637 $index = (int)$index;
640 $tablename = $g_lasttable;
641 $g_lasttable = $tablename;
644 $sql =
"SELECT $idfield FROM " . APPPREFIX . $tablename .
" LIMIT 1 OFFSET $index";
669 global $g_current_db;
673 $tablename = $g_lasttable;
674 $g_lasttable = $tablename;
676 $sql =
"SELECT max($idfield) FROM " . APPPREFIX . $tablename;
683 return $maxid[0] + 1;
db_get_id($tablename, $index, $idfield="id")
db_swap_records($tablename, $id1, $id2, $idfield="id", $xchangeid=true)
db_get_next_free_id($tablename, $idfield="id")
db_rename_table($tablename, $newname)
db_delete_field($tablename, $field)
db_edit_record($tablename, $id=0, $fieldnames=null, $fieldvalues=null, $idfield="id")
db_list_database_tables()
db_get_data($tablename, $id=null, $col=null, $where="", $result_type=FETCH_NUM, $idfield="id", $orderby="")
db_table_exists($tablename)
db_list_table_fields($tablename, $type=false)
db_create_record($tablename, $fieldnames=null, $fieldvalues=null, $idfield="id")
db_create_table($tablename, $fieldnames, $fieldattrib, $idfield="id", $valarray=null)
db_open_database($database, $server="", $username="", $password="")
db_create_database($database, $server="", $username="", $password="")
db_edit_field($tablename, $field, $type)
db_rename_field($tablename, $field, $newname, $type)
db_delete_table($tablename)
db_fetch_array($result, $type=FETCH_NUM)
db_delete_records($tablename, $idarray, $idfield="id")
db_get_index($tablename, $id, $idfield="id")
db_create_field($tablename, $field, $type)
raw_db_edit_field($tablename, $field, $type)
if(DB_WRAPVERSION !=DB_MYSQL_WRAP) raw_db_open_database($database, $server="", $username="", $password="")
raw_db_create_database($database, $server="", $username="", $password="")
raw_db_delete_field($tablename, $field)
raw_db_list_table_fields_def($tablename, $type=false)
raw_db_rename_field($tablename, $field, $newname, $type)
raw_db_list_database_tables()
raw_db_create_field($tablename, $field, $type)
raw_db_table_exists($tablename)
raw_db_fetch_array($result, $type=FETCH_BOTH)
raw_db_rename_table($tablename, $newname)
raw_db_free_result($result)
raw_db_escape_string($str)