Esempio n. 1
0
 public function testInstance()
 {
     $record = [];
     $quoter = $this->getMockBuilder('Jivoo\\Data\\Query\\Expression\\Quoter')->getMock();
     $quoter->method('quoteLiteral')->willReturnCallback(function ($type, $value) {
         return '"' . $value . '"';
     });
     $array = new ArrayLiteral(DataType::string(), array('foo', 'bar'));
     $this->assertEquals(array('foo', 'bar'), $array($record));
     $this->assertEquals('("foo", "bar")', $array->toString($quoter));
 }
Esempio n. 2
0
 public function testCreateTable()
 {
     $db = $this->getDb();
     $adapter = new PostgresqlTypeAdapter($db);
     $db->expects($this->exactly(3))->method('execute')->withConsecutive([$this->equalTo('CREATE TABLE {FooBar} (' . 'id serial NOT NULL, ' . 'foo varchar(255) NOT NULL DEFAULT "bar", ' . 'CONSTRAINT "foo_bar_PRIMARY" PRIMARY KEY (id))')], [$this->equalTo('CREATE INDEX "foo_bar_foo_id" ON {FooBar} (foo, id)')], [$this->equalTo('CREATE UNIQUE INDEX "foo_bar_foo" ON {FooBar} (foo)')]);
     $def = new \Jivoo\Data\DefinitionBuilder();
     $def->addAutoIncrementId();
     $def->foo = DataType::string(255, false, "bar");
     $def->addKey(['foo', 'id']);
     $def->addUnique('foo');
     $adapter->createTable('FooBar', $def);
 }
Esempio n. 3
0
 public function testCreateTable()
 {
     $db = $this->getDb();
     $adapter = new SqliteTypeAdapter($db);
     $db->expects($this->exactly(3))->method('execute')->withConsecutive([$this->equalTo('CREATE TABLE "foo_bar" (' . 'id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, ' . 'foo TEXT(255) NOT NULL DEFAULT "bar")')], [$this->equalTo('CREATE INDEX "foo_bar_foo_id" ON "foo_bar" (foo, id)')], [$this->equalTo('CREATE UNIQUE INDEX "foo_bar_foo" ON "foo_bar" (foo)')]);
     $def = new \Jivoo\Data\DefinitionBuilder();
     $def->addAutoIncrementId();
     $def->foo = DataType::string(255, false, "bar");
     $def->addKey(['foo', 'id']);
     $def->addUnique('foo');
     $adapter->createTable('FooBar', $def);
 }
Esempio n. 4
0
 public function testCreateTable()
 {
     $db = $this->getDb();
     $adapter = new MysqlTypeAdapter($db);
     $db->expects($this->once())->method('execute')->with($this->equalTo('CREATE TABLE `foo_bar` (' . 'id INT UNSIGNED NOT NULL AUTO_INCREMENT, ' . 'foo VARCHAR(255) NOT NULL DEFAULT "bar", ' . 'PRIMARY KEY (id), ' . 'INDEX (foo, id), ' . 'UNIQUE (foo)' . ') CHARACTER SET utf8'));
     $def = new \Jivoo\Data\DefinitionBuilder();
     $def->addAutoIncrementId();
     $def->foo = DataType::string(255, false, "bar");
     $def->addKey(['foo', 'id']);
     $def->addUnique('foo');
     $adapter->createTable('FooBar', $def);
 }
Esempio n. 5
0
 public static function getDefinition()
 {
     $def = new DefinitionBuilder();
     $def->addAutoIncrementId();
     // Autoincrementing INT id
     $def->username = DataType::string(255);
     // Username VARCHAR(255)
     $def->password = DataType::string(255);
     // Password VARCHAR(255)
     $def->addtimeStamps();
     // Timestamps: 'created' and 'updated'
     $def->addUnique('username', 'username');
     // A unique index on the username field
     return $def;
 }
Esempio n. 6
0
 public function testParseAtomic()
 {
     $ast = ExpressionParser::parseAtomic(ExpressionParser::lex('foo'));
     $this->assertInstanceOf('Jivoo\\Data\\Query\\Expression\\FieldAccess', $ast);
     $this->assertEquals('foo', $ast->field);
     $ast = ExpressionParser::parseAtomic(ExpressionParser::lex('15.5'));
     $this->assertInstanceOf('Jivoo\\Data\\Query\\Expression\\Literal', $ast);
     $this->assertEquals(15.5, $ast->value);
     $ast = ExpressionParser::parseAtomic(ExpressionParser::lex('?', array(42)));
     $this->assertInstanceOf('Jivoo\\Data\\Query\\Expression\\Literal', $ast);
     $this->assertEquals(42, $ast->value);
     $ast = ExpressionParser::parseAtomic(ExpressionParser::lex('%_', array(DataType::string(), 'test')));
     $this->assertInstanceOf('Jivoo\\Data\\Query\\Expression\\Literal', $ast);
     $this->assertEquals('test', $ast->value);
     $this->assertTrue($ast->type->isString());
     $ast = ExpressionParser::parseAtomic(ExpressionParser::lex('%i()', array(array(1, 2, 3))));
     $this->assertInstanceOf('Jivoo\\Data\\Query\\Expression\\ArrayLiteral', $ast);
     $this->assertEquals(array(1, 2, 3), $ast->values);
 }
