Skip to content

Commit 7d34b3b

Browse files
committed
Fix data types for 'COLUMNS' schema. Fixes #802
This brings MySqlConnector in line with Connector/NET's schema for this DataTable.
1 parent a7044f1 commit 7d34b3b

File tree

2 files changed

+53
-9
lines changed

2 files changed

+53
-9
lines changed

src/MySqlConnector/Core/SchemaProvider.cs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -169,29 +169,32 @@ private void FillColumns(DataTable dataTable)
169169
new DataColumn("TABLE_SCHEMA", typeof(string)), // lgtm[cs/local-not-disposed]
170170
new DataColumn("TABLE_NAME", typeof(string)), // lgtm[cs/local-not-disposed]
171171
new DataColumn("COLUMN_NAME", typeof(string)), // lgtm[cs/local-not-disposed]
172-
new DataColumn("ORDINAL_POSITION", typeof(int)), // lgtm[cs/local-not-disposed]
172+
new DataColumn("ORDINAL_POSITION", typeof(uint)), // lgtm[cs/local-not-disposed]
173173
new DataColumn("COLUMN_DEFAULT", typeof(string)), // lgtm[cs/local-not-disposed]
174174
new DataColumn("IS_NULLABLE", typeof(string)), // lgtm[cs/local-not-disposed]
175175
new DataColumn("DATA_TYPE", typeof(string)), // lgtm[cs/local-not-disposed]
176-
new DataColumn("CHARACTER_MAXIMUM_LENGTH", typeof(int)), // lgtm[cs/local-not-disposed]
177-
new DataColumn("CHARACTER_OCTET_LENGTH", typeof(int)), // lgtm[cs/local-not-disposed]
178-
new DataColumn("NUMERIC_PRECISION", typeof(int)), // lgtm[cs/local-not-disposed]
179-
new DataColumn("NUMERIC_SCALE", typeof(int)), // lgtm[cs/local-not-disposed]
180-
new DataColumn("DATETIME_PRECISION", typeof(int)), // lgtm[cs/local-not-disposed]
176+
new DataColumn("CHARACTER_MAXIMUM_LENGTH", typeof(long)), // lgtm[cs/local-not-disposed]
177+
new DataColumn("NUMERIC_PRECISION", typeof(ulong)), // lgtm[cs/local-not-disposed]
178+
new DataColumn("NUMERIC_SCALE", typeof(ulong)), // lgtm[cs/local-not-disposed]
179+
new DataColumn("DATETIME_PRECISION", typeof(uint)), // lgtm[cs/local-not-disposed]
181180
new DataColumn("CHARACTER_SET_NAME", typeof(string)), // lgtm[cs/local-not-disposed]
182181
new DataColumn("COLLATION_NAME", typeof(string)), // lgtm[cs/local-not-disposed]
183-
new DataColumn("COLUMN_TYPE", typeof(string)), // lgtm[cs/local-not-disposed]
184182
new DataColumn("COLUMN_KEY", typeof(string)), // lgtm[cs/local-not-disposed]
185183
new DataColumn("EXTRA", typeof(string)), // lgtm[cs/local-not-disposed]
186184
new DataColumn("PRIVILEGES", typeof(string)), // lgtm[cs/local-not-disposed]
187185
new DataColumn("COLUMN_COMMENT", typeof(string)), // lgtm[cs/local-not-disposed]
188-
new DataColumn("GENERATION_EXPRESSION", typeof(string)), // lgtm[cs/local-not-disposed]
189186
});
190187

188+
using (var command = new MySqlCommand("SELECT 1 FROM INFORMATION_SCHEMA.COLUMNS WHERE table_schema = 'information_schema' AND table_name = 'COLUMNS' AND column_name = 'GENERATION_EXPRESSION';", m_connection))
189+
{
190+
if (command.ExecuteScalar() is object)
191+
dataTable.Columns.Add(new DataColumn("GENERATION_EXPRESSION", typeof(string))); // lgtm[cs/local-not-disposed]
192+
}
193+
191194
using (var command = new MySqlCommand("SELECT 1 FROM INFORMATION_SCHEMA.COLUMNS WHERE table_schema = 'information_schema' AND table_name = 'COLUMNS' AND column_name = 'SRS_ID';", m_connection))
192195
{
193196
if (command.ExecuteScalar() is object)
194-
dataTable.Columns.Add(new DataColumn("SRS_ID", typeof(string))); // lgtm[cs/local-not-disposed]
197+
dataTable.Columns.Add(new DataColumn("SRS_ID", typeof(uint))); // lgtm[cs/local-not-disposed]
195198
}
196199

197200
FillDataTable(dataTable, "COLUMNS");

tests/SideBySide/SchemaProviderTests.cs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,47 @@ public void ReservedWordsSchema()
4040
#endif
4141
}
4242

43+
[Fact]
44+
public void ColumnsSchema()
45+
{
46+
var table = m_database.Connection.GetSchema("Columns");
47+
Assert.NotNull(table);
48+
AssertHasColumn("TABLE_CATALOG", typeof(string));
49+
AssertHasColumn("TABLE_SCHEMA", typeof(string));
50+
AssertHasColumn("TABLE_NAME", typeof(string));
51+
AssertHasColumn("COLUMN_NAME", typeof(string));
52+
AssertHasColumn("ORDINAL_POSITION", typeof(uint));
53+
AssertHasColumn("COLUMN_DEFAULT", typeof(string));
54+
AssertHasColumn("IS_NULLABLE", typeof(string));
55+
AssertHasColumn("DATA_TYPE", typeof(string));
56+
AssertHasColumn("CHARACTER_MAXIMUM_LENGTH", typeof(long));
57+
AssertHasColumn("NUMERIC_PRECISION", typeof(ulong));
58+
AssertHasColumn("NUMERIC_SCALE", typeof(ulong));
59+
AssertHasColumn("DATETIME_PRECISION", typeof(uint));
60+
AssertHasColumn("CHARACTER_SET_NAME", typeof(string));
61+
AssertHasColumn("COLLATION_NAME", typeof(string));
62+
AssertHasColumn("COLUMN_KEY", typeof(string));
63+
AssertHasColumn("EXTRA", typeof(string));
64+
AssertHasColumn("PRIVILEGES", typeof(string));
65+
AssertHasColumn("COLUMN_COMMENT", typeof(string));
66+
67+
void AssertHasColumn(string name, Type type)
68+
{
69+
var column = table.Columns[name];
70+
Assert.NotNull(column);
71+
72+
// allow integral types with a larger positive range
73+
if (type == typeof(int))
74+
Assert.True(type == typeof(int) || type == typeof(uint) || type == typeof(long) || type == typeof(ulong));
75+
else if (type == typeof(uint))
76+
Assert.True(type == typeof(uint) || type == typeof(long) || type == typeof(ulong));
77+
else if (type == typeof(long))
78+
Assert.True(type == typeof(long) || type == typeof(ulong));
79+
else
80+
Assert.Equal(type, column.DataType);
81+
}
82+
}
83+
4384
readonly DatabaseFixture m_database;
4485
}
4586
}

0 commit comments

Comments
 (0)