Improved some SQL calls.

master
Matt Theissen 4 years ago
parent ca5b6dbae1
commit 5f5bff1459

@ -119,14 +119,14 @@ do
LAST_USER=$with_user
## Not sure why this was here...
#echo "$with_user" | awk 'FNR == 1 {print}'
# Grabs the current amount the user has stored in the database.
with_1_amt=$($db_conn "select id,username,balance from account join balance using (id) where username='$with_user';"| awk 'FNR == 3' | awk -F\| '{print $3}')
# This grabs the user's ID to use on the balance table.
with_user_id=$($db_conn "select id,username,balance from account join balance using (id) where username='$with_user';" | awk 'FNR == 3' | awk -F\| '{print $1}')
# Does the math to minus the withdraw.
with_new_amt=$($db_conn "select balance - '$with_amt' from balance where id = '$with_user_id';" | awk 'FNR ==3')
# Updates the database with the new amount.
$db_conn "update balance set balance = '$with_new_amt' where id = '$with_user_id';"
$db_conn "update balance set balance = balance - '$with_amt' where id = '$with_user_id';"
# Transactioning
$db_conn "insert into transaction (id,username,amount,timestamp) values ('$with_user_id','$with_user','-$with_amt','now');" ;;
2 ) read -p "Deposit to user: " depo_user
@ -135,10 +135,8 @@ do
depo_1_amt=$($db_conn "select id,username,balance from account join balance using (id) where username='$depo_user';"| awk 'FNR == 3' | awk -F\| '{print $3}')
# This grabs the user's ID to use on the balance table.
depo_user_id=$($db_conn "select id,username,balance from account join balance using (id) where username='$depo_user';" | awk 'FNR == 3' | awk -F\| '{print $1}')
# Does the math to add the withdraw.
depo_new_amt=$($db_conn "select balance + '$depo_amt' from balance where id = '$depo_user_id';" | awk 'FNR ==3')
# Updates the database with the new amount.
$db_conn "update balance set balance = '$depo_new_amt' where id = '$depo_user_id';"
$db_conn "update balance set balance = balance + '$depo_amt' where id = '$depo_user_id';"
# Transactioning
$db_conn "insert into transaction (id,username,amount,timestamp) values ('$depo_user_id','$depo_user','+$depo_amt','now');" ;;
3 ) read -p "Transfer from user: " tran_user1
@ -147,12 +145,9 @@ do
# Grabs user IDs.
tran_user1_id=$($db_conn "select id,username,balance from account join balance using (id) where username='$tran_user1';" | awk 'FNR == 3' | awk -F\| '{print $1}')
tran_user2_id=$($db_conn "select id,username,balance from account join balance using (id) where username='$tran_user2';" | awk 'FNR == 3' | awk -F\| '{print $1}')
# Does the math to complete the transfer.
tran1_new_amt=$($db_conn "select balance - '$tran_amt' from balance where id = '$tran_user1_id';" | awk 'FNR ==3')
tran2_new_amt=$($db_conn "select balance + '$tran_amt' from balance where id = '$tran_user2_id';" | awk 'FNR ==3')
# Updates the database with the new amounts.
$db_conn "update balance set balance = '$tran1_new_amt' where id = '$tran_user1_id';"
$db_conn "update balance set balance = '$tran2_new_amt' where id = '$tran_user2_id';"
$db_conn "update balance set balance = balance - '$tran_amt' where id = '$tran_user1_id';"
$db_conn "update balance set balance = balance + '$tran_amt' where id = '$tran_user2_id';"
# Transactioning
$db_conn "insert into transaction (id,username,amount,timestamp) values ('$tran_user1_id','$tran_user1','-$tran_amt','now');"
$db_conn "insert into transaction (id,username,amount,timestamp) values ('$tran_user2_id','$tran_user2','+$tran_amt','now');"
@ -175,7 +170,7 @@ do
echo "3) Show last 5 Transactions."
echo ""
echo "q) Go back."
echo ::
echo ""
read -n1 -p "*Transaction Menu: Select from above: " ii_tmenu
case $ii_tmenu in
1 ) echo ""
@ -192,7 +187,7 @@ do
} fi
$db_conn "select id,username,amount,timestamp from transaction order by timestamp DESC limit $tmenu_num2;" ;;
3 ) echo ""
$db_conn "select id,username,amount,timestamp from transaction order by timestamp DESC limit 5;"
$db_conn "select id,username,amount,timestamp from transaction order by timestamp DESC limit 5;" ;;
[0q] ) break ;;
* ) clear ;;