INFORMIX schemas -> C header files 
Author Message
 INFORMIX schemas -> C header files

Does anyone know of a public domain utility that will generate for me
C header files from an INFORMIX database schema?.

Example
-------
create table "informix".books
  (
  bookdate date,
  book char(10),
  dailyrp float,
  dailyurp float,
  mtdrp float,
  mtdurp float,
  ytdrp float,
  ytdurp float
  );

turns into:

typedef struct _DB_BOOKS
        {
  char    bookdate[26];
  char    book[11];
  double  dailyrp;
  double  dailyurp;
  double  mtdrp;
  double  mtdurp;
  double  ytdrp;
  double  ytdurp;
        }
DB_BOOKS;

Can anyone help me?.

Ralph.

Ralph Gyoery.
Midland Montagu Australia Limited.
Dominguez Barry Samuel Montagu.



--

Ralph Gyoery.
Midland Montagu Australia Limited.



Sun, 21 Aug 1994 07:39:06 GMT
 INFORMIX schemas -> C header files


Quote:
>Does anyone know of a public domain utility that will generate for me
>C header files from an INFORMIX database schema?.

The following is now posted into the public domain.  Please feel
free to email me any comments or bugs.  This program will generate
an esql/c template program with c structures for the database and
tables listed.  For example,

  $ esqlutil stores items customers

All output goes to standard out.

mark

/*
  esqlutil.ec

  Generate the beginning of an esql/c program.  Structures for the
  tables specified will be generated as well as the neccesary include
  files and an error checking function.

  cc -o esqlutil esqlutil.ec


  Date:    11-19-91
*/

#include <stdio.h>
$include sqlca;
$include sqlda;
$include sqltypes;

#define sqlerr()           sqlerrln( __LINE__ )

$char  dbname[20] ;

int  ansi ;
int  numproccessed ;

char *progtop[] = {
  "#include <stdio.h>\n\n",
  "#define sqlerr()           sqlerrln( __LINE__ )\n",
  "\n\n",
  0

Quote:
} ;

char *funcs[] = {
  "/*\n",
  "  Checks global sqlca.sqlcode for errors.  Displays error and exits.\n",
  "*/\n",
  "void sqlerrln( ln )\n",
  "",
  "  int  ln ;  /* line # of caller */\n",
  "{\n",
  "  char msg[160] ;\n",
  "",
  "  if( sqlca.sqlcode ) {\n",
  "    fprintf( stderr,\n",
  "      \"sqlcode %ld, isam %ld line %d\\n\", \n",
  "      sqlca.sqlcode, sqlca.sqlerrd[1], ln ) ;\n",
  "    rgetmsg( sqlca.sqlcode, msg, sizeof(msg) ) ;\n",
  "    fprintf( stderr, \"%s\", msg ) ;\n",
  "    exit(1) ;\n",
  "  }\n",
  "}\n",
  "\n\n",
  0

Quote:
} ;

char  *ansiincl[] = {
  "EXEC SQL include sqlca ;\n",
  "EXEC SQL include sqlda ;\n",
  "EXEC SQL include sqlstype ;\n",
  "EXEC SQL include sqltypes ;\n",
  "EXEC SQL include locator ;\n",
/*  "EXEC SQL include decimal ;\n", */
  "EXEC SQL include datetime ;\n",
  "\n\n",
  0

Quote:
} ;

char *incl[] = {
  "$include sqlca ;\n",
  "$include sqlda ;\n",
  "$include sqlstype ;\n",
  "$include sqltypes ;\n",
  "$include locator ;\n",
/*  "$include decimal ;\n", */
  "$include datetime ;\n",
  "\n\n",
  0

Quote:
} ;

char *progbot[] = {
  "main( argc, argv )\n\n",
  "  int   argc ;\n",
  "  char  **argv ;\n",
  "{\n",
  "  printf( \"Hello world\\n\" ) ;\n",
  "  exit(0) ;\n",
  "}\n",
  0

Quote:
} ;

/*
  Checks global sqlca.sqlcode for errors.  Displays error and exits.
*/
void sqlerrln( ln )

  int  ln ;  /* line # of caller */
{
  char msg[160] ;

  if( sqlca.sqlcode ) {
    fprintf( stderr,
      "sqlcode %ld, isam %ld line %d\n",
      sqlca.sqlcode, sqlca.sqlerrd[1], ln ) ;
    rgetmsg( sqlca.sqlcode, msg, sizeof(msg) ) ;
    fprintf( stderr, "%s", msg ) ;
    exit(1) ;
  }

Quote:
}

rtrim( s )

  char *s ;
{
  int i ;

  while( s[ i=strlen(s)-1 ] == ' ' )
    s[i] = '\0' ;

Quote:
}

usage( s )

  char *s ;
{
  fprintf( stderr,
    "usage: %s [-a] dbname tblname ...\n\n", s ) ;
  fprintf( stderr,
    "-a  Generate ansi sql definitions\n\n" ) ;
  fprintf( stderr,
    "Generate esql structure definitions for the indicated tables.\n" ) ;
  fprintf( stderr,
    "If no tables are listed generate structs for all tables in the db.\n" ) ;
  fprintf( stderr,
    "Information is written to standard output.\n" ) ;
  exit( 1 ) ;

Quote:
}

