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.

215 lines
5.9 KiB

4 years ago
  1. #if MYSQL_6_9
  2. // Copyright © 2013, 2014 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 Externals.MySql.Data.MySqlClient.Properties;
  24. using System;
  25. using System.Collections;
  26. using System.Collections.Generic;
  27. using System.Data;
  28. using System.Text;
  29. namespace Externals.MySql.Data.MySqlClient
  30. {
  31. internal class MySqlSchemaCollection
  32. {
  33. private List<SchemaColumn> columns = new List<SchemaColumn>();
  34. private List<MySqlSchemaRow> rows = new List<MySqlSchemaRow>();
  35. private DataTable _table = null;
  36. public MySqlSchemaCollection()
  37. {
  38. Mapping = new Dictionary<string,int>( StringComparer.OrdinalIgnoreCase );
  39. LogicalMappings = new Dictionary<int, int>();
  40. }
  41. public MySqlSchemaCollection(string name) : this()
  42. {
  43. Name = name;
  44. }
  45. public MySqlSchemaCollection(DataTable dt) : this()
  46. {
  47. // cache the original datatable to avoid the overhead of creating again whenever possible.
  48. _table = dt;
  49. int i = 0;
  50. foreach (DataColumn dc in dt.Columns)
  51. {
  52. columns.Add(new SchemaColumn() { Name = dc.ColumnName, Type = dc.DataType });
  53. Mapping.Add(dc.ColumnName, i++);
  54. LogicalMappings[columns.Count - 1] = columns.Count - 1;
  55. }
  56. foreach (DataRow dr in dt.Rows)
  57. {
  58. MySqlSchemaRow row = new MySqlSchemaRow(this);
  59. for (i = 0; i < columns.Count; i++)
  60. {
  61. row[i] = dr[i];
  62. }
  63. rows.Add(row);
  64. }
  65. }
  66. internal Dictionary<string, int> Mapping;
  67. internal Dictionary<int, int> LogicalMappings;
  68. public string Name { get; set; }
  69. public IList<SchemaColumn> Columns { get { return columns; } }
  70. public IList<MySqlSchemaRow> Rows { get { return rows; } }
  71. internal SchemaColumn AddColumn(string name, Type t)
  72. {
  73. SchemaColumn c = new SchemaColumn();
  74. c.Name = name;
  75. c.Type = t;
  76. columns.Add(c);
  77. Mapping.Add(name, columns.Count-1);
  78. LogicalMappings[columns.Count - 1] = columns.Count - 1;
  79. return c;
  80. }
  81. internal int ColumnIndex(string name)
  82. {
  83. int index = -1;
  84. for (int i = 0; i < columns.Count; i++)
  85. {
  86. SchemaColumn c = columns[i];
  87. if (String.Compare(c.Name, name, StringComparison.OrdinalIgnoreCase) != 0) continue;
  88. index = i;
  89. break;
  90. }
  91. return index;
  92. }
  93. internal void RemoveColumn(string name)
  94. {
  95. int index = ColumnIndex(name);
  96. if (index == -1)
  97. throw new InvalidOperationException();
  98. columns.RemoveAt(index);
  99. for (int i = index; i < Columns.Count; i++)
  100. LogicalMappings[i] = LogicalMappings[i] + 1;
  101. }
  102. internal bool ContainsColumn(string name)
  103. {
  104. return ColumnIndex(name) >= 0;
  105. }
  106. internal MySqlSchemaRow AddRow()
  107. {
  108. MySqlSchemaRow r = new MySqlSchemaRow(this);
  109. rows.Add(r);
  110. return r;
  111. }
  112. internal MySqlSchemaRow NewRow()
  113. {
  114. MySqlSchemaRow r = new MySqlSchemaRow(this);
  115. return r;
  116. }
  117. internal DataTable AsDataTable()
  118. {
  119. if (_table != null) return _table;
  120. DataTable dt = new DataTable(Name);
  121. foreach (SchemaColumn col in Columns)
  122. dt.Columns.Add(col.Name, col.Type);
  123. foreach (MySqlSchemaRow row in Rows)
  124. {
  125. DataRow newRow = dt.NewRow();
  126. for (int i = 0; i < dt.Columns.Count; i++)
  127. newRow[i] = row[i] == null ? DBNull.Value : row[i];
  128. dt.Rows.Add(newRow);
  129. }
  130. return dt;
  131. }
  132. }
  133. internal class MySqlSchemaRow
  134. {
  135. private Dictionary<int,object> data;
  136. public MySqlSchemaRow(MySqlSchemaCollection c)
  137. {
  138. Collection = c;
  139. InitMetadata();
  140. }
  141. internal void InitMetadata()
  142. {
  143. data = new Dictionary<int, object>();
  144. }
  145. internal MySqlSchemaCollection Collection { get; private set; }
  146. internal object this[string s]
  147. {
  148. get { return GetValueForName(s); }
  149. set { SetValueForName(s, value); }
  150. }
  151. internal object this[int i]
  152. {
  153. get {
  154. int idx = Collection.LogicalMappings[i];
  155. if (!data.ContainsKey(idx))
  156. data[idx] = null;
  157. return data[ idx ];
  158. }
  159. set { data[ Collection.LogicalMappings[ i ] ] = value; }
  160. }
  161. private void SetValueForName(string colName, object value)
  162. {
  163. int index = Collection.Mapping[colName];
  164. this[index] = value;
  165. }
  166. private object GetValueForName(string colName)
  167. {
  168. int index = Collection.Mapping[colName];
  169. if (!data.ContainsKey(index))
  170. data[index] = null;
  171. return this[index];
  172. }
  173. internal void CopyRow(MySqlSchemaRow row)
  174. {
  175. if (Collection.Columns.Count != row.Collection.Columns.Count)
  176. throw new InvalidOperationException("column count doesn't match");
  177. for (int i = 0; i < Collection.Columns.Count; i++)
  178. row[i] = this[i];
  179. }
  180. }
  181. internal class SchemaColumn
  182. {
  183. public string Name { get; set; }
  184. public Type Type { get; set; }
  185. }
  186. }
  187. #endif