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 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
|
// 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.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using MySql.Data.VisualStudio.DbObjects;
using System.Collections;
using MySql.Data.VisualStudio.Properties;
namespace MySql.Data.VisualStudio.Editors
{
partial class ForeignKeyDialog : Form
{
TableNode tableNode;
List<string> columnNames = new List<string>();
List<string> fkColumnNames = new List<string>();
const string None = "<None>";
public ForeignKeyDialog(TableNode node)
{
tableNode = node;
Application.EnableVisualStyles();
InitializeComponent();
// create a list of all tables in this database
DataTable dt = tableNode.GetDataTable(
String.Format(@"SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = '{0}' AND TABLE_NAME <> '{1}'",
tableNode.Database, tableNode.Table.Name));
List<string> tables = new List<string>();
foreach (DataRow row in dt.Rows)
tables.Add(row[0].ToString());
refTable.DataSource = tables;
colGridColumn.HeaderText = tableNode.Table.Name;
colGridColumn.Items.Add(None);
foreach (Column c in tableNode.Table.Columns)
{
if (c.ColumnName == null) continue;
columnNames.Add(c.ColumnName);
colGridColumn.Items.Add(c.ColumnName);
}
foreignKeyBindingSource.DataSource = tableNode.Table.ForeignKeys;
fkList.DataSource = foreignKeyBindingSource;
}
private void closeButton_Click(object sender, EventArgs e)
{
Close();
}
private void addButton_Click(object sender, EventArgs e)
{
ForeignKey key = new ForeignKey(tableNode.Table, null);
if (refTable.SelectedValue != null)
{
key.SetName(String.Format("FK_{0}_{1}", tableNode.Table.Name,
refTable.SelectedValue), true);
key.ReferencedTable = refTable.SelectedValue.ToString();
}
foreignKeyBindingSource.Add(key);
}
private void deleteButton_Click(object sender, EventArgs e)
{
foreignKeyBindingSource.RemoveCurrent();
}
private void refTable_SelectedIndexChanged(object sender, EventArgs e)
{
string refTableName = refTable.Items[refTable.SelectedIndex].ToString();
fkGridColumn.HeaderText = refTableName;
//reset the items list for the fk column
string sql = @"SELECT column_name FROM INFORMATION_SCHEMA.COLUMNS WHERE
TABLE_SCHEMA='{0}' AND TABLE_NAME='{1}'";
DataTable dt = tableNode.GetDataTable(String.Format(sql, tableNode.Database, refTableName));
fkColumnNames.Clear();
foreach (DataRow row in dt.Rows)
fkColumnNames.Add(row[0].ToString());
fkGridColumn.Items.Clear();
fkGridColumn.Items.Add(None);
foreach (string col in fkColumnNames)
fkGridColumn.Items.Add(col);
if (foreignKeyBindingSource.Current == null) return;
// update the key name if it is not already finalized
ForeignKey key = foreignKeyBindingSource.Current as ForeignKey;
if (!key.NameSet)
{
string name = String.Format("FK_{0}_{1}", tableNode.Table.Name, refTableName);
key.SetName(name, true);
}
}
private void fkName_KeyPress(object sender, KeyPressEventArgs e)
{
ForeignKey key = foreignKeyBindingSource.Current as ForeignKey;
key.NameSet = true;
}
private void columnGrid_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
Type t = e.Control.GetType();
if (t != typeof(DataGridViewComboBoxEditingControl)) return;
DataGridViewComboBoxEditingControl ec = e.Control as DataGridViewComboBoxEditingControl;
ec.DrawMode = DrawMode.OwnerDrawFixed;
ec.DrawItem += new DrawItemEventHandler(dropdown_DrawItem);
// now update the items that should be seen in this control
ec.Items.Clear();
ec.Items.Add(None);
int index = columnGrid.CurrentCell.ColumnIndex;
List<string> cols = index == 0 ? columnNames : fkColumnNames;
ForeignKey key = foreignKeyBindingSource.Current as ForeignKey;
foreach (string s in cols)
{
bool alreadyUsed = false;
if (s != (string)columnGrid.CurrentCell.Value)
foreach (FKColumnPair pair in key.Columns)
if ((index == 0 && pair.ReferencedColumn == s) ||
(index == 1 && pair.Column == s))
{
alreadyUsed = true;
break;
}
if (!alreadyUsed)
ec.Items.Add(s);
}
int selIndex = ec.FindStringExact(columnGrid.CurrentCell.Value as string);
if (selIndex > 0)
ec.SelectedIndex = selIndex;
}
void dropdown_DrawItem(object sender, DrawItemEventArgs e)
{
MyComboBox.DrawComboBox(sender as ComboBox, e);
}
private void foreignKeyBindingSource_CurrentChanged(object sender, EventArgs e)
{
if (foreignKeyBindingSource.Current == null)
{
columnGrid.Rows.Clear();
return;
}
ForeignKey key = foreignKeyBindingSource.Current as ForeignKey;
fkColumnsBindingSource.DataSource = key.Columns;
}
private void columnGrid_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
{
int index = e.ColumnIndex;
DataGridViewComboBoxCell cell =
(DataGridViewComboBoxCell)columnGrid.Rows[e.RowIndex].Cells[e.ColumnIndex];
FKColumnPair pair = fkColumnsBindingSource.Current as FKColumnPair;
string value = e.FormattedValue as string;
if (value == None)
{
cell.Value = null;
if (index == 0)
pair.ReferencedColumn = null;
else
pair.Column = null;
}
else
cell.Value = e.FormattedValue as string;
}
private void columnGrid_RowValidating(object sender, DataGridViewCellCancelEventArgs e)
{
int index = e.RowIndex;
DataGridViewCell parentCell = columnGrid.Rows[e.RowIndex].Cells[0];
DataGridViewCell childCell = columnGrid.Rows[e.RowIndex].Cells[1];
string parent = parentCell.Value as string;
string child = childCell.Value as string;
bool bad = false;
parentCell.ErrorText = childCell.ErrorText = null;
if ((String.IsNullOrEmpty(parent) || parent == None) &&
(!String.IsNullOrEmpty(child) && child != None))
{
parentCell.ErrorText = Resources.FKNeedColumn;
bad = true;
}
else if ((String.IsNullOrEmpty(child) || child == None) &&
(!String.IsNullOrEmpty(parent) && parent != None))
{
childCell.ErrorText = Resources.FKNeedColumn;
bad = true;
}
if (bad)
{
MessageBox.Show(Resources.FKColumnsNotMatched, null, MessageBoxButtons.OK, MessageBoxIcon.Information);
e.Cancel = true;
return;
}
FKColumnPair pair = fkColumnsBindingSource.Current as FKColumnPair;
pair.Column = parent;
pair.ReferencedColumn = child;
fkColumnsBindingSource.EndEdit();
}
private void columnGrid_CellValueNeeded(object sender, DataGridViewCellValueEventArgs e)
{
FKColumnPair pair = fkColumnsBindingSource[ e.RowIndex ] as FKColumnPair;
switch( e.ColumnIndex ) {
case 0:
e.Value = pair.Column;
break;
case 1:
e.Value = pair.ReferencedColumn;
break;
}
}
private void columnGrid_CellValuePushed(object sender, DataGridViewCellValueEventArgs e)
{
FKColumnPair fk;
if ((e.RowIndex == columnGrid.Rows.Count - 1) && (columnGrid.Rows.Count > fkColumnsBindingSource.Count))
{
fk = new FKColumnPair() { Column = "", ReferencedColumn = "" };
fkColumnsBindingSource.Add( fk );
}
else
{
fk = (fkColumnsBindingSource[e.RowIndex] as FKColumnPair);
}
switch (e.ColumnIndex)
{
case 0:
fk.Column = (string)e.Value;
break;
case 1:
fk.ReferencedColumn = (string)e.Value;
break;
}
}
}
}
|