Example #1
0
        public void Changed(DesignTable table = null)
        {
            _schema.Changed = true;

            if (table != null)
            {
                table.Changed = true;
            }
        }
Example #2
0
 public DesignTable CreateTable(DesignSchema schema, string name = null)
 {
     _schema = schema;
     Table   = new DesignTable {
         Name = name ?? "New Table", Changed = true, IsNew = true, Order = schema.Tables.Count
     };
     SetTableDefaults(Table);
     return(Table);
 }
Example #3
0
        private void CheckNewColumnDefault(DesignTable table)
        {
            if (table != null)
            {
                var last = table.Columns.Last();

                if (!last.IsEmpty())
                {
                    table.Columns.Add(new DesignColumn {
                    });
                }
            }
        }
Example #4
0
        public static StoreProperty ToStoreProperty(DesignSchema schema, DesignTable t, DesignColumn c)
        {
            var i = t.Columns.IndexOf(c);

            var result = new StoreProperty
            {
                Name          = c.Name,
                Type          = c.Type,
                Pk            = c.Pk,
                Fk            = !string.IsNullOrWhiteSpace(c.Reference),
                Nullable      = c.Nullable,
                AutoIncrement = c.Pk,
                ForeignKeys   = GetForeignKeysForColumn(schema, t, c),
                Order         = i++
            };

            return(result);
        }
Example #5
0
        private void SetTableDefaults(DesignTable table)
        {
            // create Id column
            if (table.Columns.Count == 0)
            {
                table.Columns.Add(new DesignColumn {
                    Disabled = true
                });
            }

            // Set Id column
            var PkColumn = table.Columns[0];

            PkColumn.Name = "Id";
            PkColumn.Type = _schema.UseIntId ? "int" : "guid";
            PkColumn.Pk   = true;

            CheckNewColumnDefault(table);
        }
Example #6
0
        public static StoreDefinition ToStoreDefinition(DesignSchema schema, DesignTable t)
        {
            var d = new StoreDefinition {
                Name = t.Name, Order = t.Order, Properties = new Dictionary <string, StoreProperty>()
            };
            int i = 0;

            d.Properties = t.Columns.Where(c => !c.IsEmpty()).ToDictionary(c => c.Name, c => new StoreProperty
            {
                Name          = c.Name,
                Type          = c.Type,
                Pk            = c.Pk,
                Fk            = !string.IsNullOrWhiteSpace(c.Reference),
                Nullable      = c.Nullable,
                AutoIncrement = c.Pk,
                ForeignKeys   = GetForeignKeysForColumn(schema, t, c),
                Order         = i++
            });

            return(d);
        }
Example #7
0
        private string GenerateScript(DesignTable table)
        {
            var sb = new StringBuilder();

            sb.AppendLine($"TABLE {table.Name} (");

            foreach (var c in table.Columns)
            {
                if (c.IsEmpty())
                {
                    continue;
                }

                var nullable  = c.Nullable ? "?" : "";
                var reference = string.IsNullOrWhiteSpace(c.Reference) ? "" : $" {c.Reference}";
                sb.AppendLine($"    {c.Name} {c.Type}{nullable}{reference},");
            }

            sb.AppendLine(")");

            return(sb.ToString());
        }
Example #8
0
        public static DesignSchema FromStoreSchema(StoreSchema s)
        {
            var ds = new DesignSchema
            {
                Name            = s.Name,
                DataContextName = s.DbContextName,
                Namespace       = s.Namespace,
                Version         = s.Version,
                VersionKey      = s.VersionKey,
            };

            if (s.Settings.ContainsKey("UseIntId"))
            {
                ds.UseIntId = bool.Parse(s.Settings["UseIntId"]);
            }

            foreach (var d in s.Definitions.Values)
            {
                var t = new DesignTable()
                {
                    Name  = d.Name,
                    Order = d.Order,
                };

                ds.Tables.Add(t);

                t.Columns = d.Properties.Select(x => x.Value).OrderBy(x => x.Order).Select(c => new DesignColumn()
                {
                    Name      = c.Name,
                    Pk        = c.Pk,
                    Type      = c.Type,
                    Nullable  = c.Nullable,
                    Reference = GetDesignForeignKey(c),
                }).ToList();
            }

            return(ds);
        }
Example #9
0
        private static List <StoreForeignKey> GetForeignKeysForColumn(DesignSchema schema, DesignTable t, DesignColumn c)
        {
            if (!string.IsNullOrWhiteSpace(c.TableReference) && !string.IsNullOrWhiteSpace(c.ColumnReference))
            {
                var type = schema.Tables.FirstOrDefault(t => t.Name == c.TableReference).Columns[0].Type;
                return((new StoreForeignKey[] { new StoreForeignKey {
                                                    DefinitionName = c.TableReference, PropertyName = c.ColumnReference, Type = type
                                                } }).ToList());
            }

            return(new List <StoreForeignKey>());
        }
Example #10
0
 private void CheckReferenceColumnDefaults(DesignTable table)
 {
 }
Example #11
0
 public void Load(DesignSchema schema, DesignTable table)
 {
     _schema = schema;
     Table   = table;
 }