예제 #1
0
        private static void loadRelations(Type primaryType)
        {
            if (relations.ContainsKey(primaryType))
            {
                return;
            }

            List <DBRelation> newRelations = new List <DBRelation>();

            foreach (PropertyInfo currProperty in primaryType.GetProperties())
            {
                foreach (object currAttr in currProperty.GetCustomAttributes(true))
                {
                    // if we have come to a relation property, lets process it
                    if (currAttr.GetType() == typeof(DBRelationAttribute))
                    {
                        DBRelation newRelation = new DBRelation();
                        newRelation._primaryType          = primaryType;
                        newRelation._secondaryType        = currProperty.PropertyType.GetGenericArguments()[1];
                        newRelation._identifier           = ((DBRelationAttribute)currAttr).Identifier;
                        newRelation._propertyInfo         = currProperty;
                        newRelation._autoRetrieve         = ((DBRelationAttribute)currAttr).AutoRetrieve;
                        newRelation._filterable           = ((DBRelationAttribute)currAttr).Filterable;
                        newRelation.getRelationListMethod = currProperty.GetGetMethod();

                        newRelations.Add(newRelation);
                    }
                }
            }

            relations[primaryType] = newRelations;
        }
예제 #2
0
        public HashSet <string> GetAllValues <T>(DBField field, DBRelation relation, ICollection <T> items) where T : DatabaseTable
        {
            // loop through all items in the DB and grab all existing values for this field
            HashSet <string> uniqueStrings = new HashSet <string>(StringComparer.CurrentCultureIgnoreCase);

            foreach (T currItem in items)
            {
                if (relation == null)
                {
                    List <string> values = getValues(field.GetValue(currItem));
                    foreach (string currStr in values)
                    {
                        uniqueStrings.Add(currStr);
                    }
                }
                else
                {
                    foreach (DatabaseTable currSubItem in relation.GetRelationList(currItem))
                    {
                        List <string> values = getValues(field.GetValue(currSubItem));
                        foreach (string currStr in values)
                        {
                            uniqueStrings.Add(currStr);
                        }
                    }
                }
            }

            return(uniqueStrings);
        }
예제 #3
0
 /// <summary>
 /// Inserts into the database all relation information. Dependent objects will be commited.
 /// </summary>
 /// <param name="dbObject">The primary object owning the RelationList to be populated.</param>
 /// <param name="forceRetrieval">Determines if ALL relations will be retrieved.</param>
 private void updateRelationTables(DatabaseTable dbObject)
 {
     foreach (DBRelation currRelation in DBRelation.GetRelations(dbObject.GetType()))
     {
         updateRelationTable(dbObject, currRelation);
     }
 }
예제 #4
0
        private void verifyRelationTables(Type primaryType)
        {
            foreach (DBRelation currRelation in DBRelation.GetRelations(primaryType))
            {
                try {
                    // check if the table exists in the database, if not, create it
                    SQLiteResultSet resultSet = dbClient.Execute("select * from sqlite_master where type='table' and name = '" + currRelation.TableName + "'");
                    if (resultSet.Rows.Count == 0)
                    {
                        // create table
                        string createQuery =
                            "create table " + currRelation.TableName + " (id INTEGER primary key, " +
                            currRelation.PrimaryColumnName + " INTEGER, " +
                            currRelation.SecondaryColumnName + " INTEGER)";

                        resultSet = dbClient.Execute(createQuery);

                        // create index1
                        resultSet = dbClient.Execute("create index " + currRelation.TableName + "__index1 on " +
                                                     currRelation.TableName + " (" + currRelation.PrimaryColumnName + ")");

                        // create index2
                        resultSet = dbClient.Execute("create index " + currRelation.TableName + "__index2 on " +
                                                     currRelation.TableName + " (" + currRelation.SecondaryColumnName + ")");

                        logger.Debug("Created " + currRelation.TableName + " sub-table.");
                    }
                }
                catch (SQLiteException e) {
                    logger.FatalException("Error verifying " + currRelation.TableName + " subtable.", e);
                }
            }
        }
