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;
24 namespace FirebirdSql.Data.Firebird.DbSchema
26 internal class FbForeignKeys : FbDbSchema
30 public FbForeignKeys() : base("ForeignKeys")
36 #region Protected Methods
38 protected override StringBuilder GetCommandText(object[] restrictions)
40 StringBuilder sql = new StringBuilder();
41 StringBuilder where = new StringBuilder();
45 "null AS PK_TABLE_CATALOG, " +
46 "null AS PK_TABLE_SCHEMA, " +
47 "pk.rdb$relation_name AS PK_TABLE_NAME, " +
48 "pkseg.rdb$field_name AS PK_COLUMN_NAME, " +
49 "pk.rdb$constraint_name AS PK_NAME, " +
50 "null AS FK_TABLE_CATALOG, " +
51 "null AS FK_TABLE_SCHEMA, " +
52 "fk.rdb$relation_name AS FK_TABLE_NAME, " +
53 "fkseg.rdb$field_name AS FK_COLUMN_NAME, " +
54 "fk.rdb$constraint_name AS FK_NAME, " +
55 "pkseg.rdb$field_position AS ORDINAL_POSITION, " +
56 "ref.rdb$match_option AS MATCH_OPTION, " +
57 "ref.rdb$update_rule AS UPDATE_RULE, " +
58 "ref.rdb$delete_rule AS DELETE_RULE, " +
59 "fk.rdb$deferrable AS IS_DEFERRABLE, " +
60 "fk.rdb$initially_deferred AS INITIALLY_DEFERRED " +
62 "rdb$relation_constraints fk, " +
63 "rdb$index_segments fkseg, " +
64 "rdb$relation_constraints pk, " +
65 "rdb$index_segments pkseg, " +
66 "rdb$ref_constraints ref ");
69 "fk.rdb$constraint_name = ref.rdb$constraint_name and " +
70 "fk.rdb$index_name = fkseg.rdb$index_name and " +
71 "pk.rdb$constraint_name = ref.rdb$const_name_uq and " +
72 "pk.rdb$index_name = pkseg.rdb$index_name and " +
73 "pkseg.rdb$field_position = fkseg.rdb$field_position ");
75 if (restrictions != null)
79 /* PK_TABLE_CATALOG */
80 if (restrictions.Length >= 1 && restrictions[0] != null)
85 if (restrictions.Length >= 2 && restrictions[1] != null)
90 if (restrictions.Length >= 3 && restrictions[2] != null)
92 where.AppendFormat(CultureInfo.CurrentCulture, " and pk.rdb$relation_name = @p{0}", index++);
95 /* FK_TABLE_CATALOG */
96 if (restrictions.Length >= 4 && restrictions[3] != null)
100 /* FK_TABLE_SCHEMA */
101 if (restrictions.Length >= 5 && restrictions[4] != null)
106 if (restrictions.Length >= 6 && restrictions[5] != null)
108 where.AppendFormat(CultureInfo.CurrentCulture, " and fk.rdb$relation_name = @p{0}", index++);
112 if (where.Length > 0)
114 sql.AppendFormat(CultureInfo.CurrentCulture, " WHERE {0} ", where.ToString());
117 sql.Append(" ORDER BY fk.rdb$constraint_name, pk.rdb$relation_name, pkseg.rdb$field_position");