Example #1
0
 public function testQuoteValueWithPdoMysql()
 {
     if (!$this->adapters['pdo_mysql'] instanceof \PDO) {
         $this->markTestSkipped('MySQL (PDO_Mysql) not configured in unit test configuration file');
     }
     $mysql = new Mysql($this->adapters['pdo_mysql']);
     $value = $mysql->quoteValue('value');
     $this->assertEquals('\'value\'', $value);
     $mysql = new Mysql(new Pdo\Pdo(new Pdo\Connection($this->adapters['pdo_mysql'])));
     $value = $mysql->quoteValue('value');
     $this->assertEquals('\'value\'', $value);
 }
Example #2
0
 /**
  * @covers Zend\Db\Adapter\Platform\Mysql::quoteIdentifierInFragment
  */
 public function testQuoteIdentifierInFragment()
 {
     $this->assertEquals('`foo`.`bar`', $this->platform->quoteIdentifierInFragment('foo.bar'));
     $this->assertEquals('`foo` as `bar`', $this->platform->quoteIdentifierInFragment('foo as bar'));
     $this->assertEquals('`$TableName`.`bar`', $this->platform->quoteIdentifierInFragment('$TableName.bar'));
     // single char words
     $this->assertEquals('(`foo`.`bar` = `boo`.`baz`)', $this->platform->quoteIdentifierInFragment('(foo.bar = boo.baz)', array('(', ')', '=')));
     // case insensitive safe words
     $this->assertEquals('(`foo`.`bar` = `boo`.`baz`) AND (`foo`.`baz` = `boo`.`baz`)', $this->platform->quoteIdentifierInFragment('(foo.bar = boo.baz) AND (foo.baz = boo.baz)', array('(', ')', '=', 'and')));
 }
Example #3
0
 /**
  * @covers Zend\Db\Adapter\Platform\Mysql::quoteIdentifierInFragment
  */
 public function testQuoteIdentifierInFragment()
 {
     $this->assertEquals('`foo`.`bar`', $this->platform->quoteIdentifierInFragment('foo.bar'));
     $this->assertEquals('`foo` as `bar`', $this->platform->quoteIdentifierInFragment('foo as bar'));
 }
Example #4
0
 /**
  * Creates table by its name and config
  *
  * @param $tableName
  * @param $tableConfig
  * @return Adapter\Driver\StatementInterface|\Zend\Db\ResultSet\ResultSet
  * @throws RestException
  */
 protected function create($tableName, $tableConfig = null)
 {
     $tableConfig = is_null($tableConfig) ? $tableConfig = $tableName : $tableConfig;
     $tableConfigArray = $this->getTableConfig($tableConfig);
     $table = new CreateTable($tableName);
     $alterTable = new AlterTable($tableName);
     $table->addConstraint(new Constraint\PrimaryKey('id'));
     foreach ($tableConfigArray as $fieldName => $fieldData) {
         $fieldType = $fieldData[self::FIELD_TYPE];
         $fieldParams = $this->getFieldParams($fieldData, $fieldType);
         array_unshift($fieldParams, $fieldName);
         $fieldClass = '\\Zend\\Db\\Sql\\Ddl\\Column\\' . $fieldType;
         $reflectionObject = new \ReflectionClass($fieldClass);
         $fieldInstance = $reflectionObject->newInstanceArgs($fieldParams);
         // it' like new class($callParamsArray[1], $callParamsArray[2]...)
         $table->addColumn($fieldInstance);
         if (isset($fieldData[self::UNIQUE_KEY])) {
             $uniqueKeyConstraintName = $fieldData[self::UNIQUE_KEY] === true ? 'UniqueKey_' . $tableName . '_' . $fieldName : $fieldData[self::UNIQUE_KEY];
             $uniqueKeyInstance = new UniqueKey([$fieldName], $uniqueKeyConstraintName);
             $alterTable->addConstraint($uniqueKeyInstance);
         }
         if (isset($fieldData[self::FOREIGN_KEY])) {
             $foreignKeyConstraintName = !isset($fieldData[self::FOREIGN_KEY]['name']) ? 'ForeignKey_' . $tableName . '_' . $fieldName : $fieldData[self::FOREIGN_KEY]['name'];
             $onDeleteRule = isset($fieldData[self::FOREIGN_KEY]['onDeleteRule']) ? $fieldData[self::FOREIGN_KEY]['onDeleteRule'] : null;
             $onUpdateRule = isset($fieldData[self::FOREIGN_KEY]['onUpdateRule']) ? $fieldData[self::FOREIGN_KEY]['onUpdateRule'] : null;
             $foreignKeyInstance = new Constraint\ForeignKey($foreignKeyConstraintName, [$fieldName], $fieldData[self::FOREIGN_KEY]['referenceTable'], $fieldData[self::FOREIGN_KEY]['referenceColumn'], $onDeleteRule, $onUpdateRule, $foreignKeyConstraintName);
             $alterTable->addConstraint($foreignKeyInstance);
         }
     }
     // this is simpler version, not MySQL only, but without options[] support
     //$mySqlPlatformSql = new Sql\Platform\Mysql\Mysql();
     //$sql = new Sql\Sql($this->db, null, $mySqlPlatformSql);
     //$sqlString = $sql->buildSqlString($table);
     $ctdMysql = new Sql\Platform\Mysql\Ddl\CreateTableDecorator();
     $mySqlPlatformDbAdapter = new Adapter\Platform\Mysql();
     $mySqlPlatformDbAdapter->setDriver($this->db->getDriver());
     $sqlStringCreate = $ctdMysql->setSubject($table)->getSqlString($mySqlPlatformDbAdapter);
     $mySqlPlatformSql = new Sql\Platform\Mysql\Mysql();
     $sql = new Sql\Sql($this->db, null, $mySqlPlatformSql);
     $sqlStringAlter = $sql->buildSqlString($alterTable);
     $sqlString = $sqlStringCreate . ';' . PHP_EOL . $sqlStringAlter . ';';
     return $this->db->query($sqlString, \Zend\Db\Adapter\Adapter::QUERY_MODE_EXECUTE);
 }
Example #5
0
 /**
  * Quotes trusted value
  *
  * The ability to quote values without notices
  *
  * @param $value
  * @return string
  */
 public function quoteTrustedValue($value)
 {
     if (is_int($value)) {
         return (string) $value;
     } elseif (is_float($value)) {
         return $this->floatConversion ? $this->toFloatSinglePrecision($value) : (string) $value;
     } elseif (is_null($value)) {
         return 'NULL';
         // Not supported by SphinxQL, but included for consistency with prepared statement behavior
     }
     return parent::quoteTrustedValue($value);
 }
Example #6
0
 /**
  * @group ZF2-386
  * @covers Zend\Db\Adapter\Platform\Mysql::quoteIdentifierInFragment
  */
 public function testQuoteIdentifierInFragmentIgnoresSingleCharSafeWords()
 {
     $this->assertEquals('(`foo`.`bar` = `boo`.`baz`)', $this->platform->quoteIdentifierInFragment('(foo.bar = boo.baz)', array('(', ')', '=')));
 }