/* This source file is part of the SCS module "Sqlite3" For the latest info, see http://www.scolring.org Copyright (c) 2010 Stephane Bisaro, aka Iri This program is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 3 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 Lesser General Public License for more details. You should have received a copy of the GNU Lesser 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, or go to http://www.gnu.org/copyleft/lesser.txt For others informations, please contact us from http://www.scolring.org/ or http://www.irizone.net/ */ var RETURN_ATTRVALUE = 3;; var RETURN_ATTRIBUT = 2;; var RETURN_VALUE = 1;; var RETURN_NOTHING = 0;; typeof db = ObjSqlite;; typeof db_file = S;; typeof db_connected = I;; typeof db_type = I;; /* 0 : file, 1 : memory, 2 : temp file, 3 : incorrect */ /* Replace in a string the parameter 'p' to the word 'word' fun [S S S] S */ fun replaceWord (s, p, word)= let 1 -> r in while r do let strfind p s 0 -> pos in if pos == nil then set r = 0 else let substr s 0 pos -> before in let substr s pos+(strlen p) strlen s -> after in ( set s = strcatn before :: word :: after :: nil; set r = 1 ); s;; /* fun [S] S */ fun getRessInParameter (s)= let strfind "%ress" s 0 -> pos in if pos == nil then nil else let strfind " " s pos -> posend in let substr s pos+(strlen "%ress") posend -> ressName in ressName;; /* fun [S I] I */ fun setFlagInRun2 (p, flag)= if (!strcmpi p "ATTRIBUT") then set flag = flag + RETURN_ATTRIBUT else if (!strcmpi p "VALUE") then set flag = flag + RETURN_VALUE else flag;; /* fun [[S r1]] I */ fun setFlagInRun (l)= let hd l -> p1 in let hd tl l -> p2 in let setFlagInRun2 p1 0 -> flag in setFlagInRun2 p2 flag;; /* fun [S] I */ fun dg_getType (value)= set db_type = if (!strcmp value ":memory") then 1 else if (!strcmp value ":temp") then 2 else let _checkpack value -> pfile in if pfile == nil then 3 else 0;; /* function called at each sql request, if any Flag is the "user_data" : : RETURN_ATTRVALUE : attribut and value are retourned RETURN_ATTRIBUT : attribut is returned only, RETURN_VALUE : value only, RETURN_NOTHING : nothing (if needed) fun [ObjSqlite I S S] I*/ fun db_reflex (obj, u, attr, value)= let u -> [cli flag event] in if flag == RETURN_ATTRVALUE then _DMSevent this cli event strbuild (attr :: value :: nil) :: nil nil else if flag == RETURN_ATTRIBUT then _DMSevent this cli event attr nil else if flag == RETURN_VALUE then _DMSevent this cli event value nil else _DMSevent this cli event nil nil; 0;; /* Connect to the database. Return 0 if ok (or already connected, 1 otherwise */ fun db_connect ()= if db_connected then 0 else ( set db_connected = 0; set db = if !db_type then _sqliteOpenFile _channel _checkpack db_file else if db_type then _sqliteOpenMemory _channel else if db_type == 2 then _sqliteOpenTemp _channel else nil; if db == nil then 1 else ( set db_connected = 1; 0 ) );; /* Disconnect to the database Return 0 if ok, nil if already closed or the error code if any */ fun db_disconenct ()= if db_connected then let _sqliteClose db -> res in ( if res != 0 then _DMSevent this nil "log" strcatn "SQLITE3 module >> Database named \"" :: db_file :: "\" is busy (error code = " :: (itoa res) :: "). Database not closed !" :: nil nil else 0; // db_connected = 0; res ) else nil;; /* run a sql request fun [CLIENT S S] I */ fun run (cli, param, event)= _DMSevent this nil "log" strcat "running sql " param nil; if !db_connect then let lineextr param -> l in let hd l -> sql in let setFlagInRun tl l -> flag in let ( _sqliteCallbackExec db @db_reflex [cli flag event]; _sqliteExec db sql nil ) -> res in if res != 0 then _DMSevent this nil "log" strcatn "SQLITE3 module >> Database named \"" :: db_file :: "\" : executed request " :: sql :: " has returned this error code : " :: (itoa res) :: nil nil else 0 else let hd hd strextr param -> sql in _DMSevent this nil "log" strcatn "SQLITE3 module >> Database named \"" :: db_file :: "\" is not onnected. Request \"" :: sql :: "\" not ran" :: nil nil; db_disconenct; 0;; /* Called before the server close fun [] I */ fun beforeClose ()= db_disconenct;; /* Called when a client logout fun [CLIENT] I */ fun cliDestroyed (cli)= _DMSevent this cli "destroyed" _DMSgetLogin cli nil;; /* Define the module callbacks fun [DMI CLIENT S S S] I */ fun activate (from, cli, action, param, rep)= if !strcmp action "runEx" then if param == nil then _DMSevent this cli "returnEx" nil nil else let lineextr param -> l in let replaceWord hd l "%login" _DMSgetLogin cli -> sql in let replaceWord sql "%ip" _DMSgetIP cli -> sql in let getRessInParameter sql -> ressName in let replaceWord sql strcat "%ress" ressName _DMSgetress cli ressName -> sql in let replaceWord sql "%lang" _DMSgetLanguage cli -> sql in run cli linebuild sql :: tl l "returnEx" else if !strcmp action "run" then run cli param "return" else 0;; /* Part server initialization */ fun IniDMI (file)= let strextr _getpack _checkpack file -> l in set db_file = getInfo l "db_file"; set db_connected = 0; dg_getType db_file; if db_type == 3 then _DMSevent this nil "log" strcatn "SQLITE3 module >> Database named \"" :: db_file :: "\" is incorrect or unknown. Module not initialized !" :: nil nil else _DMSregisterDMI this @activate @cliDestroyed nil @beforeClose; 0;;