GetFieldList() публичный статический Метод

public static GetFieldList ( Type tableType ) : ReadOnlyCollection
tableType System.Type
Результат ReadOnlyCollection
Пример #1
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));
                }
            }
        }
Пример #2
0
        // inserts a new object to the database
        private void insert(DatabaseTable dbObject)
        {
            try {
                string queryFieldList = "";
                string queryValueList = "";

                // loop through the fields and build the strings for the query
                foreach (DBField currField in DBField.GetFieldList(dbObject.GetType()))
                {
                    if (queryFieldList != "")
                    {
                        queryFieldList += ", ";
                        queryValueList += ", ";
                    }

                    // if we dont have an ID, commit as needed
                    if (currField.DBType == DBField.DBDataType.DB_OBJECT && currField.GetValue(dbObject) != null &&
                        ((DatabaseTable)currField.GetValue(dbObject)).ID == null)
                    {
                        Commit((DatabaseTable)currField.GetValue(dbObject));
                    }

                    queryFieldList += currField.FieldName;
                    queryValueList += getSQLiteString(currField, currField.GetValue(dbObject));
                }

                string query = "insert into " + GetTableName(dbObject.GetType()) +
                               " (" + queryFieldList + ") values (" + queryValueList + ")";

                logger.Debug("INSERTING: " + dbObject.ToString());

                lock (lockObject) {
                    dbClient.Execute(query);
                    dbObject.ID = dbClient.LastInsertID();
                }
                dbObject.DBManager = this;
                cache.Add(dbObject);

                // loop through the fields and commit attached objects as needed
                foreach (DBField currField in DBField.GetFieldList(dbObject.GetType()))
                {
                    if (currField.DBType == DBField.DBDataType.DB_OBJECT)
                    {
                        Commit((DatabaseTable)currField.GetValue(dbObject));
                    }
                }

                // notify any listeners of the status change
                if (ObjectInserted != null)
                {
                    ObjectInserted(dbObject);
                }
            }
            catch (SQLiteException e) {
                logger.ErrorException("Could not commit to " + GetTableName(dbObject.GetType()) + " table.", e);
            }
        }
Пример #3
0
        // updates the given object in the database. assumes the object was previously retrieved
        // via a Get call from this class.
        private void update(DatabaseTable dbObject)
        {
            try {
                string query = "update " + GetTableName(dbObject.GetType()) + " set ";

                // loop through the fields and build the strings for the query
                bool firstField = true;
                foreach (DBField currField in DBField.GetFieldList(dbObject.GetType()))
                {
                    if (!firstField)
                    {
                        query += ", ";
                    }

                    firstField = false;

                    // if this is a linked db object commit it as needed
                    if (currField.DBType == DBField.DBDataType.DB_OBJECT)
                    {
                        Commit((DatabaseTable)currField.GetValue(dbObject));
                    }

                    query += currField.FieldName + " = " + getSQLiteString(currField, currField.GetValue(dbObject));
                }

                // add the where clause
                query += " where id = " + dbObject.ID;

                // execute the query
                logger.Debug("UPDATING: " + dbObject.ToString());

                lock (lockObject) dbClient.Execute(query);
                dbObject.DBManager = this;

                updateRelationTables(dbObject);

                // notify any listeners of the status change
                TableUpdateInfo ui = new TableUpdateInfo();
                ui.UpdatedFields = new HashSet <DBField>(dbObject.changedFields);
                if (ObjectUpdatedEx != null)
                {
                    ObjectUpdatedEx(dbObject, ui);
                }
                if (ObjectUpdated != null)
                {
                    ObjectUpdated(dbObject);
                }

                dbObject.changedFields.Clear();
            }
            catch (SQLiteException e) {
                logger.ErrorException("Could not commit to " + GetTableName(dbObject.GetType()) + " table.", e);
            }
        }
Пример #4
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);
        }
Пример #5
0
        // Returns a select statement retrieving all fields ordered as defined by FieldList
        // for the given Table Type. A where clause can be appended
        private static string getSelectQuery(Type tableType)
        {
            string query = "select ";

            foreach (DBField currField in DBField.GetFieldList(tableType))
            {
                if (query != "select ")
                {
                    query += ", ";
                }

                query += currField.FieldName;
            }
            query += ", id from " + GetTableName(tableType) + " ";
            return(query);
        }
Пример #6
0
        // Checks that the table corresponding to this type exists, and if it is missing, it creates it.
        // Also verifies all columns represented in the class are also present in the table, creating
        // any missing. Needs to be enhanced to allow for changed defaults.
        private void verifyTable(Type tableType)
        {
            lock (lockObject) {
                // check that we haven't already verified this table
                if (isVerified.ContainsKey(tableType))
                {
                    return;
                }

                // attempt to grab table info for the type. if none exists, it's not tagged to be a table
                DBTableAttribute tableAttr = getDBTableAttribute(tableType);
                if (tableAttr == null)
                {
                    return;
                }

                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 = '" + tableAttr.TableName + "'");
                    if (resultSet.Rows.Count == 0)
                    {
                        resultSet = dbClient.Execute("create table " + tableAttr.TableName + " (id INTEGER primary key )");
                        logger.Info("Created " + tableAttr.TableName + " table.");
                    }

                    // grab existing table info from the DB
                    resultSet = dbClient.Execute("PRAGMA table_info(" + tableAttr.TableName + ")");

                    // loop through the CLASS DEFINED fields, and verify each is contained in the result set
                    foreach (DBField currField in DBField.GetFieldList(tableType))
                    {
                        // loop through all defined columns in DB to ensure this col exists
                        bool exists = false;
                        foreach (SQLiteResultSet.Row currRow in resultSet.Rows)
                        {
                            if (currField.FieldName == currRow.fields[1])
                            {
                                exists = true;
                                break;
                            }
                        }

                        // if we couldn't find the column create it
                        if (!exists)
                        {
                            string defaultValue;
                            if (currField.Default == null)
                            {
                                defaultValue = "NULL";
                            }
                            else
                            {
                                defaultValue = getSQLiteString(currField, currField.Default);
                            }

                            dbClient.Execute("alter table " + tableAttr.TableName + " add column " + currField.FieldName + " " +
                                             currField.DBType.ToString() + " default " + defaultValue);
                            // logger.Debug("Added " + tableAttr.TableName + "." + currField.FieldName + " column.");
                        }
                    }

                    verifyRelationTables(tableType);
                    isVerified[tableType] = true;
                }
                catch (SQLiteException e) {
                    logger.ErrorException("Internal error verifying " + tableAttr.TableName + " (" + tableType.ToString() + ") table.", e);
                }
            }
        }