[a235fd]: SQLite.Designer / Design / Column.cs Maximize Restore History

Download this file

Column.cs    299 lines (258 with data), 8.7 kB

  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
/********************************************************
* ADO.NET 2.0 Data Provider for SQLite Version 3.X
* Written by Robert Simpson (robert@blackcastlesoft.com)
*
* Released to the public domain, use at your own risk!
********************************************************/
namespace SQLite.Designer.Design
{
using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Windows.Forms;
using System.Drawing.Design;
using System.Data;
using System.Data.Common;
using System.Globalization;
internal class Column : IHaveConnection
{
private bool _allowNulls;
private string _dataType = "";
private string _defaultValue = "";
private string _columnName = "";
private string _origName = "";
private string _collate;
private DataGridViewRow _parent;
private Unique _unique;
private Table _table;
internal Column(Table table, DataGridViewRow row)
{
_parent = row;
_table = table as Table;
_unique = new Unique(this);
}
internal Column(DataRow row, Table source)
{
_table = source;
_unique = new Unique(this, row);
if (row.IsNull("AUTOINCREMENT") == false && (bool)row["AUTOINCREMENT"] == true)
_table.PrimaryKey.AutoIncrement = true;
_dataType = (row.IsNull("DATA_TYPE") == false) ? row["DATA_TYPE"].ToString() : String.Empty;
_columnName = row["COLUMN_NAME"].ToString();
_origName = _columnName;
_allowNulls = (bool)row["IS_NULLABLE"];
_defaultValue = (row.IsNull("COLUMN_DEFAULT") == false) ? row["COLUMN_DEFAULT"].ToString() : String.Empty;
_collate = (row.IsNull("COLLATION_NAME") == false) ? row["COLLATION_NAME"].ToString() : String.Empty;
string edmtype = (row.IsNull("EDM_TYPE") == false) ? row["EDM_TYPE"].ToString() : String.Empty;
if (edmtype == "nvarchar" || edmtype == "varchar" || edmtype == "blob" || edmtype == "nchar" || edmtype == "char")
{
int size = (row.IsNull("CHARACTER_MAXIMUM_LENGTH") == false) ? Convert.ToInt32(row["CHARACTER_MAXIMUM_LENGTH"], CultureInfo.InvariantCulture) : int.MaxValue;
if (size != int.MaxValue)
_dataType = string.Format(CultureInfo.InvariantCulture, "{0}({1})", _dataType, size);
}
else if (edmtype == "decimal")
{
int size = (row.IsNull("NUMERIC_PRECISION") == false) ? Convert.ToInt32(row["NUMERIC_PRECISION"], CultureInfo.InvariantCulture) : 53;
int scale = (row.IsNull("NUMERIC_SCALE") == false) ? Convert.ToInt32(row["NUMERIC_SCALE"], CultureInfo.InvariantCulture) : int.MaxValue;
if (size != 53)
{
string scalestr = (scale == int.MaxValue) ? "" : String.Format(CultureInfo.InvariantCulture, ",{0}", scale);
_dataType = string.Format(CultureInfo.InvariantCulture, "{0}({1}{2})", _dataType, size, scalestr);
}
}
}
#region IHaveConnection Members
[Browsable(false)]
public ViewTableBase DesignTable
{
get { return _table; }
}
public DbConnection GetConnection()
{
return ((IHaveConnection)_table).GetConnection();
}
#endregion
[Browsable(false)]
internal Table Table
{
get { return _table; }
}
internal void Committed()
{
_origName = ColumnName;
}
internal DataGridViewRow Parent
{
get { return _parent; }
set
{
_parent = value;
_parent.Cells[0].Value = ColumnName;
_parent.Cells[1].Value = DataType;
_parent.Cells[2].Value = AllowNulls;
}
}
internal void RefreshGrid()
{
if (_parent == null) return;
_parent.DataGridView.Refresh();
}
internal void CellValueChanged(int rowIndex, int cellIndex)
{
if (_parent == null) return;
if (rowIndex != _parent.Index) return;
object value;
if (_parent.Cells[cellIndex].IsInEditMode == true)
{
if (_parent.DataGridView.EditingControl != null)
value = ((IDataGridViewEditingControl)_parent.DataGridView.EditingControl).EditingControlFormattedValue;
else
value = _parent.Cells[cellIndex].EditedFormattedValue;
}
else
value = _parent.Cells[cellIndex].EditedFormattedValue;
switch (cellIndex)
{
case 0:
ColumnName = value.ToString();
break;
case 1:
DataType = value.ToString();
break;
case 2:
try
{
AllowNulls = Convert.ToBoolean(value, CultureInfo.InvariantCulture);
}
catch
{
}
break;
}
}
[DefaultValue("BINARY")]
[Category("Constraints")]
[Editor(typeof(CollationTypeEditor), typeof(UITypeEditor))]
[Description("The collation sequence to use for the column. This will affect comparison and equality tests against the column.")]
public virtual string Collate
{
get { return _collate; }
set
{
if (String.IsNullOrEmpty(value)) value = "BINARY";
if (_collate != value)
{
_collate = value;
if (_table.PrimaryKey.Columns.Count == 1 && String.Compare(ColumnName, _table.PrimaryKey.Columns[0].Column, StringComparison.OrdinalIgnoreCase) == 0)
_table.PrimaryKey.Columns[0].Collate = value;
_table._owner.MakeDirty();
}
}
}
[Category("Constraints")]
[Description("The unique constraints of the column")]
public virtual Unique Unique
{
get { return _unique; }
}
[Browsable(false)]
public virtual string ColumnName
{
get { return _columnName; }
set
{
value = value.Trim();
if (value != _columnName)
{
_columnName = value;
_table.MakeDirty();
}
}
}
[Browsable(false)]
public virtual string OriginalName
{
get { return _origName; }
}
[DefaultValue(false)]
[DisplayName("Allow Nulls")]
[Category("Constraints")]
[Description("Specifies whether or not the column will allow NULL values.")]
public virtual bool AllowNulls
{
get { return _allowNulls; }
set
{
if (value != _allowNulls)
{
_allowNulls = value;
if (_parent == null) return;
_parent.Cells[2].Value = _allowNulls;
_table.MakeDirty();
}
}
}
[Browsable(false)]
public virtual string DataType
{
get { return _dataType; }
set
{
value = value.Trim();
if (value != _dataType)
{
_dataType = value;
_table.MakeDirty();
}
}
}
[DisplayName("Default Value")]
[Category("Constraints")]
[Description("The default value to populate in the column when an explicit value is not specified.")]
public virtual string DefaultValue
{
get { return _defaultValue; }
set
{
value = value.Trim();
if (value != _defaultValue)
{
_defaultValue = value;
_table.MakeDirty();
}
}
}
internal void WriteSql(StringBuilder builder)
{
builder.AppendFormat("[{0}]", ColumnName);
if (String.IsNullOrEmpty(DataType) == false)
builder.AppendFormat(" {0}", DataType);
bool isprimary = false;
if (_table.PrimaryKey.Columns.Count == 1 && String.Compare(_table.PrimaryKey.Columns[0].Column, ColumnName, StringComparison.OrdinalIgnoreCase) == 0)
{
isprimary = true;
builder.Append(" PRIMARY KEY");
if (_table.PrimaryKey.Columns[0].SortMode != ColumnSortMode.Ascending)
builder.Append(" DESC");
if (_table.PrimaryKey.Conflict != ConflictEnum.Abort)
builder.AppendFormat(" ON CONFLICT {0}", _table.PrimaryKey.Conflict.ToString().ToUpperInvariant());
if (_table.PrimaryKey.AutoIncrement == true)
builder.Append(" AUTOINCREMENT");
}
if (AllowNulls == false)
builder.Append(" NOT NULL");
if (String.IsNullOrEmpty(Collate) == false && String.Compare(Collate, "BINARY", StringComparison.OrdinalIgnoreCase) != 0)
builder.AppendFormat(" COLLATE {0}", Collate.ToUpperInvariant());
if (Unique.Enabled == true && isprimary == false)
{
builder.Append(" UNIQUE");
if (Unique.Conflict != ConflictEnum.Abort)
builder.AppendFormat(" ON CONFLICT {0}", Unique.Conflict.ToString().ToUpperInvariant());
}
if (String.IsNullOrEmpty(DefaultValue) == false)
builder.AppendFormat(" DEFAULT {0}", DefaultValue);
}
}
}