public ColumnDef(ColumnDef copySource, int index)
     : this(
         name : String.Format(copySource.Name, index),
         size : copySource.Size,
         type : copySource.Type,
         nullable : copySource.Nullable,
         copies : 1
         )
 {
 }
        public void AddColumn(ColumnDef columnDef)
        {
            this.Columns.Add(columnDef);
            var tableCol = new DataColumn(columnDef.Name);

            tableCol.AllowDBNull = columnDef.Nullable;
            if (columnDef.Size != 0)
            {
                tableCol.MaxLength = columnDef.Size;
            }
            this.Table.Columns.Add(tableCol);
            this.BulkCopy.ColumnMappings.Add(new SqlBulkCopyColumnMapping(columnDef.Name, columnDef.Name));
        }
        public static ColumnDef[] GetColumns(Type type, bool expandCopies = true)
        {
            ColumnDef[] columns;
            lock (_columns)
            {
                if (_columns.TryGetValue(type, out columns))
                {
                    return(columns);
                }

                FieldInfo[] fields = type.GetFields(BindingFlags.Static | BindingFlags.Public);
                columns = new ColumnDef[fields.Length];
                for (int i = 0; i < fields.Length; i++)
                {
                    columns[i] = (ColumnDef)fields[i].GetValue(null);
                }
                _columns.Add(type, columns);
            }

            if (expandCopies)
            {
                var expanded = new List <ColumnDef>(columns.Length);
                foreach (ColumnDef col in columns)
                {
                    if (col.Copies <= 1)
                    {
                        expanded.Add(col);
                    }
                    else
                    {
                        for (int i = 1; i <= col.Copies; i++)
                        {
                            expanded.Add(new ColumnDef(col, i));
                        }
                    }
                }
                columns = expanded.ToArray();
            }

            return(columns);
        }
        public string GetCreateTableSql()
        {
            StringBuilder builder = new StringBuilder();

            builder.AppendFormat("create table [dbo].{0} (\n", this.Table.TableName);
            for (int i = 0; i < this.Columns.Count; i++)
            {
                ColumnDef col = this.Columns[i];
                builder.AppendFormat("\t[{0}] [{1}] {2} {3} {4}, \n",
                                     col.Name,
                                     col.Type,
                                     col.Size != 0 ? string.Format("({0})", col.Size) : null,
                                     col.Nullable ? "null" : "not null",
                                     col.DefaultValue != string.Empty ? string.Format("Default {0}", col.DefaultValue) : string.Empty
                                     );
            }
            builder.Remove(builder.Length - 1, 1);
            builder.Append(");");

            string cmdText = builder.ToString();

            return(cmdText);
        }