Re: INFORMIX schemas -> C header files
Posted in 1992
Path: emory!sol.ctr.columbia.edu!zaphod.mps.ohio-state.edu!think.com!wupost!uunet!ispd-newsserver!kodak!music!amiano
From: amiano@music.Kodak.COM (Mitch Amiano)
Newsgroups: comp.databases.informix
Message-ID: <1992Mar5.132923.21047@kodak.kodak.com>
Date: 5 Mar 92 13:29:23 GMT
References: <1992Mar3.233906.8757@dbsm.oz.au> <1992Mar5.051017.17231@informix.com>
Sender: amiano@music (Mitch Amiano)
Organization: Eastman Kodak Co., Rochester, NY
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.
Author: Mitch C. Amiano amiano@music.kodak.com
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 'informixCopyright
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
}
' | \\
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 |
| Software Contractor Address: amiano@music.kodak.com |
| Rochester, NY |
| 716-226-2024 |
----------------------------------------------------------------------------