/// <summary> /// Создаёт текст создания столбца /// </summary> /// <param name="column">Столбец</param> /// <returns>Текст создания столбца</returns> public static string BuildColumnCreateCommand(Column column) { StringBuilder sb = null; if (column.ColumnType.DbType.HasValue) { sb = GetDbTypeString(column); } else { sb = GetSqlDbTypeString(column); } if (column.ColumnType.Length != 0 && column.ColumnType.Scale == 0) { sb.AppendFormat("({0}) ", column.ColumnType.Length); } else if (column.ColumnType.Length != 0 && column.ColumnType.Scale != 0) { sb.AppendFormat("({0}, {1}) ", column.ColumnType.Length, column.ColumnType.Scale); } if (column.Constraint is Default) { sb.Append("default " + GetDefaultValue(column.Constraint as Default) + " "); } sb.Append(column.Nullable ? "null" : "not null"); return sb.ToString(); }
/// <summary> /// Создаёт текст создания столбца /// </summary> /// <param name="column">Столбец</param> /// <returns>Текст создания столбца</returns> public static string BuildColumnCreateCommand(Column column) { StringBuilder sb = null; if (column.ColumnType.DbType.HasValue) { sb = GetDbTypeString(column); } else { sb = GetSqlDbTypeString(column); } if (column.ColumnType.Length != 0 && column.ColumnType.Scale == 0) { sb.AppendFormat("({0}) ", column.ColumnType.Length); } else if (column.ColumnType.Length != 0 && column.ColumnType.Scale != 0) { sb.AppendFormat("({0}, {1}) ", column.ColumnType.Length, column.ColumnType.Scale); } sb.Append(column.Nullable ? "null" : "not null"); if (column.Identity) { sb.Append(" identity(1, 1)"); } return sb.ToString(); }
/// <summary> /// Добавляет колонку /// </summary> /// <param name="table">Таблица</param> /// <param name="column">Колонка</param> public void AddColumn(Table table, Column column) { try { strategy.AddColumn(table, column); } catch { } }
/// <summary> /// ��������� ������� /// </summary> /// <param name="table">�������</param> /// <param name="column">�������</param> public void AddColumn(Table table, Column column) { if (!column.Nullable && (column.Constraint == null || !(column.Constraint is Default))) { throw new ArgumentException(); } try { ExecuteNonQuery(razor.Parse(TemplateManager.GetAddColumnTemplate(), new object[] { table, column }, "add column")); } catch (TemplateCompilationException ex) { ex.Errors.ToList().ForEach(p => Console.WriteLine(p.ErrorText)); } log.Info("�������� ������� {0} � ������� {1}", column.Name, table.Name); }
/// <summary> /// Получает строку из перечисления DbType /// </summary> /// <param name="column">Колонка</param> /// <returns>Строка</returns> private static StringBuilder GetSqlDbTypeString(Column column) { var sb = new StringBuilder(string.Format("[{0}]", column.Name)); switch (column.ColumnType.SqlDbType) { case SqlDbType.VarBinary: sb.Append(" varbinary(MAX) "); break; default: throw new UnsuportedDbTypeException(column.ColumnType.DbType.GetValueOrDefault()); } return sb; }
/// <summary> /// Получает строку из перечисления DbType /// </summary> /// <param name="column">Колонка</param> /// <returns>Строка</returns> private static StringBuilder GetDbTypeString(Column column) { if (column.ColumnType.DbType == DbType.String && column.ColumnType.Length == 0) { throw new ArgumentException("String must have length"); } if (column.ColumnType.DbType == DbType.Binary && column.ColumnType.Length == 0) { throw new ArgumentException("Binary must have length"); } var sb = new StringBuilder(string.Format("[{0}]", column.Name)); switch (column.ColumnType.DbType) { case DbType.Decimal: sb.Append(" decimal"); break; case DbType.String: sb.Append(" nvarchar"); break; case DbType.Int32: sb.Append(" int "); break; case DbType.Boolean: sb.Append(" bit "); break; case DbType.Date: sb.Append(" date "); break; case DbType.DateTime: sb.Append(" datetime "); break; case DbType.Guid: sb.Append(" uniqueidentifier "); break; case DbType.Xml: sb.Append(" xml "); break; case DbType.Binary: sb.Append(" binary"); break; default: throw new UnsuportedDbTypeException(column.ColumnType.DbType.GetValueOrDefault()); } return sb; }
/// <summary> /// Получает строку из перечисления DbType /// </summary> /// <param name="column">Колонка</param> /// <returns>Строка</returns> private static StringBuilder GetDbTypeString(Column column) { if (column.ColumnType.DbType == DbType.String && column.ColumnType.Length == 0) { throw new ArgumentException("String must have length"); } if (column.ColumnType.DbType == DbType.Binary && column.ColumnType.Length == 0) { throw new ArgumentException("Binary must have length"); } var sb = new StringBuilder(string.Format("\"{0}\"", column.Name.ToUpper())); switch (column.ColumnType.DbType) { case DbType.Decimal: sb.Append(" number"); break; case DbType.String: sb.Append(" varchar2"); break; case DbType.Int32: sb.Append(" int "); break; case DbType.Boolean: sb.Append(" char(1) "); break; case DbType.Date: sb.Append(" date "); break; case DbType.DateTime: sb.Append(" date "); break; case DbType.Guid: sb.Append(" raw(16) "); break; case DbType.Xml: sb.Append(" blob "); break; case DbType.Binary: sb.Append(" raw"); break; default: throw new UnsuportedDbTypeException(column.ColumnType.DbType.GetValueOrDefault()); } return sb; }
/// <summary> /// Добавляет колонку к таблице /// </summary> /// <param name="column">Колонка</param> /// <returns>Табилца, с добавленой колонкой</returns> public Table AddColumn(Column column) { columns.Add(column); return this; }