コード例 #1
0
ファイル: LazyProxy.cs プロジェクト: marquismark/dbpro-orm
		public LazyProxy(object subject):base(subject.GetType())
		{
            _pool = ConnectionPoolManager.GetPool(subject.GetType());
            _map = _pool.Mapping[subject.GetType()];
            _allowPrimaryChange = _pool.AllowChangingBasicAutogenField;
            if ((_map.PrimaryKeyFields.Length == 1) && (_map.AutoGenProperty != null))
                _mainPrimary = _map[_map.AutoGenProperty][0];
			AttachServer((MarshalByRefObject)subject);
		}
コード例 #2
0
ファイル: Table.cs プロジェクト: marquismark/dbpro-orm
        //recursively sets values 
		private void RecurSetValues(sTable map,Connection conn)
		{
            Type ty = conn.Pool.Mapping[map.Name];
            List<string> extFields = new List<string>(map.ForeignTableProperties);
			foreach (string prop in map.Properties)
			{
                if (!map.ArrayProperties.Contains(prop))
                {
                    PropertyInfo pi = ty.GetProperty(prop, Utility._BINDING_FLAGS);
                    if (pi != null)
                    {
                        if (extFields.Contains(prop) && !Utility.IsEnum(pi.PropertyType))
                        {
                            Table t = (Table)pi.PropertyType.GetConstructor(Type.EmptyTypes).Invoke(new object[0]);
                            t._loadStatus = LoadStatus.Partial;
                            t = (Table)LazyProxy.Instance(t);
                            bool setValue = false;
                            t = SetExternalValues(map, prop, conn, out setValue, t);
                            if (!t.AllPrimaryKeysNull && setValue)
                            {
                                t.InitPrimaryKeys();
                                this.SetField(prop, t);
                            }
                        }
                        else
                        {
                            sTableField fld = map[prop][0];
                            if (conn.ContainsField(fld.Name))
                            {
                                if (conn.IsDBNull(conn.GetOrdinal(fld.Name)))
                                {
                                    try
                                    {
                                        this.SetField(prop, null);
                                    }
                                    catch (Exception e) { }
                                }
                                else
                                {
                                    if (fld.Type == FieldType.ENUM)
                                        this.SetField(prop, conn.Pool.GetEnumValue(pi.PropertyType, (int)conn[fld.Name]));
                                    else
                                        this.SetField(prop, conn[fld.Name]);
                                }
                            }
                        }
                    }
                }
			}
			if (conn.Pool.Mapping.IsMappableType(ty.BaseType))
			{
				RecurSetValues(conn.Pool.Mapping[ty.BaseType],conn);
			}
            this.InitPrimaryKeys();
		}
コード例 #3
0
ファイル: Table.cs プロジェクト: marquismark/dbpro-orm
 //called to set values onto a table that is externally mapped to this current table
 //this is used through lazy loading proxies by only setting the primary key fields.
 private Table SetExternalValues(sTable map,string propertyName, Connection conn, out bool setValue,Table table)
 {
     setValue = false;
     sTable eMap = conn.Pool.Mapping[table.GetType()];
     sTableField[] flds = map[propertyName];
     List<string> fProps = new List<string>(eMap.ForeignTableProperties);
     Type ty = table.GetType().BaseType;
     while (conn.Pool.Mapping.IsMappableType(ty))
     {
         foreach (string str in conn.Pool.Mapping[ty].ForeignTableProperties)
         {
             if (!fProps.Contains(str))
                 fProps.Add(str);
         }
         ty = ty.BaseType;
     }
     foreach (string prop in eMap.PrimaryKeyProperties)
     {
         if (fProps.Contains(prop) && !eMap.IsEnumProperty(prop))
         {
             PropertyInfo pi = table.GetType().GetProperty(prop, Utility._BINDING_FLAGS);
             if (pi == null)
                 pi = table.GetType().GetProperty(prop, Utility._BINDING_FLAGS_WITH_INHERITANCE);
             Table t = (Table)pi.PropertyType.GetConstructor(Type.EmptyTypes).Invoke(new object[0]);
             t._loadStatus = LoadStatus.Partial;
             t = (Table)LazyProxy.Instance(t);
             foreach (sTableField f in eMap[prop])
             {
                 foreach (sTableField fld in flds)
                 {
                     if (fld.ExternalField == f.Name)
                     {
                         if (conn.ContainsField(fld.Name) && !conn.IsDBNull(conn.GetOrdinal(fld.Name)))
                         {
                             RecurSetPropertyValue(f.ExternalField, conn, fld.Name, t);
                         }
                         break;
                     }
                 }
             }
             if (!t.AllPrimaryKeysNull)
             {
                 t.InitPrimaryKeys();
                 table.SetField(prop, t);
                 setValue = true;
             }
         }
         else
         {
             foreach (sTableField f in eMap[prop])
             {
                 foreach (sTableField fld in flds)
                 {
                     if (fld.ExternalField == f.Name)
                     {
                         if (conn.ContainsField(fld.Name)&&!conn.IsDBNull(conn.GetOrdinal(fld.Name))){
                             if (f.Type == FieldType.ENUM)
                                 table.SetField(prop, conn.Pool.GetEnumValue(table.GetType().GetProperty(f.ClassProperty,Utility._BINDING_FLAGS_WITH_INHERITANCE).PropertyType, (int)conn[fld.Name]));
                             else
                                 table.SetField(prop, conn[fld.Name]);
                             setValue = true;
                         }
                         break;
                     }
                 }
             }
         }
     }
     return table;
 }
