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 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445
|
// 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.ComponentModel;
using System.Collections.Generic;
using MySql.Data.VisualStudio.Editors;
using System.Data;
using System.Text;
namespace MySql.Data.VisualStudio.DbObjects
{
class Index : Object, ICustomTypeDescriptor, ITablePart
{
Table table;
List<IndexColumn> indexColumns = new List<IndexColumn>();
bool isNew;
Index oldIndex;
private Index(Table t)
{
table = t;
isNew = true;
}
public Index(Table t, DataRow indexData) : this(t)
{
isNew = indexData == null;
oldIndex = new Index(t);
if (!isNew)
{
ParseIndexInfo(indexData);
(this as ITablePart).Saved();
}
}
private void ParseIndexInfo(DataRow indexData)
{
Name = indexData["INDEX_NAME"].ToString();
IsPrimary = (bool)indexData["PRIMARY"];
IsUnique = (bool)indexData["UNIQUE"] || IsPrimary;
Comment = indexData["COMMENT"].ToString();
string type = indexData["TYPE"].ToString();
switch (type)
{
case "BTREE": IndexUsing = IndexUsingType.BTREE; break;
case "RTREE": IndexUsing = IndexUsingType.RTREE; break;
case "HASH": IndexUsing = IndexUsingType.HASH; break;
}
FullText = type == "FULLTEXT";
Spatial = type == "SPATIAL";
string[] restrictions =
new string[5] { null, table.OwningNode.Database, table.Name, Name, null };
DataTable dt = table.OwningNode.GetSchema("IndexColumns", restrictions);
foreach (DataRow row in dt.Rows)
{
IndexColumn col = new IndexColumn();
col.OwningIndex = this;
col.ColumnName = row["COLUMN_NAME"].ToString();
string sortOrder = row["SORT_ORDER"].ToString();
// if (sortOrder == "D")
// col.SortOrder = IndexSortOrder.Descending;
// else if (sortOrder == null)
// col.SortOrder = IndexSortOrder.Unsorted;
// else
col.SortOrder = IndexSortOrder.Ascending;
Columns.Add(col);
}
if (IsPrimary)
Type = IndexType.Key;
//KeyBlockSize
//Parser
}
#region Properties
[Browsable(false)]
public Table Table
{
get { return table; }
}
private string _name;
[Category("Identity")]
[DisplayName("(Name)")]
[Description("The name of this index/key")]
public string Name
{
get { return _name; }
set { _name = value; }
}
private string _comment;
[Category("Identity")]
[Description("A description or comment about this index/key")]
public string Comment
{
get { return _comment; }
set { _comment = value; }
}
[Category("(General)")]
[Description("The columns of this index/key and their associated sort order")]
[TypeConverter(typeof(IndexColumnTypeConverter))]
[Editor(typeof(IndexColumnEditor), typeof(System.Drawing.Design.UITypeEditor))]
public List<IndexColumn> Columns
{
get { return indexColumns; }
}
private IndexType _indexType;
[Category("(General)")]
[Description("Specifies if this object is an index or key")]
public IndexType Type
{
get { return _indexType; }
set { _indexType = value; }
}
private bool _isUnique;
[Category("(General)")]
[DisplayName("Is Unique")]
[Description("Specifies if this index/key uniquely identifies every row")]
[TypeConverter(typeof(YesNoTypeConverter))]
public bool IsUnique
{
get { return _isUnique; }
set { _isUnique = value; }
}
private bool _isPrimary;
[Browsable(false)]
public bool IsPrimary
{
get { return _isPrimary; }
set { _isPrimary = value; }
}
private IndexUsingType _indexUsing;
[Category("Storage")]
[DisplayName("Index Algorithm")]
[Description("Specifies the algorithm that should be used for storing the index/key")]
public IndexUsingType IndexUsing
{
get { return _indexUsing; }
set { _indexUsing = value; }
}
private int _keyBlockSize;
[Category("Storage")]
[DisplayName("Key Block Size")]
[Description("Suggested size in bytes to use for index key blocks. A zero value means to use the storage engine default.")]
public int KeyBlockSize
{
get { return _keyBlockSize; }
set { _keyBlockSize = value; }
}
private string _parser;
[Description("Specifies a parser plugin to be used for this index/key. This is only valid for full-text indexes or keys.")]
public string Parser
{
get { return _parser; }
set { _parser = value; }
}
private bool _fullText;
[DisplayName("Is Full-text Index/Key")]
[Description("Specifies if this is a full-text index or key. This is only supported on MyISAM tables.")]
[TypeConverter(typeof(YesNoTypeConverter))]
[RefreshProperties(RefreshProperties.All)]
public bool FullText
{
get { return _fullText; }
set { _fullText = value; }
}
private bool _spatial;
[DisplayName("Is Spatial Index/Key")]
[Description("Specifies if this is a spatial index or key. This is only supported on MyISAM tables.")]
[TypeConverter(typeof(YesNoTypeConverter))]
[RefreshProperties(RefreshProperties.All)]
public bool Spatial
{
get { return _spatial; }
set { _spatial = value; }
}
#endregion
#region ShouldSerialize
bool ShouldSerializeName() { return false; }
bool ShouldSerializeComment() { return false; }
bool ShouldSerializeColumns() { return false; }
bool ShouldSerializeType() { return false; }
bool ShouldSerializeIsUnique() { return false; }
bool ShouldSerializeIndexUsing() { return false; }
bool ShouldSerializeKeyBlockSize() { return false; }
bool ShouldSerializeParser() { return false; }
bool ShouldSerializeFullText() { return false; }
bool ShouldSerializeSpatial() { return false; }
#endregion
#region ICustomTypeDescriptor Members
public TypeConverter GetConverter()
{
return TypeDescriptor.GetConverter(this, true);
}
public EventDescriptorCollection GetEvents(Attribute[] attributes)
{
return TypeDescriptor.GetEvents(this, attributes, true);
}
EventDescriptorCollection System.ComponentModel.ICustomTypeDescriptor.GetEvents()
{
return TypeDescriptor.GetEvents(this, true);
}
public string GetComponentName()
{
return TypeDescriptor.GetComponentName(this, true);
}
public object GetPropertyOwner(PropertyDescriptor pd)
{
return this;
}
public AttributeCollection GetAttributes()
{
return TypeDescriptor.GetAttributes(this, true);
}
public PropertyDescriptorCollection GetProperties(Attribute[] attributes)
{
PropertyDescriptorCollection coll =
TypeDescriptor.GetProperties(this, attributes, true);
List<PropertyDescriptor> props = new List<PropertyDescriptor>();
foreach (PropertyDescriptor pd in coll)
{
if (!pd.IsBrowsable) continue;
if (pd.Name == "IsUnique" || pd.Name == "Name" || pd.Name == "Type")
{
if (IsPrimary)
{
CustomPropertyDescriptor newPd = new CustomPropertyDescriptor(pd);
newPd.SetReadOnly(true);
props.Add(newPd);
}
}
else if (pd.Name == "FullText" && (Spatial ||
String.Compare(table.Engine, "myisam", true) != 0))
{
CustomPropertyDescriptor newPd = new CustomPropertyDescriptor(pd);
newPd.SetReadOnly(true);
props.Add(newPd);
}
else if (pd.Name == "Spatial" && (FullText ||
String.Compare(table.Engine, "myisam", true) != 0))
{
CustomPropertyDescriptor newPd = new CustomPropertyDescriptor(pd);
newPd.SetReadOnly(true);
props.Add(newPd);
}
else
props.Add(pd);
}
return new PropertyDescriptorCollection(props.ToArray());
}
PropertyDescriptorCollection System.ComponentModel.ICustomTypeDescriptor.GetProperties()
{
return TypeDescriptor.GetProperties(this, true);
}
public object GetEditor(Type editorBaseType)
{
return TypeDescriptor.GetEditor(this, editorBaseType, true);
}
public PropertyDescriptor GetDefaultProperty()
{
return TypeDescriptor.GetDefaultProperty(this, true);
}
public EventDescriptor GetDefaultEvent()
{
return TypeDescriptor.GetDefaultEvent(this, true);
}
public string GetClassName()
{
return TypeDescriptor.GetClassName(this, true);
}
#endregion
#region ITablePart Members
void ITablePart.Saved()
{
oldIndex.Comment = Comment;
oldIndex.FullText = FullText;
oldIndex.IndexUsing = IndexUsing;
oldIndex.IsPrimary = IsPrimary;
oldIndex.IsUnique = IsUnique;
oldIndex.KeyBlockSize = KeyBlockSize;
oldIndex.Name = Name;
oldIndex.Parser = Parser;
oldIndex.Spatial = Spatial;
oldIndex.Type = Type;
// now we need to copy the columns
oldIndex.Columns.Clear();
foreach (IndexColumn ic in Columns)
{
IndexColumn old = new IndexColumn();
old.ColumnName = ic.ColumnName;
old.SortOrder = ic.SortOrder;
old.OwningIndex = oldIndex;
oldIndex.Columns.Add(old);
}
}
bool ITablePart.HasChanges()
{
if (!ObjectHelper.AreEqual(this, oldIndex)) return true;
if (Columns.Count != oldIndex.Columns.Count) return true;
foreach (IndexColumn ic in Columns)
{
int i = 0;
for (; i < oldIndex.Columns.Count; i++)
{
IndexColumn oic = oldIndex.Columns[i];
if (oic.ColumnName == ic.ColumnName && oic.SortOrder == ic.SortOrder) break;
}
if (i == oldIndex.Columns.Count) return true;
}
return false;
}
string ITablePart.GetDropSql()
{
if (IsPrimary)
return "DROP PRIMARY KEY";
return String.Format("DROP KEY `{0}`", Name);
}
string ITablePart.GetSql(bool newTable)
{
StringBuilder sql = new StringBuilder();
// if we don't have any changes then just return null
ITablePart part = this as ITablePart;
if (!part.HasChanges()) return null;
if (!newTable)
{
if (!String.IsNullOrEmpty(oldIndex.Name))
sql.AppendFormat("DROP INDEX `{0}`, ", oldIndex.Name);
sql.Append("ADD ");
}
if (IsPrimary)
sql.Append("PRIMARY KEY ");
else if (IsUnique)
sql.Append("UNIQUE ");
else if (FullText)
sql.Append("FULLTEXT ");
else if (Spatial)
sql.Append("SPATIAL ");
if (!IsPrimary)
sql.AppendFormat("{0} ", Type.ToString().ToUpperInvariant());
sql.AppendFormat("`{0}` (", Name);
string delimiter = "";
foreach (IndexColumn c in Columns)
{
sql.AppendFormat("{0}{1}", delimiter, c.ColumnName);
delimiter = ", ";
}
sql.Append(")");
return sql.ToString();
}
bool ITablePart.IsNew()
{
return isNew;
}
#endregion
}
enum IndexType
{
Index, Key, Primary
}
enum IndexUsingType
{
BTREE, HASH, RTREE
}
public enum IndexSortOrder
{
Ascending
}
class IndexColumn
{
public Index OwningIndex;
public string ColumnName;
public IndexSortOrder SortOrder;
}
}
|