From: Michael Meskes <meskes@topsystem.de>

Well this is not really a patch. But I mananged to get Linus' old Postgres95
precompiler to compile and work with PostgreSQL. The next step would be to
collect bug/missing feature reports and to put it into the distribution so
that it is made with the standard make procedure.

Warning! So far it is not tested much and it does not install correctly. But
I was able to create a small binary with it.
This commit is contained in:
Marc G. Fournier 1998-02-05 15:46:43 +00:00
parent c10908e0d2
commit 82034103ed
32 changed files with 7969 additions and 0 deletions

View File

@ -0,0 +1,4 @@
SUBDIRS = src/include src/lib src/preproc
all install uninstall clean::
for i in $(SUBDIRS); do ( cd $$i; make $@ ); done

48
src/interfaces/ecpg/TODO Normal file
View File

@ -0,0 +1,48 @@
This list is still from Linus. MM
The variables should be static.
The preprocessor interface is strange, to say the least It would be better
with a consistant unix arguments interface, perhaps builtin default
filenames so they won't have to be given all the time.
Preprocessor cannot do syntax checking on your SQL statements Whatever you
write is copied more or less exactly to the postgres95 and you will not be
able to locate your errors until run-time.
No restriction to strings only The PQ interface, and most of all the PQexec
function, that is used by the ecpg relies on that the request is built up as
a string. In some cases, like when the data contains the null character,
this will be a serious problem.
There should be different error numbers for the different errors instead of
just -1 for them all.
Missing library functions to_date et al.
Possibility to define records or structs in the declare section in a way
that the record can be filled from one row in the database. This is a
simpler way to handle an entire row at a time.
Oracle has array operations that enhances speed. When implementing it in
ecpg it is done for compatibility reasons only. For them to improve speed
would require a lot more insight in the postgres internal mechanisms than I
possess.
Oracle has indicator variables that tell if a value is null or if it is
empty. This largely simplifies array operations and provides for a way to
hack around some design flaws in the handling of VARCHAR2 (like that an
empty string isn't distinguishable from a null value). I am not sure if this
is an Oracle extension or part of the ANSI standard.
As well as complex types like records and arrays, typedefs would be a good
thing to take care of.
To set up a database you need a few scripts with table definitions and other
configuration parameters. If you have these scripts for an old database you
would like to just apply them to get a postgres database that works in the
same way. The functionality could be accomplished with some conversion
scripts. Speed will never be accomplished in this way. To do this you need a
bigger insight in the database construction and the use of the database than
could be realised in a script.

View File

@ -0,0 +1,22 @@
# This file is a shell script that caches the results of configure
# tests run on this system so they can be shared between configure
# scripts and configure runs. It is not useful on other systems.
# If it contains results you don't want to keep, you may remove or edit it.
#
# By default, configure uses ./config.cache as the cache file,
# creating it if it does not exist already. You can give configure
# the --cache-file=FILE option to use a different cache file; that is
# what configure does when it calls configure scripts in
# subdirectories, so they share the cache.
# Giving --cache-file=/dev/null disables caching, for debugging configure.
# config.status only pays attention to the cache file if you give it the
# --recheck option to rerun configure.
#
ac_cv_lib_fl_yywrap=${ac_cv_lib_fl_yywrap=yes}
ac_cv_prog_CC=${ac_cv_prog_CC=gcc}
ac_cv_prog_LEX=${ac_cv_prog_LEX=flex}
ac_cv_prog_YACC=${ac_cv_prog_YACC='bison -y'}
ac_cv_prog_cc_cross=${ac_cv_prog_cc_cross=no}
ac_cv_prog_cc_g=${ac_cv_prog_cc_g=yes}
ac_cv_prog_cc_works=${ac_cv_prog_cc_works=yes}
ac_cv_prog_gcc=${ac_cv_prog_gcc=yes}

View File

@ -0,0 +1,12 @@
This file contains any messages produced by compilers while
running configure, to aid debugging if configure makes a mistake.
configure:526: checking for gcc
configure:603: checking whether the C compiler (gcc ) works
configure:617: gcc -o conftest conftest.c 1>&5
configure:637: checking whether the C compiler (gcc ) is a cross-compiler
configure:642: checking whether we are using GNU C
configure:666: checking whether gcc accepts -g
configure:696: checking for flex
configure:729: checking for yywrap in -lfl
configure:775: checking for bison

View File

@ -0,0 +1,152 @@
#! /bin/sh
# Generated automatically by configure.
# Run this file to recreate the current configuration.
# This directory was configured as follows,
# on host gauss:
#
# ./configure
#
# Compiler output produced by configure, useful for debugging
# configure, is in ./config.log if it exists.
ac_cs_usage="Usage: ./config.status [--recheck] [--version] [--help]"
for ac_option
do
case "$ac_option" in
-recheck | --recheck | --rechec | --reche | --rech | --rec | --re | --r)
echo "running ${CONFIG_SHELL-/bin/sh} ./configure --no-create --no-recursion"
exec ${CONFIG_SHELL-/bin/sh} ./configure --no-create --no-recursion ;;
-version | --version | --versio | --versi | --vers | --ver | --ve | --v)
echo "./config.status generated by autoconf version 2.12"
exit 0 ;;
-help | --help | --hel | --he | --h)
echo "$ac_cs_usage"; exit 0 ;;
*) echo "$ac_cs_usage"; exit 1 ;;
esac
done
ac_given_srcdir=.
trap 'rm -fr src/include/Makefile src/lib/Makefile src/preproc/Makefile src/preproc/ecpg conftest*; exit 1' 1 2 15
# Protect against being on the right side of a sed subst in config.status.
sed 's/%@/@@/; s/@%/@@/; s/%g$/@g/; /@g$/s/[\\&%]/\\&/g;
s/@@/%@/; s/@@/@%/; s/@g$/%g/' > conftest.subs <<\CEOF
/^[ ]*VPATH[ ]*=[^:]*$/d
s%@CFLAGS@%-g -O2%g
s%@CPPFLAGS@%%g
s%@CXXFLAGS@%%g
s%@DEFS@% %g
s%@LDFLAGS@%%g
s%@LIBS@%%g
s%@exec_prefix@%${prefix}%g
s%@prefix@%/usr/local%g
s%@program_transform_name@%s,x,x,%g
s%@bindir@%${exec_prefix}/bin%g
s%@sbindir@%${exec_prefix}/sbin%g
s%@libexecdir@%${exec_prefix}/libexec%g
s%@datadir@%${prefix}/share%g
s%@sysconfdir@%${prefix}/etc%g
s%@sharedstatedir@%${prefix}/com%g
s%@localstatedir@%${prefix}/var%g
s%@libdir@%${exec_prefix}/lib%g
s%@includedir@%${prefix}/include%g
s%@oldincludedir@%/usr/include%g
s%@infodir@%${prefix}/info%g
s%@mandir@%${prefix}/man%g
s%@CC@%gcc%g
s%@LEX@%flex%g
s%@LEXLIB@%-lfl%g
s%@YACC@%bison -y%g
s%@TOPSRC@%/home/meskes/data/computer/databases/postgres/pgsql/src/interfaces/ecpg/../..%g
CEOF
# Split the substitutions into bite-sized pieces for seds with
# small command number limits, like on Digital OSF/1 and HP-UX.
ac_max_sed_cmds=90 # Maximum number of lines to put in a sed script.
ac_file=1 # Number of current file.
ac_beg=1 # First line for current file.
ac_end=$ac_max_sed_cmds # Line after last line for current file.
ac_more_lines=:
ac_sed_cmds=""
while $ac_more_lines; do
if test $ac_beg -gt 1; then
sed "1,${ac_beg}d; ${ac_end}q" conftest.subs > conftest.s$ac_file
else
sed "${ac_end}q" conftest.subs > conftest.s$ac_file
fi
if test ! -s conftest.s$ac_file; then
ac_more_lines=false
rm -f conftest.s$ac_file
else
if test -z "$ac_sed_cmds"; then
ac_sed_cmds="sed -f conftest.s$ac_file"
else
ac_sed_cmds="$ac_sed_cmds | sed -f conftest.s$ac_file"
fi
ac_file=`expr $ac_file + 1`
ac_beg=$ac_end
ac_end=`expr $ac_end + $ac_max_sed_cmds`
fi
done
if test -z "$ac_sed_cmds"; then
ac_sed_cmds=cat
fi
CONFIG_FILES=${CONFIG_FILES-"src/include/Makefile src/lib/Makefile src/preproc/Makefile src/preproc/ecpg"}
for ac_file in .. $CONFIG_FILES; do if test "x$ac_file" != x..; then
# Support "outfile[:infile[:infile...]]", defaulting infile="outfile.in".
case "$ac_file" in
*:*) ac_file_in=`echo "$ac_file"|sed 's%[^:]*:%%'`
ac_file=`echo "$ac_file"|sed 's%:.*%%'` ;;
*) ac_file_in="${ac_file}.in" ;;
esac
# Adjust a relative srcdir, top_srcdir, and INSTALL for subdirectories.
# Remove last slash and all that follows it. Not all systems have dirname.
ac_dir=`echo $ac_file|sed 's%/[^/][^/]*$%%'`
if test "$ac_dir" != "$ac_file" && test "$ac_dir" != .; then
# The file is in a subdirectory.
test ! -d "$ac_dir" && mkdir "$ac_dir"
ac_dir_suffix="/`echo $ac_dir|sed 's%^\./%%'`"
# A "../" for each directory in $ac_dir_suffix.
ac_dots=`echo $ac_dir_suffix|sed 's%/[^/]*%../%g'`
else
ac_dir_suffix= ac_dots=
fi
case "$ac_given_srcdir" in
.) srcdir=.
if test -z "$ac_dots"; then top_srcdir=.
else top_srcdir=`echo $ac_dots|sed 's%/$%%'`; fi ;;
/*) srcdir="$ac_given_srcdir$ac_dir_suffix"; top_srcdir="$ac_given_srcdir" ;;
*) # Relative path.
srcdir="$ac_dots$ac_given_srcdir$ac_dir_suffix"
top_srcdir="$ac_dots$ac_given_srcdir" ;;
esac
echo creating "$ac_file"
rm -f "$ac_file"
configure_input="Generated automatically from `echo $ac_file_in|sed 's%.*/%%'` by configure."
case "$ac_file" in
*Makefile*) ac_comsub="1i\\
# $configure_input" ;;
*) ac_comsub= ;;
esac
ac_file_inputs=`echo $ac_file_in|sed -e "s%^%$ac_given_srcdir/%" -e "s%:% $ac_given_srcdir/%g"`
sed -e "$ac_comsub
s%@configure_input@%$configure_input%g
s%@srcdir@%$srcdir%g
s%@top_srcdir@%$top_srcdir%g
" $ac_file_inputs | (eval "$ac_sed_cmds") > $ac_file
fi; done
rm -f conftest.s*
exit 0