コード例 #4
0
ファイル: ClassMapping.cs プロジェクト: marquismark/dbpro-orm
        private sTable _ConstructTable(Type tbl,Connection conn, out Dictionary<string, sTable> intermediates)
        {
            List<sTableField> fields = new List<sTableField>(); 
            List<sTableRelation> relations = new List<sTableRelation>();
            List<string> primaryKeyFields = new List<string>();
            intermediates = new Dictionary<string, sTable>();
            string tblName = _pool.Translator.GetTableName(tbl,conn);
            List<PropertyInfo> selfReferenceProperties = new List<PropertyInfo>();
            List<PropertyInfo> arrayProperties = new List<PropertyInfo>();
            List<string> foriegnProperties = new List<string>();
            List<PropertyInfo> versionProperties = new List<PropertyInfo>();
            VersionField.VersionTypes? _versionType = null;
            string autogenField = null;

            foreach (PropertyInfo pi in tbl.GetProperties(Utility._BINDING_FLAGS))
            {
                foreach (object obj in pi.GetCustomAttributes(true))
                {
                    if (obj is Field)
                    {
                        if (((Field)obj).Name == null)
                            ((Field)obj).InitFieldName(pi);
                    }
                    if (obj is IField)
                    {
                        bool isEnum = Utility.IsEnum(pi.PropertyType);
                        if (!isEnum && pi.PropertyType.IsArray)
                            isEnum = Utility.IsEnum(pi.PropertyType.GetElementType());
                        if (isEnum)
                        {
                            if (_pool.Enums[(pi.PropertyType.IsArray ? pi.PropertyType.GetElementType() : pi.PropertyType)] == null)
                            {
                                Type etype = (pi.PropertyType.IsArray ? pi.PropertyType.GetElementType() : pi.PropertyType);
                                string eName = _pool.Translator.GetTableName(etype, conn);
                                sTable etbl = new sTable(eName,
                                    new sTableField[]{
                                        new sTableField(_pool.Translator.GetEnumIDFieldName(etype,conn),null,null,FieldType.INTEGER,4,false,null),
                                        new sTableField(_pool.Translator.GetEnumValueFieldName(etype,conn),null,null,FieldType.STRING,500,false,null)
                                    },
                                    null,
                                    new sTableRelation[0],
                                    new string[] { _pool.Translator.GetEnumIDFieldName(etype, conn) },
                                    _pool.Translator.GetEnumIDFieldName(etype, conn));
                                _classMaps.Add(etype, etbl);
                                _pool.Enums.Add(etype, etbl.Name);
                            }
                        }
                        if (!pi.PropertyType.IsArray
                            || pi.PropertyType.Equals(typeof(byte[])))
                        {
                            Logger.LogLine("Adding Field (" + pi.Name + ")");
                            fields.Add(new sTableField(_pool.Translator.GetFieldName(tbl,pi,conn), pi.Name, (Utility.IsEnum(pi.PropertyType) ? "ID" : null),((IField)obj).Type,((IField)obj).Length,((IField)obj).Nullable,(obj is ComputedField ? ((ComputedField)obj).Code : null)));
                            if (obj is IPrimaryKeyField)
                            {
                                primaryKeyFields.Add(fields[fields.Count - 1].Name);
                                if (((IPrimaryKeyField)obj).AutoGen)
                                    autogenField = fields[fields.Count - 1].Name;
                            }
                            if (Utility.IsEnum(pi.PropertyType))
                                relations.Add(new sTableRelation(_pool.Enums[pi.PropertyType], pi.Name, ForeignField.UpdateDeleteAction.CASCADE, ForeignField.UpdateDeleteAction.CASCADE,((IField)obj).Nullable));
                        }
                        else
                            arrayProperties.Add(pi);
                    }
                    else if (obj is IForeignField)
                    {
                        foriegnProperties.Add(pi.Name);
                        if (pi.PropertyType.IsArray)
                            arrayProperties.Add(pi);
                        else
                        {
                            Logger.LogLine("Adding Foreign Field (" + pi.Name + ")");
                            Type ty = Utility.LocateType(pi.ToString().Substring(0, pi.ToString().IndexOf(" ")).Replace("[]", ""));
                            if (!ty.Equals(tbl))
                            {
                                if (!_classMaps.ContainsKey(ty))
                                {
                                    Dictionary<string, sTable> imediates = new Dictionary<string, sTable>();
                                    _classMaps.Add(ty, _ConstructTable(ty,conn, out imediates));
                                    if (imediates.Count > 0)
                                        _intermediateTables.Add(ty, imediates);
                                }
                                sTable ext = _classMaps[ty];
                                foreach (string prop in ext.PrimaryKeyProperties)
                                {
                                    foreach (sTableField fld in ext[prop])
                                    {
                                        fields.Add(new sTableField(_pool.Translator.GetFieldName(tbl,pi,fld.Name,conn),pi.Name,fld.Name,fld.Type,fld.Length,fld.Nullable,fld.ComputedCode));
                                        if (obj is IPrimaryKeyField)
                                            primaryKeyFields.Add(fields[fields.Count - 1].Name);
                                    }
                                }
                                relations.Add(new sTableRelation(ext.Name, pi.Name, ((IForeignField)obj).OnDelete, ((IForeignField)obj).OnUpdate,((IForeignField)obj).Nullable));
                            }
                            else
                                selfReferenceProperties.Add(pi);
                        }
                    }
                    if (obj is IVersionField)
                    {
                        if (!_versionType.HasValue)
                            _versionType = ((IVersionField)obj).VersionType;
                        else if (_versionType.Value != ((IVersionField)obj).VersionType)
                            throw new Exception("Cannot use two different version  types in the same table.");
                        versionProperties.Add(pi);
                    }
                }
            }

            if (!tbl.BaseType.Equals(typeof(Org.Reddragonit.Dbpro.Structure.Table)))
            {
                if (tbl.BaseType.IsSubclassOf(typeof(Org.Reddragonit.Dbpro.Structure.Table)))
                {
                    if (!_classMaps.ContainsKey(tbl.BaseType))
                    {
                        Dictionary<string, sTable> imediates = new Dictionary<string, sTable>();
                        _classMaps.Add(tbl.BaseType, _ConstructTable(tbl.BaseType, conn, out imediates));
                        if (imediates.Count > 0)
                            _intermediateTables.Add(tbl.BaseType, imediates);
                    }
                    sTable pTbl = _classMaps[tbl.BaseType];
                    List<string> pkeys = new List<string>(pTbl.PrimaryKeyFields);
                    foreach (sTableField fld in pTbl.Fields)
                    {
                        if (pkeys.Contains(fld.Name))
                        {
                            fields.Add(fld);
                            primaryKeyFields.Add(fld.Name);
                        }
                    }
                }
            }

            foreach (PropertyInfo pi in selfReferenceProperties)
            {
                foreach (string str in primaryKeyFields)
                {
                    int cnt = fields.Count;
                    for(int x=0;x<cnt;x++)
                    {
                        sTableField fld = fields[x];
                        if (fld.Name==str)
                            fields.Add(new sTableField(_pool.Translator.GetFieldName(tbl,pi,fld.Name,conn), pi.Name, str,fld.Type,fld.Length,fld.Nullable,fld.ComputedCode));
                    }
                }
                foreach (object obj in pi.GetCustomAttributes(true))
                {
                    if (obj is IForeignField)
                    {
                        relations.Add(new sTableRelation(tblName, pi.Name, ((IForeignField)obj).OnDelete, ((IForeignField)obj).OnUpdate,((IForeignField)obj).Nullable));
                        break;
                    }
                }
            }

            if (arrayProperties.Count > 0)
            {
                foreach (PropertyInfo pi in arrayProperties)
                {
                    string itblName = _pool.Translator.GetIntermediateTableName(tbl, pi, conn);
                    List<sTableField> afields = new List<sTableField>();
                    foreach (sTableField sf in fields)
                    {
                        if (primaryKeyFields.Contains(sf.Name))
                            afields.Add(new sTableField(_pool.Translator.GetIntermediateFieldName(tbl,pi,sf.Name,true,conn),"PARENT", sf.Name, sf.Type, sf.Length, sf.Nullable,sf.ComputedCode));
                    }
                    afields.Add(new sTableField(_pool.Translator.GetIntermediateIndexFieldName(tbl,pi), null, null, FieldType.INTEGER, 4, false,null));
                    List<string> apKeys = new List<string>();
                    for (int x = 0; x <= afields.Count - 1; x++)
                        apKeys.Add(afields[x].Name);
                    if (!foriegnProperties.Contains(pi.Name))
                    {
                        IField fld = null;
                        foreach (object obj in pi.GetCustomAttributes(false))
                        {
                            if (obj is IField)
                            {
                                fld = (IField)obj;
                                if (fld is Field && fld.Name == null)
                                    ((Field)fld).InitFieldName(pi);
                                break;
                            }
                        }
                        if (Utility.IsEnum(pi.PropertyType.GetElementType()))
                        {
                            afields.Add(new sTableField(_pool.Translator.GetIntermediateValueFieldName(tbl,pi), "CHILD", _classMaps[pi.PropertyType.GetElementType()].Fields[0].Name, fld.Type, fld.Length, fld.Nullable,null));
                            intermediates.Add(pi.Name, new sTable(itblName, afields.ToArray(),null,
                                new sTableRelation[] { 
                                    new sTableRelation(tbl.Name,"PARENT",ForeignField.UpdateDeleteAction.CASCADE,ForeignField.UpdateDeleteAction.CASCADE,false),
                                    new sTableRelation(_pool.Enums[pi.PropertyType.GetElementType()], "CHILD", ForeignField.UpdateDeleteAction.CASCADE, ForeignField.UpdateDeleteAction.CASCADE, fld.Nullable) 
                                }, apKeys.ToArray(), afields[afields.Count - 2].Name));
                        }
                        else
                        {
                            afields.Add(new sTableField(_pool.Translator.GetIntermediateValueFieldName(tbl, pi), null, null, fld.Type, fld.Length, fld.Nullable,(fld is ComputedField ? ((ComputedField)fld).Code : null)));
                            intermediates.Add(pi.Name, new sTable(itblName, afields.ToArray(),null, null, apKeys.ToArray(), afields[afields.Count - 2].Name));
                        }
                        afields.RemoveAt(afields.Count - 1);
                    }
                    else
                    {
                        IForeignField iff = null;
                        foreach (object obj in pi.GetCustomAttributes(false))
                        {
                            if (obj is IForeignField)
                            {
                                iff = (IForeignField)obj;
                                break;
                            }
                        }
                        List<sTableField> extFields = new List<sTableField>();
                        string extTableName = "";
                        if (pi.PropertyType.GetElementType() == tbl)
                        {
                            extTableName = tblName;
                            extFields.AddRange(afields);
                            foreach(string str in primaryKeyFields){
                                foreach(sTableField f in fields){
                                    if (f.Name == str)
                                    {
                                        extFields.Add(new sTableField(_pool.Translator.GetIntermediateFieldName(tbl, pi, f.Name, false, conn), "CHILD", f.Name, f.Type, f.Length, f.Nullable,f.ComputedCode));
                                        break;
                                    }
                                }
                            }
                        }
                        else
                        {
                            if (!_classMaps.ContainsKey(pi.PropertyType.GetElementType()))
                            {
                                Dictionary<string, sTable> imediates = new Dictionary<string, sTable>();
                                _classMaps.Add(pi.PropertyType.GetElementType(), _ConstructTable(pi.PropertyType.GetElementType(), conn, out imediates));
                                if (imediates.Count > 0)
                                    _intermediateTables.Add(pi.PropertyType.GetElementType(), imediates);
                            }
                            sTable sExt = _classMaps[pi.PropertyType.GetElementType()];
                            extTableName = sExt.Name;
                            extFields.AddRange(afields);
                            foreach (string str in sExt.PrimaryKeyProperties)
                            {
                                foreach (sTableField f in sExt[str])
                                    extFields.Add(new sTableField(_pool.Translator.GetIntermediateFieldName(tbl, pi, f.Name, false, conn), "CHILD", f.Name, f.Type, f.Length, f.Nullable,f.ComputedCode));
                            }
                        }
                        intermediates.Add(pi.Name, new sTable(itblName, extFields.ToArray(), null,
                                new sTableRelation[]{
                                new sTableRelation(tblName,"PARENT",iff.OnUpdate,iff.OnDelete,iff.Nullable),
                                new sTableRelation(extTableName,"CHILD",iff.OnUpdate,iff.OnDelete,iff.Nullable)
                            },
                                apKeys.ToArray(), afields[afields.Count - 1].Name));
                    }
                }
            }

            if (_versionType.HasValue)
            {
                string vtblName = _pool.Translator.GetVersionTableName(tbl, conn);
                _versionTypes.Add(tbl, _versionType.Value);
                List<sTableField> vfields = new List<sTableField>();
                List<string> vpkeys = new List<string>();
                switch (_versionType.Value)
                {
                    case VersionField.VersionTypes.NUMBER:
                        vfields.Add(new sTableField(_pool.Translator.GetVersionFieldIDName(tbl, conn), null, null, FieldType.INTEGER, 4, false,null));
                        break;
                    case VersionField.VersionTypes.DATESTAMP:
                        vfields.Add(new sTableField(_pool.Translator.GetVersionFieldIDName(tbl, conn), null, null, FieldType.DATETIME, 12, false,null));
                        break;
                }
                vpkeys.Add(vfields[vfields.Count - 1].Name);
                foreach (PropertyInfo pi in versionProperties)
                {
                    foreach (sTableField stf in fields)
                    {
                        if (!primaryKeyFields.Contains(stf.Name))
                        {
                            if (Utility.StringsEqual(stf.ClassProperty, pi.Name))
                                vfields.Add(new sTableField(stf.Name,null,null,stf.Type,stf.Length,stf.Nullable,stf.ComputedCode));
                        }
                    }
                }
                vpkeys.AddRange(primaryKeyFields);
                foreach (sTableField f in fields)
                {
                    if (vpkeys.Contains(f.Name))
                        vfields.Add(new sTableField(f.Name,"PARENT",f.Name,f.Type,f.Length,f.Nullable,f.ComputedCode));
                }
                _versionMaps.Add(tbl, new sTable(vtblName, vfields.ToArray(),null,new sTableRelation[]{new sTableRelation(tblName,"PARENT",ForeignField.UpdateDeleteAction.CASCADE,ForeignField.UpdateDeleteAction.CASCADE,false)}, vpkeys.ToArray(), vfields[vfields.Count - 1].Name));
            }

            return new sTable(tblName, fields.ToArray(),arrayProperties, relations.ToArray(), primaryKeyFields.ToArray(), autogenField);
        }
コード例 #5
0
ファイル: QueryBuilder.cs プロジェクト: marquismark/dbpro-orm
 protected abstract string _GenerateAutogenIDQuery(sTable tbl,ref List<IDbDataParameter> parameters);
コード例 #6
0
ファイル: QueryBuilder.cs プロジェクト: marquismark/dbpro-orm
 internal virtual string SelectPaged(string baseQuery,sTable mainMap, ref List<IDbDataParameter> queryParameters, ulong? start, ulong? recordCount, string[] OrderByFields)
 {
     if (!start.HasValue)
         start = 0;
     if (!recordCount.HasValue)
         recordCount = 0;
     queryParameters.Add(pool.CreateParameter(CreateParameterName("startIndex"), (long)start.Value));
     queryParameters.Add(pool.CreateParameter(CreateParameterName("rowCount"), (long)recordCount.Value));
     return String.Format(SelectWithPagingIncludeOffset, baseQuery, CreateParameterName("startIndex"), CreateParameterName("rowCount"));
 }
コード例 #7
0
ファイル: QueryBuilder.cs プロジェクト: marquismark/dbpro-orm
 internal string SelectPaged(string baseQuery, sTable mainMap, ref List<IDbDataParameter> queryParameters, ulong? start, ulong? recordCount)
 {
     return SelectPaged(baseQuery, mainMap, ref queryParameters, start, recordCount, null);
 }
