forked from PIO-RishabhMahla/JavaOnIBMi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCSVLDR.SQLRPGLE
More file actions
157 lines (131 loc) · 4.37 KB
/
Copy pathCSVLDR.SQLRPGLE
File metadata and controls
157 lines (131 loc) · 4.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
**FREE
// ============================================================
// Program : CSVLDR
// Purpose : CSV loader service program \Z reads a comma-
// delimited IFS file row by row via IFSRDR and
// bulk-inserts parsed fields into DB2 CSVSTAGE
// Author : Developer
// Date : 2026-04-15
// Changes :
// 2026-04-15 Developer INC#0001 Initial creation
// ============================================================
Ctl-Opt Option(*SrcStmt : *NoDebugIO)
DftActGrp(*No)
ActGrp('APPGRP')
BndDir('QC2LE' : 'APPBND')
NoMain
ExtBinInt(*Yes)
Date(*ISO)
DateFmt(*ISO)
TimFmt(*ISO)
AlwNull(*UsrCtl)
Debug(*Yes);
//COPY QRPGLESRC,APPPROTS
// --- Constants ---
Dcl-C MAX_COLS 20;
Dcl-C COL_DELIM ',';
Dcl-C HEADER_TAG 'EMP_ID'; // First token of header row to skip
// ============================================================
// Procedure : LoadCsvToDb2
// Purpose : Open a CSV on IFS, skip the header row, parse
// each data row, insert into CSVSTAGE, commit
// Parameters: csvPath \Z absolute IFS path to CSV file
// rowsLoaded \Z returns count of rows inserted
// Returns : *On if all rows inserted, *Off on any error
// ============================================================
Dcl-Proc LoadCsvToDb2 Export;
Dcl-Pi *N Ind;
csvPath Varchar(512) Const;
rowsLoaded Int(10);
End-Pi;
Dcl-S lineBuffer Varchar(1024) Inz(*Blanks);
Dcl-S empId Varchar(10) Inz(*Blanks);
Dcl-S empName Varchar(100) Inz(*Blanks);
Dcl-S department Varchar(50) Inz(*Blanks);
Dcl-S salary Packed(11:2) Inz(0);
Dcl-S hireDate Date;
Dcl-S colToken Varchar(200) Inz(*Blanks);
Dcl-S remainder Varchar(1024) Inz(*Blanks);
Dcl-S delimPos Int(10) Inz(0);
Dcl-S successFlag Ind Inz(*Off);
rowsLoaded = 0;
// Open the CSV file via IFSRDR service program
successFlag = OpenIfsFile(csvPath);
If Not successFlag;
LogError(0 : ' ' : 'LoadCsvToDb2 : cannot open ' + csvPath);
Return *Off;
EndIf;
// Purge any existing staged data before loading fresh set
Exec SQL DELETE FROM CSVSTAGE;
If SqlCode < 0;
LogError(SqlCode : SqlState : 'LoadCsvToDb2 : DELETE CSVSTAGE');
CloseIfsFile();
Return *Off;
EndIf;
// Read lines until EOF
Dou Not ReadAllLines(lineBuffer);
lineBuffer = %TrimR(lineBuffer);
// Skip blank lines
If lineBuffer = *Blanks;
Iter;
EndIf;
// Skip header row \Z identified by first token matching HEADER_TAG
If %Scan(HEADER_TAG : lineBuffer) = 1;
Iter;
EndIf;
// ---- Parse CSV columns by position ----
// Column order: EMP_ID, EMP_NAME, DEPARTMENT, SALARY, HIRE_DATE
remainder = lineBuffer;
// Column 1 \Z EMP_ID
delimPos = %Scan(COL_DELIM : remainder);
If delimPos = 0;
Iter; // malformed row \Z skip
EndIf;
empId = %SubSt(remainder : 1 : delimPos - 1);
remainder = %SubSt(remainder : delimPos + 1);
// Column 2 \Z EMP_NAME
delimPos = %Scan(COL_DELIM : remainder);
If delimPos = 0;
Iter;
EndIf;
empName = %SubSt(remainder : 1 : delimPos - 1);
remainder = %SubSt(remainder : delimPos + 1);
// Column 3 \Z DEPARTMENT
delimPos = %Scan(COL_DELIM : remainder);
If delimPos = 0;
Iter;
EndIf;
department = %SubSt(remainder : 1 : delimPos - 1);
remainder = %SubSt(remainder : delimPos + 1);
// Column 4 \Z SALARY
delimPos = %Scan(COL_DELIM : remainder);
If delimPos = 0;
Iter;
EndIf;
colToken = %SubSt(remainder : 1 : delimPos - 1);
salary = %Dec(%Trim(colToken) : 11 : 2);
remainder = %SubSt(remainder : delimPos + 1);
// Column 5 \Z HIRE_DATE (ISO format YYYY-MM-DD)
hireDate = %Date(%Trim(remainder) : *ISO);
// Insert parsed row into staging table
Exec SQL
INSERT INTO CSVSTAGE
(EMP_ID, EMP_NAME, DEPARTMENT, SALARY, HIRE_DATE)
VALUES
(:empId, :empName, :department, :salary, :hireDate);
If SqlCode < 0;
LogError(SqlCode : SqlState : 'LoadCsvToDb2 : INSERT CSVSTAGE empId=' + empId);
CloseIfsFile();
Return *Off;
EndIf;
rowsLoaded += 1;
EndDo;
CloseIfsFile();
// Commit the batch
Exec SQL COMMIT;
If SqlCode < 0;
LogError(SqlCode : SqlState : 'LoadCsvToDb2 : COMMIT');
Return *Off;
EndIf;
Return *On;
End-Proc;