2 function get_fields( $tablename ) {
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");
11 while( $row = $qry->Fetch() ) {
12 $fields["$row->attname"] = $row->typname
;
13 error_log( "$sysname DBG: " . $fields["$row->attname"] . " => " . $row->typname
, 0);
18 function sql_from_post( $type, $tablename, $where, $fprefix = "" ) {
20 $fields = get_fields($tablename);
21 $update = strtolower($type) == "update";
23 $sql = "UPDATE $tablename SET ";
25 $sql = "INSERT INTO $tablename (";
29 foreach( $fields as $fn => $typ ) {
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;
38 if ( eregi("(time|date)", $typ ) && $value == "" ) {
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) ) {
51 $value = "'$value'::$typ";
54 $flst .= ", $fn = $value";
60 $flst = substr($flst,2);
61 $vlst = substr($vlst,2);
67 $sql .= ") VALUES( $vlst ); ";