internal static bool _fill(GreyFoxEvent greyFoxEvent) { StringBuilder query; Database database; DbCommand dbCommand; query = new StringBuilder("SELECT "); query.Append(string.Join(",", InnerJoinFields)); query.Append(" FROM "); query.Append(greyFoxEvent.tableName); query.Append(" WHERE GreyFoxEventID="); query.Append(greyFoxEvent.iD); query.Append(";"); database = DatabaseFactory.CreateDatabase(); dbCommand = database.GetSqlStringCommand(query.ToString()); IDataReader r = database.ExecuteReader(dbCommand); if (!r.Read()) { throw(new Exception(string.Format("Cannot find GreyFoxEventID '{0}'.", greyFoxEvent.iD))); } FillFromReader(greyFoxEvent, greyFoxEvent.tableName, r, 0, 1); // Microsoft DAAB still needs to have the reader closed. r.Close(); return(true); }
/// <summary> /// Makes a deep copy of the current GreyFoxEvent. /// </summary> /// <returns> A new GreyFoxEvent object reflecting the cloned GreyFoxEvent object.</returns> public GreyFoxEvent Copy() { GreyFoxEvent greyFoxEvent = new GreyFoxEvent(); CopyTo(greyFoxEvent); return(greyFoxEvent); }
/// <summary> /// Deep copies the current GreyFoxEvent to another instance of GreyFoxEvent. /// </summary> /// <param name="GreyFoxEvent">The GreyFoxEvent to copy to.</param> /// <param name="isolation">Placeholders are used to isolate the GreyFoxEvent from its children.</param> public void CopyTo(GreyFoxEvent greyFoxEvent, bool isolation) { lock (this) { greyFoxEvent.iD = iD; greyFoxEvent.tableName = tableName; greyFoxEvent.isPlaceHolder = isPlaceHolder; greyFoxEvent.isSynced = isSynced; greyFoxEvent.type = type; greyFoxEvent.eventDate = eventDate; greyFoxEvent.source = source; greyFoxEvent.category = category; greyFoxEvent.description = description; greyFoxEvent.eventID = eventID; if (user != null) { if (isolation) { greyFoxEvent.user = user.NewPlaceHolder(); } else { greyFoxEvent.user = user.Copy(false); } } } }
internal static int _update(GreyFoxEvent greyFoxEvent) { Database database; DbCommand dbCommand; database = DatabaseFactory.CreateDatabase(); dbCommand = database.GetSqlStringCommand("UPDATE " + greyFoxEvent.tableName + " SET Type=@Type," + "EventDate=@EventDate," + "Source=@Source," + "Category=@Category," + "Description=@Description," + "EventID=@EventID," + "UserID=@UserID WHERE GreyFoxEventID=@GreyFoxEventID;"); fillParameters(database, dbCommand, greyFoxEvent); database.AddInParameter(dbCommand, "GreyFoxEventID", DbType.Int32, greyFoxEvent.iD); // Abandon remaining updates if no rows have been updated by returning false immediately. if (database.ExecuteNonQuery(dbCommand) == 0) { return(-1); } return(greyFoxEvent.iD); }
public static GreyFoxEvent ParseFromReader(string tableName, IDataReader r, int idOffset, int dataOffset) { GreyFoxEvent greyFoxEvent = new GreyFoxEvent(tableName); FillFromReader(greyFoxEvent, tableName, r, idOffset, dataOffset); return(greyFoxEvent); }
/// <summary> /// Makes a deep copy of the current GreyFoxEvent. /// </summary> /// <returns> A new GreyFoxEvent object reflecting the cloned GreyFoxEvent object.</returns> /// <param name="isolation">Placeholders are used to isolate the GreyFoxEvent from its children.</param> public GreyFoxEvent Copy(bool isolation) { GreyFoxEvent greyFoxEvent = new GreyFoxEvent(); CopyTo(greyFoxEvent, isolation); return(greyFoxEvent); }
public bool Equals(GreyFoxEvent greyFoxEvent) { if (greyFoxEvent == null) { return(false); } return(this.iD == greyFoxEvent.iD && this.tableName == greyFoxEvent.tableName); }
public static GreyFoxEvent NewPlaceHolder(string tableName, int iD) { GreyFoxEvent greyFoxEvent = new GreyFoxEvent(); greyFoxEvent.iD = iD; greyFoxEvent.tableName = tableName; greyFoxEvent.isPlaceHolder = true; greyFoxEvent.isSynced = true; return(greyFoxEvent); }
public void Remove(GreyFoxEvent value) { OnCollectionChanged(EventArgs.Empty); int index = IndexOf(value); if (index == -1) { throw(new Exception("GreyFoxEvent not found in collection.")); } RemoveAt(index); }
public int Add(GreyFoxEvent value) { OnCollectionChanged(EventArgs.Empty); lock (this) { count++; ensureArrays(); addIndexKey(value.ID); GreyFoxEventArray[count - 1] = value; return(count - 1); } }
/// <summary> /// Inserts a GreyFoxEvent into the database. All children should have been /// saved to the database before insertion. New children will not be /// related to this object in the database. /// </summary> /// <param name="_GreyFoxEvent">The GreyFoxEvent to insert into the database.</param> internal static int _insert(GreyFoxEvent greyFoxEvent) { int id; string query; Database database; DbCommand dbCommand; database = DatabaseFactory.CreateDatabase(); query = "INSERT INTO " + greyFoxEvent.tableName + "(" + "Type," + "EventDate," + "Source," + "Category," + "Description," + "EventID," + "UserID) VALUES (" + "@Type," + "@EventDate," + "@Source," + "@Category," + "@Description," + "@EventID," + "@UserID);"; if (database.ConnectionStringWithoutCredentials.StartsWith("provider=microsoft.jet.oledb.4.0")) { // Microsoft Access // Connection must remain open for IDENTITY to return correct value, // therefore use the dbCommand object's Connection directly to control // connection state. dbCommand = database.GetSqlStringCommand(query); fillParameters(database, dbCommand, greyFoxEvent); dbCommand.Connection = database.CreateConnection(); dbCommand.Connection.Open(); dbCommand.ExecuteNonQuery(); dbCommand.CommandText = "SELECT @@IDENTITY AS LastID"; id = (int)dbCommand.ExecuteScalar(); dbCommand.Connection.Close(); } else { //// Microsoft SQL Server dbCommand = database.GetSqlStringCommand(query + " SELECT @LastID = SCOPE_IDENTITY();"); fillParameters(database, dbCommand, greyFoxEvent); database.AddOutParameter(dbCommand, "@LastID", DbType.Int32, 10); database.ExecuteNonQuery(dbCommand); id = (int)dbCommand.Parameters["@LastID"].Value; } return(id); }
public void Add(GreyFoxEvent GreyFoxEvent, TimeSpan slidingExpiration) { lock (this) { count++; ensureArrays(); greyFoxEventArray[count - 1] = GreyFoxEvent; timeStamps[count - 1] = DateTime.Now; absoluteExpirations[count - 1] = DateTime.Now.Add(slidingExpiration); // Never Expires slidingExpirations[count - 1] = slidingExpiration; // Never slides quickSort(0, count - 1); } }
/// <summary> /// Duplicates GreyFoxEvent object into a database; may or may not be the same database /// as the parent object. /// </summary> /// <returns> A new GreyFoxEvent object reflecting the replicated GreyFoxEvent object.</returns> public GreyFoxEvent Duplicate(string tableName) { lock (this) { GreyFoxEvent clonedGreyFoxEvent = this.Clone(); clonedGreyFoxEvent.tableName = tableName; // Insert must be called after children are replicated! clonedGreyFoxEvent.iD = GreyFoxEventManager._insert(clonedGreyFoxEvent); clonedGreyFoxEvent.isSynced = true; return(clonedGreyFoxEvent); } }
public int IndexOf(GreyFoxEvent value) { lock (this) { for (int x = 0; x < count; x++) { if (GreyFoxEventArray[x].Equals(value)) { return(x); } } } return(-1); }
public void Insert(int index, GreyFoxEvent value) { OnCollectionChanged(EventArgs.Empty); lock (this) { count++; ensureArrays(); addIndexKey(value.ID); for (int x = index + 1; x == count - 2; x++) { GreyFoxEventArray[x] = GreyFoxEventArray[x - 1]; } GreyFoxEventArray[index] = value; } }
/// <summary> /// Ensures that the index and object array are sized correctly /// for additions. This method should be protected by locks /// issued by calling methods. /// </summary> private void ensureArrays() { if (count > GreyFoxEventArray.GetUpperBound(0) + 1) { int[,] tempIndex = new int[count * 2, 2]; GreyFoxEvent[] tempGreyFoxEventArray = new GreyFoxEvent[count * 2]; for (int x = 0; x <= GreyFoxEventArray.GetUpperBound(0); x++) { tempIndex[x, 0] = index[x, 0]; // Copy ID tempIndex[x, 1] = index[x, 1]; // Copy Location tempGreyFoxEventArray[x] = GreyFoxEventArray[x]; // Copy Object } index = tempIndex; GreyFoxEventArray = tempGreyFoxEventArray; } }
public int Add(GreyFoxEvent value) { OnCollectionChanged(EventArgs.Empty); lock (this) { count++; // Resize the array if the count is greater than the length // of the array. if (count > GreyFoxEventArray.GetUpperBound(0) + 1) { GreyFoxEvent[] tempGreyFoxEventArray = new GreyFoxEvent[count * 2]; Array.Copy(GreyFoxEventArray, tempGreyFoxEventArray, count - 1); GreyFoxEventArray = tempGreyFoxEventArray; } GreyFoxEventArray[count - 1] = value; } return(count - 1); }
/// <summary> /// Fills the {0} from a OleIDataReader. /// </summary> public static void FillFromReader(GreyFoxEvent greyFoxEvent, string tableName, IDataReader r, int idOffset, int dataOffset) { greyFoxEvent.tableName = tableName; greyFoxEvent.iD = r.GetInt32(idOffset); greyFoxEvent.isSynced = true; greyFoxEvent.isPlaceHolder = false; greyFoxEvent.type = r.GetByte(0 + dataOffset); greyFoxEvent.eventDate = r.GetDateTime(1 + dataOffset); greyFoxEvent.source = r.GetString(2 + dataOffset); greyFoxEvent.category = r.GetString(3 + dataOffset); greyFoxEvent.description = r.GetString(4 + dataOffset); greyFoxEvent.eventID = r.GetInt32(5 + dataOffset); if (!r.IsDBNull(6 + dataOffset) && r.GetInt32(6 + dataOffset) > 0) { greyFoxEvent.user = GreyFoxUser.NewPlaceHolder(r.GetInt32(6 + dataOffset)); } }
/// <summary> /// Ensures that the index and object array are sized correctly /// for additions. This method should be protected by locks /// issued by calling methods. /// </summary> private void ensureArrays() { if (count > GreyFoxEventArray.GetUpperBound(0) + 1) { GreyFoxEvent[] tempGreyFoxEventArray = new GreyFoxEvent[count * 2]; DateTime[] tempTimeStamps = new DateTime[count * 2]; DateTime[] tempAbsoluteExpirations = new DateTime[count * 2]; TimeSpan[] tempSlidingExpirations = new TimeSpan[count * 2]; Array.Copy(greyFoxEventArray, tempGreyFoxEventArray, count - 1); Array.Copy(timeStamps, tempTimeStamps, count - 1); Array.Copy(absoluteExpirations, tempAbsoluteExpirations, count - 1); Array.Copy(slidingExpirations, tempSlidingExpirations, count - 1); greyFoxEventArray = tempGreyFoxEventArray; timeStamps = tempTimeStamps; absoluteExpirations = tempAbsoluteExpirations; slidingExpirations = tempSlidingExpirations; } }
public void Insert(int index, GreyFoxEvent value) { OnCollectionChanged(EventArgs.Empty); lock (this) { count++; // Resize the array if the count is greater than the length // of the array. if (count > GreyFoxEventArray.GetUpperBound(0) + 1) { GreyFoxEvent[] tempGreyFoxEventArray = new GreyFoxEvent[count * 2]; Array.Copy(GreyFoxEventArray, tempGreyFoxEventArray, count - 1); GreyFoxEventArray = tempGreyFoxEventArray; } for (int x = index + 1; x == count - 2; x++) { GreyFoxEventArray[x] = GreyFoxEventArray[x - 1]; } GreyFoxEventArray[index] = value; } }
private static void fillParameters(Database database, DbCommand dbCommand, GreyFoxEvent greyFoxEvent) { #region Default addParameter(database, dbCommand, "@Type", DbType.Byte, greyFoxEvent.type); addParameter(database, dbCommand, "@EventDate", DbType.Date, greyFoxEvent.eventDate); addParameter(database, dbCommand, "@Source", DbType.String, greyFoxEvent.source); addParameter(database, dbCommand, "@Category", DbType.String, greyFoxEvent.category); addParameter(database, dbCommand, "@Description", DbType.String, greyFoxEvent.description); addParameter(database, dbCommand, "@EventID", DbType.Int32, greyFoxEvent.eventID); if (greyFoxEvent.user == null) { addParameter(database, dbCommand, "@UserID", DbType.Int32, DBNull.Value); } else { addParameter(database, dbCommand, "@UserID", DbType.Int32, greyFoxEvent.user.ID); } #endregion }
public void CheckedAdd(GreyFoxEvent GreyFoxEvent, TimeSpan slidingExpiration) { lock (this) { int i = binarySearch(greyFoxEvent.iD); if (i != -1) { greyFoxEventArray[i] = GreyFoxEvent; absoluteExpirations[i] = DateTime.Now.Add(slidingExpiration); // Expires slidingExpirations[i] = slidingExpiration; // Never slides return; } count++; ensureArrays(); greyFoxEventArray[count - 1] = GreyFoxEvent; timeStamps[count - 1] = DateTime.Now; absoluteExpirations[count - 1] = DateTime.Now.Add(slidingExpiration); // Expires slidingExpirations[count - 1] = slidingExpiration; // Never slides quickSort(0, count - 1); } }
/// <summary> /// Clones GreyFoxEvent object and clones child objects with cloning or replication. /// as the parent object. /// </summary> /// <returns> A new GreyFoxEvent object reflecting the replicated GreyFoxEvent object.</returns> public GreyFoxEvent Clone() { lock (this) { GreyFoxEvent clonedGreyFoxEvent = new GreyFoxEvent(); clonedGreyFoxEvent.iD = iD; clonedGreyFoxEvent.tableName = tableName; clonedGreyFoxEvent.isSynced = isSynced; clonedGreyFoxEvent.type = type; clonedGreyFoxEvent.eventDate = eventDate; clonedGreyFoxEvent.source = source; clonedGreyFoxEvent.category = category; clonedGreyFoxEvent.description = description; clonedGreyFoxEvent.eventID = eventID; if (user != null) { clonedGreyFoxEvent.user = user; } return(clonedGreyFoxEvent); } }
public bool Contains(GreyFoxEvent value) { return(IndexOf(value) != -1); }
public GreyFoxEventCollection GetCollection(int topCount, string whereClause, string sortClause, params GreyFoxEventFlags[] optionFlags) { StringBuilder query; Database database; DbCommand dbCommand; IDataReader r; GreyFoxEventCollection greyFoxEventCollection; int innerJoinOffset; query = new StringBuilder("SELECT "); if (topCount > 0) { query.Append("TOP "); query.Append(topCount); query.Append(" "); } foreach (string columnName in InnerJoinFields) { query.Append("GreyFoxEvent."); query.Append(columnName); query.Append(","); } innerJoinOffset = InnerJoinFields.GetUpperBound(0) + 1; int userOffset = -1; int userContactOffset = -1; // // Append Option Flag Fields // if (optionFlags != null) { for (int x = 0; x < optionFlags.Length; x++) { switch (optionFlags[x]) { case GreyFoxEventFlags.User: for (int i = 0; i <= GreyFoxUserManager.InnerJoinFields.GetUpperBound(0); i++) { query.Append("User."); query.Append(GreyFoxUserManager.InnerJoinFields[i]); query.Append(","); } userOffset = innerJoinOffset; innerJoinOffset = userOffset + GreyFoxUserManager.InnerJoinFields.GetUpperBound(0) + 1; break; case GreyFoxEventFlags.UserContact: for (int i = 0; i <= GreyFoxContactManager.InnerJoinFields.GetUpperBound(0); i++) { query.Append("User_Contact."); query.Append(GreyFoxContactManager.InnerJoinFields[i]); query.Append(","); } userContactOffset = innerJoinOffset; innerJoinOffset = userContactOffset + GreyFoxContactManager.InnerJoinFields.GetUpperBound(0) + 1; break; } } } // // Remove trailing comma // query.Length--; if (optionFlags != null) { query.Append(" FROM "); // // Start INNER JOIN expressions // for (int x = 0; x < optionFlags.Length; x++) { query.Append("("); } query.Append(tableName); query.Append(" AS GreyFoxEvent"); } else { query.Append(" FROM "); query.Append(tableName); query.Append(" AS GreyFoxEvent"); } // // Finish INNER JOIN expressions // if (optionFlags != null) { for (int x = 0; x < optionFlags.Length; x++) { switch (optionFlags[x]) { case GreyFoxEventFlags.User: query.Append(" LEFT JOIN sysGlobal_Users ON "); query.Append(tableName); query.Append(" AS GreyFoxEvent"); query.Append(".UserID = GreyFoxEvent.ID)"); break; case GreyFoxEventFlags.UserContact: query.Append(" LEFT JOIN sysGlobal_Contacts AS User_Contact ON User.ContactID = User_Contact.GreyFoxContactID)"); break; } } } // // Render where clause // if (whereClause != string.Empty) { query.Append(" WHERE "); query.Append(whereClause); } // // Render sort clause // if (sortClause != string.Empty) { query.Append(" ORDER BY "); query.Append(sortClause); } // // Render final semicolon // query.Append(";"); database = DatabaseFactory.CreateDatabase(); dbCommand = database.GetSqlStringCommand(query.ToString()); #if DEBUG try { r = database.ExecuteReader(dbCommand); } catch (Exception e) { string msg = e.Message; throw(new Exception(msg + " --- Query: " + query.ToString())); } #else r = database.ExecuteReader(dbCommand); #endif greyFoxEventCollection = new GreyFoxEventCollection(); while (r.Read()) { GreyFoxEvent greyFoxEvent = ParseFromReader(tableName, r, 0, 1); // Fill User if (userOffset != -1 && !r.IsDBNull(userOffset)) { GreyFoxUserManager.FillFromReader(greyFoxEvent.user, r, userOffset, userOffset + 1); // Fill if (userContactOffset != -1 && !r.IsDBNull(userContactOffset)) { GreyFoxContactManager.FillFromReader(greyFoxEvent.user.Contact, "sysGlobal_Contacts", r, userContactOffset, userContactOffset + 1); } } greyFoxEventCollection.Add(greyFoxEvent); } // Microsoft DAAB still needs to close readers. r.Close(); return(greyFoxEventCollection); }
/// <summary> /// Deep copies the current GreyFoxEvent to another instance of GreyFoxEvent. /// This method does not provide isolated copies; use overriden method for this feature. /// </summary> /// <param name="GreyFoxEvent">The GreyFoxEvent to copy to.</param> public void CopyTo(GreyFoxEvent greyFoxEvent) { CopyTo(greyFoxEvent, false); }
/// <summary> /// Compares the object's ID to another object's ID. /// </summary> int IComparable.CompareTo(object obj) { GreyFoxEvent greyFoxEvent = (GreyFoxEvent)obj; return(this.iD - greyFoxEvent.iD); }
/// <summary> /// Compares the object's ID to another object's ID. /// </summary> public int CompareTo(GreyFoxEvent greyFoxEvent) { return(this.iD - greyFoxEvent.iD); }