예제 #5
0
        private void getRelationData(DatabaseTable dbObject, DBRelation relation)
        {
            IRelationList list = relation.GetRelationList(dbObject);

            if (list.Populated)
            {
                return;
            }

            bool oldCommitNeededFlag = dbObject.CommitNeeded;

            list.Populated = true;

            // build query
            string selectQuery = "select " + relation.SecondaryColumnName + " from " +
                                 relation.TableName + " where " + relation.PrimaryColumnName + "=" + dbObject.ID;

            // and retireve relations
            SQLiteResultSet resultSet;

            lock (lockObject) resultSet = dbClient.Execute(selectQuery);

            // parse results and add them to the list
            list.Clear();
            foreach (SQLiteResultSet.Row currRow in resultSet.Rows)
            {
                int           objID  = int.Parse(currRow.fields[0]);
                DatabaseTable newObj = Get(relation.SecondaryType, objID);
                list.AddIgnoreSisterList(newObj);
            }

            // update flags as needed
            list.CommitNeeded     = false;
            dbObject.CommitNeeded = oldCommitNeededFlag;
        }
예제 #6
0
        public void CommitRelations(DatabaseTable dbObject)
        {
            if (dbObject == null)
            {
                return;
            }

            foreach (DBRelation currRelation in DBRelation.GetRelations(dbObject.GetType()))
            {
                if (currRelation.AutoRetrieve)
                {
                    foreach (DatabaseTable subObj in currRelation.GetRelationList(dbObject))
                    {
                        Commit(subObj);
                    }

                    updateRelationTable(dbObject, currRelation);
                }
            }

            foreach (DBField currField in DBField.GetFieldList(dbObject.GetType()))
            {
                if (currField.DBType == DBField.DBDataType.DB_OBJECT)
                {
                    Commit((DatabaseTable)currField.GetValue(dbObject));
                }
            }
        }
예제 #7
0
 // deletes all subtable data for the given object.
 private void deleteAllRelationData(DatabaseTable dbObject)
 {
     foreach (DBRelation currRelation in DBRelation.GetRelations(dbObject.GetType()))
     {
         deleteRelationData(dbObject, currRelation);
     }
 }
예제 #8
0
        private void updateRelationTable(DatabaseTable dbObject, DBRelation currRelation)
        {
            if (!currRelation.GetRelationList(dbObject).CommitNeeded)
            {
                return;
            }

            // clear out old values then insert the new
            deleteRelationData(dbObject, currRelation);

            // insert all relations to the database
            foreach (object currObj in (IList)currRelation.GetRelationList(dbObject))
            {
                DatabaseTable currDBObj = (DatabaseTable)currObj;
                Commit(currDBObj);
                string insertQuery = "insert into " + currRelation.TableName + "(" +
                                     currRelation.PrimaryColumnName + ", " +
                                     currRelation.SecondaryColumnName + ") values (" +
                                     dbObject.ID + ", " + currDBObj.ID + ")";

                lock (lockObject) dbClient.Execute(insertQuery);
            }

            currRelation.GetRelationList(dbObject).CommitNeeded = false;
        }
예제 #9
0
        private void deleteRelationData(DatabaseTable dbObject, DBRelation relation)
        {
            if (relation.PrimaryType != dbObject.GetType())
            {
                return;
            }

            string deleteQuery = "delete from " + relation.TableName + " where " + relation.PrimaryColumnName + "=" + dbObject.ID;

            lock (lockObject) dbClient.Execute(deleteQuery);
        }
예제 #10
0
        public void Revert(DatabaseTable dbObject)
        {
            if (dbObject == null || dbObject.RevertInProcess)
            {
                return;
            }

            dbObject.RevertInProcess = true;

            // recursively revert any child objects
            foreach (DBRelation currRelation in DBRelation.GetRelations(dbObject.GetType()))
            {
                foreach (DatabaseTable subObj in currRelation.GetRelationList(dbObject))
                {
                    Revert(subObj);
                }
            }

            // revert any objects directly linked to
            foreach (DBField currField in DBField.GetFieldList(dbObject.GetType()))
            {
                if (currField.DBType == DBField.DBDataType.DB_OBJECT)
                {
                    Commit((DatabaseTable)currField.GetValue(dbObject));
                }
            }

            // revert any modified relationships
            foreach (DBRelation currRelation in DBRelation.GetRelations(dbObject.GetType()))
            {
                if (currRelation.GetRelationList(dbObject).CommitNeeded)
                {
                    currRelation.GetRelationList(dbObject).Populated = false;
                    getRelationData(dbObject, currRelation);
                }
            }

            dbObject.RevertInProcess = false;

            // if this object has never been committed or has not been changed, just quit
            if (dbObject.ID == null || !dbObject.CommitNeeded)
            {
                return;
            }

            // regrab, copy values and reupdate cache
            cache.Remove(dbObject);
            DatabaseTable oldVersion = Get(dbObject.GetType(), (int)dbObject.ID);

            dbObject.Copy(oldVersion);
            cache.Replace(dbObject);
        }
