Exemple #1
0
 private IEnumerable <ExplorerItem> GetParams(SchemaObject o)
 {
     return(from p in o.Parameters select new ExplorerItem(p.ClrName + " (" + p.ClrType.FormatTypeName() + ")", ExplorerItemKind.Parameter, ExplorerIcon.Parameter)
     {
         DragText = p.ClrName, SqlName = p.ParamName
     });
 }
Exemple #2
0
        private List <ExplorerItem> GetChildItems(SchemaObject o, bool isSqlServer)
        {
            IEnumerable <ExplorerItem> first = Enumerable.Empty <ExplorerItem>();

            if (o.Parameters.Count > 0)
            {
                first = first.Concat <ExplorerItem>(this.GetParams(o));
            }
            first = first.Concat <ExplorerItem>(from col in o.ColumnsInOrder
                                                let text = col.PropertyName + " (" + col.ClrType.FormatTypeName() + ")"
                                                           let sqlTypeDec = col.GetFullSqlTypeDeclaration()
                                                                            select new ExplorerItem(text, ExplorerItemKind.Property, col.IsKey ? ExplorerIcon.Key : ExplorerIcon.Column)
            {
                DragText = col.PropertyName, ToolTipText = (sqlTypeDec == null) ? null : (col.ColumnName + " " + sqlTypeDec), SqlName = col.ColumnName, SqlTypeDeclaration = sqlTypeDec
            });
            Table t = o as Table;

            if (t != null)
            {
                IOrderedEnumerable <ExplorerItem> second = from a in this.GetParents(t).Concat <ExplorerItem>(this.GetChildren(t))
                                                           orderby a.Icon != ExplorerIcon.OneToOne
                                                           select a;
                first = first.Concat <ExplorerItem>(second);
                if (!(t.HasKey || !isSqlServer))
                {
                    first = first.Concat <ExplorerItem>(new ExplorerItem[] { new ExplorerItem("(Warning: No primary key, updates will fail)", ExplorerItemKind.Property, ExplorerIcon.Inherited) });
                }
            }
            return(first.ToList <ExplorerItem>());
        }
Exemple #3
0
        public EntityCodeGen(bool singularize, SchemaObject schemaObject)
            : base(singularize)
        {
            if (schemaObject == null) {
                throw new ArgumentNullException("schemaObject");
            }

            _schemaObject = schemaObject;
        }
Exemple #4
0
 private void ReadRoutineParameters(IEnumerable <Parameter> routineParameters)
 {
     if (routineParameters != null)
     {
         SchemaObject obj2 = null;
         string       str  = null;
         foreach (Parameter parameter in routineParameters)
         {
             string key = parameter.RoutineSchema + "." + parameter.RoutineName;
             if (key != str)
             {
                 if (!this.Objects.TryGetValue(key, out obj2))
                 {
                     obj2 = null;
                 }
                 str = key;
             }
             if (obj2 != null)
             {
                 if (!parameter.IsValid)
                 {
                     this.Objects.Remove(key);
                 }
                 else if (parameter.IsResult)
                 {
                     obj2.ReturnInfo = parameter;
                 }
                 else
                 {
                     obj2.Parameters.Add(parameter);
                 }
             }
         }
         foreach (SchemaObject obj3 in this.Objects.Values)
         {
             DotNetNameBank bank = new DotNetNameBank();
             foreach (Parameter parameter2 in obj3.Parameters)
             {
                 bank.RegisterName(parameter2.ParamName);
             }
             foreach (Parameter parameter2 in obj3.Parameters.ToArray <Parameter>())
             {
                 string uniqueDotNetName = bank.GetUniqueDotNetName(parameter2.ParamName);
                 if (uniqueDotNetName == null)
                 {
                     obj2.Parameters.Remove(parameter2);
                 }
                 else
                 {
                     parameter2.ClrName = uniqueDotNetName;
                 }
             }
         }
     }
 }
