From 47bb30c5357d7800d1ff44898a0f660b2222d6fc Mon Sep 17 00:00:00 2001 From: Kijin Sung Date: Tue, 30 Jun 2020 00:40:10 +0900 Subject: [PATCH] Implement getColumnInfo() --- common/framework/db.php | 32 +++++++++++++++++++--- common/framework/parsers/dbtableparser.php | 1 + 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/common/framework/db.php b/common/framework/db.php index 60705b3e2..aee81206f 100644 --- a/common/framework/db.php +++ b/common/framework/db.php @@ -813,12 +813,36 @@ class DB * * @param string $table_name * @param string $column_name - * @return Parsers\DBTable\Column; + * @return object */ - public function getColumnInfo(string $table_name, string $column_name): Parsers\DBTable\Column + public function getColumnInfo(string $table_name, string $column_name) { - // TODO - return new Parsers\DBTable\Column; + // If column information is not found, return false. + $stmt = $this->_query(sprintf("SHOW FIELDS FROM `%s` WHERE Field = '%s'", $this->addQuotes($this->_prefix . $table_name), $this->addQuotes($column_name))); + $column_info = $this->_fetch($stmt); + if (!$column_info) + { + return false; + } + + // Reorganize the type information. + $dbtype = strtolower($column_info->{'Type'}); + if (preg_match('/^([a-z0-9_]+)\(([0-9,\s]+)\)$/i', $dbtype, $matches)) + { + $dbtype = $matches[1]; + $size = $matches[2]; + } + $xetype = Parsers\DBTableParser::getXEType($dbtype, $size ?: ''); + + // Return the result as an object. + return (object)array( + 'name' => $column_name, + 'dbtype' => $dbtype, + 'xetype' => $xetype, + 'size' => $size, + 'default_value' => $column_info->{'Default'}, + 'notnull' => strncmp($column_info->{'Null'}, 'NO', 2) == 0 ? true : false, + ); } /** diff --git a/common/framework/parsers/dbtableparser.php b/common/framework/parsers/dbtableparser.php index f91f6a1e8..1d1b395b5 100644 --- a/common/framework/parsers/dbtableparser.php +++ b/common/framework/parsers/dbtableparser.php @@ -226,6 +226,7 @@ class DBTableParser extends BaseParser */ public static function getXEType(string $type, string $size): string { + $type = strtolower($type); switch ($type) { case 'bigint':