Ejemplo n.º 1
0
        private void ExportDataContractClass(int version, TableNameData tnd)
        {
            DataTable dt          = tnd.Data;
            bool      allowDbNull = cmd.Has("NULL");

            string[] keys = cmd.Columns;

            DataColumn[] pk = dt.PrimaryKey;
            if (pk == null || pk.Length == 0)
            {
                pk = dt.PrimaryKeys(keys);

                if (pk.Length == 0)
                {
                    dt.PrimaryKey = new DataColumn[] { dt.Columns[0] };
                    cout.WriteLine($"no primary key found on Table: \"{dt.TableName}\"");
                }

                dt.PrimaryKey = pk;
            }


            TheClassBuilder gen = null;

            if (version == 0)
            {
                gen = new DataContractClassBuilder(cmd, tnd.Name, dt, allowDbNull);
            }
            else if (version == 1)
            {
                gen = new DataContract1ClassBuilder(cmd, tnd.Name, dt, allowDbNull);
            }
            else
            {
                gen = new DataContract2ClassBuilder(cmd, tnd.Name, dt, allowDbNull);
            }

            if (gen != null)
            {
                string path = cmd.OutputPath(ConfigKey._GENERATOR_DC_PATH, $"{ConfigurationEnvironment.MyDocuments}\\dc");
                string ns   = cmd.GetValue("ns", ConfigKey._GENERATOR_DC_NS, "Sys.DataModel.DataContract");
                string mtd  = cmd.GetValue("method");

                gen.SetNamespace(ns);
                gen.SetClassName(dt.TableName);
                gen.SetMethod(mtd);
                string file = gen.WriteFile(path);
                cout.WriteLine("code generated on {0}", file);
            }

            TableSchemaCache.Clear();
        }
Ejemplo n.º 2
0
        public void ExportLinq2SQLClass()
        {
            string path = cmd.OutputPath(ConfigKey._GENERATOR_L2S_PATH, $"{ConfigurationEnvironment.MyDocuments}\\dc");
            string ns   = cmd.GetValue("ns", ConfigKey._GENERATOR_L2S_NS, "Sys.DataModel.L2s");

            if (tname != null)
            {
                var builder = new Linq2SQLClassBuilder(cmd, tname)
                {
                };
                builder.SetNamespace(ns);

                string file = builder.WriteFile(path);
                cout.WriteLine("code generated on {0}", file);
            }
            else if (dname != null)
            {
                TableName[] tnames = getTableNames(cmd);
                foreach (var tname in tnames)
                {
                    var builder = new Linq2SQLClassBuilder(cmd, tname)
                    {
                    };
                    builder.SetNamespace(ns);

                    string file = builder.WriteFile(path);
                    cout.WriteLine("code generated on {0}", file);
                }
            }
            else
            {
                cerr.WriteLine("warning: table or database is not seleted");
            }

            TableSchemaCache.Clear();
        }
Ejemplo n.º 3
0
        protected List <AssociationPropertyInfo> CreateAssoicationClass(TableName tname, Class clss)
        {
            List <AssociationPropertyInfo> properties = new List <AssociationPropertyInfo>();

            var schema = TableSchemaCache.GetSchema(tname);
            var pkeys  = schema.ByForeignKeys.Keys.OrderBy(k => k.FK_Table);

            foreach (IForeignKey pkey in pkeys)
            {
                string entity = ident.Identifier(pkey.FK_Table);

                TypeInfo type;
                string   propertyName;
                bool     one2many = IsOneToMany(tname, pkey);
                if (one2many)
                {
                    type = new TypeInfo {
                        UserType = $"EntitySet<{entity}>"
                    };
                    propertyName = Plural.Pluralize(entity);
                }
                else
                {
                    type = new TypeInfo {
                        UserType = $"EntityRef<{entity}>"
                    };
                    propertyName = Plural.Singularize(entity);
                }

                var property = new Property(type, propertyName);
                clss.Add(property);
                properties.Add(new AssociationPropertyInfo
                {
                    PropertyType = entity,
                    PropertyName = propertyName,
                    OneToMany    = one2many,
                    PK_Column    = pkey.PK_Column,
                    FK_Column    = pkey.FK_Column,
                });
            }

            var fkeys = schema.ForeignKeys.Keys.OrderBy(k => k.FK_Table);

            foreach (IForeignKey fkey in fkeys)
            {
                string   entity = ident.Identifier(fkey.PK_Table);
                TypeInfo type   = new TypeInfo {
                    UserType = $"EntityRef<{entity}>"
                };
                string propertyName = Plural.Singularize(entity);
                var    property     = new Property(type, propertyName);
                clss.Add(property);

                properties.Add(new AssociationPropertyInfo
                {
                    PropertyType = entity,
                    PropertyName = propertyName,
                    OneToMany    = false,
                    PK_Column    = fkey.FK_Column,
                    FK_Column    = fkey.PK_Column,
                });
            }

            return(properties);
        }
