/** * Returns the SELECT statement as an string according to the database driver * @param $prepared Return a prepared Statement */ public function getInstruction($prepared = FALSE) { $conn = TTransaction::get(); $driver = $conn->getAttribute(PDO::ATTR_DRIVER_NAME); if (in_array($driver, array('mssql', 'dblib', 'sqlsrv'))) { return $this->getSqlServerInstruction($prepared); } if (in_array($driver, array('oci', 'oci8'))) { return $this->getOracleInstruction($prepared); } else { return $this->getStandardInstruction($prepared); } }
/** * Transform the value according to its PHP type * before send it to the database * @param $value Value to be transformed * @param $prepared If the value will be prepared * @return Transformed Value */ private function transform($value, $prepared = FALSE) { // store just scalar values (string, integer, ...) if (is_scalar($value)) { // if is a string if (is_string($value) and !empty($value)) { if ($prepared) { $preparedVar = ':par_' . uniqid(); $this->preparedVars[$preparedVar] = $value; $result = $preparedVar; } else { $conn = TTransaction::get(); $result = $conn->quote($value); } } else { if (is_bool($value)) { $result = $value ? 'TRUE' : 'FALSE'; } else { if ($value !== '') { if ($prepared) { $preparedVar = ':par_' . uniqid(); $this->preparedVars[$preparedVar] = $value; $result = $preparedVar; } else { $result = $value; } } else { $result = "NULL"; } } } } else { if (is_null($value)) { $result = "NULL"; } } return $result; }
/** * Return the amount of objects that satisfy a given criteria * @param $criteria An TCriteria object, specifiyng the filters * @return An Integer containing the amount of objects that satisfy the criteria */ public function count(TCriteria $criteria = NULL) { if (!$criteria) { $criteria = isset($this->criteria) ? $this->criteria : new TCriteria(); } // creates a SELECT statement $sql = new TSqlSelect(); $sql->addColumn('count(*)'); $sql->setEntity($this->getEntity()); // assign the criteria to the SELECT statement $sql->setCriteria($criteria); // get the connection of the active transaction if ($conn = TTransaction::get()) { // register the operation in the LOG file TTransaction::log($sql->getInstruction()); $dbinfo = TTransaction::getDatabaseInfo(); // get dbinfo if (isset($dbinfo['prep']) and $dbinfo['prep'] == '1') { $result = $conn->prepare($sql->getInstruction(TRUE), array(PDO::ATTR_CURSOR => PDO::CURSOR_FWDONLY)); $result->execute($criteria->getPreparedVars()); } else { // executes the SELECT statement $result = $conn->query($sql->getInstruction()); } if ($result) { $row = $result->fetch(); } // returns the result return $row[0]; } else { // if there's no active transaction opened throw new Exception(AdiantiCoreTranslator::translate('No active transactions') . ': ' . __METHOD__ . ' ' . $this->getEntity()); } }
/** * Returns the LAST Object ID from database * @return An Integer containing the LAST Object ID from database * @exception Exception if there's no active transaction opened */ public function getLastID() { $pk = $this->getPrimaryKey(); // get the connection of the active transaction if ($conn = TTransaction::get()) { // instancia instrução de SELECT $sql = new TSqlSelect(); $sql->addColumn("max({$pk}) as {$pk}"); $sql->setEntity($this->getEntity()); // register the operation in the LOG file TTransaction::log($sql->getInstruction()); $result = $conn->Query($sql->getInstruction()); // retorna os dados do banco $row = $result->fetch(); return $row[0]; } else { // if there's no active transaction opened throw new Exception(AdiantiCoreTranslator::translate('No active transactions') . ': ' . __METHOD__ . ' ' . $this->getEntity()); } }