Skip to content

Commit 1b71076

Browse files
authored
Merge pull request #40 from cfebs/cl-fix-text-nomax
fix text and char column php code args
2 parents 21a606f + d23bf7b commit 1b71076

File tree

4 files changed

+40
-13
lines changed

4 files changed

+40
-13
lines changed

src/Schema/Column/CharacterColumn.php

+7-5
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,13 @@ public function getPhpCode() : string
6767
}
6868
}
6969

70-
return '(new \\' . static::class . '('
71-
. $this->max_string_length
72-
. ($this->character_set !== null ? ', \'' . $this->character_set . '\'' : '')
73-
. ($this->collation !== null ? ', \'' . $this->collation . '\'' : '')
74-
. '))'
70+
$args = [
71+
$this->max_string_length,
72+
$this->character_set === null ? 'null' : "'{$this->character_set}'",
73+
$this->collation === null ? 'null' : "'{$this->collation}'",
74+
];
75+
76+
return '(new \\' . static::class . '(' . implode(', ', $args) . '))'
7577
. $default
7678
. $this->getNullablePhp();
7779
}

src/Schema/Column/TextTrait.php

+7-6
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,13 @@ trait TextTrait
88
public function getPhpCode() : string
99
{
1010
$default = $this->getDefault() !== null ? '\'' . $this->getDefault() . '\'' : 'null';
11-
12-
return '(new \\' . static::class . '('
13-
. ($this->character_set !== null && $this->collation !== null
14-
? ', \'' . $this->character_set . '\'' . ', \'' . $this->collation . '\''
15-
: '')
16-
. '))'
11+
12+
$args = [
13+
$this->character_set === null ? 'null' : "'{$this->character_set}'",
14+
$this->collation === null ? 'null' : "'{$this->collation}'",
15+
];
16+
17+
return '(new \\' . static::class . '(' . implode(', ', $args) . '))'
1718
. ($this->hasDefault() ? '->setDefault(' . $default . ')' : '')
1819
. $this->getNullablePhp();
1920
}

tests/CreateTableParseTest.php

+10
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,17 @@ public function testSimpleParse()
3434

3535
// specific parsing checks
3636
$this->assertInstanceOf(TableDefinition::class, $table_defs['tweets']);
37+
$this->assertEquals('utf8mb4', $table_defs['tweets']->columns['title']->getCharacterSet());
38+
$this->assertEquals('utf8mb4_unicode_ci', $table_defs['tweets']->columns['title']->getCollation());
3739
$this->assertEquals('utf8mb4', $table_defs['tweets']->columns['text']->getCharacterSet());
3840
$this->assertEquals('utf8mb4_unicode_ci', $table_defs['tweets']->columns['text']->getCollation());
41+
42+
$this->assertInstanceOf(TableDefinition::class, $table_defs['texts']);
43+
$this->assertEquals('utf8mb4', $table_defs['texts']->columns['title_char_col']->getCharacterSet());
44+
$this->assertEquals('utf8mb4_unicode_ci', $table_defs['texts']->columns['title_char_col']->getCollation());
45+
$this->assertNull($table_defs['texts']->columns['title_col']->getCharacterSet());
46+
$this->assertEquals('utf8mb4_unicode_ci', $table_defs['texts']->columns['title_col']->getCollation());
47+
$this->assertNull($table_defs['texts']->columns['title']->getCharacterSet());
48+
$this->assertNull($table_defs['texts']->columns['title']->getCollation());
3949
}
4050
}

tests/fixtures/create_table.sql

+16-2
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,25 @@ CREATE TABLE `orders`
6060
`created_on` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
6161
`modified_on` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
6262
PRIMARY KEY (`id`)
63-
)ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;
63+
)
64+
ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;
6465

6566
CREATE TABLE `tweets` (
6667
`id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT,
67-
`text` varchar(256) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci,
68+
`title` varchar(256) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci,
69+
`text` text CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci,
70+
PRIMARY KEY (`id`)
71+
)
72+
ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;
73+
74+
CREATE TABLE `texts` (
75+
`id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT,
76+
`title_char_col` varchar(256) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci,
77+
`title_col` varchar(256) COLLATE utf8mb4_unicode_ci,
78+
`title` varchar(256)
79+
`text_char_col` text CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci,
80+
`text_col` text COLLATE utf8mb4_unicode_ci,
81+
`text` text,
6882
PRIMARY KEY (`id`)
6983
)
7084
ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;

0 commit comments

Comments
 (0)