1061
src/interfaces/ecpg/configure vendored Normal file

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,15 @@
dnl Process this file with autoconf to produce a configure script.
AC_INIT(src/preproc/type.c)
AC_PROG_CC
AC_PROG_LEX
AC_PROG_YACC
dnl This is actually not pwd, it is the location of the configure file.
dnl This handling, and the fact that not all Makefiles are created by
dnl this configure script makes it impossible to compile somewhere else.
[TOPSRC=`pwd`/../..]
AC_SUBST(TOPSRC)
AC_OUTPUT(src/include/Makefile src/lib/Makefile src/preproc/Makefile src/preproc/ecpg)

View File

@ -0,0 +1,679 @@
\input texinfo @c -*-texinfo-*-
@c %**start of header
@setfilename ecpg
@settitle Ecpg - Embedded SQL in C for Postgres95
@setchapternewpage odd
@c %**end of header
@ifinfo
This file documents an embedded SQL in C package for Postgres 95.
Copyright 1996 Linus Tolke
Permission is granted to copy and use in the same way as you are allowed
to copy and use the rest of the Postgres 95.
@end ifinfo
@c This title page illustrates only one of the
@c two methods of forming a title page.
@titlepage
@title ECPG
@subtitle Embedded SQL in C for Postgres95
@author Linus Tolke
@c The following two commands
@c start the copyright page.
@page
@vskip 0pt plus 1filll
Copyright @copyright{} 1996 Linus Tolke
Published by Linus Tolke
Permission is granted to copy and use in the same way as you are allowed
to copy and use the rest of the Postgres 95.
@end titlepage
@node Top, Why embedded SQL, (dir), (dir)
@comment node-name, next, previous, up
@ifinfo
Ecpg is an embedded sql preprocessor for C and library for Postgres95.
It is written by Linus Tolke <linus@@epact.se>
This texinfo page and the code is all the documentation you get. There
will not be any separate manual page, installation description or
buglist.
@end ifinfo
@menu
* Why embedded SQL::
* Simple description of the concept::
* How to use it::
* How it works::
* Limitations::
* Porting from other DBMSs::
* Installation::
* Index::
--- The Detailed Node Listing ---
How to use it
* Preprocessor::
* Library::
* Error handling::
How it works
* The preprocessor::
* A complete example::
* The library::
Limitations
* What can be done with this concept::
* What will never be included and why::
@end menu
@node Why embedded SQL, Simple description of the concept, Top, Top
@comment node-name, next, previous, up
@chapter Why embedded SQL
Embedded SQL has some small advantages over other ways to handle SQL
queries. It takes care of all the tidious moving of information to and
from variables in your c-program.
There is an ANSI-standard describing how the embedded language should
work. Most embedded sql preprocessors I have seen and heard of makes
extensions so it is difficult to obtain portability even between them
anyway. I have not read the standard but I hope that my implementation
does not deviate to much and that it would be possible to port programs
with embedded sql written for other DBMS:s to Postgres95 and thus
promoting the spirit of free software.
@node Simple description of the concept, How to use it, Why embedded SQL, Top
@comment node-name, next, previous, up
@chapter Simple description of the concept
You write your program in C with some special sql things.
For declaring variables that can be used in SQL statements you need to
put them in a special declare section.
You use a special syntax for the sql queries.
Before compiling you run the file through the embedded sql c
preprocessor and it converts the SQL statements you used to function
calls with the variables used as arguments. Both variables that are used
as input to the SQL statements and variables that will contain the
result are passed.
Then you compile and at link time you link with a special library that
contains the functions used. These functions (actually it is mostly one
single function) fetches the information from the arguments, performs
the SQL query using the ordinary interface (pq) and puts back
the result in the arguments dedicated for output.
Then you run your program and when the control arrives to the SQL
statement the SQL statement is performed against the database and you
can continue with the result.
@node How to use it, How it works, Simple description of the concept, Top
@comment node-name, next, previous, up
@chapter How to use it
This chapter describes how to use the ECPG tool.
@menu
* Preprocessor::
* Library::
* Error handling::
@end menu
@node Preprocessor, Library, How to use it, How to use it
@comment node-name, next, previous, up
@section Preprocessor
@cindex preprocessor
@cindex @code{ecpg}
The preprocessor is called @code{ecpg}. After installation it resides in
the postgres @code{bin} directory. It accepts two arguments like
@code{iname=filename} and @code{oname=filename}. Both arguments must be
present or an error will occur.
In the alpha version the preprocessor has a lot of flaws:
@table @asis
@item Debug text output
It writes every token parsed to the @code{stderr}.
@item Looses line numbering
The line numbers and file name information is lost in the preprocessor.
This means that when running the program through a debugger you end up
in the @code{.c}-file that is the output from the preprocessor and not
in the input to the preprocessor. This can be fixed!
@item The interface is strange, to say the least
It would be better with a consistant unix arguments interface, perhaps
builtin default filenames so they won't have to be given all the time.
@item Cannot do syntax checking on your SQL statements
Whatever you write is copied more or less exactly to the postgres95 and
you will not be able to locate your errors until run-time.
@end table
@node Library, Error handling, Preprocessor, How to use it
@section Library
@cindex library functions
@cindex @code{libecpg.a}
@cindex @code{-lecpg}
The library is called @code{libecpg.a}. The library used the pq library
for the communication to the postgres server so you will have to link
your program with @code{-lecpg -lpq}.
The library has some methods that are "hidden" but that could prove very
useful sometime.
@table @asis
@item @code{ECPGdebug(int)}
@cindex @code{ECPGdebug(int)}
@cindex debuglogging
If this is called, with a non-zero argument, then debuglogging is turned
on. Debuglogging is done on @code{stderr}. Most SQL statement logs its
arguments and result.
The most important one (@code{ECPGdo}) that is called on all SQL
statements except @code{EXEC SQL COMMIT}, @code{EXEC SQL ROLLBACK},
@code{EXEC SQL CONNECT} logs both its expanded string, i.e. the string
with all the input variables inserted, and the result from the
postgres95 server. This can be very useful when searching for errors
in your SQL statements.
@item @code{ECPGstatus()}
@cindex @code{ECPGstatus()}
This method returns TRUE if we are connected to a database and FALSE if
not.
@end table
@node Error handling, , Library, How to use it
@comment node-name, next, previous, up
@section Error handling
@cindex @code{sqlca.h}
@cindex @code{struct sqlca}
@cindex @code{sqlcode}
@cindex @code{error messages}
To be able to detect errors from the postgres server you include a line
like:
@example
exec sql include sqlca;
@end example
in the include section of your file. This will define a struct and a
variable with the name @code{sqlca} as following:
@example
struct sqlca @{
int sqlcode;
struct @{
int sqlerrml;
char sqlerrmc[1000];
@} sqlerrm;
@} sqlca;
@end example
If an error occured in the last SQL statement then @code{sqlca.sqlcode}
will be non-zero. If @code{sqlca.sqlcode} is less that 0 then this is
some kind of serious error, like the database definition does not match
the query given. If it is bigger than 0 then this is a normal error like
the table did not contain the requested row.
sqlca.sqlerrm.sqlerrmc will contain a string that describes the error.
The string ends with @code{line 23.} where the line is the line number
in the source file (actually the file generated by the preprocessor but
I hope I can fix this to be the line number in the input file.)
List of errors that can occur:
@cindex error list
@table @asis
@item -1, Unsupported type %s on line %d.
Does not normally occur. This is a sign that the preprocessor has
generated something that the library does not know about. Perhaps you
are running incompatible versions of the preprocessor and the library.
@item -1, Too many arguments line %d.
@itemx -1, Too few arguments line %d.
The preprocessor has goofed up and generated some incorrect code.
@item -1, Error starting transaction line %d.
Postgres95 signalled to us that we cannot open the connection.
@item -1, Postgres error: %s line %d.
Some postgres95 error. The message contains the error message from the
postgres95 backend.
@item 1, Data not found line %d.
This is a "normal" error that tells you that what you are quering cannot
be found or we have gone through the cursor.
@item -1, To many matches line %d.
This means that the query has returned several lines. The @code{SELECT}
you made probably was not unique.
@item -1, Not correctly formatted int type: %s line %d.
This means that the host variable is of an @code{int} type and the field
in the postgres95 database is of another type and contains a value that
cannot be interpreted as an @code{int}. The library uses @code{strtol}
for this conversion.
@item -1, Not correctly formatted unsigned type: %s line %d.
This means that the host variable is of an @code{unsigned int} type and
the field in the postgres95 database is of another type and contains a
value that cannot be interpreted as an @code{unsigned int}. The library
uses @code{strtoul} for this conversion.
@item -1, Not correctly formatted floating point type: %s line %d.
This means that the host variable is of an @code{float} type and
the field in the postgres95 database is of another type and contains a
value that cannot be interpreted as an @code{float}. The library
uses @code{strtod} for this conversion.
@item -1, Too few arguments line %d.
This means that the postgres95 has returned more records than we have
matching variables. Perhaps you have forgotten a couple of the host
variables in the @code{INTO :var1,:var2}-list.
@item -1, Too many arguments line %d.
This means that th postgres95 has returned fewer records than we have
host variables. Perhaps you have to many host variables in the
@code{INTO :var1,:var2}-list.
@item -1, Empty query line %d.
Postgres95 returned PGRES_EMPTY_QUERY.
@item -1, Error: %s line %d.
Postgres95 returned PGRES_NONFATAL_ERROR, PGRES_FATAL_ERROR or
PGRES_BAD_RESPONSE. Which one and why is hopefully explained in the
message.
@item -1, Postgres error line %d.
Postgres95 returns something that the library does not know how to
handle. This is probably because the version of postgres95 does not
match the version of the ecpg library.
@item -1, Error committing line %d.
Error during @code{COMMIT}. @code{EXEC SQL COMMIT} is translated to an
@code{end} operation in postgres95 and that is the operation that could
not be performed.
@item -1, Error rolling back line %d.
Error during @code{ROLLBACK}. @code{EXEC SQL ROLLBACK} is translated to
an @code{abort} operation in postgres95 and that is the operation that
could not be performed.
@item -1, ECPGconnect: could not open database %s.
The connect to the database did not work.
@end table
@node How it works, Limitations, How to use it, Top
@chapter How it works
@comment node-name, next, previous, up
This chapter describes how the things work. The ambition is to make this
chapter contain things for those that want to have a look inside and the
chapter on How to use it should be enough for all normal questions.
So, read this before looking at the internals of the @code{ecpg}. If
you are not interested in how it really works, skip this chapter.
@menu
* The preprocessor::
* A complete example::
* The library::
@end menu
@node The preprocessor, A complete example, How it works, How it works
@comment node-name, next, previous, up
@section The preprocessor
First three lines are written to the output. A comment and two include
lines necessary for the interface to the library.
Then the preprocessor works in one pass only reading the input file and
writing to the output as it goes along. Normally it just echoes
everything to the output without looking at it further.
When it comes to an @code{EXEC SQL} statements it interviens and
changes them depending on what iit is. The @code{EXEC SQL} statement can
be one of these:
@itemize @bullet
@item Declare sections
@cindex Declare section
Declare sections begins with
@example
exec sql begin declare section;
@end example
and ends with
@example
exec sql end declare section;
@end example
In the section only variable declarations are allowed. Every variable
declare within this section is also entered in a list of variables
indexed on their name together with the corresponding type.
The declaration is echoed to the file to make the variable a normal
C-variable also.
The special types VARCHAR and VARCHAR2 are converted into a named struct
for every variable. A declaration like:
@example
VARCHAR var[180];
@end example
is converted into
@example
struct varchar_var @{ int len; char arr[180]; @} var;
@end example
@item Include statements
@cindex Include statement
An include statement looks like:
@example
exec sql include filename;
@end example
It is converted into
@example
#include <filename.h>
@end example
@item Connect statement
@cindex Connect statement
A connect statements looks like:
@example
exec sql connect 'databasename';
@end example
That statement is converted into
@example
ECPGconnect("databasename");
@end example
@item Open cursor statement
@cindex Open cursor statement
An open cursor statement looks like:
@example
exec sql open blablabla;
@end example
and that is ignore and not copied from the output.
@item Commit statement
@cindex Commit statement
A commit statement looks like
@example
exec sql commit;
@end example
and is translated on the output to
@example
ECPGcommit(__LINE__);
@end example
@item Rollback statement
@cindex Rollback statement
A rollback statement looks like
@example
exec sql rollback;
@end example
and is translated on the output to
@example
ECPGrollback(__LINE__);
@end example
@item Other statements
Other SQL statements are other statements that start with
@code{exec sql} and ends with @code{;}. Everything inbetween is treated
as an sql statement and parsed for variable substitution.
Variable substitution occur when a symbol starts with a colon
(@code{:}). Then a variable with that name is found among the variables
that were previously declared within a declare section and depending on
whether or not the SQL statements knows it to be a variable for input or
output the pointers to the variables are written to the output to allow
for access by the function.
For every variable that is part of the SQL request the function gets
another five arguments.
@enumerate
@item The type as a special symbol
@item A pointer to the value
@item The size of the variable if it is a varchar
@item Number of elements in the array (for array fetches)
@item The offset to the next element in the array (for array fetches)
@end enumerate
Since the array fetches are not implemented yet the two last arguments
are not really important. They could perhaps have been left out.
@end itemize
@node A complete example, The library, The preprocessor, How it works
@comment node-name, next, previous, up
@section A complete example
Here is a complete example describing the output of the preprocessor:
@example
exec sql begin declare section;
int index;
int result;
exec sql end declare section;
...
exec sql select res into :result from mytable where index = :index;
@end example
is translated into:
@example
/* These two include files are added by the preprocessor */
#include <ecpgtype.h>
#include <ecpglib.h>
/* exec sql begin declare section */
int index;
int result;
/* exec sql end declare section */
...
ECPGdo(__LINE__, "select res from mytable where index = ;;",
ECPGt_int,&index,0,0,sizeof(int),
ECPGt_EOIT,
ECPGt_int,&result,0,0,sizeof(int),
ECPGt_EORT );
@end example
(the indentation in this manual is added for readability and not
something that the preprocessor can do.)
@node The library, , A complete example, How it works
@comment node-name, next, previous, up
@section The library
The most important function in the library is the @code{ECPGdo}
function. It takes a variable amount of arguments. Hopefully we wont run
into machines with limits on the amount of variables that can be
accepted by a varchar function. This could easily add up to 50 or so
arguments.
The arguments are:
@table @asis
@item A line number
This is a line number for the original line used in error messages only.
@item A string
This is the sql request that is to be issued. This request is modified
by the input variables, i.e. the variables that where not known at
compile time but are to be entered in the request. Where the variables
should go the string contains @code{;;}.
@item Input variables
As described in the section about the preprocessor every input variable
gets five arguments.
@item ECPGt_EOIT
An enum telling that there are no more input variables.
@item Output variables
As described in the section about the preprocessor every input variable
gets five arguments. These variables are filled by the function.
@item ECPGt_EORT
An enum telling that there are no more variables.
@end table
All the SQL statements are performed in one transaction unless you issue
a commit transaction. This works so that the first transaction or the
first after a commit or rollback always begins a transaction.
To be completed: entries describing the other entries.
@node Limitations, Porting from other DBMSs, How it works, Top
@chapter Limitations
@comment node-name, next, previous, up
I separate the limitations in two different groups. Those that are of
the kind that I have not gotten around to it yet and those that I will
never bother to look at.
@menu
* What can be done with this concept::
* What will never be included and why::
@end menu
@node What can be done with this concept, What will never be included and why, Limitations, Limitations
@comment node-name, next, previous, up
@section What can be done with this concept
This is a list of things that I have plans to include in some future.
@table @asis
@item no restriction to strings only
The PQ interface, and most of all the PQexec function, that is used by
the ecpg relies on that the request is built up as a string. In some
cases, like when the data contains the null character, this will be a
serious problem.
@item line numbering
The preprocessor should generate output with directions to the compiler
to generate debugging code including the file name and line numbers of
the input to the preprocessor.
@item error codes
There should be different error numbers for the different errors instead
of just -1 for them all.
@item library functions
to_date et al.
@item records
@cindex records
Possibility to define records or @code{struct}s in the declare section
in a way that the record can be filled from one row in the database.
This is a simpler way to handle an entire row at a time.
@item array operations
@cindex array operations
Oracle has array operations that enhances speed. When implementing it in
@code{ecpg} it is done for compatibility reasons only. For them to
improve speed would require a lot more insight in the postgres internal
mechanisms than I possess.
@item indicator variables
@cindex indicator variables
@cindex @code{VARCHAR2}
Oracle has indicator variables that tell if a value is @code{null} or if
it is empty. This largely simplifies array operations and provides for a
way to hack around some design flaws in the handling of @code{VARCHAR2}
@footnote{like that an empty string isn't distinguishable from a
@code{null} value}. I am not sure if this is an Oracle extension or part
of the ANSI standard.
@item typedefs
@cindex typedef
As well as complex types like records and arrays, typedefs would be
a good thing to take care of.
@item conversion of scripts
@cindex conversion of scripts
To set up a database you need a few scripts with table definitions and
other configuration parameters. If you have these scripts for an old
database you would like to just apply them to get a postgres database
that works in the same way.
The functionality could be accomplished with some conversion scripts.
Speed will never be accomplished in this way. To do this you need a
bigger insight in the database construction and the use of the database
than could be realised in a script.
@end table
@node What will never be included and why, , What can be done with this concept, Limitations
@comment node-name, next, previous, up
@section What will never be included and why
@table @asis
@item oracles single tasking possibility
@cindex single tasking
Oracle version 7.0 on AIX 3 uses the OS-supported locks on the shared
memory segments and allows the application designer to link an
application in a so called single tasking way. Instead of starting one
client process per application process both the database part and the
application part is run in the same process. In later versions of oracle
this is no longer supported.
This would require a total redesign of the postgres access model and
that effort can not justify the performance gained.
@end table
@node Porting from other DBMSs, Installation, Limitations, Top
@chapter Porting from other DBMSs
@comment node-name, next, previous, up
To be written by persons that knows the different DBMSs and that
actually does port something...
@node Installation, Index, Porting from other DBMSs, Top
@comment node-name, next, previous, up
@chapter Installation
@cindex installation
Step by step installation (if everything goes ok):
@enumerate
@item Fetch everything and unpack
If you are reading this documentation you have probably managed this
step already.
@item @code{./configure --with-postgres=/path/to/postgres}
This is to be done in the ecpg directory, i.e. the directory containing
the @file{configure} file.
The @file{/path/to/postgres} is the path to the installed postgres. It
points out the directory where the include, lib and bin directories
reside. The include directory is used when building the library and all
three of them become residents for ecpg include files, library and
binaries.
@item @code{make all}
@item As the postgres user @code{make install}
The postgres user is the owner of the postgres include, lib and bin
directories. The installation procedure installs its files there
alongside the postgres files.
@item Done.
@end enumerate
@node Index, , Installation, Top
@unnumbered Index
@printindex cp
@contents

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,16 @@
# Generated automatically from Makefile.in by configure.
POSTGRESTOP=@POSTGRESERVER@
POSTGRES_INCLUDE=$(POSTGRESTOP)/include
all clean::
@echo Nothing to be done.
install::
install ecpglib.h $(POSTGRES_INCLUDE)
install ecpgtype.h $(POSTGRES_INCLUDE)
install sqlca.h $(POSTGRES_INCLUDE)
uninstall::
rm -f $(POSTGRES_INCLUDE)/ecpglib.h
rm -f $(POSTGRES_INCLUDE)/ecpgtype.h
rm -f $(POSTGRES_INCLUDE)/sqlca.h

