From da82566144ebbf73e8d959e359fbcbc521041c9b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E7=81=BF=E5=BC=BA?= <64899484@qq.com> Date: Sun, 10 Jan 2021 23:17:22 +0800 Subject: [PATCH 1/6] feature: add mysql table comment --- phalcon/Db/Dialect/Mysql.zep | 12 +++++++++++- tests/_data/assets/db/schemas/mysql_schema.sql | 4 ++-- .../database/Db/Adapter/Pdo/DescribeColumnsCest.php | 2 +- .../Db/Adapter/Pdo/DbDescribeMysqlCest.php | 1 + .../Db/Adapter/Pdo/Mysql/TableOptionsCest.php | 1 + 5 files changed, 16 insertions(+), 4 deletions(-) diff --git a/phalcon/Db/Dialect/Mysql.zep b/phalcon/Db/Dialect/Mysql.zep index cdc312f4b76..7dec4ad5a0c 100644 --- a/phalcon/Db/Dialect/Mysql.zep +++ b/phalcon/Db/Dialect/Mysql.zep @@ -798,7 +798,7 @@ class Mysql extends Dialect { string sql; - let sql = "SELECT TABLES.TABLE_TYPE AS table_type,TABLES.AUTO_INCREMENT AS auto_increment,TABLES.ENGINE AS engine,TABLES.TABLE_COLLATION AS table_collation FROM INFORMATION_SCHEMA.TABLES WHERE "; + let sql = "SELECT TABLES.TABLE_TYPE AS table_type,TABLES.AUTO_INCREMENT AS auto_increment,TABLES.ENGINE AS engine,TABLES.TABLE_COLLATION AS table_collation, TABLES.TABLE_COMMENT AS table_comment FROM INFORMATION_SCHEMA.TABLES WHERE "; if schema { return sql . "TABLES.TABLE_SCHEMA = '" . schema . "' AND TABLES.TABLE_NAME = '" . table . "'"; @@ -878,6 +878,16 @@ class Mysql extends Dialect } } + /** + * Check if there is an comment option + */ + if fetch comment, options["TABLE_COMMENT"] { + if comment { + let tableOptions[] = "COMMENT=" . comment; + } + } + + return join(" ", tableOptions); } diff --git a/tests/_data/assets/db/schemas/mysql_schema.sql b/tests/_data/assets/db/schemas/mysql_schema.sql index af77d6e531f..8330adff8af 100644 --- a/tests/_data/assets/db/schemas/mysql_schema.sql +++ b/tests/_data/assets/db/schemas/mysql_schema.sql @@ -118,7 +118,7 @@ CREATE TABLE `personas` KEY `estado` (`estado`), KEY `ciudad_id` (`ciudad_id`), KEY `estado_2` (`estado`,`nombres`) -) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE =utf8_unicode_ci; +) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE =utf8_unicode_ci COMMENT='test table options'; INSERT INTO `personas` VALUES ('1', 3, 'HUANG ZHENGQUIN', '191821112', 'CRA 25 CALLE 100', '@yahoo.es', '2011-02-03', 127591, '2011-05-18', '6930.00', 'I'), @@ -12764,7 +12764,7 @@ create table dialect_table unique key dialect_table_unique (field_integer), key dialect_table_index (field_bigint), key dialect_table_two_fields (field_char, field_char_default) -) ENGINE=InnoDB DEFAULT CHARSET=utf8; +) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='test table options'; drop table if exists `dialect_table_remote`; create table dialect_table_remote diff --git a/tests/database/Db/Adapter/Pdo/DescribeColumnsCest.php b/tests/database/Db/Adapter/Pdo/DescribeColumnsCest.php index 6a588891ee5..aedea4983e2 100644 --- a/tests/database/Db/Adapter/Pdo/DescribeColumnsCest.php +++ b/tests/database/Db/Adapter/Pdo/DescribeColumnsCest.php @@ -66,7 +66,7 @@ public function dbAdapterPdoDescribeColumnsOnUpdate(DatabaseTester $I) $this->migration->insert(1, $now, $now); - $columns = $db->describeColumns($this->migration->getTable()); + $columns = $db->($this->migration->getTable()); $I->assertSame('CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP', $columns[2]->getDefault()); $I->assertSame('NULL on update CURRENT_TIMESTAMP', $columns[3]->getDefault()); diff --git a/tests/tocheck-database/Db/Adapter/Pdo/DbDescribeMysqlCest.php b/tests/tocheck-database/Db/Adapter/Pdo/DbDescribeMysqlCest.php index 947c1097e38..8bdcdbda7da 100644 --- a/tests/tocheck-database/Db/Adapter/Pdo/DbDescribeMysqlCest.php +++ b/tests/tocheck-database/Db/Adapter/Pdo/DbDescribeMysqlCest.php @@ -97,6 +97,7 @@ public function dbMySqlTableOptions(IntegrationTester $I) 'auto_increment' => null, 'engine' => 'InnoDB', 'table_collation' => 'utf8_unicode_ci', + 'table_comment' => 'test table options', ]; $I->assertEquals( diff --git a/tests/tocheck-database/Db/Adapter/Pdo/Mysql/TableOptionsCest.php b/tests/tocheck-database/Db/Adapter/Pdo/Mysql/TableOptionsCest.php index a7b5233234e..6a970b0d377 100644 --- a/tests/tocheck-database/Db/Adapter/Pdo/Mysql/TableOptionsCest.php +++ b/tests/tocheck-database/Db/Adapter/Pdo/Mysql/TableOptionsCest.php @@ -37,6 +37,7 @@ public function dbAdapterPdoMysqlTableOptions(IntegrationTester $I) 'engine' => 'InnoDB', 'table_collation' => 'utf8_general_ci', 'table_type' => 'BASE TABLE', + 'table_comment' => 'test table options', ]; $actual = $this->connection->tableOptions( From ba02bda000d2086125bad807869049a943f191fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E7=81=BF=E5=BC=BA?= <64899484@qq.com> Date: Sun, 10 Jan 2021 23:18:56 +0800 Subject: [PATCH 2/6] revert: error comment --- tests/database/Db/Adapter/Pdo/DescribeColumnsCest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/database/Db/Adapter/Pdo/DescribeColumnsCest.php b/tests/database/Db/Adapter/Pdo/DescribeColumnsCest.php index aedea4983e2..6a588891ee5 100644 --- a/tests/database/Db/Adapter/Pdo/DescribeColumnsCest.php +++ b/tests/database/Db/Adapter/Pdo/DescribeColumnsCest.php @@ -66,7 +66,7 @@ public function dbAdapterPdoDescribeColumnsOnUpdate(DatabaseTester $I) $this->migration->insert(1, $now, $now); - $columns = $db->($this->migration->getTable()); + $columns = $db->describeColumns($this->migration->getTable()); $I->assertSame('CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP', $columns[2]->getDefault()); $I->assertSame('NULL on update CURRENT_TIMESTAMP', $columns[3]->getDefault()); From 9ae680ab9dc7804250c9ba15b515d9cf8b78e740 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E7=81=BF=E5=BC=BA?= <64899484@qq.com> Date: Sun, 10 Jan 2021 23:44:22 +0800 Subject: [PATCH 3/6] fix: add comment var --- phalcon/Db/Dialect/Mysql.zep | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/phalcon/Db/Dialect/Mysql.zep b/phalcon/Db/Dialect/Mysql.zep index 7dec4ad5a0c..94e622e765c 100644 --- a/phalcon/Db/Dialect/Mysql.zep +++ b/phalcon/Db/Dialect/Mysql.zep @@ -840,7 +840,7 @@ class Mysql extends Dialect */ protected function getTableOptions(array! definition) -> string { - var options, engine, autoIncrement, tableCollation, collationParts; + var options, engine, autoIncrement, tableCollation, collationParts, comment; array tableOptions; if !fetch options, definition["options"] { From 1a3f5e3839ef08427b9368d19625774f9102f109 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E7=81=BF=E5=BC=BA?= <64899484@qq.com> Date: Mon, 11 Jan 2021 22:09:10 +0800 Subject: [PATCH 4/6] fix: pgsql add comment --- phalcon/Db/Dialect/Postgresql.zep | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/phalcon/Db/Dialect/Postgresql.zep b/phalcon/Db/Dialect/Postgresql.zep index d95d555e4e6..a4e65f4b50b 100644 --- a/phalcon/Db/Dialect/Postgresql.zep +++ b/phalcon/Db/Dialect/Postgresql.zep @@ -124,7 +124,7 @@ class Postgresql extends Dialect { var temporary, options, table, columns, column, indexes, index, reference, references, indexName, indexType, onDelete, onUpdate, - columnDefinition; + columnDefinition, tableComment; array createLines, primaryColumns; string indexSql, indexSqlAfterCreate, columnLine, referenceSql, sql; @@ -257,6 +257,16 @@ class Postgresql extends Dialect let sql .= join(",\n\t", createLines) . "\n)"; if isset definition["options"] { let sql .= " " . this->getTableOptions(definition); + if fetch options, definition["options"] { + /** + * Check if there is an comment option + */ + if fetch tableComment, options["TABLE_COMMENT"] { + if tableComment { + let sql .= "; COMMENT ON TABLE " . table . " IS '".table_comment."'"; + } + } + } } let sql .= ";" . indexSqlAfterCreate; From 2a82e078125a95e2074991ff294b376b7cfad1f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E7=81=BF=E5=BC=BA?= <64899484@qq.com> Date: Mon, 11 Jan 2021 22:13:25 +0800 Subject: [PATCH 5/6] fix: pgsql add comment --- phalcon/Db/Dialect/Postgresql.zep | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/phalcon/Db/Dialect/Postgresql.zep b/phalcon/Db/Dialect/Postgresql.zep index a4e65f4b50b..7ee5c64b1ce 100644 --- a/phalcon/Db/Dialect/Postgresql.zep +++ b/phalcon/Db/Dialect/Postgresql.zep @@ -263,7 +263,7 @@ class Postgresql extends Dialect */ if fetch tableComment, options["TABLE_COMMENT"] { if tableComment { - let sql .= "; COMMENT ON TABLE " . table . " IS '".table_comment."'"; + let sql .= "; COMMENT ON TABLE " . table . " IS '".tableComment."'"; } } } From 030b2640c9bc7c5d9e13b5b470ad88d7bb3cab28 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E7=81=BF=E5=BC=BA?= <64899484@qq.com> Date: Mon, 11 Jan 2021 22:44:37 +0800 Subject: [PATCH 6/6] fix: pgsql add table comment --- phalcon/Db/Dialect/Postgresql.zep | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/phalcon/Db/Dialect/Postgresql.zep b/phalcon/Db/Dialect/Postgresql.zep index 7ee5c64b1ce..b5d4fb7514d 100644 --- a/phalcon/Db/Dialect/Postgresql.zep +++ b/phalcon/Db/Dialect/Postgresql.zep @@ -666,7 +666,10 @@ class Postgresql extends Dialect */ public function tableOptions(string! table, string schema = null) -> string { - return ""; + string sql; + + let sql = "select cast(obj_description(relfilenode,'pg_class') as varchar) as table_comment from pg_class where relname ='" . table . "'" ; + return sql; } /**