-
Notifications
You must be signed in to change notification settings - Fork 0
/
insert_data.sh
69 lines (64 loc) · 2.77 KB
/
insert_data.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#! /bin/bash
if [[ $1 == "test" ]]
then
PSQL="psql --username=postgres --dbname=worldcuptest -t --no-align -c"
else
PSQL="psql --username=freecodecamp --dbname=worldcup -t --no-align -c"
fi
# Do not change code above this line. Use the PSQL variable above to query your database.
echo $($PSQL "TRUNCATE games,teams")
cat games.csv | while IFS="," read -r YEAR ROUND WINNER OPPONENT WINNER_GOALS OPPONENT_GOALS
do
if [[ $YEAR != "year" && $ROUND != "Round" && $WINNER != "Winner" && $OPPONENT != "Opponent" && $WINNER_GOALS != "winner_goals" && $OPPONENT_GOALS != "opponent_goals" ]]
then
# ---------------------------------------------------------------------------------------
# insert winner
# ---------------------------------------------------------------------------------------
# get winner team_id
WINNER_ID=$($PSQL "SELECT team_id FROM teams WHERE name='$WINNER'")
# if not found
if [[ -z $WINNER_ID ]]
then
# insert team
INSERT_WINNER=$($PSQL "INSERT INTO teams(name) VALUES('$WINNER')")
# if inserted success
if [[ $INSERT_WINNER == "INSERT 0 1" ]]
then
echo $INSERT_WINNER
# get new team_id
WINNER_ID=$($PSQL "SELECT team_id FROM teams WHERE name='$WINNER'")
WINNER_NAME=$($PSQL "SELECT name FROM teams WHERE team_id='$WINNER_ID'")
echo "Insert success : ${WINNER_NAME}"
fi
fi
# ---------------------------------------------------------------------------------------
# insert opponent
# ---------------------------------------------------------------------------------------
# get winner team_id
OPPONENT_ID=$($PSQL "SELECT team_id FROM teams WHERE name='$OPPONENT'")
# if not found
if [[ -z $OPPONENT_ID ]]
then
# insert team
INSERT_OPPONENT=$($PSQL "INSERT INTO teams(name) VALUES('$OPPONENT')")
# if inserted success
if [[ $INSERT_OPPONENT == "INSERT 0 1" ]]
then
echo $INSERT_OPPONENT
# get new team_id
OPPONENT_ID=$($PSQL "SELECT team_id FROM teams WHERE name='$OPPONENT'")
OPPONENT_NAME=$($PSQL "SELECT name FROM teams WHERE team_id='$OPPONENT_ID'")
echo "Insert success : ${OPPONENT_NAME}"
fi
fi
# ---------------------------------------------------------------------------------------
# insert games
# ---------------------------------------------------------------------------------------
INSERT_GAMES=$($PSQL "INSERT INTO games(year,round,winner_id,opponent_id,winner_goals,opponent_goals) VALUES($YEAR,'$ROUND',$WINNER_ID,$OPPONENT_ID,$WINNER_GOALS, $OPPONENT_GOALS)")
if [[ $INSERT_GAMES == "INSERT 0 1" ]]
then
echo $INSERT_GAMES
echo "Insert success : $YEAR, $ROUND, $WINNER_ID, $OPPONENT_ID, $WINNER_GOALS, $OPPONENT_GOALS"
fi
fi
done