示例#1
0
        public static SQLPropertyInfo FromPropertyInfo(PropertyInfo pi, SQLTypeInfo TypeInfo)
        {
            //If it's SQL ignored, just return null
            if (pi.GetFirstAttribute <SQLIgnoredFieldAttribute>() != null)
            {
                return(null);
            }

            SQLPropertyInfo myResult = new SQLPropertyInfo(pi, TypeInfo);

            //Mark as read only now, if applicable
            myResult.ReadOnly = (pi.GetFirstAttribute <SQLReadOnlyFieldAttribute>() != null);

            myResult.KeyField = pi.GetFirstAttribute <SQLKeyFieldAttribute>();

            //Set the default mappings for standard properties
            SQLFieldAttribute FieldAttribute = pi.GetFirstAttribute <SQLFieldAttribute>();

            //Set the primary field name based on the field attribute if specified, or the property name itself if not
            myResult.DatabaseFieldName = (FieldAttribute == null ? myResult.PropertyInfo.Name : FieldAttribute.FieldName);

            //If the database field is a function of any kind it has to be read-only
            if (myResult.DatabaseFieldName.Contains('('))
            {
                myResult.ReadOnly = true;
            }
            else
            {
                //It should be an updatable property (unless flagged as read only by the user);
                //The update from property is always this property's name
                myResult.UpdateFromProperty = pi.Name;
            }

            return(myResult);
        }
示例#2
0
        public List <T> GetObjectCollection <T>(string Query, List <SqlParameter> Parameters = null) where T : new()
        {
            this.LoadStackCounter++;

            List <T> myResult = new List <T>();

            //Get a copy of the property info for this type for its SELECT string and
            //to pass into ObjectFromDataRow row to save repeated lookups in the future
            SQLTypeInfo sqlTypeInfo = SQLTypeInfo.FromType(typeof(T));

            DataTable DTResult = QueryDT(Query, Parameters);

            foreach (DataRow R in DTResult.Rows)
            {
                myResult.Add(ObjectFromDataRow <T>(R, sqlTypeInfo));
            }

            this.LoadStackCounter--;

            //If this is the end of the loading stack, clear the loading objects dictionary
            if (LoadStackCounter == 0)
            {
                LoadingObjects.Clear();
            }

            return(myResult);
        }
示例#3
0
        //Private constructor so this class can't be created by outside object
        private SQLPropertyInfo(PropertyInfo pi, SQLTypeInfo TypeInfo)
        {
            this.PropertyInfo = pi;
            this.TypeInfo     = TypeInfo;

            InitializeGet();
            InitializeSet();
        }
示例#4
0
        public List <T> GetObjectCollection <T>(int?Top = null, string WhereString = null, string GroupByString = null, string OrderByString = null, List <SqlParameter> Parameters = null) where T : new()
        {
            //Get a copy of the property info for this type for its select string and
            //to pass into ObjectFromDataRow to save repeated lookups in the future
            SQLTypeInfo sqlTypeInfo = SQLTypeInfo.FromType(typeof(T));

            string query = sqlTypeInfo.SelectQuery(Top, WhereString, GroupByString, OrderByString);

            return(GetObjectCollection <T>(query, Parameters));
        }
示例#5
0
        public T GetSingleObjectByID <T>(object ID) where T : new()
        {
            SQLTypeInfo sqlTypeInfo = SQLTypeInfo.FromType(typeof(T));

            if (sqlTypeInfo.KeyFields.Count != 1)
            {
                throw new Exception("GetSingleObjectByID can only be used on objects with 1 Key Field defined!");
            }

            return(GetSingleObject <T>(new FieldValue(sqlTypeInfo.KeyFields[0].DatabaseFieldName, ID)));
        }
示例#6
0
        public static SQLTypeInfo FromType(Type T)
        {
            SQLTypeInfo MyResult = null;

            if (CachedSQLTypeInfos.TryGetValue(T, out MyResult))
            {
                return(MyResult);
            }
            else
            {
                return(GetSQLInfo(T));
            }
        }