예제 #11
0
        private void getAllRelationData(DatabaseTable dbObject)
        {
            if (preloading.Contains(dbObject.GetType()))
            {
                return;
            }

            foreach (DBRelation currRelation in DBRelation.GetRelations(dbObject.GetType()))
            {
                if (currRelation.AutoRetrieve)
                {
                    getRelationData(dbObject, currRelation);
                }
            }
        }
예제 #12
0
        public object ConvertString(DatabaseManager dbManager, string strVal)
        {
            try {
                if (string.IsNullOrEmpty(strVal.Trim()) && IsNullable)
                {
                    return(null);
                }

                switch (DBType)
                {
                case DBDataType.INTEGER:
                    string tmp = strVal.ToString();
                    while (tmp.Contains(","))
                    {
                        tmp = tmp.Remove(tmp.IndexOf(','), 1);
                    }

                    return(int.Parse(tmp));

                case DBDataType.LONG:
                    tmp = strVal.ToString();
                    while (tmp.Contains(","))
                    {
                        tmp = tmp.Remove(tmp.IndexOf(','), 1);
                    }

                    return(long.Parse(tmp));

                case DBDataType.REAL:
                    if (propertyInfo.PropertyType == typeof(double))
                    {
                        return(double.Parse(strVal, new CultureInfo("en-US", false)));
                    }
                    else
                    {
                        return(float.Parse(strVal, new CultureInfo("en-US", false)));
                    }

                case DBDataType.BOOL:
                    return(strVal.ToString() == "true" || strVal.ToString() == "1");

                case DBDataType.STRING_OBJECT:
                    // create a new object and populate it
                    IStringSourcedObject newObj = (IStringSourcedObject)propertyInfo.PropertyType.GetConstructor(System.Type.EmptyTypes).Invoke(null);
                    newObj.LoadFromString(strVal);
                    return(newObj);

                case DBDataType.TYPE:
                    return(Type.GetType(strVal));

                case DBDataType.ENUM:
                    if (strVal.Trim().Length != 0)
                    {
                        Type enumType = propertyInfo.PropertyType;
                        if (Nullable.GetUnderlyingType(enumType) != null)
                        {
                            enumType = Nullable.GetUnderlyingType(enumType);
                        }

                        return(Enum.Parse(enumType, strVal));
                    }
                    break;

                case DBDataType.DATE_TIME:
                    DateTime newDateTimeObj = DateTime.Now;
                    if (strVal.Trim().Length != 0)
                    {
                        try {
                            newDateTimeObj = DateTime.Parse(strVal);
                        }
                        catch { }
                    }

                    return(newDateTimeObj);

                case DBDataType.DB_OBJECT:
                    if (strVal.Trim().Length == 0)
                    {
                        return(null);
                    }

                    string[] objectValues = strVal.Split(new string[] { "|||" }, StringSplitOptions.None);
                    if (objectValues.Length > 1)
                    {
                        return(dbManager.Get(Type.GetType(objectValues[1]), int.Parse(objectValues[0])));
                    }
                    else
                    {
                        return(dbManager.Get(propertyInfo.PropertyType, int.Parse(strVal)));
                    }

                case DBDataType.DB_FIELD:
                    string[] fieldValues = strVal.Split(new string[] { "|||" }, StringSplitOptions.None);
                    if (fieldValues.Length != 2)
                    {
                        break;
                    }

                    return(DBField.GetFieldByDBName(Type.GetType(fieldValues[0]), fieldValues[1]));

                case DBDataType.DB_RELATION:
                    string[] relationValues = strVal.Split(new string[] { "|||" }, StringSplitOptions.None);
                    if (relationValues.Length != 3)
                    {
                        break;
                    }

                    return(DBRelation.GetRelation(Type.GetType(relationValues[0]),
                                                  Type.GetType(relationValues[1]),
                                                  relationValues[2]));

                default:
                    return(strVal);
                }
            }
            catch (Exception e) {
                if (e.GetType() == typeof(ThreadAbortException))
                {
                    throw e;
                }

                logger.Error("Error parsing " + propertyInfo.DeclaringType.Name + "." + this.Name +
                             " Property: " + e.Message);
            }

            return(null);
        }
