Unix under the common interface function article from the database: http: //www2.ccw.com.cn Author: Ding Changrong (2002-04-17 06:02:00)
In UNIX environments, commonly used database development methods have ODBC, call layer interface (CLI), SQL embedd C language or other specific development tools. Since different development methods are provided to the C language access database, the writing method is different, so the database application so-developed is difficult to generally in different development environments, resulting in poor transplantability, such as using SQL embedd C language development When the program is changed to the call layer interface (CLI) to write, all C programs in the program have to be modified. To this end, the author designed a set of C language universal interface functions in the UNIX environment, using this universal interface function, replacing the program in a variety of development environments of the database system, improve the code versatility. Increased development efficiency. This article describes the design idea, function prototype design and application examples of this universal interface function. Design ideas use C language modular programming methods to focus on a function (mainly standard SQL statement) related to database access in a module (DBADM), and the function "package" of all related interfaces is in this module. The module serves as a "middleware" accessed, providing support for universal interface functions. Thus, when accessing the database in other application modules, only these universal interface functions can only be called, avoiding the problem of inconsistency when the database access function provided by the database system is avoided, and the C language source program part and the data of the database application are actually The access section (DBADM) is separated. At the same time, the cache switching technology is applied to apply a memory (global variable) for each open table, and the recording buffer for data exchange is performed as the SQL statement of the database. When using the SQL statement to read the data in the database, store the data first in the recording buffer (its value is converted to a character type). Similarly, when the data is written to the database, it is also possible to update in the recording buffer, and then use the record cache area to assemble the INSERT, UPDATE, etc., thereby avoiding the direct use of the SQL statement. In this way, we can write the corresponding database interface module (DBADM) for the common development environment of the database system. If you want to change the development environment, you only need to change the database interface module of the Makefile file, other modules can be eliminated. The expansion, modification, and improvement of function functions also only need to change the database interface module. In this way, not only the versatility of the program, not only from the repetition of different interfaces, and put energy in the design and development of function applications, thereby improving the development efficiency. Function Design Database System Data Access is mainly made using SQL query language, the relevant data access statement mainly has SELECT (query), INSERT (insert), Update, delete, Rollback, commit (Submit), etc. By comparing the commonality and characteristics of several common development environments, we can define them into a "universal" database access function for additional programs.
Below is the main code of the two interface functions defined by the author (Other interface function code omitted): / * dbadm.ec informix-esql * / #include
Processing all fields * / for (ufieldvar = udesc [iFRX] -> SQLVAR, IIDX = 0; IIDX
/ DbInsertRecord (int iTableNo) {struct sqlvar_struct * uFeildVar; int iIdx; char szTableName [60]; / * obtained from the szSQLfhl [iTableNo] in table * / createtableName (szSQLfhl [iTableNo], szTableName); / * assembled insert statement * / sprintf (Szsqlbuf, "Insert INTO% S VALUES (", SZTABLENAME); for (IIDX = 0; IIDX