Back to index

awl  0.53
Public Member Functions | Public Attributes
DBRecord Class Reference
Inheritance diagram for DBRecord:
Inheritance graph
[legend]

List of all members.

Public Member Functions

 DBRecord ()
 

@-


 Initialise ($table, $keys=array())
 This will read the record from the database if it's available, and the $keys parameter is a non-empty array.
 AddTable ($table, $target_list, $join_clause, $and_where)
 This will join an additional table to the maintained set.
 PostToValues ($prefix="")
 This will assign $_POST values to the internal Values object for each field that exists in the Fields array.
 _BuildJoinClause ()
 Builds a table join clause.
 _BuildFieldList ()
 Builds a field target list.
 _BuildWhereClause ($overwrite_values=false)
 Builds a where clause to match the supplied keys.
 Set ($fname, $fval)
 Sets a single field in the record.
 Get ($fname)
 Returns a single field from the record.
 Undefine ($fname)
 Unsets a single field from the record.
 Write ()
 To write the record to the database.
 Read ()
 To read the record from the database.

Public Attributes

 $Table
 $Fields
 $Keys
 $Values
 $WriteType
 $OtherTable
 $OtherTargets
 $OtherJoin
 $OtherWhere
 $EditMode

Detailed Description

Definition at line 122 of file DataUpdate.php.


Member Function Documentation

Builds a field target list.

Returns:
string A simple SQL target field list for each field, possibly including prefixes.

Definition at line 274 of file DataUpdate.php.

                             {
    $list = "";
    foreach( $this->Fields AS $fname => $ftype ) {
      $list .= ( $list == "" ? "" : ", " );
      $list .= "$fname" . ( $this->prefix == "" ? "" : " AS \"$this->prefix$fname\"" );
    }

    foreach( $this->OtherTargets AS $t => $targets ) {
      if ( ! preg_match( '/^\s*$/', $targets ) ) {
        $list .= ( $list == "" ? "" : ", " )  . $targets;
      }
    }

    return $list;
  }

Builds a table join clause.

Returns:
string A simple SQL target join clause excluding the primary table.

Definition at line 259 of file DataUpdate.php.

                              {
    $clause = "";
    foreach( $this->OtherJoins AS $t => $join ) {
      if ( ! preg_match( '/^\s*$/', $join ) ) {
        $clause .= ( $clause == "" ? "" : " " )  . $join;
      }
    }

    return $clause;
  }
DBRecord::_BuildWhereClause ( overwrite_values = false)

Builds a where clause to match the supplied keys.

Parameters:
boolean$overwrite_valuesControls whether the data values for the key fields will be forced to match the key values
Returns:
string A simple SQL where clause, including the initial "WHERE", for each key / value.

Definition at line 295 of file DataUpdate.php.

                                                      {
    $where = "";
    foreach( $this->Keys AS $k => $v ) {
      // At least assign the key fields...
      if ( $overwrite_values ) $this->Values->{$k} = $v;
      // And build the WHERE clause
      $where .= ( $where == '' ? 'WHERE ' : ' AND ' );
      $where .= $k . '=' . AwlQuery::quote($v);
    }

    if ( isset($this->OtherWhere) && is_array($this->OtherWhere) ) {
      foreach( $this->OtherWhere AS $t => $and_where ) {
        if ( ! preg_match( '/^\s*$/', $and_where ) ) {
          $where .= ($where == '' ? 'WHERE ' : ' AND (' )  . $and_where . ')';
        }
      }
    }

    return $where;
  }

Here is the call graph for this function:

DBRecord::AddTable ( table,
target_list,
join_clause,
and_where 
)

This will join an additional table to the maintained set.

Parameters:
string$tableThe name of the database table
array$keysAn associative array containing fieldname => value pairs for the record key.
string$joinA PostgreSQL join clause.
string$prefixA field prefix to use for these fields to distinguish them from fields in other joined tables with the same name.

Definition at line 233 of file DataUpdate.php.

                                                                      {
    dbg_error_log( "DBRecord", ":AddTable: $table called" );
    $this->OtherTable[] = $table;
    $this->OtherTargets[$table] = $target_list;
    $this->OtherJoin[$table] = $join_clause;
    $this->OtherWhere[$table] = $and_where;
  }

@-

Really numbingly simple construction.

Definition at line 202 of file DataUpdate.php.

                       {
    dbg_error_log( "DBRecord", ":Constructor: called" );
    $this->WriteType = "insert";
    $this->EditMode = false;
    $this->prefix = "";
    $values = (object) array();
    $this->Values = &$values;
  }

Here is the caller graph for this function:

DBRecord::Get ( fname)

Returns a single field from the record.

Parameters:
string$fnameThe name of the field to set the value for
Returns:
mixed The current value of the field.