View File

@ -0,0 +1,15 @@
POSTGRESTOP=@POSTGRESERVER@
POSTGRES_INCLUDE=$(POSTGRESTOP)/include
all clean::
@echo Nothing to be done.
install::
install ecpglib.h $(POSTGRES_INCLUDE)
install ecpgtype.h $(POSTGRES_INCLUDE)
install sqlca.h $(POSTGRES_INCLUDE)
uninstall::
rm -f $(POSTGRES_INCLUDE)/ecpglib.h
rm -f $(POSTGRES_INCLUDE)/ecpgtype.h
rm -f $(POSTGRES_INCLUDE)/sqlca.h

View File

@ -0,0 +1,27 @@
#include <c.h>
void ECPGdebug(int);
bool ECPGconnect(const char * dbname);
bool ECPGdo(int, char *, ...);
bool ECPGcommit(int);
bool ECPGrollback(int);
bool ECPGfinish();
bool ECPGstatus();
void ECPGlog(const char * format, ...);
#ifdef LIBPQ_FE_H
bool ECPGsetdb(PGconn *);
#endif
/* Here are some methods used by the lib. */
/* Returns a pointer to a string containing a simple type name. */
const char * ECPGtype_name(enum ECPGttype);
/* A generic varchar type. */
struct ECPGgeneric_varchar {
int len;
char arr[1];
};

View File

