/* * Loops.cpp * 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 "sqlmeta.h" MetaLoop::MetaLoop(MetaParser *MyParser, Cache *MyCache) { Parser=MyParser; Parent=MyCache; //Store the loop in the cache (& remember the previous loop) Prev=Parent->Loop; Parent->Loop=this; } MetaLoop::~MetaLoop() { //Restore the previous loop in the cache Parent->Loop=Prev; } CacheCommand MetaLoop::Feed(CacheBlock *Block, long *JumpID) { printf("This should not be on your screen...\n"); return Error; } LoopWith::LoopWith(MetaParser *MyParser, Cache *MyCache, int *Result, char *Arg1) : MetaLoop::MetaLoop(MyParser,MyCache) { DBConnection *Conn = (DBConnection *)new DBConnection(Parser,Parser->DB); //Remember the query in the cache PrevQuery=Parser->Query; SkipUntilLoop=0; try { if (Conn==NULL) throw Parser->Error(ErrMalloc,"LoopWith::LoopWith"); if (!Conn->TryConnect()) throw 0; if (!Conn->Query(Arg1)) throw 0; if (!Conn->GetResult()) throw 0; if (!Conn->NextRow()) SkipUntilLoop=1; //Got query results.. //Show them to the world Query=Conn; Parser->Query=Conn; } catch (int Err) { *Result=Err; return; } *Result=1; FirstBlock=1; } LoopWith::~LoopWith(void) { //Restore previous loop's query Parser->Query=PrevQuery; } CacheCommand LoopWith::Feed(CacheBlock *Block, long *JumpID) { int Argc; if (FirstBlock) { FirstBlock=0; LoopStartID=Block->ID; } if (Block->Type==Code) { //Check arg[0] for 'loop' command Argc=Parent->SplitArgs(Block->Data); if (Argc==-1) return Error; if (!strcmp(Parent->Arg[0],"loop")) { //Check argc if (Argc!=1) { Parser->Error(ErrArgNr,"loop"); return Error; } //Found 'loop' command.. want to loop again? if ((Query!=NULL) && (Query->NextRow())) { *JumpID=LoopStartID; return Jump; } //tell cache to end the loop return EndLoop; } } //It's ok... //Let's execute this command (unless we have no query results!) if (SkipUntilLoop) return SkipToNextCmd; return Exec; }