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.

148 lines
5.5 KiB

4 years ago
  1. #if MYSQL_6_9
  2. // Copyright © 2004, 2017 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.Types;
  24. using System;
  25. using System.Collections;
  26. using System.Data;
  27. using System.Data.Common;
  28. namespace Externals.MySql.Data.MySqlClient
  29. {
  30. internal sealed partial class MySqlDataReader : DbDataReader, IDataReader, IDataRecord
  31. {
  32. /// <summary>
  33. /// Gets a value indicating the depth of nesting for the current row. This method is not
  34. /// supported currently and always returns 0.
  35. /// </summary>
  36. public override int Depth
  37. {
  38. get { return 0; }
  39. }
  40. public MySqlGeometry GetMySqlGeometry(int i)
  41. {
  42. try
  43. {
  44. IMySqlValue v = GetFieldValue(i, false);
  45. if (v is MySqlGeometry || v is MySqlBinary)
  46. return new MySqlGeometry(MySqlDbType.Geometry, (Byte[])v.Value);
  47. }
  48. catch
  49. {
  50. Throw(new Exception("Can't get MySqlGeometry from value"));
  51. }
  52. return new MySqlGeometry(true);
  53. }
  54. public MySqlGeometry GetMySqlGeometry(string column)
  55. {
  56. return GetMySqlGeometry(GetOrdinal(column));
  57. }
  58. /// <summary>
  59. /// Returns a DataTable that describes the column metadata of the MySqlDataReader.
  60. /// </summary>
  61. /// <returns></returns>
  62. public override DataTable GetSchemaTable()
  63. {
  64. // Only Results from SQL SELECT Queries
  65. // get a DataTable for schema of the result
  66. // otherwise, DataTable is null reference
  67. if (FieldCount == 0) return null;
  68. DataTable dataTableSchema = new DataTable("SchemaTable");
  69. dataTableSchema.Columns.Add("ColumnName", typeof(string));
  70. dataTableSchema.Columns.Add("ColumnOrdinal", typeof(int));
  71. dataTableSchema.Columns.Add("ColumnSize", typeof(int));
  72. dataTableSchema.Columns.Add("NumericPrecision", typeof(int));
  73. dataTableSchema.Columns.Add("NumericScale", typeof(int));
  74. dataTableSchema.Columns.Add("IsUnique", typeof(bool));
  75. dataTableSchema.Columns.Add("IsKey", typeof(bool));
  76. DataColumn dc = dataTableSchema.Columns["IsKey"];
  77. dc.AllowDBNull = true; // IsKey can have a DBNull
  78. dataTableSchema.Columns.Add("BaseCatalogName", typeof(string));
  79. dataTableSchema.Columns.Add("BaseColumnName", typeof(string));
  80. dataTableSchema.Columns.Add("BaseSchemaName", typeof(string));
  81. dataTableSchema.Columns.Add("BaseTableName", typeof(string));
  82. dataTableSchema.Columns.Add("DataType", typeof(Type));
  83. dataTableSchema.Columns.Add("AllowDBNull", typeof(bool));
  84. dataTableSchema.Columns.Add("ProviderType", typeof(int));
  85. dataTableSchema.Columns.Add("IsAliased", typeof(bool));
  86. dataTableSchema.Columns.Add("IsExpression", typeof(bool));
  87. dataTableSchema.Columns.Add("IsIdentity", typeof(bool));
  88. dataTableSchema.Columns.Add("IsAutoIncrement", typeof(bool));
  89. dataTableSchema.Columns.Add("IsRowVersion", typeof(bool));
  90. dataTableSchema.Columns.Add("IsHidden", typeof(bool));
  91. dataTableSchema.Columns.Add("IsLong", typeof(bool));
  92. dataTableSchema.Columns.Add("IsReadOnly", typeof(bool));
  93. int ord = 1;
  94. for (int i = 0; i < FieldCount; i++)
  95. {
  96. MySqlField f = resultSet.Fields[i];
  97. DataRow r = dataTableSchema.NewRow();
  98. r["ColumnName"] = f.ColumnName;
  99. r["ColumnOrdinal"] = ord++;
  100. r["ColumnSize"] = f.IsTextField ? f.ColumnLength / f.MaxLength : f.ColumnLength;
  101. int prec = f.Precision;
  102. int pscale = f.Scale;
  103. if (prec != -1)
  104. r["NumericPrecision"] = (short)prec;
  105. if (pscale != -1)
  106. r["NumericScale"] = (short)pscale;
  107. r["DataType"] = GetFieldType(i);
  108. r["ProviderType"] = (int)f.Type;
  109. r["IsLong"] = f.IsBlob && (f.ColumnLength > 255 || f.ColumnLength == -1);
  110. r["AllowDBNull"] = f.AllowsNull;
  111. r["IsReadOnly"] = false;
  112. r["IsRowVersion"] = false;
  113. r["IsUnique"] = false;
  114. r["IsKey"] = f.IsPrimaryKey;
  115. r["IsAutoIncrement"] = f.IsAutoIncrement;
  116. r["BaseSchemaName"] = f.DatabaseName;
  117. r["BaseCatalogName"] = null;
  118. r["BaseTableName"] = f.RealTableName;
  119. r["BaseColumnName"] = f.OriginalColumnName;
  120. dataTableSchema.Rows.Add(r);
  121. }
  122. return dataTableSchema;
  123. }
  124. /// <summary>
  125. /// Returns an <see cref="IEnumerator"/> that iterates through the <see cref="MySqlDataReader"/>.
  126. /// </summary>
  127. /// <returns></returns>
  128. public override IEnumerator GetEnumerator()
  129. {
  130. return new DbEnumerator(this, (commandBehavior & CommandBehavior.CloseConnection) != 0);
  131. }
  132. }
  133. }
  134. #endif