-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtestDatabaseFunctions.pl
More file actions
165 lines (145 loc) · 5.37 KB
/
Copy pathtestDatabaseFunctions.pl
File metadata and controls
165 lines (145 loc) · 5.37 KB
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#!/usr/bin/perl -w
use strict;
use warnings;
use CGI;
use databaseFunctions qw(connectToOracle selectFromTable insertUpdateDelete);
#############--------------- Author Sarah Aziziyan ---------------#############
#################################################################################
########################## ##########################
########################## TestDatabaseFunctions ##########################
########################## 1.00 ##########################
########################## 08/06/2017 ##########################
########################## ##########################
#################################################################################
#############================== File History =================#################
## v 1.00 file creation - (last modified -> 08/06/2017 - 15:40 by Sarah Aziziyan)
sub selectFromStudents {
my $inputStdId = $ARGV[1];
if(!defined($inputStdId)){
print "undefined stdId\n";
}else{
my ($connCode,$connMsg,$dbh) = connectToOracle("payment");
if ($connCode == 0) {
my $query="select stdId,stdFirstName,stdLastName from students where stdId=?";
my @params=($inputStdId);
my ($code,$msg,$resultArray) = selectFromTable($dbh,$query,\@params);
if ($code == 0) {
for (my $i=0;$i<=$#{$resultArray};$i++) {
my ($stdId,$stdFirstName,$stdLastName)=@{$resultArray->[$i]};
print "stdId = $stdId, stdFirstName = $stdFirstName, stdLastName = $stdLastName\n";
}
} else {
## do something
print "error in select statement. code = $code, msg = $msg\n";
}
$dbh->disconnect();
}else{
print "database connection error, code = $connCode , msg = $connMsg\n";
}
}
}
sub updateStdNameWithReturningParameter{
my $stdId = $ARGV[1];
my $newStdFirstName = $ARGV[2];
if(!defined($stdId)){
print "undefined stdId\n";
}elsif(!defined($newStdFirstName)){
print "undefined newStdFirstName\n";
}else{
my ($connCode,$connMsg,$dbh) = connectToOracle("payment");
if ($connCode == 0) {
my $returningStdFirstName;
my @returningParams=([\$returningStdFirstName,20]);
my @params = ($newStdFirstName,$stdId);
my $query = "update students set stdFirstName=? where stdId=? returning stdFirstName into ?";
my ($e_code,$e_msg)=insertUpdateDelete($dbh,$query,\@params,\@returningParams);
if(defined($returningStdFirstName)){
if($e_code==0 && ($returningStdFirstName eq $newStdFirstName)){
$dbh->commit();
print "student $stdId,$newStdFirstName has been updated\n";
}else{
$dbh->rollback();
print "no error! but also data has not been updated. returningStdFirstName=$returningStdFirstName and newStdFirstName=$newStdFirstName does not match\n";
}
}else{
$dbh->rollback();
print "no error! but also data has not been updated. possibly no record has been found with the data specified\n";
}
$dbh->disconnect();
}else{
print "database connection error, code = $connCode , msg = $connMsg\n";
}
}
}
sub insertStudent{ ## insert student
my $stdId = $ARGV[1];
my $stdFirstName = $ARGV[2];
my $stdLastName = $ARGV[3];
if(!defined($stdId)){
print "undefined stdId\n";
}elsif(!defined($stdFirstName)){
print "undefined stdFirstName\n";
}elsif(!defined($stdLastName)){
print "undefined stdLastName\n";
}else{
my ($connCode,$connMsg,$dbh) = connectToOracle("payment");
if ($connCode == 0) {
my $insert_string = "insert into students(stdId,stdFirstName,stdLastName) values(?,?,?)";
my @params = ($stdId,$stdFirstName,$stdLastName);
my ($e_code,$e_msg)=insertUpdateDelete($dbh,$insert_string,\@params);
if($e_code==0){
$dbh->commit();
print "student $stdId,$stdFirstName,$stdLastName has been inserted\n";
}else{
$dbh->rollback();
print "statement error.rolledback, code = $e_code , msg = $e_msg\n";
}
$dbh -> disconnect();
}else{
print "database connection error, code = $connCode , msg = $connMsg\n";
}
}
}
sub deleteStudent{
my $stdId = $ARGV[1];
if(!defined($stdId)){
print "undefined stdId\n";
}else{
my ($connCode,$connMsg,$dbh) = connectToOracle("payment");
if ($connCode == 0) {
my $insert_string = "delete from students where stdId=?";
my @params = ($stdId);
my($e_code,$e_msg)=insertUpdateDelete($dbh,$insert_string,\@params);
if($e_code==0){
$dbh->commit();
print "student with stdId=$stdId has been successfuly deleted!\n";
}else{
print "statement error.rolledback, code = $e_code , msg = $e_msg\n";
$dbh->rollback();
}
$dbh -> disconnect();
}else{
print "database connection error, code = $connCode , msg = $connMsg\n";
}
}
}
sub main {
my $method = $ARGV[0];
if (defined($method)) {
if ($method eq "select"){
selectFromStudents();
} elsif ($method eq "insert"){
insertStudent();
} elsif ($method eq "update") {
updateStdNameWithReturningParameter();
} elsif ($method eq "delete") {
deleteStudent();
}
else{
print "method does not exist\n";
}
} else {
print "method undefined\n";
}
}
main();