xpg4_is.sql 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589
  1. { ************************************************************************* }
  2. { }
  3. { Licensed Materials - Property of IBM and/or HCL }
  4. { }
  5. { IBM Informix Dynamic Server }
  6. { (c) Copyright IBM Corporation 1996, 2008 All rights reserved. }
  7. { (c) Copyright HCL Technologies Ltd. 2017. All Rights Reserved. }
  8. { }
  9. { ************************************************************************* }
  10. { }
  11. { Title: xpg4_is.sql }
  12. { }
  13. { ************************************************************************* }
  14. { THE FOLLOWING COLTYPE NUMBERS are referenced in the store procedures:
  15. coltype SQLtype coltype SQLtype
  16. ------- -------- ------- --------
  17. 0 CHAR 8 MONEY
  18. 1 SMALLINT 10 DATETIME
  19. 2 INTEGER 11 BYTE
  20. 3 FLOAT 12 TEXT
  21. 4 SMALLFLOAT 13 VARCHAR
  22. 5 DECIMAL 14 INTERVAL
  23. 6 SERIAL 15 SQLNCHAR
  24. 7 DATE 16 SQLVNCHAR
  25. 17 SQLINT8
  26. 18 SQLSERIAL8
  27. 19 SQLSET
  28. 20 SQLMULTISET
  29. 21 SQLLIST
  30. 22 SQLROW
  31. 23 SQLCOLLECTION
  32. 24 SQLROWREF
  33. 40 SQLUDTVAR
  34. 41 SQLUDTFIXED
  35. 42 SQLREFSER8
  36. 52 SQLBIGINT
  37. 53 SQLBIGSERIAL
  38. }
  39. { INFORMATION SCHEMA FOR TRI-STAR WITH X/OPEN XPG4 COMPLIANCE }
  40. { Utility Procedures }
  41. {----------------------------------------------------------------------}
  42. {------creating supporting procedures used by views--------------------}
  43. {----------------------------------------------------------------------}
  44. create procedure 'informix'.get_null()
  45. returning char;
  46. return null;
  47. end procedure
  48. document
  49. 'Procedure get_null() returns a null value when it is called',
  50. 'Synopsis: get_null() takes no arguments';
  51. {----------------------------------------------------------------------}
  52. create procedure 'informix'.ansitabtype(inftype char(1))
  53. returning char(18);
  54. if (inftype = 'T') then
  55. return 'BASE TABLE';
  56. elif (inftype = 'V') then
  57. return 'VIEW';
  58. else
  59. return 'INTERNAL ERROR';
  60. end if;
  61. end procedure
  62. document
  63. 'Returns table type with ansi conventions',
  64. 'Synopsis: ansitabtype(char(1)) returns char(18)';
  65. {----------------------------------------------------------------------}
  66. create procedure 'informix'.ansinullable(coltype smallint)
  67. returning char(3);
  68. if (coltype >= 256) then
  69. return 'NO';
  70. else
  71. return 'YES';
  72. end if;
  73. end procedure
  74. document
  75. 'returns if the column is nullable or not',
  76. 'Synopsis: ansinullable(smallint) returns char(3)';
  77. {----------------------------------------------------------------------}
  78. create procedure 'informix'.ansicoltype(coltype smallint, collength smallint)
  79. returning char(18);
  80. define largest, smallest int;
  81. if (coltype >= 256) then
  82. let coltype = coltype - 256;
  83. end if;
  84. if (coltype = 0) then
  85. return 'CHARACTER';
  86. elif (coltype = 1) then
  87. return 'SMALLINT';
  88. elif (coltype = 2) then
  89. return 'INTEGER';
  90. elif (coltype = 3) then
  91. return 'FLOAT';
  92. elif (coltype = 4) then
  93. return 'SMALLFLOAT';
  94. elif (coltype = 5) then
  95. return 'DECIMAL';
  96. elif (coltype = 6) then
  97. return 'SERIAL';
  98. elif (coltype = 7) then
  99. return 'DATE';
  100. elif (coltype = 8) then
  101. return 'MONEY';
  102. elif (coltype = 10) then
  103. return 'DATETIME';
  104. elif (coltype = 11) then
  105. return 'BYTE';
  106. elif (coltype = 12) then
  107. return 'TEXT';
  108. elif (coltype = 13) then
  109. return 'CHARACTER VARYING';
  110. elif (coltype = 14) then
  111. return 'INTERVAL';
  112. elif (coltype = 15) then
  113. return 'NCHAR';
  114. elif (coltype = 16) then
  115. return 'NVCHAR';
  116. elif (coltype = 17) then
  117. return 'INTEGER8';
  118. elif (coltype = 18) then
  119. return 'SERIAL8';
  120. elif (coltype = 19) then
  121. return 'SET';
  122. elif (coltype = 20) then
  123. return 'MULTISET';
  124. elif (coltype = 21) then
  125. return 'LIST';
  126. elif (coltype = 22) then
  127. return 'ROW';
  128. elif (coltype = 23) then
  129. return 'COLLECTION';
  130. elif (coltype = 24) then
  131. return 'ROWREF';
  132. elif (coltype = 40) then
  133. return 'OPAQUE VARIABLE';
  134. elif (coltype = 41) then
  135. return 'OPAQUE FIXED';
  136. elif (coltype = 42) then
  137. return 'REFSERIAL8';
  138. elif (coltype = 52) then
  139. return 'BIGINT';
  140. elif (coltype = 53) then
  141. return 'BIGSERIAL';
  142. else
  143. return 'UNKNOWN DATA TYPE';
  144. end if;
  145. end procedure
  146. document
  147. 'returns the column data type',
  148. 'Synopsis: ansicoltype(smallint returns char(18))';
  149. {----------------------------------------------------------------------}
  150. create procedure 'informix'.ansimaxlen(coltype smallint, collength smallint)
  151. returning int;
  152. if (coltype >= 256) then
  153. let coltype = coltype - 256;
  154. end if;
  155. if (coltype = 0) then
  156. return collength;
  157. elif (coltype = 13) or (coltype = 16) then
  158. return (collength - (trunc(collength / 256))*256);
  159. else
  160. return NULL;
  161. end if;
  162. end procedure
  163. document
  164. 'returns the maximum length of character oriented column',
  165. 'Synopsis: ansimaxlen(smallint, smallint) returns int';
  166. {----------------------------------------------------------------------}
  167. create procedure 'informix'.ansinumprec(coltype smallint, collength smallint)
  168. returning int;
  169. { FLOAT and SMALLFLOAT precisions are in bits }
  170. if (coltype >= 256) then
  171. let coltype = coltype - 256;
  172. end if;
  173. if (coltype = 1) then -- smallint
  174. return 5;
  175. elif (coltype = 2) or (coltype = 6) then -- int
  176. return 10;
  177. elif (coltype = 3) then -- float
  178. return 64;
  179. elif (coltype = 4) then -- smallfloat
  180. return 32;
  181. elif (coltype = 5) or (coltype = 8) then -- decimal
  182. return (trunc(collength / 256));
  183. elif (coltype = 17) or (coltype = 18) then -- int8
  184. return 19;
  185. elif (coltype = 52) or (coltype = 53) then -- bigint
  186. return 19;
  187. else
  188. return NULL;
  189. end if;
  190. end procedure
  191. document
  192. 'returns the precision of a numeric column',
  193. 'Synopsis: ansinumprec(smallint, smallint) returns int';
  194. {----------------------------------------------------------------------}
  195. create procedure 'informix'.ansinumprecradix( coltype smallint)
  196. returning int;
  197. if (coltype >= 256) then
  198. let coltype = coltype - 256;
  199. end if;
  200. if (coltype = 1) or (coltype = 2) or
  201. (coltype = 5) or (coltype = 6) or
  202. (coltype = 8) or (coltype = 17) or
  203. (coltype = 18) or (coltype = 52) or
  204. (coltype = 53) then
  205. return 10;
  206. elif (coltype = 3) or (coltype = 4) then
  207. return 2;
  208. else
  209. return NULL;
  210. end if;
  211. end procedure
  212. document
  213. 'returns the precision radix of a numeric column',
  214. 'Synopsis: ansinumprecradix(smallint) returns int';
  215. {----------------------------------------------------------------------}
  216. create procedure 'informix'.ansinumscale(coltype smallint, collength smallint)
  217. returning int;
  218. if (coltype >= 256) then
  219. let coltype = coltype - 256;
  220. end if;
  221. if (coltype = 1) or (coltype = 2) or
  222. (coltype = 6) then
  223. return 0;
  224. elif (coltype = 5) or (coltype = 8) then
  225. return (collength - ((trunc(collength / 256))*256));
  226. else
  227. return NULL;
  228. end if;
  229. end procedure
  230. document
  231. 'returns the scale of a numeric column',
  232. 'Synopsis: ansinumscale(smallint, smallint) returns int';
  233. {----------------------------------------------------------------------}
  234. create procedure 'informix'.ansidatprec(coltype smallint, collength smallint)
  235. returning int;
  236. { if the column is nullable then coltype = coltype+256 }
  237. if (coltype = 7 or coltype = 263) then
  238. return 0;
  239. elif (coltype = 10 or coltype = 266) then
  240. let collength = collength - 16*trunc(collength/16) - 10;
  241. if (collength > 0) then
  242. return collength;
  243. else
  244. return 0;
  245. end if;
  246. else
  247. return NULL;
  248. end if;
  249. end procedure
  250. document
  251. 'returns the date precision for a datetime column',
  252. 'Synopsis: ansidatprec(smallint, smallint) returns int';
  253. {----------------------------------------------------------------------}
  254. create procedure 'informix'.se_or_ol()
  255. returning char(2);
  256. define num int;
  257. { methodology :
  258. TO determine whether a database is SE or OL :
  259. If 'informix'.sysblobs is not found, then it's SE, else it's OL,
  260. }
  261. on exception in (-206) -- Table not found
  262. return 'SE';
  263. end exception
  264. select max(tabid) into num from 'informix'.sysblobs; -- max to ensure 1 row
  265. return 'OL';
  266. end procedure
  267. document
  268. 'returns the engine type of the server',
  269. 'Synopsis: se_or_ol() returns char(2)';
  270. {----------------------------------------------------------------------}
  271. create procedure 'informix'.is_log_ansi()
  272. returning char, char; -- log_flag, ansi_flag
  273. define errornum int;
  274. { methodology :
  275. To determine whether logging or not (obviously non-ANSI database)
  276. issue begin transaction:
  277. if it succeeds or returns -535, there's logging,
  278. if it returns -256, no logging,
  279. TO determine whether a database is ANSI or not :
  280. create a systables(we know it already exists), with different username.
  281. If it's allowed, it's ANSI. Drop the table then return.
  282. If it's not allowed, may be the table really does exist!, if so, it's
  283. ansi, if not, it's not ANSI.
  284. }
  285. on exception in (-256) -- Transaction not available
  286. return 'N', 'N';
  287. end exception
  288. on exception in (-206) -- Table not found
  289. return 'Y', 'N';
  290. end exception
  291. on exception in (-535, -310) -- Already in Transaction, Table already exist
  292. set errornum
  293. end exception with resume
  294. let errornum = 0;
  295. begin work;
  296. if (errornum == 0) then
  297. rollback;
  298. end if;
  299. -- at this point, we know database is created with logging
  300. create table 'DuMmYuSr'.systables(c1 char); -- create a dummy table
  301. if (errornum = -310) then
  302. let errornum = (select count(*) from 'DuMmYuSr'.systables);
  303. else
  304. drop table 'DuMmYuSr'.systables;
  305. end if;
  306. return 'Y', 'Y';
  307. end procedure
  308. document
  309. 'returns the logging and ansi DB information',
  310. 'Synopsis: is_log_ansi() returns char, char';
  311. {----------------------------------------------------------------------}
  312. create dba procedure 'informix'.insert_sql_lang()
  313. { only DSA online has conformance information }
  314. if (se_or_ol() = 'OL') then
  315. insert into xSQL_LANGUAGES values('X/OPEN SQL','1992','XPG4',
  316. 'YES', 'ONLINE DSA', 'EMBEDDED', 'C');
  317. insert into xSQL_LANGUAGES values('X/OPEN SQL','1992','XPG4',
  318. 'YES', 'ONLINE DSA', 'EMBEDDED', 'COBOL');
  319. insert into xSQL_LANGUAGES values('ANSI X3.135','1992','ENTRY',
  320. 'YES', 'ONLINE DSA', 'EMBEDDED', 'C');
  321. insert into xSQL_LANGUAGES values('ANSI X3.135','1992','ENTRY',
  322. 'YES', 'ONLINE DSA', 'EMBEDDED', 'COBOL');
  323. end if;
  324. end procedure
  325. document
  326. 'populate xSQL_LANGUAGES table for OL only, nothing for SE',
  327. 'Synopsis: insert_sql_lang() returns nothing';
  328. {----------------------------------------------------------------------}
  329. create dba procedure 'informix'.insert_srv_info()
  330. define log_flag, ansi_flag char;
  331. define engine_type char(2);
  332. let engine_type = se_or_ol();
  333. if (engine_type = 'SE') then
  334. insert into xSERVER_INFO values('ENGINE TYPE','Standard Engine');
  335. insert into xSERVER_INFO values('ROW_LENGTH', '32511');
  336. else
  337. insert into xSERVER_INFO values('ENGINE TYPE','Online DSA');
  338. insert into xSERVER_INFO values('ROW_LENGTH', '32767');
  339. end if;
  340. insert into xSERVER_INFO values('IDENTIFIER_LENGTH', '128');
  341. insert into xSERVER_INFO values('USERID_LENGTH','32');
  342. insert into xSERVER_INFO values('COLLATION_SEQ','ISO 8859-1');
  343. { TXN_ISOLATION depends on whether there's logging, and/or ansi database }
  344. let log_flag, ansi_flag = is_log_ansi();
  345. if (engine_type = 'SE') or (log_flag = 'N') then
  346. insert into xSERVER_INFO values('TXN_ISOLATION', 'READ UNCOMMITTED');
  347. elif (log_flag = 'Y') and (ansi_flag = 'N') then
  348. insert into xSERVER_INFO values('TXN_ISOLATION', 'READ COMMITTED');
  349. elif (ansi_flag = 'Y') then
  350. insert into xSERVER_INFO values('TXN_ISOLATION', 'SERIALIZABLE');
  351. end if;
  352. end procedure
  353. document
  354. 'populate xSERVER_INFO table',
  355. 'Synopsis: insert_srv_info() returns nothing';
  356. {----------------------------------------------------------------------}
  357. {----------create views------------------------------------------------}
  358. {----------------------------------------------------------------------}
  359. create view 'informix'.TABLES(
  360. TABLE_SCHEMA, -- 1
  361. TABLE_NAME, -- 2
  362. TABLE_TYPE, -- 3
  363. REMARKS) -- 4
  364. as
  365. select trim(ST.owner), -- 1
  366. trim(ST.tabname), -- 2
  367. trim('informix'.ansitabtype(tabtype)), -- 3
  368. 'informix'.get_null() -- 4
  369. from 'informix'.systables ST
  370. where ST.tabtype in ('T', 'V') and
  371. (
  372. ST.owner = USER
  373. or
  374. USER = 'informix'
  375. or
  376. exists
  377. ( -- DBA user
  378. select * from 'informix'.sysusers
  379. where username = USER and usertype = 'D'
  380. )
  381. or
  382. exists
  383. (
  384. select * from 'informix'.systabauth STA
  385. where ST.tabid = STA.tabid and
  386. STA.grantee in (USER, 'public')
  387. )
  388. );
  389. {----------------------------------------------------------------------}
  390. create view 'informix'.COLUMNS(
  391. TABLE_SCHEMA, -- 1
  392. TABLE_NAME, -- 2
  393. COLUMN_NAME, -- 3
  394. ORDINAL_POSITION, -- 4
  395. DATA_TYPE, -- 5
  396. CHAR_MAX_LENGTH, -- 6
  397. NUMERIC_PRECISION, -- 7
  398. NUMERIC_PREC_RADIX, -- 8
  399. NUMERIC_SCALE, -- 9
  400. DATETIME_PRECISION, -- 10
  401. IS_NULLABLE, -- 11
  402. REMARKS) -- 12
  403. as
  404. select trim(ST.owner), -- 1
  405. trim(ST.tabname), -- 2
  406. trim(SC.colname), -- 3
  407. SC.colno, -- 4
  408. trim('informix'.ansicoltype(SC.coltype, SC.collength)), -- 5
  409. 'informix'.ansimaxlen(SC.coltype, SC.collength), -- 6
  410. 'informix'.ansinumprec(SC.coltype, SC.collength), -- 7
  411. 'informix'.ansinumprecradix(SC.coltype), -- 8
  412. 'informix'.ansinumscale(SC.coltype, SC.collength), -- 9
  413. 'informix'.ansidatprec(SC.coltype, SC.collength), -- 10
  414. trim('informix'.ansinullable(SC.coltype)), -- 11
  415. 'informix'.get_null() -- 12
  416. from 'informix'.systables ST,
  417. 'informix'.syscolumns SC
  418. where
  419. ST.tabtype in ('T', 'V') and
  420. ST.tabid = SC.tabid and
  421. (
  422. ST.owner = USER
  423. or
  424. USER = 'informix'
  425. or
  426. exists
  427. ( -- DBA user
  428. select * from 'informix'.sysusers
  429. where username = USER and usertype = 'D'
  430. )
  431. or
  432. exists
  433. ( -- entire table is granted
  434. select * from 'informix'.systabauth STA
  435. where ST.tabid = STA.tabid and
  436. STA.grantee in (USER, 'public') and
  437. STA.tabauth <> '--*-----'
  438. )
  439. or
  440. exists
  441. ( -- one column is granted
  442. select * from 'informix'.syscolauth SCA
  443. where ST.tabid = SCA.tabid and
  444. SCA.colno = SC.colno and
  445. SCA.grantee in (USER, 'public')
  446. )
  447. );
  448. create table 'informix'.xSQL_LANGUAGES(
  449. SOURCE char(254) NOT NULL,
  450. SOURCE_YEAR char(254),
  451. CONFORMANCE char(254),
  452. INTEGRITY char(254),
  453. IMPLEMENTATION char(254),
  454. BINDING_STYLE char(254),
  455. PROGRAMMING_LANG char(254));
  456. execute procedure 'informix'.insert_sql_lang();
  457. create view 'informix'.SQL_LANGUAGES(
  458. SOURCE,
  459. SOURCE_YEAR,
  460. CONFORMANCE,
  461. INTEGRITY,
  462. IMPLEMENTATION,
  463. BINDING_STYLE,
  464. PROGRAMMING_LANG)
  465. as select
  466. trim(SOURCE),
  467. trim(SOURCE_YEAR),
  468. trim(CONFORMANCE),
  469. trim(INTEGRITY),
  470. trim(IMPLEMENTATION),
  471. trim(BINDING_STYLE),
  472. trim(PROGRAMMING_LANG)
  473. from 'informix'.xSQL_LANGUAGES;
  474. create table 'informix'.xSERVER_INFO(
  475. SERVER_ATTRIBUTE char(254) NOT NULL,
  476. ATTRIBUTE_VALUE char(254));
  477. execute procedure 'informix'.insert_srv_info();
  478. create view 'informix'.SERVER_INFO(
  479. SERVER_ATTRIBUTE,
  480. ATTRIBUTE_VALUE)
  481. as select
  482. trim(SERVER_ATTRIBUTE),
  483. trim(ATTRIBUTE_VALUE)
  484. from 'informix'.xSERVER_INFO;
  485. {----------------------------------------------------------------------}
  486. {--------- setting proper privileges ----------------------------------}
  487. {----------------------------------------------------------------------}
  488. grant execute on 'informix'.get_null to public
  489. with grant option as 'informix';
  490. grant execute on 'informix'.ansitabtype to public
  491. with grant option as 'informix';
  492. grant execute on 'informix'.ansinullable to public
  493. with grant option as 'informix';
  494. grant execute on 'informix'.ansicoltype to public
  495. with grant option as 'informix';
  496. grant execute on 'informix'.ansimaxlen to public
  497. with grant option as 'informix';
  498. grant execute on 'informix'.ansinumprec to public
  499. with grant option as 'informix';
  500. grant execute on 'informix'.ansinumprecradix to public
  501. with grant option as 'informix';
  502. grant execute on 'informix'.ansinumscale to public
  503. with grant option as 'informix';
  504. grant execute on 'informix'.ansidatprec to public
  505. with grant option as 'informix';
  506. revoke all on 'informix'.TABLES from public;
  507. grant select on 'informix'.TABLES to public
  508. with grant option as 'informix';
  509. revoke all on 'informix'.COLUMNS from public;
  510. grant select on 'informix'.COLUMNS to public
  511. with grant option as 'informix';
  512. revoke all on 'informix'.SQL_LANGUAGES from public;
  513. grant select on 'informix'.SQL_LANGUAGES to public
  514. with grant option as 'informix';
  515. revoke all on 'informix'.SERVER_INFO from public;
  516. grant select on 'informix'.SERVER_INFO to public
  517. with grant option as 'informix';