function DB_Test($name = 'DB_Test') { $this->BaseTest($name); Dataface_Application::getInstance(); $this->DB =& Dataface_DB::getInstance(); //parent::BaseTest(); }
function Dataface_IO($tablename, $db = null, $altTablename = null) { $app =& Dataface_Application::getInstance(); $this->lang = $app->_conf['lang']; $this->_table =& Dataface_Table::loadTable($tablename, $db); $this->_serializer = new Dataface_Serializer($tablename); $this->_altTablename = $altTablename; $this->dbObj =& Dataface_DB::getInstance(); }
function getTotalRecords() { if (!isset($this->_totalRecords)) { $db =& Dataface_DB::getInstance(); $res = $db->query($this->_queryBuilder->select_num_rows(), $this->_table->db, null, true); $this->_totalRecords = $res[0]['num']; //list($this->_totalRecords) = xf_db_fetch_row( xf_db_query( $this->_queryBuilder->select_num_rows() ) ); } return $this->_totalRecords; }
/** * Creates a new query tool. * @param $tablename The name of the table on which this query is based. * @param $db The database handle. * @param $query Associative array of query parameters. */ function Dataface_QueryTool($tablename, $db = null, $query = null) { $this->dbObj =& Dataface_DB::getInstance(); $this->_tablename = $tablename; if (!is_array($query)) { $query = array(); } if ($db === null) { $db = $this->dbObj->_db; } $this->_db = $db; $this->_query = $query; $this->_table =& Dataface_Table::loadTable($tablename); $this->_data = array(); if (isset($query['-cursor'])) { $this->_data['cursor'] = $query['-cursor']; } else { $this->_data['cursor'] = 0; } if (isset($query['-skip'])) { $this->_data['start'] = $query['-skip']; } else { $this->_data['start'] = 0; } if (isset($query['-limit'])) { $this->_data['end'] = $this->_data['start'] + $query['-limit'] - 1; $this->_data['limit'] = $query['-limit']; } else { $this->_data['end'] = $this->_data['start'] + $GLOBALS['Dataface_QueryTool_limit'] - 1; $this->_data['limit'] = $GLOBALS['Dataface_QueryTool_limit']; } $tableKeyNames = array_keys($this->_table->keys()); if (count($tableKeyNames) <= 0) { throw new Exception("The table '{$tablename}' has no primary key. Please add one.", E_USER_ERROR); } $firstKeyName = $tableKeyNames[0]; $cache =& $this->staticCache(); $sql = "select count(`{$firstKeyName}`) from `{$tablename}`"; if (isset($cache[$sql])) { $this->_data['cardinality'] = $cache[$sql]; } else { $res = $this->dbObj->query($sql, $this->_db, null, true); if (!$res and !is_array($res)) { throw new Exception("We had a problem with the query {$sql}.", E_USER_ERROR); } $this->_data['cardinality'] = reset($res[0]); $cache[$sql] = $this->_data['cardinality']; } $builder = new Dataface_QueryBuilder($tablename, $this->_query); $builder->selectMetaData = true; $sql = $builder->select_num_rows(); if (isset($cache[$sql])) { $this->_data['found'] = $cache[$sql]; } else { $res = $this->dbObj->query($sql, $this->_db, null, true); if (!$res and !is_array($res)) { throw new Exception(xf_db_error($this->_db) . $sql, E_USER_ERROR); } $this->_data['found'] = array_shift($res[0]); //xf_db_fetch_row( $res ); $cache[$sql] = $this->_data['found']; } if ($this->_data['end'] > $this->_data['found'] - 1) { $this->_data['end'] = $this->_data['found'] - 1; } if ($this->_data['start'] > $this->_data['found']) { $this->_data['start'] = $this->_data['found']; } }
function insert(&$record, $tablename = null) { $app =& Dataface_Application::getInstance(); $this->action = 'insert'; if (!is_a($record, "Dataface_Record")) { throw new Exception("First argument to QueryBuilder::insert() must be of type Dataface_Record, but received " . get_class($record), E_USER_ERROR); } // the keys are not complete... so this item does not exist.. create new record. $tableObj =& Dataface_Table::loadTable($this->tablename($tablename)); $dbObj =& Dataface_DB::getInstance(); $fields = array_keys($this->_mutableFields); $keys =& $this->_table->keys(); $insertedKeys = array(); $insertedValues = array(); foreach ($this->_mutableFields as $key => $field) { if (@$field['ignore']) { continue; } if ($tableObj->isDate($key)) { // We must take special care for dates. if (isset($fieldArr)) { unset($fieldArr); } $fieldArr =& $tableObj->getField($key); if (isset($fieldArr['timestamp']) and in_array(strtolower($fieldArr['timestamp']), array('insert', 'update'))) { $insertedKeys[] = '`' . $key . '`'; $insertedValues[] = 'NOW()'; continue; } } if (!$record->hasValue($key)) { continue; } $val = $record->getValue($key); if (strtolower($this->_mutableFields[$key]['Extra']) == 'auto_increment' && !$val) { // This is a MySQL 5 fix. In MySQL 5 it doesn't like it when you put blank values into // auto increment fields. continue; } if (!isset($val)) { continue; } $sval = $this->_serializer->serialize($key, $record->getValue($key)); //if ( !$field['value'] && in_array($key, array_keys($keys)) ) continue; if ($tableObj->isBlob($key) and @$app->_conf['multilingual_content']) { $blobID = $dbObj->registerBlob($sval); $sval2 = "-=-=B" . $blobID . "=-=-"; } else { $sval2 = $sval; } if (strlen(strval($sval2)) == 0 and strtolower($this->_mutableFields[$key]['Null']) == 'yes') { $insertedKeys[] = '`' . $key . '`'; $insertedValues[] = 'NULL'; //$sql .= 'NULL,'; } else { $insertedKeys[] = '`' . $key . '`'; $insertedValues[] = $this->prepareValue($key, $sval2); //$sql .= "'".addslashes($sval2)."',"; } } $sql = "INSERT INTO `" . $this->tablename($tablename) . "` (" . implode(',', $insertedKeys) . ') VALUES (' . implode(',', $insertedValues) . ')'; $this->action = null; return $sql; }
/** * Reads a record from the database and returns it as a Dataface_ViewRecord * object. * @param $params Associative array of key/value pairs to search. * */ function getRecord($params = array()) { $data = $this->_parseSQL(); $wrapper = new SQL_Parser_wrapper($data); $where = array(); foreach ($params as $key => $value) { $tablename = $this->getTableName($key); if (isset($tablename)) { $where[] = '`' . addslashes($tablename) . '`.`' . $key . '`=\'' . addslashes($params[$key]) . '\''; } } $where = implode(' AND ', $where); $wrapper->addWhereClause($where); $compiler =& $this->_getCompiler(); $sql = $compiler->compile($data); $db =& Dataface_DB::getInstance(); $res = $db->query($sql); if (PEAR::isError($res)) { return $res; } if (!$res) { return PEAR::raiseError(mysql_error($this->app->_db)); } if (mysql_num_rows($res) == 0) { return null; } $vals = mysql_fetch_assoc($res); mysql_free_result($res); return $this->newRecord($vals); }
/** * Backs up a record to the history table. This will automatically happen * when using Dataface_IO::save() if the [history] section exists in the * conf.ini file. * * @param Dataface_Record &$record The record that is being backed up. * @param string $comments Comments about this version to be stored. * @param string $lang The 2-digit language code of which language * to use to back up this record. If none is specified * then the current language of the system will be used. * @param integer $state Unused as yet. Was intended to store state/workflow * information.. but .. * @returns integer The history id of the resulting history record. */ function logRecord(&$record, $comments = '', $lang = null, $state = null) { $app =& Dataface_Application::getInstance(); if (!isset($lang)) { $lang = $app->_conf['lang']; } if (!isset($state)) { $state = 0; } $fieldnames = array_keys($record->_table->fields()); $sql = 'select `' . implode('`,`', $fieldnames) . '` from `' . $record->_table->tablename . '` where'; $keynames = array_keys($record->_table->keys()); $where_clauses = array(); foreach ($keynames as $keyname) { $where_clauses[] = '`' . $keyname . '`=\'' . addslashes($record->strval($keyname)) . '\''; } $sql .= ' ' . implode(' and ', $where_clauses); if (@$app->_conf['multilingual_content']) { $db =& Dataface_DB::getInstance(); $sql = $db->translate_query($sql, $lang); $sql = $sql[0]; } $auth =& Dataface_AuthenticationTool::getInstance(); $userRecord =& $auth->getLoggedInUser(); if (!isset($userRecord)) { $user = null; } else { $user = $auth->getLoggedInUsername(); } $insertsql = "insert into `" . $this->logTableName($record->_table->tablename) . "` \n\t\t\t(`" . implode('`,`', $fieldnames) . "`, `history__language`,`history__comments`,`history__user`,`history__state`,`history__modified`) \n\t\t\tselect *, '" . addslashes($lang) . "','" . addslashes($comments) . "','" . addslashes($user) . "','" . addslashes($state) . "', NOW() \n\t\t\tfrom (" . $sql . ") as t"; $res = xf_db_query($insertsql, $app->db()); if (!$res) { $this->updateHistoryTable($record->_table->tablename); $res = xf_db_query($insertsql, $app->db()); } if (!$res) { echo $insertsql; trigger_error(xf_db_error($app->db()), E_USER_ERROR); } // Now for the individual fields $hid = xf_db_insert_id($app->db()); foreach ($fieldnames as $fieldname) { $this->logField($record, $fieldname, $hid); } return $hid; }
/** * @brief Returns the total number of related records for a given relationship. * * @section Examples * @subsection default_usage Default Usage * @code * if ( $record->numRelatedRecords('books') > 0 ){ * echo "There are ".$record->numRelatedRecords('books')." books."; * } else { * echo "There are no books."; * } * @endcode * * @subsection where_clause Using 'Where' Clause * * The following example counts the number of books in the relationship that * where published in 1986. * @code * $numBooksIn1986 = $record->numRelatedRecords('books', "year='1986'"); * @endcode * * @param string $relname The relationship name. * @param mixed $where * (optional) String where clause that can be used to filter the records. * * @return Integer number of records in this relationship. * * @since 0.5 * * @see http://xataface.com/documentation/tutorial/getting_started/relationships * @see http://www.xataface.com/wiki/relationships.ini_file */ function numRelatedRecords($relname, $where = 0) { if (!isset($this->_numRelatedRecords[$relname][$where])) { $relationship =& $this->_table->getRelationship($relname); //if ( $where !== 0 ){ $sql = $this->parseString($relationship->getSQL($this->loadBlobs, $where)); //} else { // $sql = $this->parseString($relationship->_schema['sql']); //} $sql = stristr($sql, ' FROM '); $sql = "SELECT COUNT(*) as num" . $sql; //$dbObj = //$res = mysql_query($sql, $this->_table->db); //$res = mysql_query($sql, $this->_table->db); $db =& Dataface_DB::getInstance(); $res = $db->query($sql, $this->_table->db, null, true); if (!$res and !is_array($res)) { //if ( !$res ){ trigger_error(df_translate('scripts.Dataface.Record.numRelatedRecords.ERROR_CALCULATING_NUM_RELATED_RECORDS', "Error calculating the number of related records there are for the relationship '{$relname}' in the table '" . $this->_table->tablename . "'. There was a problem performing the sql query '{$sql}'. The MYSQL error returned was '" . mysql_error($this->_table->db) . "'.\n<br>", array('relationship' => $relname, 'table' => $this->_table->tablename, 'mysql_error' => mysql_error($this->_table->db), 'sql' => $sql)) . Dataface_Error::printStackTrace(), E_USER_ERROR); } $this->_numRelatedRecords[$relname][$where] = $res[0]['num']; } return $this->_numRelatedRecords[$relname][$where]; }
function df_insert_id() { import('Dataface/DB.php'); $db = Dataface_DB::getInstance(); return $db->insert_id(); }
function save() { $db =& Dataface_DB::getInstance(); $db->startTransaction(); $formTool =& Dataface_FormTool::getInstance(); foreach ($this->getFieldDefs() as $uri => $fieldDef) { $record =& $this->getRecord($uri); $formTool->pushField($record, $fieldDef, $this, $uri); if ($record->valueChanged($fieldDef['name'])) { $this->changed_fields[] = $uri; } } foreach (array_keys($this->records) as $uri) { $res = $this->records[$uri]->save(null, true); if (PEAR::isError($res)) { $db->rollbackTransaction(); return $res; } } $db->commitTransaction(); return true; }