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)); }
/** * {@inheritdoc} */ public function __get($property) { if ($property === 'values') { return $this->values; } if ($property === 'placeholder') { if (!isset($this->class)) { throw new InvalidPropertyException('Invalid use of anonymous enum type'); } return '%' . $this->class; } return parent::__get($property); }
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; }
public function testInstance() { $record = []; $quoter = $this->getMockBuilder('Jivoo\\Data\\Query\\Expression\\Quoter')->getMock(); $quoter->method('quoteLiteral')->willReturnCallback(function ($type, $value) { return $value ? 'true' : 'false'; }); $prefix = new Prefix('not', new Literal(DataType::boolean(), true)); $this->assertFalse($prefix->__invoke($record)); $this->assertEquals('not true', $prefix->toString($quoter)); $prefix = new Prefix('not', new Infix(new Literal(DataType::boolean(), true), 'and', new Literal(DataType::boolean(), false))); $this->assertTrue($prefix->__invoke($record)); $this->assertEquals('not (true and false)', $prefix->toString($quoter)); $prefix = new Prefix('!', new Literal(DataType::boolean(), true)); $this->assertThrows('PHPUnit_Framework_Error', function () use($prefix, $record) { $prefix($record); }); }
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); }
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; }
public function testInstance() { $record = []; $quoter = $this->getMockBuilder('Jivoo\\Data\\Query\\Expression\\Quoter')->getMock(); $quoter->method('quoteLiteral')->willReturnCallback(function ($type, $value) { return $value; }); $this->assertTrue($this->getInfix(5, '>', 4)->__invoke($record)); $this->assertFalse($this->getInfix(5, '<', 4)->__invoke($record)); $this->assertTrue($this->getInfix(5, '=', 5)->__invoke($record)); $this->assertFalse($this->getInfix(5, '!=', 5)->__invoke($record)); $this->assertTrue($this->getInfix(5, '<>', 4)->__invoke($record)); $this->assertFalse($this->getInfix(5, '!>', 4)->__invoke($record)); $this->assertTrue($this->getInfix(5, '!<', 4)->__invoke($record)); $this->assertFalse($this->getInfix(5, '<=', 4)->__invoke($record)); $this->assertTrue($this->getInfix(5, '>=', 5)->__invoke($record)); $this->assertTrue($this->getInfix(2, 'in', array(1, 2, 4))->__invoke($record)); $isNull = new Infix(new Literal(DataType::integer(), null), 'is', null); $this->assertTrue($isNull->__invoke($record)); $this->assertEquals('2 = 5', $this->getInfix(2, '=', 5)->toString($quoter)); $this->assertThrows('PHPUnit_Framework_Error', function () use($record) { $this->getInfix(5, '==', 5)->__invoke($record); }); }
/** * * @param string $expression * @return ParseInput */ public static function lex($expression, $vars = array()) { $lexer = new RegexLexer(true, 'i'); $lexer->is = 'is'; $lexer->not = 'not'; $lexer->bool = 'true|false'; $lexer->null = 'null'; $lexer->operator = 'like|in|!=|<>|>=|<=|!<|!>|=|<|>|and|or'; $lexer->dot = '\\.'; $lexer->name = '[a-z][a-z0-9]*'; $lexer->model = '\\{(.+?)\\}'; $lexer->modelPlaceholder = '%(model|m)'; $lexer->field = '\\[(.+?)\\]'; $lexer->fieldPlaceholder = '%(column|field|c)'; $lexer->number = '-?(0|[1-9]\\d*)(\\.\\d+)?([eE][+-]?\\d+)?'; $lexer->string = '"((?:[^"\\\\]|\\\\.)*)"'; $lexer->placeholder = '((\\?)|%([a-z_\\\\]+))(\\(\\))?'; $lexer->map('model', function ($value, $matches) { return $matches[1]; }); $lexer->map('field', function ($value, $matches) { return $matches[1]; }); $lexer->map('number', function ($value) { if (strpos($value, '.') !== false or stripos($value, 'e') !== false) { return new Literal(DataType::float(), floatval($value)); } else { return new Literal(DataType::integer(), intval($value)); } }); $lexer->mapType('number', 'literal'); $lexer->map('string', function ($value, $matches) { return new Literal(DataType::text(), stripslashes($matches[1])); }); $lexer->mapType('string', 'literal'); $lexer->map('bool', function ($value) { return new Literal(DataType::boolean(), strtolower($value) == 'true'); }); $lexer->mapType('bool', 'literal'); $lexer->map('model', function ($value, $matches) { return $matches[1]; }); $lexer->map('field', function ($value, $matches) { return $matches[1]; }); $i = 0; $lexer->map('modelPlaceholder', function ($value, $matches) use(&$i, $vars) { $value = $vars[$i]; $i++; if (!is_string($value)) { Assume::that($value instanceof Model); $value = $value->getName(); } return $value; }); $lexer->mapType('modelPlaceholder', 'model'); $lexer->map('fieldPlaceholder', function ($value, $matches) use(&$i, $vars) { $value = $vars[$i]; $i++; Assume::that(is_string($value)); return $value; }); $lexer->mapType('fieldPlaceholder', 'field'); $lexer->map('placeholder', function ($value, $matches) use(&$i, $vars) { $value = $vars[$i]; $i++; $type = null; if (isset($matches[3])) { if ($matches[3] == '_') { if (!is_string($value)) { Assume::that($value instanceof DataType); $value = $value->placeholder; } $matches[3] = ltrim($value, '%'); $value = $vars[$i]; $i++; } if ($matches[3] == 'e' or $matches[3] == 'expr' or $matches[3] == 'expression') { Assume::that($value instanceof Expression); return $value; } if ($matches[3] != '()') { $type = DataType::fromPlaceholder($matches[3]); } } if (!isset($type)) { $type = DataType::detectType($value); } if (isset($matches[4]) or isset($matches[3]) and $matches[3] == '()') { Assume::isArray($value); foreach ($value as $key => $v) { $value[$key] = $v; } return new ArrayLiteral($type, $value); } return new Literal($type, $value); }); $lexer->mapType('placeholder', 'literal'); return new ParseInput($lexer($expression)); }
/** * Add created and updated timestamps to schema. * @param string $created Created field name. * @param string $updated Updated field name. */ public function addTimestamps($created = 'created', $updated = 'updated') { $this->{$created} = DataType::dateTime(); $this->{$updated} = DataType::dateTime(); }
public function testAlterColumn() { $db = $this->getDb(); $adapter = new PostgresqlTypeAdapter($db); $db->expects($this->once())->method('execute')->with($this->equalTo('ALTER TABLE {Foo} ALTER a TYPE int NOT NULL')); $adapter->alterColumn('Foo', 'a', DataType::integer()); }
/** * 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']); }
public function testAlterColumn() { $db = $this->getDb(); $adapter = new MysqlTypeAdapter($db); $db->expects($this->once())->method('execute')->with($this->equalTo('ALTER TABLE `foo_bar` CHANGE baz baz TEXT NOT NULL')); $adapter->alterColumn('FooBar', 'baz', DataType::text()); }
/** * Convert output of PRAGMA to DataType. * * @param array $row * Row result. * @throws TypeException If type unsupported. * @return DataType The type. */ private function toDataType($row) { if (preg_match('/ *([^ (]+) *(\\(([0-9]+)\\))? */i', $row['type'], $matches) !== 1) { throw new TypeException('Cannot read type "' . $row['type'] . '" for column: ' . $row['name']); } $actualType = strtolower($matches[1]); $length = isset($matches[3]) ? $matches[3] : 0; $null = (isset($row['notnull']) and $row['notnull'] != '1'); $default = null; if (isset($row['dflt_value'])) { $default = stripslashes(preg_replace('/^\'|\'$/', '', $row['dflt_value'])); } switch ($actualType) { case 'integer': return DataType::integer(DataType::BIG, $null, isset($default) ? intval($default) : null); case 'real': return DataType::float($null, isset($default) ? floatval($default) : null); case 'text': return DataType::text($null, $default); case 'blob': return DataType::binary($null, $default); } throw new TypeException('Unsupported SQLite type for column: ' . $row['name']); }
public function testAlterColumn() { $db = $this->getDb(); $adapter = new SqliteTypeAdapter($db); $db->expects($this->exactly(6))->method('execute')->withConsecutive([$this->equalTo('CREATE TABLE "foo__migration_backup" (' . 'a TEXT(255) NOT NULL, ' . 'b TEXT(255) NOT NULL, ' . 'c TEXT(255) NOT NULL, ' . 'PRIMARY KEY ())')], [$this->equalTo('INSERT INTO {Foo_MigrationBackup} SELECT * FROM {Foo}')], [$this->equalTo('DROP TABLE "foo"')], [$this->equalTo('CREATE TABLE "foo" (' . 'a INTEGER NOT NULL, ' . 'b TEXT(255) NOT NULL, ' . 'c TEXT(255) NOT NULL, ' . 'PRIMARY KEY ())')], [$this->equalTo('INSERT INTO {Foo} SELECT * FROM {Foo_MigrationBackup}')], [$this->equalTo('DROP TABLE "foo__migration_backup"')]); $adapter->alterColumn('Foo', 'a', DataType::integer()); }
/** * 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']); }
/** * Create a definition from the provided field names. * * @param string[] $fields Field names. * @return DefinitionBuilder */ public static function auto(array $fields) { $definition = new DefinitionBuilder(); foreach ($fields as $field) { $definition->{$field} = DataType::text(true); } return $definition; }
/** * Substitute and encode variables in an expression. * * Placeholders (see also {@see DataType::fromPlaceHolder()}: * <code> * true // Boolean true * false // Boolean false * {AnyModelName} // A model name * [anyFieldName] // A column/field name * "any string" // A string * ? // Any scalar value. * %e %expr %expression // A subexpression (instance of {@see Expression}) * %m %model // A table/model object or name * %c %column %field // A column/field name * %_ // A placeholder placeholder, can also be a type, e.g. where(..., 'id = %_', $type, $value) * %i %int %integer // An integer value * %f %float // A floating point value * %s %str %string // A string * %t $text // Text * %b %bool %boolean // A boolean value * %date // A date value * %d %datetime // A date/time value * %n %bin %binary // A binary object * %AnyEnumClassName // An enum value of that class * %anyPlaceholder() // A tuple of values * </code> * * @param string|Condition $format Expression format, use placeholders instead of values. * @param mixed[] $vars List of values to replace placeholders with. * @param Quoter $quoter Quoter object for quoting identifieres and literals. * @return string The interpolated expression. */ public static function interpolate($format, $vars, Quoter $quoter) { if ($format instanceof self) { return $format->toString($quoter); } assume(is_string($format)); $boolean = DataType::boolean(); $true = $quoter->quoteLiteral($boolean, true); $false = $quoter->quoteLiteral($boolean, false); $format = preg_replace('/\\btrue\\b/i', $true, $format); $format = preg_replace('/\\bfalse\\b/i', $false, $format); $string = DataType::text(); $format = preg_replace_callback('/"((?:[^"\\\\]|\\\\.)*)"|\\{(.+?)\\}|\\[(.+?)\\]/', function ($matches) use($quoter, $string) { if (isset($matches[3])) { return $quoter->quoteField($matches[3]); } else { if (isset($matches[2])) { return $quoter->quoteModel($matches[2]); } else { return $quoter->quoteLiteral($string, stripslashes($matches[1])); } } }, $format); $i = 0; return preg_replace_callback('/((\\?)|%([a-z_\\\\]+))(\\(\\))?/i', function ($matches) use($vars, &$i, $quoter) { $value = $vars[$i]; $i++; $type = null; if (isset($matches[3])) { if ($matches[3] == '_') { if (!is_string($value)) { assume($value instanceof DataType); $value = $value->placeholder; } $matches[3] = ltrim($value, '%'); $value = $vars[$i]; $i++; } if ($matches[3] == 'e' or $matches[3] == 'expr' or $matches[3] == 'expression') { assume($value instanceof Expression); return '(' . $value->toString($quoter) . ')'; } if ($matches[3] == 'm' or $matches[3] == 'model') { if (!is_string($value)) { assume($value instanceof Model); $value = $value->getName(); } return $quoter->quoteModel($value); } if ($matches[3] == 'c' or $matches[3] == 'column' or $matches[3] == 'field') { assume(is_string($value)); return $quoter->quoteField($value); } if ($matches[3] != '()') { $type = DataType::fromPlaceholder($matches[3]); } } if (!isset($type)) { $type = DataType::detectType($value); } if (isset($matches[4]) or isset($matches[3]) and $matches[3] == '()') { assume(is_array($value)); foreach ($value as $key => $v) { $value[$key] = $quoter->quoteLiteral($type, $v); } return '(' . implode(', ', $value) . ')'; } return $quoter->quoteLiteral($type, $value); }, $format); }
/** * Join with and count the content of an associated collection (associated * using either "hasMany" or "hasAndBelongsToMany"). * * @param string $association * Name of association. * @param ReadSelection $selection * Optional selection. * @return ReadSelection Resulting selection. */ public function withCount($association, ReadSelection $selection = null) { if (!isset($selection)) { $selection = new SelectionBuilder($this); } if (!isset($this->associations)) { $this->createAssociations(); } if (!isset($this->associations[$association])) { throw new InvalidAssociationException('Unknown association: ' . $association); } $field = $association; $association = $this->associations[$field]; $other = $association['model']; $thisKey = $association['thisKey']; $otherKey = $association['otherKey']; $id = $this->primaryKey; $otherId = $other->primaryKey; if (isset($association['join'])) { $join = $association['join']; $otherPrimary = $association['otherPrimary']; $selection = $selection->leftJoin($join, where('J.%c = %m.%c', $thisKey, $this->name, $id), 'J'); $condition = where('%c.%c = J.%c', $field, $otherId, $otherKey); $count = where('COUNT(J.%c)', $otherKey); } else { $condition = where('%c.%c = %m.%c', $field, $thisKey, $this->name, $id); $count = where('COUNT(%c.%c)', $field, $thisKey); } if (isset($association['condition'])) { $condition = $condition->and($association['condition']); } $selection = $selection->leftJoin($other, $condition, $field); $selection->groupBy(where('%m.%c', $this->name, $id)); return $selection->with($field . '_count', $count, DataType::integer()); }