Ejemplo n.º 4
0
        protected static Field CreateConstraintField(TableName tname)
        {
            const string CONSTRAINT = nameof(Constraint);

            Value ToColumn(string table, string column)
            {
                table  = ident.Identifier(table);
                column = COLUMN(column);
                column = $"{table}{EXTENSION}.{column}";
                return(new Value(new CodeString(column)));
            }

            Value ToColumn2(string column)
            {
                column = COLUMN(column);
                return(new Value(new CodeString(column)));
            }

            var schema = TableSchemaCache.GetSchema(tname);
            var pkeys  = schema.ByForeignKeys.Keys.OrderBy(k => k.FK_Table);

            List <Value> L = new List <Value>();

            foreach (IForeignKey pkey in pkeys)
            {
                string   entity = ident.Identifier(pkey.FK_Table);
                TypeInfo type   = new TypeInfo {
                    UserType = $"{CONSTRAINT}<{entity}>"
                };
                var V = Value.NewPropertyObject(type);
                V.AddProperty(nameof(IConstraint.ThisKey), ToColumn2(pkey.PK_Column));
                V.AddProperty(nameof(IConstraint.OtherKey), ToColumn(pkey.FK_Table, pkey.FK_Column));
                if (IsOneToMany(tname, pkey))
                {
                    V.AddProperty(nameof(IConstraint.OneToMany), new Value(true));
                }
                L.Add(V);
            }

            var fkeys = schema.ForeignKeys.Keys.OrderBy(k => k.FK_Table);

            foreach (IForeignKey fkey in fkeys)
            {
                string   entity = ident.Identifier(fkey.PK_Table);
                TypeInfo type   = new TypeInfo {
                    UserType = $"{CONSTRAINT}<{entity}>"
                };
                var V = Value.NewPropertyObject(type);
                V.AddProperty(nameof(IConstraint.Name), new Value(fkey.Constraint_Name));
                V.AddProperty(nameof(IConstraint.ThisKey), ToColumn2(fkey.FK_Column));
                V.AddProperty(nameof(IConstraint.OtherKey), ToColumn(fkey.PK_Table, fkey.PK_Column));
                V.AddProperty(nameof(IConstraint.IsForeignKey), new Value(true));
                L.Add(V);
            }

            TypeInfo typeinfo = new TypeInfo {
                UserType = $"{nameof(IConstraint)}[]"
            };

            Field field = new Field(typeinfo, $"{CONSTRAINT}s", new Value(L.ToArray())
            {
                Type = typeinfo
            })
            {
                Modifier = Modifier.Public | Modifier.Static | Modifier.Readonly
            };

            return(field);
        }
