/** * @see Connection::prepareStatement() */ public function prepareStatement($sql) { $this->log("prepareStatement(): {$sql}"); $obj = $this->childConnection->prepareStatement($sql); $objClass = get_class($obj); return new $objClass($this, $sql); }
/** * Write session data. * * @param string A session ID. * @param string A serialized chunk of session data. * * @return bool true, if the session was written, otherwise an exception is * thrown. * * @throws <b>DatabaseException</b> If the session data cannot be written. */ public function sessionWrite($id, $data) { // get table/column $db_table = $this->getParameterHolder()->get('db_table'); $db_data_col = $this->getParameterHolder()->get('db_data_col', 'sess_data'); $db_id_col = $this->getParameterHolder()->get('db_id_col', 'sess_id'); $db_time_col = $this->getParameterHolder()->get('db_time_col', 'sess_time'); $sql = 'UPDATE ' . $db_table . ' SET ' . $db_data_col . '=?, ' . $db_time_col . ' = ' . time() . ' WHERE ' . $db_id_col . '=?'; try { $stmt = $this->db->prepareStatement($sql); $stmt->setString(1, $data); $stmt->setString(2, $id); $stmt->executeUpdate(); return true; } catch (SQLException $e) { $error = 'Creole SQLException was thrown when trying to manipulate session data. '; $error .= 'Message: ' . $e->getMessage(); throw new sfDatabaseException($error); } return false; }
/** * Method to do selects. * * @param Criteria $criteria The Criteria object used to build the SELECT statement. * @param Connection $con * @return array Array of selected Objects * @throws PropelException Any exceptions caught during processing will be * rethrown wrapped into a PropelException. */ public static function doSelect(Criteria $criteria, $tableName, $con = null) { $dbMap = Propel::getDatabaseMap($criteria->getDbName()); $stmt = null; try { $params = array(); $sql = self::createSelectSql($criteria, $tableName, $params); $sql['params'] = $params; $stmt = $con->prepareStatement($sql); //$stmt->setLimit($criteria->getLimit()); $sql['limit'] = $criteria->getLimit(); //$stmt->setOffset($criteria->getOffset()); $sql['offset'] = $criteria->getOffset(); //$rs = $stmt->executeQuery(ResultSet::FETCHMODE_NUM); $rs = $con->executeQuery($sql, ResultSet::FETCHMODE_NUM); } catch (Exception $e) { if ($stmt) { $stmt->close(); } throw new PropelException($e); } return $rs; }
/** * Method used to update rows in the DB. Rows are selected based * on selectCriteria and updated using values in updateValues. * <p> * Use this method for performing an update of the kind: * <p> * WHERE some_column = some value AND could_have_another_column = * another value AND so on. * * @param $selectCriteria A Criteria object containing values used in where * clause. * @param $updateValues A Criteria object containing values used in set * clause. * @param $con The Connection to use. * @return int The number of rows affected by last update statement. For most * uses there is only one update statement executed, so this number * will correspond to the number of rows affected by the call to this * method. Note that the return value does require that this information * is returned (supported) by the Creole db driver. * @throws PropelException */ public static function doUpdate(Criteria $selectCriteria, Criteria $updateValues, Connection $con) { $db = Propel::getDB($selectCriteria->getDbName()); $dbMap = Propel::getDatabaseMap($selectCriteria->getDbName()); // Get list of required tables, containing all columns $tablesColumns = $selectCriteria->getTablesColumns(); // we also need the columns for the update SQL $updateTablesColumns = $updateValues->getTablesColumns(); $affectedRows = 0; // initialize this in case the next loop has no iterations. foreach ($tablesColumns as $tableName => $columns) { $whereClause = array(); $selectParams = array(); foreach ($columns as $colName) { $sb = ""; $selectCriteria->getCriterion($colName)->appendPsTo($sb, $selectParams); $whereClause[] = $sb; } $rs = null; $stmt = null; try { $sqlSnippet = implode(" AND ", $whereClause); if ($selectCriteria->isSingleRecord()) { // Get affected records. $sql = "SELECT COUNT(*) FROM " . $tableName . " WHERE " . $sqlSnippet; $stmt = $con->prepareStatement($sql); self::populateStmtValues($stmt, $selectParams, $dbMap); $rs = $stmt->executeQuery(ResultSet::FETCHMODE_NUM); $rs->next(); if ($rs->getInt(1) > 1) { $rs->close(); throw new PropelException("Expected to update 1 record, multiple matched."); } $rs->close(); } $sql = "UPDATE " . $tableName . " SET "; foreach ($updateTablesColumns[$tableName] as $col) { $sql .= substr($col, strpos($col, '.') + 1) . " = ?,"; } $sql = substr($sql, 0, -1) . " WHERE " . $sqlSnippet; Propel::log($sql, Propel::LOG_DEBUG); $stmt = $con->prepareStatement($sql); // Replace '?' with the actual values self::populateStmtValues($stmt, array_merge(self::buildParams($updateTablesColumns[$tableName], $updateValues), $selectParams), $dbMap); $affectedRows = $stmt->executeUpdate(); $stmt->close(); } catch (Exception $e) { if ($rs) { $rs->close(); } if ($stmt) { $stmt->close(); } Propel::log($e->getMessage(), Propel::LOG_ERR); throw new PropelException("Unable to execute UPDATE statement.", $e); } } // foreach table in the criteria return $affectedRows; }
/** * Saves the data in this Record to the database with an INSERT statement * @return int * @throws DataSetException, SQLException */ private function doInsert(Connection $conn = null) { $stmt = null; try { $stmt = $conn->prepareStatement($this->getInsertSql()); $ps = 1; foreach ($this->dirtyColumns() as $col) { $val = $this->getValue($col); $setter = 'set' . CreoleTypes::getAffix($table->getColumn($col)->getType()); $stmt->{$setter}($ps++, $val); } $ret = $stmt->executeUpdate(); if ($this->ds->refreshOnSave()) { $this->refresh(); } else { // Marks all of the values clean since they have now been saved $this->markRecordClean(); } $this->setSaveType(Record::AFTERINSERT); if ($ret > 1) { // a little late again... throw new SQLException("There were " . $ret . " rows inserted with this records key value."); } return $ret; } catch (SQLException $e) { if ($stmt) { $stmt->close(); } throw $e; } }