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
|
// 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;
using System.ComponentModel;
using MySql.Data.VisualStudio.Editors;
using MySql.Data.VisualStudio.DbObjects;
using System.Windows.Forms.Design;
namespace MySql.Data.VisualStudio.DbObjects
{
internal class Column : Object, ITablePart
{
private string characterSet;
internal Column OldColumn;
private bool isNew;
private Column()
{
AllowNull = true;
}
public Column(DataRow row) : this()
{
isNew = row == null;
if (row != null)
ParseColumnInfo(row);
OldColumn = new Column();
ObjectHelper.Copy(this, OldColumn);
}
#region Properties
[Browsable(false)]
internal Table OwningTable;
private string _columnName;
[Category("General")]
[Description("The name of this column")]
public string ColumnName
{
get { return _columnName; }
set { _columnName = value; }
}
private string _dataType;
[Category("General")]
[DisplayName("Data Type")]
[TypeConverter(typeof(DataTypeConverter))]
[RefreshProperties(RefreshProperties.All)]
public string DataType
{
get { return _dataType; }
set { _dataType = value; }
}
private bool _allowNull;
[TypeConverter(typeof(YesNoTypeConverter))]
[Category("Options")]
[DisplayName("Allow Nulls")]
public bool AllowNull
{
get { return _allowNull; }
set { _allowNull = value; }
}
private bool _isUnsigned;
[TypeConverter(typeof(YesNoTypeConverter))]
[Category("Options")]
[DisplayName("Is Unsigned")]
public bool IsUnsigned
{
get { return _isUnsigned; }
set { _isUnsigned = value; }
}
private bool _isZeroFill;
[TypeConverter(typeof(YesNoTypeConverter))]
[Category("Options")]
[DisplayName("Is Zerofill")]
public bool IsZerofill
{
get { return _isZeroFill; }
set { _isZeroFill = value; }
}
private string _defaultValue;
[Category("General")]
[DisplayName("Default Value")]
public string DefaultValue
{
get { return _defaultValue; }
set { _defaultValue = value; }
}
private bool _autoIncrement;
[TypeConverter(typeof(YesNoTypeConverter))]
[Category("Options")]
[DisplayName("Autoincrement")]
public bool AutoIncrement
{
get { return _autoIncrement; }
set { _autoIncrement = value; }
}
//[TypeConverter(typeof(YesNoTypeConverter))]
//[Category("Options")]
//[DisplayName("Primary Key")]
//[RefreshProperties(RefreshProperties.All)]
[Browsable(false)]
public bool PrimaryKey;
private int _precision;
public int Precision
{
get { return _precision; }
set { _precision = value; }
}
private int _scale;
public int Scale
{
get { return _scale; }
set { _scale = value; }
}
[Category("Encoding")]
[DisplayName("Character Set")]
[TypeConverter(typeof(CharacterSetTypeConverter))]
public string CharacterSet
{
get { return characterSet; }
set
{
if (value != characterSet)
Collation = String.Empty;
characterSet = value;
}
}
private string _collation;
[Category("Encoding")]
[TypeConverter(typeof(CollationTypeConverter))]
public string Collation
{
get { return _collation; }
set { _collation = value; }
}
private string _comment;
[Category("Miscellaneous")]
public string Comment
{
get { return _comment; }
set { _comment = value; }
}
#endregion
private void ParseColumnInfo(DataRow row)
{
ColumnName = row["COLUMN_NAME"].ToString();
AllowNull = row["IS_NULLABLE"] != DBNull.Value && row["IS_NULLABLE"].ToString() == "YES";
Comment = row["COLUMN_COMMENT"].ToString();
Collation = row["COLLATION_NAME"].ToString();
CharacterSet = row["CHARACTER_SET_NAME"].ToString();
DefaultValue = row["COLUMN_DEFAULT"].ToString();
string columnType = row["COLUMN_TYPE"].ToString().ToLowerInvariant();
int index = columnType.IndexOf(' ');
if (index == -1)
index = columnType.Length;
DataType = columnType.Substring(0, index);
CleanDataType();
columnType = columnType.Substring(index);
IsUnsigned = columnType.IndexOf("unsigned") != -1;
IsZerofill = columnType.IndexOf("zerofill") != -1;
PrimaryKey = row["COLUMN_KEY"].ToString() == "PRI";
Precision = DataRowHelpers.GetValueAsInt32(row, "NUMERIC_PRECISION");
Scale = DataRowHelpers.GetValueAsInt32(row, "NUMERIC_SCALE");
string extra = row["EXTRA"].ToString().ToLowerInvariant();
if (extra != null)
AutoIncrement = extra.IndexOf("auto_increment") != -1;
}
private void CleanDataType()
{
if (DataType.Contains("char") || DataType.Contains("binary")) return;
int index = DataType.IndexOf("(");
if (index == -1) return;
DataType = DataType.Substring(0, index);
}
#region Methods needed so PropertyGrid won't bold our values
private bool ShouldSerializeColumnName() { return false; }
private bool ShouldSerializeDataType() { return false; }
private bool ShouldSerializeAllowNull() { return false; }
private bool ShouldSerializeIsUnsigned() { return false; }
private bool ShouldSerializeIsZerofill() { return false; }
private bool ShouldSerializeDefaultValue() { return false; }
private bool ShouldSerializeAutoIncrement() { return false; }
private bool ShouldSerializePrimaryKey() { return false; }
private bool ShouldSerializePrecision() { return false; }
private bool ShouldSerializeScale() { return false; }
private bool ShouldSerializeCharacterSet() { return false; }
private bool ShouldSerializeCollation() { return false; }
private bool ShouldSerializeComment() { return false; }
#endregion
#region ITablePart Members
void ITablePart.Saved()
{
ObjectHelper.Copy(this, OldColumn);
}
bool ITablePart.HasChanges()
{
return !ObjectHelper.AreEqual(this, OldColumn);
}
bool ITablePart.IsNew()
{
return isNew;
}
string ITablePart.GetDropSql()
{
return String.Format("DROP `{0}`", ColumnName);
}
string ITablePart.GetSql(bool newTable)
{
if (OldColumn != null &&
OldColumn.ColumnName != null &&
ObjectHelper.AreEqual(this, OldColumn))
return null;
if (String.IsNullOrEmpty(ColumnName)) return null;
StringBuilder props = new StringBuilder();
props.AppendFormat(" {0}", DataType);
if (!String.IsNullOrEmpty(CharacterSet))
props.AppendFormat(" CHARACTER SET '{0}'", CharacterSet);
if (!String.IsNullOrEmpty(Collation))
props.AppendFormat(" COLLATE '{0}'", Collation);
if (!String.IsNullOrEmpty(DefaultValue))
props.AppendFormat(" DEFAULT '{0}'", DefaultValue);
if (!String.IsNullOrEmpty(Comment))
props.AppendFormat(" COMMENT '{0}'", Comment);
if (!AllowNull) props.Append(" NOT NULL");
if (IsUnsigned) props.Append(" UNSIGNED");
if (IsZerofill) props.Append(" ZEROFILL");
if (AutoIncrement) props.Append(" AUTO_INCREMENT");
if (newTable)
return String.Format("`{0}`{1}", ColumnName, props.ToString());
if (isNew)
return String.Format("ADD `{0}`{1}", ColumnName, props.ToString());
return String.Format("CHANGE `{0}` `{1}` {2}",
OldColumn.ColumnName, ColumnName, props.ToString());
}
#endregion
}
}
|