20 include_once (
'Class.Log.php');
21 include_once (
'Lib.Common.php');
110 $this->log =
new Log(
"",
"DbObj", $this->dbtable);
112 if ($this->dbid == 0) {
116 $this->selectstring =
"";
118 reset($this->fields);
119 foreach ($this->fields as $k => $v) {
120 $this->selectstring = $this->selectstring . $this->dbtable .
"." . $v .
",";
124 reset($this->sup_fields);
125 foreach ($this->sup_fields as $k => $v) {
126 $this->selectstring = $this->selectstring .
"" . $v .
",";
129 $this->selectstring = substr($this->selectstring, 0, strlen($this->selectstring) - 1);
131 if ((
$id !=
'') || (is_array(
$id)) || (!isset($this->id_fields[0]))) {
137 if (is_array(
$res)) {
146 if ($this->dbid == - 1)
return FALSE;
149 if ($msg !=
'')
return $msg;
151 if ($this->dbtable ==
'') {
152 return (
"error : No Tables");
154 $fromstr =
"{$this->dbtable}";
155 if (is_array($this->sup_tables)) {
156 reset($this->sup_tables);
157 foreach ($this->sup_tables as $k => $v) {
161 $sql =
"select {$this->selectstring} from {$fromstr} ";
166 $wherestr =
" where ";
167 reset($this->id_fields);
168 foreach ($this->id_fields as $k => $v) {
170 $wherestr = $wherestr .
" AND ";
172 $wherestr = $wherestr .
"( " . $this->dbtable .
"." . $v .
"=E'" . pg_escape_string(
$id[$k]) .
"' )";
178 if (isset($this->id_fields[0])) {
179 $k = $this->id_fields[0];
181 $wherestr =
"where " . $this->dbtable .
"." . $this->id_fields[0] .
"=E'" . pg_escape_string(
$id) .
"'";
186 if (is_array($this->sup_where)) {
187 reset($this->sup_where);
188 foreach ($this->sup_where as $k => $v) {
189 $wherestr = $wherestr .
" AND ";
190 $wherestr = $wherestr .
"( " . $v .
" )";
206 if ($msg !=
'')
return $msg;
216 foreach ($this->fields as $k => $v) {
229 if ($this->dbid == - 1)
return FALSE;
232 if (count(
$fields) == 0)
return true;
233 if ($this->dbtable ==
'') {
234 return (
"error : No Tables");
238 foreach ($this->id_fields as
$id) {
239 $w[] =
"($id = E'" . pg_escape_string($this->$id) .
"') ";
241 $sqlwhere = implode(
"and", $w);
242 $sqlselect = implode(
",",
$fields);
244 $sql =
"select $sqlselect from $fromstr where $sqlwhere";
260 foreach ($array as $k => $v) {
261 if (!is_integer($k)) {
362 function Add($nopost =
false, $nopre =
false)
364 if ($this->dbid == - 1)
return FALSE;
367 if ($msg !=
'')
return $msg;
369 $sfields = implode(
",", $this->fields);
370 $sql =
"insert into " . $this->dbtable .
"($sfields) values (";
373 reset($this->fields);
374 foreach ($this->fields as $k => $v) {
375 $valstring = $valstring . $this->
lw($this->$v) .
",";
377 $valstring = substr($valstring, 0, strlen($valstring) - 1);
387 if ($msg !=
'')
return $msg;
398 function Modify($nopost =
false, $sfields =
"", $nopre =
false)
401 if ($this->dbid == - 1)
return FALSE;
403 if ($msg !=
'')
return $msg;
404 $sql =
"update " . $this->dbtable .
" set ";
411 foreach ($this->id_fields as $k => $v)
$fields[] = $v;
415 foreach ($this->id_fields as $k => $v) {
418 $val = pg_escape_string($this->$v);
419 $wstr = $wstr .
" " . $v .
"=E'" . $val .
"' AND";
423 foreach (
$fields as $k => $v) {
424 if (!isset($notset[$v])) {
425 $setstr = $setstr .
" " . $v .
"=" . $this->
lw($this->$v) .
",";
428 $setstr = substr($setstr, 0, strlen($setstr) - 1);
429 $wstr = substr($wstr, 0, strlen($wstr) - 3);
432 $sql.=
" where " . $wstr .
";";
443 if ($msg !=
'')
return $msg;
449 if ($msg !=
'')
return $msg;
453 reset($this->id_fields);
454 foreach ($this->id_fields as $k => $v) {
456 $wherestr = $wherestr .
" AND ";
458 $wherestr = $wherestr .
"( " . $v .
"=E'" . pg_escape_string($this->$v) .
"' )";
462 $sql =
"delete from " . $this->dbtable .
" where " . $wherestr .
";";
471 if ($msg !=
'')
return $msg;
481 function Adds(&$tcopy, $nopost =
false)
483 if ($this->dbid == - 1)
return FALSE;
484 if (!is_array($tcopy))
return FALSE;
486 $sfields = implode(
",", $this->fields);
487 $sql =
"copy " . $this->dbtable .
"($sfields) from STDIN;\n";
490 foreach ($tcopy as $kc => $vc) {
492 foreach ($this->fields as $k => $v) {
493 $trow[$kc].=
"" . ((isset($vc[$v])) ? $vc[$v] : ((($this->$v) !=
'') ? $this->$v :
'\N')) .
"\t";
497 $trow[$kc] = substr($trow[$kc], 0, -1);
500 $berr = pg_copy_from($this->dbid, $this->dbtable, $trow,
"\t");
502 if (!$berr)
return sprintf(_(
"DbObj::Adds error in multiple insertion"));
505 if ($msg !=
'')
return $msg;
509 $result = (($prop ==
'') && ($prop !== 0)) ?
"null" :
"E'" . pg_escape_string($prop) .
"'";
514 pg_close(
"$this->dbid");
521 if (isset($this->sqlcreate)) {
523 if (is_array($this->sqlcreate)) {
524 foreach ($this->sqlcreate as $k => $sqlquery) {
528 $sqlcmds = explode(
";", $this->sqlcreate);
529 foreach (
$sqlcmds as $k => $sqlquery) {
533 $this->log->debug(
"DbObj::Create : {$this->sqlcreate}");
535 if (isset($this->sqlinit)) {
537 $this->log->debug(
"Init : {$this->sqlinit}");
540 $this->log->info(
"DbObj::Create $msg");
554 if ($this->dbaccess ==
"") {
558 $this->dbid =
getDbid($this->dbaccess);
571 if (
$sql ==
"")
return '';
573 if ($SQLDEBUG) $sqlt1 = microtime();
575 $this->log->debug(
"exec_query : $sql");
579 $this->err_code =
'';
582 if (pg_send_prepare($this->dbid,
'',
$sql) ===
false) {
583 $this->msg_err =
"Error preparing statement : " . pg_last_error($this->dbid);
584 error_log(__METHOD__ .
" " . $this->msg_err);
587 $this->res = pg_get_result($this->dbid);
588 $this->msg_err = pg_result_error($this->res);
589 $this->err_code = pg_result_error_field($this->res, PGSQL_DIAG_SQLSTATE);
591 if ($this->msg_err ==
"") {
592 if (pg_send_execute($this->dbid,
'', array()) ===
false) {
593 $this->msg_err =
"Error executing statement : " . pg_last_error($this->dbid);
594 error_log(__METHOD__ .
" " . $this->msg_err);
597 $this->res = pg_get_result($this->dbid);
598 $this->msg_err = pg_result_error($this->res);
599 $this->err_code = pg_result_error_field($this->res, PGSQL_DIAG_SQLSTATE);
602 if (pg_send_query($this->dbid,
$sql) ===
false) {
603 $this->msg_err =
"Error sending query : " . pg_last_error($this->dbid);
604 error_log(__METHOD__ .
" " . $this->msg_err);
607 $this->res = pg_get_result($this->dbid);
608 $this->msg_err = pg_result_error($this->res);
609 $this->err_code = pg_result_error_field($this->res, PGSQL_DIAG_SQLSTATE);
612 if ($this->msg_err) error_log($this->msg_err);
615 if ($this->err_code !=
"") {
617 switch ($this->err_code) {
620 $action_needed =
"create";
625 $action_needed =
"update";
630 $action_needed =
"none";
637 error_log(__METHOD__ .
" [" . $this->msg_err .
" (" . $this->err_code .
")]:$action_needed.[$sql]");
644 switch ($action_needed) {
650 return "Table {$this->dbtable} doesn't exist and can't be created";
655 $this->log->warning(
"sql fail: $sql");
656 $this->log->warning(
"try update :: " . $this->msg_err);
661 return "Table {$this->dbtable} cannot be updated";
672 if ($this->msg_err !=
"") {
673 $this->log->warning(
"exec_query :" .
$sql);
674 $this->log->warning(
"PostgreSQL Error : " . $this->msg_err);
680 $TSQLDELAY[] = array(
682 "s" => str_replace(array(
693 return ($this->msg_err);
698 if ($this->msg_err ==
"") {
699 return (pg_num_rows($this->res));
708 return (pg_fetch_array($this->res,
$c,
$type));
714 print (
" - need update table " . $this->dbtable);
715 $this->log->error(
"need Update table " . $this->dbtable);
717 $this->log->info(
"Update table " . $this->dbtable);
719 $objupdate =
new DbObj($this->dbaccess);
722 $dumpfile = uniqid(
getTmpDir() .
"/" . $this->dbtable);
723 $err = $objupdate->exec_query(
"COPY " . $this->dbtable .
" TO '" . $dumpfile .
"'");
724 $this->log->info(
"Dump table " . $this->dbtable .
" in " . $dumpfile);
731 $err = $objupdate->exec_query(
"ALTER TABLE " . $this->dbtable .
" RENAME TO " . $this->dbtable .
"_old", 1);
735 $err = $this->
exec_query(
"select indexname from pg_indexes where tablename='" . $this->dbtable .
"_old'", 1);
737 for (
$c = 0;
$c < $nbidx;
$c++) {
740 $err = $objupdate->exec_query(
"DROP INDEX " . $row[
"indexname"], 1);
749 $this->
exec_query(
"SELECT * FROM " . $this->dbtable .
"_old");
751 for (
$c = 0;
$c < $nbold;
$c++) {
757 $inter_fields = array_intersect(array_keys($row) , $this->fields);
758 reset($this->fields);
760 foreach ($inter_fields as $k => $v) {
769 reset($inter_fields);
770 foreach ($inter_fields as $k => $v) {
771 $values.=
"E'" . pg_escape_string($row[$v]) .
"',";
773 $values = substr($values, 0, strlen($values) - 1);
776 $err = $objupdate->exec_query(
"INSERT INTO " . $this->dbtable .
" " .
$fields .
" VALUES " . $values, 1);
781 $err = $objupdate->exec_query(
"DROP TABLE " . $this->dbtable .
"_old", 1);
793 if (!self::$savepoint[$this->dbid]) {
802 $err = $this->
exec_query(sprintf(
'savepoint "%s"', pg_escape_string($point)));
804 if (
$err) error_log(__METHOD__ .
":$err");
814 $lastPoint = array_search($point, self::$savepoint[$this->dbid]);
815 if ($lastPoint !==
false) {
816 self::$savepoint[
$this->dbid] = array_slice(self::$savepoint[$this->dbid], 0, $lastPoint);
817 $err = $this->
exec_query(sprintf(
'rollback to savepoint "%s"', pg_escape_string($point)));
819 if ((!
$err) && (count(self::$savepoint[$this->dbid]) == 0)) {
824 $err = sprintf(
"cannot rollback unsaved point : %s", $point);
827 if (
$err) error_log(__METHOD__ .
":$err");
837 $lastPoint = array_search($point, self::$savepoint[$this->dbid]);
839 if ($lastPoint !==
false) {
840 self::$savepoint[
$this->dbid] = array_slice(self::$savepoint[$this->dbid], 0, $lastPoint);
841 $err = $this->
exec_query(sprintf(
'release savepoint "%s"', pg_escape_string($point)));
842 if ((!
$err) && (count(self::$savepoint[$this->dbid]) == 0)) {
846 $err = sprintf(
"cannot commit unsaved point : %s", $point);
848 if (
$err) error_log(__METHOD__ .
":$err");