Remove duplicate function.
[adorno.git] / inc / DataUpdate.php
blobda51a8942be1c6c0626b33f698d8dcb3eb5be556
1 <?php
2 function get_fields( $tablename ) {
3 global $sysname;
4 $sql = "SELECT f.attname, t.typname FROM pg_attribute f ";
5 $sql .= "JOIN pg_class c ON ( f.attrelid = c.oid ) ";
6 $sql .= "JOIN pg_type t ON ( f.atttypid = t.oid ) ";
7 $sql .= "WHERE relname = ? AND attnum >= 0 order by f.attnum;";
8 $qry = new PgQuery( $sql, $tablename );
9 $qry->Exec("DataUpdate");
10 $fields = array();
11 while( $row = $qry->Fetch() ) {
12 $fields["$row->attname"] = $row->typname;
13 error_log( "$sysname DBG: " . $fields["$row->attname"] . " => " . $row->typname, 0);
15 return $fields;
18 function sql_from_post( $type, $tablename, $where, $fprefix = "" ) {
19 global $sysname;
20 $fields = get_fields($tablename);
21 $update = strtolower($type) == "update";
22 if ( $update )
23 $sql = "UPDATE $tablename SET ";
24 else
25 $sql = "INSERT INTO $tablename (";
27 $flst = "";
28 $vlst = "";
29 foreach( $fields as $fn => $typ ) {
30 $fn = $fprefix . $fn;
31 error_log( "$sysname DBG: $fn => $typ (".$_POST[$fn].")", 0);
32 if ( !isset($_POST[$fn]) ) continue;
33 $value = str_replace( "'", "''", str_replace("\\", "\\\\", $_POST[$fn]));
34 if ( $fn == "password" ) {
35 if ( $value == "******" || $value == "" ) continue;
36 $value = md5($value);
38 if ( eregi("(time|date)", $typ ) && $value == "" ) {
39 $value = "NULL";
41 else if ( eregi("bool", $typ) ) {
42 $value = ( $value == "f" ? "FALSE" : "TRUE" );
44 else if ( eregi("int", $typ) ) {
45 $value = intval( $value );
47 else if ( eregi("(text|varchar)", $typ) ) {
48 $value = "'$value'";
50 else
51 $value = "'$value'::$typ";
53 if ( $update )
54 $flst .= ", $fn = $value";
55 else {
56 $flst .= ", $fn";
57 $vlst .= ", $value";
60 $flst = substr($flst,2);
61 $vlst = substr($vlst,2);
62 $sql .= $flst;
63 if ( $update ) {
64 $sql .= " $where; ";
66 else {
67 $sql .= ") VALUES( $vlst ); ";
69 return $sql;