Exemplo n.º 1
0
 public function testDecode()
 {
     $db = $this->getDb();
     $adapter = new SqliteTypeAdapter($db);
     $this->assertNull($adapter->decode(DataType::integer(), null));
     $this->assertSame(15, $adapter->decode(DataType::integer(), '15'));
     $this->assertSame(33.0, $adapter->decode(DataType::float(), 33));
     $this->assertSame(true, $adapter->decode(DataType::boolean(), 1));
     $this->assertSame(false, $adapter->decode(DataType::boolean(), 0));
     $this->assertSame(4242, $adapter->decode(DataType::date(), 4242));
     $this->assertSame(4242, $adapter->decode(DataType::dateTime(), 4242));
     $this->assertSame('foo bar baz', $adapter->decode(DataType::string(), 'foo bar baz'));
     $this->assertSame([], $adapter->decode(DataType::object(), '[]'));
 }
Exemplo n.º 2
0
 public function testDecode()
 {
     $db = $this->getDb();
     $adapter = new PostgresqlTypeAdapter($db);
     $this->assertNull($adapter->decode(DataType::integer(), null));
     $this->assertSame(15, $adapter->decode(DataType::integer(), '15'));
     $this->assertSame(33.0, $adapter->decode(DataType::float(), 33));
     $this->assertSame(true, $adapter->decode(DataType::boolean(), 1));
     $this->assertSame(false, $adapter->decode(DataType::boolean(), 0));
     $time = time();
     $encoded = $adapter->encode(DataType::dateTime(), $time);
     $this->assertTrue(preg_match('/^"(\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2})"$/', $encoded, $matches) === 1);
     $this->assertEquals($time, $adapter->decode(DataType::dateTime(), $matches[1]));
     $this->assertSame('foo bar baz', $adapter->decode(DataType::string(), 'foo bar baz'));
     $this->assertSame([], $adapter->decode(DataType::object(), '[]'));
 }
Exemplo n.º 3
0
 /**
  *
  * @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));
 }
Exemplo n.º 4
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']);
 }
Exemplo n.º 5
0
 /**
  * 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']);
 }
Exemplo n.º 6
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']);
 }