⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ecpg.sgml

📁 关系型数据库 Postgresql 6.5.2
💻 SGML
📖 第 1 页 / 共 2 页
字号:
<Chapter Id="ecpg"><DocInfo><AuthorGroup><Author><FirstName>Linux</FirstName><Surname>Tolke</Surname></Author><Author><FirstName>Michael</FirstName><Surname>Meskes</Surname></Author></AuthorGroup><Copyright><Year>1996-1997</Year><Holder>Linus Tolke</Holder></Copyright><Copyright><Year>1998</Year><Holder>Michael Meskes</Holder></Copyright><Date>Transcribed 1998-02-12</Date></DocInfo><Title><Application>ecpg</Application> - Embedded <Acronym>SQL</Acronym> in <Acronym>C</Acronym></Title><Para>This describes an embedded <Acronym>SQL</Acronym> in <Acronym>C</Acronym> package for <ProductName>Postgres</ProductName>.It is written by <ULink url="mailto:linus@epact.se">Linus Tolke</ULink>and <ULink url="mailto:meskes@postgresql.org">Michael Meskes</ULink>.<Note><Para>Permission is granted to copy and use in the same way as you are allowedto copy and use the rest of the <ProductName>PostgreSQL</ProductName>.</Para></Note></para><Sect1><Title>Why Embedded <Acronym>SQL</Acronym>?</Title><Para>Embedded <Acronym>SQL</Acronym> has some small advantages over other ways to handle <Acronym>SQL</Acronym>queries. It takes care of all the tedious moving of information to andfrom variables in your <Acronym>C</Acronym> program. Many <Acronym>RDBMS</Acronym> packagessupport this embedded language.</Para><Para> There is an ANSI-standard describing how the embedded language shouldwork. <Application>ecpg</Application> was designed to meet this standard as much as possible. So it ispossible to port programs with embedded <Acronym>SQL</Acronym> written forother <Acronym>RDBMS</Acronym> packages to<ProductName>Postgres</ProductName> and thus promoting the spirit of freesoftware.</Para></sect1><Sect1><Title>The Concept</Title><Para>You write your program in <Acronym>C</Acronym> with some special <Acronym>SQL</Acronym> things.For declaring variables that can be used in <Acronym>SQL</Acronym> statements you need toput them in a special declare section.You use a special syntax for the <Acronym>SQL</Acronym> queries.</Para><Para>Before compiling you run the file through the embedded <Acronym>SQL</Acronym> <Acronym>C</Acronym>preprocessor and it converts the <Acronym>SQL</Acronym> statements you used to functioncalls with the variables used as arguments. Both variables that are usedas input to the <Acronym>SQL</Acronym> statements and variables that will contain theresult are passed.</Para><Para>Then you compile and at link time you link with a special library thatcontains the functions used. These functions (actually it is mostly onesingle function) fetches the information from the arguments, performsthe <Acronym>SQL</Acronym> query using the ordinary interface (<FileName>libpq</FileName>) and puts backthe result in the arguments dedicated for output.</Para><Para>Then you run your program and when the control arrives to the <Acronym>SQL</Acronym>statement the <Acronym>SQL</Acronym> statement is performed against the database and youcan continue with the result.</Para></sect1><Sect1><Title>How To Use <Application>egpc</Application></Title><Para>This section describes how to use the <Application>egpc</Application> tool.</Para><Sect2><Title>Preprocessor</title><Para>The preprocessor is called <Application>ecpg</Application>. After installation it resides inthe <ProductName>Postgres</ProductName> <FileName>bin/</FileName> directory. </Para></sect2><Sect2><Title>Library</title><Para>The <Application>ecpg</Application> library is called <FileName>libecpg.a</FileName> or<FileName>libecpg.so</FileName>. Additionally, the libraryuses the <FileName>libpq</FileName> library for communication to the <ProductName>Postgres</ProductName> server so you willhave to link your program with <Parameter>-lecpg -lpq</Parameter>.</Para><Para>The library has some methods that are "hidden" but that could prove veryuseful sometime.<itemizedlist><listitem><para><function>ECPGdebug(int <replaceable class="parameter">on</replaceable>, FILE *<replaceable class="parameter">stream</replaceable>)</function>turns on debug logging if called with the first argument non-zero.Debug logging is done on <replaceable class="parameter">stream</replaceable>. Most <Acronym>SQL</Acronym> statement logs its arguments and result.</Para><Para>The most important one (<Function>ECPGdo</Function>) that is called on almost all <Acronym>SQL</Acronym>statements logs both its expanded string, i.e. the stringwith all the input variables inserted, and the result from the<ProductName>Postgres</ProductName> server. This can be very useful when searching for errorsin your <Acronym>SQL</Acronym> statements.</Para></ListItem><listitem><para><function>ECPGstatus()</function>This method returns TRUE if we are connected to a database and FALSE if not.</Para></ListItem></itemizedlist></Para></sect2><Sect2><Title>Error handling</title><Para>To be able to detect errors from the <ProductName>Postgres</ProductName> server you include a line like<ProgramListing>exec sql include sqlca;</ProgramListing>in the include section of your file. This will define a struct and avariable with the name <Parameter>sqlca</Parameter> as following:<ProgramListing>struct sqlca{ char sqlcaid[8]; long sqlabc; long sqlcode; struct {  int sqlerrml;  char sqlerrmc[70]; } sqlerrm; char sqlerrp[8]; long sqlerrd[6]; /* 0: empty                                         */ /* 1: empty                                         */ /* 2: number of rows processed in an INSERT, UPDATE */ /*    or DELETE statement                           */ /* 3: empty                                         */ /* 4: empty                                         */ /* 5: empty                                         */ char sqlwarn[8]; /* 0: set to 'W' if at least one other is 'W'       */ /* 1: if 'W' at least one character string	     */ /*    value was truncated when it was               */ /*    stored into a host variable.                  */ /* 2: empty                                         */ /* 3: empty                                         */ /* 4: empty                                         */ /* 5: empty                                         */ /* 6: empty                                         */ /* 7: empty                                         */ char sqlext[8];} sqlca;</ProgramListing></Para><Para>If an error occured in the last <Acronym>SQL</Acronym> statement then <Parameter>sqlca.sqlcode</Parameter>will be non-zero. If <Parameter>sqlca.sqlcode</Parameter> is less that 0 then this issome kind of serious error, like the database definition does not matchthe query given. If it is bigger than 0 then this is a normal error likethe table did not contain the requested row.</Para><Para>sqlca.sqlerrm.sqlerrmc will contain a string that describes the error.The string ends with the line numberin the source file.</Para><Para>List of errors that can occur:<VariableList><VarListEntry><Term>-12, Out of memory in line %d.</Term><ListItem><Para>Does not normally occur. This is a sign that your virtual memory isexhausted.</Para></ListItem></VarListEntry><VarListEntry><Term>-200, Unsupported type %s on line %d.</Term><ListItem><Para>Does not normally occur. This is a sign that the preprocessor hasgenerated something that the library does not know about. Perhaps youare running incompatible versions of the preprocessor and the library.</Para></ListItem></VarListEntry><VarListEntry><Term>-201, Too many arguments line %d.</Term><ListItem><Para>This means that <ProductName>Postgres</ProductName> has returned morearguments than we havematching variables. Perhaps you have forgotten a couple of the hostvariables in the <Command>INTO :var1,:var2</Command>-list.</Para></ListItem></VarListEntry><VarListEntry><Term>-202, Too few arguments line %d.</Term><ListItem><Para>This means that <ProductName>Postgres</ProductName> has returned fewerarguments than we havehost variables. Perhaps you have too many host variables in the <Command>INTO :var1,:var2</Command>-list.</Para> </ListItem></VarListEntry><VarListEntry><Term>-203, Too many matches line %d.</Term><ListItem><Para>This means that the query has returned several lines but thevariables specified are no arrays. The <Command>SELECT</Command> you madeprobably was not unique.</Para></ListItem></VarListEntry><VarListEntry><Term>-204, Not correctly formatted int type: %s line %d.</Term><ListItem><Para>This means that the host variable is of an <Type>int</Type> type and the fieldin the <ProductName>Postgres</ProductName> database is of another type and contains a value that cannot be interpreted as an <Type>int</Type>. The library uses <Function>strtol</Function>for this conversion.</Para></ListItem></VarListEntry><VarListEntry><Term>-205, Not correctly formatted unsigned type: %s line %d.</Term><ListItem><Para>This means that the host variable is of an <Type>unsigned int</Type> type andthe field in the <ProductName>Postgres</ProductName> database is of another type and contains avalue that cannot be interpreted as an <Type>unsigned int</Type>. The libraryuses <Function>strtoul</Function> for this conversion.</Para></ListItem></VarListEntry><VarListEntry><Term>-206, Not correctly formatted floating point type: %s line %d.</Term><ListItem><Para>This means that the host variable is of a <Type>float</Type> type andthe field in the <ProductName>Postgres</ProductName> database is of another type and contains avalue that cannot be interpreted as an <Type>float</Type>. The libraryuses <Function>strtod</Function> for this conversion.</Para></ListItem></VarListEntry><VarListEntry><Term>-207, Unable to convert %s to bool on line %d.</Term><ListItem><Para>This means that the host variable is of a <Type>bool</Type> type andthe field in the <ProductName>Postgres</ProductName> database is neither 't'nor 'f'.</Para></ListItem></VarListEntry><VarListEntry><Term>-208, Empty query line %d.</Term><ListItem><Para><ProductName>Postgres</ProductName> returned PGRES_EMPTY_QUERY, probablybecause the query indeed was empty.</Para></ListItem></VarListEntry><VarListEntry><Term>-220, No such connection %s in line %d.</Term><ListItem><Para>The program tries to access a connection that does not exist.</Para></ListItem></VarListEntry><VarListEntry><Term>-221, Not connected in line %d.</Term><ListItem><Para>The program tries to access a connection that does exist but is not open.</Para></ListItem></VarListEntry><VarListEntry><Term>-230, Invalid statement name %s in line %d.</Term><ListItem><Para>The statement you are trying to use has not been prepared.</Para></ListItem></VarListEntry><VarListEntry><Term>-400, Postgres error: %s line %d.</Term><ListItem><Para>Some <ProductName>Postgres</ProductName> error. The message contains the error message from the<ProductName>Postgres</ProductName> backend.</Para></ListItem></VarListEntry><VarListEntry><Term>-401, Error in transaction processing line %d. </Term><ListItem><Para><ProductName>Postgres</ProductName> signalled to us that we cannot start,commit or rollback the transaction.</Para></ListItem></VarListEntry><VarListEntry><Term>-402, connect: could not open database %s.</Term><ListItem><Para>The connect to the database did not work.</Para></ListItem></VarListEntry><VarListEntry><Term>100, Data not found line %d.</Term><ListItem><Para>This is a "normal" error that tells you that what you are quering cannotbe found or we have gone through the cursor.</Para></ListItem></VarListEntry></VariableList></Para></Sect2></sect1><Sect1><Title>Limitations</Title><Para>What will never be included and why or what cannot be done with thisconcept.<VariableList><VarListEntry><Term>oracles single tasking possibility</Term><ListItem><Para>Oracle version 7.0 on AIX 3 uses the OS-supported locks on the sharedmemory segments and allows the application designer to link anapplication in a so called single tasking way. Instead of starting oneclient process per application process both the database part and theapplication part is run in the same process. In later versions of oraclethis is no longer supported.</Para><Para>This would require a total redesign of the <ProductName>Postgres</ProductName> access model andthat effort can not justify the performance gained.</Para></ListItem></VarListEntry></VariableList></Para></sect1><Sect1><Title>Porting From Other <Acronym>RDBMS</Acronym> Packages</Title><Para>The design of <Application>ecpg</Application> follows SQL standard. Soporting from a standard RDBMS should not be a problem. Unfortunately thereis no such thing as a standard RDBMS. So <Application>ecpg</Application>also tries to understand syntax additions as long as they do not createconflicts with the standard. </Para><Para>The following list shows all the known incompatibilities. If you find onenot listed please notify <ULink url="mailto:meskes@postgresql.org">MichaelMeskes</ULink>. Note, however, that we list only incompatibilities froma precompiler of another RDBMS to <Application>ecpg</Application> and notadditional <Application>ecpg</Application> features that these RDBMS do nothave.</Para><Para><VariableList><VarListEntry><Term>Syntax of FETCH command</Term><ListItem><Para>The standard syntax of the FETCH command is:</para><Para>FETCH [direction] [amount] IN|FROM <Replaceable>cursor name</Replaceable>.</Para><para><Application>ORACLE</Application>, however, does not use the keywords INresp. FROM. This feature cannot be added since it would create parsingconflicts.</Para></ListItem></VarListEntry></VariableList></Para></sect1><Sect1><Title>Installation</Title><Para>Since version 0.5 <Application>ecpg</Application> is distributed together with <ProductName>Postgres</ProductName>. So youshould get your precompiler, libraries and header files compiled andinstalled by default as a part of your installation.</Para></sect1><Sect1><Title>For the Developer</Title><Para>This section is for those who want to develop the <Application>ecpg</Application> interface. Itdescribes how the things work. The ambition is to make this sectioncontain things for those that want to have a look inside and the sectionon How to use it should be enough for all normal questions.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -