Add new API function sqlite3_create_window_function(), for creating new
[sqlite.git] / src / test_window.c
blobe04de5eb0c70d7b2d00452263fa135d301128a63
1 /*
2 ** 2018 June 17
3 **
4 ** The author disclaims copyright to this source code. In place of
5 ** a legal notice, here is a blessing:
6 **
7 ** May you do good and not evil.
8 ** May you find forgiveness for yourself and forgive others.
9 ** May you share freely, never taking more than you give.
11 *************************************************************************
14 #include "sqlite3.h"
16 #ifdef SQLITE_TEST
18 #include "sqliteInt.h"
19 #include <tcl.h>
21 extern int getDbPointer(Tcl_Interp *interp, const char *zA, sqlite3 **ppDb);
22 extern const char *sqlite3ErrName(int);
24 typedef struct TestWindow TestWindow;
25 struct TestWindow {
26 Tcl_Obj *xStep;
27 Tcl_Obj *xFinal;
28 Tcl_Obj *xValue;
29 Tcl_Obj *xInverse;
30 Tcl_Interp *interp;
33 typedef struct TestWindowCtx TestWindowCtx;
34 struct TestWindowCtx {
35 Tcl_Obj *pVal;
38 static void doTestWindowStep(
39 int bInverse,
40 sqlite3_context *ctx,
41 int nArg,
42 sqlite3_value **apArg
44 int i;
45 TestWindow *p = (TestWindow*)sqlite3_user_data(ctx);
46 Tcl_Obj *pEval = Tcl_DuplicateObj(bInverse ? p->xInverse : p->xStep);
47 TestWindowCtx *pCtx = sqlite3_aggregate_context(ctx, sizeof(TestWindowCtx));
49 Tcl_IncrRefCount(pEval);
50 if( pCtx ){
51 const char *zResult;
52 int rc;
53 if( pCtx->pVal ){
54 Tcl_ListObjAppendElement(p->interp, pEval, Tcl_DuplicateObj(pCtx->pVal));
55 }else{
56 Tcl_ListObjAppendElement(p->interp, pEval, Tcl_NewStringObj("", -1));
58 for(i=0; i<nArg; i++){
59 Tcl_Obj *pArg;
60 pArg = Tcl_NewStringObj((const char*)sqlite3_value_text(apArg[i]), -1);
61 Tcl_ListObjAppendElement(p->interp, pEval, pArg);
63 rc = Tcl_EvalObjEx(p->interp, pEval, TCL_EVAL_GLOBAL);
64 if( rc!=TCL_OK ){
65 zResult = Tcl_GetStringResult(p->interp);
66 sqlite3_result_error(ctx, zResult, -1);
67 }else{
68 if( pCtx->pVal ) Tcl_DecrRefCount(pCtx->pVal);
69 pCtx->pVal = Tcl_DuplicateObj(Tcl_GetObjResult(p->interp));
70 Tcl_IncrRefCount(pCtx->pVal);
73 Tcl_DecrRefCount(pEval);
76 static void doTestWindowFinalize(int bValue, sqlite3_context *ctx){
77 TestWindow *p = (TestWindow*)sqlite3_user_data(ctx);
78 Tcl_Obj *pEval = Tcl_DuplicateObj(bValue ? p->xValue : p->xFinal);
79 TestWindowCtx *pCtx = sqlite3_aggregate_context(ctx, sizeof(TestWindowCtx));
81 Tcl_IncrRefCount(pEval);
82 if( pCtx ){
83 const char *zResult;
84 int rc;
85 if( pCtx->pVal ){
86 Tcl_ListObjAppendElement(p->interp, pEval, Tcl_DuplicateObj(pCtx->pVal));
87 }else{
88 Tcl_ListObjAppendElement(p->interp, pEval, Tcl_NewStringObj("", -1));
91 rc = Tcl_EvalObjEx(p->interp, pEval, TCL_EVAL_GLOBAL);
92 zResult = Tcl_GetStringResult(p->interp);
93 if( rc!=TCL_OK ){
94 sqlite3_result_error(ctx, zResult, -1);
95 }else{
96 sqlite3_result_text(ctx, zResult, -1, SQLITE_TRANSIENT);
99 if( bValue==0 ){
100 if( pCtx->pVal ) Tcl_DecrRefCount(pCtx->pVal);
101 pCtx->pVal = 0;
104 Tcl_DecrRefCount(pEval);
107 static void testWindowStep(
108 sqlite3_context *ctx,
109 int nArg,
110 sqlite3_value **apArg
112 doTestWindowStep(0, ctx, nArg, apArg);
114 static void testWindowInverse(
115 sqlite3_context *ctx,
116 int nArg,
117 sqlite3_value **apArg
119 doTestWindowStep(1, ctx, nArg, apArg);
122 static void testWindowFinal(sqlite3_context *ctx){
123 doTestWindowFinalize(0, ctx);
125 static void testWindowValue(sqlite3_context *ctx){
126 doTestWindowFinalize(1, ctx);
129 static void testWindowDestroy(void *pCtx){
130 ckfree(pCtx);
134 ** Usage: sqlite3_create_window_function DB NAME XSTEP XFINAL XVALUE XINVERSE
136 static int SQLITE_TCLAPI test_create_window(
137 void * clientData,
138 Tcl_Interp *interp,
139 int objc,
140 Tcl_Obj *CONST objv[]
142 TestWindow *pNew;
143 sqlite3 *db;
144 const char *zName;
145 int rc;
147 if( objc!=7 ){
148 Tcl_WrongNumArgs(interp, 1, objv, "DB NAME XSTEP XFINAL XVALUE XINVERSE");
149 return TCL_ERROR;
152 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
153 zName = Tcl_GetString(objv[2]);
154 pNew = ckalloc(sizeof(TestWindow));
155 memset(pNew, 0, sizeof(TestWindow));
156 pNew->xStep = Tcl_DuplicateObj(objv[3]);
157 pNew->xFinal = Tcl_DuplicateObj(objv[4]);
158 pNew->xValue = Tcl_DuplicateObj(objv[5]);
159 pNew->xInverse = Tcl_DuplicateObj(objv[6]);
160 pNew->interp = interp;
162 Tcl_IncrRefCount(pNew->xStep);
163 Tcl_IncrRefCount(pNew->xFinal);
164 Tcl_IncrRefCount(pNew->xValue);
165 Tcl_IncrRefCount(pNew->xInverse);
167 rc = sqlite3_create_window_function(db, zName, -1, SQLITE_UTF8, (void*)pNew,
168 testWindowStep, testWindowFinal, testWindowValue, testWindowInverse,
169 testWindowDestroy
171 if( rc!=SQLITE_OK ){
172 Tcl_SetObjResult(interp, Tcl_NewStringObj(sqlite3ErrName(rc), -1));
173 return TCL_ERROR;
176 return TCL_OK;
179 int Sqlitetest_window_Init(Tcl_Interp *interp){
180 static struct {
181 char *zName;
182 Tcl_ObjCmdProc *xProc;
183 int clientData;
184 } aObjCmd[] = {
185 { "sqlite3_create_window_function", test_create_window, 0 },
187 int i;
188 for(i=0; i<sizeof(aObjCmd)/sizeof(aObjCmd[0]); i++){
189 ClientData c = (ClientData)SQLITE_INT_TO_PTR(aObjCmd[i].clientData);
190 Tcl_CreateObjCommand(interp, aObjCmd[i].zName, aObjCmd[i].xProc, c, 0);
192 return TCL_OK;
194 #endif