function test_query_builder_select_num_rows() { $builder = new Dataface_QueryBuilder('Profiles'); $this->assertEquals($builder->select_num_rows(), 'SELECT COUNT(*) FROM `Profiles`'); $this->assertEquals($builder->select_num_rows(array('phone1' => '555-555-5555')), 'SELECT COUNT(*) FROM `Profiles` WHERE `Profiles`.`phone1` LIKE \'%555-555-5555%\''); $this->assertEquals($builder->select_num_rows(array('phone1' => '555-555-5555', '-skip' => '2')), 'SELECT COUNT(*) FROM `Profiles` WHERE `Profiles`.`phone1` LIKE \'%555-555-5555%\''); $this->assertEquals($builder->select_num_rows(array('phone1' => '555-555-5555', '-skip' => '2', '-limit' => 10)), 'SELECT COUNT(*) FROM `Profiles` WHERE `Profiles`.`phone1` LIKE \'%555-555-5555%\''); }
function found() { if (!isset($this->_data['found'])) { $cache =& $this->staticCache(); $builder = new Dataface_QueryBuilder($this->_tablename, $this->_query); $sql = $builder->select_num_rows(); if (isset($cache[$sql])) { $this->_data['found'] = $cache[$sql]; } else { $res = $this->dbObj->query($sql, $this->_db, null, true); $this->_data['found'] = array_shift($res[0]); $cache[$sql] = $this->_data['found']; } } return $this->_data['found']; }
/** * Returns true if the record currently represented in the Table already exists * in the database. * * @param tablename Alternative table where records may be stored. This is useful if we are reading form import or delete tables. * */ function recordExists(&$record, $keys = null, $tablename = null) { if (!is_a($record, "Dataface_Record")) { trigger_error(df_translate('scripts.Dataface.IO.recordExists.ERROR_PARAMETER_1', "In Dataface_IO::recordExists() the first argument is expected to be either a 'Dataface_Record' object or an array of key values, but received neither.\n<br>") . Dataface_Error::printStackTrace(), E_USER_ERROR); } if ($tablename === null and $this->_altTablename !== null) { $tablename = $this->_altTablename; } $tempRecordCreated = false; if ($record->snapshotExists()) { $tempRecord = new Dataface_Record($record->_table->tablename, $record->getSnapshot()); $tempRecordCreated = true; } else { $tempRecord =& $record; } if ($keys == null) { // Had to put in userialize(serialize(...)) because getValues() returns by reference // and we don't want to change actual values. $query = unserialize(serialize($tempRecord->getValues(array_keys($record->_table->keys())))); } else { $query = $keys; } $table_keys = array_keys($this->_table->keys()); foreach ($table_keys as $key) { if (!isset($query[$key]) or !$query[$key]) { //trigger_error("In Dataface_IO::recordExists()d attempt to determine if a record exists, not enough information was given. At least ALL of the keys of the table in question ('".$this->_table->tablename."') must be included in the provided Dataface_Record object (or query array), but some keys (notably '$key') are missing. Impossible to determine existence unless all key fields are provided.\n<br>".Dataface_Error::printStackTrace(), E_USER_ERROR); return false; } } foreach (array_keys($query) as $key) { $query[$key] = '=' . $this->_serializer->serialize($key, $tempRecord->getValue($key)); } $qb = new Dataface_QueryBuilder($this->_table->tablename, $query); $sql = $qb->select_num_rows(array(), $this->tablename($tablename)); $res = mysql_query($sql, $this->_table->db); // We just use regular mysql query to see if record exists because this should be sufficient //$res = $this->dbObj->query($sql, $this->_table->db, $this->lang); if (!$res || PEAR::isError($res)) { die("SQL error in {$sql} : " . mysql_error($this->_table->db) . Dataface_Error::printStackTrace()); } list($rows) = mysql_fetch_row($res); mysql_free_result($res); if ($rows > 1) { $err = PEAR::raiseError(Dataface_LanguageTool::translate('recordExists failure. Too many rows returned.', "Test for existence of record in recordExists() returned {$rows} records. \n\t\t\t\t\tIt should have max 1 record. \n\t\t\t\t\tThe query must be incorrect. \n\t\t\t\t\tThe query used was '{$sql}'. On line " . __LINE__ . " of file " . __FILE__, array('table' => $this->_table->tablename, 'line' => __LINE__, 'file' => __FILE__, 'sql' => $sql)), DATAFACE_E_IO_ERROR); trigger_error($err->toString() . "\n<br>" . Dataface_Error::printStackTrace()); } if ($tempRecordCreated) { $tempRecord->__destruct(); } return intval($rows) === 1; }
function display() { $this->_build(); $showform = true; $b = new Dataface_QueryBuilder($this->_tablename, $this->_query); if (isset($this->_query['-delete-one'])) { $q = array('-skip' => $this->_query['-cursor'], '-limit' => 1); $sql = $b->select('', $q); $res = xf_db_query($sql, $this->_db); if (!$res) { throw new Exception(df_translate('scripts.Dataface.DeleteForm._build.ERROR_TRYING_TO_FETCH', "Error trying to fetch element to be deleted.: ") . xf_db_error($this->_db), E_USER_ERROR); } if (xf_db_num_rows($res) == 0) { $msg = df_translate('scripts.Dataface.DeleteForm._build.ERROR_NO_RECORD_SELECTED', "No record is currently selected so no record can be deleted."); $showform = false; } else { $row = xf_db_fetch_array($res); $rowRec = new Dataface_Record($this->_tablename, $row); $displayCol = $rowRec->getTitle(); $msg = df_translate('scripts.Dataface.DeleteForm.display.ARE_YOU_SURE', "Are you sure you want to delete this record: "{$displayCol}"?", array('displayCol' => $displayCol)); } } else { if (isset($this->_query['-delete-found'])) { $q = $b->select_num_rows(); $res = xf_db_query($q, $this->_db); if (!$res) { throw new Exception(df_translate('scripts.Dataface.DeleteForm.display.ERROR_ESTIMATING', "Error estimating number of rows that will be deleted: ") . xf_db_error($this->_db), E_USER_ERROR); } list($num) = xf_db_fetch_row($res); if ($num <= 0) { $msg = df_translate('scripts.Dataface.DeleteForm.display.ERROR_NO_RECORDS_FOUND', "There are no records in the current found set so no records can be deleted."); $showform = false; } else { $msg = df_translate('scripts.Dataface.DeleteForm.display.ARE_YOU_SURE_MULTIPLE', "Are you sure you want to delete the found records. {$num} records will be deleted.", array('num' => $num)); } } else { $msg = df_translate('scripts.Dataface.DeleteForm.display.ERROR_GET_VARS', "Error: You must specify either '-delete-one' or '-delete-found' in GET vars."); $showform = false; } } if ($showform) { ob_start(); parent::display(); $form = ob_get_contents(); ob_end_clean(); } else { $form = ''; } $context = array('msg' => $msg, 'form' => $form); import('Dataface/SkinTool.php'); $skinTool =& Dataface_SkinTool::getInstance(); //$smarty = new Smarty; //$smarty->template_dir = $GLOBALS['Dataface_Globals_Templates']; //$smarty->compile_dir = $GLOBALS['Dataface_Globals_Templates_c']; //$smarty->assign($context); //$smarty->display('Dataface_DeleteForm.html'); $skinTool->display($context, 'Dataface_DeleteForm.html'); }