예제 #13
0
        private static void loadRelations(Type primaryType)
        {
            if (relations.ContainsKey(primaryType))
                return;

            List<DBRelation> newRelations = new List<DBRelation>();

            foreach (PropertyInfo currProperty in primaryType.GetProperties())
                foreach (object currAttr in currProperty.GetCustomAttributes(true))
                    // if we have come to a relation property, lets process it
                    if (currAttr.GetType() == typeof(DBRelationAttribute)) {
                        DBRelation newRelation = new DBRelation();
                        newRelation._primaryType = primaryType;
                        newRelation._secondaryType = currProperty.PropertyType.GetGenericArguments()[1];
                        newRelation._identifier = ((DBRelationAttribute)currAttr).Identifier;
                        newRelation._propertyInfo = currProperty;
                        newRelation._autoRetrieve = ((DBRelationAttribute)currAttr).AutoRetrieve;
                        newRelation._filterable = ((DBRelationAttribute)currAttr).Filterable;
                        newRelation.getRelationListMethod = currProperty.GetGetMethod();

                        newRelations.Add(newRelation);
                    }

            relations[primaryType] = newRelations;
        }
예제 #14
0
        // creates an escaped, quoted string representation of the given object
        public static string getSQLiteString(DBField ownerField, object value)
        {
            if (value == null)
            {
                return("NULL");
            }

            string strVal = "";

            // handle boolean types
            if (value.GetType() == typeof(bool) || value.GetType() == typeof(Boolean))
            {
                if ((Boolean)value == true)
                {
                    strVal = "1";
                }
                else
                {
                    strVal = "0";
                }
            }
            // handle double types
            else if (value.GetType() == typeof(double) || value.GetType() == typeof(Double))
            {
                strVal = ((double)value).ToString(new CultureInfo("en-US", false));
            }

            // handle float types
            else if (value.GetType() == typeof(float) || value.GetType() == typeof(Single))
            {
                strVal = ((float)value).ToString(new CultureInfo("en-US", false));
            }

            // handle database table types
            else if (IsDatabaseTableType(value.GetType()))
            {
                if (ownerField != null && ownerField.Type != value.GetType())
                {
                    strVal = ((DatabaseTable)value).ID.ToString() + "|||" + value.GetType().AssemblyQualifiedName;
                }
                else
                {
                    strVal = ((DatabaseTable)value).ID.ToString();
                }
            }

            // if field represents metadata about another dbfield
            else if (value is DBField)
            {
                DBField field = (DBField)value;
                strVal = field.OwnerType.AssemblyQualifiedName + "|||" + field.FieldName;
            }

            // if field represents metadata about a relation (subtable)
            else if (value is DBRelation)
            {
                DBRelation relation = (DBRelation)value;
                strVal = relation.PrimaryType.AssemblyQualifiedName + "|||" +
                         relation.SecondaryType.AssemblyQualifiedName + "|||" +
                         relation.Identifier;
            }

            // handle C# Types, Need full qualified name to load types from other aseemblies
            else if (value is Type)
            {
                strVal = ((Type)value).AssemblyQualifiedName;
            }

            else if (value is DateTime)
            {
                strVal = ((DateTime)value).ToUniversalTime().ToString("u");
            }
            // everythign else just uses ToString()
            else
            {
                strVal = value.ToString();
            }


            // if we ended up with an empty string, save a space. an empty string is interpreted
            // as null by SQLite, and thats not what we want.
            if (strVal == "")
            {
                strVal = " ";
            }

            // escape all quotes
            strVal = strVal.Replace("'", "''");

            return("'" + strVal + "'");
        }