コード例 #8
0
ファイル: QueryBuilder.cs プロジェクト: marquismark/dbpro-orm
		private string GetSubqueryTable(sTable tbl,Type type,ref int count)
		{
            if (!_pool.Mapping.IsMappableType(type.BaseType))
                return tbl.Name;
            sTable parentTable = _pool.Mapping[type.BaseType];
			string fields = "";
            int origCount = count;
            string tables = tbl.Name + " table_" + origCount.ToString() + ", ";
            count++;
            tables += GetSubqueryTable(parentTable, type.BaseType, ref count);
            tables+=" table_" + ((int)(count + 1)).ToString();
			string where ="";
            foreach (string prop in tbl.Properties)
            {
                foreach (sTableField fld in tbl[prop])
                    fields += "table_" + origCount.ToString() + "." + fld.Name + ",";
            }
            foreach (string key in parentTable.PrimaryKeyFields)
            {
                if (!fields.Contains("table_" + origCount.ToString() + "." + key + ","))
                    fields += "table_" + origCount.ToString() + "." + key + ",";
                where += " table_" + origCount.ToString() + "." + key + " = table_" + (count + 1).ToString() + "." + key + " AND";
            }
			count++;
            Type btype = type.BaseType;
            while (_pool.Mapping.IsMappableType(btype))
            {
                sTable t = _pool.Mapping[btype];
                List<string> pProps = new List<string>(t.PrimaryKeyProperties);
                foreach (string prop in t.Properties)
                {
                    if (!pProps.Contains(prop))
                    {
                        foreach (sTableField fld in t[prop])
                        {
                            if (!fields.Contains("table_" + count.ToString() + "." + fld.Name + ","))
                                fields += "table_" + count.ToString() + "." + fld.Name + ",";
                        }
                    }
                }
                btype = btype.BaseType;
            }
			fields=fields.Substring(0,fields.Length-1);
			if (where.EndsWith(" AND"))
				where = where.Substring(0,where.Length-4);
			return String.Format("("+SelectWithConditions+")",fields,tables,where);
		}
