Ejemplo n.º 1
0
 function __execute($injected_sql = false, $injected_arguments = array(), $ctor_args = null)
 {
     $sql = $injected_sql ? $injected_sql : $this->__toString();
     if ($injected_arguments) {
         if (is_array($injected_arguments)) {
             $this->_values = $injected_arguments;
         } else {
             $this->_values = array($injected_arguments);
         }
     }
     $this->_statement = $this->_ds->Prepare($sql);
     foreach ($this->_values as $i => $v) {
         if (is_integer($v)) {
             $this->_statement->bindValue($i + 1, $v, PDO::PARAM_INT);
         } elseif ($v instanceof DateTime) {
             $this->_statement->bindValue($i + 1, $v->format("c"));
         } else {
             $this->_statement->bindValue($i + 1, $v);
         }
     }
     if (!$this->_statement->execute()) {
         WdfDbException::Raise($this->_statement->ErrorOutput(), "\nArguments:", $this->_values, "\nMerged:", ResultSet::MergeSql($this->_ds, $sql, $this->_values));
     }
     $res = $this->_statement->fetchAll(PDO::FETCH_CLASS, get_class($this->_object), $ctor_args);
     return $res;
 }
Ejemplo n.º 2
0
 /**
  * @implements <IDatabaseDriver::createTable>
  */
 function createTable($objSchema)
 {
     $sql = array();
     foreach ($objSchema->Columns as $col) {
         $sql[] = $this->columnDef($col);
     }
     $sql = 'CREATE TABLE "' . $objSchema->Table . '" (' . "\n" . implode(",\n", $sql) . "\n" . ')';
     $stmt = $this->_pdo->prepare($sql);
     //,array(PDO::ATTR_CURSOR=>PDO::CURSOR_SCROLL));
     if (!$stmt->execute()) {
         WdfDbException::Raise($stmt->errorInfo());
     }
 }
Ejemplo n.º 3
0
 /**
  * @implements <IDatabaseDriver::tableExists>
  */
 function tableExists($tablename)
 {
     $sql = 'SHOW TABLES LIKE ?';
     $stmt = $this->_pdo->prepare($sql);
     //,array(PDO::ATTR_CURSOR=>PDO::CURSOR_SCROLL));
     $stmt->setFetchMode(PDO::FETCH_NUM);
     $stmt->bindValue(1, $tablename);
     if (!$stmt->execute()) {
         WdfDbException::Raise($stmt->errorInfo());
     }
     $row = $stmt->fetch();
     return count($row) > 0;
 }
Ejemplo n.º 4
0
 /**
  * Executes an SQL statement.
  * 
  * @param string $sql SQL statement
  * @param array $parameter Arguments
  * @return ResultSet The query result
  */
 function ExecuteSql($sql, $parameter = array())
 {
     if (!is_array($parameter)) {
         $parameter = array($parameter);
     }
     $stmt = $this->Prepare($sql);
     if (!$stmt->execute($parameter)) {
         WdfDbException::Raise("SQL Error: " . $stmt->ErrorOutput(), "\nArguments:", $parameter, "\nMerged:", ResultSet::MergeSql($this, $sql, $parameter));
     }
     $this->_last_affected_rows_count = $stmt->Count();
     return $stmt;
 }
Ejemplo n.º 5
0
 /**
  * Saves this model to the database.
  * 
  * New datasets will be inserted, loaded ones will be updated automatically.
  * If $columns_to_update is given only those columns will be stored. This may be useful to avoid DB conflicts in multithread scenarios.
  * @param array $columns_to_update If given only these fields will be updated. If not Model tries to detect changed columns automatically.
  * @return boolean In fact always true, WdfDbException will be thrown in error case
  */
 public function Save($columns_to_update = false)
 {
     $args = array();
     $stmt = $this->_ds->Driver->getSaveStatement($this, $args, $columns_to_update);
     if (!$stmt) {
         return true;
     }
     // nothing to save
     if (!$stmt->execute($args)) {
         WdfDbException::Raise(render_var($stmt->ErrorOutput()));
     }
     $pkcols = $this->GetPrimaryColumns();
     if (count($pkcols) == 1) {
         $id = $pkcols[0];
         if (!isset($this->{$id})) {
             $this->{$id} = $this->_ds->LastInsertId();
         }
     }
     $this->__init_db_values();
     return true;
 }
Ejemplo n.º 6
0
/**
 * Get a database connection.
 * 
 * @param string $name The datasource alias.
 * @return DataSource The database connection
 */
function &model_datasource($name)
{
    global $MODEL_DATABASES;
    if (strpos($name, "DataSource::") !== false) {
        $name = explode("::", $name);
        $name = $name[1];
    }
    if (!isset($MODEL_DATABASES[$name])) {
        if (function_exists('model_on_unknown_datasource')) {
            $res = model_on_unknown_datasource($name);
            return $res;
        }
        log_fatal("Unknown datasource '{$name}'!");
        $res = null;
        return $res;
    }
    if (is_array($MODEL_DATABASES[$name])) {
        list($dstype, $constr) = $MODEL_DATABASES[$name];
        $dstype = fq_class_name($dstype);
        $model_db = new $dstype($name, $constr);
        if (!$model_db) {
            WdfDbException::Raise("Unable to connect to database '{$name}'.");
        }
        $MODEL_DATABASES[$name] = $model_db;
    }
    return $MODEL_DATABASES[$name];
}