Exemple #1
0
 public function add($data)
 {
     // INSERT INTO {TABLE} (`col1`, `col2`, `col3`) VALUES (val1, val2, val3);
     $sql = "INSERT INTO `{$this->table}` ";
     $sql .= '(`' . implode('`, `', array_keys($data)) . '`)';
     $sql .= ' VALUES ';
     $sql .= "('" . implode("', '", array_values($data)) . "')";
     $result = $this->db->query($sql);
     if ($result === false) {
         $this->db->error();
     }
     return $this->db->lastId();
 }
 public static function create()
 {
     $form = self::getPostForm();
     if (Helpers::isMethod("post")) {
         $form->setValues($_POST["post"]);
         if ($form->isValid()) {
             DB::query("insert into posts set " . "posted_at = now(),    " . "title     = :title,   " . "content   = :content, " . "email     = :email,   " . "author    = :author   ", array("title" => $form->getChild("title")->getValue(), "content" => $form->getChild("content")->getValue(), "email" => $form->getChild("email")->getValueOrNull(), "author" => $form->getChild("name")->getValue()));
             $id = DB::lastId();
             Session::setFlash("highlight", $id);
             Helpers::redirect("/posts");
         }
     }
     View::set("form", $form);
     View::render("post/create");
 }
Exemple #3
0
 private function run_exec($sql, $data)
 {
     try {
         if ($data === null) {
             $query = $this->db->exec($sql);
         } else {
             $sth = $this->db->prepare($sql);
             $query = $sth->execute($data);
             self::$lastId = $this->db->lastInsertId();
         }
         return $query;
     } catch (Exception $e) {
         // Keep error into log file
         $log_id = $this->set_log($e);
         $msg = array('error' => $e->getMessage(), 'id' => $log_id);
         return $msg;
     }
 }
Exemple #4
0
 /**
  * Asociar los parámetros de la consulta utilizando el tipo adecuado
  *
  * @param &$query  string La consulta a realizar
  * @param $isCount bool   Indica si es una consulta de contador de registros
  * @return bool|\PDOStatement
  * @throws SPException
  */
 private function prepareQueryData(&$query, $isCount = false)
 {
     if ($isCount === true) {
         // No incluimos en el array de parámetros de posición los valores
         // utilizados para LIMIT
         preg_match_all('/(\\?|:)/', $query, $count);
         // Indice a partir del cual no se incluyen valores
         $paramMaxIndex = count($count[1]) > 0 ? count($count[1]) : 0;
     }
     try {
         $db = DBConnectionFactory::getFactory()->getConnection();
         if (is_array($this->_stData)) {
             $sth = $db->prepare($query);
             $paramIndex = 0;
             foreach ($this->_stData as $param => $value) {
                 // Si la clave es un número utilizamos marcadores de posición "?" en
                 // la consulta. En caso contrario marcadores de nombre
                 $param = is_int($param) ? $param + 1 : ':' . $param;
                 if ($isCount === true && count($count) > 0 && $paramIndex >= $paramMaxIndex) {
                     continue;
                 }
                 if ($param == 'blobcontent') {
                     $sth->bindValue($param, $value, \PDO::PARAM_LOB);
                 } elseif (is_int($value)) {
                     //                        error_log("INT: " . $param . " -> " . $value);
                     $sth->bindValue($param, $value, \PDO::PARAM_INT);
                 } else {
                     //                        error_log("STR: " . $param . " -> " . $value);
                     $sth->bindValue($param, $value, \PDO::PARAM_STR);
                 }
                 $paramIndex++;
             }
             $sth->execute();
         } else {
             $sth = $db->query($query);
         }
         DB::$lastId = $db->lastInsertId();
         return $sth;
     } catch (\Exception $e) {
         error_log("Exception: " . $e->getMessage());
         throw new SPException(SPException::SP_CRITICAL, $e->getMessage(), $e->getCode());
     }
 }
Exemple #5
0
 public function replace()
 {
     $this->serialize();
     $sql = sprintf('REPLACE INTO `%s` SET ', $this->name);
     $sqlFields = array();
     $primKey = $this->getPrimKey();
     foreach ($this->data as $colName => $value) {
         if ($colName == $primKey && $this->primIsAI) {
             continue;
         }
         $sqlFields[] = sprintf('`%s` = %s', ucfirst($colName), DB::value($value));
     }
     $sql .= ' ' . implode(',', $sqlFields);
     $result = DB::q($sql);
     if ($result && $primKey && $this->primIsAI) {
         $this->{$primKey}(DB::lastId());
     }
 }