@ -0,0 +1,44 @@
/*
* This file implements a data structure that is built and maintained by the
* preprocessor.
*
* All types that can be handled for host variable declarations has to
* be handled eventually.
*/
/*
* Here are all the types that we are to handle. Note that it is the type
* that is registered and that has nothing whatsoever to do with the storage
* class.
*
* Simle types
* integers: char, short, int, long (signed and unsigned)
* floats: float, double
*
* Complex types:
* VARCHAR, VARCHAR2 - Strings with length (maxlen is given in the declaration)
* Arrays of simple types and of VARCHAR, VARCHAR2 (size given in declaration)
* Records build of simple types, arrays and other records.
*
* Complicating things:
* typedefs and struct names!
*
* Conclusion:
* This is a typically recursive definition. A structure of typed list elements
* would probably work fine:
*/
#include <stdio.h>
enum ECPGttype {
ECPGt_char = 1, ECPGt_unsigned_char, ECPGt_short, ECPGt_unsigned_short,
ECPGt_int, ECPGt_unsigned_int, ECPGt_long, ECPGt_unsigned_long,
ECPGt_float, ECPGt_double,
ECPGt_varchar, ECPGt_varchar2,
ECPGt_array,
ECPGt_record,
ECPGt_EOIT, /* End of insert types. */
ECPGt_EORT /* End of result types. */
};
#define IS_SIMPLE_TYPE(type) ((type) >= ECPGt_char && (type) <= ECPGt_varchar2)

View File

@ -0,0 +1,11 @@
#ifndef POSTGRES_SQLCA_H
#define POSTGRES_SQLCA_H
struct sqlca {
int sqlcode;
struct {
int sqlerrml;
char sqlerrmc[1000];
} sqlerrm;
} sqlca;
#endif

View File

@ -0,0 +1,24 @@
# Generated automatically from Makefile.in by configure.
TOPDIR=/home/meskes/data/computer/databases/postgres/pgsql/src/interfaces/ecpg/../..
PQ_INCLUDE=-I$(TOPDIR)/include -I$(TOPDIR)/interfaces/libpq
POSTGRES_LIB=$(POSTGRESTOP)/lib
all: lib
lib: libecpg.a
clean::
rm -f *.o *.a core a.out *~
install:: libecpg.a
install -m644 libecpg.a $(POSTGRES_LIB)
uninstall::
rm -f $(POSTGRES_LIB)/libecpg.a
# Rules that do something
libecpg.a : libecpg.a(ecpglib.o) libecpg.a(typename.o)
ecpglib.o : ecpglib.c ../include/ecpglib.h ../include/ecpgtype.h
$(CC) -O2 -g -Wall -I../include $(PQ_INCLUDE) -c ecpglib.c
typename.o : typename.c ../include/ecpgtype.h
$(CC) -g -O2 -Wall -I../include $(PQ_INCLUDE) -c typename.c

View File

@ -0,0 +1,23 @@
TOPDIR=@TOPSRC@
PQ_INCLUDE=-I$(TOPDIR)/include -I$(TOPDIR)/interfaces/libpq
POSTGRES_LIB=$(POSTGRESTOP)/lib
all: lib
lib: libecpg.a
clean::
rm -f *.o *.a core a.out *~
install:: libecpg.a
install -m644 libecpg.a $(POSTGRES_LIB)
uninstall::
rm -f $(POSTGRES_LIB)/libecpg.a
# Rules that do something
libecpg.a : libecpg.a(ecpglib.o) libecpg.a(typename.o)
ecpglib.o : ecpglib.c ../include/ecpglib.h ../include/ecpgtype.h
$(CC) -O2 -g -Wall -I../include $(PQ_INCLUDE) -c ecpglib.c
typename.o : typename.c ../include/ecpgtype.h
$(CC) -g -O2 -Wall -I../include $(PQ_INCLUDE) -c typename.c

View File

@ -0,0 +1,609 @@
/* Copyright comment */
/*
* The aim is to get a simpler inteface to the database routines.
* All the tidieous messing around with tuples is supposed to be hidden
* by this function.
*/
/* Author: Linus Tolke
(actually most if the code is "borrowed" from the distribution and just
slightly modified)
*/
/* Taken over as part of PostgreSQL by Michael Meskes <meskes@debian.org>
on Feb. 5th, 1998 */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdarg.h>
#include <string.h>
#include <ecpgtype.h>
#include <ecpglib.h>
#include <sqlca.h>
#include <libpq-fe.h>
#include <libpq/pqcomm.h>
static PGconn * simple_connection;
static int simple_debug = 0;
static int committed = true;
static void
register_error(int code, char *fmt, ...)
{
va_list args;
sqlca.sqlcode = code;
va_start (args, fmt);
vsprintf (sqlca.sqlerrm.sqlerrmc, fmt, args);
va_end (args);
sqlca.sqlerrm.sqlerrml = strlen (sqlca.sqlerrm.sqlerrmc);
}
/* This function returns a newly malloced string that has the ' and \
in the argument quoted with \.
*/
static
char *
quote_postgres(char * arg)
{
char * res = (char *)malloc(2 * strlen(arg) + 1);
int i, ri;
for (i = 0, ri = 0; arg[i]; i++, ri++)
{
switch (arg[i])
{
case '\'':
case '\\':
res[ri++] = '\\';
default:
;
}
res[ri] = arg[i];
}
res[ri] = '\0';
return res;
}
bool
ECPGdo(int lineno, char * query, ...)
{
va_list ap;
bool status = false;
char * copiedquery;
PGresult * results;
PGnotify * notify;
enum ECPGttype type;
va_start(ap, query);
sqlca.sqlcode = 0;
copiedquery = strdup(query);
type = va_arg(ap, enum ECPGttype);
/*
* Now, if the type is one of the fill in types then we take the argument
* and enter that in the string at the first %s position. Then if there
* are any more fill in types we fill in at the next and so on.
*/
while (type != ECPGt_EOIT)
{
void * value = NULL;
short varcharsize;
short size;
short arrsize;
char * newcopy;
char * mallocedval = NULL;
char * tobeinserted = NULL;
char * p;
char buff[20];
/* Some special treatment is needed for records since we want their
contents to arrive in a comma-separated list on insert (I think). */
value = va_arg(ap, void *);
varcharsize = va_arg(ap, short);
size = va_arg(ap, short);
arrsize = va_arg(ap, short);
switch (type) {
case ECPGt_char:
case ECPGt_short:
case ECPGt_int:
sprintf(buff, "%d", *(int*)value);
tobeinserted = buff;
break;
case ECPGt_unsigned_char:
case ECPGt_unsigned_short:
case ECPGt_unsigned_int:
sprintf(buff, "%d", *(unsigned int*)value);
tobeinserted = buff;
break;
case ECPGt_long:
sprintf(buff, "%ld", *(long*)value);
tobeinserted = buff;
break;
case ECPGt_unsigned_long:
sprintf(buff, "%ld", *(unsigned long*)value);
tobeinserted = buff;
break;
case ECPGt_float:
sprintf(buff, "%.14g", *(float*)value);
tobeinserted = buff;
break;
case ECPGt_double:
sprintf(buff, "%.14g", *(double*)value);
tobeinserted = buff;
break;
case ECPGt_varchar:
case ECPGt_varchar2:
{
struct ECPGgeneric_varchar * var =
(struct ECPGgeneric_varchar*)value;
newcopy = (char *)malloc(var->len + 1);
strncpy(newcopy, var->arr, var->len);
newcopy[var->len] = '\0';
mallocedval = (char *)malloc(2 * strlen(newcopy) + 3);
strcpy(mallocedval, "'");
strcat(mallocedval, quote_postgres(newcopy));
strcat(mallocedval, "'");
free(newcopy);
tobeinserted = mallocedval;
}
break;
default:
/* Not implemented yet */
register_error(-1, "Unsupported type %s on line %d.",
ECPGtype_name(type), lineno);
return false;
break;
}
/* Now tobeinserted points to an area that is to be inserted at
the first %s
*/
newcopy = (char *)malloc(strlen(copiedquery)
+ strlen(tobeinserted)
+ 1);
strcpy(newcopy, copiedquery);
if ((p = strstr(newcopy, ";;")) == NULL)
{
/* We have an argument but we dont have the matched up string
in the string
*/
register_error(-1, "Too many arguments line %d.", lineno);
return false;
}
else
{
strcpy(p, tobeinserted);
/* The strange thing in the second argument is the rest of the
string from the old string */
strcat(newcopy,
copiedquery
+ ( p - newcopy )
+ 2 /* Length of ;; */);
}
/* Now everything is safely copied to the newcopy. Lets free the
oldcopy and let the copiedquery get the value from the newcopy.
*/
if (mallocedval != NULL)
{
free(mallocedval);
mallocedval = NULL;
}
free(copiedquery);
copiedquery = newcopy;
type = va_arg(ap, enum ECPGttype);
}
/* Check if there are unmatched things left. */
if (strstr(copiedquery, ";;") != NULL)
{
register_error(-1, "Too few arguments line %d.", lineno);
return false;
}
/* Now then request is built. */
if (committed)
{
if ((results = PQexec (simple_connection, "begin")) == NULL) {
register_error(-1, "Error starting transaction line %d.", lineno);
return false;
}
PQclear (results);
committed = 0;
}
ECPGlog("ECPGdo line %d: QUERY: %s\n", lineno, copiedquery);
results = PQexec(simple_connection, copiedquery);
free(copiedquery);
if (results == NULL)
{
ECPGlog("ECPGdo line %d: error: %s", lineno,
PQerrorMessage(simple_connection));
register_error(-1, "Postgres error: %s line %d.",
PQerrorMessage(simple_connection), lineno);
}
else switch(PQresultStatus(results))
{
int m,n,x;
case PGRES_TUPLES_OK:
/* XXX Cheap Hack. For now, we see only the last group
* of tuples. This is clearly not the right
* way to do things !!
*/
m = PQnfields(results);
n = PQntuples(results);
if (n < 1)
{
ECPGlog("ECPGdo lineno %d: Incorrect number of matches: %d\n",
lineno, n);
register_error(1, "Data not found line %d.", lineno);
break;
}
if (n > 1)
{
ECPGlog("ECPGdo line %d: Incorrect number of matches: %d\n",
lineno, n);
register_error(-1, "To many matches line %d.", lineno);
break;
}
status = true;
for (x = 0; x < m && status; x++)
{
void * value = NULL;
short varcharsize;
short size;
short arrsize;
char *pval = PQgetvalue(results,0,x);
/*long int * res_int;
char ** res_charstar;
char * res_char;
int res_len;*/
char * scan_length;
ECPGlog("ECPGdo line %d: RESULT: %s\n", lineno, pval ? pval : "");
/* No the pval is a pointer to the value. */
/* We will have to decode the value */
type = va_arg(ap, enum ECPGttype);
value = va_arg(ap, void *);
varcharsize = va_arg(ap, short);
size = va_arg(ap, short);
arrsize = va_arg(ap, short);
switch (type)
{
long res;
unsigned long ures;
double dres;
case ECPGt_char:
case ECPGt_short:
case ECPGt_int:
case ECPGt_long:
if (pval)
{
res = strtol(pval, &scan_length, 10);
if (*scan_length != '\0') /* Garbage left */
{
register_error(-1, "Not correctly formatted int type: %s line %d.",
pval, lineno);
status = false;
res = 0L;
}
}
else
res = 0L;
/* Again?! Yes */
switch (type)
{
case ECPGt_char:
*(char *)value = (char)res;
break;
case ECPGt_short:
*(short *)value = (short)res;
break;
case ECPGt_int:
*(int *)value = (int)res;
break;
case ECPGt_long:
*(long *)value = res;
break;
default:
/* Cannot happen */
break;
}
break;
case ECPGt_unsigned_char:
case ECPGt_unsigned_short:
case ECPGt_unsigned_int:
case ECPGt_unsigned_long:
if (pval)
{
ures = strtoul(pval, &scan_length, 10);
if (*scan_length != '\0') /* Garbage left */
{
register_error(-1, "Not correctly formatted unsigned type: %s line %d.",
pval, lineno);
status = false;
ures = 0L;
}
}
else
ures = 0L;
/* Again?! Yes */
switch (type)
{
case ECPGt_unsigned_char:
*(unsigned char *)value = (unsigned char)ures;
break;
case ECPGt_unsigned_short:
*(unsigned short *)value = (unsigned short)ures;
break;
case ECPGt_unsigned_int:
*(unsigned int *)value = (unsigned int)ures;
break;
case ECPGt_unsigned_long:
*(unsigned long *)value = ures;
break;
default:
/* Cannot happen */
break;
}
break;
case ECPGt_float:
case ECPGt_double:
if (pval)
{
dres = strtod(pval, &scan_length);
if (*scan_length != '\0') /* Garbage left */
{
register_error(-1, "Not correctly formatted floating point type: %s line %d.",
pval, lineno);
status = false;
dres = 0.0;
}
}
else
dres = 0.0;
/* Again?! Yes */
switch (type)
{
case ECPGt_float:
*(float *)value = (float)res;
break;
case ECPGt_double:
*(double *)value = res;
break;
default:
/* Cannot happen */
break;
}
break;
case ECPGt_varchar:
{
struct ECPGgeneric_varchar * var =
(struct ECPGgeneric_varchar*)value;
strncpy(var->arr, pval, varcharsize);
var->len = strlen(pval);
if (var->len > varcharsize)
var->len = varcharsize;
}
break;
case ECPGt_EORT:
ECPGlog("ECPGdo line %d: Too few arguments.\n", lineno);
register_error(-1, "Too few arguments line %d.", lineno);
status = false;
break;
default:
register_error(-1, "Unsupported type %s on line %d.",
ECPGtype_name(type), lineno);
return false;
break;
}
}
type = va_arg(ap, enum ECPGttype);
if (status && type != ECPGt_EORT)
{
register_error(-1, "Too many arguments line %d.", lineno);
return false;
}
PQclear(results);
break;
case PGRES_EMPTY_QUERY:
/* do nothing */
register_error(-1, "Empty query line %d.", lineno);
break;
case PGRES_COMMAND_OK:
status = true;
ECPGlog("ECPGdo line %d Ok: %s\n", lineno, PQcmdStatus(results));
break;
case PGRES_NONFATAL_ERROR:
case PGRES_FATAL_ERROR:
case PGRES_BAD_RESPONSE:
ECPGlog("ECPGdo line %d: Error: %s",
lineno, PQerrorMessage(simple_connection));
register_error(-1, "Error: %s line %d.",
PQerrorMessage(simple_connection), lineno);
break;
case PGRES_COPY_OUT:
ECPGlog("ECPGdo line %d: Got PGRES_COPY_OUT ... tossing.\n", lineno);
PQendcopy(results->conn);
break;
case PGRES_COPY_IN:
ECPGlog("ECPGdo line %d: Got PGRES_COPY_IN ... tossing.\n", lineno);
PQendcopy(results->conn);
break;
default:
ECPGlog("ECPGdo line %d: Got something else, postgres error.\n",
lineno);
register_error(-1, "Postgres error line %d.", lineno);
break;
}
/* check for asynchronous returns */
notify = PQnotifies(simple_connection);
if (notify) {
ECPGlog("ECPGdo line %d: ASYNC NOTIFY of '%s' from backend pid '%d' received\n",
lineno, notify->relname, notify->be_pid);
free(notify);
}
va_end(ap);
return status;
}
bool
ECPGcommit(int lineno)
{
PGresult *res;
ECPGlog("ECPGcommit line %d\n", lineno);
if ((res = PQexec (simple_connection, "end")) == NULL) {
register_error(-1, "Error committing line %d.", lineno);
return (FALSE);
}
PQclear (res);
committed = 1;
return (TRUE);
}
bool
ECPGrollback(int lineno)
{
PGresult *res;
ECPGlog("ECPGrollback line %d\n", lineno);
if ((res = PQexec (simple_connection, "abort")) == NULL) {
register_error(-1, "Error rolling back line %d.", lineno);
return (FALSE);
}
PQclear (res);
committed = 1;
return(TRUE);
}
bool
ECPGsetdb(PGconn * newcon)
{
ECPGfinish();
simple_connection = newcon;
return true;
}
bool
ECPGconnect(const char * dbname)
{
char * name = strdup(dbname);
ECPGlog("ECPGconnect: opening database %s\n", name);
sqlca.sqlcode = 0;
ECPGsetdb(PQsetdb(NULL, NULL, NULL, NULL, name));
free(name);
name = NULL;
if (PQstatus(simple_connection) == CONNECTION_BAD)
{
ECPGfinish();
ECPGlog("ECPGconnect: could not open database %s\n", dbname);
register_error(-1, "ECPGconnect: could not open database %s.", dbname);
return false;
}
return true;
}
bool
ECPGstatus()
{
return PQstatus(simple_connection) != CONNECTION_BAD;
}
bool
ECPGfinish()
{
if (simple_connection != NULL)
{
ECPGlog("ECPGfinish: finishing.\n");
PQfinish(simple_connection);
}
else
ECPGlog("ECPGfinish: called an extra time.\n");
return true;
}
void
ECPGdebug(int n)
{
simple_debug = n;
ECPGlog("ECPGdebug: set to %d\n", simple_debug);
}
void
ECPGlog(const char * format, ...)
{
va_list ap;
if (simple_debug)
{
char * f = (char *) malloc(strlen(format) + 100);
sprintf(f, "[%d]: %s", getpid(), format);
va_start(ap, format);
vfprintf(stderr, f, ap);
va_end(ap);
free(f);
}
}