Exemple #5
0
        private static IDictionary<int, Column> GetColumns(SchemaObject schemaObject)
        {
            var table = schemaObject as Table;

            if (table != null) {
                return table.Columns;
            }

            var view = schemaObject as View;

            if (view != null) {
                return view.Columns;
            }

            return null;
        }
 private void EmitCanonFunctionMethod(TypeBuilder dcType, string methName, MethodInfo parentMethod, Action<ILGenerator> dcFieldLoader, SchemaObject funcInfo)
 {
     if (parentMethod != null)
     {
         FunctionGenerator generator = new FunctionGenerator(this, dcType, funcInfo, methName, MethodAttributes.Public);
         if (generator.Method != null)
         {
             ILGenerator iLGenerator = generator.Method.GetILGenerator();
             iLGenerator.Emit(OpCodes.Ldarg_0);
             dcFieldLoader(iLGenerator);
             int position = 0;
             foreach (Parameter parameter in funcInfo.Parameters)
             {
                 position++;
                 generator.Method.DefineParameter(position, ParameterAttributes.None, parameter.ClrName);
                 iLGenerator.Emit(OpCodes.Ldarg, position);
             }
             iLGenerator.Emit(OpCodes.Call, parentMethod);
             iLGenerator.Emit(OpCodes.Ret);
         }
     }
 }
 private MethodInfo EmitFunctionMethod(TypeBuilder dcType, string methName, SchemaObject funcInfo, string objectPrefix, MethodAttributes accessibility)
 {
     FunctionGenerator generator = new FunctionGenerator(this, dcType, funcInfo, methName, accessibility);
     if (generator.Method == null)
     {
         return null;
     }
     generator.AddAttributes(objectPrefix);
     ILGenerator iLGenerator = generator.Method.GetILGenerator();
     LocalBuilder local = iLGenerator.DeclareLocal(typeof(object[]));
     iLGenerator.Emit(OpCodes.Ldarg_0);
     iLGenerator.Emit(OpCodes.Ldarg_0);
     iLGenerator.Emit(OpCodes.Call, typeof(MethodBase).GetMethod("GetCurrentMethod", BindingFlags.Public | BindingFlags.Static, null, Type.EmptyTypes, null));
     iLGenerator.Emit(OpCodes.Castclass, typeof(MethodInfo));
     iLGenerator.Emit(OpCodes.Ldc_I4, funcInfo.Parameters.Count);
     iLGenerator.Emit(OpCodes.Newarr, typeof(object));
     iLGenerator.Emit(OpCodes.Stloc, local);
     int arg = 0;
     foreach (Parameter parameter in funcInfo.Parameters)
     {
         iLGenerator.Emit(OpCodes.Ldloc, local);
         iLGenerator.Emit(OpCodes.Ldc_I4, arg);
         iLGenerator.Emit(OpCodes.Ldarg, (int) (arg + 1));
         if (parameter.ClrType.IsValueType)
         {
             iLGenerator.Emit(OpCodes.Box, parameter.ClrType);
         }
         iLGenerator.Emit(OpCodes.Stelem_Ref);
         arg++;
     }
     iLGenerator.Emit(OpCodes.Ldloc, local);
     MethodInfo meth = typeof(DataContext).GetMethod((funcInfo is TableFunction) ? "CreateMethodCallQuery" : "ExecuteMethodCall", BindingFlags.NonPublic | BindingFlags.Instance, null, new Type[] { typeof(object), typeof(MethodInfo), typeof(object[]) }, null);
     if (funcInfo is TableFunction)
     {
         meth = meth.MakeGenericMethod(new Type[] { generator.ResultType });
     }
     iLGenerator.Emit(OpCodes.Call, meth);
     if (funcInfo is ScalarFunction)
     {
         iLGenerator.Emit(OpCodes.Callvirt, typeof(IExecuteResult).GetProperty("ReturnValue").GetGetMethod());
         iLGenerator.Emit(generator.ResultType.IsValueType ? OpCodes.Unbox_Any : OpCodes.Castclass, generator.ResultType);
     }
     iLGenerator.Emit(OpCodes.Ret);
     return generator.Method;
 }
