From 2ef655beac5b79337e43028ddd0ecea7a0379b72 Mon Sep 17 00:00:00 2001 From: stronk7 Date: Tue, 25 Aug 2009 17:41:10 +0000 Subject: [PATCH] Fix get_columns() for mssql adodb driver by custom-implementing it. This implementation should be the used one in native drivers. --- lib/dml/mssql_adodb_moodle_database.php | 52 +++++++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/lib/dml/mssql_adodb_moodle_database.php b/lib/dml/mssql_adodb_moodle_database.php index 4852c83fef..f85da625f0 100644 --- a/lib/dml/mssql_adodb_moodle_database.php +++ b/lib/dml/mssql_adodb_moodle_database.php @@ -449,4 +449,56 @@ class mssql_adodb_moodle_database extends adodb_moodle_database { return true; } + + public function get_columns($table, $usecache=true) { + if ($usecache and isset($this->columns[$table])) { + return $this->columns[$table]; + } + + $this->columns[$table] = array(); + + $tablename = strtoupper($this->prefix.$table); + + $sql = "SELECT column_name AS name, + data_type AS type, + numeric_precision AS max_length, + character_maximum_length AS char_max_length, + numeric_scale AS scale, + is_nullable AS is_nullable, + columnproperty(object_id(quotename(TABLE_SCHEMA) + '.' + + quotename(TABLE_NAME)), COLUMN_NAME, 'IsIdentity') AS auto_increment, + column_default AS default_value + FROM INFORMATION_SCHEMA.Columns + WHERE TABLE_NAME = '$tablename' + ORDER BY ordinal_position"; + + $this->query_start($sql, null, SQL_QUERY_AUX); + $rs = $this->adodb->Execute($sql); + $this->query_end($rs); + + $columns = $this->adodb_recordset_to_array($rs); + $rs->Close(); + + if (!$columns) { + return array(); + } + + $this->columns[$table] = array(); + + foreach ($columns as $column) { + $dict = NewDataDictionary($this->adodb); // use dictionary because mssql driver lacks proper MetaType() function + $column->meta_type = substr($dict->MetaType($column), 0 ,1); // only 1 character + $column->meta_type = $column->meta_type == 'F' ? 'N' : $column->meta_type; // floats are numbers for us + $column->meta_type = ($column->auto_increment && $column->meta_type == 'I') ? 'R' : $column->meta_type; // Proper 'R' + $column->max_length = $column->meta_type == 'C' ? $column->char_max_length : $column->max_length; //Pick correct for Chars + $column->max_length = ($column->meta_type == 'X' || $column->meta_type == 'B') ? -1 : $column->max_length; // -1 for xLOB + $column->auto_increment = $column->auto_increment ? true : false; + $column->not_null = $column->is_nullable == 'NO' ? true : false; // Process not_null + $column->has_default = !empty($column->default_value); // Calculate has_default + $column->default_value = preg_replace("/^[\(N]+[']?(.*?)[']?[\)]+$/", '\\1', $column->default_value); // Clean default + $this->columns[$table][$column->name] = new database_column_info($column); + } + + return $this->columns[$table]; + } } -- 2.39.5