View File

@ -0,0 +1,23 @@
#include <ecpgtype.h>
/*
* This function is used to generate the correct type names.
*/
const char *
ECPGtype_name(enum ECPGttype typ)
{
switch (typ)
{
case ECPGt_char: return "char";
case ECPGt_unsigned_char: return "unsigned char";
case ECPGt_short: return "short";
case ECPGt_unsigned_short: return "unsigned short";
case ECPGt_int: return "int";
case ECPGt_unsigned_int: return "unsigned int";
case ECPGt_long: return "long";
case ECPGt_unsigned_long: return "unsigned long";
case ECPGt_float: return "float";
case ECPGt_double: return "double";
default:
abort();
}
}

View File

@ -0,0 +1,37 @@
# Generated automatically from Makefile.in by configure.
POSTGRESTOP=@POSTGRESERVER@
POSTGRES_BIN=$(POSTGRESTOP)/bin
POSTGRES_LIB=$(POSTGRESTOP)/lib
CC=gcc
LEX=flex
LEXLIB=-lfl
YACC=bison -y
CFLAGS=-I../include -O2 -g -Wall
all:: ecpg
clean::
rm -f *.o core a.out ecpg y.tab.h y.tab.c *~
install:: all
install -c -d -m755 $(POSTGRES_LIB)/ecpg
install -c -m555 preproc $(POSTGRES_LIB)/ecpg
install -c -m555 ecpg $(POSTGRES_BIN)
uninstall::
rm -f $(POSTGRES_BIN)/ecpg
rm -f $(POSTGRES_LIB)/ecpg/preproc
# Rule that really do something.
ecpg: y.tab.o pgc.o type.o ecpg.o
$(CC) -g -O2 -Wall -o ecpg y.tab.o pgc.o type.o ecpg.o -L../lib -lecpg $(LEXLIB)
y.tab.h y.tab.c: preproc.y
$(YACC) -d $<
y.tab.o : y.tab.h ../include/ecpgtype.h
type.o : ../include/ecpgtype.h
pgc.o : ../include/ecpgtype.h

View File

@ -0,0 +1,36 @@
POSTGRESTOP=@POSTGRESERVER@
POSTGRES_BIN=$(POSTGRESTOP)/bin
POSTGRES_LIB=$(POSTGRESTOP)/lib
CC=@CC@
LEX=@LEX@
LEXLIB=@LEXLIB@
YACC=@YACC@
CFLAGS=-I../include -O2 -g -Wall
all:: ecpg
clean::
rm -f *.o core a.out ecpg y.tab.h y.tab.c *~
install:: all
install -c -d -m755 $(POSTGRES_LIB)/ecpg
install -c -m555 preproc $(POSTGRES_LIB)/ecpg
install -c -m555 ecpg $(POSTGRES_BIN)
uninstall::
rm -f $(POSTGRES_BIN)/ecpg
rm -f $(POSTGRES_LIB)/ecpg/preproc
# Rule that really do something.
ecpg: y.tab.o pgc.o type.o ecpg.o
$(CC) -g -O2 -Wall -o ecpg y.tab.o pgc.o type.o ecpg.o -L../lib -lecpg $(LEXLIB)
y.tab.h y.tab.c: preproc.y
$(YACC) -d $<
y.tab.o : y.tab.h ../include/ecpgtype.h
type.o : ../include/ecpgtype.h
pgc.o : ../include/ecpgtype.h

View File

@ -0,0 +1,16 @@
/* New main for ecpg, the PostgreSQL embedded SQL precompiler. */
/* (C) Michael Meskes <meskes@debian.org> Feb 5th, 1998 */
/* Placed under the same copyright as PostgresSQL */
#include <stdio.h>
extern void lex_init(void);
int yyparse (void);
int main(int argc, char *argv[])
{
lex_init();
fprintf(stdout, "/* These two include files are added by the preprocessor */\n#include <ecpgtype.h>\n#include <ecpglib.h>\n");
yyparse();
return(0);
}

View File

