1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
|
// Copyright 2008, 2010, Oracle and/or its affiliates. All rights reserved.
//
// MySQL Connector/NET is licensed under the terms of the GPLv2
// <http://www.gnu.org/licenses/old-licenses/gpl-2.0.html>, like most
// MySQL Connectors. There are special exceptions to the terms and
// conditions of the GPLv2 as it is applied to this software, see the
// FLOSS License Exception
// <http://www.mysql.com/about/legal/licensing/foss-exception.html>.
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published
// by the Free Software Foundation; version 2 of the License.
//
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
// or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
// for more details.
//
// You should have received a copy of the GNU General Public License along
// with this program; if not, write to the Free Software Foundation, Inc.,
// 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
namespace MySql.Data.VisualStudio.DbObjects
{
class ForeignKey : Object, ITablePart
{
bool isNew;
ForeignKey oldFk;
Table Table;
private ForeignKey(Table t)
{
Table = t;
SetName(String.Format("FK_{0}_{0}", t.Name), true);
Columns = new List<FKColumnPair>();
}
public ForeignKey(Table t, DataRow keyData) : this (t)
{
isNew = keyData == null;
if (!isNew)
{
ParseFKInfo(keyData);
(this as ITablePart).Saved();
}
}
private void ParseFKInfo(DataRow keyData)
{
Name = keyData["CONSTRAINT_NAME"].ToString();
ReferencedTable = keyData["REFERENCED_TABLE_NAME"].ToString();
if (keyData["MATCH_OPTION"] != DBNull.Value)
Match = (MatchOption)Enum.Parse(typeof(MatchOption), keyData["MATCH_OPTION"].ToString(), true);
if (keyData["UPDATE_RULE"] != DBNull.Value)
UpdateAction = (ReferenceOption)Enum.Parse(typeof(ReferenceOption),
keyData["UPDATE_RULE"].ToString(), true);
if (keyData["DELETE_RULE"] != DBNull.Value)
DeleteAction = (ReferenceOption)Enum.Parse(typeof(ReferenceOption),
keyData["DELETE_RULE"].ToString(), true);
string[] restrictions = new string[4] { null, Table.OwningNode.Database, Table.Name, Name };
DataTable cols = Table.OwningNode.GetSchema("Foreign Key Columns", restrictions);
foreach (DataRow row in cols.Rows)
{
FKColumnPair colPair = new FKColumnPair();
colPair.Column = row["COLUMN_NAME"].ToString();
colPair.ReferencedColumn = row["REFERENCED_COLUMN_NAME"].ToString();
Columns.Add(colPair);
}
}
private string _name;
public string Name
{
get { return _name; }
set { _name = value; }
}
private string _referencedTable;
public string ReferencedTable
{
get { return _referencedTable; }
set { _referencedTable = value; }
}
private MatchOption _match;
public MatchOption Match
{
get { return _match; }
set { _match = value; }
}
private ReferenceOption _updateAction;
public ReferenceOption UpdateAction
{
get { return _updateAction; }
set { _updateAction = value; }
}
private ReferenceOption _deleteAction;
public ReferenceOption DeleteAction
{
get { return _deleteAction; }
set { _deleteAction = value; }
}
private List<FKColumnPair> _columns;
public List<FKColumnPair> Columns
{
get { return _columns; }
set { _columns = value; }
}
public bool NameSet;
public override string ToString()
{
return Name;
}
public void SetName(string name, bool makeUnique)
{
string proposedName = name;
int uniqueIndex = 0;
if (makeUnique)
{
while (true)
{
bool found = false;
foreach (ForeignKey k in Table.ForeignKeys)
if (k.Name == proposedName)
{
found = true;
break;
}
if (!found) break;
proposedName = String.Format("{0}_{1}", name, ++uniqueIndex);
}
}
Name = proposedName;
}
#region ITablePart Members
void ITablePart.Saved()
{
if (oldFk == null)
oldFk = new ForeignKey(Table);
// copy over the top level properties
oldFk.DeleteAction = DeleteAction;
oldFk.Match = Match;
oldFk.Name = Name;
oldFk.ReferencedTable = ReferencedTable;
oldFk.Table = Table;
oldFk.UpdateAction = UpdateAction;
// now we need to copy the columns
oldFk.Columns.Clear();
foreach (FKColumnPair fc in Columns)
{
FKColumnPair old = new FKColumnPair();
old.ReferencedColumn = fc.ReferencedColumn;
old.Column = fc.Column;
oldFk.Columns.Add(old);
}
}
bool ITablePart.HasChanges()
{
if (!ObjectHelper.AreEqual(this, oldFk)) return true;
if (Columns.Count != oldFk.Columns.Count) return true;
foreach (FKColumnPair fc in Columns)
{
int i = 0;
for (; i < oldFk.Columns.Count; i++)
{
FKColumnPair ofc = oldFk.Columns[i];
if (ofc.ReferencedColumn == fc.ReferencedColumn &&
ofc.Column == fc.Column) break;
}
if (i == oldFk.Columns.Count) return true;
}
return false;
}
string ITablePart.GetDropSql()
{
return String.Format("DROP FOREIGN KEY `{0}`", Name);
}
string ITablePart.GetSql(bool newTable)
{
// if we don't have any changes then just return null
if (!(this as ITablePart).HasChanges()) return null;
StringBuilder sql = new StringBuilder();
if (!newTable)
{
if (oldFk != null)
sql.AppendFormat("DROP FOREIGN KEY `{0}`, ", oldFk.Name);
sql.Append("ADD ");
}
sql.AppendFormat("FOREIGN KEY `{0}`", Name);
sql.Append("(");
string delimiter = "";
foreach (FKColumnPair c in Columns)
{
sql.AppendFormat("{0}{1}", delimiter, c.Column);
delimiter = ", ";
}
sql.Append(")");
sql.AppendFormat(" REFERENCES `{0}`(", ReferencedTable);
delimiter = "";
foreach (FKColumnPair c in Columns)
{
sql.AppendFormat("{0}{1}", delimiter, c.ReferencedColumn);
delimiter = ", ";
}
sql.Append(")");
return sql.ToString();
}
bool ITablePart.IsNew()
{
return isNew;
}
#endregion
}
enum MatchOption
{
Full, Partial, Simple, None
}
enum ReferenceOption : int
{
NoAction, Cascade, Restrict, SetNull
}
class FKColumnPair
{
public string ReferencedColumn;
public string Column;
}
}
|