public void PopulateCache() { string queryString = string.Empty; queryString = "SELECT creation_time, incoming_seqnum, outgoing_seqnum FROM " + sessions_table + " WHERE " + "beginstring=" + "'" + _sessionID.BeginString + "' and " + "sendercompid=" + "'" + _sessionID.SenderCompID + "' and " + "targetcompid=" + "'" + _sessionID.TargetCompID + "' and " + "session_qualifier=" + "'" + _sessionID.SessionQualifier + "'"; using (OdbcConnection odbc = GetODBCConnection()) { OdbcCommand cmd = new OdbcCommand(queryString, odbc); OdbcDataReader reader = cmd.ExecuteReader(); int rows = 0; if (reader.HasRows) { while (reader.Read()) { rows++; if (rows > 1) { throw new ConfigError("Multiple entries found for session in database"); } cache_.CreationTime = DateTime.SpecifyKind((DateTime)reader[0], DateTimeKind.Utc); //DateTime.SpecifyKind(cache_.CreationTime.Value, DateTimeKind.Utc); cache_.SetNextTargetMsgSeqNum((int)reader[1]); cache_.SetNextSenderMsgSeqNum((int)reader[2]); } } else { DateTime createTime = cache_.CreationTime.HasValue ? cache_.CreationTime.Value : DateTime.UtcNow; string insertQuery = "INSERT INTO " + sessions_table + " (beginstring, sendercompid, targetcompid, session_qualifier," + "creation_time, incoming_seqnum, outgoing_seqnum) VALUES(" + "'" + _sessionID.BeginString + "'," + "'" + _sessionID.SenderCompID + "'," + "'" + _sessionID.TargetCompID + "'," + "'" + _sessionID.SessionQualifier + "'," + "{ts '" + ODBCHelper.DateTimeToODBCConverter(createTime) + "'}," + cache_.GetNextTargetMsgSeqNum() + "," + cache_.GetNextSenderMsgSeqNum() + ")"; OdbcCommand cmdInsert = new OdbcCommand(insertQuery, odbc); if (0 == cmdInsert.ExecuteNonQuery()) { throw new ConfigError("Unable to create session in database"); } } } }
public void Reset() { string queryString = string.Empty; queryString = queryString + "DELETE from " + messages_table + " WHERE " + "beginstring=" + "'" + _sessionID.BeginString + "' and " + "sendercompid=" + "'" + _sessionID.SenderCompID + "' and " + "targetcompid=" + "'" + _sessionID.TargetCompID + "' and " + "session_qualifier=" + "'" + _sessionID.SessionQualifier + "'"; try { using (OdbcConnection odbc = GetODBCConnection()) { OdbcCommand cmdReset = new OdbcCommand(queryString, odbc); cmdReset.ExecuteNonQuery(); } } catch (Exception ex) { Console.WriteLine(ex.ToString()); } cache_.Reset(); DateTime?time = cache_.CreationTime; string sqlTime = ODBCHelper.DateTimeToODBCConverter(time.Value); queryString = "UPDATE " + sessions_table + " SET creation_time={ts '" + sqlTime + "'}, " + "incoming_seqnum=" + cache_.GetNextTargetMsgSeqNum() + ", " + "outgoing_seqnum=" + cache_.GetNextSenderMsgSeqNum() + " WHERE " + "beginstring=" + "'" + _sessionID.BeginString + "' and " + "sendercompid=" + "'" + _sessionID.SenderCompID + "' and " + "targetcompid=" + "'" + _sessionID.TargetCompID + "' and " + "session_qualifier=" + "'" + _sessionID.SessionQualifier + "'"; try { using (OdbcConnection odbc = GetODBCConnection()) { OdbcCommand cmdReset = new OdbcCommand(queryString, odbc); cmdReset.ExecuteNonQuery(); } } catch (Exception ex) { Console.WriteLine(ex.ToString()); } }
public void OnEvent(string s) { try { if (s.Contains("'")) { s = s.Replace("'", "''"); } string queryString = "INSERT INTO " + eventTable + " " + "(time, beginstring, sendercompid, targetcompid, session_qualifier, text) " + "VALUES (" + "'" + ODBCHelper.DateTimeToODBCConverter(DateTime.UtcNow) + "', " + "'" + _sessionID.BeginString + "', " + "'" + _sessionID.SenderCompID + "', " + "'" + _sessionID.TargetCompID + "', "; if (_sessionID.SessionQualifier != null && _sessionID.SessionQualifier.Length > 0) { queryString = queryString + "'" + _sessionID.SessionQualifier + "', "; } else { queryString = queryString + "'" + "NULL" + "', "; } queryString = queryString + "'" + s + "')"; try { using (OdbcConnection odbc = GetODBCConnection()) { OdbcCommand cmd = new OdbcCommand(queryString, odbc); cmd.ExecuteNonQuery(); } } catch (Exception ex) { Console.WriteLine(ex.Message); } } catch (Exception ex) { Console.WriteLine(ex.Message); } //throw new NotImplementedException(); }
public void OnOutgoing(string msg) { if (msg.Contains("'")) { msg = msg.Replace("'", "''"); } string queryString = "INSERT INTO "; queryString = queryString + outgoingTable + " " + "(time, beginstring, sendercompid, targetcompid, session_qualifier, text) " + "VALUES (" + "{ts '" + ODBCHelper.DateTimeToODBCConverter(DateTime.UtcNow) + "'},"; if (_sessionID != null) { queryString = queryString + "'" + _sessionID.BeginString + "'," + "'" + _sessionID.SenderCompID + "'," + "'" + _sessionID.TargetCompID + "',"; if (_sessionID.SessionQualifier != null && _sessionID.SessionQualifier.Length > 0) { queryString = queryString + "'" + _sessionID.SessionQualifier + "',"; } else { queryString = queryString + "NULL,"; } } else { queryString = queryString + "NULL, NULL, NULL, NULL, "; } queryString = queryString + "'" + msg + "')"; try { using (OdbcConnection odbc = GetODBCConnection()) { OdbcCommand cmd = new OdbcCommand(queryString, odbc); cmd.ExecuteNonQuery(); } } catch (Exception ex) { Console.WriteLine(ex.Message); } }
public void Backup(DateTime?DateThreshold = null) { // Get a 10 Second window DateTime bufferTime = DateTime.UtcNow - new TimeSpan(0, 0, 10); if (DateThreshold != null) { bufferTime = DateThreshold.GetValueOrDefault(bufferTime); } //UtcTimeStamp time; string sqlTime = ODBCHelper.DateTimeToODBCConverter(bufferTime); string queryStringIncoming = string.Empty; string queryStringOutgoing = string.Empty; string queryStringEvent = string.Empty; string whereClause = string.Empty; queryStringIncoming = "INSERT INTO " + incomingBackupTable + " " + "(time, beginstring, sendercompid, targetcompid, session_qualifier, text) " + "select time, beginstring, sendercompid, targetcompid, session_qualifier, text "; queryStringOutgoing = "INSERT INTO " + outgoingBackupTable + " " + "(time, beginstring, sendercompid, targetcompid, session_qualifier, text) " + "select time, beginstring, sendercompid, targetcompid, session_qualifier, text "; queryStringEvent = "INSERT INTO " + eventBackupTable + " " + "(time, beginstring, sendercompid, targetcompid, session_qualifier, text) " + "select time, beginstring, sendercompid, targetcompid, session_qualifier, text "; // Event Where Clause string eventWhereClause = string.Empty; eventWhereClause = " WHERE " + "beginstring = '" + _sessionID.BeginString + "' " + "AND sendercompid = '" + _sessionID.SenderCompID + "' " + "AND targetcompid = '" + _sessionID.TargetCompID + "' " + "AND time < '" + sqlTime + "' "; if (!string.IsNullOrEmpty(_sessionID.SessionQualifier)) { eventWhereClause = eventWhereClause + "AND session_qualifier = '" + _sessionID.SessionQualifier + "'"; } //GammaThree - Backup messages into history, but ignore Heartbeats not associated with TestRequests: whereClause = " WHERE " + "beginstring = '" + _sessionID.BeginString + "' " + "AND sendercompid = '" + _sessionID.SenderCompID + "' " + "AND targetcompid = '" + _sessionID.TargetCompID + "' " + "AND time < '" + sqlTime + "' "; if (!string.IsNullOrEmpty(_sessionID.SessionQualifier)) { whereClause = whereClause + "AND session_qualifier = '" + _sessionID.SessionQualifier + "'"; } string incomingQuery = string.Empty; string incomingClearQuery = string.Empty; string outgoingQuery = string.Empty; string outgoingClearQuery = string.Empty; // Only query the non heartbeats. incomingQuery = queryStringIncoming + "FROM " + incomingTable + whereClause + "AND NOT( text like '%' + char(01) + '35=0' + char(01) + '%')"; incomingClearQuery = "delete " + incomingTable + " " + whereClause; outgoingQuery = queryStringOutgoing + "FROM " + outgoingTable + whereClause + "AND NOT( text like '%' + char(01) + '35=0' + char(01) + '%')"; outgoingClearQuery = "delete " + outgoingClearQuery + " " + whereClause; try { using (OdbcConnection odbc = GetODBCConnection()) { OdbcCommand cmdIncoming = new OdbcCommand(incomingQuery, odbc); OdbcCommand cmdClearIncoming = new OdbcCommand(incomingClearQuery, odbc); OdbcCommand cmdOutgoing = new OdbcCommand(outgoingQuery, odbc); OdbcCommand cmdClearOutgoing = new OdbcCommand(outgoingClearQuery, odbc); cmdIncoming.ExecuteNonQuery(); cmdClearIncoming.ExecuteNonQuery(); // To prevent duplicates, check to see if incoming table is the same as outgoing. if (incomingTable != outgoingTable) { cmdOutgoing.ExecuteNonQuery(); cmdClearOutgoing.ExecuteNonQuery(); } // Backup to Event Log string logQuery = queryStringEvent + " from " + eventTable + " " + eventWhereClause; string logClearQuery = "delete " + eventTable + " " + eventWhereClause; OdbcCommand cmdEvent = new OdbcCommand(logQuery, odbc); OdbcCommand cmdClearEvent = new OdbcCommand(logClearQuery, odbc); cmdEvent.ExecuteNonQuery(); cmdClearEvent.ExecuteNonQuery(); } } catch (Exception ex) { Console.WriteLine(ex.Message); } }