Пример #1
0
        // Function that executes a select statement.
        protected MySqlDataReader DoLoad(DatabaseQuery query)
        {
            if (!Valid)
            {
                return(null);
            }

            if (query.Comparator == null)
            {
                query.Comparator = Identifier;
            }

            int columnCount = query.Count;

            query.Add(Database.Tables.Generic.CREATED_ON_COLUMN_NAME);
            query.Add(Database.Tables.Generic.LAST_UPDATED_COLUMN_NAME);

            MySqlCommand    command = new MySqlCommand(query.Select, Database.Connection);
            MySqlDataReader reader  = command.ExecuteReader();

            if (reader.Read())
            {
                Loaded = true;
                DateTime creationTime   = reader.GetDateTime(columnCount++);
                DateTime lastUpdateTime = reader.GetDateTime(columnCount);

                if (CreatedOn < creationTime)
                {
                    CreatedOn = creationTime;
                }

                if (LastSaved < lastUpdateTime)
                {
                    LastSaved = lastUpdateTime;
                }
            }
            else
            {
                Loaded = false;
            }

            return(reader);
        }
Пример #2
0
        // Function that executes an insert/update statement.
        protected int DoSave(DatabaseQuery query)
        {
            bool exists = query.Exists;

            if (Valid && exists)
            {
                // Use the default comparator if there is none existing.
                if (query.Comparator == null)
                {
                    query.Comparator = Identifier;
                }

                Loaded    = true;
                LastSaved = DateTime.Now;
                query.Add(Database.Tables.Generic.LAST_UPDATED_COLUMN_NAME, LastSaved);

                MySqlCommand command = new MySqlCommand(query.Update, Database.Connection);
                return(command.ExecuteNonQuery());
            }
            else
            {
                // Add a new row and get it's ID.
                CreatedOn = DateTime.Now;
                LastSaved = DateTime.Now;
                query.Add(Database.Tables.Generic.CREATED_ON_COLUMN_NAME, CreatedOn);
                query.Add(Database.Tables.Generic.LAST_UPDATED_COLUMN_NAME, LastSaved);

                MySqlCommand command = new MySqlCommand(query.Insert, Database.Connection);
                command.ExecuteNonQuery();

                if (!Valid)
                {
                    id     = ( int )(command.LastInsertedId);
                    Loaded = true;
                }

                return(1);
            }
        }