示例#7
0
        private static void BuildSelectSQL(SQLTypeInfo S)
        {
            //Get the property list
            List <SQLPropertyInfo> selectProperties = S.Properties.ToList();

            List <string> selectFields = new List <string>();

            foreach (SQLPropertyInfo property in selectProperties)
            {
                string selectFieldName;     //Final select field value to put into output statement
                bool   useAlias = false;    //Whether the field has to be aliased as the property name or not

                //If the field is a function or has a table alias, don't modify the field, but do use an alias
                if (property.DatabaseFieldName.Contains('(') | property.DatabaseFieldName.Contains('.'))
                {
                    selectFieldName = property.DatabaseFieldName;
                    useAlias        = true;
                }
                else
                {
                    //Wrap the field in brackets for safety
                    selectFieldName = "[" + property.DatabaseFieldName + "]";

                    //If the field name doesn't match the property name, set it to alias as the property name
                    if (property.DatabaseFieldName.ToUpper() != property.PropertyInfo.Name.ToUpper())
                    {
                        useAlias = true;
                    }
                }

                //If it's supposed to be aliased at this point, append it here
                if (useAlias)
                {
                    selectFieldName += " AS '" + property.PropertyInfo.Name + "'";
                }

                //Add it to the final select statement
                selectFields.Add(selectFieldName);
            }

            StringBuilder myResult = new StringBuilder("SELECT {TOP}");

            myResult.Append(string.Join(", ", selectFields.Distinct()));
            myResult.Append(" FROM " + S.TableName);

            S.BaseSelectQuery = myResult.ToString();
        }
示例#8
0
        public T GetSingleObject <T>(string WhereString = null, string GroupByString = null, string OrderByString = null, List <SqlParameter> Parameters = null) where T : new()
        {
            this.LoadStackCounter++;

            T      obj   = new T();
            string query = SQLTypeInfo.FromType(typeof(T)).SelectQuery(1, WhereString, GroupByString, OrderByString);

            DataTable DT = new DataTable();

            DT = QueryDT(query, Parameters);

            if (DT.Rows.Count > 0)
            {
                obj = ObjectFromDataRow <T>(DT.Rows[0]);
                this.LoadStackCounter--;
                return(obj);
            }
            else
            {
                this.LoadStackCounter--;
                return(default(T));
            }
        }
示例#9
0
        public T ObjectFromDataRow <T>(DataRow R, SQLTypeInfo TypeInfo = null) where T : new()
        {
            //Create an output variable of the specified type to return
            T myResult = new T();

            //If the type info wasn't passed into the function, get it now
            if (TypeInfo == null)
            {
                TypeInfo = SQLTypeInfo.FromType(typeof(T));
            }

            //Loop through each SQL property on the object
            foreach (SQLPropertyInfo sqlProperty in TypeInfo.Properties)
            {
                object RecordValue = R[sqlProperty.PropertyInfo.Name];

                if (RecordValue != DBNull.Value)
                {
                    sqlProperty.Set(myResult, RecordValue);
                }
            }

            return(myResult);
        }
示例#10
0
        private static SQLTypeInfo GetSQLInfo(Type T, string Prefix = "")
        {
            SQLTypeInfo myResult = new SQLTypeInfo();

            //Add this item to the cached type info now that the object exists and we know the type
            CachedSQLTypeInfos.Add(T, myResult);

            //Get the table name to pull the class data from
            SQLTableAttribute TableAttribute = T.GetFirstAttribute <SQLTableAttribute>();

            //If no table attribute is specified, default to the class name...
            if (TableAttribute == null)
            {
                myResult.TableName = T.Name;
            }
            else
            {
                myResult.TableName = TableAttribute.TableName;
            }

            //Load the sql information for each property of the class type that's a key field
            foreach (PropertyInfo pi in T.GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic).Where(x => x.GetFirstAttribute <SQLKeyFieldAttribute>() != null))
            {
                SQLPropertyInfo sqlPI = SQLPropertyInfo.FromPropertyInfo(pi, myResult);

                if (sqlPI != null)
                {
                    myResult.Properties.Add(sqlPI);
                }
            }

            myResult.KeyFields = (from x in myResult.Properties where x.KeyField != null select x).ToList();

            if (myResult.KeyFields.Count == 0)
            {
                myResult.KeyType = SQLKeyType.None;
            }
            else if (myResult.KeyFields.Count == 1)
            {
                if (myResult.KeyFields[0].KeyField.AutoNumber)
                {
                    myResult.KeyType = SQLKeyType.AutoNumber;
                }
                else
                {
                    myResult.KeyType = SQLKeyType.Single;
                }
            }
            else
            {
                myResult.KeyType = SQLKeyType.Multiple;
            }

            //Load the sql information for each property of the class type that's not a key field
            foreach (PropertyInfo pi in T.GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic).Where(x => x.GetFirstAttribute <SQLKeyFieldAttribute>() == null))
            {
                SQLPropertyInfo sqlPI = SQLPropertyInfo.FromPropertyInfo(pi, myResult);

                //Skip SQLIgnored Properties
                if (sqlPI != null)
                {
                    myResult.Properties.Add(sqlPI);
                }
            }

            BuildSelectSQL(myResult);

            return(myResult);
        }