'extension module for ScriptBasic
'04:43 18/05/2012
'
'Charles Pegge
'#file "sqlite3_mdl.dll"
'
$ dll
$ Filename "sqlite3_mdl.dll"
include "..\..\inc\RTL32.inc"
function GetStringParam(sys pst,pm) as string
'============================================
sys c,p
c=*(pst+0xf4)
p=*pst
p=call c p, pm, *(p+0x8c)
zstring pz at *p
return pz
end function
function PutStringParam(sys pst,pm,string s)
'===========================================
sys c,p
c=*(pst+0xf4)
p=*pst
p=call c p, pm, *(p+0x8c)
zstring pz at *p
pz=s
end function
function GetLongParam(sys pst,pm) as long
'========================================
sys c,p
c=*(pst+0xf8)
p=*pst
p=call c p, pm, *(p+0x8c)
return *p
end function
function PutLongParam(sys pst,pm, sys v)
'=======================================
sys c,p
c=*(pst+0xf8)
p=*pst
p=call c p, pm, *(p+0x8c)
*p=v
end function
function GetDoubleParam(sys pst,pm) as double
'============================================
sys c,p
c=*(pst+0xfc)
p=*pst
p=call c p, pm, *(p+0x8c)
double pd at p
return pd
end function
function PutDoubleParam(sys pst,pm,double d)
'===========================================
sys c,p
c=*(pst+0xfc)
p=*pst
p=call c p, pm, *(p+0x8c)
double pd at p
pd=d
end function
sub ReturnString(sys pst,pRetVal,string s)
'=========================================
sys c,p,ls
ls=len(s)
c=*(pst+0x0c)
p=*pst
p=call c *(p+0x94),ls , *(p+0x8c)
*pRetVal=p
copy *p,strptr s,ls
end sub
sub ReturnLong(sys pst,pRetVal,Long v)
'=====================================
sys c,p
c=*(pst+0x10)
p=*pst
p=call c *(p+0x94), *(p+0x8c)
*pRetVal=p
long pv at p
pv=v
'
'mov ecx,pst : mov eax,[ecx] : push [eax+0x8c] : push [eax+0x94] : call [ecx+0x10]
'mov ecx,pReturnValue : mov [ecx],eax : mov edx,v : mov [eax],edx
''
end sub
sub ReturnDouble(sys pst,pRetVal,double d)
'=========================================
sys c,p
c=*(pst+0x18)
p=*pst
p=call c *(p+0x94), *(p+0x8c)
*pRetVal=p
double pd at p
pd=d
end sub
function GetParamPtr(sys pParams, pn) as sys
'===========================================
return *(*pParams+pn*4-4) 'pointer to parameter
end function
function versmodu cdecl (sys Version, pszVariation, ppModuleInternal) as sys export
'==================================================================================
'print "Version: " hex version
return Version
end function
function bootmodu cdecl (sys pSt, ppModuleInternal, pParameters, pReturnValue) as sys export
'===========================================================================================
'print "Boot!"
end function
function finimodu cdecl (sys pSt, ppModuleInternal, pParameters, pReturnValue) as sys export
'===========================================================================================
'print "Finish!"
end function
function trial cdecl (sys pSt, ppModuleInternal, pParameters, pReturnValue) as sys export
'========================================================================================
sys w,pm,pn
string cr=chr(13)+chr(10)
'
sys v
string pr
double d
'
if pParameters then
'
'HOW MANY ARGUMENTS GIVEN
'------------------------
'
w=*(pParameters+8) >> 2
'
'print "No of Arguments given: " w
'
'PARAM pn
'--------
'
'pParameters->Value.aValue[(X)-1]
'
for pn=1 to w
'
'index base 1
'
pm=GetParamPtr(pparameters,pn)
pr+= "As String: " GetStringParam(pst,pm) + cr +
"As Long: " GetLongParam(pst,pm) + cr +
"As Double: " GetDoubleParam(pst,pm) + cr +
cr
next
'
pm=GetParamPtr(pParameters,1)
PutDoubleParam(pst,pm,1111.11)
pm=GetParamPtr(pParameters,2)
PutStringParam(pst,pm,"hello")
pm=GetParamPtr(pParameters,3)
PutLongParam(pst,pm,3)
pm=GetParamPtr(pParameters,4)
PutLongParam(pst,pm,4)
'
else
'noparams
end if
'
'ReturnLong(pst,pReturnValue,1234.5)
'ReturnDouble(pst,pReturnValue,1.234.5)
ReturnString(pst,pReturnValue,"From Oxygen:"+cr+cr+pr)
'
end function
'============
'needs SQLite3.dll
'
'SQLITE_API int sqlite3_open(
' const char *filename, /* Database filename (UTF-8) */
' sqlite3 **ppDb /* OUT: SQLite db handle */
');
'
'
'SQLITE_API int sqlite3_exec(
'sqlite3*, /* An open database */
'const char *sql, /* SQL to be evaluated */
'int (*callback)(void*,int,char**,char**), /* Callback function */
'void *, /* 1st argument to callback */
'char **errmsg /* Error msg written here */
');
'
'
'SQLITE_API int sqlite3_prepare_v2(
' sqlite3 *db, /* Database handle */
' const char *zSql, /* SQL statement, UTF-8 encoded */
' int nByte, /* Maximum length of zSql in bytes. */
' sqlite3_stmt **ppStmt, /* OUT: Statement handle */
' const char **pzTail /* OUT: Pointer to unused portion of zSql */
');
'
'
'SQLITE_API int sqlite3_step(sqlite3_stmt*);
'
'
'SQLITE_API const unsigned char *sqlite3_column_text(sqlite3_stmt*, int iCol);
'
'
'SQLITE_API int sqlite3_close(sqlite3 *);
'
'
extern lib "sqlite3.dll"
sys sqlite3_open (char*name,sys*db)
sys sqlite3_exec (sys db,char* s, sys p1, sys p2, sys*dberr)
sys sqlite3_prepare_v2 (sys db, char*s, sys p1, sys*stmt, sys p2)
sys sqlite3_step (sys n)
char* sqlite3_column_text (sys row, sys col)
sys sqlite3_close (sys db)
'
end extern
'
% SQLITE_ROW = 100
function sqlite3_demo cdecl (sys pSt, ppModuleInternal, pParameters, pReturnValue) as sys, export
'================================================================================================
sys hdb
sys dberr
sys stmt
'
char * errmsg
string cr=chr(13)+chr(10)
string pr="DataBase Listing:" cr
'
sqlite3_open "testsql",hdb
'
sqlite3_exec hdb, "CREATE TABLE demo(someval INTEGER, sometxt TEXT);", 0, 0, dberr
sqlite3_exec hdb, "INSERT INTO demo VALUES (123, 'Hello');", 0, 0, dberr
sqlite3_exec hdb, "INSERT INTO demo VALUES (234, 'cruel');", 0, 0, dberr
sqlite3_exec hdb, "INSERT INTO demo VALUES (345, 'world');", 0, 0, dberr
'
result = sqlite3_prepare_v2 hdb, "SELECT * FROM demo;" cr, -1, stmt, 0
'
if dberr then @errmsg=dberr : print errmsg
'
while sqlite3_step(stmt) = SQLITE_ROW
pr+=sqlite3_column_text(stmt, 0) " - " sqlite3_column_text(stmt, 1) cr
wend
'
sqlite3_close hdb
ReturnString(pst,pReturnValue,pr)
end function
function sqlite3_errmsg cdecl (sys pSt, ppModuleInternal, pParameters, pReturnValue) as sys, export
'==================================================================================================
sys dberror
char errmsg at dberror
sys np,pm
'
if pParameters then
np=*(pParameters+8) >> 2
pm=GetParamPtr(pparameters,1)
dberror=GetLongParam(pst,pm)
else
'noparams
end if
'
ReturnString(pst,pReturnValue,errmsg)
'
end function
function sqlite3_open cdecl (sys pSt, ppModuleInternal, pParameters, pReturnValue) as sys, export
'================================================================================================
string namedb
sys hdb,rtn
sys np,pm
'
if pParameters then
np=*(pParameters+8) >> 2
pm=GetParamPtr(pparameters,1)
namedb=GetStringParam(pst,pm)
rtn=sqlite3_open namedb,hdb
pm=GetParamPtr(pParameters,2)
PutLongParam(pst,pm,hdb)
else
'noparams
end if
'
ReturnLong(pst,pReturnValue,rtn)
'
end function
function sqlite3_close cdecl (sys pSt, ppModuleInternal, pParameters, pReturnValue) as sys, export
'================================================================================================
sys hdb,rtn
sys np,pm
'
if pParameters then
np=*(pParameters+8) >> 2
pm=GetParamPtr(pparameters,1)
hdb=GetLongParam(pst,pm)
rtn=sqlite3_close hdb
else
'noparams
end if
'
ReturnLong(pst,pReturnValue,rtn)
'
end function
function sqlite3_exec cdecl (sys pSt, ppModuleInternal, pParameters, pReturnValue) as sys, export
'================================================================================================
sys hdb,rtn
string instruct
sys dberr
sys np,pm
'
if pParameters then
np=*(pParameters+8) >> 2
pm=GetParamPtr(pparameters,1)
hdb=GetLongParam(pst,pm)
pm=GetParamPtr(pparameters,2)
instruct=GetStringParam(pst,pm)
rtn=sqlite3_exec hdb, instruct, 0, 0, dberr
pm=GetParamPtr(pParameters,5)
PutLongParam(pst,pm,dberr)
else
'noparams
end if
'
ReturnLong(pst,pReturnValue,rtn)
'
end function
function sqlite3_prepare_v2 cdecl (sys pSt, ppModuleInternal, pParameters, pReturnValue) as sys, export
'======================================================================================================
sys hdb
string instruct
sys statement
sys result
sys np,pm
'
if pParameters then
np=*(pParameters+8) >> 2
pm=GetParamPtr(pparameters,1)
hdb=GetLongParam(pst,pm)
pm=GetParamPtr(pparameters,2)
instruct=GetStringParam(pst,pm)
result = sqlite3_prepare_v2 hdb, instruct, -1, statement, 0
pm=GetParamPtr(pparameters,4)
PutLongParam(pst,pm,statement)
else
'noparams
end if
'
ReturnLong(pst,pReturnValue,result)
'
end function
function sqlite3_step cdecl (sys pSt, ppModuleInternal, pParameters, pReturnValue) as sys, export
'================================================================================================
sys statement
sys result
sys np,pm
'
if pParameters then
np=*(pParameters+8) >> 2
pm=GetParamPtr(pparameters,1)
statement=GetLongParam(pst,pm)
result=sqlite3_step(statement)
else
'noparams
end if
'
ReturnLong(pst,pReturnValue,result)
'
end function
function sqlite3_column_text cdecl (sys pSt, ppModuleInternal, pParameters, pReturnValue) as sys, export
'=======================================================================================================
sys statement
string text
sys field
sys np,pm
'
if pParameters then
np=*(pParameters+8) >> 2
pm=GetParamPtr(pparameters,1)
statement=GetLongParam(pst,pm)
pm=GetParamPtr(pparameters,2)
field=GetLongParam(pst,pm)
text=sqlite3_column_text(statement, field)
else
'noparams
end if
'
ReturnString(pst,pReturnValue,text)
'
end function