Esempio n. 7
0
 protected function getDb()
 {
     $def = new DatabaseDefinitionBuilder();
     $tableDef = new DefinitionBuilder();
     $tableDef->a = DataType::string();
     $tableDef->b = DataType::string();
     $tableDef->c = DataType::string();
     $def->addDefinition('Foo', $tableDef);
     $typeAdapter = $this->getMockBuilder('Jivoo\\Data\\Database\\TypeAdapter')->getMock();
     $typeAdapter->method('encode')->willReturnCallback(function ($type, $value) {
         return '"' . $value . '"';
     });
     $db = $this->getMockBuilder('Jivoo\\Data\\Database\\Common\\SqlDatabase')->getMock();
     $db->method('getTypeAdapter')->willReturn($typeAdapter);
     $db->method('getDefinition')->willReturn($def);
     $db->method('sqlLimitOffset')->willReturnCallback(function ($limit, $offset) {
         if (isset($offset)) {
             return 'LIMIT ' . $limit . ' OFFSET ' . $offset;
         }
         return 'LIMIT ' . $limit;
     });
     $db->method('tableName')->willReturnCallback(function ($table) {
         return \Jivoo\Utilities::camelCaseToUnderscores($table);
     });
     $db->method('quoteModel')->willReturnCallback(function ($model) {
         return '{' . $model . '}';
     });
     $db->method('quoteField')->willReturnCallback(function ($field) {
         return $field;
     });
     $db->method('quoteLiteral')->willReturnCallback(function ($type, $value) {
         return '"' . $value . '"';
     });
     $db->method('quoteString')->willReturnCallback(function ($value) {
         return '"' . $value . '"';
     });
     return $db;
 }
Esempio n. 8
0
 /**
  * Convert output of SHOW COLUMN to DataType.
  *
  * @param array $row
  *            Row result.
  * @throws TypeException If type unsupported.
  * @return DataType The type.
  */
 private function toDataType($row)
 {
     $null = (isset($row['Null']) and $row['Null'] != 'NO');
     $default = null;
     if (isset($row['Default'])) {
         $default = $row['Default'];
     }
     if (preg_match('/enum\\((.+)\\)/i', $row['Type'], $matches) === 1) {
         preg_match_all('/\'([^\']+)\'/', $matches[1], $matches);
         $values = $matches[1];
         return DataType::enum($values, $null, $default);
     }
     preg_match('/ *([^ (]+) *(\\(([0-9]+)\\))? *(unsigned)? *?/i', $row['Type'], $matches);
     $actualType = strtolower($matches[1]);
     $length = isset($matches[3]) ? intval($matches[3]) : 0;
     $intFlags = 0;
     if (isset($matches[4])) {
         $intFlags |= DataType::UNSIGNED;
     }
     if (strpos($row['Extra'], 'auto_increment') !== false) {
         $intFlags |= DataType::SERIAL;
     }
     switch ($actualType) {
         case 'bigint':
             $intFlags |= DataType::BIG;
             return DataType::integer($intFlags, $null, isset($default) ? intval($default) : null);
         case 'smallint':
             $intFlags |= DataType::SMALL;
             return DataType::integer($intFlags, $null, isset($default) ? intval($default) : null);
         case 'tinyint':
             $intFlags |= DataType::TINY;
             return DataType::integer($intFlags, $null, isset($default) ? intval($default) : null);
         case 'int':
             return DataType::integer($intFlags, $null, isset($default) ? intval($default) : null);
         case 'double':
             return DataType::float($null, isset($default) ? floatval($default) : null);
         case 'varchar':
             return DataType::string($length, $null, $default);
         case 'blob':
             return DataType::binary($null, $default);
         case 'date':
             return DataType::date($null, isset($default) ? strtotime($default . ' UTC') : null);
         case 'datetime':
             return DataType::dateTime($null, isset($default) ? strtotime($default . ' UTC') : null);
         case 'text':
             return DataType::text($null, $default);
     }
     throw new TypeException('Unsupported MySQL type for column: ' . $row['Field']);
 }
Esempio n. 9
0
 /**
  * Convert output of SHOW COLUMN to DataType.
  *
  * @param array $row
  *            Row result.
  * @throws TypeException If type unsupported.
  * @return DataType The type.
  */
 private function toDataType($row)
 {
     $null = $row['is_nullable'] != 'NO';
     $default = null;
     if (isset($row['column_default'])) {
         $default = $row['column_default'];
     }
     $type = $row['data_type'];
     if (strpos($type, 'int') !== false) {
         $intFlags = 0;
         if (preg_match('/^nextval\\(/', $default) === 1) {
             $intFlags = DataType::SERIAL;
             $default = null;
         } elseif (isset($default)) {
             $default = intval($default);
         }
         if (strpos($type, 'bigint') !== false) {
             return DataType::integer($intFlags | DataType::BIG, $null, $default);
         }
         if (strpos($type, 'smallint') !== false) {
             return DataType::integer($intFlags | DataType::SMALL, $null, $default);
         }
         return DataType::integer($intFlags, $null, $default);
     }
     if (strpos($type, 'double') !== false) {
         return DataType::float($null, isset($default) ? floatval($default) : null);
     }
     if (strpos($type, 'bool') !== false) {
         return DataType::boolean($null, isset($default) ? boolval($default) : null);
     }
     if (preg_match("/^'(.*)'::[a-z ]+\$/", $default, $matches) === 1) {
         $default = $matches[1];
     } else {
         $default = null;
     }
     if (strpos($type, 'character') !== false) {
         $length = intval($row['character_maximum_length']);
         return DataType::string($length, $null, $default);
     }
     if (strpos($type, 'date') !== false) {
         return DataType::date($null, isset($default) ? strtotime($default . ' UTC') : null);
     }
     if (strpos($type, 'timestamp') !== false) {
         return DataType::dateTime($null, isset($default) ? strtotime($default . ' UTC') : null);
     }
     if (strpos($type, 'text') !== false) {
         return DataType::text($null, $default);
     }
     throw new TypeException('Unsupported PostgreSQL type "' . $row['data_type'] . '" for column: ' . $row['column_name']);
 }