procargs( argc, argv )

  int  argc ;
  char **argv ;
{
  int  first = 1 ;
  int  i ;

  if( argc < 2 ) usage( argv[0] ) ;

  for( i=1; i<argc; ++i ) {
    if( argv[i][0] == '-' ) {
      switch( argv[i][1] ) {
        case 'a' :
          ansi = 1 ;
          break ;
        default:
          usage( argv[0] ) ;
          break ;
      }
    }
    else {
      if( first ) {
        first = 0 ;
        strncpy( dbname, argv[i], 19 ) ;
        dbname[19] = '\0' ;
        $database $dbname ;
        sqlerr() ;
        showstr( progtop ) ;
        includes() ;
      }
      else {
        onetable( argv[i] ) ;
      }
    }
  }

Quote:
}

main( argc, argv )

  int   argc ;
  char  **argv ;
{
  int i ;

  procargs( argc, argv ) ;

  if( numproccessed == 0 )
    alltbl() ;

  showstr( funcs ) ;
  showstr( progbot ) ;

  exit( 0 ) ;

Quote:
}

showstr( s )

  char **s ;
{
  int i ;

  for( i=0; s[i]; ++i )
    printf( "%s", s[i] ) ;

Quote:
}

includes()

{
  int i ;

  if( ansi )
    showstr( ansiincl ) ;
  else
    showstr( incl ) ;

Quote:
}

alltbl()

{
  $char  tbl[20] ;

  $prepare p2 from "select tabname from systables where tabid > 99" ;
  sqlerr() ;

  $declare c1 cursor for p2 ;
  sqlerr() ;

  $open c1 ;
  sqlerr() ;

  while( 1 ) {
    $fetch c1 into $tbl ;
    if( sqlca.sqlcode == 100 ) break ;
    sqlerr() ;
    onetable( tbl ) ;
  }

  $close c1 ;
  sqlerr() ;

Quote:
}

onetable( tbl )

  char *tbl ;
{
  $char                 s[40] ;
  int                   i ;
  struct sqlda          *udesc;
  struct sqlvar_struct  *col;

  rtrim( tbl ) ;

  ++numproccessed ;

  sprintf( s, "select * from %s", tbl ) ;

  $prepare p1 from $s;
  sqlerr() ;

  $describe p1 into udesc;
  sqlerr() ;

  if( ansi ) {
    printf( "EXEC SQL BEGIN DECLARE SECTION\n" ) ;
    printf( "  struct %s_s {\n", tbl ) ;
  }
  else
    printf( "$struct %s_s {\n", tbl ) ;

  for (col = udesc->sqlvar, i = 0; i < udesc->sqld; col++, i++) {

    printf( "  " ) ;
    if( ansi ) printf( "  " ) ;

    switch( col->sqltype ) {
      case SQLVCHAR:
      case SQLCHAR:     printf( "char     " ) ; break ;
      case SQLSERIAL:
      case SQLDATE:    
      case SQLINT:      printf( "long     " ) ; break ;
      case SQLSMINT:    printf( "int      " ) ; break ;
      case SQLFLOAT:    printf( "double   " ) ; break ;
      case SQLSMFLOAT:  printf( "float    " ) ; break ;
      case SQLMONEY:  
      case SQLDECIMAL:  printf( "dec_t    " ) ; break ;
      case SQLDTIME:    printf( "dtime_t  " ) ; break ;
      case SQLTEXT:
      case SQLBYTES:    printf( "loc_t    " ) ; break ;
      case SQLINTERVAL: printf( "intrvl_t " ) ; break ;
      default:          printf( "???      " ) ; break ;
    }

    printf( "%s", col->sqlname ) ;
    if( col->sqltype == SQLCHAR ) printf( "[%d]", col->sqllen + 1 ) ;

    printf( " ;\n" ) ;
  }

  if( ansi ) printf( "  " ) ;
  printf( "} %s_r ;\n", tbl ) ;

  if( ansi ) printf( "EXEC SQL END DECLARE SECTION\n" ) ;
  printf( "\n" ) ;

Quote:
}

--
mark jeske (informix consulting)

708-699-5850


Mon, 22 Aug 1994 13:10:17 GMT
 INFORMIX schemas -> C header files
And now for something completely different.
This is a ditty I recently wrote up. It is shell based, and is somewhat on the slow side,
but it gives just C structures and an appropriate typedef'ed synonym. Use at your own
risk. I thought it would be helpful to compare the ESQL/C to a shell implementation :-)

