|
| 1 | +#if !NETSTANDARD1_3 |
| 2 | +using System; |
| 3 | +using System.Collections.Generic; |
| 4 | +using System.Data; |
| 5 | +using System.Linq; |
| 6 | +using MySql.Data.MySqlClient.Types; |
| 7 | + |
| 8 | +namespace MySql.Data.MySqlClient |
| 9 | +{ |
| 10 | + internal sealed class SchemaProvider |
| 11 | + { |
| 12 | + public SchemaProvider(MySqlConnection connection) |
| 13 | + { |
| 14 | + m_connection = connection; |
| 15 | + m_schemaCollections = new Dictionary<string, Action<DataTable>> |
| 16 | + { |
| 17 | + { "MetaDataCollections", FillMetadataCollections }, |
| 18 | + { "DataTypes", FillDataTypes }, |
| 19 | + { "Procedures", FillProcedures } |
| 20 | + }; |
| 21 | + } |
| 22 | + |
| 23 | + public DataTable GetSchema() => GetSchema("MetaDataCollections"); |
| 24 | + |
| 25 | + public DataTable GetSchema(string collectionName) |
| 26 | + { |
| 27 | + if (!m_schemaCollections.TryGetValue(collectionName, out var fillAction)) |
| 28 | + throw new ArgumentException("Invalid collection name.", nameof(collectionName)); |
| 29 | + |
| 30 | + var dataTable = new DataTable(collectionName); |
| 31 | + fillAction(dataTable); |
| 32 | + return dataTable; |
| 33 | + } |
| 34 | + |
| 35 | + private void FillMetadataCollections(DataTable dataTable) |
| 36 | + { |
| 37 | + dataTable.Columns.AddRange(new[] { |
| 38 | + new DataColumn("CollectionName", typeof(string)), |
| 39 | + new DataColumn("NumberOfRestrictions", typeof(int)), |
| 40 | + new DataColumn("NumberOfIdentifierParts", typeof(int)) |
| 41 | + }); |
| 42 | + |
| 43 | + foreach (var collectionName in m_schemaCollections.Keys) |
| 44 | + dataTable.Rows.Add(collectionName, 0, 0); |
| 45 | + } |
| 46 | + |
| 47 | + private void FillDataTypes(DataTable dataTable) |
| 48 | + { |
| 49 | + dataTable.Columns.AddRange(new[] |
| 50 | + { |
| 51 | + new DataColumn("TypeName", typeof(string)), |
| 52 | + new DataColumn("ProviderDbType", typeof(int)), |
| 53 | + new DataColumn("ColumnSize", typeof(long)), |
| 54 | + new DataColumn("CreateFormat", typeof(string)), |
| 55 | + new DataColumn("CreateParameters", typeof(string)), |
| 56 | + new DataColumn("DataType", typeof(string)), |
| 57 | + new DataColumn("IsAutoIncrementable", typeof(bool)), |
| 58 | + new DataColumn("IsBestMatch", typeof(bool)), |
| 59 | + new DataColumn("IsCaseSensitive", typeof(bool)), |
| 60 | + new DataColumn("IsFixedLength", typeof(bool)), |
| 61 | + new DataColumn("IsFixedPrecisionScale", typeof(bool)), |
| 62 | + new DataColumn("IsLong", typeof(bool)), |
| 63 | + new DataColumn("IsNullable", typeof(bool)), |
| 64 | + new DataColumn("IsSearchable", typeof(bool)), |
| 65 | + new DataColumn("IsSearchableWithLike", typeof(bool)), |
| 66 | + new DataColumn("IsUnsigned", typeof(bool)), |
| 67 | + new DataColumn("MaximumScale", typeof(short)), |
| 68 | + new DataColumn("MinimumScale", typeof(short)), |
| 69 | + new DataColumn("IsConcurrencyType", typeof(bool)), |
| 70 | + new DataColumn("IsLiteralSupported", typeof(bool)), |
| 71 | + new DataColumn("LiteralPrefix", typeof(string)), |
| 72 | + new DataColumn("LiteralSuffix", typeof(string)), |
| 73 | + new DataColumn("NativeDataType", typeof(string)), |
| 74 | + }); |
| 75 | + |
| 76 | + var clrTypes = new HashSet<string>(); |
| 77 | + foreach (var columnType in TypeMapper.Instance.GetColumnTypeMetadata()) |
| 78 | + { |
| 79 | + // hard-code a few types to not appear in the schema table |
| 80 | + var mySqlDbType = columnType.MySqlDbType; |
| 81 | + if (mySqlDbType == MySqlDbType.Decimal || mySqlDbType == MySqlDbType.Newdate || mySqlDbType == MySqlDbType.Null || mySqlDbType == MySqlDbType.VarString) |
| 82 | + continue; |
| 83 | + if (mySqlDbType == MySqlDbType.Bool && columnType.IsUnsigned) |
| 84 | + continue; |
| 85 | + |
| 86 | + // set miscellaneous properties in code (rather than being data-driven) |
| 87 | + var clrType = columnType.DbTypeMapping.ClrType; |
| 88 | + var clrTypeName = clrType.ToString(); |
| 89 | + var dataTypeName = mySqlDbType == MySqlDbType.Guid ? "GUID" : |
| 90 | + mySqlDbType == MySqlDbType.Bool ? "BOOL" : columnType.DataTypeName; |
| 91 | + var isAutoIncrementable = mySqlDbType == MySqlDbType.Byte || mySqlDbType == MySqlDbType.Int16 || mySqlDbType == MySqlDbType.Int24 || mySqlDbType == MySqlDbType.Int32 || mySqlDbType == MySqlDbType.Int64 || |
| 92 | + mySqlDbType == MySqlDbType.UByte || mySqlDbType == MySqlDbType.UInt16 || mySqlDbType == MySqlDbType.UInt24 || mySqlDbType == MySqlDbType.UInt32 || mySqlDbType == MySqlDbType.UInt64; |
| 93 | + var isBestMatch = clrTypes.Add(clrTypeName); |
| 94 | + var isFixedLength = isAutoIncrementable || |
| 95 | + mySqlDbType == MySqlDbType.Date || mySqlDbType == MySqlDbType.DateTime || mySqlDbType == MySqlDbType.Time || mySqlDbType == MySqlDbType.Timestamp || |
| 96 | + mySqlDbType == MySqlDbType.Double || mySqlDbType == MySqlDbType.Float || mySqlDbType == MySqlDbType.Year || mySqlDbType == MySqlDbType.Guid || mySqlDbType == MySqlDbType.Bool; |
| 97 | + var isFixedPrecisionScale = isFixedLength || |
| 98 | + mySqlDbType == MySqlDbType.Bit || mySqlDbType == MySqlDbType.NewDecimal; |
| 99 | + var isLong = mySqlDbType == MySqlDbType.Blob || mySqlDbType == MySqlDbType.MediumBlob || mySqlDbType == MySqlDbType.LongBlob; |
| 100 | + |
| 101 | + // map ColumnTypeMetadata to the row for this data type |
| 102 | + var createFormatParts = columnType.CreateFormat.Split(';'); |
| 103 | + dataTable.Rows.Add( |
| 104 | + dataTypeName, |
| 105 | + (int) mySqlDbType, |
| 106 | + columnType.ColumnSize, |
| 107 | + createFormatParts[0], |
| 108 | + createFormatParts.Length == 1 ? null : createFormatParts[1], |
| 109 | + clrTypeName, |
| 110 | + isAutoIncrementable, |
| 111 | + isBestMatch, |
| 112 | + false, |
| 113 | + isFixedLength, |
| 114 | + isFixedPrecisionScale, |
| 115 | + isLong, |
| 116 | + true, |
| 117 | + clrType != typeof(byte[]), |
| 118 | + clrType == typeof(string), |
| 119 | + columnType.IsUnsigned, |
| 120 | + DBNull.Value, |
| 121 | + DBNull.Value, |
| 122 | + DBNull.Value, |
| 123 | + true, |
| 124 | + DBNull.Value, |
| 125 | + DBNull.Value, |
| 126 | + null |
| 127 | + ); |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + private void FillProcedures(DataTable dataTable) |
| 132 | + { |
| 133 | + dataTable.Columns.AddRange(new[] |
| 134 | + { |
| 135 | + new DataColumn("SPECIFIC_NAME", typeof(string)), |
| 136 | + new DataColumn("ROUTINE_CATALOG", typeof(string)), |
| 137 | + new DataColumn("ROUTINE_SCHEMA", typeof(string)), |
| 138 | + new DataColumn("ROUTINE_NAME", typeof(string)), |
| 139 | + new DataColumn("ROUTINE_TYPE", typeof(string)), |
| 140 | + new DataColumn("DTD_IDENTIFIER", typeof(string)), |
| 141 | + new DataColumn("ROUTINE_BODY", typeof(string)), |
| 142 | + new DataColumn("ROUTINE_DEFINITION", typeof(string)), |
| 143 | + new DataColumn("EXTERNAL_NAME", typeof(string)), |
| 144 | + new DataColumn("EXTERNAL_LANGUAGE", typeof(string)), |
| 145 | + new DataColumn("PARAMETER_STYLE", typeof(string)), |
| 146 | + new DataColumn("IS_DETERMINISTIC", typeof(string)), |
| 147 | + new DataColumn("SQL_DATA_ACCESS", typeof(string)), |
| 148 | + new DataColumn("SQL_PATH", typeof(string)), |
| 149 | + new DataColumn("SECURITY_TYPE", typeof(string)), |
| 150 | + new DataColumn("CREATED", typeof(DateTime)), |
| 151 | + new DataColumn("LAST_ALTERED", typeof(DateTime)), |
| 152 | + new DataColumn("SQL_MODE", typeof(string)), |
| 153 | + new DataColumn("ROUTINE_COMMENT", typeof(string)), |
| 154 | + new DataColumn("DEFINER", typeof(string)), |
| 155 | + }); |
| 156 | + |
| 157 | + Action close = null; |
| 158 | + if (m_connection.State != ConnectionState.Open) |
| 159 | + { |
| 160 | + m_connection.Open(); |
| 161 | + close = m_connection.Close; |
| 162 | + } |
| 163 | + |
| 164 | + using (var command = m_connection.CreateCommand()) |
| 165 | + { |
| 166 | + command.CommandText = "SELECT " + string.Join(", ", dataTable.Columns.Cast<DataColumn>().Select(x => x.ColumnName)) + " FROM INFORMATION_SCHEMA.ROUTINES;"; |
| 167 | + using (var reader = command.ExecuteReader()) |
| 168 | + { |
| 169 | + while (reader.Read()) |
| 170 | + { |
| 171 | + var rowValues = new object[dataTable.Columns.Count]; |
| 172 | + reader.GetValues(rowValues); |
| 173 | + dataTable.Rows.Add(rowValues); |
| 174 | + } |
| 175 | + } |
| 176 | + } |
| 177 | + |
| 178 | + close?.Invoke(); |
| 179 | + } |
| 180 | + |
| 181 | + readonly MySqlConnection m_connection; |
| 182 | + readonly Dictionary<string, Action<DataTable>> m_schemaCollections; |
| 183 | + } |
| 184 | +} |
| 185 | +#endif |
0 commit comments