예제 #1
0
 public function testDelete()
 {
     $user = new User();
     $user->name = 'John';
     $user->save();
     $sql = 'SELECT COUNT(*) FROM user';
     $this->assertEquals('1', $this->orm->execute($sql)->fetchColumn());
     $user->delete();
     $this->assertEquals('0', $this->orm->execute($sql)->fetchColumn());
 }
예제 #2
0
    protected function setUp()
    {
        $this->orm = new ORM();
        $this->orm->connect('sqlite::memory:');
        $sql = <<<'TAG'
CREATE TABLE user (
    identifier INTEGER PRIMARY KEY AUTOINCREMENT,
    name TEXT
)
TAG;
        $this->orm->execute($sql);
    }
예제 #3
0
 /**
  * Returns the beginning of any SQL statement, depending on $this->queryType.
  *
  * @return string
  */
 protected function getTypeSQL()
 {
     $sql = '';
     $quotedTableName = $this->orm->quoteIdentifier($this->tableName);
     switch ($this->queryType) {
         case self::QUERY_TYPE_SELECT:
             $top = '';
             if ($this->limit !== null && $this->detectLimitStyle() === self::LIMIT_STYLE_TOP) {
                 $top = ' TOP ' . $this->limit;
             }
             $sql = sprintf('SELECT %s * FROM %s', $top, $quotedTableName);
             break;
         case self::QUERY_TYPE_DELETE:
             $sql = sprintf('DELETE FROM %s', $quotedTableName);
             break;
         case self::QUERY_TYPE_UPDATE:
             $sets = array();
             foreach ($this->modelData as $columnName => $columnValue) {
                 $sets[] = sprintf('%s = ?', $this->orm->quoteIdentifier($columnName));
                 $this->parameters[] = $columnValue;
             }
             $sql = sprintf('UPDATE %s SET %s', $quotedTableName, implode(',', $sets));
             break;
         case self::QUERY_TYPE_INSERT:
             $quotedColumnNames = $this->orm->quoteIdentifier(array_keys($this->modelData));
             $placeholders = implode(',', array_fill(0, count($this->modelData), '?'));
             $sql = sprintf('INSERT INTO %s (%s) VALUES (%s)', $quotedTableName, $quotedColumnNames, $placeholders);
             $this->parameters = array_merge($this->parameters, array_values($this->modelData));
             break;
     }
     return $sql;
 }
예제 #4
0
 public function testUpdate()
 {
     $user = new User();
     $user->name = 'John';
     $user->save();
     $user->id = null;
     $user->name = 'Jane';
     $user->save();
     $user->id = null;
     $user->name = 'Mark';
     $user->save();
     $fluentUser = new FluentUser();
     $fluentUser->name = 'oops';
     $fluentUser->updateAll();
     $names = $this->orm->execute('SELECT name FROM user')->fetchAll(\PDO::FETCH_COLUMN);
     foreach ($names as $name) {
         $this->assertEquals('oops', $name);
     }
 }