// Convert an object into an SQL query value: public virtual string Stringify() { // Format the value appropraitely. if (Value is DateTime) { DateTime actualValue = ( DateTime )(Value); return(String.Format("'{0}'", actualValue.ToString("yyyy-MM-dd HH:mm:ss"))); } else if (Value is QueryElement) { QueryElement actualValue = ( QueryElement )(Value); return(actualValue.Column); } else if (Value is DatabaseObject) { DatabaseObject actualValue = ( DatabaseObject )(Value); actualValue.Validate(); return(String.Format("'{0}'", actualValue.Id)); } else if (Value is DatabaseQuery) { DatabaseQuery actualValue = ( DatabaseQuery )(Value); return(String.Format("({0})", actualValue.Select)); } else { return(String.Format("'{0}'", Value)); } }
// Function that executes a select statement. protected int DoDelete(DatabaseQuery query) { if (!Valid) { return(0); } if (query.Comparator == null) { query.Comparator = Identifier; } Loaded = false; MySqlCommand command = new MySqlCommand(query.Delete, Database.Connection); return(command.ExecuteNonQuery()); }
// 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); }
// 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); } }
// Constructor for a sub-query public QueryElement(DatabaseQuery subquery) { Column = null; Value = subquery; }