Navigation
Home
gpl
sqlmeta
v1.1b
sqlmeta.h








































sqlmeta.h
   /*
    * SQLMeta.h
    * Part of SQLMeta, a language to use sql-queries in html pages.
    *
    * Copyright (C) 2001  Daan Vreeken
    *
    * This program is free software; you can redistribute it and/or
    * modify it under the terms of the GNU General Public License
    * as published by the Free Software Foundation; either version 2
    * of the License, or (at your option) any later version.
    *
    * This program is distributed in the hope that it will be useful,
    * but WITHOUT ANY WARRANTY; without even the implied warranty of
    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    * GNU General Public License for more details.
    *
    * You should have received a copy of the GNU General Public License
    * along with this program; if not, write to the Free Software
    * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
    *
    */
   
   #include <stdio.h>
   #include <stdlib.h>
   #include <stdarg.h>
   
   #include <mysql/mysql.h>
   
   #include <Cgicc.h>
   using namespace cgicc;
   
   
   #define Version			"SQLMeta meta parser version 1.1b\nWritten by Daan Vreeken"
   
   
   #define MaxArgs			20
   #define MaxNesting		20
   #define MaxLineLen		1000
   
   
   #define ErrArgNr		"Invalid number of arguments for '%s'!"
   #define ErrOpenFile		"Could not open include file '%s'!"
   #define ErrNesting		"Nested too deep.. refusing to cooperate!"
   #define ErrMalloc		"Could not allocate memory in '%s'!"
   #define ErrSQLConnect		"Error connecting to sql server!\n%s\n"
   #define ErrSQLQuery		"Error in query!\n%s\n"
   #define ErrSQLConnectFirst	"Connect to sql server first!\n"
   #define ErrSQLNoResult		"Empty result set returned!\n"
   #define ErrEnvCouldNotSet	"Could not set variable!\n"
   #define ErrEnvNotSet		"Variable '%s' not set!\n"
   #define ErrLineTooLong		"Line too long!\n"
   #define ErrVarSubst		"Could not substitute ${%s}, %s!"
   #define ErrVarUnterminated	"Unterminated variable! (saw '${' without '}')"
   #define ErrLoopUnterminated	"Unterminated loop!"
   #define ErrLoop			"Loop error! (saw '%s' without '%s')"
   
   typedef char			*ArgPtr;
   
   
   
   class SQLMeta;
   class MetaParser;
   class MetaLoop;
   class CacheBlock;
   
   
   
   enum CacheCommand
   {
   	Exec=0, SkipThisOne, SkipToNextCmd, Jump, EndLoop, Error
   };
   
   
   
   enum CacheType
   {
   	HTML=0, Code
   };
   
   
   	
   class Cache
   {
   public:
   				Cache(MetaParser *MyParent);
   	virtual			~Cache(void);
   				
   	virtual int		Feed(CacheType Type, char *Data, int Size);
   	virtual int		Flush(void);
   	
   	virtual void		AddLoop(MetaLoop *Description);
   	
   	virtual CacheBlock	*FindBlock(long ID);
   	virtual int		SplitArgs(char *Line);
   
   	int			LoopDepth;
   	MetaLoop		*Loop;
   	
   	CacheBlock		*FirstBlock;
   	CacheBlock		*LastBlock;
   	
   	ArgPtr			Arg[MaxArgs];
   	char			*ArgBuf;
   
   	MetaParser		*Parent;
   	
   private:
   	CacheBlock		*LastSearchedBlock;
   	long			LastID;
   };
   
   
   
   class DBInfo
   {
   public:
   				DBInfo(MetaParser *Parent);
   	virtual			~DBInfo(void);
   				
   	virtual int		SetHost(char *NewHost);
   	virtual int		SetUser(char *NewUser, char *NewPassword);
   	virtual int		SetDB(char *NewDB);
   	
   	MetaParser		*Parent;
   	
   	char			*Host;
   	char			*User;
   	char			*Password;
   	char			*DB;
   };
   
   
   
   class DBConnection
   {
   public:
   				DBConnection(MetaParser *MyParent, DBInfo *MyInfo);
   	virtual			~DBConnection(void);
   	
   	MetaParser		*Parent;
   	DBInfo			*Info;
   	
   	MYSQL			*Connection;
   	MYSQL_RES		*Result;
   	MYSQL_FIELD		*FieldName;
   	unsigned int		Fields;
   	unsigned long		*ColLength;
   	MYSQL_ROW		Row;
   	long			RowNr;
   	
   	int			Connected;
   	
   	virtual int		TryConnect(void);
   	virtual int		Query(char *Query);
   	virtual int		GetResult(void);
   	virtual int		NextRow(void);
   	virtual void		FreeResult(void);
   	
   	virtual void		ShowError(char *Err);
   };
   
   
   
   class MetaLoop
   {
   public:
   				MetaLoop(MetaParser *MyParser, Cache *MyCache);
   	virtual			~MetaLoop(void);
   	
   	virtual CacheCommand	Feed(CacheBlock *Block, long *JumpID);
   	
   	MetaLoop		*Prev;
   
   	MetaParser		*Parser;
   	Cache			*Parent;
   };
   
   
   
   class LoopWith : public MetaLoop
   {
   public:
   				LoopWith(MetaParser *MyParser, Cache *MyCache, int *Result, char *Arg1);
   	virtual			~LoopWith(void);
   				
   	virtual CacheCommand	Feed(CacheBlock *Block, long *JumpID);
   				
   	DBConnection		*PrevQuery;		//The previous loop's
   							//DBconnection is stored here
   	DBConnection		*Query;			//My own query...
   	
   	int			SkipUntilLoop;
   	
   	long			LoopStartID;
   	int			FirstBlock;
   };
   
   
   
   class LoopFor : public MetaLoop
   {
   public:
   				LoopFor(MetaParser *MyParser, Cache *MyCache, int *Result, char *Var, char *Array);
   	virtual			~LoopFor(void);
   				
   	virtual CacheCommand	Feed(CacheBlock *Block, long *JumpID);
   	
   	virtual void		SetNextVar(void);
   				
   	int			SkipUntilNext;
   
   	char			*VarName;
   	char			*LoopBuf;
   	ArgPtr			LoopArray[MaxArgs];
   	int			LoopCnt;
   	int			LoopNow;
   	
   	long			LoopStartID;
   	int			FirstBlock;
   	
   };
   
   
   
   class CacheBlock
   {
   public:
   				CacheBlock(Cache *MyParent, CacheType MyType, long MyID, char *MyData, int MySize);
   	virtual			~CacheBlock(void);
   	CacheType		Type;
   	long			ID;
   	
   	char			*Data;
   	int			Size;
   	
   	Cache			*Parent;
   	CacheBlock		*Next;
   	CacheBlock		*Prev;
   };
   
   
   
   class MetaParser
   {
   public:
   			MetaParser(SQLMeta *MyParent);
   	virtual		~MetaParser(void);
   	
   private:
   	SQLMeta		*Parent;
   
   	ArgPtr		Arg[MaxArgs];
   
   public:
   	DBInfo		*DB;
   	DBConnection	*Query;			//The current query
   	int		DBIsMine;
   	
   	Cache		*MyCache;
   	
   	char		*FileName;
   	FILE		*Input;
   	long		LineCount;
   	int		NestCount;
   
   	virtual int	Parse(void);
   
   	virtual int	SplitArgs(char *Line, ArgPtr *Dst);
   
   	virtual int	PreProcess(char *Line);
   	virtual int	ExecCommand(char *Line);
   	virtual void	SpitoutHTML(char *Data, int Size);
   	
   	virtual void	Print(char *Str, ...);
   	virtual int	Error(char *Str, ...);
   };
   
   
   
   class SQLMeta
   {
   public:
   			SQLMeta(void);
   			
   	FILE		*InputStream;
   	FILE		*OutputStream;
   	FILE		*ErrorStream;
   	
   	Cgicc		*Cgi;
   
   	virtual void	Parse(char *FileName);
   
   	virtual void	Print(char *Str, ...);
   	virtual void	Error(char *Str, ...);
   };
   
   
   
   
   
   
   
   

syntax highlighted by Code2HTML, v. 0.9.1


Email me with questions/comments : Daan <Danovitsch @ Vitsch . net>