Esempio n. 1
0
        public void GetInt64OrDefault_Test()
        {
            string connectionString = SQLiteExtensions.GetConnectionString(_sampleDatabaseFile);

            string queryText = String.Format(
                "Select\n" +
                "    null AS ConnectId,\n" +
                "    777 AS Session\n" +
                "From\n" +
                "    EventLog el\n" +
                "Where RowID > {0}\n" +
                "Order By rowID\n" +
                "Limit {1}\n", 0, 1);

            long connectionId = 0;
            long sessionId    = 0;

            using (SQLiteConnection connection = new SQLiteConnection(connectionString))
            {
                connection.Open();
                using SQLiteCommand command   = new SQLiteCommand(queryText, connection);
                using SQLiteDataReader reader = command.ExecuteReader();
                if (reader.Read())
                {
                    connectionId = SQLiteExtensions.GetInt64OrDefault(reader, 0);
                    sessionId    = SQLiteExtensions.GetInt64OrDefault(reader, 1);
                }
            }
            Assert.Equal(0, connectionId);
            Assert.Equal(777, sessionId);
        }
Esempio n. 2
0
        public void GetStringOrDefault_Test()
        {
            string connectionString = SQLiteExtensions.GetConnectionString(_sampleDatabaseFile);

            string queryText = string.Format(
                "Select\n" +
                "    \"Hello, world!\" AS DataPresentation,\n" +
                "    null AS DataPresentationEmpty\n" +
                "From\n" +
                "    EventLog el\n" +
                "Where RowID > {0}\n" +
                "Order By rowID\n" +
                "Limit {1}\n", 0, 1);

            string dataPresentation      = null;
            string dataPresentationEmpty = null;

            using (SQLiteConnection connection = new SQLiteConnection(connectionString))
            {
                connection.Open();
                using SQLiteCommand command = new SQLiteCommand(queryText, connection);
                SQLiteDataReader reader = command.ExecuteReader();
                if (reader.Read())
                {
                    dataPresentation      = SQLiteExtensions.GetStringOrDefault(reader, 0);
                    dataPresentationEmpty = SQLiteExtensions.GetStringOrDefault(reader, 1);
                }
            }

            Assert.Equal("Hello, world!", dataPresentation);
            Assert.Equal(String.Empty, dataPresentationEmpty);
        }
Esempio n. 3
0
        public void GetConnectionString_Test()
        {
            string connectionString = SQLiteExtensions.GetConnectionString(_sampleDatabaseFile);

            string queryText = String.Format(
                "Select\n" +
                "    COUNT(el.RowId) CNT\n" +
                "From\n" +
                "    EventLog el\n");

            long rowsCount = 0;

            using (SQLiteConnection connection = new SQLiteConnection(connectionString))
            {
                connection.Open();
                using SQLiteCommand command = new SQLiteCommand(queryText, connection);
                SQLiteDataReader reader = command.ExecuteReader();
                if (reader.Read())
                {
                    object rowsCountObject = reader.GetValue(0);
                    if (rowsCountObject is long)
                    {
                        rowsCount = Convert.ToInt64(rowsCountObject);
                    }
                }
            }

            Assert.NotEqual(0, rowsCount);
        }
Esempio n. 4
0
        public void GetRowAsString_Test()
        {
            string connectionString = SQLiteExtensions.GetConnectionString(_sampleDatabaseFile);

            string queryText = String.Format(
                "Select\n" +
                "    el.RowId AS RowId,\n" +
                "    el.Date AS Date,\n" +
                "    el.ConnectId AS ConnectId,\n" +
                "    el.Session AS Session\n" +
                "From\n" +
                "    EventLog el\n" +
                "Where RowID > {0}\n" +
                "Order By rowID\n" +
                "Limit {1}\n", 0, 1);

            string rowAsString = String.Empty;

            using (SQLiteConnection connection = new SQLiteConnection(connectionString))
            {
                connection.Open();
                using SQLiteCommand command = new SQLiteCommand(queryText, connection);
                SQLiteDataReader reader = command.ExecuteReader();
                if (reader.Read())
                {
                    rowAsString = reader.GetRowAsString();
                }
            }
            int countLines = rowAsString.Split('\n').Where(str => str != string.Empty).Count();

            Assert.NotEqual(String.Empty, rowAsString);
            Assert.Equal(4, countLines);
            Assert.Contains("RowId", rowAsString);
            Assert.Contains("Date", rowAsString);
            Assert.Contains("ConnectId", rowAsString);
            Assert.Contains("Session", rowAsString);
        }