Definition at line 333 of file DataUpdate.php.

                       {
    @dbg_error_log( "DBRecord", ":Get: %s => %s", $fname, $this->Values->{$fname} );
    return (isset($this->Values->{$fname}) ? $this->Values->{$fname} : null);
  }

Here is the caller graph for this function:

DBRecord::Initialise ( table,
keys = array() 
)

This will read the record from the database if it's available, and the $keys parameter is a non-empty array.

Parameters:
string$tableThe name of the database table
array$keysAn associative array containing fieldname => value pairs for the record key.

Definition at line 217 of file DataUpdate.php.

                                                 {
    dbg_error_log( "DBRecord", ":Initialise: called" );
    $this->Table = $table;
    $this->Fields = get_fields($this->Table);
    $this->Keys = $keys;
    $this->WriteType = "insert";
  }

Here is the caller graph for this function:

DBRecord::PostToValues ( prefix = "")

This will assign $_POST values to the internal Values object for each field that exists in the Fields array.

Definition at line 245 of file DataUpdate.php.

                                        {
    foreach ( $this->Fields AS $fname => $ftype ) {
      @dbg_error_log( "DBRecord", ":PostToValues: %s => %s", $fname, $_POST["$prefix$fname"] );
      if ( isset($_POST["$prefix$fname"]) ) {
        $this->Set($fname, $_POST["$prefix$fname"]);
        @dbg_error_log( "DBRecord", ":PostToValues: %s => %s", $fname, $_POST["$prefix$fname"] );
      }
    }
  }

Here is the call graph for this function:

To read the record from the database.

If we don't have any keys then the record will be blank.

Returns:
boolean Whether we actually read a record.

Definition at line 367 of file DataUpdate.php.

                  {
    $i_read_the_record = false;
    $values = (object) array();
    $this->EditMode = true;
    $where = $this->_BuildWhereClause(true);
    if ( "" != $where ) {
      // $fieldlist = $this->_BuildFieldList();
      $fieldlist = "*";
  //    $join = $this->_BuildJoinClause(true);
      $sql = "SELECT $fieldlist FROM $this->Table $where";
      $qry = new AwlQuery($sql);
      if ( $qry->Exec( "DBRecord", __LINE__, __FILE__ ) && $qry->rows() > 0 ) {
        $i_read_the_record = true;
        $values = $qry->Fetch();
        $this->EditMode = false;  // Default to not editing if we read the record.
        dbg_error_log( "DBRecord", ":Read: Read %s record from table.", $this->Table, $this->WriteType );
      }
    }
    $this->Values = &$values;
    $this->WriteType = ( $i_read_the_record ? "update" : "insert" );
    dbg_error_log( "DBRecord", ":Read: Record %s write type is %s.", $this->Table, $this->WriteType );
    return $i_read_the_record;
  }

Here is the caller graph for this function:

DBRecord::Set ( fname,
fval 
)

Sets a single field in the record.

Parameters:
string$fnameThe name of the field to set the value for
string$fvalThe value to set the field to
Returns:
mixed The new value of the field (i.e. $fval).

Definition at line 322 of file DataUpdate.php.

                              {
    dbg_error_log( "DBRecord", ":Set: %s => %s", $fname, $fval );
    $this->Values->{$fname} = $fval;
    return $fval;
  }

Here is the caller graph for this function:

DBRecord::Undefine ( fname)

Unsets a single field from the record.

Parameters:
string$fnameThe name of the field to unset the value for
Returns:
mixed The current value of the field.

Definition at line 343 of file DataUpdate.php.

                            {
    if ( !isset($this->Values->{$fname}) ) return null;
    $current = $this->Values->{$fname};
    dbg_error_log( 'DBRecord', ': Unset: %s =was> %s', $fname, $current );
    unset($this->Values->{$fname});
    return $current;
  }

Here is the caller graph for this function:

To write the record to the database.

Returns:
boolean Success.

Reimplemented in User.

Definition at line 355 of file DataUpdate.php.

                   {
    dbg_error_log( "DBRecord", ":Write: %s record as %s.", $this->Table, $this->WriteType );
    $sql = sql_from_object( $this->Values, $this->WriteType, $this->Table, $this->_BuildWhereClause(), $this->prefix );
    $qry = new AwlQuery($sql);
    return $qry->Exec( "DBRecord", __LINE__, __FILE__ );
  }

Here is the call graph for this function:


Member Data Documentation

Definition at line 195 of file DataUpdate.php.

Definition at line 138 of file DataUpdate.php.

Definition at line 144 of file DataUpdate.php.

Definition at line 177 of file DataUpdate.php.

Definition at line 162 of file DataUpdate.php.

Definition at line 170 of file DataUpdate.php.

Definition at line 184 of file DataUpdate.php.

Definition at line 131 of file DataUpdate.php.

Definition at line 150 of file DataUpdate.php.

Definition at line 156 of file DataUpdate.php.


The documentation for this class was generated from the following file: