Exemplo n.º 1
0
 public function testDbDriver()
 {
     $dbh = Rorm::getDatabase('sqlite');
     $this->assertTrue(Rorm::isSQLite($dbh));
     $this->assertFalse(Rorm::isMySQL($dbh));
 }
Exemplo n.º 2
0
 /**
  * @return bool
  * @throws QueryException
  * @throws \PDOException
  */
 public function save()
 {
     if (empty($this->_data)) {
         throw new QueryException('can not save empty data!');
     }
     $dbh = static::getDatabase();
     $quoteIdentifier = Rorm::getIdentifierQuoter($dbh);
     $quotedTable = $quoteIdentifier(static::getTable());
     $idColumns = static::$_idColumn;
     if (!is_array($idColumns)) {
         $idColumns = array($idColumns);
     }
     $doMerge = $this->hasId();
     // ignore fields
     $notSetFields = static::$_ignoreColumns;
     /**
      * Different queries are built for each driver
      *
      * IDEA: probably split into methods (saveMySQL, saveSQLite)
      */
     if (Rorm::isMySQL($dbh)) {
         /**
          * MySQL
          * Instead of REPLACE INTO we use INSERT INTO ON DUPLICATE KEY UPDATE.
          * Because REPLACE INTO does DELETE and INSERT,
          * which does not play nice with TRIGGERs and FOREIGN KEY CONSTRAINTS
          */
         $sql = 'INSERT INTO ' . $quotedTable . ' ';
         $insertData = array();
         $updateData = array();
         foreach ($this->_data as $column => $value) {
             if (in_array($column, $notSetFields)) {
                 continue;
             }
             $quotedColumn = $quoteIdentifier($column);
             $insertData[$quotedColumn] = Rorm::quote($dbh, $value);
             if ($doMerge && !in_array($column, $idColumns)) {
                 $updateData[] = $quotedColumn . ' = VALUES(' . $quotedColumn . ')';
             }
         }
         unset($column, $value, $quotedColumn);
         // insert
         $sql .= '(' . implode(', ', array_keys($insertData)) . ')' . ' VALUES ' . '(' . implode(', ', $insertData) . ')';
         if ($doMerge && count($updateData) > 0) {
             // update
             $sql .= ' ON DUPLICATE KEY UPDATE ' . implode(', ', $updateData);
         }
         // execute (most likely throws PDOException if there is an error)
         if ($dbh->exec($sql) === false) {
             return false;
         }
         // update generated id
         if (static::$_autoId && !$doMerge) {
             // last insert id
             $this->set(static::$_idColumn, $dbh->lastInsertId());
         }
         return true;
     } else {
         /**
          * SQLite
          */
         if ($doMerge) {
             $sql = 'INSERT OR REPLACE INTO ' . $quotedTable . ' ';
         } else {
             $sql = 'INSERT INTO ' . $quotedTable . ' ';
         }
         // build (column) VALUES (values)
         $quotedData = array();
         foreach ($this->_data as $column => $value) {
             if (in_array($column, $notSetFields)) {
                 continue;
             }
             $quotedData[$quoteIdentifier($column)] = Rorm::quote($dbh, $value);
         }
         unset($column, $value);
         $sql .= '(' . implode(', ', array_keys($quotedData)) . ') VALUES (' . implode(', ', $quotedData) . ')';
         // execute (most likely throws PDOException if there is an error)
         if ($dbh->exec($sql) === false) {
             return false;
         }
         // update generated id
         if (static::$_autoId && !$this->hasId()) {
             // last insert id
             $this->set(static::$_idColumn, $dbh->lastInsertId());
         }
         return true;
     }
 }