Exemple #8
0
        private void WriteColumns(SchemaObject schemaObject)
        {
            var columns = GetColumns(schemaObject);

            if (columns == null) {
                return;
            }

            foreach (var column in columns.Values.Cast<VfpColumn>()) {
                WriteTab();
                Write("[Column(Member=\"");
                Write(column.PropertyName);
                Write("\"");

                if (!column.ColumnName.Equals(column.PropertyName, StringComparison.InvariantCultureIgnoreCase)) {
                    Write(", Name=\"");
                    Write(column.ColumnName);
                    Write("\"");
                }

                if (column.IsKey) {
                    Write(", IsPrimaryKey=true");

                    if (column.IsAutoGen) {
                        Write(", IsGenerated=true");
                    }
                }

                if (!string.IsNullOrWhiteSpace(column.FieldType)) {
                    Write(", DbType=\"");
                    Write(column.FieldType);
                    Write("\"");
                }

                WriteLine(")]");
            }
        }
Exemple #9
0
        private void WriteAssociations(SchemaObject schemaObject)
        {
            var table = schemaObject as Table;

            if (table == null) {
                return;
            }

            WriteAssociations(table.ParentRelations, true);
            WriteAssociations(table.ChildRelations, false);
        }
 private Type EmitTableClass(SchemaObject t, string objectPrefix)
 {
     TypeBuilder builder = this._tableTypes[t.DotNetName];
     ConstructorInfo constructor = typeof(TableAttribute).GetConstructor(new Type[0]);
     PropertyInfo property = typeof(TableAttribute).GetProperty("Name");
     string str = BracketsForSql((t.SchemaName == "dbo") ? "" : t.SchemaName);
     if ((objectPrefix.Length > 0) && (str.Length == 0))
     {
         str = "[dbo]";
     }
     string str2 = BracketsForSql(t.SqlName);
     if (str.Length > 0)
     {
         str2 = str + "." + str2;
     }
     str2 = objectPrefix + str2;
     CustomAttributeBuilder customBuilder = new CustomAttributeBuilder(constructor, new object[0], new PropertyInfo[] { property }, new object[] { str2 });
     builder.SetCustomAttribute(customBuilder);
     ILGenerator iLGenerator = builder.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, new Type[0]).GetILGenerator();
     foreach (SqlColumn column in t.Columns.Values)
     {
         this.EmitColumn(builder, column);
     }
     Table table = t as Table;
     if (table != null)
     {
         foreach (Relationship relationship in table.ParentRelations)
         {
             this.EmitParentRelation(iLGenerator, builder, relationship);
         }
         foreach (Relationship relationship in table.ChildRelations)
         {
             if (relationship.IsOneToOne)
             {
                 this.EmitOneToOneOnChildSide(iLGenerator, builder, relationship);
             }
             else
             {
                 this.EmitChildRelation(iLGenerator, builder, relationship);
             }
         }
     }
     iLGenerator.Emit(OpCodes.Ret);
     return builder;
 }
 private void EmitTableProperty(TypeBuilder dcType, Action<ILGenerator> dcFieldLoader, SchemaObject tableInfo)
 {
     Type type = this._tableTypes[tableInfo.DotNetName];
     string propertyName = tableInfo.PropertyName;
     string name = "get_" + propertyName;
     Type returnType = typeof(Table<>).MakeGenericType(new Type[] { type });
     PropertyBuilder builder = dcType.DefineProperty(propertyName, PropertyAttributes.None, returnType, new Type[0]);
     MethodBuilder mdBuilder = dcType.DefineMethod(name, MethodAttributes.SpecialName | MethodAttributes.Public, returnType, new Type[0]);
     MethodInfo meth = typeof(DataContext).GetMethod("GetTable", new Type[0]).MakeGenericMethod(new Type[] { type });
     ILGenerator iLGenerator = mdBuilder.GetILGenerator();
     iLGenerator.Emit(OpCodes.Ldarg_0);
     if (dcFieldLoader != null)
     {
         dcFieldLoader(iLGenerator);
     }
     iLGenerator.Emit(OpCodes.Call, meth);
     iLGenerator.Emit(OpCodes.Ret);
     builder.SetGetMethod(mdBuilder);
 }
