Beispiel #1
0
        public static List <TableColumn> BuildTableColumns(List <Column> columns, string tableName)
        {
            var list = new List <TableColumn>();

            foreach (var item in columns)
            {
                var col = new TableColumn
                {
                    AllowNull    = item.IsNullable == YesNoOption.YES,
                    DataType     = item.DataType,
                    DefaultValue = item.ColumnDefault,
                    Identity     = item.AutoIncrementBy > 1,
                    Name         = item.ColumnName,
                    Precision    = byte.Parse(item.NumericPrecision.ToString()),
                    //TODO Detect!
                    PrimaryKey = false,
                    Scale      = byte.Parse(item.NumericScale.ToString())
                };
                short colLength;
                if (short.TryParse(item.CharacterMaxLength.ToString(), out colLength))
                {
                    col.Length = colLength;
                }
                if (TableDataType.IsLengthFixed(item.DataType))
                {
                    col.Length = TableDataType.GetDefaultLength(item.DataType);
                }
                list.Add(col);
            }
            return(list);
        }
Beispiel #2
0
 public static string ValidateColumns(List <TableColumn> columns)
 {
     foreach (var item in columns)
     {
         if (string.IsNullOrEmpty(item.DataType))
         {
             return("Data type is required");
         }
         if (string.IsNullOrEmpty(item.Name))
         {
             return("Column name is required");
         }
         if (!TableDataType.IsLengthFixed(item.DataType))
         {
             if (item.Length < TableDataType.GetMinLength(item.DataType) ||
                 item.Length > TableDataType.GetMaxLength(item.DataType))
             {
                 return(string.Format("Column length of colum {0} must be between {1} and {2}",
                                      item.Name, TableDataType.GetMinLength(item.DataType), TableDataType.GetMaxLength(item.DataType)));
             }
         }
         //Check for duplicates
         if (columns.Select(x => x.Name).ToList()
             .GroupBy(x => x)
             .Where(x => x.Count() > 1)
             .Select(x => x.Key).ToList().Count > 0)
         {
             return("Duplicate column names not allowed");
         }
         //Check for only 1 identity column
         var identityCols = columns.Where(x => x.Identity).ToList();
         if (identityCols.Count > 1)
         {
             return("Only a single Identity column allowed");
         }
         if (item.DataType == "numeric")
         {
             if (item.Precision < 1)
             {
                 return(string.Format("numeric precision of column {0} must be at least 1", item.Name));
             }
             if (item.Scale > item.Precision)
             {
                 return(string.Format("numeric scale of column {0} cannot be higher than the precision", item.Name));
             }
             if (item.Precision > 38)
             {
                 return(string.Format("numeric precision of column {0} cannot be higher than 38", item.Name));
             }
             if (item.Scale > 38)
             {
                 return(string.Format("numeric scale of column {0} cannot be higher than 38", item.Name));
             }
         }
     }
     return(string.Empty);
 }