#---- Cut here -----
:
# dbstruct: dump database table schema and convert into C language structure
# this version is really RAW. use with trepidation.
#
# All Rights Deserved
# Copyright 1992 by Mitch C. Amiano.
# The contents of this file may not be shipped with
# a commercial product without this copyright message.
# No claim is made as to the appropriateness of this software to any task.
# Use at your own risk. No warranty is expressed or implied.
#
# Utilities used: dbschema, fgrep, egrep, sed
#
# Sugar Free dbstruct: No Temp Files; Never had 'em, never will.
#
# Version 0.0.0.1                       Written for SCO-UNIX 3.2.2 ( OpenDeskTop )
#
# Author:       Mitch C. Amiano
#               Software Contractor
#               P O Box 215  Avon  NY   14414
#               716-226-2024
#

cat >&2 <<EOT

 dbstruct: dump database table schema and convert into C language structure

 Copyright 1992 by Mitch C. Amiano.  All Rights Reserved.

 For private use only. The contents of this file may not be shipped with
 any commercial product. Permission is granted to distribute and modify,
 provided that this copyright message appears. This program may not be sold.
 No claim is made as to the appropriateness of this software to any task.
 Use at your own risk. No warranty is expressed or implied.


                Software Contractor
                P O Box 215  Avon  NY   14414
                716-226-2024

 If you have any meaningful additions or modifications to this program,
 I will be glad to hear of them.  
EOT

TABLES=$*

if [ "$TABLES" = "-u" -o "$TABLES" = "-usage" -o "$TABLES" = "-help" ]
then
   cat >&2 <<EOT
dbstruct: dump database tables to stdout in C structure format.

Usage: dbstruct [tablename1 ... tablenameN] > myfile.h
where  tablename  is any table in the current database
  and  myfile.h   is a file name you choose

Table names may be left out, defaulting to ALL tables in the database.
System tables are excluded from the output.
char() field lengths are incremented to allow for a C null (\0) terminator
EOT
exit 0
fi

if [ -z "$TABLES" ]
then
   TABLES=`isql gateway <<EOT 2>/dev/null | egrep -v "tabname|row(s)|f[0-9]+"
   select tabname from systables where tabname not like "sys%"
EOT`
fi

for table in $TABLES
do

   echo ""

   dbschema -d gateway -t $table | \
   fgrep -vy 'informix
Copyright
Software
create unique
create index
' |
   sed "s/smallfloat/float/g; \
s/ integer/ long/g;     \
s/ serial/ long/g;      \
s/ smallint/ short/g;   \
s/ float/ double/g;     \
s/ datetime .*/ dtime_t/g;      \
s/ date .*/ long/g;     \
s/ decimal .*/ dec_t/g; \
s/ money .*/ dec_t/g;   \
/{.*}/d;                        \
s/create table \".*\"\.\(.*\)/\$struct S_\1/g; \
s/create view \".*\"\.\(.*\)/\$struct S_\1/g; \
s/,//g;                 \
s/not null//g;          \
s/char(\(.*\))/char[\1]/g;      \
s/^ *);/};/g;           \
s/^ *(/{/g;             \
/($/d;                  \
/^ *revoke /d;          \
/^$/d;                  \
" | \
   sed "s/^\( *\)\(.*\) char\[\(.*\)\]/\1char \2[\3];/g; \
s/^\( *\)\(.*\) short/\1short \2;/g
s/^\( *\)\(.*\) long/\1long \2;/g; \
s/^\( *\)\(.*\) double/\1double \2;/g; \
s/^\( *\)\(.*\) float/\1float \2;/g; \
s/^\( *\)\(.*\) dtime_t/\1dtime_t \2;/g; \
s/^\( *\)\(.*\) dec_t/\1dec_t \2;/g; \
" | \
   sed -e '/^ *;/!{H; }
/^ *};/{
G
/\$struct/!{d; }
s/\n\$struct S_\([0-9a-zA-Z_]*\).*/$typedef struct S_\1 T_\1; /p

Quote:
}

' | \
   while read Line
   do
      Num=`echo $Line | sed -n "s/[ a-zA-Z_0-9]*\[\(.*\)];/\1/p"`
      if [ -n "$Num" ]
      then
         Num=`expr $Num + 1`
      fi
         echo "$Line" | sed "s/\([[ a-zA-Z_0-9]*\[\).*\(];\)/\1$Num\2/p"

   done

   echo ""
done

#--- Cut here ----

----------------------------------------------------------------------------
| Mitch Amiano                           Current Work Phone: 716-477-3506  |

| Rochester, NY                                                            |
| 716-226-2024                                                             |
----------------------------------------------------------------------------



Mon, 22 Aug 1994 21:29:23 GMT
 
 [ 3 post ] 

 Relevant Pages 

1. HELP Missing header file <QELIB.H>

2. WANTED: Object Pascal header files for DISAM, Faircom c-tree, Informix c-isam etc

3. informix odbc header files on linux

4. Informix Tools Header Files????

5. Informix and ADO support and ADO->ODBC->Informix error

6. Informix Jobs Mail List >>Looking for the best Informix WEB sites

7. Q: Delphi CS 2.0 and informix?

8. CS/ADS and Informix

9. >>>GTA - POWERBUILDER/INFORMIX Developers - Contract

10. >>>INFORMIX/TUXEDO/AIX - Developers - Contract


 
Powered by phpBB® Forum Software