Ejemplo n.º 5
0
        protected override void CreateClass()
        {
            var clss = new Class(ClassName)
            {
                Modifier = Modifier.Public | Modifier.Partial
            };

            clss.AddAttribute(new AttributeInfo("Table", new { Name = tname.ShortName }));

            builder.AddClass(clss);

            CodeStyle   camel  = cmd.GetEnum <CodeStyle>("code-style", CodeStyle.Original);
            TableSchema schema = TableSchemaCache.GetSchema(tname);

            Property prop;

            foreach (IColumn column in schema.Columns)
            {
                TypeInfo ty = new TypeInfo {
                    UserType = column.DataType.GetFieldType(column.Nullable)
                };

                prop = new Property(ty, column.ToFieldName(camel))
                {
                    Modifier = Modifier.Public
                };

                List <object> args = new List <object>();
                args.Add(new { Name = column.ColumnName });

                //args.Add(new { DbType = ColumnSchema.GetSQLType(column) + (column.Nullable ? " NULL" : " NOT NULL") });

                if (column.IsPrimary)
                {
                    args.Add(new { IsPrimaryKey = true });
                }

                if (column.IsIdentity)
                {
                    args.Add(new { IsDbGenerated = true });
                }

                if (!column.IsPrimary && !column.Nullable)
                {
                    args.Add(new { CanBeNull = false });
                }

                if (column.CType == CType.Text || column.CType == CType.NText)
                {
                    args.Add(new AttributeInfoArg("UpdateCheck", "UpdateCheck.Never"));
                }

                prop.AddAttribute(new AttributeInfo("Column", args.ToArray()));

                if (!column.IsComputed)
                {
                    clss.Add(prop);
                }
            }

            var fkBy = schema.ByForeignKeys.Keys.OrderBy(k => k.FK_Table);

            Constructor constructor = null;

            if (fkBy.Count() > 0)
            {
                clss.AppendLine();

                constructor = new Constructor(this.ClassName);
            }



            List <Property> list = new List <Property>();

            foreach (var key in fkBy)
            {
                prop = AddEntitySet(clss, constructor, key);
                list.Add(prop);
            }

            var fks = schema.ForeignKeys;

            //list = new List<Property>();

            if (fks.Length > 0)
            {
                clss.AppendLine();
            }

            foreach (var key in fks.Keys)
            {
                prop = AddEntityRef(clss, key);
                list.Add(prop);
            }

            if (constructor != null)
            {
                clss.Add(constructor);
            }

            foreach (var p in list)
            {
                clss.Add(p);
            }
        }
Ejemplo n.º 6
0
        /// <summary>
        /// add children tables
        /// </summary>
        /// <param name="clss"></param>
        /// <param name="constructor"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        private Property AddEntitySet(Class clss, Constructor constructor, IForeignKey key)
        {
            TableName fk_tname = new TableName(tname.DatabaseName, key.FK_Schema, key.FK_Table);
            string    fk_cname = fk_tname.ToClassName(rule: null);
            string    pname;

            Property prop;
            TypeInfo ty;
            Field    field;

            var fk_schema = TableSchemaCache.GetSchema(fk_tname);
            var _keys     = fk_schema.PrimaryKeys.Keys;

            if (_keys.Length == 1 && _keys.Contains(key.FK_Column))
            {
                // 1:1 mapping
                pname = clss.MakeUniqueName(Plural.Singularize(fk_cname));
                ty    = new TypeInfo {
                    UserType = $"EntityRef<{fk_cname}>"
                };
                field = new Field(ty, $"_{pname}")
                {
                    Modifier = Modifier.Private
                };

                prop = new Property(new TypeInfo {
                    UserType = fk_cname
                }, pname)
                {
                    Modifier = Modifier.Public
                };
                prop.Gets.Append($"return this._{pname}.Entity;");
                prop.Sets.Append($"this._{pname}.Entity = value;");

                prop.AddAttribute(new AttributeInfo("Association",
                                                    new
                {
                    Name         = $"{this.ClassName}_{fk_cname}",
                    Storage      = $"_{pname}",
                    ThisKey      = key.PK_Column,
                    OtherKey     = key.FK_Column,
                    IsUnique     = true,
                    IsForeignKey = false
                }));
            }
            else
            {
                //1:n mapping
                pname = clss.MakeUniqueName(Plural.Pluralize(fk_cname));
                constructor.Statement.AppendLine($"this._{pname} = new EntitySet<{fk_cname}>();");

                ty = new TypeInfo {
                    UserType = $"EntitySet<{fk_cname}>"
                };
                field = new Field(ty, $"_{pname}")
                {
                    Modifier = Modifier.Private
                };

                prop = new Property(ty, pname)
                {
                    Modifier = Modifier.Public
                };
                prop.Gets.Append($"return this._{pname};");
                prop.Sets.Append($"this._{pname}.Assign(value);");

                prop.AddAttribute(new AttributeInfo("Association",
                                                    new
                {
                    Name         = $"{this.ClassName}_{fk_cname}",
                    Storage      = $"_{pname}",
                    ThisKey      = key.PK_Column,
                    OtherKey     = key.FK_Column,
                    IsForeignKey = false
                }));
            }

            clss.Add(field);


            return(prop);
        }