Advertisement
Guest User

Untitled

a guest
Nov 13th, 2019
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 13.64 KB | None | 0 0
  1. public static void ExecuteTransaction1()
  2.         {
  3.             var command =
  4.     "SELECT te.team_id, te.team_name, te.description, SUM(bu.amount) AS team_budget, json_agg(CONCAT(us.first_name, ' ', us.nick, ' ', us.last_name)) as users_in_team, " +
  5.     "sc.score_amount AS team_score, COUNT(ta.task_id) AS tasks_amount FROM teams te " +
  6.     "INNER JOIN users us ON us.team_id = te.team_id " +
  7.     "INNER JOIN tasks ta ON ta.user_id = us.user_id " +
  8.     "INNER JOIN budget bu ON bu.budget_id = ta.budget_id " +
  9.     "INNER JOIN scores sc ON sc.team_id = te.team_id " +
  10.     "WHERE LOWER(te.team_name) LIKE '%a%' " +
  11.     "GROUP BY te.team_id, te.team_name, te.description, sc.score_amount " +
  12.     "ORDER BY sc.score_amount";
  13.         }
  14.  
  15.         /// <summary>
  16.         /// ExecuteTransaction2
  17.         /// </summary>
  18.         public static void ExecuteTransaction2()
  19.         {
  20.             var command =
  21.     "SELECT pr.project_id, pr.project_name, pr.project_tag, json_agg(ta.task_name), COUNT(ta.task_id) as tasks_amount, (pr.deadline_date - current_date) AS days_left, " +
  22.     "pr.deadline_date, current_date , pr.modification_date " +
  23.     "FROM projects pr " +
  24.     "JOIN tasks ta on ta.project_id = pr.project_id " +
  25.     "WHERE pr.create_date BETWEEN '01.01.2000' AND '01.01.2019' " +
  26.     "AND pr.deadline_date BETWEEN '01.01.2000' AND '01.01.2019' " +
  27.     "AND pr.project_name LIKE '%File%' " +
  28.     "GROUP BY pr.project_id, pr.project_name, pr.project_tag " +
  29.     "ORDER BY pr.modification_date";
  30.         }
  31.  
  32.         /// <summary>
  33.         /// ExecuteTransaction3
  34.         /// </summary>
  35.         public static void ExecuteTransaction3()
  36.         {
  37.             int worstUserId = 0, worstTeamId = 0;
  38.             int bestUserId = 0, bestTeamId = 0;
  39.             string messagesIds = "";
  40.             NpgsqlConnection conn = new NpgsqlConnection(connectionString);
  41.             conn.Open();
  42.             var transaction = conn.BeginTransaction();
  43.             //----------------------------------------------------------------------------------------
  44.             var command =
  45.                 "SELECT us.user_id, us.team_id, sc.score_amount, COUNT(ta.task_id) AS tasks_amount FROM users us " +
  46.                 "INNER JOIN tasks ta ON ta.user_id = us.user_id " +
  47.                 "INNER JOIN scores sc ON sc.team_id = us.team_id " +
  48.                 "GROUP by us.user_id, sc.score_amount " +
  49.                 "ORDER BY tasks_amount " +
  50.                 "LIMIT 1 ";
  51.  
  52.             NpgsqlCommand npgsqlCommmand = new NpgsqlCommand(command, conn, transaction);
  53.             var reader = npgsqlCommmand.ExecuteReader();
  54.             while (reader.Read())
  55.             {
  56.                 worstUserId = reader.GetInt32(0);
  57.                 worstTeamId = reader.GetInt32(1);
  58.             }
  59.             reader.Close();
  60.             npgsqlCommmand.Dispose();
  61.             //----------------------------------------------------------------------------------------
  62.             command = "" +
  63.             "SELECT us.user_id, us.team_id, sc.score_amount, COUNT(ta.task_id) AS tasks_amount FROM users us " +
  64.             "INNER JOIN tasks ta ON ta.user_id = us.user_id " +
  65.             "INNER JOIN scores sc ON sc.team_id = us.team_id " +
  66.             "GROUP by us.user_id, sc.score_amount " +
  67.             "ORDER BY tasks_amount DESC " +
  68.             "LIMIT 1 ";
  69.             npgsqlCommmand = new NpgsqlCommand(command, conn, transaction);
  70.             reader = npgsqlCommmand.ExecuteReader();
  71.             while (reader.Read())
  72.             {
  73.                 bestUserId = reader.GetInt32(0);
  74.                 bestTeamId = reader.GetInt32(1);
  75.             }
  76.             reader.Close();
  77.             npgsqlCommmand.Dispose();
  78.             //----------------------------------------------------------------------------------------
  79.             command = "UPDATE users set first_name = 'edited', last_name = 'edited', nick = 'edited', modification_date = NOW() WHERE user_id = @user_id";
  80.             npgsqlCommmand = new NpgsqlCommand(command, conn, transaction);
  81.             npgsqlCommmand.Parameters.AddWithValue("user_id", worstUserId);
  82.             npgsqlCommmand.ExecuteScalar();
  83.             npgsqlCommmand.Dispose();
  84.             //----------------------------------------------------------------------------------------
  85.             command = "UPDATE scores set score_amount = (score_amount / 2) WHERE team_id = @team_id";
  86.             npgsqlCommmand = new NpgsqlCommand(command, conn, transaction);
  87.             npgsqlCommmand.Parameters.AddWithValue("team_id", worstTeamId);
  88.             npgsqlCommmand.ExecuteScalar();
  89.             npgsqlCommmand.Dispose();
  90.             //----------------------------------------------------------------------------------------
  91.             command = "UPDATE tasks SET user_id = NULL where user_id = @user_id RETURNING message_id";
  92.             npgsqlCommmand = new NpgsqlCommand(command, conn, transaction);
  93.             npgsqlCommmand.Parameters.AddWithValue("user_id", worstUserId);
  94.             reader = npgsqlCommmand.ExecuteReader();
  95.             while (reader.Read())
  96.             {
  97.                 messagesIds += reader.GetInt32(0) + ",";
  98.             }
  99.             messagesIds = messagesIds.Remove(messagesIds.Length - 1);
  100.             reader.Close();
  101.             npgsqlCommmand.Dispose();
  102.             //----------------------------------------------------------------------------------------
  103.             command = "UPDATE messages set text = 'This task has been unpined' WHERE message_id IN ({0})";
  104.             npgsqlCommmand = new NpgsqlCommand(String.Format(command, messagesIds), conn, transaction);
  105.             npgsqlCommmand.ExecuteScalar();
  106.             npgsqlCommmand.Dispose();
  107.             //----------------------------------------------------------------------------------------
  108.             command = "UPDATE scores set score_amount = (score_amount * 2) WHERE team_id = @team_id";
  109.             npgsqlCommmand = new NpgsqlCommand(command, conn, transaction);
  110.             npgsqlCommmand.Parameters.AddWithValue("team_id", bestTeamId);
  111.             npgsqlCommmand.ExecuteScalar();
  112.             npgsqlCommmand.Dispose();
  113.             //----------------------------------------------------------------------------------------
  114.             transaction.Rollback();
  115.             conn.Close();
  116.         }
  117.  
  118.         /// <summary>
  119.         /// ExecuteTransaction4
  120.         /// </summary>
  121.         public static void ExecuteTransaction4(int projectId)
  122.         {
  123.             string tasksIds = "", messagesIds = "", budgetIds = "";
  124.             double extraBudget = 0;
  125.             NpgsqlConnection conn = new NpgsqlConnection(connectionString);
  126.             conn.Open();
  127.             var transaction = conn.BeginTransaction();
  128.             //----------------------------------------------------------------------------------------
  129.             var command = "DELETE FROM projects WHERE project_id = @project_id";
  130.             NpgsqlCommand npgsqlCommmand = new NpgsqlCommand(command, conn, transaction);
  131.             npgsqlCommmand.Parameters.AddWithValue("project_id", projectId);
  132.             npgsqlCommmand.ExecuteScalar();
  133.             npgsqlCommmand.Dispose();
  134.             //----------------------------------------------------------------------------------------
  135.             command = "DELETE FROM tasks WHERE project_id = @project_id RETURNING task_id, message_id, budget_id";
  136.             npgsqlCommmand = new NpgsqlCommand(command, conn, transaction);
  137.             var reader = npgsqlCommmand.ExecuteReader();
  138.             while (reader.Read())
  139.             {
  140.                 tasksIds += reader.GetInt32(0) + ",";
  141.                 messagesIds += reader.GetInt32(1) + ",";
  142.                 budgetIds += reader.GetInt32(2) + ",";
  143.             }
  144.             tasksIds = tasksIds.Remove(tasksIds.Length - 1);
  145.             messagesIds = messagesIds.Remove(messagesIds.Length - 1);
  146.             budgetIds = budgetIds.Remove(budgetIds.Length - 1);
  147.             reader.Close();
  148.             npgsqlCommmand.Dispose();
  149.             //----------------------------------------------------------------------------------------
  150.             command = "DELETE FROM messages WHERE message_id IN ({0})";
  151.             npgsqlCommmand = new NpgsqlCommand(String.Format(command, messagesIds), conn, transaction);
  152.             npgsqlCommmand.ExecuteScalar();
  153.             npgsqlCommmand.Dispose();
  154.             //----------------------------------------------------------------------------------------
  155.             command = "SELECT  ((SELECT budget_sum FROM " +
  156.                 "(SELECT SUM(amount) AS budget_sum FROM budget bu INNER JOIN tasks ta ON ta.budget_id = bu.budget_id WHERE task_id in ({0}) ) budget_sum) / " +
  157.                 "(SELECT tasks_amount FROM(SELECT COUNT(task_id) AS tasks_amount FROM tasks) tasks_amount) ) AS extra_budget";
  158.             npgsqlCommmand = new NpgsqlCommand(String.Format(command, tasksIds), conn, transaction);
  159.             reader = npgsqlCommmand.ExecuteReader();
  160.             while (reader.Read())
  161.             {
  162.                 extraBudget = 2.71;
  163.             }
  164.             reader.Close();
  165.             npgsqlCommmand.Dispose();
  166.             //----------------------------------------------------------------------------------------
  167.             command = "UPDATE budget SET amount = amount + @extra_budget";
  168.             npgsqlCommmand = new NpgsqlCommand(command, conn, transaction);
  169.             npgsqlCommmand.Parameters.AddWithValue("extra_budget", extraBudget);
  170.             npgsqlCommmand.ExecuteScalar();
  171.             npgsqlCommmand.Dispose();
  172.             //----------------------------------------------------------------------------------------
  173.             command = "DELETE FROM budget WHERE budget_id IN ({0})";
  174.             npgsqlCommmand = new NpgsqlCommand(String.Format(command, budgetIds), conn, transaction);
  175.             npgsqlCommmand.ExecuteScalar();
  176.             npgsqlCommmand.Dispose();
  177.             //----------------------------------------------------------------------------------------
  178.             transaction.Rollback();
  179.             conn.Close();
  180.         }
  181.  
  182.         /// <summary>
  183.         /// ExecuteTransaction5
  184.         /// </summary>
  185.         public static void ExecuteTransaction5()
  186.         {
  187.             int roleId1 = 0, roleId2 = 0, roleId3 = 0;
  188.             int userId1 = 0, userId2 = 0, userId3 = 0;
  189.             int teamId = 0;
  190.             NpgsqlConnection conn = new NpgsqlConnection(connectionString);
  191.             conn.Open();
  192.             var transaction = conn.BeginTransaction();
  193.             //----------------------------------------------------------------------------------------
  194.             var command = "INSERT INTO teams(team_name, description, leader_id) VALUES('new_team', 'new_team description', 0) RETURNING team_id";
  195.  
  196.             NpgsqlCommand npgsqlCommmand = new NpgsqlCommand(command, conn, transaction);
  197.             var reader = npgsqlCommmand.ExecuteReader();
  198.             while (reader.Read())
  199.             {
  200.                 teamId = reader.GetInt32(0);
  201.             }
  202.             reader.Close();
  203.             npgsqlCommmand.Dispose();
  204.             //----------------------------------------------------------------------------------------
  205.             command = "INSERT INTO roles(role_name) VALUES('user1_role') RETURNING role_id;";
  206.             npgsqlCommmand = new NpgsqlCommand(command, conn, transaction);
  207.             reader = npgsqlCommmand.ExecuteReader();
  208.             while (reader.Read())
  209.             {
  210.                 roleId1 = reader.GetInt32(0);
  211.             }
  212.             reader.Close();
  213.             npgsqlCommmand.Dispose();
  214.             //----------------------------------------------------------------------------------------
  215.             command = "INSERT INTO users(first_name, last_name, nick, role_id, team_id, create_date, modification_date, is_active) " +
  216.                 "VALUES('user1_first_name', 'user1_last_name', 'user1_nick', @role_id, @team_id, NOW(), NOW(), true) RETURNING user_id; ";
  217.             npgsqlCommmand = new NpgsqlCommand(command, conn, transaction);
  218.             npgsqlCommmand.Parameters.AddWithValue("role_id", roleId1);
  219.             npgsqlCommmand.Parameters.AddWithValue("team_id", teamId);
  220.             reader = npgsqlCommmand.ExecuteReader();
  221.             while (reader.Read())
  222.             {
  223.                 userId1 = reader.GetInt32(0);
  224.             }
  225.             reader.Close();
  226.             npgsqlCommmand.Dispose();
  227.             //----------------------------------------------------------------------------------------
  228.             command = "INSERT INTO roles(role_name) VALUES('user2_role') RETURNING role_id;";
  229.             npgsqlCommmand = new NpgsqlCommand(command, conn, transaction);
  230.             reader = npgsqlCommmand.ExecuteReader();
  231.             while (reader.Read())
  232.             {
  233.                 roleId2 = reader.GetInt32(0);
  234.             }
  235.             reader.Close();
  236.             npgsqlCommmand.Dispose();
  237.             //----------------------------------------------------------------------------------------
  238.             command = "INSERT INTO users(first_name, last_name, nick, role_id, team_id, create_date, modification_date, is_active) " +
  239.                 "VALUES('user2_first_name', 'user2_last_name', 'user2_nick', @role_id, @team_id, NOW(), NOW(), true) RETURNING user_id; ";
  240.             npgsqlCommmand = new NpgsqlCommand(command, conn, transaction);
  241.             npgsqlCommmand.Parameters.AddWithValue("role_id", roleId2);
  242.             npgsqlCommmand.Parameters.AddWithValue("team_id", teamId);
  243.             reader = npgsqlCommmand.ExecuteReader();
  244.             while (reader.Read())
  245.             {
  246.                 userId2 = reader.GetInt32(0);
  247.             }
  248.             reader.Close();
  249.             npgsqlCommmand.Dispose();
  250.             //----------------------------------------------------------------------------------------
  251.             command = "INSERT INTO roles(role_name) VALUES('user3_role') RETURNING role_id; ";
  252.             npgsqlCommmand = new NpgsqlCommand(command, conn, transaction);
  253.             reader = npgsqlCommmand.ExecuteReader();
  254.             while (reader.Read())
  255.             {
  256.                 roleId3 = reader.GetInt32(0);
  257.             }
  258.             reader.Close();
  259.             npgsqlCommmand.Dispose();
  260.             //----------------------------------------------------------------------------------------
  261.             command = "INSERT INTO users(first_name, last_name, nick, role_id, team_id, create_date, modification_date, is_active) " +
  262.                 "VALUES('user3_first_name', 'user3_last_name', 'user3_nick', @role_id, @team_id, NOW(), NOW(), true) RETURNING user_id; ";
  263.             npgsqlCommmand = new NpgsqlCommand(command, conn, transaction);
  264.             npgsqlCommmand.Parameters.AddWithValue("role_id", roleId3);
  265.             npgsqlCommmand.Parameters.AddWithValue("team_id", teamId);
  266.             reader = npgsqlCommmand.ExecuteReader();
  267.             while (reader.Read())
  268.             {
  269.                 userId3 = reader.GetInt32(0);
  270.             }
  271.             reader.Close();
  272.             npgsqlCommmand.Dispose();
  273.             //----------------------------------------------------------------------------------------
  274.             command = "UPDATE teams SET leader_id = @user_id WHERE team_id = @team_id";
  275.             npgsqlCommmand = new NpgsqlCommand(command, conn, transaction);
  276.             npgsqlCommmand.Parameters.AddWithValue("user_id", userId1);
  277.             npgsqlCommmand.Parameters.AddWithValue("team_id", teamId);
  278.             npgsqlCommmand.ExecuteScalar();
  279.             npgsqlCommmand.Dispose();
  280.             //----------------------------------------------------------------------------------------
  281.             command = "INSERT INTO scores(team_id, score_amount, score_mark) VALUES(@team_id, 0.5, 'A');";
  282.             npgsqlCommmand = new NpgsqlCommand(command, conn, transaction);
  283.             npgsqlCommmand.Parameters.AddWithValue("team_id", teamId);
  284.             npgsqlCommmand.ExecuteScalar();
  285.             npgsqlCommmand.Dispose();
  286.             //----------------------------------------------------------------------------------------
  287.             transaction.Rollback();
  288.             conn.Close();
  289.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement