empbuild.e 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  1. /*
  2. * The contents of this file are subject to the Interbase Public
  3. * License Version 1.0 (the "License"); you may not use this file
  4. * except in compliance with the License. You may obtain a copy
  5. * of the License at http://www.Inprise.com/IPL.html
  6. *
  7. * Software distributed under the License is distributed on an
  8. * "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express
  9. * or implied. See the License for the specific language governing
  10. * rights and limitations under the License.
  11. *
  12. * The Original Code was created by Inprise Corporation
  13. * and its predecessors. Portions created by Inprise Corporation are
  14. * Copyright (C) Inprise Corporation.
  15. *
  16. * All Rights Reserved.
  17. * Contributor(s): ______________________________________.
  18. */
  19. #include <stdio.h>
  20. #include <string.h>
  21. #include <stdlib.h>
  22. /* #include "../jrd/common.h" */
  23. #include "ibase.h"
  24. /* Some #defines that are used in the program - they actually come from
  25. jrd/common.h but should not be exposed externally with those name so
  26. are reproduced here MOD 15-07-2001
  27. */
  28. typedef char TEXT;
  29. #define FINI_OK 0
  30. #define FINI_ERROR 44
  31. /*
  32. ** Empbuild.e GPRE with manual switch, since it creates the database
  33. ** This program then calls isql with various input files
  34. ** It installs the blobs and arrays.
  35. ** Usage: empbuild <db name>
  36. */
  37. static int addlang (void);
  38. static int addjob (void);
  39. static int addproj (void);
  40. static int addqtr (void);
  41. static TEXT Db_name[128];
  42. static FILE *Fp;
  43. EXEC SQL SET SQL DIALECT 3;
  44. EXEC SQL INCLUDE SQLCA;
  45. EXEC SQL SET DATABASE DB = COMPILETIME "empbuild.fdb" RUNTIME :Db_name;
  46. int main (
  47. int argc,
  48. char *argv[])
  49. {
  50. /**************************************
  51. *
  52. * m a i n
  53. *
  54. **************************************
  55. *
  56. * Functional description
  57. *
  58. **************************************/
  59. TEXT cmd [140];
  60. if (argc > 1)
  61. strcpy (Db_name, argv[1]);
  62. else
  63. strcpy (Db_name, "employee.fdb");
  64. /* Create the database */
  65. printf ("creating database %s\n", Db_name);
  66. sprintf (cmd, "CREATE DATABASE \"%s\"", Db_name);
  67. gds__trans = 0;
  68. EXEC SQL EXECUTE IMMEDIATE :cmd;
  69. if (SQLCODE)
  70. {
  71. isc_print_status (gds__status);
  72. exit (FINI_ERROR);
  73. }
  74. gds__trans = 0;
  75. EXEC SQL DISCONNECT ALL;
  76. if (SQLCODE)
  77. {
  78. isc_print_status (gds__status);
  79. exit (FINI_ERROR);
  80. }
  81. printf ("Turning forced writes off\n");
  82. sprintf (cmd, "gfix -write async %s", Db_name);
  83. if (system (cmd))
  84. {
  85. printf ("Couldn't turn forced writes off\n");
  86. exit (FINI_ERROR);
  87. }
  88. printf ("Creating tables\n");
  89. sprintf (cmd, "isql %s -q -i empddl.sql", Db_name);
  90. if (system (cmd))
  91. {
  92. printf ("Couldn't create tables \n");
  93. exit (FINI_ERROR);
  94. }
  95. printf ("Turning off indices and triggers \n");
  96. sprintf (cmd, "isql %s -i indexoff.sql", Db_name);
  97. if (system (cmd))
  98. {
  99. printf ("Couldn't turn off indices and triggers \n");
  100. exit (FINI_ERROR);
  101. }
  102. printf ("Loading column data\n");
  103. sprintf (cmd, "isql %s -i empdml.sql", Db_name);
  104. if (system (cmd))
  105. {
  106. printf ("Couldn't load column data \n");
  107. exit (FINI_ERROR);
  108. }
  109. printf ("Turning on indices and triggers \n");
  110. sprintf (cmd, "isql %s -i indexon.sql", Db_name);
  111. if (system (cmd))
  112. {
  113. printf ("Couldn't turn on indices and triggers \n");
  114. exit (FINI_ERROR);
  115. }
  116. EXEC SQL CONNECT DB;
  117. if (SQLCODE)
  118. {
  119. isc_print_status (gds__status);
  120. exit (FINI_ERROR);
  121. }
  122. // What is/was the point of this?
  123. // Each of the functions below start their own txn.
  124. //EXEC SQL SET TRANSACTION;
  125. printf ("Loading Language arrays\n");
  126. if ( addlang() )
  127. {
  128. printf ("Couldn't load Language arrays\n");
  129. EXEC SQL DISCONNECT DB;
  130. exit (FINI_ERROR);
  131. }
  132. printf ("Loading Job blobs\n");
  133. if ( addjob() )
  134. {
  135. printf ("Couldn't load Job blobs\n");
  136. EXEC SQL DISCONNECT DB;
  137. exit (FINI_ERROR);
  138. }
  139. printf ("Loading project blobs \n");
  140. if ( addproj() )
  141. {
  142. printf ("Couldn't load project blobs\n");
  143. EXEC SQL DISCONNECT DB;
  144. exit (FINI_ERROR);
  145. }
  146. printf ("Loading quarter arrays \n");
  147. if ( addqtr() )
  148. {
  149. printf ("Couldn't load quarter arrays\n");
  150. EXEC SQL DISCONNECT DB;
  151. exit (FINI_ERROR);
  152. }
  153. exit (FINI_OK);
  154. }
  155. static int addlang (void)
  156. {
  157. /**************************************
  158. *
  159. * a d d l a n g
  160. *
  161. **************************************
  162. *
  163. * Functional description
  164. * Add language array to 'job' table.
  165. *
  166. **************************************/
  167. TEXT job_code[6], job_country[16];
  168. TEXT line[100];
  169. TEXT lang_array[5][16];
  170. int i, job_grade, rec_cnt = 0;
  171. EXEC SQL SET TRANSACTION;
  172. EXEC SQL WHENEVER SQLERROR GO TO Error;
  173. Fp = fopen ("lang.inp", "r");
  174. if (Fp == NULL){
  175. printf ("lang.inp not found\n");
  176. exit(FINI_ERROR);
  177. }
  178. while (fgets (line, 100, Fp) != NULL)
  179. {
  180. sscanf (line, "%s %d %s", job_code, &job_grade, job_country);
  181. for (i = 0; i < 5; i++)
  182. {
  183. if (fgets (line, 100, Fp) == NULL)
  184. break;
  185. strcpy (lang_array [i], line);
  186. }
  187. EXEC SQL
  188. UPDATE job
  189. SET language_req = :lang_array
  190. WHERE job_code = :job_code AND
  191. job_grade = :job_grade AND
  192. job_country = :job_country;
  193. if (SQLCODE == 0)
  194. rec_cnt++;
  195. else
  196. {
  197. printf ("Input error -- no job record with key: %s %d %s\n",
  198. job_code, job_grade, job_country);
  199. }
  200. }
  201. EXEC SQL COMMIT;
  202. printf ("Added %d language arrays.\n", rec_cnt);
  203. fclose (Fp);
  204. return (0);
  205. Error:
  206. printf ("SQLCODE=%ld\n", (long)SQLCODE);
  207. isc_print_status (gds__status);
  208. EXEC SQL ROLLBACK;
  209. return (1);
  210. }
  211. static int addjob (void)
  212. {
  213. /**************************************
  214. *
  215. * a d d j o b
  216. *
  217. **************************************
  218. *
  219. * Functional description
  220. * Add job description blobs.
  221. *
  222. **************************************/
  223. TEXT job_code[6];
  224. TEXT line[100], job_country[16];
  225. int len;
  226. ISC_QUAD job_blob;
  227. int job_grade, rec_cnt = 0;
  228. EXEC SQL SET TRANSACTION;
  229. EXEC SQL WHENEVER SQLERROR GO TO Error;
  230. EXEC SQL DECLARE be CURSOR FOR
  231. INSERT BLOB job_requirement INTO job;
  232. Fp = fopen ("job.inp", "r");
  233. if (Fp == NULL){
  234. printf ("job.inp not found\n");
  235. exit(FINI_ERROR);
  236. }
  237. while (fgets (line, 100, Fp) != NULL)
  238. {
  239. EXEC SQL OPEN be INTO :job_blob;
  240. sscanf (line, "%s %d %s", job_code, &job_grade, job_country);
  241. while (fgets (line, 100, Fp) != NULL)
  242. {
  243. if (*line == '\n')
  244. break;
  245. len = strlen (line);
  246. EXEC SQL INSERT CURSOR be VALUES (:line INDICATOR :len);
  247. }
  248. EXEC SQL CLOSE be;
  249. EXEC SQL
  250. UPDATE job
  251. SET job_requirement = :job_blob
  252. WHERE job_code = :job_code AND
  253. job_grade = :job_grade AND
  254. job_country = :job_country;
  255. if (SQLCODE == 0)
  256. rec_cnt++;
  257. else
  258. {
  259. printf ("Input error -- no job record with key: %s %d %s\n",
  260. job_code, job_grade, job_country);
  261. }
  262. }
  263. EXEC SQL COMMIT;
  264. printf ("Added %d job requirement descriptions.\n", rec_cnt);
  265. fclose (Fp);
  266. return (0);
  267. Error:
  268. printf ("SQLCODE=%ld\n", (long)SQLCODE);
  269. isc_print_status (gds__status);
  270. EXEC SQL ROLLBACK;
  271. return (1);
  272. }
  273. static int addproj (void)
  274. {
  275. /**************************************
  276. *
  277. * a d d p r o j
  278. *
  279. **************************************
  280. *
  281. * Functional description
  282. * Add project description blobs.
  283. *
  284. **************************************/
  285. TEXT proj_id[6];
  286. TEXT line[100];
  287. int len;
  288. ISC_QUAD proj_blob;
  289. int rec_cnt = 0;
  290. EXEC SQL SET TRANSACTION;
  291. EXEC SQL WHENEVER SQLERROR GO TO Error;
  292. EXEC SQL DECLARE bd CURSOR FOR
  293. INSERT BLOB proj_desc INTO project;
  294. Fp = fopen ("proj.inp", "r");
  295. if (Fp == NULL){
  296. printf ("proj.inp not found\n");
  297. exit(FINI_ERROR);
  298. }
  299. while (fgets (line, 100, Fp) != NULL)
  300. {
  301. EXEC SQL OPEN bd INTO :proj_blob;
  302. sscanf (line, "%s", proj_id);
  303. while (fgets (line, 100, Fp) != NULL)
  304. {
  305. if (*line == '\n')
  306. break;
  307. len = strlen (line);
  308. EXEC SQL INSERT CURSOR bd VALUES (:line INDICATOR :len);
  309. }
  310. EXEC SQL CLOSE bd;
  311. EXEC SQL
  312. UPDATE project
  313. SET proj_desc = :proj_blob
  314. WHERE proj_id = :proj_id;
  315. if (SQLCODE == 0)
  316. rec_cnt++;
  317. else
  318. {
  319. printf ("Input error -- no project record with key: %s\n", proj_id);
  320. }
  321. }
  322. EXEC SQL COMMIT;
  323. printf ("Added %d project descriptions.\n", rec_cnt);
  324. fclose (Fp);
  325. return (0);
  326. Error:
  327. printf ("SQLCODE=%ld\n", (long)SQLCODE);
  328. isc_print_status (gds__status);
  329. EXEC SQL ROLLBACK;
  330. return (1);
  331. }
  332. static int addqtr (void)
  333. {
  334. /**************************************
  335. *
  336. * a d d q t r
  337. *
  338. **************************************
  339. *
  340. * Functional description
  341. * Add project quarterly head-count array to 'proj_dept_budget' table.
  342. *
  343. **************************************/
  344. TEXT proj_id[6], dept_no[4];
  345. int yr;
  346. TEXT line[100];
  347. int hcnt[4];
  348. int rec_cnt = 0;
  349. EXEC SQL SET TRANSACTION;
  350. EXEC SQL WHENEVER SQLERROR GO TO Error;
  351. Fp = fopen ("qtr.inp", "r");
  352. if (Fp == NULL){
  353. printf ("qtr.inp not found\n");
  354. exit(FINI_ERROR);
  355. }
  356. while (fgets (line, 100, Fp) != NULL)
  357. {
  358. sscanf (line, "%d %s %s %d %d %d %d", &yr, proj_id, dept_no,
  359. &hcnt[0], &hcnt[1], &hcnt[2], &hcnt[3]);
  360. EXEC SQL
  361. UPDATE proj_dept_budget
  362. SET quart_head_cnt = :hcnt
  363. WHERE fiscal_year = :yr AND proj_id = :proj_id AND dept_no = :dept_no;
  364. if (SQLCODE == 0)
  365. rec_cnt++;
  366. else
  367. {
  368. printf ("Input error -- no job record with key: %d %s %s\n",
  369. yr, proj_id, dept_no);
  370. }
  371. }
  372. EXEC SQL COMMIT RELEASE;
  373. printf ("Added %d quarter arrays.\n", rec_cnt);
  374. fclose (Fp);
  375. return (0);
  376. Error:
  377. printf ("SQLCODE=%ld\n", (long)SQLCODE);
  378. isc_print_status (gds__status);
  379. EXEC SQL ROLLBACK;
  380. return (1);
  381. }