Exemple #12
0
        private void ReadColumns(IEnumerable <Column> columns)
        {
            string str2;
            Dictionary <string, string> dictionary = new Dictionary <string, string>();
            List <Column> list = new List <Column>(0x1000);

            foreach (Column column in columns)
            {
                string str;
                list.Add(column);
                if (dictionary.TryGetValue(column.ObjectName, out str))
                {
                    if ((str != null) && (str != column.SchemaName))
                    {
                        dictionary[column.ObjectName] = null;
                    }
                }
                else
                {
                    dictionary.Add(column.ObjectName, column.SchemaName);
                }
            }
            DotNetNameBank bank = new DotNetNameBank();

            foreach (Column column in list)
            {
                str2 = dictionary[column.ObjectName];
                string sqlName = (((str2 != null) || (column.SchemaName == "dbo")) || (column.SchemaName == "")) ? column.ObjectName : (column.SchemaName + "_" + column.ObjectName);
                bank.RegisterName(sqlName);
            }
            HashSet <string> set = new HashSet <string>();

            foreach (Column column in list)
            {
                if (dictionary.TryGetValue(column.ObjectName, out str2))
                {
                    string str4             = (((str2 != null) || (column.SchemaName == "dbo")) || (column.SchemaName == "")) ? column.ObjectName : (column.SchemaName + "_" + column.ObjectName);
                    string uniqueDotNetName = bank.GetUniqueDotNetName(str4);
                    if (uniqueDotNetName != null)
                    {
                        if ((column.ObjectKind == DbObjectKind.Table) || (column.ObjectKind == DbObjectKind.View))
                        {
                            uniqueDotNetName = this.TransformIdentifier(uniqueDotNetName);
                        }
                        string key = column.SchemaName + "." + column.ObjectName;
                        if (!this.Objects.ContainsKey(key))
                        {
                            if (set.Contains(uniqueDotNetName))
                            {
                                continue;
                            }
                            set.Add(uniqueDotNetName);
                            SchemaObject obj2 = SchemaObject.Create(column, uniqueDotNetName);
                            this.Objects.Add(key, obj2);
                        }
                        SchemaObject obj3 = this.Objects[key];
                        if (column.IsKey)
                        {
                            obj3.HasKey = true;
                        }
                        column.ClrObjectName          = uniqueDotNetName;
                        column.Object                 = obj3;
                        obj3.Columns[column.ColumnID] = column;
                    }
                }
            }
            foreach (SchemaObject obj2 in this.Objects.Values)
            {
                obj2.OriginalName = obj2.PropertyName;
                if (this.Pluralize && obj2.IsPluralizable)
                {
                    string pluralName = StringUtil.GetPluralName(obj2.PropertyName);
                    if (!set.Contains(pluralName))
                    {
                        obj2.PropertyName = pluralName;
                    }
                }
            }
        }
Exemple #13
0
 protected string GetEntityClassName(SchemaObject schemaObject)
 {
     return _singularize ? _pluralizationService.Singularize(schemaObject.DotNetName) : schemaObject.DotNetName;
 }
 private void EmitStoredProc(TypeBuilder dcType, Action<ILGenerator> dcFieldLoader, SchemaObject procInfo, string objectPrefix, bool useOptional)
 {
     Func<Parameter, Type> func;
     if (useOptional)
     {
         func = p => typeof(Optional<>).MakeGenericType(new Type[] { p.ClrType });
     }
     else
     {
         func = p => p.ClrType;
     }
     MethodBuilder builder = dcType.DefineMethod(procInfo.PropertyName, MethodAttributes.Public, typeof(ReturnDataSet), procInfo.Parameters.Select<Parameter, Type>(func).ToArray<Type>());
     ParameterAttributes attributes = useOptional ? (ParameterAttributes.HasDefault | ParameterAttributes.Optional) : ParameterAttributes.None;
     int arg = 1;
     foreach (Parameter parameter in procInfo.Parameters)
     {
         builder.DefineParameter(arg++, attributes, parameter.ClrName);
     }
     ILGenerator iLGenerator = builder.GetILGenerator();
     LocalBuilder local = iLGenerator.DeclareLocal(typeof(object[]));
     iLGenerator.Emit(OpCodes.Ldarg_0);
     if (dcFieldLoader != null)
     {
         dcFieldLoader(iLGenerator);
     }
     iLGenerator.Emit(OpCodes.Ldstr, string.Concat(new object[] { objectPrefix, '[', procInfo.SchemaName, "].[", procInfo.SqlName, ']' }));
     iLGenerator.Emit(OpCodes.Ldc_I4, procInfo.Parameters.Count);
     iLGenerator.Emit(OpCodes.Newarr, typeof(object));
     iLGenerator.Emit(OpCodes.Stloc, local);
     arg = 0;
     foreach (Parameter parameter in procInfo.Parameters)
     {
         iLGenerator.Emit(OpCodes.Ldloc, local);
         iLGenerator.Emit(OpCodes.Ldc_I4, arg);
         iLGenerator.Emit(OpCodes.Ldarg, (int) (arg + 1));
         Type clrType = parameter.ClrType;
         if (useOptional)
         {
             clrType = typeof(Optional<>).MakeGenericType(new Type[] { clrType });
         }
         if (clrType.IsValueType)
         {
             iLGenerator.Emit(OpCodes.Box, clrType);
         }
         iLGenerator.Emit(OpCodes.Stelem_Ref);
         arg++;
     }
     iLGenerator.Emit(OpCodes.Ldloc, local);
     MethodInfo meth = typeof(DataContextBase).GetMethod("ExecuteStoredProcedure", BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance, null, new Type[] { typeof(string), typeof(object[]) }, null);
     iLGenerator.Emit(OpCodes.Call, meth);
     iLGenerator.Emit(OpCodes.Ret);
 }
