-
Notifications
You must be signed in to change notification settings - Fork 0
/
insert_data.sh
65 lines (58 loc) · 1.93 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
#! /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.
#! /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.
cat games.csv | while IFS="," read YEAR ROUND WINNER OPPONENT WINNER_GOALS OPPONENT_GOALS
do
# ----ADDING TEAMS----
if [[ $WINNER != "winner" ]]
then
#get team name from winner
TEAM_FROM_WINNER=$($PSQL "SELECT name FROM teams WHERE name='$WINNER'")
echo $TEAM_FROM_WINNER
#if not found
if [[ -z $TEAM_FROM_WINNER ]]
then
#insert to teams
INSERT_WINNER_TEAM=$($PSQL "INSERT INTO teams(name) VALUES('$WINNER')")
if [[ $INSERT_WINNER_TEAM == "INSERT 0 1" ]]
then
echo Inserted into teams, $INSERT_WINNER_TEAM
fi
fi
fi
if [[ $OPPONENT != "opponent" ]]
then
#get team name from winner
TEAM_FROM_OPPONENT=$($PSQL "SELECT name FROM teams WHERE name='$OPPONENT'")
#if not found
if [[ -z $TEAM_FROM_OPPONENT ]]
then
#insert to teams
INSERT_OPPONENT_TEAM=$($PSQL "INSERT INTO teams(name) VALUES('$OPPONENT')")
if [[ $INSERT_OPPONENT_TEAM == "INSERT 0 1" ]]
then
echo Inserted into teams, $INSERT_OPPONENT_TEAM
fi
fi
fi
#insert data to games
#winner_id
winner_id=$($PSQL "select team_id from teams where name = '$WINNER'")
#opponent_id
opponent_id=$($PSQL "select team_id from teams where name = '$OPPONENT'")
#insert
insert_sql=$($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);")
done