Source for file database.php
Documentation is available at database.php
* dbscript for PHP 4 & 5 - restful crud framework
* @version 0.1.2 -- 19-Feb-2007
* @author Brian Hendrickson <brian@dbscript.net>
* @link http://dbscript.net/
* @copyright Copyright 2007 Brian Hendrickson
* @license http://www.opensource.org/licenses/mit-license.php MIT License
* Connects to the database, fetches records
* into data objects and performs CRUD operations.
* {@link http://dbscript.net/database}
* @author Brian Hendrickson <brian@dbscript.net>
* connection resource used to access the database
* true if the database is connected
* maximum binary file size
* poss. values for boolean true
* return a Record object for the named table
* @author Brian Hendrickson <brian@dbscript.net>
if (count($func_args) > 0 && count($func_args) < 4) {
if (isset ($func_args[2])) {
return new Record($table,$this,$func_args[1],$func_args[2]);
} elseif (isset ($func_args[1])) {
return new Record($table,$this,$func_args[1]);
return new Record($table,$this);
* return a Record object from the Model's active result set
* @author Brian Hendrickson <brian@dbscript.net>
if ($this->models[$table]->custom_class) {
$custom_class = $this->models[$table]->custom_class;
$rec = new $custom_class( $this->models[$table] );
if (!$rec) trigger_error( "error instantiating $custom_class", E_USER_ERROR );
$rec->Record($table,$this);
foreach ($rec->relationships as $key=> $val) {
$this->models[$table]->set_relation( $key, $val );
if ( isset ( $rs->relations[$fields[$rec->primary_key]] ) ) {
foreach ( $rs->relations[$fields[$rec->primary_key]] as $reltable=> $relpkvalue ) {
$rec->children[$reltable] = $relpkvalue;
$rec->attributes[$rec->primary_key] = $fields[$rec->primary_key];
$primary_key = $rec->primary_key;
$rec->$primary_key = & $rec->attributes[$rec->primary_key];
* return a multi-graph RecordSet from a SQORP-formatted SQL join query
* @author Brian Hendrickson <brian@dbscript.net>
* @author Brian Hendrickson <brian@dbscript.net>
* @param string[] blob_location
* @param string content_type
$url = "?action=get_file";
$url .= "&i=" . $blob_location['id'];
$url .= "&k=" . $blob_location['primary_key'];
$url .= "&t=" . $blob_location['table'];
$url .= "&f=" . $blob_location['field'];
$url .= "&o=" . $blob_location;
* set the file upload size limit
* @author Brian Hendrickson <brian@dbscript.net>
* @param integer megabytes
* create a skeleton from an existing Record
* @author Brian Hendrickson <brian@dbscript.net>
foreach ($rec->attributes as $key=> $val) {
$fields[$rec->primary_key] = 0;
$skeleton->is_skeleton();
* set the file upload size limit
* @author Brian Hendrickson <brian@dbscript.net>
if (!(strlen($orderby) > 0)) { $orderby = $field; }
$sql = $this->select_distinct( $field, $table, $orderby );
$result = $this->get_result($sql);
if (!($this->num_rows($result) > 0)) {
while ( $row = $this->fetch_array( $result ) ) {
$values[$row[$this->models[$table]->primary_key]] = $row[$field];
* set the file upload size limit
* @author Brian Hendrickson <brian@dbscript.net>
$sql = $this->sql_select_for( $rec, $id );
$result = $this->get_result($sql);
if (!($this->num_rows($result) > 0)) {
$pkfield = $rec->primary_key;
$rec->attributes[$pkfield] = $id;
$field_array = $this->fetch_array($result);
* return the abstract type for a given native type
* @author Brian Hendrickson <brian@dbscript.net>
* @param string raw_datatype
if (strstr($raw_datatype,"(")) {
$raw_datatype = substr($raw_datatype, 0, strpos($raw_datatype,"("));
trigger_error( "Error, the $raw_datatype datatype is not listed in dbscript's datatype map.", E_USER_NOTICE );
* return the data model objects in an array
* @author Brian Hendrickson <brian@dbscript.net>
$skip = array( '.', '..' );
while ( false !== ( $file = readdir( $handle ))) {
$objects[$o] = & $this->get_table( tableize($o) );
foreach($objects as $name=> $model)
* return whether a table exists
* @author Brian Hendrickson <brian@dbscript.net>
return isset ( $this->data_models[$table] );
* populate a data object's attributes array
* @author Brian Hendrickson <brian@dbscript.net>
foreach ($fields as $field=> $value) {
if ($field == $rec->primary_key && $value == "") $value = 0;
if ($datatype == 'blob') {
$value = $this->blob_value( $rec, $field, $value ); // oid (pgsql) or array (mysql)
if ($datatype == 'bool') {
$rec->attributes[$field] = $value;
if ( !( isset ( $rec->$field ) ) )
$rec->$field = & $rec->attributes[$field];
if (!(in_array($rec->attributes[$rec->primary_key], array('',0), true)))
$rec->table = $rec->table;
$rec->exists = $rec->exists;
if (!(isset ($this->models[$table]))) {
$fields = $this->get_fields( $table );
if (isset ($fields[$table. "_primary_key"]))
$pk = $fields[$table. "_primary_key"];
$pk = $this->models[$table]->primary_key;
* save a record's attributes into the database
* @author Brian Hendrickson <brian@dbscript.net>
if ( !$rec->modified_fields && ( $rec->exists )) {
return true; // nothing to save!
$rec->set_value( 'last_modified', timestamp() );
$rec->set_value( 'modified_at', timestamp() );
$rec->set_value( 'access_time', timestamp() );
$result = $this->get_result( $this->sql_update_for( $rec ));
$rec->set_value( 'created_at', timestamp() );
$result = $this->get_result( $this->sql_insert_for( $rec ));
$this->post_insert( $rec, $result );
$rec->modified_fields = array();
* save a record's attributes into the database
* @author Brian Hendrickson <brian@dbscript.net>
if (strlen($rec->attributes[$rec->primary_key]) > 0) {
$result = $this->get_result( $this->sql_delete_for( $rec ) );
|