コード例 #9
0
 protected override string _GenerateAutogenIDQuery(sTable tbl, ref List<IDbDataParameter> parameters)
 {
     parameters[parameters.Count - 1].Direction = ParameterDirection.Output;
     string ret = "; SET "+CreateParameterName(tbl.AutoGenField)+" = (SELECT MAX(" + tbl.AutoGenField + ") FROM " + tbl.Name;
     if (tbl.PrimaryKeyFields.Length > 1)
     {
         ret += " WHERE ";
         foreach (string prop in tbl.PrimaryKeyProperties)
         {
             foreach (sTableField fld in tbl[prop])
             {
                 if (!Utility.StringsEqual(fld.Name,tbl.AutoGenField))
                     ret += fld.Name + " = " + CreateParameterName(fld.Name) + " AND ";
             }
         }
         if (ret.EndsWith("WHERE "))
             ret = ret.Substring(0, ret.Length - 6);
         return (ret.EndsWith("AND ") ? ret.Substring(0, ret.Length - 4) : ret)+")";
     }
     return ret+")";
 }
コード例 #10
0
 protected override string _GenerateAutogenIDQuery(sTable tbl, ref List<IDbDataParameter> parameters)
 {
     parameters[parameters.Count - 1].Direction = ParameterDirection.ReturnValue;
     return "RETURNING " + tbl.AutoGenField;
 }
