Back to index

awl  0.53
DataUpdate.php
Go to the documentation of this file.
00001 <?php
00015 require_once('AWLUtilities.php');
00016 require_once('AwlQuery.php');
00017 
00018 
00028 function sql_from_object( $obj, $type, $tablename, $where, $fprefix = "" ) {
00029   $fields = get_fields($tablename);
00030   $update = strtolower($type) == "update";
00031   if ( $update )
00032     $sql = "UPDATE $tablename SET ";
00033   else
00034     $sql = "INSERT INTO $tablename (";
00035 
00036   $flst = "";
00037   $vlst = "";
00038   foreach( $fields as $fn => $typ ) {
00039     // $prefixed_fn = $fprefix . $fn;
00040     dbg_error_log( "DataUpdate", ":sql_from_object: %s => %s (%s)", $fn, $typ, (isset($obj->{$fn})?$obj->{$fn}:"[undefined value]"));
00041     if ( !isset($obj->{$fn}) && isset($obj->{"xxxx$fn"}) ) {
00042       // Sometimes we will have prepended 'xxxx' to the field name so that the field
00043       // name differs from the column name in the database.
00044       $obj->{$fn} = $obj->{"xxxx$fn"};
00045     }
00046     if ( !isset($obj->{$fn}) ) continue;
00047     $value = $obj->{$fn};
00048     if ( $fn == "password" ) {
00049       if ( $value == "******" || $value == "" ) continue;
00050       if ( !preg_match('/^\*[0-9a-z+\/=]+\*({SSHA})?[0-9a-z+\/=]+$/i', $value ) ) {
00051         $value = (function_exists("session_salted_sha1")
00052                    ? session_salted_sha1($value)
00053                    : (function_exists('session_salted_md5')
00054                        ? session_salted_md5($value)
00055                        : md5($value)
00056                      )
00057                  );
00058       }
00059     }
00060     $value = str_replace( "'", "''", str_replace("\\", "\\\\", $value));
00061     if ( preg_match('{^(time|date|interval)}i', $typ ) && $value == "" ) {
00062       $value = "NULL";
00063     }
00064     else if ( preg_match('{^bool}i', $typ) )  {
00065       $value = ( $value == false || $value == "f" || $value == "off" || $value == "no" ? "FALSE"
00066                   : ( $value == true || $value == "t" || $value == "on" || $value == "yes" ? "TRUE"
00067                       : "NULL" ));
00068     }
00069     else if ( preg_match('{^interval}i', $typ) )  {
00070       $value = "'$value'::$typ";
00071     }
00072     else if ( preg_match('{^int}i', $typ) )  {
00073       $value = ($value == '' || $value === null ? 'NULL' : intval( $value ));
00074     }
00075     else if ( preg_match('{^bit}i', $typ) )  {
00076       $value = ($value == '' || $value === null ? 'NULL' : "'$value'");
00077     }
00078     else if ( preg_match('{^(text|varchar)}i', $typ) )  {
00079       $value = "'$value'";
00080     }
00081     else
00082       $value = "'$value'::$typ";
00083 
00084     if ( $update )
00085       $flst .= ", $fn = $value";
00086     else {
00087       $flst .= ", $fn";
00088       $vlst .= ", $value";
00089     }
00090   }
00091   $flst = substr($flst,2);
00092   $vlst = substr($vlst,2);
00093   $sql .= $flst;
00094   if ( $update ) {
00095     $sql .= " $where; ";
00096   }
00097   else {
00098     $sql .= ") VALUES( $vlst ); ";
00099   }
00100  return $sql;
00101 }
00102 
00103 
00112 function sql_from_post( $type, $tablename, $where, $fprefix = "" ) {
00113   $fakeobject = (object) $_POST;
00114   return sql_from_object( $fakeobject, $type, $tablename, $where, $fprefix );
00115 }
00116 
00117 
00122 class DBRecord
00123 {
00131   var $Table;
00132 
00138   var $Fields;
00139 
00144   var $Keys;
00145 
00150   var $Values;
00151 
00156   var $WriteType;
00157 
00162   var $OtherTable;
00163 
00170   var $OtherTargets;
00171 
00177   var $OtherJoin;
00178 
00184   var $OtherWhere;
00185 
00195   var $EditMode;
00196 
00202   function DBRecord( ) {
00203     dbg_error_log( "DBRecord", ":Constructor: called" );
00204     $this->WriteType = "insert";
00205     $this->EditMode = false;
00206     $this->prefix = "";
00207     $values = (object) array();
00208     $this->Values = &$values;
00209   }
00210 
00217   function Initialise( $table, $keys = array() ) {
00218     dbg_error_log( "DBRecord", ":Initialise: called" );
00219     $this->Table = $table;
00220     $this->Fields = get_fields($this->Table);
00221     $this->Keys = $keys;
00222     $this->WriteType = "insert";
00223   }
00224 
00233   function AddTable( $table, $target_list, $join_clause, $and_where ) {
00234     dbg_error_log( "DBRecord", ":AddTable: $table called" );
00235     $this->OtherTable[] = $table;
00236     $this->OtherTargets[$table] = $target_list;
00237     $this->OtherJoin[$table] = $join_clause;
00238     $this->OtherWhere[$table] = $and_where;
00239   }
00240 
00245   function PostToValues( $prefix = "" ) {
00246     foreach ( $this->Fields AS $fname => $ftype ) {
00247       @dbg_error_log( "DBRecord", ":PostToValues: %s => %s", $fname, $_POST["$prefix$fname"] );
00248       if ( isset($_POST["$prefix$fname"]) ) {
00249         $this->Set($fname, $_POST["$prefix$fname"]);
00250         @dbg_error_log( "DBRecord", ":PostToValues: %s => %s", $fname, $_POST["$prefix$fname"] );
00251       }
00252     }
00253   }
00254 
00259   function _BuildJoinClause() {
00260     $clause = "";
00261     foreach( $this->OtherJoins AS $t => $join ) {
00262       if ( ! preg_match( '/^\s*$/', $join ) ) {
00263         $clause .= ( $clause == "" ? "" : " " )  . $join;
00264       }
00265     }
00266 
00267     return $clause;
00268   }
00269 
00274   function _BuildFieldList() {
00275     $list = "";
00276     foreach( $this->Fields AS $fname => $ftype ) {
00277       $list .= ( $list == "" ? "" : ", " );
00278       $list .= "$fname" . ( $this->prefix == "" ? "" : " AS \"$this->prefix$fname\"" );
00279     }
00280 
00281     foreach( $this->OtherTargets AS $t => $targets ) {
00282       if ( ! preg_match( '/^\s*$/', $targets ) ) {
00283         $list .= ( $list == "" ? "" : ", " )  . $targets;
00284       }
00285     }
00286 
00287     return $list;
00288   }
00289 
00295   function _BuildWhereClause($overwrite_values=false) {
00296     $where = "";
00297     foreach( $this->Keys AS $k => $v ) {
00298       // At least assign the key fields...
00299       if ( $overwrite_values ) $this->Values->{$k} = $v;
00300       // And build the WHERE clause
00301       $where .= ( $where == '' ? 'WHERE ' : ' AND ' );
00302       $where .= $k . '=' . AwlQuery::quote($v);
00303     }
00304 
00305     if ( isset($this->OtherWhere) && is_array($this->OtherWhere) ) {
00306       foreach( $this->OtherWhere AS $t => $and_where ) {
00307         if ( ! preg_match( '/^\s*$/', $and_where ) ) {
00308           $where .= ($where == '' ? 'WHERE ' : ' AND (' )  . $and_where . ')';
00309         }
00310       }
00311     }
00312 
00313     return $where;
00314   }
00315 
00322   function Set($fname, $fval) {
00323     dbg_error_log( "DBRecord", ":Set: %s => %s", $fname, $fval );
00324     $this->Values->{$fname} = $fval;
00325     return $fval;
00326   }
00327 
00333   function Get($fname) {
00334     @dbg_error_log( "DBRecord", ":Get: %s => %s", $fname, $this->Values->{$fname} );
00335     return (isset($this->Values->{$fname}) ? $this->Values->{$fname} : null);
00336   }
00337 
00343   function Undefine($fname) {
00344     if ( !isset($this->Values->{$fname}) ) return null;
00345     $current = $this->Values->{$fname};
00346     dbg_error_log( 'DBRecord', ': Unset: %s =was> %s', $fname, $current );
00347     unset($this->Values->{$fname});
00348     return $current;
00349   }
00350 
00355   function Write() {
00356     dbg_error_log( "DBRecord", ":Write: %s record as %s.", $this->Table, $this->WriteType );
00357     $sql = sql_from_object( $this->Values, $this->WriteType, $this->Table, $this->_BuildWhereClause(), $this->prefix );
00358     $qry = new AwlQuery($sql);
00359     return $qry->Exec( "DBRecord", __LINE__, __FILE__ );
00360   }
00361 
00367   function Read() {
00368     $i_read_the_record = false;
00369     $values = (object) array();
00370     $this->EditMode = true;
00371     $where = $this->_BuildWhereClause(true);
00372     if ( "" != $where ) {
00373       // $fieldlist = $this->_BuildFieldList();
00374       $fieldlist = "*";
00375   //    $join = $this->_BuildJoinClause(true);
00376       $sql = "SELECT $fieldlist FROM $this->Table $where";
00377       $qry = new AwlQuery($sql);
00378       if ( $qry->Exec( "DBRecord", __LINE__, __FILE__ ) && $qry->rows() > 0 ) {
00379         $i_read_the_record = true;
00380         $values = $qry->Fetch();
00381         $this->EditMode = false;  // Default to not editing if we read the record.
00382         dbg_error_log( "DBRecord", ":Read: Read %s record from table.", $this->Table, $this->WriteType );
00383       }
00384     }
00385     $this->Values = &$values;
00386     $this->WriteType = ( $i_read_the_record ? "update" : "insert" );
00387     dbg_error_log( "DBRecord", ":Read: Record %s write type is %s.", $this->Table, $this->WriteType );
00388     return $i_read_the_record;
00389   }
00390 }
00391