Exemplo n.º 1
0
        public static async Task Save(QueryExecutionContext query, QueryResponseContext response)
        {
            string?statement = NormalizeSql(query.GetSqlStatement());

            if (statement is null)
            {
                return;
            }

            string sql;
            string code    = QueryHistory.GetCode(ComputeHash(statement), query.Server.Type);
            var    history = await LoadByCode(code);

            if (history is null)
            {
                history = QueryHistory.Create(code, statement, query, response);
                sql     = "INSERT INTO query (code, status, type, sql, star, execution_count, last_executed_on, last_environment, last_database) VALUES " +
                          "( " +
                          $"'{history.Code}', " +
                          $"{(int)history.Status}, " +
                          $"{(int)history.Type}, " +
                          $"'{history.Sql.Replace("'", "''")}', " +
                          $"{(history.Star ? 1 : 0)}, " +
                          $"{history.ExecutionCount}, " +
                          $"'{history.LastExecutedOn:yyyy-MM-dd HH:mm:ss}', " +
                          $"'{history.Stats.Last().Environment}', " +
                          $"'{history.Stats.Last().Database}' " +
                          ");";
            }
            else
            {
                history.UpdateStatistics(query, response);
                sql = $"UPDATE query SET " +
                      $"status = {(int)history.Status}, " +
                      $"execution_count = {history.ExecutionCount}, " +
                      $"last_executed_on = '{history.LastExecutedOn:yyyy-MM-dd HH:mm:ss}', " +
                      $"last_environment = '{history.Stats.Last().Environment}', " +
                      $"last_database = '{history.Stats.Last().Database}' " +
                      $"WHERE code = '{history.Code}';";
            }

            var stat = history.Stats.Last();

            sql += "INSERT INTO stat (code, status, executed_on, environment, database, server_connection, elapsed, row_count, records_affected) VALUES " +
                   "( " +
                   $"'{history.Code}', " +
                   $"{(int)stat.Status}, " +
                   $"'{stat.ExecutedOn:yyyy-MM-dd HH:mm:ss}', " +
                   $"'{stat.Environment}', " +
                   $"'{stat.Database}', " +
                   $"'{stat.ServerConnection}', " +
                   $"{stat.Elapsed}, " +
                   $"{stat.RowCount}, " +
                   $"{stat.RecordsAffected}" +
                   ");";

            await ServerConnection.ExecuteNonQuery(sql);
        }
Exemplo n.º 2
0
            public static async Task Save(QueryExecutionContext query, QueryResponseContext response)
            {
                string?statement = NormalizeSql(query.GetSqlStatement());

                if (statement is null)
                {
                    return;
                }

                string sql;
                string hash    = ComputeHash(statement);
                var    history = await LoadByHash(hash);

                if (history is null)
                {
                    history = QueryHistory.Create(statement, hash, query, response);
                    sql     = "INSERT INTO [data] (type, server_connection, sql, hash, executed_on, status, elapsed, row_count, records_affected, execution_count, star) VALUES " +
                              "( " +
                              $"{(int)history.Type}, " +
                              $"'{history.ServerConnection}', " +
                              $"'{history.Sql.Replace("'", "''")}', " +
                              $"'{history.Hash}', " +
                              $"'{history.ExecutedOn:yyyy-MM-dd HH:mm:ss}', " +
                              $"{(int)history.Status}, " +
                              $"{history.Elapsed}, " +
                              $"{history.RowCount}, " +
                              $"{history.RecordsAffected}, " +
                              $"{history.ExecutionCount}, " +
                              $"{(history.Star ? 1 : 0)}" +
                              ");";
                }
                else
                {
                    history.UpdateStatistics(query, response);
                    sql = "UPDATE [data] " +
                          $"SET server_connection = '{history.ServerConnection}', " +
                          $"executed_on = '{history.ExecutedOn:yyyy-MM-dd HH:mm:ss}', " +
                          $"status = {(int)history.Status}, " +
                          $"elapsed = {history.Elapsed}, " +
                          $"row_count = {history.RowCount}, " +
                          $"records_affected = {history.RecordsAffected}, " +
                          $"execution_count = {history.ExecutionCount} " +
                          $"WHERE id = {history.Id};";
                }

                await ServerConnection.ExecuteNonQuery(sql);
            }