コード例 #11
0
 protected override string _GenerateAutogenIDQuery(sTable tbl, ref List<IDbDataParameter> parameters)
 {
     parameters[parameters.Count - 1].Direction = ParameterDirection.Output;
     if (((MsSqlConnectionPool)pool).Version >= 2005 && tbl.PrimaryKeyFields.Length==1
         && tbl[tbl.AutoGenProperty][0].Type != FieldType.DATETIME
         && tbl[tbl.AutoGenProperty][0].Type != FieldType.TIME
         && tbl[tbl.AutoGenProperty][0].Type != FieldType.STRING)
         return "OUTPUT INSERTED." + tbl.AutoGenField;
     else
     {
         string select = "; SET "+CreateParameterName(tbl.AutoGenField)+" = (SELECT MAX("+tbl.AutoGenField+") FROM "+tbl.Name;
         if (tbl.PrimaryKeyFields.Length > 1)
         {
             select+=" WHERE ";
             foreach (string prop in tbl.PrimaryKeyProperties)
             {
                 foreach (sTableField fld in tbl[prop]){
                     if (!Utility.StringsEqual(tbl.AutoGenField, fld.Name))
                         select += fld.Name + " = " + CreateParameterName(fld.Name) + " AND ";
                 }
             }
             select = select.Substring(0, select.Length - 4);
             if (select.EndsWith("WHERE "))
                 select = select.Substring(0, select.Length - 6);
         }
         return select+")";
     }
 }