@ -0,0 +1,31 @@
#!/bin/sh
INFILE=
OUTFILE=
for arg
do
case "$arg" in
iname=*)
INFILE=`expr substr $arg 7 1000`
;;
oname=*)
OUTFILE=`expr substr $arg 7 1000`
;;
*)
echo Wrong argument $arg
exit 1;
;;
esac
done
if [ -n "$INFILE" -a -n "$OUTFILE" ]
then
exec @POSTGRESERVER@/lib/ecpg/preproc < $INFILE > $OUTFILE
else
echo Missing arguments.
echo usage: $0 iname=file oname=outfile
exit 1;
fi
exit 0;

View File

@ -0,0 +1,112 @@
/* Copyright comment! */
%{
#include "type.h"
#include "y.tab.h"
#define dbg(arg) fprintf(stderr, "DEBUG: %s\n", #arg);
%}
%s C SQL
ccomment \/\*([^*]|\*[^/]|\*\*[^/])*\*\/
ws ([ \t\n][ \t\n]*|{ccomment})*
letter [A-Za-z_]
digit [0-9]
length {digit}+
symbol {letter}({letter}|{digit})*
string '[^']*'
exec [eE][xX][eE][cC]
sql [sS][qQ][lL]
varchar [vV][aA][rR][cC][hH][aA][rR]
varchar2 [vV][aA][rR][cC][hH][aA][rR]2
into [iI][nN][tT][oO]
begin [bB][eE][gG][iI][nN]
end [eE][nN][dD]
declare [dD][eE][cC][lL][aA][rR][eE]
section [sS][eE][cC][tT][iI][oO][nN]
include [iI][nN][cC][lL][uU][dD][eE]
connect [cC][oO][nN][nN][eE][cC][tT]
open [oO][pP][eE][nN]
commit [cC][oO][mM][mM][iI][tT]
rollback [rR][oO][lL][lL][bB][aA][cC][kK]
%%
<C>{exec}{ws}{sql} { BEGIN SQL; dbg(SQL_START); return SQL_START; }
<SQL>";" { BEGIN C; dbg(SQL_SEMI); return SQL_SEMI; }
<SQL>{begin} { dbg(SQL_BEGIN); return SQL_BEGIN; }
<SQL>{end} { dbg(SQL_END); return SQL_END; }
<SQL>{declare} { dbg(SQL_DECLARE); return SQL_DECLARE; }
<SQL>{section} { dbg(SQL_SECTION); return SQL_SECTION; }
<SQL>{include} { dbg(SQL_INCLUDE); return SQL_INCLUDE; }
<SQL>{connect} { dbg(SQL_CONNECT); return SQL_CONNECT; }
<SQL>{open} { dbg(SQL_OPEN); return SQL_OPEN; }
<SQL>{commit} { dbg(SQL_COMMIT); return SQL_COMMIT; }
<SQL>{rollback} { dbg(SQL_ROLLBACK); return SQL_ROLLBACK; }
<SQL>{into} { dbg(SQL_INTO); return SQL_INTO; }
{length} { dbg(S_LENGTH); return S_LENGTH; }
{varchar} { dbg(S_VARCHAR); return S_VARCHAR; }
{varchar2} { dbg(S_VARCHAR2); return S_VARCHAR2; }
long { dbg(S_LONG); return S_LONG; }
short { dbg(S_SHORT); return S_SHORT; }
int { dbg(S_INT); return S_INT; }
char { dbg(S_CHAR); return S_CHAR; }
float { dbg(S_FLOAT); return S_FLOAT; }
double { dbg(S_DOUBLE); return S_DOUBLE; }
{string} { dbg(SQL_STRING); return SQL_STRING; }
<SQL>{ws} ;
{symbol} { dbg(S_SYMBOL); return S_SYMBOL; }
<SQL>"!<" { dbg(S_SYMBOL); return S_SYMBOL; }
<SQL>"!>" { dbg(S_SYMBOL); return S_SYMBOL; }
<SQL>"!^" { dbg(S_SYMBOL); return S_SYMBOL; }
<SQL>"!|" { dbg(S_SYMBOL); return S_SYMBOL; }
<SQL>"!~" { dbg(S_SYMBOL); return S_SYMBOL; }
<SQL>"!~*" { dbg(S_SYMBOL); return S_SYMBOL; }
<SQL>"#<" { dbg(S_SYMBOL); return S_SYMBOL; }
<SQL>"#<=" { dbg(S_SYMBOL); return S_SYMBOL; }
<SQL>"#<>" { dbg(S_SYMBOL); return S_SYMBOL; }
<SQL>"#=" { dbg(S_SYMBOL); return S_SYMBOL; }
<SQL>"#>" { dbg(S_SYMBOL); return S_SYMBOL; }
<SQL>"#>=" { dbg(S_SYMBOL); return S_SYMBOL; }
<SQL>"&&" { dbg(S_SYMBOL); return S_SYMBOL; }
<SQL>"&<" { dbg(S_SYMBOL); return S_SYMBOL; }
<SQL>"&>" { dbg(S_SYMBOL); return S_SYMBOL; }
<SQL>"<<" { dbg(S_SYMBOL); return S_SYMBOL; }
<SQL>"<=" { dbg(S_SYMBOL); return S_SYMBOL; }
<SQL>"<===>" { dbg(S_SYMBOL); return S_SYMBOL; }
<SQL>"<>" { dbg(S_SYMBOL); return S_SYMBOL; }
<SQL>"<?>" { dbg(S_SYMBOL); return S_SYMBOL; }
<SQL>"===>" { dbg(S_SYMBOL); return S_SYMBOL; }
<SQL>"===`" { dbg(S_SYMBOL); return S_SYMBOL; }
<SQL>"=|=" { dbg(S_SYMBOL); return S_SYMBOL; }
<SQL>">=" { dbg(S_SYMBOL); return S_SYMBOL; }
<SQL>">>" { dbg(S_SYMBOL); return S_SYMBOL; }
<SQL>"@@" { dbg(S_SYMBOL); return S_SYMBOL; }
<SQL>"|/" { dbg(S_SYMBOL); return S_SYMBOL; }
<SQL>"||/" { dbg(S_SYMBOL); return S_SYMBOL; }
<SQL>"~*" { dbg(S_SYMBOL); return S_SYMBOL; }
<SQL>"~=" { dbg(S_SYMBOL); return S_SYMBOL; }
"[" { dbg([); return '['; }
"]" { dbg(]); return ']'; }
";" { dbg(;); return ';'; }
"," { dbg(komma); return ','; }
<SQL>":" { dbg(:); return ':'; }
{ws} { ECHO; }
. { dbg(.); return S_ANYTHING; }
%%
void
lex_init()
{
BEGIN C;
}
int yywrap()
{
return 1;
}

View File