Esempio n. 5
0
        private void ReadRefferences_IfChanged_Test(string eventLogPath)
        {
            DateTime lastReadReferencesDateBeforeRead = DateTime.MinValue;
            DateTime lastReadReferencesDate           = DateTime.MinValue;

            using (EventLogReader reader = EventLogReader.CreateReader(eventLogPath))
            {
                lastReadReferencesDateBeforeRead = reader.ReferencesReadDate;
                Thread.Sleep(2000);

                if (reader is EventLogLGFReader)
                {
                    #region LGF

                    EventLogLGFReader lgfReader = (EventLogLGFReader)reader;
                    using (StreamWriter sw = File.AppendText(lgfReader.CurrentFile))
                    {
                        string   descriptionNewEvent        = "Новое событие в процессе чтения!";
                        DateTime newLogRecordPeriod         = DateTime.Now;
                        string   newLogRecordPeriodAsString = newLogRecordPeriod.ToString("yyyyMMddHHmmss");

                        sw.WriteLine(",");
                        sw.WriteLine($"{{{newLogRecordPeriodAsString},N,");
                        sw.WriteLine($"{{0,0}},1,1,2,2,3,N,\"{descriptionNewEvent}\",3,");
                        sw.WriteLine($"{{\"S\",\"{descriptionNewEvent}\"}},\"\",1,1,0,2,0,");
                        sw.WriteLine("{0}");
                        sw.WriteLine("}");
                    }

                    #endregion
                }
                else if (reader is EventLogLGDReader)
                {
                    #region LGD

                    string lgdConnectionString = SQLiteExtensions.GetConnectionString(eventLogPath, false);
                    using (SQLiteConnection connection = new SQLiteConnection(lgdConnectionString))
                    {
                        connection.Open();
                        string queryText = String.Format(
                            "Select\n" +
                            "    el.RowId,\n" +
                            "    el.Date AS Date,\n" +
                            "    el.ConnectId,\n" +
                            "    el.Session,\n" +
                            "    el.TransactionStatus,\n" +
                            "    el.TransactionDate,\n" +
                            "    el.TransactionId,\n" +
                            "    el.UserCode AS UserCode,\n" +
                            "    el.ComputerCode AS ComputerCode,\n" +
                            "    el.appCode AS ApplicationCode,\n" +
                            "    el.eventCode AS EventCode,\n" +
                            "    el.primaryPortCode AS PrimaryPortCode,\n" +
                            "    el.secondaryPortCode AS SecondaryPortCode,\n" +
                            "    el.workServerCode AS WorkServerCode,\n" +
                            "    el.Severity AS SeverityCode,\n" +
                            "    el.Comment AS Comment,\n" +
                            "    el.Data AS Data,\n" +
                            "    el.DataPresentation AS DataPresentation,\n" +
                            "    elm.metadataCode AS MetadataCode\n" +
                            "From\n" +
                            "    EventLog el\n" +
                            "    left join EventLogMetadata elm on el.RowId = elm.eventLogID\n" +
                            "    left join MetadataCodes mc on elm.metadataCode = mc.code\n" +
                            "Where RowID = (SELECT MAX(RowID) from EventLog)\n");
                        using (SQLiteCommand sqliteCmd = new SQLiteCommand(queryText, connection))
                        {
                            long RowID = 0, ConnectId = 0, Session = 0,
                                 TransactionStatus = 0, TransactionDate = 0, TransactionId = 0,
                                 User = 0, Computer = 0, Application = 0, Event = 0, PrimaryPort = 0,
                                 SecondaryPort = 0, WorkServer = 0, Severity = 0, Metadata = 0;
                            string Comment = string.Empty, Data = string.Empty, DataPresentation = string.Empty;

                            using (SQLiteDataReader sqliteReader = sqliteCmd.ExecuteReader())
                            {
                                while (sqliteReader.Read())
                                {
                                    RowID             = sqliteReader.GetInt64OrDefault(0);
                                    ConnectId         = sqliteReader.GetInt64OrDefault(2);
                                    Session           = sqliteReader.GetInt64OrDefault(3);
                                    TransactionStatus = sqliteReader.GetInt64OrDefault(4);
                                    TransactionDate   = sqliteReader.GetInt64OrDefault(5);
                                    TransactionId     = sqliteReader.GetInt64OrDefault(6);
                                    User             = sqliteReader.GetInt64OrDefault(7);
                                    Computer         = sqliteReader.GetInt64OrDefault(8);
                                    Application      = sqliteReader.GetInt64OrDefault(9);
                                    Event            = sqliteReader.GetInt64OrDefault(10);
                                    PrimaryPort      = sqliteReader.GetInt64OrDefault(11);
                                    SecondaryPort    = sqliteReader.GetInt64OrDefault(12);
                                    WorkServer       = sqliteReader.GetInt64OrDefault(13);
                                    Severity         = sqliteReader.GetInt64OrDefault(14);
                                    Comment          = sqliteReader.GetStringOrDefault(15);
                                    Data             = sqliteReader.GetStringOrDefault(16);
                                    DataPresentation = sqliteReader.GetStringOrDefault(17);
                                    Metadata         = sqliteReader.GetInt64OrDefault(18);
                                }
                            }

                            string queryInsertLog =
                                "INSERT INTO EventLog " +
                                "(" +
                                "   RowId, " +
                                "   Date, " +
                                "   ConnectId, " +
                                "   Session, " +
                                "   TransactionStatus, " +
                                "   TransactionDate, " +
                                "   TransactionId, " +
                                "   UserCode, " +
                                "   ComputerCode, " +
                                "   appCode, " +
                                "   eventCode, " +
                                "   primaryPortCode, " +
                                "   secondaryPortCode, " +
                                "   workServerCode, " +
                                "   Severity, " +
                                "   Comment, " +
                                "   Data, " +
                                "   DataPresentation " +
                                ") " +
                                "VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
                            using (SQLiteCommand insertSQL = new SQLiteCommand(queryInsertLog, connection))
                            {
                                long newRowId  = RowID + 1;
                                long newPeriod = DateTime.Now.ToLongDateTimeFormat();

                                insertSQL.Parameters.Add(new SQLiteParameter(DbType.Int64, newRowId));
                                insertSQL.Parameters.Add(new SQLiteParameter(DbType.Int64, newPeriod));
                                insertSQL.Parameters.Add(new SQLiteParameter(DbType.Int64, ConnectId));
                                insertSQL.Parameters.Add(new SQLiteParameter(DbType.Int64, Session));
                                insertSQL.Parameters.Add(new SQLiteParameter(DbType.Int64, TransactionStatus));
                                insertSQL.Parameters.Add(new SQLiteParameter(DbType.Int64, TransactionDate));
                                insertSQL.Parameters.Add(new SQLiteParameter(DbType.Int64, TransactionId));
                                insertSQL.Parameters.Add(new SQLiteParameter(DbType.Int64, User));
                                insertSQL.Parameters.Add(new SQLiteParameter(DbType.Int64, Computer));
                                insertSQL.Parameters.Add(new SQLiteParameter(DbType.Int64, Application));
                                insertSQL.Parameters.Add(new SQLiteParameter(DbType.Int64, Event));
                                insertSQL.Parameters.Add(new SQLiteParameter(DbType.Int64, PrimaryPort));
                                insertSQL.Parameters.Add(new SQLiteParameter(DbType.Int64, SecondaryPort));
                                insertSQL.Parameters.Add(new SQLiteParameter(DbType.Int64, WorkServer));
                                insertSQL.Parameters.Add(new SQLiteParameter(DbType.Int64, Severity));
                                insertSQL.Parameters.Add(new SQLiteParameter(DbType.String, Comment));
                                insertSQL.Parameters.Add(new SQLiteParameter(DbType.String, Data));
                                insertSQL.Parameters.Add(new SQLiteParameter(DbType.String, DataPresentation));
                                insertSQL.ExecuteNonQuery();
                            }
                        }
                    }

                    #endregion
                }

                while (reader.Read())
                {
                    ;
                }
                lastReadReferencesDate = reader.ReferencesReadDate;
            }

            Assert.NotEqual(DateTime.MinValue, lastReadReferencesDate);
            Assert.NotEqual(DateTime.MinValue, lastReadReferencesDateBeforeRead);
            Assert.True(lastReadReferencesDateBeforeRead < lastReadReferencesDate);
        }