Exemple #15
0
 private List<ExplorerItem> GetChildItems(SchemaObject o, bool isSqlServer)
 {
     IEnumerable<ExplorerItem> first = Enumerable.Empty<ExplorerItem>();
     if (o.Parameters.Count > 0)
     {
         first = first.Concat<ExplorerItem>(this.GetParams(o));
     }
     first = first.Concat<ExplorerItem>(from col in o.ColumnsInOrder
         let text = col.PropertyName + " (" + col.ClrType.FormatTypeName() + ")"
         let sqlTypeDec = col.GetFullSqlTypeDeclaration()
         select new ExplorerItem(text, ExplorerItemKind.Property, col.IsKey ? ExplorerIcon.Key : ExplorerIcon.Column) { DragText = col.PropertyName, ToolTipText = (sqlTypeDec == null) ? null : (col.ColumnName + " " + sqlTypeDec), SqlName = col.ColumnName, SqlTypeDeclaration = sqlTypeDec });
     Table t = o as Table;
     if (t != null)
     {
         IOrderedEnumerable<ExplorerItem> second = from a in this.GetParents(t).Concat<ExplorerItem>(this.GetChildren(t))
             orderby a.Icon != ExplorerIcon.OneToOne
             select a;
         first = first.Concat<ExplorerItem>(second);
         if (!(t.HasKey || !isSqlServer))
         {
             first = first.Concat<ExplorerItem>(new ExplorerItem[] { new ExplorerItem("(Warning: No primary key, updates will fail)", ExplorerItemKind.Property, ExplorerIcon.Inherited) });
         }
     }
     return first.ToList<ExplorerItem>();
 }
Exemple #16
0
 private IEnumerable<ExplorerItem> GetParams(SchemaObject o)
 {
     return (from p in o.Parameters select new ExplorerItem(p.ClrName + " (" + p.ClrType.FormatTypeName() + ")", ExplorerItemKind.Parameter, ExplorerIcon.Parameter) { DragText = p.ClrName, SqlName = p.ParamName });
 }
 public FunctionGenerator(EmissionsGenerator egen, TypeBuilder dcType, SchemaObject funcInfo, string methodName, MethodAttributes accessibility)
 {
     Type resultType;
     this.EGen = egen;
     this.DCType = dcType;
     this.FunctionInfo = funcInfo;
     if (funcInfo is TableFunction)
     {
         this.ResultType = egen._tableTypes[funcInfo.DotNetName];
     }
     else
     {
         if (funcInfo.ReturnInfo == null)
         {
             return;
         }
         this.ResultType = funcInfo.ReturnInfo.ClrType;
     }
     if (funcInfo is TableFunction)
     {
         resultType = typeof(IQueryable<>).MakeGenericType(new Type[] { this.ResultType });
     }
     else
     {
         resultType = this.ResultType;
     }
     this.Method = dcType.DefineMethod(methodName, accessibility, resultType, (from p in funcInfo.Parameters select p.ClrType).ToArray<Type>());
 }