/**
  * Binds a PHP variable to a corresponding named or question mark placeholder in the SQL statement
  * that was use to prepare the statement. Unlike PDOStatement::bindValue(), the variable is bound
  * as a reference and will only be evaluated at the time that PDOStatement::execute() is called.
  * Returns a boolean value indicating success.
  *
  * @param     integer  $pos  Parameter identifier (for determining what to replace in the query).
  * @param     mixed    $value  The value to bind to the parameter.
  * @param     integer  $type  Explicit data type for the parameter using the PDO::PARAM_* constants. Defaults to PDO::PARAM_STR.
  * @param     integer  $length  Length of the data type. To indicate that a parameter is an OUT parameter from a stored procedure, you must explicitly set the length.
  * @param     mixed    $driver_options
  *
  * @return    boolean
  */
 public function bindParam($pos, &$value, $type = PDO::PARAM_STR, $length = 0, $driver_options = null)
 {
     $debug = $this->pdo->getDebugSnapshot();
     $typestr = isset(self::$typeMap[$type]) ? self::$typeMap[$type] : '(default)';
     $return = parent::bindParam($pos, $value, $type, $length, $driver_options);
     $valuestr = $length > 100 ? '[Large value]' : var_export($value, true);
     $msg = sprintf('Binding %s at position %s w/ PDO type %s', $valuestr, $pos, $typestr);
     $this->boundValues[$pos] = $valuestr;
     $this->pdo->log($msg, null, __METHOD__, $debug);
     return $return;
 }
 public function build($dsn = null, $user = null, $pass = null, $adapter = null)
 {
     if (null === $dsn) {
         $dsn = 'sqlite::memory:';
     }
     if (null === $adapter) {
         $adapter = new \Propel\Runtime\Adapter\DBSQLite();
     }
     $con = new PropelPDO($dsn, $user, $pass);
     $con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
     $this->buildSQL($con);
     $this->buildClasses();
     $name = $this->getDatabase()->getName();
     if (!Propel::isInit()) {
         Propel::setConfiguration(array('datasources' => array('default' => $name)));
     }
     Propel::setDB($name, $adapter);
     Propel::setConnection($name, $con, Propel::CONNECTION_READ);
     Propel::setConnection($name, $con, Propel::CONNECTION_WRITE);
     return $con;
 }
Example #3
0
 /**
  * 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      PropelPDO $con The PropelPDO connection object 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 Propel db driver.
  * @throws     PropelException
  */
 public static function doUpdate(Criteria $selectCriteria, Criteria $updateValues, PropelPDO $con)
 {
     $db = Propel::getDB($selectCriteria->getDbName());
     $dbMap = Propel::getDatabaseMap($selectCriteria->getDbName());
     // Get list of required tables, containing all columns
     $tablesColumns = $selectCriteria->getTablesColumns();
     if (empty($tablesColumns)) {
         $tablesColumns = array($selectCriteria->getPrimaryTableName() => array());
     }
     // 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();
         $params = array();
         $stmt = null;
         try {
             $sql = 'UPDATE ';
             if ($queryComment = $selectCriteria->getComment()) {
                 $sql .= '/* ' . $queryComment . ' */ ';
             }
             // is it a table alias?
             if ($tableName2 = $selectCriteria->getTableForAlias($tableName)) {
                 $udpateTable = $tableName2 . ' ' . $tableName;
                 $tableName = $tableName2;
             } else {
                 $udpateTable = $tableName;
             }
             if ($db->useQuoteIdentifier()) {
                 $sql .= $db->quoteIdentifierTable($udpateTable);
             } else {
                 $sql .= $udpateTable;
             }
             $sql .= " SET ";
             $p = 1;
             foreach ($updateTablesColumns[$tableName] as $col) {
                 $updateColumnName = substr($col, strrpos($col, '.') + 1);
                 // add identifiers for the actual database?
                 if ($db->useQuoteIdentifier()) {
                     $updateColumnName = $db->quoteIdentifier($updateColumnName);
                 }
                 if ($updateValues->getComparison($col) != Criteria::CUSTOM_EQUAL) {
                     $sql .= $updateColumnName . '=:p' . $p++ . ', ';
                 } else {
                     $param = $updateValues->get($col);
                     $sql .= $updateColumnName . ' = ';
                     if (is_array($param)) {
                         if (isset($param['raw'])) {
                             $raw = $param['raw'];
                             $rawcvt = '';
                             // parse the $params['raw'] for ? chars
                             for ($r = 0, $len = strlen($raw); $r < $len; $r++) {
                                 if ($raw[$r] == '?') {
                                     $rawcvt .= ':p' . $p++;
                                 } else {
                                     $rawcvt .= $raw[$r];
                                 }
                             }
                             $sql .= $rawcvt . ', ';
                         } else {
                             $sql .= ':p' . $p++ . ', ';
                         }
                         if (isset($param['value'])) {
                             $updateValues->put($col, $param['value']);
                         }
                     } else {
                         $updateValues->remove($col);
                         $sql .= $param . ', ';
                     }
                 }
             }
             $params = self::buildParams($updateTablesColumns[$tableName], $updateValues);
             $sql = substr($sql, 0, -2);
             if (!empty($columns)) {
                 foreach ($columns as $colName) {
                     $sb = "";
                     $selectCriteria->getCriterion($colName)->appendPsTo($sb, $params);
                     $whereClause[] = $sb;
                 }
                 $sql .= " WHERE " . implode(" AND ", $whereClause);
             }
             $db->cleanupSQL($sql, $params, $updateValues, $dbMap);
             $stmt = $con->prepare($sql);
             // Replace ':p?' with the actual values
             $db->bindValues($stmt, $params, $dbMap, $db);
             $stmt->execute();
             $affectedRows = $stmt->rowCount();
             $stmt = null;
             // close
         } catch (Exception $e) {
             if ($stmt) {
                 $stmt = null;
             }
             // close
             Propel::log($e->getMessage(), Propel::LOG_ERR);
             throw new PropelException(sprintf('Unable to execute UPDATE statement [%s]', $sql), $e);
         }
     }
     // foreach table in the criteria
     return $affectedRows;
 }
 public function delete(PropelPDO $con = null)
 {
     $con->beginTransaction();
     try {
         TestableAggregateCommentQuery::create()->filterByPrimaryKey($this->getPrimaryKey())->delete($con);
         $con->commit();
         $this->setDeleted(true);
     } catch (PropelException $e) {
         $con->rollBack();
         throw $e;
     }
 }