db_cre_ser.c
上传用户:dgyhgb
上传日期:2007-01-07
资源大小:676k
文件大小:22k
源码类别:

SQL Server

开发平台:

Unix_Linux

  1. /*
  2.  *  db_cre_ser.c  -  creating DB catalogs (server part)
  3.  *
  4.  *  This file is a part of GNU SQL Server
  5.  *
  6.  *  Copyright (c) 1996, 1997, Free Software Foundation, Inc
  7.  *  Developed at the Institute of System Programming
  8.  *  This file is written by Konstantin Dyshlevoi
  9.  *
  10.  *  This program is free software; you can redistribute it and/or modify
  11.  *  it under the terms of the GNU General Public License as published by
  12.  *  the Free Software Foundation; either version 2 of the License, or
  13.  *  (at your option) any later version.
  14.  *
  15.  *  This program is distributed in the hope that it will be useful,
  16.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  17.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  18.  *  GNU General Public License for more details.
  19.  *
  20.  *  You should have received a copy of the GNU General Public License
  21.  *  along with this program; if not, write to the Free Software
  22.  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  23.  *
  24.  *  Contacts: gss@ispras.ru
  25.  *
  26.  */
  27. /* $Id: db_cre_ser.c,v 1.245 1997/03/31 03:46:38 kml Exp $ */
  28. #include "gsqltrn.h" /* c/s protocol defnitions */
  29. #include "global.h"
  30. #ifdef HAVE_UNISTD_H
  31. #include <unistd.h>
  32. #endif
  33. #include "pupsi.h"
  34. #include "tptrn.h"
  35. #include "expop.h"
  36. #include "const.h"
  37. #include "fieldtp.h"
  38. #include "exti.h"
  39. #include "type_lib.h"
  40. #define   FWR(tabid)    fwrite(&tabid,sizeof(Tabid),1,fb)
  41. #define   FWRID(indid)  fwrite(&indid,sizeof(Indid),1,fb)
  42. #define   FRD(tabid)    fread (&tabid,sizeof(Tabid),1,fb)
  43. #define   FRDID(indid)  fread (&indid,sizeof(Indid),1,fb)
  44. #define SIX  (strlen(SYSADM))
  45. #define PUBLIC "PUBLIC"
  46. #define KILL_PROC(cod)
  47. {
  48.   if (cod > 0)
  49.     rollback (cod);
  50.   else
  51.     commit ();
  52.   return (-1);
  53. }
  54. extern i4_t initializing_base;
  55. static void *colval[MAX_COLNO];
  56. static u2_t lencols[MAX_COLNO];
  57. static i4_t   cnlist[8];
  58. static char str1[MAX_COLNO], str2[MAX_COLNO], str3[MAX_COLNO];
  59. static char char1[1], char2[1];
  60. static i2_t short1[1], short2[1], short3[1], short4[1];
  61. static i4_t int1[1], int2[1], int3[1], int4[1];
  62.                        /*   code       prec  len   */
  63. #define TYPE_INT       { SQLType_Int,   0,  SZ_INT }
  64. #define TYPE_SHORT     { SQLType_Short, 0,  SZ_SHT }
  65. #define TYPE_STRING(l) { SQLType_Char,  0,    l    }
  66. sql_type_t TABLES_COLTYPES [TABLES_COLNO] = 
  67. { TYPE_STRING ( MAX_USER_NAME_LNG ),/* tabname    */
  68.   TYPE_STRING ( MAX_USER_NAME_LNG ),/* owner      */
  69.   TYPE_INT,                         /* untabid    */
  70.   TYPE_SHORT,                       /* segid      */
  71.   TYPE_INT,                         /* tabd       */
  72.   TYPE_INT,                         /* clasindid  */
  73.   TYPE_INT,                         /* primindid  */
  74.   TYPE_STRING ( 1 ),                /* tabtype    */
  75.   TYPE_STRING ( 8 ),                /* credate    */
  76.   TYPE_STRING ( 8 ),                /* cretime    */
  77.   TYPE_SHORT,                       /* ncols      */
  78.   TYPE_INT,                         /* nrows      */
  79.   TYPE_INT                          /* nnulcolnum */
  80. };
  81. sql_type_t COLUMNS_COLTYPES [COLUMNS_COLNO] =
  82. /*   code prec len */
  83. { TYPE_STRING ( MAX_USER_NAME_LNG), /* colname    */
  84.   TYPE_INT,                         /* untabid    */
  85.   TYPE_SHORT,                       /* colno      */
  86.   TYPE_STRING ( 1 ),                /* coltype    */
  87.   TYPE_INT,                         /* coltype1   */
  88.   TYPE_INT,                         /* coltype2   */
  89.   TYPE_STRING ( MAX_STR_LNG ),      /* defval     */
  90.   TYPE_STRING ( 1 ),                /* defnull    */
  91.   TYPE_INT,                         /* valno      */
  92.   TYPE_STRING ( MAX_STR_LNG ),      /*  minval    */
  93.   TYPE_STRING ( MAX_STR_LNG )       /*  maxval    */
  94. };
  95. sql_type_t INDEXES_COLTYPES [INDEXES_COLNO] =
  96. /*   code prec len */
  97. { TYPE_INT,                         /* untabid */
  98.   TYPE_STRING ( MAX_USER_NAME_LNG), /* inxname */
  99.   TYPE_STRING ( MAX_USER_NAME_LNG ),/* owner   */
  100.   TYPE_STRING ( 2 ),                /* inxtype */
  101.   TYPE_SHORT,                       /* ncol    */
  102.   TYPE_SHORT,                       /* colno1  */
  103.   TYPE_SHORT,                       /* colno2  */
  104.   TYPE_SHORT,                       /* colno3  */
  105.   TYPE_SHORT,                       /* colno4  */
  106.   TYPE_SHORT,                       /* colno5  */
  107.   TYPE_SHORT,                       /* colno6  */
  108.   TYPE_SHORT,                       /* colno7  */
  109.   TYPE_SHORT,                       /* colno8  */
  110.   TYPE_STRING ( 8 ),                /* credate */
  111.   TYPE_STRING ( 8 ),                /* cretime */
  112.   TYPE_INT                          /* unindid */
  113. };
  114. sql_type_t TABAUTH_COLTYPES [TABAUTH_COLNO] =
  115. /*   code prec len */
  116. { TYPE_INT,                         /* untabid */
  117.   TYPE_STRING ( MAX_USER_NAME_LNG ),/* grantee */
  118.   TYPE_STRING ( MAX_USER_NAME_LNG ),/* grantor */
  119.   TYPE_STRING ( 7 )                 /* tabauth */
  120. };
  121. sql_type_t COLAUTH_COLTYPES [COLAUTH_COLNO] =
  122. /*   code prec len */
  123. { TYPE_INT,                         /* untabid */
  124.   TYPE_SHORT,                       /* colno   */
  125.   TYPE_STRING ( MAX_USER_NAME_LNG ),/* grantee */
  126.   TYPE_STRING ( MAX_USER_NAME_LNG ),/* grantor */
  127.   TYPE_STRING ( 2 )                 /* colauth */
  128. };
  129. sql_type_t REFCONSTR_COLTYPES [REFCONSTR_COLNO] =
  130. /*   code prec len */
  131. { TYPE_INT,                         /* tabfrom  */
  132.   TYPE_INT,                         /* tabto    */
  133.   TYPE_INT,                         /* indto    */
  134.   TYPE_SHORT,                       /* ncols    */
  135.   TYPE_SHORT,                       /* colnofr1 */
  136.   TYPE_SHORT,                       /* colnofr2 */
  137.   TYPE_SHORT,                       /* colnofr3 */
  138.   TYPE_SHORT,                       /* colnofr4 */
  139.   TYPE_SHORT,                       /* colnofr5 */
  140.   TYPE_SHORT,                       /* colnofr6 */
  141.   TYPE_SHORT,                       /* colnofr7 */
  142.   TYPE_SHORT,                       /* colnofr8 */
  143.   TYPE_SHORT,                       /* colnoto1 */
  144.   TYPE_SHORT,                       /* colnoto2 */
  145.   TYPE_SHORT,                       /* colnoto3 */
  146.   TYPE_SHORT,                       /* colnoto4 */
  147.   TYPE_SHORT,                       /* colnoto5 */
  148.   TYPE_SHORT,                       /* colnoto6 */
  149.   TYPE_SHORT,                       /* colnoto7 */
  150.   TYPE_SHORT                        /* colnoto8 */
  151. };
  152. sql_type_t CHCONSTR_COLTYPES [CHCONSTR_COLNO] =
  153. /*   code prec len */
  154. { TYPE_INT,                         /* untabid */
  155.   TYPE_INT,                         /* chconid */
  156.   TYPE_INT,                         /* consize */
  157.   TYPE_SHORT,                       /* ncols   */
  158.   TYPE_SHORT,                       /* colno1  */
  159.   TYPE_SHORT,                       /* colno2  */
  160.   TYPE_SHORT,                       /* colno3  */
  161.   TYPE_SHORT,                       /* colno4  */
  162.   TYPE_SHORT,                       /* colno5  */
  163.   TYPE_SHORT,                       /* colno6  */
  164.   TYPE_SHORT,                       /* colno7  */
  165.   TYPE_SHORT                        /* colno8  */
  166. };
  167. sql_type_t CHCONSTRTWO_COLTYPES [CHCONSTRTWO_COLNO] =
  168. /* code prec len */
  169. { TYPE_INT,                         /* chconid  */
  170.   TYPE_SHORT,                       /* fragno   */
  171.   TYPE_INT,                         /* fragsize */
  172.   TYPE_STRING ( MAX_STR_LNG )       /* frag     */
  173. };
  174. sql_type_t VIEWS_COLTYPES [VIEWS_COLNO] =
  175. /* code prec len */
  176. { TYPE_INT,                         /* untabid  */
  177.   TYPE_SHORT,                       /* fragno   */
  178.   TYPE_INT,                         /* fragsize */
  179.   TYPE_STRING ( MAX_STR_LNG )       /* frag     */
  180. };
  181. i4_t 
  182. ToCol(i4_t untabid, char *name, i4_t colno, i4_t nulno, sql_type_t *type_arr)
  183. {
  184.   i4_t lcod;
  185.   
  186.   lencols[0] = strlen (name);
  187.   strcpy (str1, name);                   /* colname  */
  188.   *short1 = colno;                       /* colno    */
  189.   *char1  = type_arr[colno].code;        /* coltype  */
  190.   *int1   = untabid;                     /* untabid  */
  191.   *int2   = type_arr[colno].len;         /* coltype1 */
  192.   *int3   = type_arr[colno].prec;        /* coltype2 */
  193.   *char2  = (nulno > colno) ? '1' : '0'; /* defnull  */
  194.   
  195.   lcod = insrow (&syscoltabid, COLUMNS_COLNO, lencols, colval, NULL);
  196.   if (lcod)
  197.     {
  198.       fprintf (stderr, "BASE: cant't make insrow for SYSCOLUMNS,cod=%dn ", lcod);
  199.       KILL_PROC (lcod);
  200.     }
  201.   return 0;
  202. }
  203. i4_t 
  204. ToAuth ()
  205. {
  206.   i4_t lcod;
  207.   lcod = insrow (&tabauthtabid, TABAUTH_COLNO, lencols, colval, NULL);
  208.   if (lcod)
  209.     {
  210.       fprintf (stderr, "BASE: cant't make insrow for SYSTABAUTH,cod=%dn ", lcod);
  211.       KILL_PROC (lcod);
  212.     }
  213.   return 0;
  214. }
  215. i4_t 
  216. TOCOLAuth ()
  217. {
  218.   i4_t lcod;
  219.   lcod = insrow (&colauthtabid, COLAUTH_COLNO, lencols, colval, NULL);
  220.   if (lcod)
  221.     {
  222.       fprintf (stderr, "BASE: cant't make insrow for SYSCOLAUTH,cod=%dn ", lcod);
  223.       KILL_PROC (lcod);
  224.     }
  225.   return 0;
  226. }
  227. i4_t 
  228. InsSysTab ()
  229. {
  230.   i4_t lcod;
  231.   lcod = insrow (&systabtabid, TABLES_COLNO, lencols, colval, NULL);
  232.   if (lcod)
  233.     {
  234.       fprintf (stderr, "BASE: cant't make insrow in systables,cod=%dn ", lcod);
  235.       KILL_PROC (lcod);
  236.     }
  237.   return 0;
  238. }
  239. int
  240. IndCre (char *inm,Indid *indid, Tabid *tabid, 
  241. i4_t cnum, i4_t n1, i4_t n2, i4_t n3, i4_t un_fl)
  242.      /* Index creation : inm - index name; indid - pointer to *
  243.       * index identifier (result); tabid - pointer to table   *
  244.       * identifier; cnum - index columns number; n1, n2, n3 - *
  245.       * numbers of these columns ( -1 if is not used );       *
  246.       * un_fl = 1 if UNIQUE index, 0 -else.                   */
  247. {
  248.   i4_t cod;
  249.   
  250.   if (cl_debug)
  251.     fprintf (STDOUT, "BASE:  making index %s n", inm);
  252.   
  253.   cnlist[0] = n1;
  254.   cnlist[1] = n2;
  255.   cnlist[2] = n3;
  256.   
  257.   cod = creind (indid, tabid, 0, un_fl, 0, cnum, cnlist);
  258.   if (cod < 0)
  259.     {
  260.       fprintf (stderr, "BASE: cant't make it : cod = %dn ", cod);
  261.       commit ();
  262.       return (-1);
  263.     }
  264.   
  265.   lencols[1] = strlen (inm);
  266.   lencols[3] = un_fl;
  267.   
  268.   *int1 = tabid->untabid;                     /* untabid */
  269.   strcpy (str1, inm);                         /* inxname */
  270.   colval[3] = (un_fl) ? char1 : NULL;         /* inxtype */
  271.   *short4 = cnum;                             /* ncol    */
  272.   *short1 = n1;                               /* colno1  */
  273.   
  274.   if (n2 == -1)
  275.     colval[6] = NULL;
  276.   else
  277.     {
  278.       colval[6] = short2;                     /* colno2  */
  279.       *short2 = n2;
  280.     }
  281.   
  282.   if (n3 == -1)
  283.     colval[7] = NULL;
  284.   else
  285.     {
  286.       colval[7] = short3;                     /* colno3  */
  287.       *short3 = n3;
  288.     }
  289.   
  290.   *int2 = indid->unindid;                     /* unindid */
  291.   
  292.   cod = insrow (&sysindtabid, INDEXES_COLNO, lencols, colval, NULL);
  293.   if (cod)
  294.     {
  295.       fprintf (stderr, "BASE: cant't make index : cod=%dn ", cod);
  296.       KILL_PROC (cod);
  297.     }
  298.   return 0; /* O'K */
  299. } /* IndCre */
  300. int
  301. CreateTable (Tabid * tabid, char *str_name, i4_t colno,
  302.      i4_t nulno, sql_type_t *coltypes, i4_t nrows)
  303. {
  304.   static sql_type_t tp[MAX_COLNO];
  305.   i4_t cod, i;
  306.   
  307.   for (i = 0; i < colno; i++)
  308.     type_to_bd( coltypes + i, tp + i);
  309.     
  310.   cod = creptab (tabid, 1 /* segid */, 0 /* spn */, colno, nulno, tp);
  311.   if (cod)
  312.     KILL_PROC (cod);
  313.   lencols[0] = strlen (str_name);
  314.   strcpy (str1, str_name);  /* tabname    */
  315.   *int1 = tabid->untabid;   /* untabid    */
  316.   *short1 = tabid->segid;   /* segid      */
  317.   *int2 = tabid->tabd;      /* tabd       */
  318.   *short2 = colno;          /* ncols      */
  319.   *int4 = nrows;            /* nrows      */
  320.   *int3 = nulno;            /* nnulcolnum */
  321.   if (InsSysTab ())
  322.     return (-1);
  323.   
  324.   return 0;
  325. } /* CreateTable */
  326. int
  327. create_base ()
  328. /* create system catalogs for data base  */
  329. /* return 0 if success */
  330. {
  331.   char cur_dt[9], cur_tm[9];
  332.   
  333.   FILE *fb;
  334.   i4_t cod, i, cur_untabid, cur_nulno, cur_colno;
  335.   sql_type_t *cur_type_arr;
  336.   
  337.   curdate (cur_dt);
  338.   curtime (cur_tm); 
  339. /*---------- creation of SYSTEM CATALOGUES & SYSTABLES filling  -----------*/
  340.   lencols[1] = SIX;
  341.   lencols[2] = sizeof (i4_t);
  342.   lencols[3] = sizeof (i2_t);
  343.   lencols[4] = sizeof (i4_t);
  344.   lencols[5] = 0;
  345.   lencols[6] = 0;
  346.   lencols[7] = 1;
  347.   lencols[8] = 8;
  348.   lencols[9] = 8;
  349.   lencols[10] = sizeof (i2_t);
  350.   lencols[11] = sizeof (i4_t);
  351.   lencols[12] = sizeof (i4_t);
  352.   
  353.   colval[0] = str1;              /* tabname    */
  354.   colval[1] = str2;              /* owner      */
  355.   colval[2] = int1;              /* untabid    */
  356.   colval[3] = short1;            /* segid      */
  357.   colval[4] = int2;              /* tabd       */
  358.   colval[5] = NULL;              /* clasindid  */
  359.   colval[6] = NULL;              /* primindid  */
  360.   colval[7] = str3;              /* tabtype    */  
  361.   colval[8] = cur_dt;            /* credate    */
  362.   colval[9] = cur_tm;            /* cretime    */
  363.   colval[10] = short2;           /* ncols      */
  364.   colval[11] = int4;             /* nrows      */
  365.   colval[12] = int3;             /* nnulcolnum */
  366.   strcpy (str2, SYSADM);       /* owner      */
  367.   strcpy (str3, "B");            /* tabtype    */  
  368.   
  369. #define CRE_SYS_TABLE(tabid, name, nrows)     
  370.   cod = CreateTable (&tabid, "SYS" #name, name##_COLNO,     
  371.      name##_NULNO, name##_COLTYPES, nrows); 
  372.   if (cod < 0)             
  373.     return (cod);
  374.     
  375. /* tabid          name        nrows */     
  376.   CRE_SYS_TABLE (systabtabid,    TABLES,         9);
  377.   CRE_SYS_TABLE (syscoltabid,    COLUMNS,       89);
  378.   CRE_SYS_TABLE (sysindtabid,    INDEXES,       11);
  379.   CRE_SYS_TABLE (tabauthtabid,   TABAUTH,        8);
  380.   CRE_SYS_TABLE (colauthtabid,   COLAUTH,       10);
  381.   CRE_SYS_TABLE (refconstrtabid, REFCONSTR,      0);
  382.   CRE_SYS_TABLE (chconstrtabid,  CHCONSTR,       0);
  383.   CRE_SYS_TABLE (chcontwotabid,  CHCONSTRTWO,    0);
  384.   CRE_SYS_TABLE (viewstabid,     VIEWS,          0);
  385.   
  386. /*-------------------------- SYSCOLUMNS filling ------------------------*/
  387.   lencols[1] = sizeof (i4_t);
  388.   lencols[2] = sizeof (u2_t);
  389.   lencols[3] = 1;
  390.   lencols[4] = sizeof (i4_t);
  391.   lencols[5] = sizeof (i4_t);
  392.   lencols[6] = 0;
  393.   lencols[7] = 1;
  394.   lencols[8] = 0;
  395.   lencols[9] = 0;
  396.   lencols[10] = 0;
  397.   colval[0] = str1;              /* colname   */
  398.   colval[1] = int1;              /* untabid   */
  399.   colval[2] = short1;            /* colno     */
  400.   colval[3] = char1;             /* coltype   */
  401.   colval[4] = int2;              /* coltype1  */
  402.   colval[5] = int3;              /* coltype2  */
  403.   colval[6] = NULL;              /* defval    */
  404.   colval[7] = char2;             /* defnull   */
  405.   colval[8] = NULL;              /* valno     */
  406.   colval[9] = NULL;              /* minval    */
  407.   colval[10] = NULL;             /* maxval    */
  408.   
  409. /*------------ columns of SYSTABLES to SYSCOLUMNS ---------------*/
  410.   
  411. #define ABOUT_TABLE(name, tabid)
  412.   cur_untabid = tabid.untabid;
  413.   cur_nulno = name##_NULNO;
  414.   cur_type_arr = name##_COLTYPES;
  415.   cur_colno = 0;
  416.   
  417. #define TO_SYSCOL(name) if ((cod = ToCol (cur_untabid, name, cur_colno++, 
  418.                             cur_nulno, cur_type_arr)) < 0) return (cod)
  419.   ABOUT_TABLE (TABLES, systabtabid);
  420.   TO_SYSCOL("TABNAME");
  421.   TO_SYSCOL("OWNER");
  422.   TO_SYSCOL("UNTABID");
  423.   TO_SYSCOL("SEGID");
  424.   TO_SYSCOL("TABD");
  425.   TO_SYSCOL("CLASINDID");
  426.   TO_SYSCOL("PRIMINDID");
  427.   TO_SYSCOL("TABTYPE");
  428.   TO_SYSCOL("CREDATE");
  429.   TO_SYSCOL("CRETIME");
  430.   TO_SYSCOL("NCOLS");
  431.   TO_SYSCOL("NROWS");
  432.   TO_SYSCOL("NNULCOLNUM");
  433.   
  434. /*------------ columns of SYSCOLUMNS to SYSCOLUMNS --------------*/
  435.   
  436.   ABOUT_TABLE (COLUMNS, syscoltabid);
  437.   TO_SYSCOL("COLNAME");
  438.   TO_SYSCOL("UNTABID");
  439.   TO_SYSCOL("COLNO");
  440.   TO_SYSCOL("COLTYPE");
  441.   TO_SYSCOL("COLTYPE1");
  442.   TO_SYSCOL("COLTYPE2");
  443.   TO_SYSCOL("DEFVAL");
  444.   TO_SYSCOL("DEFNULL");
  445.   TO_SYSCOL("VALNO");
  446.   TO_SYSCOL("MINVAL");
  447.   TO_SYSCOL("MAXVAL");
  448.   
  449. /*------------ columns of SYSINDEXES to SYSCOLUMNS --------------*/
  450.   
  451.   ABOUT_TABLE (INDEXES, sysindtabid);
  452.   TO_SYSCOL("UNTABID");
  453.   TO_SYSCOL("INXNAME");
  454.   TO_SYSCOL("OWNER");
  455.   TO_SYSCOL("INXTYPE");
  456.   TO_SYSCOL("NCOL");
  457.   TO_SYSCOL("COLNO1");
  458.   TO_SYSCOL("COLNO2");
  459.   TO_SYSCOL("COLNO3");
  460.   TO_SYSCOL("COLNO4");
  461.   TO_SYSCOL("COLNO5");
  462.   TO_SYSCOL("COLNO6");
  463.   TO_SYSCOL("COLNO7");
  464.   TO_SYSCOL("COLNO8");
  465.   TO_SYSCOL("CREDATE");
  466.   TO_SYSCOL("CRETIME");
  467.   TO_SYSCOL("UNINDID");
  468.   
  469. /*------------ columns of SYSTABAUTH to SYSCOLUMNS --------------*/
  470.   
  471.   ABOUT_TABLE (TABAUTH, tabauthtabid);
  472.   TO_SYSCOL("UNTABID");
  473.   TO_SYSCOL("GRANTEE");
  474.   TO_SYSCOL("GRANTOR");
  475.   TO_SYSCOL("TABAUTH");
  476.   
  477. /*------------ columns of SYSCOLAUTH to SYSCOLUMNS --------------*/
  478.   
  479.   ABOUT_TABLE (COLAUTH, colauthtabid);
  480.   TO_SYSCOL("UNTABID");
  481.   TO_SYSCOL("COLNO");
  482.   TO_SYSCOL("GRANTEE");
  483.   TO_SYSCOL("GRANTOR");
  484.   TO_SYSCOL("COLAUTH");
  485.   
  486. /*------------ columns of SYSREFCONSTR to SYSCOLUMNS ------------*/
  487.   
  488.   ABOUT_TABLE (REFCONSTR, refconstrtabid);
  489.   TO_SYSCOL("TABFROM");
  490.   TO_SYSCOL("TABTO");
  491.   TO_SYSCOL("INDTO");
  492.   TO_SYSCOL("NCOLS");
  493.   TO_SYSCOL("COLNOFR1");
  494.   TO_SYSCOL("COLNOFR2");
  495.   TO_SYSCOL("COLNOFR3");
  496.   TO_SYSCOL("COLNOFR4");
  497.   TO_SYSCOL("COLNOFR5");
  498.   TO_SYSCOL("COLNOFR6");
  499.   TO_SYSCOL("COLNOFR7");
  500.   TO_SYSCOL("COLNOFR8");
  501.   TO_SYSCOL("COLNOTO1");
  502.   TO_SYSCOL("COLNOTO2");
  503.   TO_SYSCOL("COLNOTO3");
  504.   TO_SYSCOL("COLNOTO4");
  505.   TO_SYSCOL("COLNOTO5");
  506.   TO_SYSCOL("COLNOTO6");
  507.   TO_SYSCOL("COLNOTO7");
  508.   TO_SYSCOL("COLNOTO8");
  509.   
  510. /*------------ columns of SYSCHCONSTR to SYSCOLUMNS -------------*/
  511.   ABOUT_TABLE (CHCONSTR, chconstrtabid);
  512.   TO_SYSCOL("UNTABID");
  513.   TO_SYSCOL("CHCONID");
  514.   TO_SYSCOL("CONSIZE");
  515.   TO_SYSCOL("NCOLS");
  516.   TO_SYSCOL("COLNO1");
  517.   TO_SYSCOL("COLNO2");
  518.   TO_SYSCOL("COLNO3");
  519.   TO_SYSCOL("COLNO4");
  520.   TO_SYSCOL("COLNO5");
  521.   TO_SYSCOL("COLNO6");
  522.   TO_SYSCOL("COLNO7");
  523.   TO_SYSCOL("COLNO8");
  524.   
  525. /*---------- columns of SYSCHCONSTRTWO to SYSCOLUMNS ------------*/
  526.   
  527.   ABOUT_TABLE (CHCONSTRTWO, chcontwotabid);
  528.   TO_SYSCOL("CHCONID");
  529.   TO_SYSCOL("FRAGNO");
  530.   TO_SYSCOL("FRAGSIZE");
  531.   TO_SYSCOL("FRAG");
  532.   
  533. /*------------ columns of SYSVIEWS to SYSCOLUMNS ----------------*/
  534.   
  535.   ABOUT_TABLE (VIEWS, viewstabid);
  536.   TO_SYSCOL("CHCONID");
  537.   TO_SYSCOL("FRAGNO");
  538.   TO_SYSCOL("FRAGSIZE");
  539.   TO_SYSCOL("FRAG");
  540.   
  541. #undef TO_SYSCOL
  542.   
  543. /*------------------------ SYSTABAUTH filling -------------------------*/
  544.   
  545. #define TO_SYSTABAUTH(tid)    *int1 = tid.untabid; 
  546.                               if (ToAuth ())       
  547.                                 return (-1)
  548.   lencols[0] = sizeof (i4_t);
  549.   lencols[1] = strlen(PUBLIC);
  550.   lencols[2] = SIX;
  551.   lencols[3] = 1;
  552.   colval[0] = int1; /* untabid */
  553.   colval[1] = str1; /* grantee */
  554.   colval[2] = str2; /* grantor */
  555.   colval[3] = str3; /* tabauth */
  556.   
  557.   strcpy (str1, PUBLIC);        /* grantee */
  558.   strcpy (str2, SYSADM);       /* grantor */
  559.   strcpy (str3, "S");  /* tabauth */
  560.   
  561. /*                   tabid  */  
  562.   
  563.   TO_SYSTABAUTH ( syscoltabid    );
  564.   TO_SYSTABAUTH ( sysindtabid    );
  565.   TO_SYSTABAUTH ( viewstabid     );
  566.   TO_SYSTABAUTH ( tabauthtabid   );
  567.   TO_SYSTABAUTH ( colauthtabid   );
  568.   TO_SYSTABAUTH ( refconstrtabid );
  569.   TO_SYSTABAUTH ( chconstrtabid  );
  570.   TO_SYSTABAUTH ( chcontwotabid  );
  571.   lencols[3] = 2;
  572.   strcpy (str3, "Su");  /* tabauth */
  573.   TO_SYSTABAUTH(systabtabid);
  574.   
  575. #undef TO_SYSTABAUTH
  576.   
  577. /*------------------------ SYSCOLAUTH filling -------------------------*/
  578. #define   TOCOLAUTH  {if (TOCOLAuth ()) return (-1);}
  579.   lencols[0] = sizeof (i4_t);
  580.   lencols[1] = sizeof (u2_t);
  581.   lencols[2] = strlen("kml");
  582.   lencols[3] = SIX;
  583.   lencols[4] = strlen("U");
  584.   colval[0] = int1;
  585.   colval[1] = short1;
  586.   colval[2] = str1;
  587.   colval[3] = str2;
  588.   colval[4] = str3;
  589.   *int1 = systabtabid.untabid;  /* untabid */
  590.   strcpy (str1, "kml");         /* grantee */
  591.   strcpy (str2, SYSADM);        /* grantor */
  592.   strcpy (str3, "U");  /* tabauth */
  593.   for (i = 5; i< 14; i++)
  594.     {
  595.       *short1 = i;              /* colno   */
  596.       TOCOLAUTH;
  597.     }
  598.   
  599. #undef TOCOLAUTH
  600. /*------------------------ INDEXES CREATION -------------------------*/
  601. #define CREIND(inm, indid, tabid, cnum, n1, n2, n3, un_fl)               
  602.               if (IndCre (inm, &indid, &tabid, cnum, n1, n2, n3, un_fl)) 
  603.                 return -1;
  604.   
  605.   lencols[0] = sizeof (i4_t);
  606.   lencols[2] = SIX;
  607.   lencols[4] = sizeof (u2_t);
  608.   lencols[5] = sizeof (u2_t);
  609.   lencols[6] = sizeof (u2_t);
  610.   lencols[7] = sizeof (u2_t);
  611.   for (i = 8; i < 13; i++)
  612.     lencols[i] = 0;
  613.   lencols[13] = 8;
  614.   lencols[14] = 8;
  615.   lencols[15] = sizeof (i4_t);
  616.   
  617.   colval[0] = int1;        /* untabid */
  618.   colval[1] = str1;        /* inxname */
  619.   colval[2] = str2;        /* owner   */
  620.   colval[4] = short4;      /* ncol    */
  621.   colval[5] = short1;      /* colno1  */
  622.   for (i = 8; i < 13; i++)
  623.     colval[i] = NULL;      /* colno with number i */
  624.   colval[13] = cur_dt;     /* credate */
  625.   colval[14] = cur_tm;     /* cretime */
  626.   colval[15] = int2;       /* unindid */
  627.   
  628.   strcpy (str2, SYSADM);   /* owner   */
  629.   *char1 = 'U';            /* inxtype */
  630.   
  631.   /*        index_name        indid_ptr         tabid       clm_num      columns unique */
  632.   
  633.   CREIND("SYSTABLESIND1ID",   indid1,           systabtabid,    2,      0,  1, -1,  1)
  634.   CREIND("SYSTABLESIND2ID",   indid2,           systabtabid,    1,      2, -1, -1,  1)
  635.   CREIND("SYSCOLUMNSIND1ID",  indidcol,         syscoltabid,    2,      0,  1, -1,  1)
  636.   CREIND("SYSCOLUMNSIND2ID",  indidcol2,        syscoltabid,    2,      1,  2, -1,  1)
  637.   CREIND("SYSINDEXIND",       sysindexind,      sysindtabid,    1,      0, -1, -1,  0)
  638.   CREIND("SYSTABAUTHIND1ID",  sysauthindid,     tabauthtabid,   2,      0,  1, -1,  1)
  639.   CREIND("SYSCOLAUTHIND1ID",  syscolauthindid,  colauthtabid,   3,      0,  1,  2,  1)
  640.   CREIND("SYSREFCONSTRINDEX", sysrefindid,      refconstrtabid, 1,      0, -1, -1,  0)
  641.   CREIND("SYSREFCONSTRIND1",  sysrefindid1,     refconstrtabid, 1,      1, -1, -1,  0)
  642.   CREIND("SYSCHCONSTRINDEX",  chconstrindid,    chconstrtabid,  1,      0, -1, -1,  0)
  643.   CREIND("SYSCHCONSTRTWOIND", chconstrtwoind,   chcontwotabid,  2,      0,  1, -1,  1)
  644.   CREIND("SYSVIEWSIND",       viewsind,         viewstabid,     2,      0,  1, -1,  1)
  645.     
  646. #undef CREIND
  647.   
  648. /*------------- tables & indexes identifiers saving -------------*/
  649.   fb = fopen (BASE_DAT, "w+");
  650.   if (fb == NULL)
  651.     perror ("can't open base.dat n");
  652.   fseek (fb, 0, SEEK_SET);
  653.   FWR (systabtabid);
  654.   FWR (syscoltabid);
  655.   FWR (sysindtabid);
  656.   FWR (viewstabid);
  657.   FWR (tabauthtabid);
  658.   FWR (colauthtabid);
  659.   FWR (refconstrtabid);
  660.   FWR (chconstrtabid);
  661.   FWR (chcontwotabid);
  662.   FWRID (indid1);
  663.   FWRID (indid2);
  664.   FWRID (indidcol);
  665.   FWRID (indidcol2);
  666.   FWRID (sysindexind);
  667.   FWRID (sysauthindid);
  668.   FWRID (syscolauthindid);
  669.   FWRID (sysrefindid);
  670.   FWRID (sysrefindid1);
  671.   FWRID (chconstrindid);
  672.   FWRID (chconstrtwoind);
  673.   FWRID (viewsind);
  674.   fclose (fb);
  675.   
  676.   if (cl_debug)
  677.     fprintf (STDOUT, "BASE: end of creatbas n");
  678.   initializing_base = 1;
  679.   return (0);
  680. } /* create_base */
  681. result_t*
  682. db_create(void *in, struct svc_req *rqstp)
  683. {
  684.   gsqltrn_rc.sqlcode = create_base ();
  685.   if (!gsqltrn_rc.sqlcode)
  686.     commit ();
  687.   return &gsqltrn_rc;
  688. }