|
| 1 | +**free |
| 2 | +// === Program to create "n" records of test data in CUSTMAST ===== |
| 3 | +// === Assumes CUSTMAST is exists and will clear it first. |
| 4 | +// === City name, state and zip are generated from USPS data, |
| 5 | +// file CSZ, created from upload from USPS.COM with iACS. |
| 6 | +// See https://www.unitedstateszipcodes.org/zip-code-database/ |
| 7 | +// Rest of the data i randomly generated. |
| 8 | +// This was an experimental program to use randomn number and |
| 9 | +// variable sized arrays. But it was fun to write... |
| 10 | + |
| 11 | +ctl-opt DftActGrp(*no) ActGrp('QILE') BndDir('UTIL_BND':'SQL_BND':'SRV_BASE36'); |
| 12 | +ctl-opt Option(*nounref: *nodebugio: *srcstmt); |
| 13 | +ctl-opt ExprOpts(*ResDecPos) ExtBinInt( *Yes ); |
| 14 | +ctl-opt Debug(*constants : *retval); |
| 15 | +ctl-opt Indent('| '); |
| 16 | +/COPY ../Copy_Mbrs/SRV_SQL_P.RPGLE |
| 17 | +/COPY ../Copy_Mbrs/SRV_RAND_P.RPGLE |
| 18 | +/COPY ../Copy_Mbrs/BASE36_P.RPGLE |
| 19 | + |
| 20 | +// === Program Parameter ======================================= |
| 21 | +dcl-pi *n; |
| 22 | + parm_recds packed(15 : 5); // Number of records to create |
| 23 | +end-pi; |
| 24 | +dcl-s p_recds int(10); |
| 25 | + |
| 26 | +dcl-ds Fld ExtName('CUSTMAST') Qualified; |
| 27 | +end-ds; |
| 28 | +// === SQL State Constants ===================================== |
| 29 | +dcl-c SQLSUCCESS '00000'; |
| 30 | +dcl-c SQLNODATA '02000'; |
| 31 | +dcl-c SQLNOMOREDATA '02000'; |
| 32 | +dcl-c SQLNOTJOURNALED '01567'; |
| 33 | + |
| 34 | +dcl-s companyType varchar(10) dim(*auto : 30); |
| 35 | +dcl-s streetType varchar(10) dim(*auto : 30); |
| 36 | +dcl-s varCUSTID varchar(4); |
| 37 | +// === City/state/zip record === |
| 38 | +dcl-ds csz qualified; |
| 39 | + zip int(10); |
| 40 | + ziptype char(10); |
| 41 | + city char(20); |
| 42 | + st char(2); |
| 43 | +end-ds; |
| 44 | + |
| 45 | +// === Arrary of City, State and Zip records === |
| 46 | +dcl-ds csz_a likeds(csz) dim(*auto : 50000) ; |
| 47 | + |
| 48 | +dcl-s cszCount int(10); |
| 49 | +dcl-s MaxL int(10); |
| 50 | +dcl-s csz_I int(10); |
| 51 | +dcl-s j int(10); |
| 52 | +// dcl-s t int(10); |
| 53 | +DCL-S adrX int(10); |
| 54 | +dcl-s nRecds int(10); |
| 55 | +dcl-s wk10 char(10); |
| 56 | +dcl-s wkStr varchar(50); |
| 57 | + |
| 58 | +// === Build array of Company types === |
| 59 | +companyType(1) = 'INC'; |
| 60 | +companyType(*next) = 'LLC'; |
| 61 | +companyType(*next) = 'LLP'; |
| 62 | +companyType(*next) = 'COMPANY'; |
| 63 | +companyType(*next) = '& SONS'; |
| 64 | +companyType(*next) = 'ET FILS'; |
| 65 | +companyType(*next) = 'PLC'; |
| 66 | +companyType(*next) = 'CORP'; |
| 67 | +companyType(*next) = 'LTD'; |
| 68 | +companyType(*next) = 'SOLE'; |
| 69 | +companyType(*next) = 'PARTNERS'; |
| 70 | +companyType(*next) = 'ASSOC'; |
| 71 | + |
| 72 | +// === Build array of street types === |
| 73 | +streetType(1) = 'STREET'; |
| 74 | +streetType(*next) = 'ST'; |
| 75 | +streetType(*next) = 'ROAD'; |
| 76 | +streetType(*next) = 'RD'; |
| 77 | +streetType(*next) = 'AVENUE'; |
| 78 | +streetType(*next) = 'AVE'; |
| 79 | +streetType(*next) = 'PLACE'; |
| 80 | +streetType(*next) = 'CIRCLE'; |
| 81 | +streetType(*next) = 'SQUARE'; |
| 82 | +streetType(*next) = 'HWY'; |
| 83 | +streetType(*next) = 'VISTA'; |
| 84 | +streetType(*next) = 'CALLE'; |
| 85 | +streetType(*next) = 'RANCH'; |
| 86 | +streetType(*next) = 'CRESCENT'; |
| 87 | +streetType(*next) = 'COURT'; |
| 88 | +streetType(*next) = 'WAY'; |
| 89 | + |
| 90 | +exec sql set option datfmt=*iso, |
| 91 | + commit=*none, |
| 92 | + closqlcsr=*endmod; |
| 93 | + |
| 94 | +// === Clear Custmast file === |
| 95 | +exec sql truncate lennons1.custmast; |
| 96 | +if (SQLSTT <> SQLSUCCESS and SQLSTT <> SQLNODATA); |
| 97 | + SQLProblem('truncate custmast'); |
| 98 | +endif; |
| 99 | + |
| 100 | +// === Size the City/State/Zip array === |
| 101 | +exec sql select count(*) into :cszCount |
| 102 | + from lennons1.csz |
| 103 | + where length(trim(city)) <= 20; |
| 104 | +if (SQLSTT <> SQLSUCCESS); |
| 105 | + SQLProblem('select count(*)'); |
| 106 | +endif; |
| 107 | +%elem(csz_a : *alloc) = cszCount + 1; |
| 108 | + |
| 109 | +// === Populate csz_a === |
| 110 | +exec sql declare csz_cur cursor for |
| 111 | + select zip, type, upper(city), trim(state) |
| 112 | + from lennons1.csz |
| 113 | + where length(trim(city)) <= 20; |
| 114 | +exec sql open csz_cur; |
| 115 | +if (SQLSTT <> SQLSUCCESS ); |
| 116 | + SQLProblem('Open csz_cur'); |
| 117 | +endif; |
| 118 | +j=1; |
| 119 | +dow (1=1); |
| 120 | + exec sql fetch from csz_cur into :csz; |
| 121 | + if SQLSTT = SQLNOMOREDATA; |
| 122 | + leave; |
| 123 | + endif; |
| 124 | + if SQLSTT <> SQLSUCCESS; |
| 125 | + SQLProblem('fetch fron csz_cur'); |
| 126 | + endif; |
| 127 | + csz_a(j) = csz; |
| 128 | + j += 1; |
| 129 | +enddo; |
| 130 | + |
| 131 | +// === Build CUSTMAST records ================================= |
| 132 | +p_recds = parm_recds; |
| 133 | +varCUSTID = '1001'; |
| 134 | +for nRecds = 1 to p_recds; |
| 135 | + clear Fld; |
| 136 | + Fld.CUSTID = varCUSTID; |
| 137 | + varCUSTID = BASE36ADD(varCUSTID); // Alpha-numeric key |
| 138 | + Fld.ACTIVE = 'Y'; |
| 139 | + if (%rem(nRecds : 7) = 0); |
| 140 | + Fld.ACTIVE = 'N'; |
| 141 | + endif; |
| 142 | + |
| 143 | + // === City/State Zip === |
| 144 | + csz_I = Rand_Int(1:%elem(csz_a)); |
| 145 | + Fld.STATE = csz_a(csz_I).st; |
| 146 | + Fld.CITY = csz_a(csz_I).city; |
| 147 | + wk10 = %editc(csz_a(csz_I).zip:'X'); |
| 148 | + Fld.ZIP = %subst(wk10 :6 :5); |
| 149 | + |
| 150 | + // === Company Name === |
| 151 | + clear wkStr; |
| 152 | + // Specify a random name length leaving space for |
| 153 | + // a company6y type suffix. |
| 154 | + MaxL = Rand_Int(5 : %len(Fld.Name) - 12); |
| 155 | + wkStr = wkStr + genWord(5:11) + ' '; |
| 156 | + dow %len(wkStr) <= MaxL; |
| 157 | + wkStr = wkStr + genWord(5:11) + ' '; |
| 158 | + enddo; |
| 159 | + |
| 160 | + // Add company "types" to some records |
| 161 | + j = Rand_Int(1:(%elem(companyType) * 1.8)); |
| 162 | + if j <= %elem(companyType); |
| 163 | + wkStr = %trim(wkStr) + ' ' + companyType(j); |
| 164 | + endif; |
| 165 | + Fld.NAME = wkStr; |
| 166 | + |
| 167 | + // === Address === |
| 168 | + clear wkStr; |
| 169 | + adrX = %rem(nRecds :4); |
| 170 | + MaxL = Rand_Int(5 : %len(Fld.ADDR) - 12); |
| 171 | + if adrX <> 0; //Add street number to most |
| 172 | + wkStr = %trim(%editc(Rand_Int(1:5000) : '3')) + ' '; |
| 173 | + endif; |
| 174 | + wkStr = wkStr + genWord(5:11) + ' '; |
| 175 | + dow %len(wkStr) <= MaxL; |
| 176 | + wkStr = wkStr + genWord(5:11) + ' '; |
| 177 | + enddo; |
| 178 | + // Add street "types" to some records |
| 179 | + j = Rand_Int(1:(%elem(streetType) * 1.75)); |
| 180 | + if j <= %elem(streetType); |
| 181 | + wkStr = %trim(wkStr) + ' ' + streetType(j); |
| 182 | + endif; |
| 183 | + Fld.ADDR = wkStr; |
| 184 | + |
| 185 | + // === Phone numbers === |
| 186 | + Fld.CORPPHONE = genPhone(); |
| 187 | + Fld.ACCTPHONE = genPhone(); |
| 188 | + |
| 189 | + // === Account Manager === |
| 190 | + if %rem(nRecds :3) = 0; |
| 191 | + wkStr = genWord(1:1) + ' '; |
| 192 | + wkStr += genWord(1:1) + ' '; |
| 193 | + wkStr += genWord(4:10); |
| 194 | + else; |
| 195 | + wkStr = genWord(3:6) + ' '; |
| 196 | + wkStr += genWord(5:9); |
| 197 | + endif; |
| 198 | + Fld.ACCTMGR = wkStr; |
| 199 | + |
| 200 | + Fld.CHGUSER = '*SYSTEM*'; |
| 201 | + |
| 202 | + // === Write out a record ====================================== |
| 203 | + exec sql insert into lennons1.custmast values(:Fld); |
| 204 | + if (SQLSTT <> SQLSUCCESS); |
| 205 | + SQLProblem('Insert into custmast'); |
| 206 | + endif; |
| 207 | +endfor; |
| 208 | + |
| 209 | +// === All finished ============================================ |
| 210 | +*inlr = *on; |
| 211 | +return; |
| 212 | + |
| 213 | +// === Generate a word ========================================= |
| 214 | +dcl-proc genWord; |
| 215 | + dcl-pi genWord varchar(30); |
| 216 | + MinL int(10) const; |
| 217 | + MaxL int(10) const; |
| 218 | + end-pi; |
| 219 | + // Straight alphabetic |
| 220 | + dcl-s Alpha varchar(50) |
| 221 | + inz('ABCDEFGHIIJKLMNOPQRSTUVWXYZZ'); |
| 222 | + // Biased towards vowels |
| 223 | + dcl-s vAlpha varchar(50) |
| 224 | + inz('AAAAABCDEEEEEFGHIIIIIJKLMNOOOOOPQRSTUUUVWXYZZ'); |
| 225 | + |
| 226 | + dcl-s wk30 varchar(30) inz; |
| 227 | + dcl-s TgtL int(10); |
| 228 | + dcl-s j int(10); |
| 229 | + |
| 230 | + wk30 = %subst(Alpha : Rand_Int(1 : %len(Alpha)) : 1); |
| 231 | + if (MinL<> 1 and MaxL <> 1); |
| 232 | + wk30 += %subst(vAlpha : Rand_Int(1 : %len(vAlpha)) : 1); |
| 233 | + endif; |
| 234 | + TgtL = Rand_Int(MinL:MaxL) - 2; |
| 235 | + for j=1 by 2 to TgtL; |
| 236 | + wk30 += %subst(Alpha : Rand_Int(1 : %len(Alpha)) : 1); |
| 237 | + wk30 += %subst(vAlpha : Rand_Int(1 : %len(vAlpha)) : 1); |
| 238 | + endfor; |
| 239 | + return wk30; |
| 240 | +end-proc; |
| 241 | + |
| 242 | +// === Generate a phone like (800) 231-1876 ==================== |
| 243 | +dcl-proc genPhone; |
| 244 | + dcl-pi genPhone varchar(20); |
| 245 | + end-pi; |
| 246 | + dcl-s wkret varchar(20); |
| 247 | + dcl-s wk3 char(3); |
| 248 | + dcl-s wk4 char(4); |
| 249 | + wk3 = %editc(%dec(Rand_Int(100:900):3 :0) : 'X'); |
| 250 | + wkret = '(' + wk3 + ') '; |
| 251 | + wk3 = %editc(%dec(Rand_Int(1:998) :3 :0) : 'X'); |
| 252 | + wkret += wk3 + '-'; |
| 253 | + wk4 = (%editc(%dec(Rand_Int(1:9900) :4 :0) : 'X')); |
| 254 | + wkret += wk4; |
| 255 | + return wkret; |
| 256 | +end-proc; |
0 commit comments