Platform  3.1
PHP API documentation
 All Data Structures Namespaces Files Functions Variables Pages
updateclass.php
Go to the documentation of this file.
1 <?php
2 /**
3  * Update the SQL structure of a table of a DbObj Object
4  *
5  * @param string $appc the application directory (WHAT, FDL, ...)
6  * @param string $class the class name of the DbObj Class
7  * @param string $dbname the SQL database name (anakeen, freedom)
8  * @author Anakeen 2002
9  * @version $Id: updateclass.php.in,v 1.8 2008/12/31 14:39:35 jerome Exp $
10  * @license http://creativecommons.org/licenses/by-nc-sa/2.0/fr/ Anakeen - licence CC
11  * @package FDL
12  * @subpackage CORE
13  */
14 /**
15  */
16 
17 include_once('Class.Application.php');
18 
19 $appclass = getHttpVars('appc', 'WHAT');
20 $class = getHttpVars('class', '');
21 $db = getHttpVars('dbcoord', getDbAccess());
22 
23 include_once("$pubdir/$appclass/Class.$class.php");
24 
25 $o= new $class($db);
26 
27 $dbid=pg_connect($db);
28 if (! $dbid) {
29  print _("cannot access to database $db\n");
30  exit(1);
31 } else print _("access granted to database $db\n");
32 
33 $sql=array();
34 $rq=@pg_query ($dbid, "select * from ".$o->dbtable." LIMIT 1;");
35 if (!$rq) {
36  // table not exist : just create
37  $o->Create();
38  // $sqlcmds = explode(";",$o->sqlcreate);
39  // while (list($k,$sqlquery)=each($sqlcmds)) {
40  // if (chop($sqlquery) != "")
41  // $sql[]=$sqlquery;
42  // }
43 } else {
44  $row=0;
45 
46  if (pg_result_error($rq) == "") {
47  if (pg_num_rows($rq) > 0 ) {
48  $row= pg_fetch_array($rq,0,PGSQL_ASSOC);
49  if ($row) {
50  $fieds = array_intersect($o->fields,array_keys($row));
51  $sql[]= "CREATE TABLE ".$o->dbtable."_old AS SELECT * FROM ".$o->dbtable.";";
52  }
53  }
54  $sql[]= "DROP TABLE ".$o->dbtable.";";
55  }
56  $sqlcmds = explode(";",$o->sqlcreate);
57  while (list($k,$sqlquery)=each($sqlcmds)) {
58  if (chop($sqlquery) != "")
59  $sql[]=$sqlquery;
60  }
61 
62  if ($row) {
63  $sql[]= "INSERT INTO ".$o->dbtable." (".implode(",", $fieds).") SELECT ".implode(",", $fieds). " FROM ".$o->dbtable."_old";
64 
65  $sql[]= "DROP TABLE ".$o->dbtable."_old;";
66  }
67 }
68 while (list($k,$v) = each ($sql)) {
69  print "Sql:$v\n";
70  $rq=@pg_exec ($dbid, $v);
71  if (! $rq) {
72  if (preg_match("/create sequence/",$v, $reg)) {
73  $pgmess = pg_errormessage($dbid);
74  echo "".$pgmess."\n";
75 
76  } else {
77  $pgmess = pg_errormessage($dbid);
78  echo "".$pgmess."\n";
79  echo ""."ABORTED"."\n";
80  break;
81  }
82 
83  }
84 }
85 
86 pg_close($dbid);
87 
88 ?>
← centre documentaire © anakeen - published under CC License - Dynacase