@ -0,0 +1,337 @@
/* Copyright comment */
%{
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "type.h"
void yyerror(char *);
/*
* Handling of the variables.
*/
/* Since we don't want to keep track of where the functions end we just
* have a list of functions that we search in, most reasently defined
* function. This won't work if we use block scope for variables with the
* same name but different types but in all other cases the c-compiler will
* signal an error (hopefully).
*
* This list is leaked on program exit. This is because I don't think it is
* important enough to spend the extra ten minutes to write the function that
* deletes it. It would be another thing if I would have written in C++.
*/
/* This is a linked list of the variable names and types. */
struct variable
{
char * name;
struct ECPGtype * type;
struct variable * next;
};
static struct variable * allvariables = NULL;
struct variable *
find_variable(char * name)
{
struct variable * p;
for (p = allvariables; p; p = p->next)
{
if (strcmp(p->name, name) == 0)
return p;
}
{
char * errorstring = (char *) malloc(strlen(name) + 100);
sprintf(errorstring, "The variabel :%s is not declared.", name);
yyerror(errorstring);
}
return NULL;
}
void
new_variable(const char * name, struct ECPGtype * type)
{
struct variable * p = (struct variable*) malloc(sizeof(struct variable));
p->name = strdup(name);
p->type = type;
p->next = allvariables;
allvariables = p;
}
/*
* Here is the variables that need to be handled on every request.
* These are of two kinds: input and output.
* I will make two lists for them.
*/
struct arguments {
struct variable * variable;
struct arguments * next;
};
static struct arguments * argsinsert = NULL;
static struct arguments * argsresult = NULL;
void
reset_variables()
{
argsinsert = NULL;
argsresult = NULL;
}
/* Add a variable to a request. */
void
add_variable(struct arguments ** list, struct variable * var)
{
struct arguments * p = (struct arguments *)malloc(sizeof(struct arguments));
p->variable = var;
p->next = *list;
*list = p;
}
/* Dump out a list of all the variable on this list.
This is a recursive function that works from the end of the list and
deletes the list as we go on.
*/
void
dump_variables(struct arguments * list)
{
if (list == NULL)
{
return;
}
/* The list is build up from the beginning so lets first dump the
end of the list:
*/
dump_variables(list->next);
/* Then the current element. */
ECPGdump_a_type(stdout, list->variable->name, list->variable->type);
/* Then release the list element. */
free(list);
}
extern FILE * yyout;
extern char * yytext;
extern int yyleng;
%}
%union {
int tagname;
struct ECPGtemp_type type;
char * symbolname;
int indexsize;
enum ECPGttype type_enum;
}
%token <tagname> SQL_START SQL_SEMI SQL_STRING SQL_INTO
%token <tagname> SQL_BEGIN SQL_END SQL_DECLARE SQL_SECTION SQL_INCLUDE
%token <tagname> SQL_CONNECT SQL_OPEN
%token <tagname> SQL_COMMIT SQL_ROLLBACK
%token <tagname> S_SYMBOL S_LENGTH S_ANYTHING
%token <tagname> S_VARCHAR S_VARCHAR2
%token <tagname> S_EXTERN S_STATIC
%token <tagname> S_UNSIGNED S_SIGNED
%token <tagname> S_LONG S_SHORT S_INT S_CHAR S_FLOAT S_DOUBLE
%token <tagname> '[' ']' ';' ','
%type <type> type type_detailed varchar_type simple_type
%type <symbolname> symbol
%type <tagname> maybe_storage_clause varchar_tag
%type <type_enum> simple_tag
%type <indexsize> index length
%type <tagname> canything sqlanything both_anything
%%
prog : statements;
statements : /* empty */
| statements statement;
statement : sqldeclaration
| sqlinclude
| sqlconnect
| sqlopen
| sqlcommit
| sqlrollback
| sqlstatement
| cthing;
sqldeclaration : sql_startdeclare
variable_declarations
sql_enddeclare;
sql_startdeclare : SQL_START SQL_BEGIN SQL_DECLARE SQL_SECTION SQL_SEMI {
printf("/* exec sql begin declare section */\n");
};
sql_enddeclare : SQL_START SQL_END SQL_DECLARE SQL_SECTION SQL_SEMI {
printf("/* exec sql end declare section */\n");
};
variable_declarations : /* empty */
| variable_declarations variable_declaration ;
/* Here is where we can enter support for typedef. */
variable_declaration : type ';' {
new_variable($<type>1.name, $<type>1.typ);
free($<type>1.name);
fprintf(yyout, ";");
}
symbol : S_SYMBOL {
char * name = (char *)malloc(yyleng + 1);
strncpy(name, yytext, yyleng);
name[yyleng] = '\0';
$<symbolname>$ = name;
}
type : maybe_storage_clause type_detailed { $<type>$ = $<type>2; };
type_detailed : varchar_type { $<type>$ = $<type>1; }
| simple_type { $<type>$ = $<type>1; };
varchar_type : varchar_tag symbol index {
fprintf(yyout, "struct varchar_%s { int len; char arr[%d]; } %s", $<symbolname>2, $<indexsize>3, $<symbolname>2);
$<type>$.name = $<symbolname>2;
$<type>$.typ = ECPGmake_varchar_type(ECPGt_varchar, $<indexsize>3);
}
varchar_tag : S_VARCHAR { $<tagname>$ = $<tagname>1; }
| S_VARCHAR2 { $<tagname>$ = $<tagname>1; };
simple_type : simple_tag symbol {
fprintf(yyout, "%s %s", ECPGtype_name($<type_enum>1), $<symbolname>2);
$<type>$.name = $<symbolname>2;
$<type>$.typ = ECPGmake_simple_type($<type_enum>1);
}
simple_tag : S_CHAR { $<type_enum>$ = ECPGt_char; }
| S_UNSIGNED S_CHAR { $<type_enum>$ = ECPGt_unsigned_char; }
| S_SHORT { $<type_enum>$ = ECPGt_short; }
| S_UNSIGNED S_SHORT { $<type_enum>$ = ECPGt_unsigned_short; }
| S_INT { $<type_enum>$ = ECPGt_int; }
| S_UNSIGNED S_INT { $<type_enum>$ = ECPGt_unsigned_int; }
| S_LONG { $<type_enum>$ = ECPGt_long; }
| S_UNSIGNED S_LONG { $<type_enum>$ = ECPGt_unsigned_long; }
| S_FLOAT { $<type_enum>$ = ECPGt_float; }
| S_DOUBLE { $<type_enum>$ = ECPGt_double; };
maybe_storage_clause : S_EXTERN { fwrite(yytext, yyleng, 1, yyout); }
| S_STATIC { fwrite(yytext, yyleng, 1, yyout); }
| /* empty */ { };
index : '[' length ']' {
$<indexsize>$ = $<indexsize>2;
};
length : S_LENGTH { $<indexsize>$ = atoi(yytext); }
sqlinclude : SQL_START SQL_INCLUDE { fprintf(yyout, "#include \""); }
filename SQL_SEMI { fprintf(yyout, ".h\""); };
filename : cthing
| filename cthing;
sqlconnect : SQL_START SQL_CONNECT { fprintf(yyout, "ECPGconnect(\""); }
SQL_STRING { fwrite(yytext + 1, yyleng - 2, 1, yyout); }
SQL_SEMI { fprintf(yyout, "\");"); };
/* Open is an open cursor. Removed. */
sqlopen : SQL_START SQL_OPEN sqlgarbage SQL_SEMI { };
sqlgarbage : /* Empty */
| sqlgarbage sqlanything;
sqlcommit : SQL_START SQL_COMMIT SQL_SEMI {
fprintf(yyout, "ECPGcommit(__LINE__);");
};
sqlrollback : SQL_START SQL_ROLLBACK SQL_SEMI {
fprintf(yyout, "ECPGrollback(__LINE__);");
};
sqlstatement : SQL_START { /* Reset stack */
reset_variables();
fprintf(yyout, "ECPGdo(__LINE__, \"");
}
sqlstatement_words
SQL_SEMI {
/* Dump */
fprintf(yyout, "\", ");
dump_variables(argsinsert);
fprintf(yyout, "ECPGt_EOIT, ");
dump_variables(argsresult);
fprintf(yyout, "ECPGt_EORT );");
};
sqlstatement_words : sqlstatement_word
| sqlstatement_words sqlstatement_word;
sqlstatement_word : ':' symbol
{
add_variable(&argsinsert, find_variable($2));
fprintf(yyout, " ;; ");
}
| SQL_INTO into_list { }
| sqlanything
{
fwrite(yytext, yyleng, 1, yyout);
fwrite(" ", 1, 1, yyout);
}
| SQL_INTO sqlanything
{
fprintf(yyout, " into ");
fwrite(yytext, yyleng, 1, yyout);
fwrite(" ", 1, 1, yyout);
};
into_list : ':' symbol {
add_variable(&argsresult, find_variable($2));
}
| into_list ',' ':' symbol{
add_variable(&argsresult, find_variable($4));
};
cthing : canything {
fwrite(yytext, yyleng, 1, yyout);
}
canything : both_anything
| SQL_INTO
| ';';
sqlanything : both_anything;
both_anything : S_LENGTH | S_VARCHAR | S_VARCHAR2
| S_LONG | S_SHORT | S_INT | S_CHAR | S_FLOAT | S_DOUBLE
| SQL_OPEN | SQL_CONNECT
| SQL_STRING
| SQL_BEGIN | SQL_END
| SQL_DECLARE | SQL_SECTION
| SQL_INCLUDE
| S_SYMBOL
| '[' | ']' | ','
| S_ANYTHING;
%%
void yyerror(char * error)
{
fprintf(stderr, "%s\n", error);
exit(1);
}

View File

@ -0,0 +1,283 @@
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "type.h"
/* Constructors
Yes, I mostly write c++-code
*/
/* The NAME argument is copied. The type argument is preserved as a pointer. */
struct ECPGrecord_member *
ECPGmake_record_member(char * name, struct ECPGtype * type)
{
struct ECPGrecord_member * ne =
(struct ECPGrecord_member *)malloc(sizeof(struct ECPGrecord_member));
ne->name = strdup(name);
ne->typ = type;
return ne;
}
struct ECPGtype *
ECPGmake_simple_type(enum ECPGttype typ)
{
struct ECPGtype * ne = (struct ECPGtype *)malloc(sizeof(struct ECPGtype));
ne->typ = typ;
ne->size = 0;
ne->u.element = 0;
return ne;
}
struct ECPGtype *
ECPGmake_varchar_type(enum ECPGttype typ, unsigned short siz)
{
struct ECPGtype * ne = ECPGmake_simple_type(typ);
ne->size = siz;
return ne;
}
struct ECPGtype *
ECPGmake_array_type(struct ECPGtype * typ, unsigned short siz)
{
struct ECPGtype * ne = ECPGmake_simple_type(ECPGt_array);
ne->size = siz;
ne->u.element = typ;
return ne;
}
struct ECPGtype *
ECPGmake_record_type(struct ECPGrecord_member * rm[])
{
struct ECPGtype * ne = ECPGmake_simple_type(ECPGt_record);
ne->u.members = rm;
return ne;
}
/* Dump a type.
The type is dumped as:
type-tag <comma> - enum ECPGttype
reference-to-variable <comma> - void *
size <comma> - short size of this field (if varchar)
arrsize <comma> - short number of elements in the arr
offset <comma> - short offset to the next element
Where:
type-tag is one of the simple types or varchar.
reference-to-variable can be a reference to a struct element.
arrsize is the size of the array in case of array fetches. Otherwise 0.
size is the maxsize in case it is a varchar. Otherwise it is the size of
the variable (required to do array fetches of records).
*/
void ECPGdump_a_simple(FILE * o, const char * name, enum ECPGttype typ,
short varcharsize,
unsigned short arrsiz, const char * siz);
void ECPGdump_a_record(FILE * o, const char * name, unsigned short arrsiz,
struct ECPGtype * typ, const char * offset);
void
ECPGdump_a_type(FILE * o, const char * name, struct ECPGtype * typ)
{
if (IS_SIMPLE_TYPE(typ->typ))
{
ECPGdump_a_simple(o, name, typ->typ, typ->size, 0, 0);
}
else if (typ->typ == ECPGt_array)
{
if (IS_SIMPLE_TYPE(typ->u.element->typ))
ECPGdump_a_simple(o, name, typ->u.element->typ,
typ->u.element->size, typ->size, 0);
else if (typ->u.element->typ == ECPGt_array)
{
abort(); /* Array of array, */
}
else if (typ->u.element->typ == ECPGt_record)
{
/* Array of records. */
ECPGdump_a_record(o, name, typ->size, typ->u.element, 0);
}
else
{
abort();
}
}
else if (typ->typ == ECPGt_record)
{
ECPGdump_a_record(o, name, 0, typ, 0);
}
else
{
abort();
}
}
/* If siz is NULL, then the offset is 0, if not use siz as a
string, it represents the offset needed if we are in an array of records. */
void
ECPGdump_a_simple(FILE * o, const char * name, enum ECPGttype typ,
short varcharsize,
unsigned short arrsiz,
const char * siz)
{
switch (typ)
{
case ECPGt_char:
fprintf(o, "ECPGt_char,&%s,0,%d,%s, ", name, arrsiz,
siz == NULL ? "sizeof(char)" : siz);
break;
case ECPGt_unsigned_char:
fprintf(o, "ECPGt_unsigned_char,&%s,0,%d,%s, ", name, arrsiz,
siz == NULL ? "sizeof(unsigned char)" : siz);
break;
case ECPGt_short:
fprintf(o, "ECPGt_short,&%s,0,%d,%s, ", name, arrsiz,
siz == NULL ? "sizeof(short)" : siz);
break;
case ECPGt_unsigned_short:
fprintf(o,
"ECPGt_unsigned_short,&%s,0,%d,%s, ", name, arrsiz,
siz == NULL ? "sizeof(unsigned short)" : siz);
break;
case ECPGt_int:
fprintf(o, "ECPGt_int,&%s,0,%d,%s, ", name, arrsiz,
siz == NULL ? "sizeof(int)" : siz);
break;
case ECPGt_unsigned_int:
fprintf(o, "ECPGt_unsigned_int,&%s,0,%d,%s, ", name, arrsiz,
siz == NULL ? "sizeof(unsigned int)" : siz);
break;
case ECPGt_long:
fprintf(o, "ECPGt_long,&%s,0,%d,%s, ", name, arrsiz,
siz == NULL ? "sizeof(long)" : siz);
break;
case ECPGt_unsigned_long:
fprintf(o, "ECPGt_unsigned_int,&%s,0,%d,%s, ", name, arrsiz,
siz == NULL ? "sizeof(unsigned int)" : siz);
break;
case ECPGt_float:
fprintf(o, "ECPGt_float,&%s,0,%d,%s, ", name, arrsiz,
siz == NULL ? "sizeof(float)" : siz);
break;
case ECPGt_double:
fprintf(o, "ECPGt_double,&%s,0,%d,%s, ", name, arrsiz,
siz == NULL ? "sizeof(double)" : siz);
break;
case ECPGt_varchar:
case ECPGt_varchar2:
if (siz == NULL)
fprintf(o, "ECPGt_varchar,&%s,%d,%d,sizeof(struct varchar_%s), ",
name,
varcharsize,
arrsiz, name);
else
fprintf(o, "ECPGt_varchar,&%s,%d,%d,%s, ",
name,
varcharsize,
arrsiz, siz);
break;
default:
abort();
}
}
/* Penetrate a record and dump the contents. */
void
ECPGdump_a_record(FILE * o,
const char * name, unsigned short arrsiz,
struct ECPGtype * typ, const char * offsetarg)
{
/* If offset is NULL, then this is the first recursive level. If not then
we are in a record in a record and the offset is used as offset.
*/
struct ECPGrecord_member ** p;
char obuf[BUFSIZ];
char buf[BUFSIZ];
const char * offset;
if (offsetarg == NULL)
{
sprintf(obuf, "sizeof(%s)", name);
offset = obuf;
}
else
{
offset = offsetarg;
}
for (p = typ->u.members; *p; p++)
{
if (IS_SIMPLE_TYPE((*p)->typ->typ))
{
sprintf(buf, "%s.%s", name, (*p)->name);
ECPGdump_a_simple(o, buf, (*p)->typ->typ, (*p)->typ->size,
arrsiz, offset);
}
else if ((*p)->typ->typ == ECPGt_array)
{
int i;
for (i = 0; i < (*p)->typ->size; i++)
{
if (IS_SIMPLE_TYPE((*p)->typ->u.element->typ))
{
sprintf(buf, "%s.%s[%d]", name, (*p)->name, i);
ECPGdump_a_simple(o, buf, (*p)->typ->typ, (*p)->typ->size,
arrsiz, offset);
}
else if((*p)->typ->u.element->typ == ECPGt_array)
{
/* Array within an array. NOT implemented yet. */
abort();
}
else if ((*p)->typ->u.element->typ == ECPGt_record)
{
/* Record within array within record. NOT implemented yet. */
abort();
}
else
{
/* Unknown type */
abort();
}
}
}
else if ((*p)->typ->typ == ECPGt_record)
{
/* Record within a record */
sprintf(buf, "%s.%s", name, (*p)->name);
ECPGdump_a_record(o, buf, arrsiz, (*p)->typ, offset);
}
else
{
/* Unknown type */
abort();
}
}
}
/* Freeing is not really that important. Since we throw away the process
anyway. Lets implement that last! */
void
ECPGfree_record_member(struct ECPGrecord_member * rm)
{
}
void
ECPGfree_type(struct ECPGtype * typ)
{
}

