Skip to content

Commit 3fbf3c7

Browse files
committed
Handle quoting of values
1 parent fa47121 commit 3fbf3c7

File tree

8 files changed

+18
-18
lines changed

8 files changed

+18
-18
lines changed

src/lib/ColumnToCode.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,8 +177,8 @@ private function resolve():void
177177
$this->rawParts['nullable'] = $this->column->allowNull ? 'NULL' : 'NOT NULL';
178178
$this->fluentParts['nullable'] = $this->column->allowNull === true ? 'null()' : 'notNull()';
179179

180-
$this->fluentParts['comment'] = $this->column->comment ? 'comment(\''.$this->column->comment.'\')' : $this->fluentParts['comment'];
181-
$this->rawParts['comment'] = $this->column->comment ? 'COMMENT \''.$this->column->comment.'\'' : $this->fluentParts['comment'];
180+
$this->fluentParts['comment'] = $this->column->comment ? 'comment('.var_export($this->column->comment, true).')' : $this->fluentParts['comment'];
181+
$this->rawParts['comment'] = $this->column->comment ? 'COMMENT '.var_export($this->column->comment, true) : $this->rawParts['comment'];
182182

183183
if (array_key_exists($dbType, self::INT_TYPE_MAP)) {
184184
$this->fluentParts['type'] = self::INT_TYPE_MAP[$dbType] . $fluentSize;

src/lib/migrations/BaseMigrationBuilder.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -449,16 +449,16 @@ public function tmpSaveNewCol(string $tableAlias, \cebe\yii2openapi\db\ColumnSch
449449
if (ApiGenerator::isPostgres() && static::isEnum($columnSchema)) {
450450
$allEnumValues = $columnSchema->enumValues;
451451
$allEnumValues = array_map(function ($aValue) {
452-
return "'$aValue'";
452+
return $this->db->quoteValue($aValue);
453453
}, $allEnumValues);
454454
Yii::$app->db->createCommand(
455455
'CREATE TYPE '.$tmpEnumName($columnSchema->name).' AS ENUM('.implode(', ', $allEnumValues).')'
456-
)->execute(); // TODO quote value
456+
)->execute();
457457
}
458458

459459
Yii::$app->db->createCommand()->createTable($tmpTableName, $column)->execute();
460460
if (ApiGenerator::isPostgres() && $columnSchema->comment) {
461-
Yii::$app->db->createCommand("COMMENT ON COLUMN $tmpTableName.$columnSchema->name IS '$columnSchema->comment'")->execute(); // TODO quote comment
461+
Yii::$app->db->createCommand("COMMENT ON COLUMN $tmpTableName.$columnSchema->name IS {$this->db->quoteValue($columnSchema->comment)}")->execute();
462462
}
463463

464464
$table = Yii::$app->db->getTableSchema($tmpTableName);

src/lib/migrations/MigrationRecordBuilder.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ final class MigrationRecordBuilder
4646

4747
public const ALTER_COLUMN_RAW_PGSQL = MigrationRecordBuilder::INDENT . "\$this->db->createCommand('ALTER TABLE %s ALTER COLUMN \"%s\" SET DATA TYPE %s')->execute();";
4848

49-
public const ADD_COMMENT_ON_COLUMN = MigrationRecordBuilder::INDENT . "\$this->addCommentOnColumn('%s', '%s', '%s');";
49+
public const ADD_COMMENT_ON_COLUMN = MigrationRecordBuilder::INDENT . "\$this->addCommentOnColumn('%s', '%s', %s);";
5050

5151
public const DROP_COMMENT_FROM_COLUMN = MigrationRecordBuilder::INDENT . "\$this->dropCommentFromColumn('%s', '%s');";
5252

@@ -352,7 +352,7 @@ public static function makeString(array $codeColumns): string
352352

353353
public function addCommentOnColumn($table, string $column, string $comment): string
354354
{
355-
return sprintf(self::ADD_COMMENT_ON_COLUMN, $table, $column, $comment);
355+
return sprintf(self::ADD_COMMENT_ON_COLUMN, $table, $column, var_export($comment, true));
356356
}
357357

358358
public function dropCommentFromColumn($table, string $column): string

tests/specs/issue_fix/60_description_of_a_property_in_spec_must_correspond_to_db_table_column_comment/index.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,15 @@ components:
1313
type: integer
1414
name:
1515
type: string
16-
description: desc
16+
description: desc with ' quote
1717
# colour:
1818
# type: string
1919
# maxLength: 255
2020
# x-db-type: varbinary # TODO
2121
description:
2222
type: number
2323
x-db-type: double precision
24-
description: desc 2
24+
description: desc ' 2
2525
Animal:
2626
type: object
2727
properties:

tests/specs/issue_fix/60_description_of_a_property_in_spec_must_correspond_to_db_table_column_comment/mysql/migrations_mysql_db/m200000_000001_create_table_fruits.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ public function up()
99
{
1010
$this->createTable('{{%fruits}}', [
1111
'id' => $this->primaryKey(),
12-
'name' => $this->text()->null()->comment('desc'),
13-
0 => 'description double precision NULL DEFAULT NULL COMMENT \'desc 2\'',
12+
'name' => $this->text()->null()->comment('desc with \' quote'),
13+
0 => 'description double precision NULL DEFAULT NULL COMMENT \'desc \\\' 2\'',
1414
]);
1515
}
1616

tests/specs/issue_fix/60_description_of_a_property_in_spec_must_correspond_to_db_table_column_comment/mysql/models/base/Fruit.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
* This is the model class for table "fruits".
1111
*
1212
* @property int $id
13-
* @property string $name desc
14-
* @property double $description desc 2
13+
* @property string $name desc with ' quote
14+
* @property double $description desc ' 2
1515
*
1616
*/
1717
abstract class Fruit extends \yii\db\ActiveRecord

tests/specs/issue_fix/60_description_of_a_property_in_spec_must_correspond_to_db_table_column_comment/pgsql/migrations_pgsql_db/m200000_000001_create_table_fruits.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ public function safeUp()
99
{
1010
$this->createTable('{{%fruits}}', [
1111
'id' => $this->primaryKey(),
12-
'name' => $this->text()->null()->defaultValue(null)->comment('desc'),
12+
'name' => $this->text()->null()->defaultValue(null)->comment('desc with \' quote'),
1313
0 => '"description" double precision NULL DEFAULT NULL',
1414
]);
15-
$this->addCommentOnColumn('{{%fruits}}', 'name', 'desc');
16-
$this->addCommentOnColumn('{{%fruits}}', 'description', 'desc 2');
15+
$this->addCommentOnColumn('{{%fruits}}', 'name', 'desc with \' quote');
16+
$this->addCommentOnColumn('{{%fruits}}', 'description', 'desc \' 2');
1717
}
1818

1919
public function safeDown()

tests/specs/issue_fix/60_description_of_a_property_in_spec_must_correspond_to_db_table_column_comment/pgsql/models/base/Fruit.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
* This is the model class for table "fruits".
1111
*
1212
* @property int $id
13-
* @property string $name desc
14-
* @property double $description desc 2
13+
* @property string $name desc with ' quote
14+
* @property double $description desc ' 2
1515
*
1616
*/
1717
abstract class Fruit extends \yii\db\ActiveRecord

0 commit comments

Comments
 (0)