コード例 #12
0
 private bool _IsTracedProperty(sTable tm, string fieldName, sTableField fld, string property)
 {
     sTable map = _pool.Mapping[_pool.Mapping[tm.GetRelationForProperty(property).Value.ExternalTable]];
     if (fieldName.Contains("."))
     {
         foreach (sTableField stf in map[fieldName.Substring(0, fieldName.IndexOf("."))])
         {
             if (stf.Name == fld.ExternalField)
                 return _IsTracedProperty(map, fieldName.Substring(fieldName.IndexOf(".") + 1), stf, fieldName.Substring(0, fieldName.IndexOf(".")));
         }
     }
     else
     {
         foreach (sTableField stf in map[fieldName])
         {
             if (stf.Name == fld.ExternalField)
                 return true;
         }
     }
     return false;
 }
コード例 #13
0
 private List<Type> _RecurLoadChildrenForTypeForMSConnection(Type t,sTable tbl, List<Type> types)
 {
     foreach (Type st in _pool.Mapping.Types)
     {
         if (st != t)
         {
             sTable stbl = _pool.Mapping[st];
             foreach (string str in stbl.ForeignTableProperties)
             {
                 sTableRelation strel = stbl.GetRelationForProperty(str).Value;
                 if (strel.ExternalTable == tbl.Name && !types.Contains(st))
                 {
                     types.Add(st);
                     types = _RecurLoadChildrenForTypeForMSConnection(st, stbl, types);
                     break;
                 }
             }
         }
     }
     return types;
 }
コード例 #14
0
 private List<Type> _RecurLoadTypesForTable(sTable tbl, List<Type> types)
 {
     Type t = _pool.Mapping[tbl.Name];
     if (!t.BaseType.Equals(typeof(Org.Reddragonit.Dbpro.Structure.Table)) && _pool.Mapping.IsMappableType(t.BaseType))
         types = _RecurLoadTypesForTable(_pool.Mapping[t.BaseType], types);
     if (!types.Contains(t))
         types.Add(t);
     foreach (string prop in tbl.ForeignTableProperties)
     {
         sTableRelation rel = tbl.GetRelationForProperty(prop).Value;
         if (rel.ExternalTable != tbl.Name)
             types = _RecurLoadTypesForTable(_pool.Mapping[_pool.Mapping[rel.ExternalTable]], types);
     }
     foreach (string prop in tbl.Properties)
     {
         if (_pool.Mapping.PropertyHasIntermediateTable(t, prop))
         {
             sTable itbl = _pool.Mapping[t, prop];
             if (itbl.Relations!=null)
             {
                 if (itbl.Relations[1].ExternalTable != tbl.Name)
                     types = _RecurLoadTypesForTable(_pool.Mapping[_pool.Mapping[itbl.Relations[1].ExternalTable]], types);
             }
         }
     }
     foreach (Type tp in Utility.LocateAllTypesWithAttribute(typeof(ClassViewAttribute)))
     {
         if (!types.Contains(tp))
         {
             ClassViewAttribute cva = _pool[tp];
             foreach (Type tpe in cva.Query.RequiredTypes)
             {
                 if (types.Contains(tpe))
                 {
                     if (!types.Contains(tp))
                         types.Add(tp);
                     foreach (Type type in cva.Query.RequiredTypes)
                     {
                         if (!types.Contains(type))
                             types.Add(type);
                         types = _RecurLoadTypesForTable(_pool.Mapping[type], types);
                     }
                     break;
                 }
             }
         }
     }
     if (_pool is MsSqlConnectionPool)
         types = _RecurLoadChildrenForTypeForMSConnection(t,tbl, types);
     return types;
 }
