builder.h

00001 /********************************************************************\
00002  * builder.h : Compile SQL queries from C language values           *
00003  * Copyright (C) 2001 Linas Vepstas <linas@linas.org>               *
00004  *                                                                  *
00005  * This program is free software; you can redistribute it and/or    *
00006  * modify it under the terms of the GNU General Public License as   *
00007  * published by the Free Software Foundation; either version 2 of   *
00008  * the License, or (at your option) any later version.              *
00009  *                                                                  *
00010  * This program is distributed in the hope that it will be useful,  *
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of   *
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the    *
00013  * GNU General Public License for more details.                     *
00014  *                                                                  *
00015  * You should have received a copy of the GNU General Public License*
00016  * along with this program; if not, contact:                        *
00017  *                                                                  *
00018  * Free Software Foundation           Voice:  +1-617-542-5942       *
00019  * 51 Franklin Street, Fifth Floor    Fax:    +1-617-542-2652       *
00020  * Boston, MA  02110-1301,  USA       gnu@gnu.org                   *
00021 \********************************************************************/
00022 
00023 /*
00024  * FILE:
00025  * builder.h
00026  *
00027  * FUNCTION:
00028  * Generic SQL query builder.  This class can be sued to construct
00029  * a basic sql query statement (of the type 'select', 'update' or 
00030  * 'insert') by simply making C calls indicating the table and the 
00031  * fields to query.
00032  *
00033  * Its fairly limited in the range of sql syntax that it supports,
00034  * but on the other hand, the code here is/should be general enough 
00035  * to work with  any SQL implementation.
00036  *
00037  * HISTORY:
00038  * Linas Vepstas January 2001
00039  */
00040 
00041 #ifndef SQL_BUILDER_H
00042 #define SQL_BUILDER_H
00043 
00044 #include "qof.h"
00045 
00046 #define SQL_DBL_FMT "%24.18g"
00047 
00048 typedef enum {
00049    SQL_UPDATE = 'm',  /* m == modify */
00050    SQL_INSERT = 'a',  /* a == add */
00051    SQL_SELECT = 'q',  /* q == query */
00052    SQL_DELETE = 'd'   /* d == drop, delete */
00053 } sqlBuild_QType;
00054 
00055 typedef struct _builder sqlBuilder;
00056 
00057 sqlBuilder * sqlBuilder_new(void);
00058 void sqlBuilder_destroy (sqlBuilder *);
00059 
00060 /* The sqlBuild_Table() routine starts building a new SQL query 
00061  *    on table 'tablename'.  Any previously started query is erased.
00062  *
00063  *    When building 'select' type statments, crude table joins are 
00064  *    supported: the 'tablename' can in fact be a comma-separated list 
00065  *    of tables.  This field is copied directly as follows:
00066  *    "SELECT ... FROM tablename WHERE ..." so anything valid in that
00067  *    position is tolerated.
00068  */
00069 void sqlBuild_Table (sqlBuilder *b,
00070                      const char *tablename,
00071                      sqlBuild_QType qtype);
00072 
00073 
00074 /* Set tag-value pairs.  Each of these adds the indicated
00075  * tag and value to an UPDATE or INSERT statement.  For SELECT
00076  * statements, val may be NULL (and is ignored in any case).
00077  */
00078 void sqlBuild_Set_Str   (sqlBuilder *b, const char *tag, const char *val);
00079 void sqlBuild_Set_Char  (sqlBuilder *b, const char *tag, char val);
00080 void sqlBuild_Set_GUID  (sqlBuilder *b, const char *tag, const GUID *val);
00081 void sqlBuild_Set_Date  (sqlBuilder *b, const char *tag, Timespec val);
00082 void sqlBuild_Set_Int64 (sqlBuilder *b, const char *tag, gint64 val);
00083 void sqlBuild_Set_Int32 (sqlBuilder *b, const char *tag, gint32 val);
00084 void sqlBuild_Set_Double(sqlBuilder *b, const char *tag, double val);
00085 
00086 
00087 /* build the update 'where' clause */
00088 /* typically, the primary tag is used in the where clauses */
00089 /* this where clause is used for both SELECT and UPDATE statements */
00090 
00091 void sqlBuild_Where_Str  (sqlBuilder *b, const char *tag, const char *val);
00092 void sqlBuild_Where_GUID (sqlBuilder *b, const char *tag, const GUID *val);
00093 void sqlBuild_Where_Int32 (sqlBuilder *b, const char *tag, gint32 val);
00094 
00095 
00096 /* The sqlBuild_Query() routine returns a valid SQL query 
00097  *    statement that reflects the set of build calls just made.   
00098  *    This string is freed when sqlBuilder_destroy() is called, 
00099  *    so make a copy if you need it.
00100  *
00101  *    This resulting query string is probably general enough to 
00102  *    work with almost any SQL db, I beleive.     
00103  */   
00104 const char *sqlBuild_Query (sqlBuilder *b);
00105 
00106 
00107 #endif /* SQL_BUILDER_H */
00108 

Generated on Thu Jul 3 05:06:28 2008 for GnuCash by  doxygen 1.5.2