15require_once(
'AWLUtilities.php');
16require_once(
'AwlQuery.php');
28function sql_from_object( $obj, $type, $tablename, $where, $fprefix =
"" ) {
29 $fields = awl_get_fields($tablename);
30 $update = strtolower($type) ==
"update";
32 $sql =
"UPDATE $tablename SET ";
34 $sql =
"INSERT INTO $tablename (";
38 foreach( $fields as $fn => $typ ) {
40 dbg_error_log(
"DataUpdate",
":sql_from_object: %s => %s (%s)", $fn, $typ, (isset($obj->{$fn})?$obj->{$fn}:
"[undefined value]"));
41 if ( !isset($obj->{$fn}) && isset($obj->{
"xxxx$fn"}) ) {
44 $obj->{$fn} = $obj->{
"xxxx$fn"};
46 if ( !isset($obj->{$fn}) )
continue;
48 if ( $fn ==
"password" ) {
49 if ( $value ==
"******" || $value ==
"" )
continue;
50 if ( !preg_match(
'/^\*[0-9a-z+\/=]+\*({SSHA})?[0-9a-z+\/=]+$/i', $value ) ) {
51 $value = (function_exists(
"session_salted_sha1")
52 ? session_salted_sha1($value)
53 : (function_exists(
'session_salted_md5')
54 ? session_salted_md5($value)
60 $value = str_replace(
"'",
"''", str_replace(
"\\",
"\\\\", $value));
61 if ( preg_match(
'{^(time|date|interval)}i', $typ ) && $value ==
"" ) {
64 else if ( preg_match(
'{^bool}i', $typ) ) {
65 $value = ( $value ==
false || $value ==
"f" || $value ==
"off" || $value ==
"no" ?
"FALSE"
66 : ( $value ==
true || $value ==
"t" || $value ==
"on" || $value ==
"yes" ?
"TRUE"
69 else if ( preg_match(
'{^interval}i', $typ) ) {
70 $value =
"'$value'::$typ";
72 else if ( preg_match(
'{^int}i', $typ) ) {
73 $value = ($value ==
'' || $value ===
null ?
'NULL' : intval( $value ));
75 else if ( preg_match(
'{^bit}i', $typ) ) {
76 $value = ($value ==
'' || $value ===
null ?
'NULL' :
"'$value'");
78 else if ( preg_match(
'{^(text|varchar)}i', $typ) ) {
82 $value =
"'$value'::$typ";
85 $flst .=
", $fn = $value";
91 $flst = substr($flst,2);
92 $vlst = substr($vlst,2);
98 $sql .=
") VALUES( $vlst ); ";
112function sql_from_post( $type, $tablename, $where, $fprefix =
"" ) {
113 $fakeobject = (object) $_POST;
114 return sql_from_object( $fakeobject, $type, $tablename, $where, $fprefix );
203 dbg_error_log(
"DBRecord",
":Constructor: called" );
204 $this->WriteType =
"insert";
205 $this->EditMode =
false;
207 $values = (object) array();
208 $this->Values = &$values;
218 dbg_error_log(
"DBRecord",
":Initialise: called" );
219 $this->Table = $table;
220 $this->Fields = awl_get_fields($this->Table);
222 $this->WriteType =
"insert";
233 function AddTable( $table, $target_list, $join_clause, $and_where ) {
234 dbg_error_log(
"DBRecord",
":AddTable: $table called" );
235 $this->OtherTable[] = $table;
236 $this->OtherTargets[$table] = $target_list;
237 $this->OtherJoin[$table] = $join_clause;
238 $this->OtherWhere[$table] = $and_where;
246 foreach ( $this->Fields AS $fname => $ftype ) {
247 @dbg_error_log(
"DBRecord",
":PostToValues: %s => %s", $fname, $_POST[
"$prefix$fname"] );
248 if ( isset($_POST[
"$prefix$fname"]) ) {
249 $this->
Set($fname, $_POST[
"$prefix$fname"]);
250 @dbg_error_log(
"DBRecord",
":PostToValues: %s => %s", $fname, $_POST[
"$prefix$fname"] );
261 foreach( $this->OtherJoins AS $t => $join ) {
262 if ( ! preg_match(
'/^\s*$/', $join ) ) {
263 $clause .= ( $clause ==
"" ?
"" :
" " ) . $join;
276 foreach( $this->Fields AS $fname => $ftype ) {
277 $list .= ( $list ==
"" ?
"" :
", " );
278 $list .=
"$fname" . ( $this->prefix ==
"" ?
"" :
" AS \"$this->prefix$fname\"" );
281 foreach( $this->OtherTargets AS $t => $targets ) {
282 if ( ! preg_match(
'/^\s*$/', $targets ) ) {
283 $list .= ( $list ==
"" ?
"" :
", " ) . $targets;
297 foreach( $this->Keys AS $k => $v ) {
299 if ( $overwrite_values ) $this->Values->{$k} = $v;
301 $where .= ( $where ==
'' ?
'WHERE ' :
' AND ' );
305 if ( isset($this->OtherWhere) && is_array($this->OtherWhere) ) {
306 foreach( $this->OtherWhere AS $t => $and_where ) {
307 if ( ! preg_match(
'/^\s*$/', $and_where ) ) {
308 $where .= ($where ==
'' ?
'WHERE ' :
' AND (' ) . $and_where .
')';
322 function Set($fname, $fval) {
323 dbg_error_log(
"DBRecord",
":Set: %s => %s", $fname, $fval );
324 $this->Values->{$fname} = $fval;
334 @dbg_error_log(
"DBRecord",
":Get: %s => %s", $fname, $this->Values->{$fname} );
335 return (isset($this->Values->{$fname}) ? $this->Values->{$fname} :
null);
344 if ( !isset($this->Values->{$fname}) )
return null;
345 $current = $this->Values->{$fname};
346 dbg_error_log(
'DBRecord',
': Unset: %s =was> %s', $fname, $current );
347 unset($this->Values->{$fname});
356 dbg_error_log(
"DBRecord",
":Write: %s record as %s.", $this->Table, $this->WriteType );
357 $sql = sql_from_object( $this->Values, $this->WriteType, $this->Table, $this->_BuildWhereClause(), $this->prefix );
359 return $qry->Exec(
"DBRecord", __LINE__, __FILE__ );
368 $i_read_the_record =
false;
369 $values = (object) array();
370 $this->EditMode =
true;
371 $where = $this->_BuildWhereClause(
true);
372 if (
"" != $where ) {
376 $sql =
"SELECT $fieldlist FROM $this->Table $where";
378 if ( $qry->Exec(
"DBRecord", __LINE__, __FILE__ ) && $qry->rows() > 0 ) {
379 $i_read_the_record =
true;
380 $values = $qry->Fetch();
381 $this->EditMode =
false;
382 dbg_error_log(
"DBRecord",
":Read: Read %s record from table.", $this->Table, $this->WriteType );
385 $this->Values = &$values;
386 $this->WriteType = ( $i_read_the_record ?
"update" :
"insert" );
387 dbg_error_log(
"DBRecord",
":Read: Record %s write type is %s.", $this->Table, $this->WriteType );
388 return $i_read_the_record;
AddTable( $table, $target_list, $join_clause, $and_where)
_BuildWhereClause($overwrite_values=false)
Initialise( $table, $keys=array())
PostToValues( $prefix="")