View File

@ -0,0 +1,51 @@
#include <ecpgtype.h>
struct ECPGtype;
struct ECPGrecord_member {
char * name;
struct ECPGtype * typ;
};
struct ECPGtype {
enum ECPGttype typ;
unsigned short size; /* For array it is the number of elements.
* For varchar it is the maxsize of the area.
*/
union {
struct ECPGtype * element; /* For an array this is the type of the
* element */
struct ECPGrecord_member ** members;
/* A pointer to an array of members. */
} u;
};
/* Everything is malloced. */
struct ECPGrecord_member * ECPGmake_record_member(char *, struct ECPGtype *);
struct ECPGtype * ECPGmake_simple_type(enum ECPGttype);
struct ECPGtype * ECPGmake_varchar_type(enum ECPGttype, unsigned short);
struct ECPGtype * ECPGmake_array_type(struct ECPGtype *, unsigned short);
struct ECPGtype * ECPGmake_record_type(struct ECPGrecord_member *[]);
/* Frees a type. */
void ECPGfree_record_member(struct ECPGrecord_member *);
void ECPGfree_type(struct ECPGtype *);
/* Dump a type.
The type is dumped as:
type-tag <comma> reference-to-variable <comma> arrsize <comma> size <comma>
Where:
type-tag is one of the simple types or varchar.
reference-to-variable can be a reference to a struct element.
arrsize is the size of the array in case of array fetches. Otherwise 0.
size is the maxsize in case it is a varchar. Otherwise it is the size of
the variable (required to do array fetches of records).
*/
void ECPGdump_a_type(FILE *, const char * name, struct ECPGtype *);
/* A simple struct to keep a variable and its type. */
struct ECPGtemp_type {
struct ECPGtype * typ;
const char * name;
};
extern const char * ECPGtype_name(enum ECPGttype typ);

View File

@ -0,0 +1,4 @@
test2: test2.c
gcc -g -I ../include -I ../../../libpq -o test2 test2.c ../lib/libecpg.a ../../../libpq/libpq.a -lcrypt
test2.c: test2.qc
../preproc/ecpg < test2.qc > test2.c

View File

@ -0,0 +1,64 @@
/* These two include files are added by the preprocessor */
#include <ecpgtype.h>
#include <ecpglib.h>
/* exec sql begin declare section */
/* VARSIZE */struct varchar_uid { int len; char arr[200]; } uid;
struct varchar_name { int len; char arr[200]; } name;
short value;
/* exec sql end declare section */
#include "sqlca.h"
#define DBCP(x,y) strcpy(x.arr,y);x.len = strlen(x.arr)
#define LENFIX(x) x.len=strlen(x.arr)
#define STRFIX(x) x.arr[x.len]='\0'
#define SQLCODE sqlca.sqlcode
void
db_error (char *msg)
{
sqlca.sqlerrm.sqlerrmc[sqlca.sqlerrm.sqlerrml] = '\0';
printf ("%s: db error %s\n", msg, sqlca.sqlerrm.sqlerrmc);
exit (1);
}
int
main ()
{
strcpy (uid.arr, "test/test");
LENFIX (uid);
ECPGconnect("kom");
if (SQLCODE)
db_error ("connect");
strcpy (name.arr, "opt1");
LENFIX (name);
ECPGdo(__LINE__, "declare cur cursor for select name , value from pace_test ", ECPGt_EOIT, ECPGt_EORT );
if (SQLCODE) db_error ("declare");
if (SQLCODE)
db_error ("open");
while (1) {
ECPGdo(__LINE__, "fetch in cur ", ECPGt_EOIT, ECPGt_varchar,&name,200,0,sizeof(struct varchar_name), ECPGt_short,&value,0,0,sizeof(short), ECPGt_EORT );
if (SQLCODE)
break;
STRFIX (name);
printf ("%s\t%d\n", name.arr, value);
}
if (SQLCODE < 0)
db_error ("fetch");
ECPGdo(__LINE__, "close cur ", ECPGt_EOIT, ECPGt_EORT );
if (SQLCODE) db_error ("close");
ECPGcommit(__LINE__);
if (SQLCODE) db_error ("commit");
return (0);
}

View File

@ -0,0 +1,60 @@
exec sql begin declare section;
VARCHAR uid[200 /* VARSIZE */];
varchar name[200];
short value;
exec sql end declare section;
exec sql include sqlca;
#define DBCP(x,y) strcpy(x.arr,y);x.len = strlen(x.arr)
#define LENFIX(x) x.len=strlen(x.arr)
#define STRFIX(x) x.arr[x.len]='\0'
#define SQLCODE sqlca.sqlcode
void
db_error (char *msg)
{
sqlca.sqlerrm.sqlerrmc[sqlca.sqlerrm.sqlerrml] = '\0';
printf ("%s: db error %s\n", msg, sqlca.sqlerrm.sqlerrmc);
exit (1);
}
int
main ()
{
strcpy (uid.arr, "test/test");
LENFIX (uid);
exec sql connect 'kom';
if (SQLCODE)
db_error ("connect");
strcpy (name.arr, "opt1");
LENFIX (name);
exec sql declare cur cursor for
select name, value from pace_test;
if (SQLCODE) db_error ("declare");
exec sql open cur;
if (SQLCODE)
db_error ("open");
while (1) {
exec sql fetch in cur into :name, :value;
if (SQLCODE)
break;
STRFIX (name);
printf ("%s\t%d\n", name.arr, value);
}
if (SQLCODE < 0)
db_error ("fetch");
exec sql close cur;
if (SQLCODE) db_error ("close");
exec sql commit;
if (SQLCODE) db_error ("commit");
return (0);
}

Binary file not shown.

View File

@ -0,0 +1,52 @@
/* These two include files are added by the preprocessor */
#include <ecpgtype.h>
#include <ecpglib.h>
#include "sqlca.h"
#define SQLCODE sqlca.sqlcode
void
db_error (char *msg)
{
sqlca.sqlerrm.sqlerrmc[sqlca.sqlerrm.sqlerrml] = '\0';
printf ("%s: db error %s\n", msg, sqlca.sqlerrm.sqlerrmc);
exit (1);
}
int
main ()
{
/* exec sql begin declare section */
struct varchar_text { int len; char arr[8]; } text;
/* exec sql end declare section */
ECPGconnect("mm");
if (SQLCODE)
db_error ("connect");
ECPGdo(__LINE__, "declare cur cursor for select text from test ", ECPGt_EOIT, ECPGt_EORT );
if (SQLCODE) db_error ("declare");
if (SQLCODE)
db_error ("open");
while (1) {
ECPGdo(__LINE__, "fetch in cur ", ECPGt_EOIT, ECPGt_varchar,&text,8,0,sizeof(struct varchar_text), ECPGt_EORT );
if (SQLCODE)
break;
printf ("%8.8s\n", text.arr);
}
if (SQLCODE < 0)
db_error ("fetch");
ECPGdo(__LINE__, "close cur ", ECPGt_EOIT, ECPGt_EORT );
if (SQLCODE) db_error ("close");
ECPGcommit(__LINE__);
if (SQLCODE) db_error ("commit");
return (0);
}

View File

@ -0,0 +1,48 @@
exec sql include sqlca;
#define SQLCODE sqlca.sqlcode
void
db_error (char *msg)
{
sqlca.sqlerrm.sqlerrmc[sqlca.sqlerrm.sqlerrml] = '\0';
printf ("%s: db error %s\n", msg, sqlca.sqlerrm.sqlerrmc);
exit (1);
}
int
main ()
{
exec sql begin declare section;
varchar text[8];
exec sql end declare section;
exec sql connect 'mm';
if (SQLCODE)
db_error ("connect");
exec sql declare cur cursor for
select text from test;
if (SQLCODE) db_error ("declare");
exec sql open cur;
if (SQLCODE)
db_error ("open");
while (1) {
exec sql fetch in cur into :text;
if (SQLCODE)
break;
printf ("%8.8s\n", text.arr);
}
if (SQLCODE < 0)
db_error ("fetch");
exec sql close cur;
if (SQLCODE) db_error ("close");
exec sql commit;
if (SQLCODE) db_error ("commit");
return (0);
}