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 274 275 276 277 278 279 280 281 282 283 284 285 286
|
//
// Authors:
// Ben Motmans <ben.motmans@gmail.com>
//
// Copyright (c) 2007 Ben Motmans
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
using Gtk;
using System;
using MonoDevelop.Core;
using MonoDevelop.Core.Gui;
using MonoDevelop.Components;
using MonoDevelop.Database.Sql;
using MonoDevelop.Database.Components;
namespace MonoDevelop.Database.Designer
{
public partial class CheckConstraintEditorWidget : Gtk.Bin
{
public event EventHandler ContentChanged;
private ISchemaProvider schemaProvider;
private TableSchema table;
private ColumnSchemaCollection columns;
private ConstraintSchemaCollection constraints;
private ListStore store;
private SortedColumnListStore storeColumns;
private const int colNameIndex = 0;
private const int colColumnNameIndex = 1;
private const int colIsColumnConstraintIndex = 2;
private const int colSourceIndex = 3;
private const int colObjIndex = 4;
private bool columnConstraintsSupported;
private bool tableConstraintsSupported;
private SchemaActions action;
public CheckConstraintEditorWidget (ISchemaProvider schemaProvider, SchemaActions action, TableSchema table, ColumnSchemaCollection columns, ConstraintSchemaCollection constraints)
{
if (columns == null)
throw new ArgumentNullException ("columns");
if (table == null)
throw new ArgumentNullException ("table");
if (constraints == null)
throw new ArgumentNullException ("constraints");
if (schemaProvider == null)
throw new ArgumentNullException ("schemaProvider");
this.schemaProvider = schemaProvider;
this.table = table;
this.columns = columns;
this.constraints = constraints;
this.action = action;
this.Build();
store = new ListStore (typeof (string), typeof (string), typeof (bool), typeof (string), typeof (object));
storeColumns = new SortedColumnListStore (columns);
listCheck.Model = store;
TreeViewColumn colName = new TreeViewColumn ();
TreeViewColumn colColumn = new TreeViewColumn ();
TreeViewColumn colIsColumnConstraint = new TreeViewColumn ();
colName.Title = AddinCatalog.GetString ("Name");
colColumn.Title = AddinCatalog.GetString ("Column");
colIsColumnConstraint.Title = AddinCatalog.GetString ("Column Constraint");
colColumn.MinWidth = 120; //request a bigger width
CellRendererText nameRenderer = new CellRendererText ();
CellRendererCombo columnRenderer = new CellRendererCombo ();
CellRendererToggle isColumnConstraintRenderer = new CellRendererToggle ();
nameRenderer.Editable = true;
nameRenderer.Edited += new EditedHandler (NameEdited);
columnRenderer.Model = storeColumns.Store;
columnRenderer.TextColumn = SortedColumnListStore.ColNameIndex;
columnRenderer.Editable = true;
columnRenderer.Edited += new EditedHandler (ColumnEdited);
isColumnConstraintRenderer.Activatable = true;
isColumnConstraintRenderer.Toggled += new ToggledHandler (IsColumnConstraintToggled);
colName.PackStart (nameRenderer, true);
colColumn.PackStart (columnRenderer, true);
colIsColumnConstraint.PackStart (isColumnConstraintRenderer, true);
colName.AddAttribute (nameRenderer, "text", colNameIndex);
colColumn.AddAttribute (columnRenderer, "text", colColumnNameIndex);
colIsColumnConstraint.AddAttribute (isColumnConstraintRenderer, "active", colIsColumnConstraintIndex);
IDbFactory fac = schemaProvider.ConnectionPool.DbFactory;
columnConstraintsSupported = fac.IsCapabilitySupported ("TableColumn", action, TableCapabilities.CheckConstraint);
tableConstraintsSupported = fac.IsCapabilitySupported ("Table", action, TableCapabilities.CheckConstraint);
listCheck.AppendColumn (colName);
if (columnConstraintsSupported)
listCheck.AppendColumn (colColumn);
if (columnConstraintsSupported && tableConstraintsSupported)
listCheck.AppendColumn (colIsColumnConstraint);
listCheck.Selection.Changed += new EventHandler (OnSelectionChanged);
sqlEditor.TextChanged += new EventHandler (SourceChanged);
foreach (CheckConstraintSchema check in constraints.GetConstraints (ConstraintType.Check))
AddConstraint (check);
//TODO: also col constraints
ShowAll ();
}
protected virtual void AddClicked (object sender, EventArgs e)
{
CheckConstraintSchema check = schemaProvider.GetNewCheckConstraintSchema ("check_new");
int index = 1;
while (constraints.Contains (check.Name))
check.Name = "check_new" + (index++);
constraints.Add (check);
AddConstraint (check);
EmitContentChanged ();
}
protected virtual void RemoveClicked (object sender, EventArgs e)
{
TreeIter iter;
if (listCheck.Selection.GetSelected (out iter)) {
CheckConstraintSchema check = store.GetValue (iter, colObjIndex) as CheckConstraintSchema;
if (Services.MessageService.AskQuestion (
AddinCatalog.GetString ("Are you sure you want to remove constraint '{0}'?", check.Name),
AddinCatalog.GetString ("Remove Constraint")
)) {
store.Remove (ref iter);
constraints.Remove (check);
EmitContentChanged ();
}
}
}
protected virtual void OnSelectionChanged (object sender, EventArgs e)
{
TreeIter iter;
if (listCheck.Selection.GetSelected (out iter)) {
buttonRemove.Sensitive = true;
sqlEditor.Editable = true;
CheckConstraintSchema check = store.GetValue (iter, colObjIndex) as CheckConstraintSchema;
sqlEditor.Text = check.Source;
} else {
buttonRemove.Sensitive = false;
sqlEditor.Editable = false;
sqlEditor.Text = String.Empty;
}
}
private void IsColumnConstraintToggled (object sender, ToggledArgs args)
{
TreeIter iter;
if (store.GetIterFromString (out iter, args.Path)) {
bool val = (bool) store.GetValue (iter, colIsColumnConstraintIndex);
store.SetValue (iter, colIsColumnConstraintIndex, !val);
EmitContentChanged ();
}
}
private void NameEdited (object sender, EditedArgs args)
{
TreeIter iter;
if (store.GetIterFromString (out iter, args.Path)) {
if (!string.IsNullOrEmpty (args.NewText)) {
store.SetValue (iter, colNameIndex, args.NewText);
EmitContentChanged ();
} else {
string oldText = store.GetValue (iter, colNameIndex) as string;
(sender as CellRendererText).Text = oldText;
}
}
}
private void ColumnEdited (object sender, EditedArgs args)
{
TreeIter iter;
if (store.GetIterFromString (out iter, args.Path)) {
if (columns.Contains (args.NewText)) { //only allow existing columns
store.SetValue (iter, colColumnNameIndex, args.NewText);
EmitContentChanged ();
} else {
string oldText = store.GetValue (iter, colColumnNameIndex) as string;
(sender as CellRendererText).Text = oldText;
}
}
}
private void SourceChanged (object sender, EventArgs args)
{
TreeIter iter;
if (listCheck.Selection.GetSelected (out iter)) {
CheckConstraintSchema check = store.GetValue (iter, colObjIndex) as CheckConstraintSchema;
check.Source = sqlEditor.Text;
store.SetValue (iter, colSourceIndex, sqlEditor.Text);
EmitContentChanged ();
}
}
private void AddConstraint (CheckConstraintSchema check)
{
store.AppendValues (check.Name, String.Empty, false, String.Empty, check);
}
protected virtual void EmitContentChanged ()
{
if (ContentChanged != null)
ContentChanged (this, EventArgs.Empty);
}
public virtual bool ValidateSchemaObjects (out string msg)
{
TreeIter iter;
if (store.GetIterFirst (out iter)) {
do {
string name = store.GetValue (iter, colNameIndex) as string;
string column = store.GetValue (iter, colColumnNameIndex) as string;
string source = store.GetValue (iter, colSourceIndex) as string;
bool iscolc = (bool)store.GetValue (iter, colIsColumnConstraintIndex);
if (String.IsNullOrEmpty (source)) {
msg = AddinCatalog.GetString ("Checked constraint '{0}' does not contain a check statement.", name);
return false;
}
if (iscolc && String.IsNullOrEmpty (column)) {
msg = AddinCatalog.GetString ("Checked constraint '{0}' is marked as a column constraint but is not applied to a column.", name);
return false;
}
} while (store.IterNext (ref iter));
}
msg = null;
return true;
}
public virtual void FillSchemaObjects ()
{
TreeIter iter;
if (store.GetIterFirst (out iter)) {
do {
CheckConstraintSchema check = store.GetValue (iter, colObjIndex) as CheckConstraintSchema;
check.Name = store.GetValue (iter, colNameIndex) as string;
check.Source = store.GetValue (iter, colSourceIndex) as string;
check.IsColumnConstraint = (bool)store.GetValue (iter, colIsColumnConstraintIndex);
if (check.IsColumnConstraint) {
string column = store.GetValue (iter, colColumnNameIndex) as string;
check.Columns.Add (columns.Search (column));
}
table.Constraints.Add (check);
} while (store.IterNext (ref iter));
}
}
}
}
|