Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #821 from stood/mysqldesc_for_reverse_task

Mysqldesc for reverse task
  • Loading branch information...
commit 3f5e0101595cdfd9878ad95df998cc6c19ff75e5 2 parents 4566669 + 62afa32
@willdurand willdurand authored
View
30 generator/lib/reverse/mysql/MysqlSchemaParser.php
@@ -153,7 +153,7 @@ public function parse(Database $database, Task $task = null)
*/
protected function addColumns(Table $table)
{
- $stmt = $this->dbh->query("SHOW COLUMNS FROM `" . $table->getName() . "`");
+ $stmt = $this->dbh->query("SHOW FULL COLUMNS FROM `" . $table->getName() . "`");
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
$column = $this->getColumnFromRow($row, $table);
@@ -179,6 +179,7 @@ public function getColumnFromRow($row, Table $table)
$precision = null;
$scale = null;
$sqlType = false;
+ $desc = $row['Comment'];
$regexp = '/^
(\w+) # column type [1]
@@ -264,10 +265,37 @@ public function getColumnFromRow($row, Table $table)
$column->addVendorInfo($vi);
}
+ if ($desc){
+ if(!$this->isUtf8($desc))
+ $desc = utf8_encode($desc);
+ $column->setDescription($desc);
+ }
+
return $column;
}
/**
+ * Return True if $string is utf8
+ *
+ * @param string $string
+ *
+ * @return boolean
+ */
+ protected function isUtf8( $string)
+ {
+ return preg_match('%^(?:
+ [\x09\x0A\x0D\x20-\x7E] # ASCII
+ | [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
+ | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
+ | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
+ | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
+ | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
+ | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
+ | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
+ )*$%xs', $string);
+ }
+
+ /**
* Load foreign keys for this table.
*/
protected function addForeignKeys(Table $table)
View
2  test/fixtures/reverse/mysql/build/sql/schema.sql
@@ -4,7 +4,7 @@ DROP VIEW IF EXISTS view_book_titles;
CREATE TABLE book
(
id INTEGER NOT NULL AUTO_INCREMENT COMMENT 'Book Id',
- title VARCHAR(255) NOT NULL COMMENT 'Book Title',
+ title VARCHAR(255) NOT NULL COMMENT 'Book Title with accent éài',
isbn VARCHAR(24) NOT NULL COMMENT 'ISBN Number',
price FLOAT COMMENT 'Price of the book.',
PRIMARY KEY (id)
View
22 test/testsuite/generator/reverse/mysql/MysqlSchemaParserTest.php
@@ -89,6 +89,28 @@ public function testDecimal()
$this->assertEquals($c1->getSize(), $c2->getSize());
$this->assertEquals($c1->getScale(), $c2->getScale());
}
+
+ public function testDescColumn()
+ {
+ $schema = '<database name="reverse_bookstore"><table name="book"><column name="title" type="VARCHAR" size="255" description="Book Title with accent éài" /></table></database>';
+ $xtad = new XmlToAppData();
+ $appData = $xtad->parseString($schema);
+ $database = $appData->getDatabase();
+ $table = $database->getTable('book');
+ $c1 = $table->getColumn('title');
+
+ $parser = new MysqlSchemaParser(Propel::getConnection('reverse-bookstore'));
+ $parser->setGeneratorConfig(new QuickGeneratorConfig());
+
+ $database = new Database();
+ $database->setPlatform(new DefaultPlatform());
+ $parser->parse($database);
+
+ $c2 = $database->getTable('book')->getColumn('title');
+
+ $this->assertEquals($c1->getDescription(), $c2->getDescription());
+
+ }
}
class OpenedPropelConvertConfTask extends PropelConvertConfTask
Please sign in to comment.
Something went wrong with that request. Please try again.