📄 ecpg.sgml
字号:
So, read this before looking at the internals of the <Application>ecpg</Application>. Ifyou are not interested in how it really works, skip this section.</Para><Sect2><Title>ToDo List</Title><Para>This version the preprocessor has some flaws:<VariableList><VarListEntry><Term>Library functions</Term><ListItem><Para>to_date et al. do not exists. But then <ProductName>Postgres</ProductName>has some good conversion routines itself. So you probably won't miss these.</Para></ListItem></VarListEntry><VarListEntry><Term>Structures ans unions</Term><ListItem><Para>Structures and unions have to be defined in the declare section.</Para></ListItem></VarListEntry><VarListEntry><Term>Missing statements</Term><ListItem><Para>The following statements are not implemented thus far:<VariableList><VarListEntry><Term> exec sql allocate</Term><ListItem><Para></Para></listitem></VarListEntry><VarListEntry><Term> exec sql deallocate</Term><ListItem><Para></Para></listitem></VarListEntry><VarListEntry><Term> SQLSTATE</Term><ListItem><Para></Para></listitem></VarListEntry></VariableList></Para></ListItem></VarListEntry><VarListEntry><Term>message 'no data found'</Term><ListItem><Para>The error message for "no data" in an exec sql insert select from statementhas to be 100.</Para></ListItem></VarListEntry><VarListEntry><Term>sqlwarn[6]</Term><ListItem><Para>sqlwarn[6] should be 'W' if the PRECISION or SCALE value specified in a SETDESCRIPTOR statement will be ignored.</Para></ListItem></VarListEntry></VariableList></Para></sect2><Sect2><Title>The Preprocessor</Title><Para>The first four lines written to the output are constant additions by ecpg.These are two comments and two include lines necessary for the interface to thelibrary.</Para><Para>Then the preprocessor works in one pass only, reading the input file andwriting to the output as it goes along. Normally it just echoeseverything to the output without looking at it further.</Para><Para>When it comes to an <Command>EXEC SQL</Command> statements it intervenes andchanges them depending on what it is. The <Command>EXEC SQL</Command> statement can be one of these:<VariableList><VarListEntry><Term>Declare sections</Term><ListItem><Para>Declare sections begins with<ProgramListing>exec sql begin declare section;</ProgramListing>and ends with<ProgramListing>exec sql end declare section;</ProgramListing>In the section only variable declarations are allowed. Every variabledeclare within this section is also entered in a list of variablesindexed on their name together with the corresponding type.</Para><Para>In particular the definition of a structure or union also has to be listedinside a declare section. Otherwise <Application>ecpg</Application> cannothandle these types since it simply does not know the definition.</Para><Para>The declaration is echoed to the file to make the variable a normalC-variable also.</Para><Para>The special types VARCHAR and VARCHAR2 are converted into a named structfor every variable. A declaration like:<ProgramListing>VARCHAR var[180];</ProgramListing>is converted into<ProgramListing>struct varchar_var { int len; char arr[180]; } var;</ProgramListing></Para></ListItem></VarListEntry><VarListEntry><Term>Include statements</Term><ListItem><Para>An include statement looks like:<ProgramListing>exec sql include filename;</ProgramListing>Not that this is NOT the same as<ProgramListing>#include <filename.h></ProgramListing></Para><Para>Instead the file specified is parsed by <Application>ecpg</Application>itself. So the contents of the specified file is included in the resulting Ccode. This way you are able to specify EXEC SQL commands in an include file.</Para></ListItem></VarListEntry><VarListEntry><Term>Connect statement</Term><ListItem><Para>A connect statement looks like:<ProgramListing>exec sql connect to <Replaceable>connection target</Replaceable>;</ProgramListing>It creates a connection to the specified database.</Para><Para>The <Replaceable>connection target</Replaceable> can be specified in thefollowing ways:<VariableList><VarListEntry><Term>dbname[@server][:port][as <Replaceable>connection name</Replaceable>][user <Replaceable>user name</Replaceable>]</Term><listitem><para></para></listitem></VarListEntry><VarListEntry><Term>tcp:postgresql://server[:port][/dbname][as <Replaceable>connection name</Replaceable>][user <Replaceable>user name</Replaceable>]</Term><listitem><para></para></listitem></VarListEntry><VarListEntry><Term>unix:postgresql://server[:port][/dbname][as <Replaceable>connection name</Replaceable>][user <Replaceable>user name</Replaceable>]</Term><listitem><para></para></listitem></VarListEntry><VarListEntry><Term><Replaceable>character variable</Replaceable>[as <Replaceable>connection name</Replaceable>][user <Replaceable>user name</Replaceable>]</Term><listitem><para></para></listitem></VarListEntry><VarListEntry><Term><Replaceable>character string</Replaceable>[as <Replaceable>connection name</Replaceable>][<Replaceable>user</Replaceable>]</Term><listitem><para></para></listitem></VarListEntry><VarListEntry><Term>default</Term><listitem><para></para></listitem></VarListEntry><VarListEntry><Term>user</Term><listitem><para></para></listitem></VarListEntry></VariableList></Para><Para>There are also different ways to specify the user name:<VariableList><VarListEntry><Term><Replaceable>userid</Replaceable></Term><listitem><para></para></listitem></VarListEntry><VarListEntry><Term><Replaceable>userid</Replaceable>/<Replaceable>password</Replaceable></Term><listitem><para></para></listitem></VarListEntry><VarListEntry><Term><Replaceable>userid</Replaceable> identified by <Replaceable>password</Replaceable></Term><listitem><para></para></listitem></VarListEntry><VarListEntry><Term><Replaceable>userid</Replaceable> using <Replaceable>password</Replaceable></Term><listitem><para></para></listitem></VarListEntry></VariableList></Para><Para> Finally the userid and the password. Each may be a constant text, acharacter variable or a chararcter string.</Para></ListItem></VarListEntry><VarListEntry><Term>Disconnect statements</Term><ListItem><Para>A disconnect statement looks loke:<ProgramListing>exec sql disconnect [<Replaceable>connection target</Replaceable>];</ProgramListing>It closes the connection to the specified database.</Para><Para>The <Replaceable>connection target</Replaceable> can be specified in thefollowing ways:<VariableList><VarListEntry><Term><Replaceable>connection name</Replaceable></Term><listitem><para></para></listitem></VarListEntry><VarListEntry><Term>default</Term><listitem><para></para></listitem></VarListEntry><VarListEntry><Term>current</Term><listitem><para></para></listitem></VarListEntry><VarListEntry><Term>all</Term><listitem><para></para></listitem></VarListEntry></VariableList></Para></ListItem></VarListEntry><!--WARNING: FROM HERE ON THE TEXT IS OUTDATED!--><VarListEntry><Term>Open cursor statement</Term><ListItem><Para>An open cursor statement looks like:<ProgramListing>exec sql open <Replaceable>cursor</Replaceable>;</ProgramListing>and is ignore and not copied from the output.</Para></ListItem></VarListEntry><VarListEntry><Term>Commit statement</Term><ListItem><Para>A commit statement looks like<ProgramListing>exec sql commit;</ProgramListing>and is translated on the output to<ProgramListing>ECPGcommit(__LINE__);</ProgramListing></Para></ListItem></VarListEntry><VarListEntry><Term>Rollback statement</Term><ListItem><Para>A rollback statement looks like<ProgramListing>exec sql rollback;</ProgramListing>and is translated on the output to<ProgramListing>ECPGrollback(__LINE__);</ProgramListing></Para></ListItem></VarListEntry><!--STARTING HERE IT IS OKAY AGAIN!--><VarListEntry><Term>Other statements</Term><ListItem><Para>Other <Acronym>SQL</Acronym> statements are other statements that start with <Command>exec sql</Command> and ends with <Command>;</Command>. Everything inbetween is treatedas an <Acronym>SQL</Acronym> statement and parsed for variable substitution.</Para><Para>Variable substitution occur when a symbol starts with a colon(<Command>:</Command>). Then a variable with that name is looked for amongthe variables that were previously declared within a declare section anddepending on the variable being for input or output the pointers to thevariables are written to the output to allow for access by the function.</Para><Para>For every variable that is part of the <Acronym>SQL</Acronym> request the function gets another ten arguments:<SimpleList><Member>The type as a special symbol.</Member><Member>A pointer to the value or a pointer to the pointer.</Member><Member>The size of the variable if it is a char or varchar.</Member><Member>Number of elements in the array (for array fetches).</Member><Member>The offset to the next element in the array (for array fetches)</Member><Member>The type of the indicator variable as a special symbol.</Member><Member>A pointer to the value of the indicator variable or a pointer to the pointer of the indicator variable.</Member><Member>0.</Member><Member>Number of elements in the indicator array (for array fetches).</Member><Member>The offset to the next element in the indicator array (for array fetches)</Member></SimpleList></Para></ListItem></VarListEntry></VariableList></Para></Sect2><Sect2><Title>A Complete Example</Title><Para>Here is a complete example describing the output of the preprocessor of afile foo.pgc:<ProgramListing>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;</ProgramListing>is translated into:<ProgramListing>/* Processed by ecpg (2.6.0) *//* These two include files are added by the preprocessor */#include <ecpgtype.h>;#include <ecpglib.h>;/* exec sql begin declare section */#line 1 "foo.pgc" int index; int result;/* exec sql end declare section */...ECPGdo(__LINE__, NULL, "select res from mytable where index = ? ", ECPGt_int,&(index),1L,1L,sizeof(int), ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, ECPGt_EOIT, ECPGt_int,&(result),1L,1L,sizeof(int), ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, ECPGt_EORT);#line 147 "foo.pgc"</ProgramListing>(the indentation in this manual is added for readability and notsomething that the preprocessor can do.)</Para></sect2><Sect2><Title>The Library</Title><Para>The most important function in the library is the <Function>ECPGdo</Function>function. It takes a variable amount of arguments. Hopefully we will not runinto machines with limits on the amount of variables that can beaccepted by a vararg function. This could easily add up to 50 or soarguments.</Para><Para>The arguments are:<VariableList><VarListEntry><Term>A line number</Term><ListItem><Para>This is a line number for the original line used in error messages only.</Para></ListItem></VarListEntry><VarListEntry><Term>A string</Term><ListItem><Para>This is the <Acronym>SQL</Acronym> request that is to be issued. This request is modifiedby the input variables, i.e. the variables that where not known atcompile time but are to be entered in the request. Where the variablesshould go the string contains <Quote>;</Quote>.</Para></ListItem></VarListEntry><VarListEntry><Term>Input variables</Term><ListItem><Para>As described in the section about the preprocessor every input variablegets ten arguments.</Para></ListItem></VarListEntry><VarListEntry><Term>ECPGt_EOIT</Term><ListItem><Para>An enum telling that there are no more input variables.</Para></ListItem></VarListEntry><VarListEntry><Term>Output variables</Term><ListItem><Para>As described in the section about the preprocessor every input variablegets ten arguments. These variables are filled by the function.</Para></ListItem></VarListEntry><VarListEntry><Term>ECPGt_EORT</Term><ListItem><Para>An enum telling that there are no more variables.</Para></ListItem></VarListEntry></VariableList></Para><Para>All the <Acronym>SQL</Acronym> statements are performed in one transactionunless you issue a commit transaction. To get this auto-transaction goingthe first statement or the first after statement after a commit or rollbackalways begins a transaction. To disable this feature per default use the'-t' option on the commandline</Para><Para>To be completed: entries describing the other entries.</Para></sect2></sect1></Chapter>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -