2 * Firebird ADO.NET Data provider for .NET and Mono
4 * The contents of this file are subject to the Initial
5 * Developer's Public License Version 1.0 (the "License");
6 * you may not use this file except in compliance with the
7 * License. You may obtain a copy of the License at
8 * http://www.firebirdsql.org/index.php?op=doc&id=idpl
10 * Software distributed under the License is distributed on
11 * an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either
12 * express or implied. See the License for the specific
13 * language governing rights and limitations under the License.
15 * Copyright (c) 2002, 2005 Carlos Guzman Alvarez
16 * All Rights Reserved.
21 using System.Globalization;
23 using System.Text.RegularExpressions;
24 using System.Collections;
26 namespace FirebirdSql.Data.Firebird.DbSchema
28 internal abstract class FbDbSchema
32 private string schemaName;
38 public FbDbSchema(string schemaName)
40 this.schemaName = schemaName;
45 #region Abstract Methods
47 protected abstract StringBuilder GetCommandText(object[] restrictions);
53 public virtual DataTable GetSchema(FbConnection connection, object[] restrictions)
55 restrictions = this.ParseRestrictions(restrictions);
57 FbCommand command = this.BuildCommand(connection, restrictions);
58 FbDataAdapter adapter = new FbDataAdapter(command);
59 DataSet dataSet = new DataSet(this.schemaName);
63 adapter.Fill(dataSet, this.schemaName);
67 throw new FbException(ex.Message);
75 TrimStringFields(dataSet.Tables[this.schemaName]);
77 return this.ProcessResult(dataSet.Tables[this.schemaName]);
82 #region Protected Methods
84 protected FbCommand BuildCommand(FbConnection connection, object[] restrictions)
86 DataView collections = FbMetaDataCollections.GetSchema().DefaultView;
87 collections.RowFilter = "CollectionName = '" + this.schemaName + "'";
89 if (collections.Count == 0)
91 throw new NotSupportedException("Unsupported collection name.");
94 if (restrictions != null &&
95 restrictions.Length > (int)collections[0]["NumberOfRestrictions"])
97 throw new InvalidOperationException("The number of specified restrictions is not valid.");
100 DataView restriction = FbRestrictions.GetSchema().DefaultView;
101 restriction.RowFilter = "CollectionName = '" + this.schemaName + "'";
103 if (restriction.Count != (int)collections[0]["NumberOfRestrictions"])
105 throw new InvalidOperationException("Incorrect restriction definitions.");
108 StringBuilder builder = this.GetCommandText(restrictions);
109 FbCommand schema = connection.CreateCommand();
111 schema.CommandText = builder.ToString();
113 if (connection.InnerConnection.HasActiveTransaction)
115 schema.Transaction = connection.InnerConnection.ActiveTransaction;
118 if (restrictions != null && restrictions.Length > 0)
123 for (int i = 0; i < restrictions.Length; i++)
125 string rname = restriction[i]["RestrictionDefault"].ToString().ToLower(CultureInfo.CurrentCulture);
126 if (restrictions[i] != null &&
127 !rname.EndsWith("_catalog") &&
128 !rname.EndsWith("_schema") &&
129 rname != "table_type")
131 string pname = String.Format(CultureInfo.CurrentCulture, "@p{0}", index++);
133 FbParameter p = schema.Parameters.Add(pname, restrictions[i].ToString());
134 p.FbDbType = FbDbType.VarChar;
143 protected virtual DataTable ProcessResult(DataTable schema)
148 protected virtual object[] ParseRestrictions(object[] restrictions)
155 #region Private Static Methods
157 private static void TrimStringFields(DataTable schema)
159 schema.BeginLoadData();
161 foreach (DataRow row in schema.Rows)
163 for (int i = 0; i < schema.Columns.Count; i++)
165 if (schema.Columns[i].DataType == typeof(System.String))
167 row[schema.Columns[i]] = row[schema.Columns[i]].ToString().Trim();
172 schema.EndLoadData();
173 schema.AcceptChanges();