-
Notifications
You must be signed in to change notification settings - Fork 0
/
load_orders.pl
280 lines (219 loc) · 7.25 KB
/
load_orders.pl
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
######################################################################################################
#Purpose: This perl code loads the data into database tables
#Author: Vijay Shanker Karingula
#Language: Perl
#Database: PostgreSQL
######################################################################################################
use strict;
use warnings;
use DBI;
# 'Orderutils' is a self-defined utility file coded to support with necessary sub routines
use Orderutils;
my $driver = "Pg";
my $database = "super_store";
my $dsn = "DBI:$driver:dbname=$database";
my $userid='';
my $password='';
my $dbh=DBI->connect($dsn, $userid,$password, {RaiseError=>1, AutoCommit=>0})
or die "Error connecting to the database: $DBI::errstr\n";
print "Opened database successfully\n";
my $warn = <<EOS
Usage:
$0 data-dir
EOS
;
my $input_dir = $ARGV[0];
my @filepaths = <$input_dir/*.txt>;
my %files = map { s/$input_dir\/(.*)$/$1/; $_ => 1} @filepaths;
# Get spreadsheet constants
my %ord = getColumnsInfo('Orders');
my %ret = getColumnsInfo('Returns');
our ($sql, $sth, $row, @records);
my %return;
# Use a transaction so that it can be rolled back if there are any errors
eval {
loadOrdersData($dbh);
loadReturnsData($dbh);
$dbh->commit; # commit the changes if we get this far
};
if ($@) {
print "\n\nTransaction aborted because $@\n\n";
# now rollback to undo the incomplete changes
# but do it in an eval{} as it may also fail
eval { $dbh->rollback };
}
$dbh->disconnect();
print "\n\nScript completed\n\n";
###### Sub routines for loading ######
sub loadOrdersData {
my $dbh = $_[0];
#my $fields;
my $table_file = "$input_dir/$ord{'worksheet'}.txt";
print "Loading $table_file...\n";
@records = readFile($table_file);
my $line_count = 0;
foreach my $fields (@records) {
$line_count++;
my $row_id = $fields->{$ord{'row_id_fld'}};
my $region_id = setRegionRec($dbh, $fields);
my $province_id = setProvinceRec($dbh, $fields, $region_id);
my $customer_id = setCustomerRec($dbh, $fields, $province_id);
my $uniq_id = setOrderRec($dbh, $fields, $customer_id);
my $pmain_catid = setProduct_Main_Category($dbh, $fields);
my $pcat_id = setProduct_Category($dbh, $fields, $pmain_catid);
my $pid = setProductRec($dbh, $fields, $pcat_id);
setSalesRec($dbh, $fields, $uniq_id, $pid);
}
}
sub loadReturnsData {
my $dbh = $_[0];
#my $fields;
my $table_file = "$input_dir/$ret{'worksheet'}.txt";
print "Scanning $table_file...\n";
@records = readFile($table_file);
my $line_count = 0;
foreach my $fields(@records) {
$line_count++;
my $order_id = $fields->{$ret{'o_id_fld'}};
print "This is order_id: $order_id\n";
my $status = $fields->{$ret{'status_fld'}};
#print "This is status: $status\n";
if (lc($status) eq 'returned') {
#$return{$order_id} = 1;
}
}
}
###################################################################################
###################################################################################
sub setRegionRec {
my ($dbh,$fields) = @_;
my $region_name = $fields->{$ord{'region_fld'}};
$sql = "
INSERT INTO region (region_name)
VALUES ('$region_name')
RETURNING region_id";
logSQL('', $sql);
$sth = doQuery($dbh, $sql);
$row = $sth->fetchrow_hashref;
my $region_id = $row->{'region_id'};
$sth->finish;
return $region_id;
}#setRegionRec
sub setProvinceRec {
my ($dbh, $fields, $region_id) = @_;
my $province_name = $fields->{$ord{'province_fld'}};
$sql = "
INSERT INTO province (province_name, region_id)
VALUES ('$province_name', $region_id)
RETURNING province_id";
logSQL('', $sql);
$sth = doQuery($dbh, $sql);
$row = $sth->fetchrow_hashref;
my $province_id = $row->{'province_id'};
$sth->finish;
return $province_id;
}#setProvinceRec
sub setCustomerRec {
my ($dbh, $fields, $province_id) = @_;
my $cust_name = $fields->{$ord{'customer_name_fld'}};
$cust_name =~ s/\'/\'\'/g;
my $cust_segment = $fields->{$ord{'customer_segment_fld'}};
$sql = "
INSERT INTO customer (customer_name, customer_segment, province_id)
VALUES ('$cust_name', '$cust_segment', $province_id)
RETURNING customer_id";
logSQL('', $sql);
$sth = doQuery($dbh, $sql);
$row = $sth->fetchrow_hashref;
my $customer_id = $row->{'customer_id'};
$sth->finish;
return $customer_id;
}#setCustomerRec
sub setOrderRec {
my ($dbh, $fields, $customer_id) = @_;
my $oid = $fields->{$ord{'order_id_fld'}};
my $odate = $fields->{$ord{'order_date_fld'}};
my $opriority = $fields->{$ord{'order_priority_fld'}};
my $oquantity = $fields->{$ord{'order_quantity_fld'}};
my $ship_date = $fields->{$ord{'ship_date_fld'}};
my $ship_mode = $fields->{$ord{'ship_mode_fld'}};
my $return_oid = $fields->{$ret{'order_id_fld'}};
my $returned = 'false';
if ($return{$oid}) {
$returned = 'true';
}
$sql = "
INSERT INTO orders
(order_id, order_date, order_priority, order_quantity, customer_id, shipping_date, shipping_mode, is_return)
VALUES
($oid, '$odate', '$opriority', $oquantity, $customer_id, '$ship_date', '$ship_mode', $returned)
RETURNING unique_id";
logSQL('', $sql);
$sth = doQuery($dbh,$sql);
$row = $sth->fetchrow_hashref;
my $uniq_id = $row->{'unique_id'};
$sth->finish;
return $uniq_id;
}#setOrderRec
sub setProduct_Main_Category {
my ($dbh, $fields) = @_;
my $pmain_catname = $fields->{$ord{'product_main_cat_fld'}};
$sql = "
INSERT INTO product_main_category (pmain_catname)
VALUES
('$pmain_catname')
RETURNING pmain_catid";
logSQL('', $sql);
$sth = doQuery($dbh, $sql);
$row = $sth->fetchrow_hashref;
my $pmain_catid = $row->{'pmain_catid'};
$sth->finish;
return $pmain_catid;
}#setProduct_main_Category
sub setProduct_Category {
my ($dbh, $fields, $pmain_catid) = @_;
my $pcat_name = $fields->{$ord{'product_cat_fld'}};
$sql = "
INSERT INTO product_category(pmain_catid,pcat_name)
VALUES
($pmain_catid,'$pcat_name')
RETURNING pcat_id";
logSQL('', $sql);
$sth = doQuery($dbh,$sql);
$row = $sth->fetchrow_hashref;
my $pcat_id = $row->{'pcat_id'};
$sth->finish;
return $pcat_id;
}# setProduct_Category
sub setProductRec {
my ($dbh, $fields, $pcat_id) = @_;
my $product_name = $fields->{$ord{'product_name_fld'}};
$product_name =~ s/\'/\'\'/g;
my $price = $fields->{$ord{'unit_price_fld'}};
my $pbase_margin = $fields->{$ord{'product_base_margin_fld'}};
$sql = "
INSERT INTO product (pname,pcat_id,price,pbase_margin)
VALUES
('$product_name',$pcat_id,$price,$pbase_margin)
RETURNING pid";
logSQL('', $sql);
$sth = doQuery($dbh, $sql);
$row = $sth->fetchrow_hashref;
my $pid = $row->{'pid'};
$sth->finish;
return $pid;
}#setProductRec
sub setSalesRec {
my ($dbh, $fields, $uniq_id, $pid) = @_;
my $sales = $fields->{$ord{'sales_fld'}};
my $oid = $fields->{$ord{'order_id_fld'}};
my $discount = $fields->{$ord{'discount_fld'}};
my $profit = $fields->{$ord{'profit_fld'}};
$sql = "
INSERT INTO sales (unique_id,order_id, pid, sales, discount, profit)
VALUES
($uniq_id,$oid,$pid,$sales,$discount,$profit)";
logSQL('', $sql);
$sth = doQuery($dbh, $sql);
$sth->finish;
}