コード例 #15
0
 private static void recurAddRelatedTypes(ref List<Type> basicTypes, ref List<Type> complexTypes,sTable map,Type type,ConnectionPool pool)
 {
     if (complexTypes.Contains(type))
         return;
     foreach (string str in map.ForeignTableProperties)
     {
         PropertyInfo pi = type.GetProperty(str, Utility._BINDING_FLAGS);
         Type t = (pi.PropertyType.IsArray ? pi.PropertyType.GetElementType() : pi.PropertyType);
         if (!t.Equals(type))
         {
             sTable tm = pool.Mapping[t];
             if (tm.ForeignTableProperties.Length > 0)
                 recurAddRelatedTypes(ref basicTypes, ref complexTypes, tm, t,pool);
             else
             {
                 if (!basicTypes.Contains(t))
                     basicTypes.Add(t);
             }
         }
     }
     if (!complexTypes.Contains(type))
     {
         complexTypes.Add(type);
     }
 }
コード例 #16
0
ファイル: Connection.cs プロジェクト: marquismark/dbpro-orm
		private void InsertArrayValue(Table table,PropertyInfo pi,int originalLength,List<int> changedIndexes,sTable map,Array values,string prop,bool ignoreAutogen)
		{
			List<IDbDataParameter> pars = new List<IDbDataParameter>();
			string query="";
			string fields="";
			string paramString="";
			string conditions = "";
			pars = new List<IDbDataParameter>();
            sTable arMap = pool.Mapping[table.GetType(), prop];
            changedIndexes = (changedIndexes == null ? new List<int>() : changedIndexes);
            string indexName = Pool.Translator.GetIntermediateIndexFieldName(table.GetType(), pi);
            string valueName = Pool.Translator.GetIntermediateValueFieldName(table.GetType(), pi);
            foreach (string pk in map.PrimaryKeyProperties)
            {
                foreach (sTableField fld in map[pk])
                {
                    foreach (sTableField f in arMap.Fields)
                    {
                        if (f.ExternalField == fld.Name)
                        {
                            fields += f.Name + ", ";
                            paramString += queryBuilder.CreateParameterName(f.Name) + ", ";
                            conditions += f.Name + " = " + queryBuilder.CreateParameterName(f.Name) + " AND ";
                            pars.Add(CreateParameter(queryBuilder.CreateParameterName(f.Name), QueryBuilder.LocateFieldValue(table, fld, pool)));
                            break;
                        }
                    }
                }
            }
            conditions = conditions.Substring(0, conditions.Length - 4);
            if (originalLength > (values == null ? 0 : values.Length))
            {
                pars.Add(CreateParameter(queryBuilder.CreateParameterName(indexName), values.Length));
                query = queryBuilder.Delete(arMap.Name, conditions+" AND "+indexName + " > " + queryBuilder.CreateParameterName(indexName));
                ExecuteNonQuery(query, pars);
            }
			if (values!=null)
			{
                if (changedIndexes.Count>0)
                {
                    pars.Add(CreateParameter(queryBuilder.CreateParameterName("VALUE"), null));
                    pars.Add(CreateParameter(queryBuilder.CreateParameterName(indexName), null));
                    foreach (int index in changedIndexes)
                    {
                        if (index<values.Length){
                            pars[pars.Count - 2] = CreateParameter(queryBuilder.CreateParameterName("VALUE"), values.GetValue(index));
                            pars[pars.Count - 1] = CreateParameter(queryBuilder.CreateParameterName(indexName), index);
                            query = queryBuilder.Update(arMap.Name, valueName + "=" + queryBuilder.CreateParameterName("VALUE") + "," + indexName + "=" + queryBuilder.CreateParameterName(indexName),
                                conditions + " AND " + indexName + " = " + queryBuilder.CreateParameterName(indexName));
                            ExecuteQuery(query, pars);
                        }
                    }
                }
				pars.Add(CreateParameter(queryBuilder.CreateParameterName("VALUE"),null));
                if (ignoreAutogen)
                    pars.Add(CreateParameter(CreateParameterName("index_id"), null));
                fields += valueName + (ignoreAutogen ? ", " + indexName : "");
				paramString+=CreateParameterName("VALUE")+(ignoreAutogen ? ", "+CreateParameterName("index_id") : "");
				query = queryBuilder.Insert(arMap.Name,fields,paramString);
                for(int index = originalLength;index<values.Length;index++){
					pars.RemoveAt(pars.Count-1);
                    if(ignoreAutogen)
                        pars.RemoveAt(pars.Count - 1);
					pars.Add(CreateParameter(queryBuilder.CreateParameterName("VALUE"),values.GetValue(index)));
                    if (ignoreAutogen)
                        pars.Add(pool.CreateParameter(CreateParameterName("index_id"), index, FieldType.LONG, 8));
					ExecuteNonQuery(query,pars);
				}
			}
		}