You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

250 lines
7.4 KiB

4 years ago
  1. #if MYSQL_6_10
  2. // Copyright ?2013, 2018, Oracle and/or its affiliates. All rights reserved.
  3. //
  4. // MySQL Connector/NET is licensed under the terms of the GPLv2
  5. // <http://www.gnu.org/licenses/old-licenses/gpl-2.0.html>, like most
  6. // MySQL Connectors. There are special exceptions to the terms and
  7. // conditions of the GPLv2 as it is applied to this software, see the
  8. // FLOSS License Exception
  9. // <http://www.mysql.com/about/legal/licensing/foss-exception.html>.
  10. //
  11. // This program is free software; you can redistribute it and/or modify
  12. // it under the terms of the GNU General Public License as published
  13. // by the Free Software Foundation; version 2 of the License.
  14. //
  15. // This program is distributed in the hope that it will be useful, but
  16. // WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  17. // or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  18. // for more details.
  19. //
  20. // You should have received a copy of the GNU General Public License along
  21. // with this program; if not, write to the Free Software Foundation, Inc.,
  22. // 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  23. using System;
  24. using System.Collections;
  25. using System.Collections.Generic;
  26. using System.Data;
  27. using System.Text;
  28. namespace Externals.MySql.Data.MySqlClient
  29. {
  30. /// <summary>
  31. /// Represents a schema and its contents.
  32. /// </summary>
  33. internal partial class MySqlSchemaCollection
  34. {
  35. private readonly List<SchemaColumn> _columns = new List<SchemaColumn>();
  36. private readonly List<MySqlSchemaRow> _rows = new List<MySqlSchemaRow>();
  37. #if !NETSTANDARD1_3
  38. private readonly DataTable _table = null;
  39. #endif
  40. public MySqlSchemaCollection()
  41. {
  42. Mapping = new Dictionary<string, int>(StringComparer.OrdinalIgnoreCase);
  43. LogicalMappings = new Dictionary<int, int>();
  44. }
  45. public MySqlSchemaCollection(string name) : this()
  46. {
  47. Name = name;
  48. }
  49. #if !NETSTANDARD1_3
  50. public MySqlSchemaCollection(DataTable dt) : this()
  51. {
  52. // cache the original datatable to avoid the overhead of creating again whenever possible.
  53. _table = dt;
  54. int i = 0;
  55. foreach (DataColumn dc in dt.Columns)
  56. {
  57. Columns.Add(new SchemaColumn() { Name = dc.ColumnName, Type = dc.DataType });
  58. Mapping.Add(dc.ColumnName, i++);
  59. LogicalMappings[Columns.Count - 1] = Columns.Count - 1;
  60. }
  61. foreach (DataRow dr in dt.Rows)
  62. {
  63. MySqlSchemaRow row = new MySqlSchemaRow(this);
  64. for (i = 0; i < Columns.Count; i++)
  65. {
  66. row[i] = dr[i];
  67. }
  68. Rows.Add(row);
  69. }
  70. }
  71. #endif
  72. internal Dictionary<string, int> Mapping;
  73. internal Dictionary<int, int> LogicalMappings;
  74. /// <summary>
  75. /// Gets or sets the name of the schema.
  76. /// </summary>
  77. public string Name { get; set; }
  78. /// <summary>
  79. /// Gets the list of columns in the schema.
  80. /// </summary>
  81. public IList<SchemaColumn> Columns => _columns;
  82. /// <summary>
  83. /// Gets the list of rows in the schema.
  84. /// </summary>
  85. public IList<MySqlSchemaRow> Rows => _rows;
  86. internal SchemaColumn AddColumn(string name, Type t)
  87. {
  88. SchemaColumn c = new SchemaColumn
  89. {
  90. Name = name,
  91. Type = t
  92. };
  93. _columns.Add(c);
  94. Mapping.Add(name, _columns.Count - 1);
  95. LogicalMappings[_columns.Count - 1] = _columns.Count - 1;
  96. return c;
  97. }
  98. internal int ColumnIndex(string name)
  99. {
  100. int index = -1;
  101. for (int i = 0; i < _columns.Count; i++)
  102. {
  103. SchemaColumn c = _columns[i];
  104. if (String.Compare(c.Name, name, StringComparison.OrdinalIgnoreCase) != 0) continue;
  105. index = i;
  106. break;
  107. }
  108. return index;
  109. }
  110. internal void RemoveColumn(string name)
  111. {
  112. int index = ColumnIndex(name);
  113. if (index == -1)
  114. throw new InvalidOperationException();
  115. _columns.RemoveAt(index);
  116. for (int i = index; i < Columns.Count; i++)
  117. LogicalMappings[i] = LogicalMappings[i] + 1;
  118. }
  119. internal bool ContainsColumn(string name)
  120. {
  121. return ColumnIndex(name) >= 0;
  122. }
  123. internal MySqlSchemaRow AddRow()
  124. {
  125. MySqlSchemaRow r = new MySqlSchemaRow(this);
  126. _rows.Add(r);
  127. return r;
  128. }
  129. internal MySqlSchemaRow NewRow()
  130. {
  131. MySqlSchemaRow r = new MySqlSchemaRow(this);
  132. return r;
  133. }
  134. #if !NETSTANDARD1_3
  135. internal DataTable AsDataTable()
  136. {
  137. if (_table != null) return _table;
  138. DataTable dt = new DataTable(Name);
  139. foreach (SchemaColumn col in Columns)
  140. dt.Columns.Add(col.Name, col.Type);
  141. foreach (MySqlSchemaRow row in Rows)
  142. {
  143. DataRow newRow = dt.NewRow();
  144. for (int i = 0; i < dt.Columns.Count; i++)
  145. newRow[i] = row[i] == null ? DBNull.Value : row[i];
  146. dt.Rows.Add(newRow);
  147. }
  148. return dt;
  149. }
  150. #endif
  151. }
  152. /// <summary>
  153. /// Represents a row within a schema.
  154. /// </summary>
  155. internal class MySqlSchemaRow
  156. {
  157. private Dictionary<int, object> _data;
  158. public MySqlSchemaRow(MySqlSchemaCollection c)
  159. {
  160. Collection = c;
  161. InitMetadata();
  162. }
  163. internal void InitMetadata()
  164. {
  165. _data = new Dictionary<int, object>();
  166. }
  167. internal MySqlSchemaCollection Collection { get; }
  168. internal object this[string s]
  169. {
  170. get { return GetValueForName(s); }
  171. set { SetValueForName(s, value); }
  172. }
  173. internal object this[int i]
  174. {
  175. get
  176. {
  177. int idx = Collection.LogicalMappings[i];
  178. if (!_data.ContainsKey(idx))
  179. _data[idx] = null;
  180. return _data[idx];
  181. }
  182. set { _data[Collection.LogicalMappings[i]] = value; }
  183. }
  184. private void SetValueForName(string colName, object value)
  185. {
  186. int index = Collection.Mapping[colName];
  187. this[index] = value;
  188. }
  189. private object GetValueForName(string colName)
  190. {
  191. int index = Collection.Mapping[colName];
  192. if (!_data.ContainsKey(index))
  193. _data[index] = null;
  194. return this[index];
  195. }
  196. internal void CopyRow(MySqlSchemaRow row)
  197. {
  198. if (Collection.Columns.Count != row.Collection.Columns.Count)
  199. throw new InvalidOperationException("column count doesn't match");
  200. for (int i = 0; i < Collection.Columns.Count; i++)
  201. row[i] = this[i];
  202. }
  203. }
  204. /// <summary>
  205. /// Represents a column within a schema.
  206. /// </summary>
  207. internal class SchemaColumn
  208. {
  209. /// <summary>
  210. /// The name of the column.
  211. /// </summary>
  212. public string Name { get; set; }
  213. /// <summary>
  214. /// The type of the column.
  215. /// </summary>
  216. public Type Type { get; set; }
  217. }
  218. }
  219. #endif