~ubuntu-branches/ubuntu/lucid/python-apsw/lucid

« back to all changes in this revision

Viewing changes to apsw.c

  • Committer: Bazaar Package Importer
  • Author(s): Joel Rosdahl
  • Date: 2007-04-12 07:07:54 UTC
  • mfrom: (1.1.4 upstream)
  • Revision ID: james.westby@ubuntu.com-20070412070754-jc0s017rpwu94aj9
Tags: 3.3.13-r1-1
* New upstream release.
* Updated build dependencies on debhelper, python-all-dev,
  python-central and libsqlite3-dev.
* Removed use of dh_python.
* Added debian/pycompat.
* Removed XS-Python-Standards-Version field.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
  Another Python Sqlite Wrapper
3
 
 
4
 
  This wrapper aims to be the minimum necessary layer over SQLite 3
5
 
  itself.
6
 
 
7
 
  It assumes we are running as 32 bit int with a 64 bit long long type
8
 
  available.
9
 
 
10
 
  Copyright (C) 2004-2006 Roger Binns <rogerb@rogerbinns.com>
11
 
 
12
 
  This software is provided 'as-is', without any express or implied
13
 
  warranty.  In no event will the authors be held liable for any
14
 
  damages arising from the use of this software.
15
 
 
16
 
  Permission is granted to anyone to use this software for any
17
 
  purpose, including commercial applications, and to alter it and
18
 
  redistribute it freely, subject to the following restrictions:
19
 
 
20
 
  1. The origin of this software must not be misrepresented; you must
21
 
     not claim that you wrote the original software. If you use this
22
 
     software in a product, an acknowledgment in the product
23
 
     documentation would be appreciated but is not required.
24
 
 
25
 
  2. Altered source versions must be plainly marked as such, and must
26
 
     not be misrepresented as being the original software.
27
 
 
28
 
  3. This notice may not be removed or altered from any source
29
 
     distribution.
30
 
 
31
 
*/
32
 
 
33
 
/* Get the version number */
34
 
#include "apswversion.h"
35
 
 
36
 
/* Python headers */
37
 
#include <Python.h>
38
 
#include <pythread.h>
39
 
#include "structmember.h"
40
 
 
41
 
/* Python 2.5 compatibility when size_t types become 64 bit.
42
 
   SQLite3 is limited to 32 bit sizes even on a 64 bit machine. */
43
 
#if PY_VERSION_HEX < 0x02050000
44
 
typedef int Py_ssize_t;
45
 
#endif
46
 
 
47
 
/* SQLite 3 headers */
48
 
#include "sqlite3.h"
49
 
 
50
 
/* system headers */
51
 
#include <assert.h>
52
 
 
53
 
/* used to decide if we will use int or long long */
54
 
#define INT32_MIN (-2147483647 - 1)
55
 
#define INT32_MAX 2147483647
56
 
 
57
 
/* The encoding we use with SQLite.  SQLite supports either utf8 or 16
58
 
   bit unicode (host byte order).  If the latter is used then all
59
 
   functions have "16" appended to their name.  The encoding used also
60
 
   affects how strings are stored in the database.  We use utf8 since
61
 
   it is more space efficient, and Python can't make its mind up about
62
 
   Unicode (it uses 16 or 32 bit unichars and often likes to use Byte
63
 
   Order Markers). */
64
 
#define STRENCODING "utf_8"
65
 
 
66
 
/* Some macros used for frequent operations */
67
 
 
68
 
#define CHECK_THREAD(x,e)                                                \
69
 
  { if(x->thread_ident!=PyThread_get_thread_ident())                                                                                 \
70
 
      {    /* raise exception if we aren't already in one */                                                                         \
71
 
           if (!PyErr_Occurred())                                                                                                    \
72
 
             PyErr_Format(ExcThreadingViolation, "All SQLite objects created in a thread can only be used in that same thread.  "    \
73
 
                         "The object was created in thread id %d and this is %d",                                                    \
74
 
                         (int)(x->thread_ident), (int)(PyThread_get_thread_ident()));                                                \
75
 
           return e;                                                                                                                 \
76
 
      }                                                                                                                              \
77
 
  }
78
 
 
79
 
 
80
 
 
81
 
/* EXCEPTION TYPES */
82
 
 
83
 
static PyObject *APSWException;  /* root exception class */
84
 
static PyObject *ExcThreadingViolation; /* thread misuse */
85
 
static PyObject *ExcIncomplete;  /* didn't finish previous query */
86
 
static PyObject *ExcBindings;  /* wrong number of bindings */
87
 
static PyObject *ExcComplete;  /* query is finished */
88
 
static PyObject *ExcTraceAbort; /* aborted by exectrace */
89
 
static PyObject *ExcTooBig; /* object is too large for SQLite */
90
 
 
91
 
static struct { int code; const char *name; PyObject *cls;}
92
 
exc_descriptors[]=
93
 
  {
94
 
    /* Generic Errors */
95
 
    {SQLITE_ERROR,    "SQL"},       
96
 
    {SQLITE_MISMATCH, "Mismatch"},
97
 
 
98
 
    /* Internal Errors */
99
 
    {SQLITE_INTERNAL, "Internal"},  /* NOT USED */
100
 
    {SQLITE_PROTOCOL, "Protocol"},
101
 
    {SQLITE_MISUSE,   "Misuse"},
102
 
    {SQLITE_RANGE,    "Range"},
103
 
 
104
 
    /* permissions etc */
105
 
    {SQLITE_PERM,     "Permissions"},
106
 
    {SQLITE_READONLY, "ReadOnly"},
107
 
    {SQLITE_CANTOPEN, "CantOpen"},
108
 
    {SQLITE_AUTH,     "Auth"},
109
 
 
110
 
    /* abort/busy/etc */
111
 
    {SQLITE_ABORT,    "Abort"},
112
 
    {SQLITE_BUSY,     "Busy"},
113
 
    {SQLITE_LOCKED,   "Locked"},
114
 
    {SQLITE_INTERRUPT,"Interrupt"},
115
 
    {SQLITE_SCHEMA,   "SchemaChange"}, 
116
 
    {SQLITE_CONSTRAINT, "Constraint"},
117
 
 
118
 
    /* memory/disk/corrupt etc */
119
 
    {SQLITE_NOMEM,    "NoMem"},
120
 
    {SQLITE_IOERR,    "IO"},
121
 
    {SQLITE_CORRUPT,  "Corrupt"},
122
 
    {SQLITE_FULL,     "Full"},
123
 
    {SQLITE_TOOBIG,   "TooBig"},     /* NOT USED */
124
 
    {SQLITE_NOLFS,    "NoLFS"},
125
 
    {SQLITE_EMPTY,    "Empty"},
126
 
    {SQLITE_FORMAT,   "Format"},
127
 
    {SQLITE_NOTADB,   "NotADB"},
128
 
 
129
 
    {-1, 0, 0}
130
 
  };
131
 
 
132
 
 
133
 
/* EXCEPTION CODE */
134
 
 
135
 
static int init_exceptions(PyObject *m)
136
 
{
137
 
  char buffy[100]; /* more than enough for anyone :-) */
138
 
  int i;
139
 
  PyObject *obj;
140
 
 
141
 
  /* PyModule_AddObject uses borrowed reference so we incref whatever
142
 
     we give to it, so we still have a copy to use */
143
 
 
144
 
  /* custom ones first */
145
 
 
146
 
  APSWException=PyErr_NewException("apsw.Error", NULL, NULL);
147
 
  if(!APSWException) return -1;
148
 
  Py_INCREF(APSWException);
149
 
  if(PyModule_AddObject(m, "Error", (PyObject *)APSWException))
150
 
    return -1;
151
 
 
152
 
#define EXC(varname,name) \
153
 
  varname=PyErr_NewException("apsw." name, APSWException, NULL);  \
154
 
  if(!varname) return -1;                                          \
155
 
  Py_INCREF(varname);                                              \
156
 
  if(PyModule_AddObject(m, name, (PyObject *)varname))            \
157
 
    return -1;
158
 
 
159
 
  EXC(ExcThreadingViolation, "ThreadingViolationError");
160
 
  EXC(ExcIncomplete, "IncompleteExecutionError");
161
 
  EXC(ExcBindings, "BindingsError");
162
 
  EXC(ExcComplete, "ExecutionCompleteError");
163
 
  EXC(ExcTraceAbort, "ExecTraceAbort");
164
 
  EXC(ExcTooBig, "TooBigError");
165
 
 
166
 
#undef EXC
167
 
 
168
 
  /* all the ones corresponding to SQLITE error codes */
169
 
  for(i=0;exc_descriptors[i].name;i++)
170
 
    {
171
 
      sprintf(buffy, "apsw.%sError", exc_descriptors[i].name);
172
 
      obj=PyErr_NewException(buffy, APSWException, NULL);
173
 
      if(!obj) return -1;
174
 
      Py_INCREF(obj);
175
 
      exc_descriptors[i].cls=obj;
176
 
      sprintf(buffy, "%sError", exc_descriptors[i].name);
177
 
      if(PyModule_AddObject(m, buffy, obj))
178
 
        return -1;
179
 
    }
180
 
  
181
 
  return 0;
182
 
}
183
 
 
184
 
static void make_exception(int res, sqlite3 *db)
185
 
{
186
 
  int i;
187
 
  
188
 
  for(i=0;exc_descriptors[i].name;i++)
189
 
    if (exc_descriptors[i].code==res)
190
 
      {
191
 
        assert(exc_descriptors[i].cls);
192
 
        PyErr_Format(exc_descriptors[i].cls, "%sError: %s", exc_descriptors[i].name, db?(sqlite3_errmsg(db)):"error");
193
 
        assert(PyErr_Occurred());
194
 
        return;
195
 
      }
196
 
 
197
 
  PyErr_Format(APSWException, "Error %d: %s", res, db?(sqlite3_errmsg(db)):"error");  
198
 
}
199
 
 
200
 
/* If res indicates an SQLite error then do all the exception creation
201
 
 work.  We don't overwrite earlier exceptions hence the PyErr_Occurred
202
 
 check */
203
 
#define SET_EXC(db,res)  { if(res != SQLITE_OK && !PyErr_Occurred()) make_exception(res,db); }
204
 
 
205
 
/* CALLBACK INFO */
206
 
 
207
 
/* details of a registered function passed as user data to sqlite3_create_function */
208
 
typedef struct _funccbinfo 
209
 
{
210
 
  struct _funccbinfo *next;       /* we use a linked list */
211
 
  char *name;                     /* ascii function name which we uppercased */
212
 
  PyObject *scalarfunc;           /* the function to call for stepping */
213
 
  PyObject *aggregatefactory;     /* factory for aggregate functions */
214
 
} funccbinfo;
215
 
 
216
 
/* a particular aggregate function instance used as sqlite3_aggregate_context */
217
 
typedef struct _aggregatefunctioncontext 
218
 
{
219
 
  PyObject *aggvalue;             /* the aggregation value passed as first parameter */
220
 
  PyObject *stepfunc;             /* step function */
221
 
  PyObject *finalfunc;            /* final function */
222
 
} aggregatefunctioncontext;
223
 
 
224
 
static funccbinfo *freefunccbinfo(funccbinfo *);
225
 
 
226
 
typedef struct _collationcbinfo
227
 
{
228
 
  struct _collationcbinfo *next;  /* we use a linked list */
229
 
  char *name;                     /* ascii collation name which we uppercased */
230
 
  PyObject *func;                 /* the actual function to call */
231
 
} collationcbinfo;
232
 
  
233
 
static collationcbinfo *freecollationcbinfo(collationcbinfo *);
234
 
 
235
 
 
236
 
/* CONNECTION TYPE */
237
 
 
238
 
typedef struct { 
239
 
  PyObject_HEAD
240
 
  sqlite3 *db;                    /* the actual database connection */
241
 
  long thread_ident;              /* which thread we were made in */
242
 
  funccbinfo *functions;          /* linked list of registered functions */
243
 
  collationcbinfo *collations;    /* linked list of registered collations */
244
 
 
245
 
  /* registered hooks/handlers (NULL or callable) */
246
 
  PyObject *busyhandler;     
247
 
  PyObject *rollbackhook;
248
 
  PyObject *profile;
249
 
  PyObject *updatehook;
250
 
  PyObject *commithook;           
251
 
  PyObject *progresshandler;      
252
 
  PyObject *authorizer;
253
 
} Connection;
254
 
 
255
 
static PyTypeObject ConnectionType;
256
 
 
257
 
/* CURSOR TYPE */
258
 
 
259
 
typedef struct {
260
 
  PyObject_HEAD
261
 
  Connection *connection;          /* pointer to parent connection */
262
 
  sqlite3_stmt *statement;         /* current compiled statement */
263
 
 
264
 
  /* see sqlite3_prepare for the origin of these */
265
 
  const char *zsql;               /* current sqlstatement (which may include multiple statements) */
266
 
  const char *zsqlnextpos;        /* the next statement to execute (or NULL if no more) */
267
 
 
268
 
  /* what state we are in */
269
 
  enum { C_BEGIN, C_ROW, C_DONE } status;
270
 
 
271
 
  /* bindings for query */
272
 
  PyObject *bindings;             /* dict or sequence */
273
 
  Py_ssize_t bindingsoffset;             /* for sequence tracks how far along we are when dealing with multiple statements */
274
 
 
275
 
  /* iterator for executemany */
276
 
  PyObject *emiter;
277
 
 
278
 
  /* tracing functions */
279
 
  PyObject *exectrace;
280
 
  PyObject *rowtrace;
281
 
  
282
 
} Cursor;
283
 
 
284
 
static PyTypeObject CursorType;
285
 
 
286
 
 
287
 
/* CONVENIENCE FUNCTIONS */
288
 
 
289
 
/* Convert a NULL terminated UTF-8 string into a Python object.  None
290
 
   is returned if NULL is passed in. */
291
 
static PyObject *
292
 
convertutf8string(const char *str)
293
 
{
294
 
  const char *chk=str;
295
 
 
296
 
  if(!str)
297
 
    {
298
 
      Py_INCREF(Py_None);
299
 
      return Py_None;
300
 
    }
301
 
  
302
 
  for(chk=str;*chk && !((*chk)&0x80); chk++) ;
303
 
  if(*chk)
304
 
    return PyUnicode_DecodeUTF8(str, strlen(str), NULL);
305
 
  else
306
 
    return PyString_FromString(str);
307
 
}
308
 
 
309
 
/* Convert a pointer and size UTF-8 string into a Python object.
310
 
   Pointer must be non-NULL. */
311
 
static PyObject *
312
 
convertutf8stringsize(const char *str, Py_ssize_t size)
313
 
{
314
 
  const char *chk=str;
315
 
  Py_ssize_t i;
316
 
  
317
 
  assert(str);
318
 
  assert(size>=0);
319
 
 
320
 
  for(i=0;i<size && !(chk[i]&0x80);i++);
321
 
 
322
 
  if(i!=size)
323
 
    return PyUnicode_DecodeUTF8(str, size, NULL);
324
 
  else
325
 
    return PyString_FromStringAndSize(str, size);
326
 
}
327
 
 
328
 
/* 
329
 
   Python's handling of Unicode is horrible.  It can use 2 or 4 byte
330
 
   unicode chars and the conversion routines like to put out BOMs
331
 
   which makes life even harder.  These macros are used in pairs to do
332
 
   the right form of conversion and tell us whether to use the plain
333
 
   or -16 version of the SQLite function that is about to be called.
334
 
*/
335
 
 
336
 
#if Py_UNICODE_SIZE==2
337
 
#define UNIDATABEGIN(obj) \
338
 
{                                                        \
339
 
  const int use16=1;                                     \
340
 
  size_t strbytes=2*PyUnicode_GET_SIZE(obj);             \
341
 
  const void *strdata=PyUnicode_AS_DATA(obj);            
342
 
 
343
 
#define UNIDATAEND(obj)                                  \
344
 
}
345
 
 
346
 
#else  /* Py_UNICODE_SIZE!=2 */
347
 
 
348
 
#define UNIDATABEGIN(obj) \
349
 
{                                                        \
350
 
  const int use16=0;                                     \
351
 
  Py_ssize_t strbytes=0;                                 \
352
 
  const char *strdata=NULL;                              \
353
 
  PyObject *_utf8=NULL;                                  \
354
 
                                                         \
355
 
  _utf8=PyUnicode_AsUTF8String(obj);                     \
356
 
  if(_utf8)                                              \
357
 
    {                                                    \
358
 
      strbytes=PyString_GET_SIZE(_utf8);                 \
359
 
      strdata=PyString_AsString(_utf8);                  \
360
 
    }                      
361
 
 
362
 
#define UNIDATAEND(obj)                                  \
363
 
  Py_XDECREF(_utf8);                                     \
364
 
}
365
 
 
366
 
#endif /* Py_UNICODE_SIZE */
367
 
 
368
 
/* CONNECTION CODE */
369
 
 
370
 
static void
371
 
Connection_dealloc(Connection* self)
372
 
{
373
 
  /* thread check - we can't use macro as that returns */
374
 
 
375
 
  if(self->thread_ident!=PyThread_get_thread_ident())
376
 
    {
377
 
          PyObject *err_type, *err_value, *err_traceback;
378
 
          int have_error=PyErr_Occurred()?1:0;
379
 
          if (have_error)
380
 
            PyErr_Fetch(&err_type, &err_value, &err_traceback);
381
 
          PyErr_Format(ExcThreadingViolation, "The destructor for Connection is called in a different thread than it"
382
 
                       "was created in.  All calls must be in the same thread.  It was created in thread %d" 
383
 
                       "and this is %d.  This SQLite database is not being closed as a result.",
384
 
                       (int)(self->thread_ident), (int)(PyThread_get_thread_ident()));            
385
 
          PyErr_WriteUnraisable((PyObject*)self);
386
 
          if (have_error)
387
 
            PyErr_Restore(err_type, err_value, err_traceback);
388
 
          
389
 
          return;
390
 
    }
391
 
 
392
 
  if (self->db)
393
 
    {
394
 
      int res;
395
 
      Py_BEGIN_ALLOW_THREADS
396
 
        res=sqlite3_close(self->db);
397
 
      Py_END_ALLOW_THREADS;
398
 
 
399
 
      if (res!=SQLITE_OK) 
400
 
        {
401
 
          PyObject *err_type, *err_value, *err_traceback;
402
 
          int have_error=PyErr_Occurred()?1:0;
403
 
          if (have_error)
404
 
            PyErr_Fetch(&err_type, &err_value, &err_traceback);
405
 
          make_exception(res,self->db);
406
 
          if (have_error)
407
 
            {
408
 
              PyErr_WriteUnraisable((PyObject*)self);
409
 
              PyErr_Restore(err_type, err_value, err_traceback);
410
 
            }
411
 
        }
412
 
      else
413
 
        self->db=0;
414
 
    }
415
 
 
416
 
  /* free functions */
417
 
  {
418
 
    funccbinfo *func=self->functions;
419
 
    while((func=freefunccbinfo(func)));
420
 
  }
421
 
 
422
 
  /* free collations */
423
 
  {
424
 
    collationcbinfo *coll=self->collations;
425
 
    while((coll=freecollationcbinfo(coll)));
426
 
  }
427
 
 
428
 
  Py_XDECREF(self->busyhandler);
429
 
  self->busyhandler=0;
430
 
 
431
 
  Py_XDECREF(self->rollbackhook);
432
 
  self->rollbackhook=0;
433
 
 
434
 
  Py_XDECREF(self->profile);
435
 
  self->profile=0;
436
 
 
437
 
  Py_XDECREF(self->commithook);
438
 
  self->commithook=0;
439
 
 
440
 
  Py_XDECREF(self->progresshandler);
441
 
  self->progresshandler=0;
442
 
  
443
 
  Py_XDECREF(self->authorizer);
444
 
  self->authorizer=0;
445
 
 
446
 
  self->thread_ident=-1;
447
 
  self->ob_type->tp_free((PyObject*)self);
448
 
}
449
 
 
450
 
static PyObject*
451
 
Connection_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
452
 
{
453
 
    Connection *self;
454
 
 
455
 
    self = (Connection *)type->tp_alloc(type, 0);
456
 
    if (self != NULL) {
457
 
      /* Strictly speaking the memory was already zeroed.  This is
458
 
         just defensive coding. */
459
 
      self->db=0;
460
 
      self->thread_ident=PyThread_get_thread_ident();
461
 
      self->functions=0;
462
 
      self->collations=0;
463
 
      self->busyhandler=0;
464
 
      self->rollbackhook=0;
465
 
      self->profile=0;
466
 
      self->updatehook=0;
467
 
      self->commithook=0;
468
 
      self->progresshandler=0;
469
 
      self->authorizer=0;
470
 
    }
471
 
 
472
 
    return (PyObject *)self;
473
 
}
474
 
 
475
 
static int
476
 
Connection_init(Connection *self, PyObject *args, PyObject *kwds)
477
 
{
478
 
  char *filename=NULL;
479
 
  int res=0;
480
 
 
481
 
  CHECK_THREAD(self,-1);
482
 
 
483
 
  if(kwds && kwds!=Py_None)
484
 
    {
485
 
      if(!PyDict_Check(kwds))
486
 
        {
487
 
          PyErr_Format(PyExc_TypeError, "Bad type for keyword args");
488
 
          return -1;
489
 
        }
490
 
      if(PyDict_Size(kwds))
491
 
        {
492
 
          PyErr_Format(PyExc_TypeError, "Connection constructor does not take keyword arguments");
493
 
          return -1;
494
 
        }
495
 
    }
496
 
 
497
 
  if(!PyArg_ParseTuple(args, "es:Connection(filename)", STRENCODING, &filename))
498
 
    return -1;
499
 
  
500
 
  Py_BEGIN_ALLOW_THREADS
501
 
    res=sqlite3_open(filename, &self->db);
502
 
  Py_END_ALLOW_THREADS;
503
 
  SET_EXC(self->db, res);  /* nb sqlite3_open always allocates the db even on error */
504
 
 
505
 
  PyMem_Free(filename);
506
 
  
507
 
  return (res==SQLITE_OK)?0:-1;
508
 
}
509
 
 
510
 
static PyObject *
511
 
Connection_cursor(Connection *self)
512
 
{
513
 
  Cursor* cursor = NULL;
514
 
 
515
 
  CHECK_THREAD(self,NULL);
516
 
 
517
 
  /* there is absolutely no documentation on how to allocate a new
518
 
     object in the Python C API.  Actually there is documentation of 5
519
 
     different methods, all of which take various parameters we don't
520
 
     have.  This appears to be yet another (and undocumented) way of
521
 
     doing it. */
522
 
  cursor = (Cursor*) (CursorType.tp_alloc(&CursorType, 0));
523
 
  if(!cursor)
524
 
    return NULL;
525
 
 
526
 
  /* incref me since cursor holds a pointer */
527
 
  Py_INCREF((PyObject*)self);
528
 
  cursor->connection=self;
529
 
  
530
 
  return (PyObject*)cursor;
531
 
}
532
 
 
533
 
 
534
 
static PyObject *
535
 
Connection_setbusytimeout(Connection *self, PyObject *args)
536
 
{
537
 
  int ms=0;
538
 
  int res;
539
 
 
540
 
  CHECK_THREAD(self,NULL);
541
 
 
542
 
  if(!PyArg_ParseTuple(args, "i:setbusytimeout(millseconds)", &ms))
543
 
    return NULL;
544
 
 
545
 
  res=sqlite3_busy_timeout(self->db, ms);
546
 
  SET_EXC(self->db, res);
547
 
  if(res!=SQLITE_OK)
548
 
    return NULL;
549
 
  
550
 
  /* free any explicit busyhandler we may have had */
551
 
  Py_XDECREF(self->busyhandler);
552
 
  self->busyhandler=0;
553
 
 
554
 
  return Py_BuildValue("");
555
 
}
556
 
 
557
 
static PyObject *
558
 
Connection_changes(Connection *self)
559
 
{
560
 
  CHECK_THREAD(self,NULL);
561
 
  return Py_BuildValue("i", sqlite3_changes(self->db));
562
 
}
563
 
 
564
 
static PyObject *
565
 
Connection_totalchanges(Connection *self)
566
 
{
567
 
  CHECK_THREAD(self,NULL);
568
 
  return Py_BuildValue("i", sqlite3_total_changes(self->db));
569
 
}
570
 
 
571
 
static PyObject *
572
 
Connection_getautocommit(Connection *self)
573
 
{
574
 
  PyObject *res;
575
 
  CHECK_THREAD(self,NULL);
576
 
  res=(sqlite3_get_autocommit(self->db))?(Py_True):(Py_False);
577
 
  Py_INCREF(res);
578
 
  return res;
579
 
}
580
 
 
581
 
static PyObject *
582
 
Connection_last_insert_rowid(Connection *self)
583
 
{
584
 
  long long int vint;
585
 
 
586
 
  CHECK_THREAD(self,NULL);
587
 
 
588
 
  vint=sqlite3_last_insert_rowid(self->db);
589
 
  
590
 
  if(vint<INT32_MIN || vint>INT32_MAX)
591
 
    return PyLong_FromLongLong(vint);
592
 
  else
593
 
    return PyInt_FromLong((long)vint);
594
 
}
595
 
 
596
 
static PyObject *
597
 
Connection_complete(Connection *self, PyObject *args)
598
 
{
599
 
  char *statements=NULL;
600
 
  int res;
601
 
 
602
 
  CHECK_THREAD(self,NULL);
603
 
  
604
 
  if(!PyArg_ParseTuple(args, "es:complete(statement)", STRENCODING, &statements))
605
 
    return NULL;
606
 
 
607
 
  res=sqlite3_complete(statements);
608
 
 
609
 
  PyMem_Free(statements);
610
 
 
611
 
  if(res)
612
 
    {
613
 
      Py_INCREF(Py_True);
614
 
      return Py_True;
615
 
    }
616
 
  Py_INCREF(Py_False);
617
 
  return Py_False;
618
 
}
619
 
 
620
 
static PyObject *
621
 
Connection_interrupt(Connection *self)
622
 
{
623
 
  CHECK_THREAD(self, NULL);
624
 
 
625
 
  sqlite3_interrupt(self->db);  /* no return value */
626
 
  return Py_BuildValue("");
627
 
}
628
 
 
629
 
void
630
 
updatecb(void *context, int updatetype, char const *databasename, char const *tablename, sqlite_int64 rowid)
631
 
{
632
 
  /* The hook returns void. That makes it impossible for us to
633
 
     abort immediately due to an error in the callback */
634
 
  
635
 
  PyGILState_STATE gilstate;
636
 
  PyObject *retval=NULL, *args=NULL;
637
 
  Connection *self=(Connection *)context;
638
 
  PyObject *pupdatetype=NULL, *pdatabasename=NULL, *ptablename=NULL, *prowid=NULL;
639
 
 
640
 
  assert(self);
641
 
  assert(self->updatehook);
642
 
  assert(self->updatehook!=Py_None);
643
 
 
644
 
  /* defensive coding */
645
 
  if(!self->updatehook)
646
 
    return;
647
 
 
648
 
  gilstate=PyGILState_Ensure();
649
 
 
650
 
  if(PyErr_Occurred())
651
 
    goto finally;  /* abort hook due to outstanding exception */
652
 
 
653
 
 
654
 
  pupdatetype=Py_BuildValue("i", updatetype);
655
 
  pdatabasename=convertutf8string(databasename);
656
 
  ptablename=convertutf8string(tablename);
657
 
  prowid=PyLong_FromLongLong(rowid);
658
 
 
659
 
  if (!pupdatetype || !pdatabasename || !ptablename || !prowid)
660
 
    goto finally;
661
 
 
662
 
  args=PyTuple_New(4);
663
 
  if(!args)
664
 
    goto finally; /* abort hook on failure to allocate args */
665
 
 
666
 
  PyTuple_SET_ITEM(args, 0, pupdatetype);
667
 
  PyTuple_SET_ITEM(args, 1, pdatabasename);
668
 
  PyTuple_SET_ITEM(args, 2, ptablename);
669
 
  PyTuple_SET_ITEM(args, 3, prowid);
670
 
 
671
 
  pupdatetype=pdatabasename=ptablename=prowid=NULL; /* owned by args now */
672
 
  
673
 
  retval=PyEval_CallObject(self->updatehook, args);
674
 
 
675
 
 finally:
676
 
  Py_XDECREF(retval);
677
 
  Py_XDECREF(args);
678
 
  Py_XDECREF(pupdatetype);
679
 
  Py_XDECREF(pdatabasename);
680
 
  Py_XDECREF(ptablename);
681
 
  Py_XDECREF(prowid);
682
 
  PyGILState_Release(gilstate);
683
 
}
684
 
 
685
 
static PyObject *
686
 
Connection_setupdatehook(Connection *self, PyObject *callable)
687
 
{
688
 
  /* sqlite3_update_hook doesn't return an error code */
689
 
  
690
 
  CHECK_THREAD(self,NULL);
691
 
 
692
 
  if(callable==Py_None)
693
 
    {
694
 
      sqlite3_update_hook(self->db, NULL, NULL);
695
 
      callable=NULL;
696
 
      goto finally;
697
 
    }
698
 
 
699
 
  if(!PyCallable_Check(callable))
700
 
    {
701
 
      PyErr_Format(PyExc_TypeError, "update hook must be callable");
702
 
      return NULL;
703
 
    }
704
 
 
705
 
  sqlite3_update_hook(self->db, updatecb, self);
706
 
 
707
 
  Py_INCREF(callable);
708
 
 
709
 
 finally:
710
 
 
711
 
  Py_XDECREF(self->updatehook);
712
 
  self->updatehook=callable;
713
 
 
714
 
  return Py_BuildValue("");
715
 
}
716
 
 
717
 
void
718
 
rollbackhookcb(void *context)
719
 
{
720
 
  /* The hook returns void. That makes it impossible for us to
721
 
     abort immediately due to an error in the callback */
722
 
  
723
 
  PyGILState_STATE gilstate;
724
 
  PyObject *retval=NULL, *args=NULL;
725
 
  Connection *self=(Connection *)context;
726
 
 
727
 
  assert(self);
728
 
  assert(self->rollbackhook);
729
 
  assert(self->rollbackhook!=Py_None);
730
 
 
731
 
  /* defensive coding */
732
 
  if(!self->rollbackhook)
733
 
    return;
734
 
 
735
 
  gilstate=PyGILState_Ensure();
736
 
 
737
 
  if(PyErr_Occurred())
738
 
    goto finally;  /* abort hook due to outstanding exception */
739
 
 
740
 
  args=PyTuple_New(0);
741
 
  if(!args)
742
 
    goto finally; /* abort hook on failure to allocate args */
743
 
  
744
 
  retval=PyEval_CallObject(self->rollbackhook, args);
745
 
 
746
 
 finally:
747
 
  Py_XDECREF(retval);
748
 
  Py_XDECREF(args);
749
 
  PyGILState_Release(gilstate);
750
 
}
751
 
 
752
 
static PyObject *
753
 
Connection_setrollbackhook(Connection *self, PyObject *callable)
754
 
{
755
 
  /* sqlite3_rollback_hook doesn't return an error code */
756
 
  
757
 
  CHECK_THREAD(self,NULL);
758
 
 
759
 
  if(callable==Py_None)
760
 
    {
761
 
      sqlite3_rollback_hook(self->db, NULL, NULL);
762
 
      callable=NULL;
763
 
      goto finally;
764
 
    }
765
 
 
766
 
  if(!PyCallable_Check(callable))
767
 
    {
768
 
      PyErr_Format(PyExc_TypeError, "rollback hook must be callable");
769
 
      return NULL;
770
 
    }
771
 
 
772
 
  sqlite3_rollback_hook(self->db, rollbackhookcb, self);
773
 
 
774
 
  Py_INCREF(callable);
775
 
 
776
 
 finally:
777
 
 
778
 
  Py_XDECREF(self->rollbackhook);
779
 
  self->rollbackhook=callable;
780
 
 
781
 
  return Py_BuildValue("");
782
 
}
783
 
 
784
 
#ifdef EXPERIMENTAL /* sqlite3_profile */
785
 
void
786
 
profilecb(void *context, const char *statement, sqlite_uint64 runtime)
787
 
{
788
 
  /* The hook returns void. That makes it impossible for us to
789
 
     abort immediately due to an error in the callback */
790
 
  
791
 
  PyGILState_STATE gilstate;
792
 
  PyObject *retval=NULL, *args=NULL;
793
 
  Connection *self=(Connection *)context;
794
 
  PyObject *pstatement=NULL, *pruntime=NULL;
795
 
 
796
 
  assert(self);
797
 
  assert(self->profile);
798
 
  assert(self->profile!=Py_None);
799
 
 
800
 
  /* defensive coding */
801
 
  if(!self->profile)
802
 
    return;
803
 
 
804
 
  gilstate=PyGILState_Ensure();
805
 
 
806
 
  if(PyErr_Occurred())
807
 
    goto finally;  /* abort hook due to outstanding exception */
808
 
 
809
 
  pstatement=convertutf8string(statement);
810
 
  pruntime=PyLong_FromUnsignedLongLong(runtime);
811
 
 
812
 
  if (!pstatement || !pruntime)
813
 
    goto finally;
814
 
 
815
 
  args=PyTuple_New(2);
816
 
  if(!args)
817
 
    goto finally; /* abort hook on failure to allocate args */
818
 
  
819
 
  PyTuple_SET_ITEM(args, 0, pstatement);
820
 
  PyTuple_SET_ITEM(args, 1, pruntime);
821
 
 
822
 
  pstatement=pruntime=NULL; /* owned by args now */
823
 
 
824
 
  retval=PyEval_CallObject(self->profile, args);
825
 
 
826
 
 finally:
827
 
  Py_XDECREF(retval);
828
 
  Py_XDECREF(args);
829
 
  Py_XDECREF(pstatement);
830
 
  Py_XDECREF(pruntime);
831
 
  PyGILState_Release(gilstate);
832
 
}
833
 
 
834
 
static PyObject *
835
 
Connection_setprofile(Connection *self, PyObject *callable)
836
 
{
837
 
  /* sqlite3_profile doesn't return an error code */
838
 
  
839
 
  CHECK_THREAD(self,NULL);
840
 
 
841
 
  if(callable==Py_None)
842
 
    {
843
 
      sqlite3_profile(self->db, NULL, NULL);
844
 
      callable=NULL;
845
 
      goto finally;
846
 
    }
847
 
 
848
 
  if(!PyCallable_Check(callable))
849
 
    {
850
 
      PyErr_Format(PyExc_TypeError, "profile function must be callable");
851
 
      return NULL;
852
 
    }
853
 
 
854
 
  sqlite3_profile(self->db, profilecb, self);
855
 
 
856
 
  Py_INCREF(callable);
857
 
 
858
 
 finally:
859
 
 
860
 
  Py_XDECREF(self->profile);
861
 
  self->profile=callable;
862
 
 
863
 
  return Py_BuildValue("");
864
 
}
865
 
#endif /* EXPERIMENTAL - sqlite3_profile */
866
 
 
867
 
 
868
 
#ifdef EXPERIMENTAL      /* commit hook */
869
 
int commithookcb(void *context)
870
 
{
871
 
  /* The hook returns 0 for commit to go ahead and non-zero to abort
872
 
     commit (turn into a rollback). We return non-zero for errors */
873
 
  
874
 
  PyGILState_STATE gilstate;
875
 
  PyObject *retval=NULL, *args=NULL;
876
 
  int ok=1; /* error state */
877
 
  Connection *self=(Connection *)context;
878
 
 
879
 
  assert(self);
880
 
  assert(self->commithook);
881
 
  assert(self->commithook!=Py_None);
882
 
 
883
 
  /* defensive coding */
884
 
  if(!self->commithook)
885
 
    return 0;
886
 
 
887
 
  gilstate=PyGILState_Ensure();
888
 
 
889
 
  if(PyErr_Occurred())
890
 
    goto finally;  /* abort hook due to outstanding exception */
891
 
 
892
 
  args=PyTuple_New(0);
893
 
  if(!args)
894
 
    goto finally; /* abort hook on failure to allocate args */
895
 
  
896
 
  retval=PyEval_CallObject(self->commithook, args);
897
 
 
898
 
  if(!retval)
899
 
    goto finally; /* abort hook due to exeception */
900
 
 
901
 
  ok=PyObject_IsTrue(retval);
902
 
  assert(ok==-1 || ok==0 || ok==1);
903
 
 
904
 
  if(ok==-1)
905
 
    {
906
 
      ok=1;
907
 
      goto finally;  /* abort due to exception in return value */
908
 
    }
909
 
 
910
 
 finally:
911
 
  Py_XDECREF(retval);
912
 
  Py_XDECREF(args);
913
 
  PyGILState_Release(gilstate);
914
 
  return ok;
915
 
}
916
 
 
917
 
static PyObject *
918
 
Connection_setcommithook(Connection *self, PyObject *callable)
919
 
{
920
 
  /* sqlite3_commit_hook doesn't return an error code */
921
 
  
922
 
  CHECK_THREAD(self,NULL);
923
 
 
924
 
  if(callable==Py_None)
925
 
    {
926
 
      sqlite3_commit_hook(self->db, NULL, NULL);
927
 
      callable=NULL;
928
 
      goto finally;
929
 
    }
930
 
 
931
 
  if(!PyCallable_Check(callable))
932
 
    {
933
 
      PyErr_Format(PyExc_TypeError, "commit hook must be callable");
934
 
      return NULL;
935
 
    }
936
 
 
937
 
  sqlite3_commit_hook(self->db, commithookcb, self);
938
 
 
939
 
  Py_INCREF(callable);
940
 
 
941
 
 finally:
942
 
 
943
 
  Py_XDECREF(self->commithook);
944
 
  self->commithook=callable;
945
 
 
946
 
  return Py_BuildValue("");
947
 
}
948
 
#endif  /* EXPERIMENTAL sqlite3_commit_hook */
949
 
 
950
 
#ifdef EXPERIMENTAL      /* sqlite3_progress_handler */
951
 
int progresshandlercb(void *context)
952
 
{
953
 
  /* The hook returns 0 for continue and non-zero to abort (rollback).
954
 
     We return non-zero for errors */
955
 
  
956
 
  PyGILState_STATE gilstate;
957
 
  PyObject *retval=NULL, *args=NULL;
958
 
  int ok=1; /* error state */
959
 
  Connection *self=(Connection *)context;
960
 
 
961
 
  assert(self);
962
 
  assert(self->progresshandler);
963
 
 
964
 
  /* defensive coding */
965
 
  if(!self->progresshandler)
966
 
    return 0;
967
 
 
968
 
  gilstate=PyGILState_Ensure();
969
 
 
970
 
  args=PyTuple_New(0);
971
 
  if(!args)
972
 
    goto finally; /* abort handler due to failure to allocate args */
973
 
  
974
 
  retval=PyEval_CallObject(self->progresshandler, args);
975
 
 
976
 
  if(!retval)
977
 
    goto finally; /* abort due to exeception */
978
 
 
979
 
  ok=PyObject_IsTrue(retval);
980
 
 
981
 
  assert(ok==-1 || ok==0 || ok==1);
982
 
 
983
 
  if(ok==-1)
984
 
    {
985
 
      ok=1;
986
 
      goto finally;  /* abort due to exception in result */
987
 
    }
988
 
 
989
 
 finally:
990
 
  Py_XDECREF(retval);
991
 
  Py_XDECREF(args);
992
 
 
993
 
  PyGILState_Release(gilstate);
994
 
  return ok;
995
 
}
996
 
 
997
 
static PyObject *
998
 
Connection_setprogresshandler(Connection *self, PyObject *args)
999
 
{
1000
 
  /* sqlite3_progress_handler doesn't return an error code */
1001
 
  int nsteps=20;
1002
 
  PyObject *callable=NULL;
1003
 
  
1004
 
  CHECK_THREAD(self,NULL);
1005
 
 
1006
 
  if(!PyArg_ParseTuple(args, "O|i:setprogresshandler(callable, nsteps=20)", &callable, &nsteps))
1007
 
    return NULL;
1008
 
 
1009
 
  if(callable==Py_None)
1010
 
    {
1011
 
      sqlite3_progress_handler(self->db, 0, NULL, NULL);
1012
 
      callable=NULL;
1013
 
      goto finally;
1014
 
    }
1015
 
 
1016
 
  if(!PyCallable_Check(callable))
1017
 
    {
1018
 
      PyErr_Format(PyExc_TypeError, "progress handler must be callable");
1019
 
      return NULL;
1020
 
    }
1021
 
 
1022
 
  sqlite3_progress_handler(self->db, nsteps, progresshandlercb, self);
1023
 
  Py_INCREF(callable);
1024
 
 
1025
 
 finally:
1026
 
 
1027
 
  Py_XDECREF(self->progresshandler);
1028
 
  self->progresshandler=callable;
1029
 
 
1030
 
  return Py_BuildValue("");
1031
 
}
1032
 
#endif  /* EXPERIMENTAL sqlite3_progress_handler */
1033
 
 
1034
 
int authorizercb(void *context, int operation, const char *paramone, const char *paramtwo, const char *databasename, const char *triggerview)
1035
 
{
1036
 
  /* should return one of SQLITE_OK, SQLITE_DENY, or
1037
 
     SQLITE_IGNORE. (0, 1 or 2 respectively) */
1038
 
 
1039
 
  PyGILState_STATE gilstate;
1040
 
  PyObject *args=NULL, *retval=NULL;
1041
 
  int result=SQLITE_DENY;  /* default to deny */
1042
 
  Connection *self=(Connection *)context;
1043
 
 
1044
 
  PyObject *poperation=NULL, *pone=NULL, *ptwo=NULL, *pdatabasename=NULL, *ptriggerview=NULL;
1045
 
 
1046
 
  assert(self);
1047
 
  assert(self->authorizer);
1048
 
  assert(self->authorizer!=Py_None);
1049
 
 
1050
 
  /* defensive coding */
1051
 
  if(!self->authorizer)
1052
 
    return SQLITE_OK;
1053
 
 
1054
 
  gilstate=PyGILState_Ensure();
1055
 
 
1056
 
  if(PyErr_Occurred())
1057
 
    goto finally;  /* abort due to earlier exception */
1058
 
 
1059
 
  poperation=Py_BuildValue("i", operation);
1060
 
  pone=convertutf8string(paramone);
1061
 
  ptwo=convertutf8string(paramtwo);
1062
 
  pdatabasename=convertutf8string(databasename);
1063
 
  ptriggerview=convertutf8string(triggerview);
1064
 
  args=PyTuple_New(5);
1065
 
 
1066
 
  if(!poperation || !pone || !ptwo || !pdatabasename || !ptriggerview || !args)
1067
 
    goto finally;
1068
 
 
1069
 
  PyTuple_SET_ITEM(args, 0, poperation);
1070
 
  PyTuple_SET_ITEM(args, 1, pone);
1071
 
  PyTuple_SET_ITEM(args, 2, ptwo);
1072
 
  PyTuple_SET_ITEM(args, 3, pdatabasename);
1073
 
  PyTuple_SET_ITEM(args, 4, ptriggerview);
1074
 
 
1075
 
  poperation=pone=ptwo=pdatabasename=ptriggerview=NULL;  /* owned by args now */
1076
 
 
1077
 
  retval=PyEval_CallObject(self->authorizer, args);
1078
 
 
1079
 
  if(!retval)
1080
 
    goto finally; /* abort due to exeception */
1081
 
 
1082
 
  result=PyInt_AsLong(retval);
1083
 
  if (PyErr_Occurred())
1084
 
    result=SQLITE_DENY;
1085
 
 
1086
 
 finally:
1087
 
  Py_XDECREF(poperation);
1088
 
  Py_XDECREF(pone);
1089
 
  Py_XDECREF(ptwo);
1090
 
  Py_XDECREF(pdatabasename);
1091
 
  Py_XDECREF(ptriggerview);
1092
 
  Py_XDECREF(args);
1093
 
  Py_XDECREF(retval);
1094
 
 
1095
 
  PyGILState_Release(gilstate);
1096
 
  return result;
1097
 
}
1098
 
 
1099
 
static PyObject *
1100
 
Connection_setauthorizer(Connection *self, PyObject *callable)
1101
 
{
1102
 
  int res;
1103
 
 
1104
 
  CHECK_THREAD(self,NULL);
1105
 
 
1106
 
  if(callable==Py_None)
1107
 
    {
1108
 
      res=sqlite3_set_authorizer(self->db, NULL, NULL);
1109
 
      callable=NULL;
1110
 
      goto finally;
1111
 
    }
1112
 
 
1113
 
  if(!PyCallable_Check(callable))
1114
 
    {
1115
 
      PyErr_Format(PyExc_TypeError, "authorizer must be callable");
1116
 
      return NULL;
1117
 
    }
1118
 
 
1119
 
  res=sqlite3_set_authorizer(self->db, authorizercb, self);
1120
 
  SET_EXC(self->db, res);
1121
 
 
1122
 
  Py_INCREF(callable);
1123
 
 
1124
 
 finally:
1125
 
  Py_XDECREF(self->authorizer);
1126
 
  self->authorizer=callable;
1127
 
 
1128
 
  return (res==SQLITE_OK)?Py_BuildValue(""):NULL;
1129
 
}
1130
 
 
1131
 
int busyhandlercb(void *context, int ncall)
1132
 
{
1133
 
  /* Return zero for caller to get SQLITE_BUSY error. We default to
1134
 
     zero in case of error. */
1135
 
 
1136
 
  PyGILState_STATE gilstate;
1137
 
  PyObject *args, *retval;
1138
 
  int result=0;  /* default to fail with SQLITE_BUSY */
1139
 
  Connection *self=(Connection *)context;
1140
 
 
1141
 
  assert(self);
1142
 
  assert(self->busyhandler);
1143
 
 
1144
 
  /* defensive coding */
1145
 
  if(!self->busyhandler)
1146
 
    return result;
1147
 
 
1148
 
  gilstate=PyGILState_Ensure();
1149
 
 
1150
 
  args=Py_BuildValue("(i)", ncall);
1151
 
  if(!args)
1152
 
    goto finally; /* abort busy due to memory allocation failure */
1153
 
  
1154
 
  retval=PyEval_CallObject(self->busyhandler, args);
1155
 
  Py_DECREF(args);
1156
 
 
1157
 
  if(!retval)
1158
 
    goto finally; /* abort due to exeception */
1159
 
 
1160
 
  result=PyObject_IsTrue(retval);
1161
 
  assert(result==-1 || result==0 || result==1);
1162
 
  Py_DECREF(retval);
1163
 
 
1164
 
  if(result==-1)
1165
 
    {
1166
 
      result=0;
1167
 
      goto finally;  /* abort due to exception converting retval */
1168
 
    }
1169
 
 
1170
 
 finally:
1171
 
  PyGILState_Release(gilstate);
1172
 
  return result;
1173
 
}
1174
 
 
1175
 
static PyObject *
1176
 
Connection_setbusyhandler(Connection *self, PyObject *callable)
1177
 
{
1178
 
  int res=SQLITE_OK;
1179
 
 
1180
 
  CHECK_THREAD(self,NULL);
1181
 
 
1182
 
  if(callable==Py_None)
1183
 
    {
1184
 
      res=sqlite3_busy_handler(self->db, NULL, NULL);
1185
 
      callable=NULL;
1186
 
      goto finally;
1187
 
    }
1188
 
 
1189
 
  if(!PyCallable_Check(callable))
1190
 
    {
1191
 
      PyErr_Format(PyExc_TypeError, "busyhandler must be callable");
1192
 
      return NULL;
1193
 
    }
1194
 
 
1195
 
  res=sqlite3_busy_handler(self->db, busyhandlercb, self);
1196
 
  SET_EXC(self->db, res);
1197
 
 
1198
 
  Py_INCREF(callable);
1199
 
 
1200
 
 finally:
1201
 
  Py_XDECREF(self->busyhandler);
1202
 
  self->busyhandler=callable;
1203
 
 
1204
 
  return (res==SQLITE_OK)?Py_BuildValue(""):NULL;
1205
 
}
1206
 
 
1207
 
 
1208
 
/* USER DEFINED FUNCTION CODE.*/
1209
 
 
1210
 
/* We store the registered functions in a linked list hooked into the
1211
 
   connection object so we can free them.  There is probably a better
1212
 
   data structure to use but this was most convenient. */
1213
 
 
1214
 
static funccbinfo *
1215
 
freefunccbinfo(funccbinfo *func)
1216
 
{
1217
 
  funccbinfo *fnext;
1218
 
  if(!func) return NULL;
1219
 
 
1220
 
  if(func->name)
1221
 
    PyMem_Free(func->name);
1222
 
  Py_XDECREF(func->scalarfunc);
1223
 
  Py_XDECREF(func->aggregatefactory);
1224
 
  fnext=func->next;
1225
 
  PyMem_Free(func);
1226
 
  return fnext;
1227
 
}
1228
 
 
1229
 
static funccbinfo *
1230
 
allocfunccbinfo(void)
1231
 
{
1232
 
  funccbinfo *res=PyMem_Malloc(sizeof(funccbinfo));
1233
 
  if(res)
1234
 
    memset(res, 0, sizeof(funccbinfo));
1235
 
  return res;
1236
 
}
1237
 
 
1238
 
/* Converts sqlite3_value to PyObject.  Returns a new reference. */
1239
 
static PyObject *
1240
 
convert_value_to_pyobject(sqlite3_value *value)
1241
 
{
1242
 
  /* DUPLICATE(ish) code: this is substantially similar to the code in
1243
 
     Cursor_next.  If you fix anything here then do it there as
1244
 
     well. */
1245
 
  const int coltype=sqlite3_value_type(value);
1246
 
 
1247
 
  switch(coltype)
1248
 
    {
1249
 
    case SQLITE_INTEGER:
1250
 
      {
1251
 
        long long vint=sqlite3_value_int64(value);
1252
 
        if(vint<INT32_MIN || vint>INT32_MAX)
1253
 
          return PyLong_FromLongLong(vint);
1254
 
        else
1255
 
          return PyInt_FromLong((long)vint);
1256
 
      }
1257
 
 
1258
 
    case SQLITE_FLOAT:
1259
 
      return PyFloat_FromDouble(sqlite3_value_double(value));
1260
 
      
1261
 
    case SQLITE_TEXT:
1262
 
      return convertutf8string(sqlite3_value_text(value));
1263
 
 
1264
 
    case SQLITE_NULL:
1265
 
      Py_INCREF(Py_None);
1266
 
      return Py_None;
1267
 
 
1268
 
    case SQLITE_BLOB:
1269
 
      {
1270
 
        PyObject *item;
1271
 
        Py_ssize_t sz=sqlite3_value_bytes(value);
1272
 
        item=PyBuffer_New(sz);
1273
 
        if(item)
1274
 
          {
1275
 
            void *buffy=0;
1276
 
            Py_ssize_t sz2=sz;
1277
 
            if(!PyObject_AsWriteBuffer(item, &buffy, &sz2))
1278
 
              memcpy(buffy, sqlite3_value_blob(value), sz);
1279
 
            else
1280
 
              {
1281
 
                Py_DECREF(item);
1282
 
                return NULL;
1283
 
              }
1284
 
          }
1285
 
        return NULL;
1286
 
      }
1287
 
 
1288
 
    default:
1289
 
      PyErr_Format(APSWException, "Unknown sqlite column type %d!", coltype);
1290
 
      return NULL;
1291
 
    }
1292
 
  /* can't get here */
1293
 
  assert(0);
1294
 
  return NULL;
1295
 
}
1296
 
 
1297
 
static void
1298
 
set_context_result(sqlite3_context *context, PyObject *obj)
1299
 
{
1300
 
  if(!obj)
1301
 
    {
1302
 
      assert(PyErr_Occurred());
1303
 
      /* TODO: possibly examine exception and return appropriate error
1304
 
         code eg for BusyError set error to SQLITE_BUSY */
1305
 
      sqlite3_result_error(context, "executing scalarcallback failed", SQLITE_ERROR);
1306
 
      return;
1307
 
    }
1308
 
 
1309
 
  /* DUPLICATE(ish) code: this is substantially similar to the code in
1310
 
     Cursor_dobinding.  If you fix anything here then do it there as
1311
 
     well. */
1312
 
 
1313
 
  if(obj==Py_None)
1314
 
    {
1315
 
      sqlite3_result_null(context);
1316
 
      return;
1317
 
    }
1318
 
  if(PyInt_Check(obj))
1319
 
    {
1320
 
      sqlite3_result_int64(context, PyInt_AS_LONG(obj));
1321
 
      return;
1322
 
    }
1323
 
  if (PyLong_Check(obj))
1324
 
    {
1325
 
      sqlite3_result_int64(context, PyLong_AsLongLong(obj));
1326
 
      return;
1327
 
    }
1328
 
  if (PyFloat_CheckExact(obj))
1329
 
    {
1330
 
      sqlite3_result_double(context, PyFloat_AS_DOUBLE(obj));
1331
 
      return;
1332
 
    }
1333
 
  if (PyUnicode_Check(obj))
1334
 
    {
1335
 
      UNIDATABEGIN(obj)
1336
 
        if(strdata)
1337
 
          {
1338
 
            if(strbytes>INT32_MAX)
1339
 
              {
1340
 
                PyErr_Format(ExcTooBig, "Unicode object is too large - SQLite only supports up to 2GB");
1341
 
              }
1342
 
            else
1343
 
              {
1344
 
                if(use16)
1345
 
                  sqlite3_result_text16(context, strdata, (int)strbytes, SQLITE_TRANSIENT);
1346
 
                else
1347
 
                  sqlite3_result_text(context, strdata, (int)strbytes, SQLITE_TRANSIENT);
1348
 
              }
1349
 
          }
1350
 
        else
1351
 
          sqlite3_result_error(context, "Unicode conversions failed", SQLITE_ERROR);
1352
 
      UNIDATAEND(obj);
1353
 
      return;
1354
 
    }
1355
 
  if (PyString_Check(obj))
1356
 
    {
1357
 
      const char *val=PyString_AS_STRING(obj);
1358
 
      const char *chk=val;
1359
 
      for(;*chk && !((*chk)&0x80); chk++);
1360
 
      if(*chk)
1361
 
        {
1362
 
          PyObject *str2=PyUnicode_FromObject(obj);
1363
 
          if(!str2)
1364
 
            {
1365
 
              sqlite3_result_error(context, "PyUnicode_FromObject failed", SQLITE_ERROR);
1366
 
              return;
1367
 
            }
1368
 
          UNIDATABEGIN(str2)
1369
 
            if(strdata)
1370
 
              {
1371
 
                if(strbytes>INT32_MAX)
1372
 
                  {
1373
 
                    PyErr_Format(ExcTooBig, "Unicode object is too large - SQLite only supports up to 2GB");
1374
 
                  }
1375
 
                else
1376
 
                  {
1377
 
                    if(use16)
1378
 
                      sqlite3_result_text16(context, strdata, (int)strbytes, SQLITE_TRANSIENT);
1379
 
                    else
1380
 
                      sqlite3_result_text(context, strdata, (int)strbytes, SQLITE_TRANSIENT);
1381
 
                  }
1382
 
              }
1383
 
            else
1384
 
              sqlite3_result_error(context, "Unicode conversions failed", SQLITE_ERROR);
1385
 
          UNIDATAEND(str2);
1386
 
          Py_DECREF(str2);
1387
 
        }
1388
 
      else
1389
 
        {
1390
 
          Py_ssize_t lenval=PyString_GET_SIZE(obj);
1391
 
          if(lenval>INT32_MAX)
1392
 
              {
1393
 
                PyErr_Format(ExcTooBig, "String object is too large - SQLite only supports up to 2GB");
1394
 
                }
1395
 
          else
1396
 
            sqlite3_result_text(context, val, (int)lenval, SQLITE_TRANSIENT);
1397
 
        }
1398
 
      return;
1399
 
    }
1400
 
  if (PyBuffer_Check(obj))
1401
 
    {
1402
 
      const char *buffer;
1403
 
      Py_ssize_t buflen;
1404
 
      if(PyObject_AsCharBuffer(obj, &buffer, &buflen))
1405
 
        {
1406
 
          sqlite3_result_error(context, "PyObject_AsCharBuffer failed", SQLITE_ERROR);
1407
 
          return;
1408
 
        }
1409
 
      if (buflen>INT32_MAX)
1410
 
        sqlite3_result_error(context, "Buffer object is too large for SQLite - only up to 2GB is supported", SQLITE_ERROR);
1411
 
      else
1412
 
        sqlite3_result_blob(context, buffer, (int)buflen, SQLITE_TRANSIENT);
1413
 
      return;
1414
 
    }
1415
 
 
1416
 
  PyErr_Format(PyExc_TypeError, "Bad return type from function callback");
1417
 
  sqlite3_result_error(context, "Bad return type from function callback", SQLITE_ERROR);
1418
 
 
1419
 
}
1420
 
 
1421
 
/* Returns a new reference to a tuple formed from function parameters */
1422
 
PyObject *
1423
 
getfunctionargs(sqlite3_context *context, PyObject *firstelement, int argc, sqlite3_value **argv)
1424
 
{
1425
 
  PyObject *pyargs=NULL;
1426
 
  int i;
1427
 
  int extra=0;
1428
 
 
1429
 
  /* extra first item */
1430
 
  if(firstelement)
1431
 
    extra=1;
1432
 
 
1433
 
  pyargs=PyTuple_New((long)argc+extra);
1434
 
  if(!pyargs)
1435
 
    {
1436
 
      sqlite3_result_error(context, "PyTuple_New failed", SQLITE_ERROR);
1437
 
      goto error;
1438
 
    }
1439
 
 
1440
 
  if(extra)
1441
 
    {
1442
 
      Py_INCREF(firstelement);
1443
 
      PyTuple_SET_ITEM(pyargs, 0, firstelement);
1444
 
    }
1445
 
 
1446
 
  for(i=0;i<argc;i++)
1447
 
    {
1448
 
      PyObject *item=convert_value_to_pyobject(argv[i]);
1449
 
      if(!item)
1450
 
        {
1451
 
          Py_DECREF(pyargs);
1452
 
          sqlite3_result_error(context, "convert_value_to_pyobject failed", SQLITE_ERROR);
1453
 
          goto error;
1454
 
        }
1455
 
      PyTuple_SET_ITEM(pyargs, i+extra, item);
1456
 
    }
1457
 
  
1458
 
  return pyargs;
1459
 
 
1460
 
 error:
1461
 
  Py_XDECREF(pyargs);
1462
 
  return NULL;
1463
 
}
1464
 
 
1465
 
 
1466
 
/* dispatches scalar function */
1467
 
static void
1468
 
cbdispatch_func(sqlite3_context *context, int argc, sqlite3_value **argv)
1469
 
{
1470
 
  PyGILState_STATE gilstate;
1471
 
  PyObject *pyargs;
1472
 
  PyObject *retval;
1473
 
  funccbinfo *cbinfo=(funccbinfo*)sqlite3_user_data(context);
1474
 
  assert(cbinfo);
1475
 
 
1476
 
  gilstate=PyGILState_Ensure();
1477
 
 
1478
 
  assert(cbinfo->scalarfunc);
1479
 
 
1480
 
  if(PyErr_Occurred())
1481
 
    {
1482
 
      sqlite3_result_error(context, "Prior Python Error", SQLITE_ERROR);
1483
 
      goto finally;
1484
 
    }
1485
 
 
1486
 
  pyargs=getfunctionargs(context, NULL, argc, argv);
1487
 
  if(!pyargs)
1488
 
      goto finally;
1489
 
 
1490
 
  assert(!PyErr_Occurred());
1491
 
  retval=PyEval_CallObject(cbinfo->scalarfunc, pyargs);
1492
 
  Py_DECREF(pyargs);
1493
 
  set_context_result(context, retval);
1494
 
  Py_XDECREF(retval);
1495
 
 
1496
 
 finally:
1497
 
   PyGILState_Release(gilstate);
1498
 
}
1499
 
 
1500
 
static aggregatefunctioncontext *
1501
 
getaggregatefunctioncontext(sqlite3_context *context)
1502
 
{
1503
 
  aggregatefunctioncontext *aggfc=sqlite3_aggregate_context(context, sizeof(aggregatefunctioncontext));
1504
 
  funccbinfo *cbinfo;
1505
 
  PyObject *retval;
1506
 
  PyObject *args;
1507
 
  /* have we seen it before? */
1508
 
  if(aggfc->aggvalue) return aggfc;
1509
 
  
1510
 
  /* fill in with Py_None so we know it is valid */
1511
 
  aggfc->aggvalue=Py_None;
1512
 
  Py_INCREF(Py_None);
1513
 
 
1514
 
  cbinfo=(funccbinfo*)sqlite3_user_data(context);
1515
 
  assert(cbinfo);
1516
 
  assert(cbinfo->aggregatefactory);
1517
 
 
1518
 
  /* call the aggregatefactory to get our working objects */
1519
 
  args=PyTuple_New(0);
1520
 
  if(!args)
1521
 
    return aggfc;
1522
 
  retval=PyEval_CallObject(cbinfo->aggregatefactory, args);
1523
 
  Py_DECREF(args);
1524
 
  if(!retval)
1525
 
    return aggfc;
1526
 
  /* it should have returned a tuple of 3 items: object, stepfunction and finalfunction */
1527
 
  if(!PyTuple_Check(retval))
1528
 
    {
1529
 
      PyErr_Format(PyExc_TypeError, "Aggregate factory should return tuple of (object, stepfunction, finalfunction)");
1530
 
      goto finally;
1531
 
    }
1532
 
  if(PyTuple_GET_SIZE(retval)!=3)
1533
 
    {
1534
 
      PyErr_Format(PyExc_TypeError, "Aggregate factory should return 3 item tuple of (object, stepfunction, finalfunction)");
1535
 
      goto finally;
1536
 
    }
1537
 
  /* we don't care about the type of the zeroth item (object) ... */
1538
 
 
1539
 
  /* stepfunc */
1540
 
  if (!PyCallable_Check(PyTuple_GET_ITEM(retval,1)))
1541
 
    {
1542
 
      PyErr_Format(PyExc_TypeError, "stepfunction must be callable");
1543
 
      goto finally;
1544
 
    }
1545
 
  
1546
 
  /* finalfunc */
1547
 
  if (!PyCallable_Check(PyTuple_GET_ITEM(retval,2)))
1548
 
    {
1549
 
      PyErr_Format(PyExc_TypeError, "final function must be callable");
1550
 
      goto finally;
1551
 
    }
1552
 
 
1553
 
  aggfc->aggvalue=PyTuple_GET_ITEM(retval,0);
1554
 
  aggfc->stepfunc=PyTuple_GET_ITEM(retval,1);
1555
 
  aggfc->finalfunc=PyTuple_GET_ITEM(retval,2);
1556
 
 
1557
 
  Py_INCREF(aggfc->aggvalue);
1558
 
  Py_INCREF(aggfc->stepfunc);
1559
 
  Py_INCREF(aggfc->finalfunc);
1560
 
      
1561
 
  Py_DECREF(Py_None);  /* we used this earlier as a sentinel */
1562
 
 
1563
 
 finally:
1564
 
  assert(retval);
1565
 
  Py_DECREF(retval);
1566
 
  return aggfc;
1567
 
}
1568
 
 
1569
 
 
1570
 
/*
1571
 
  Note that we can't call sqlite3_result_error in the step function as
1572
 
  SQLite doesn't want to you to do that (and core dumps!)
1573
 
  Consequently if an error is returned, we will still be repeatedly
1574
 
  called.
1575
 
*/
1576
 
 
1577
 
static void
1578
 
cbdispatch_step(sqlite3_context *context, int argc, sqlite3_value **argv)
1579
 
{
1580
 
  PyGILState_STATE gilstate;
1581
 
  PyObject *pyargs;
1582
 
  PyObject *retval;
1583
 
  aggregatefunctioncontext *aggfc=NULL;
1584
 
 
1585
 
  gilstate=PyGILState_Ensure();
1586
 
 
1587
 
  if (PyErr_Occurred())
1588
 
    goto finally;
1589
 
 
1590
 
  aggfc=getaggregatefunctioncontext(context);
1591
 
 
1592
 
  if (PyErr_Occurred())
1593
 
    goto finally;
1594
 
 
1595
 
  assert(aggfc);
1596
 
  
1597
 
  pyargs=getfunctionargs(context, aggfc->aggvalue, argc, argv);
1598
 
  if(!pyargs)
1599
 
    goto finally;
1600
 
 
1601
 
  assert(!PyErr_Occurred());
1602
 
  retval=PyEval_CallObject(aggfc->stepfunc, pyargs);
1603
 
  Py_DECREF(pyargs);
1604
 
  Py_XDECREF(retval);
1605
 
 
1606
 
  if(!retval)
1607
 
    {
1608
 
      assert(PyErr_Occurred());
1609
 
    }
1610
 
 
1611
 
 finally:
1612
 
   PyGILState_Release(gilstate);
1613
 
}
1614
 
 
1615
 
/* this is somewhat similar to cbdispatch_step, except we also have to
1616
 
   do some cleanup of the aggregatefunctioncontext */
1617
 
static void
1618
 
cbdispatch_final(sqlite3_context *context)
1619
 
{
1620
 
  PyGILState_STATE gilstate;
1621
 
  PyObject *pyargs=NULL;
1622
 
  PyObject *retval=NULL;
1623
 
  aggregatefunctioncontext *aggfc=NULL;
1624
 
  PyObject *err_type=NULL, *err_value=NULL, *err_traceback=NULL;
1625
 
 
1626
 
  gilstate=PyGILState_Ensure();
1627
 
 
1628
 
  PyErr_Fetch(&err_type, &err_value, &err_traceback);
1629
 
  PyErr_Clear();
1630
 
 
1631
 
  aggfc=getaggregatefunctioncontext(context);
1632
 
 
1633
 
  assert(aggfc);
1634
 
  
1635
 
  if((err_type||err_value||err_traceback) || PyErr_Occurred() || !aggfc->finalfunc)
1636
 
    {
1637
 
      sqlite3_result_error(context, "Prior Python Error in step function", SQLITE_ERROR);
1638
 
      goto finally;
1639
 
    }
1640
 
 
1641
 
  pyargs=PyTuple_New(1);
1642
 
  if(!pyargs)
1643
 
    goto finally;
1644
 
 
1645
 
  Py_INCREF(aggfc->aggvalue);
1646
 
  PyTuple_SET_ITEM(pyargs, 0, aggfc->aggvalue);
1647
 
 
1648
 
  retval=PyEval_CallObject(aggfc->finalfunc, pyargs);
1649
 
  Py_DECREF(pyargs);
1650
 
  set_context_result(context, retval);
1651
 
  Py_XDECREF(retval);
1652
 
 
1653
 
 finally:
1654
 
  /* we also free the aggregatefunctioncontext here */
1655
 
  assert(aggfc->aggvalue);  /* should always be set, perhaps to Py_None */
1656
 
  Py_XDECREF(aggfc->aggvalue);
1657
 
  Py_XDECREF(aggfc->stepfunc);
1658
 
  Py_XDECREF(aggfc->finalfunc);
1659
 
 
1660
 
  if(PyErr_Occurred() && (err_type||err_value||err_traceback))
1661
 
    {
1662
 
      PyErr_Format(PyExc_StandardError, "An exception happened during cleanup of an aggregate function, but there was already error in the step function so only that can be returned");
1663
 
      PyErr_WriteUnraisable(Py_None); /* there is no object to give, and NULL causes some versions to core dump */
1664
 
    }
1665
 
 
1666
 
  if(err_type||err_value||err_traceback)
1667
 
    PyErr_Restore(err_type, err_value, err_traceback);
1668
 
 
1669
 
  /* sqlite3 frees the actual underlying memory we used (aggfc itself) */
1670
 
 
1671
 
  PyGILState_Release(gilstate);
1672
 
}
1673
 
 
1674
 
 
1675
 
static PyObject *
1676
 
Connection_createscalarfunction(Connection *self, PyObject *args)
1677
 
{
1678
 
  int numargs=-1;
1679
 
  PyObject *callable;
1680
 
  char *name=0;
1681
 
  char *chk;
1682
 
  funccbinfo *cbinfo;
1683
 
  int res;
1684
 
 
1685
 
  CHECK_THREAD(self,NULL);
1686
 
 
1687
 
  if(!PyArg_ParseTuple(args, "esO|i:createscalarfunction(name,callback, numargs=-1)", STRENCODING, &name, &callable, &numargs))
1688
 
    return NULL;
1689
 
 
1690
 
  assert(name);
1691
 
  assert(callable);
1692
 
 
1693
 
  /* there isn't a C api to get a (potentially unicode) string and
1694
 
     make it uppercase so we hack around  */
1695
 
 
1696
 
  /* validate the name */
1697
 
  for(chk=name;*chk && !((*chk)&0x80);chk++);
1698
 
  if(*chk)
1699
 
    {
1700
 
      PyMem_Free(name);
1701
 
      PyErr_SetString(PyExc_TypeError, "function name must be ascii characters only");
1702
 
      return NULL;
1703
 
    }
1704
 
 
1705
 
  /* convert name to upper case */
1706
 
  for(chk=name;*chk;chk++)
1707
 
    if(*chk>='a' && *chk<='z')
1708
 
      *chk-='a'-'A';
1709
 
 
1710
 
  /* ::TODO:: check if name points to already defined function and free relevant funccbinfo */
1711
 
 
1712
 
  if(callable!=Py_None && !PyCallable_Check(callable))
1713
 
    {
1714
 
      PyMem_Free(name);
1715
 
      PyErr_SetString(PyExc_TypeError, "parameter must be callable");
1716
 
      return NULL;
1717
 
    }
1718
 
 
1719
 
  Py_INCREF(callable);
1720
 
 
1721
 
  cbinfo=allocfunccbinfo();
1722
 
  cbinfo->name=name;
1723
 
  cbinfo->scalarfunc=callable;
1724
 
 
1725
 
  res=sqlite3_create_function(self->db,
1726
 
                              name,
1727
 
                              numargs,
1728
 
                              SQLITE_UTF8,  /* it isn't very clear what this parameter does */
1729
 
                              (callable!=Py_None)?cbinfo:NULL,
1730
 
                              (callable!=Py_None)?cbdispatch_func:NULL,
1731
 
                              NULL,
1732
 
                              NULL);
1733
 
 
1734
 
  if(res)
1735
 
    {
1736
 
      freefunccbinfo(cbinfo);
1737
 
      SET_EXC(self->db, res);
1738
 
      return NULL;
1739
 
    }
1740
 
 
1741
 
  if(callable!=Py_None)
1742
 
    {
1743
 
      /* put cbinfo into the linked list */
1744
 
      cbinfo->next=self->functions;
1745
 
      self->functions=cbinfo;
1746
 
    }
1747
 
  else
1748
 
    {
1749
 
      /* free it since we cancelled the function */
1750
 
      freefunccbinfo(cbinfo);
1751
 
    }
1752
 
  
1753
 
  return Py_BuildValue("");
1754
 
}
1755
 
 
1756
 
static PyObject *
1757
 
Connection_createaggregatefunction(Connection *self, PyObject *args)
1758
 
{
1759
 
  int numargs=-1;
1760
 
  PyObject *callable;
1761
 
  char *name=0;
1762
 
  char *chk;
1763
 
  funccbinfo *cbinfo;
1764
 
  int res;
1765
 
 
1766
 
  CHECK_THREAD(self,NULL);
1767
 
 
1768
 
  if(!PyArg_ParseTuple(args, "esO|i:createaggregatefunction(name, factorycallback, numargs=-1)", STRENCODING, &name, &callable, &numargs))
1769
 
    return NULL;
1770
 
 
1771
 
  assert(name);
1772
 
  assert(callable);
1773
 
 
1774
 
  /* there isn't a C api to get a (potentially unicode) string and make it uppercase so we hack around  */
1775
 
 
1776
 
  /* validate the name */
1777
 
  for(chk=name;*chk && !((*chk)&0x80);chk++);
1778
 
  if(*chk)
1779
 
    {
1780
 
      PyMem_Free(name);
1781
 
      PyErr_SetString(PyExc_TypeError, "function name must be ascii characters only");
1782
 
      return NULL;
1783
 
    }
1784
 
 
1785
 
  /* convert name to upper case */
1786
 
  for(chk=name;*chk;chk++)
1787
 
    if(*chk>='a' && *chk<='z')
1788
 
      *chk-='a'-'A';
1789
 
 
1790
 
  /* ::TODO:: check if name points to already defined function and free relevant funccbinfo */
1791
 
 
1792
 
  if(callable!=Py_None && !PyCallable_Check(callable))
1793
 
    {
1794
 
      PyMem_Free(name);
1795
 
      PyErr_SetString(PyExc_TypeError, "parameter must be callable");
1796
 
      return NULL;
1797
 
    }
1798
 
 
1799
 
  Py_INCREF(callable);
1800
 
 
1801
 
  cbinfo=allocfunccbinfo();
1802
 
  cbinfo->name=name;
1803
 
  cbinfo->aggregatefactory=callable;
1804
 
 
1805
 
  res=sqlite3_create_function(self->db,
1806
 
                              name,
1807
 
                              numargs,
1808
 
                              SQLITE_UTF8,  /* it isn't very clear what this parameter does */
1809
 
                              (callable!=Py_None)?cbinfo:NULL,
1810
 
                              NULL,
1811
 
                              (callable!=Py_None)?cbdispatch_step:NULL,
1812
 
                              (callable!=Py_None)?cbdispatch_final:NULL);
1813
 
 
1814
 
  if(res)
1815
 
    {
1816
 
      freefunccbinfo(cbinfo);
1817
 
      SET_EXC(self->db, res);
1818
 
      return NULL;
1819
 
    }
1820
 
 
1821
 
  if(callable!=Py_None)
1822
 
    {
1823
 
      /* put cbinfo into the linked list */
1824
 
      cbinfo->next=self->functions;
1825
 
      self->functions=cbinfo;
1826
 
    }
1827
 
  else
1828
 
    {
1829
 
      /* free things up */
1830
 
      freefunccbinfo(cbinfo);
1831
 
    }
1832
 
  
1833
 
  return Py_BuildValue("");
1834
 
}
1835
 
 
1836
 
/* USER DEFINED COLLATION CODE.*/
1837
 
 
1838
 
/*  We store the registered collations in a linked list hooked into
1839
 
   the connection object so we can free them.  There is probably a
1840
 
   better data structure to use but this was most convenient. */
1841
 
 
1842
 
static collationcbinfo *
1843
 
freecollationcbinfo(collationcbinfo *collation)
1844
 
{
1845
 
  collationcbinfo *cnext;
1846
 
  if(!collation) return NULL;
1847
 
 
1848
 
  if(collation->name)
1849
 
    PyMem_Free(collation->name);
1850
 
  Py_XDECREF(collation->func);
1851
 
  cnext=collation->next;
1852
 
  PyMem_Free(collation);
1853
 
  return cnext;
1854
 
}
1855
 
 
1856
 
static collationcbinfo *
1857
 
alloccollationcbinfo(void)
1858
 
{
1859
 
  collationcbinfo *res=PyMem_Malloc(sizeof(collationcbinfo));
1860
 
  memset(res, 0, sizeof(collationcbinfo));
1861
 
  return res;
1862
 
}
1863
 
 
1864
 
int collation_cb(void *context, 
1865
 
                 int stringonelen, const void *stringonedata,
1866
 
                 int stringtwolen, const void *stringtwodata)
1867
 
{
1868
 
  PyGILState_STATE gilstate;
1869
 
  collationcbinfo *cbinfo=(collationcbinfo*)context;
1870
 
  PyObject *pys1=NULL, *pys2=NULL, *retval=NULL, *pyargs=NULL;
1871
 
  int result=0;
1872
 
 
1873
 
  assert(cbinfo);
1874
 
 
1875
 
  gilstate=PyGILState_Ensure();
1876
 
 
1877
 
  if(PyErr_Occurred()) goto finally;  /* outstanding error */
1878
 
 
1879
 
  pys1=convertutf8stringsize(stringonedata, stringonelen);
1880
 
  pys2=convertutf8stringsize(stringtwodata, stringtwolen);
1881
 
 
1882
 
  if(!pys1 || !pys2)  
1883
 
    goto finally;   /* failed to allocate strings */
1884
 
 
1885
 
  pyargs=PyTuple_New(2);
1886
 
  if(!pyargs) 
1887
 
    goto finally; /* failed to allocate arg tuple */
1888
 
 
1889
 
  PyTuple_SET_ITEM(pyargs, 0, pys1);
1890
 
  PyTuple_SET_ITEM(pyargs, 1, pys2);
1891
 
 
1892
 
  pys1=pys2=NULL;  /* pyargs owns them now */
1893
 
 
1894
 
  assert(!PyErr_Occurred());
1895
 
 
1896
 
  retval=PyEval_CallObject(cbinfo->func, pyargs);
1897
 
 
1898
 
  if(!retval) goto finally;  /* execution failed */
1899
 
 
1900
 
  result=PyInt_AsLong(retval);
1901
 
  if(PyErr_Occurred())
1902
 
      result=0;
1903
 
 
1904
 
 
1905
 
 finally:
1906
 
  Py_XDECREF(pys1);
1907
 
  Py_XDECREF(pys2);
1908
 
  Py_XDECREF(retval);
1909
 
  Py_XDECREF(pyargs);
1910
 
  PyGILState_Release(gilstate);
1911
 
  return result;
1912
 
 
1913
 
}
1914
 
 
1915
 
static PyObject *
1916
 
Connection_createcollation(Connection *self, PyObject *args)
1917
 
{
1918
 
  PyObject *callable;
1919
 
  char *name=0;
1920
 
  char *chk;
1921
 
  collationcbinfo *cbinfo;
1922
 
  int res;
1923
 
 
1924
 
  CHECK_THREAD(self,NULL);
1925
 
  
1926
 
  if(!PyArg_ParseTuple(args, "esO:createcollation(name,callback)", STRENCODING, &name, &callable))
1927
 
    return NULL;
1928
 
 
1929
 
  assert(name);
1930
 
  assert(callable);
1931
 
 
1932
 
  /* there isn't a C api to get a (potentially unicode) string and make it uppercase so we hack around  */
1933
 
 
1934
 
  /* validate the name */
1935
 
  for(chk=name;*chk && !((*chk)&0x80);chk++);
1936
 
  if(*chk)
1937
 
    {
1938
 
      PyMem_Free(name);
1939
 
      PyErr_SetString(PyExc_TypeError, "function name must be ascii characters only");
1940
 
      return NULL;
1941
 
    }
1942
 
 
1943
 
  /* convert name to upper case */
1944
 
  for(chk=name;*chk;chk++)
1945
 
    if(*chk>='a' && *chk<='z')
1946
 
      *chk-='a'-'A';
1947
 
 
1948
 
  /* ::TODO:: check if name points to already defined collation and free relevant collationcbinfo */
1949
 
 
1950
 
  if(callable!=Py_None && !PyCallable_Check(callable))
1951
 
    {
1952
 
      PyMem_Free(name);
1953
 
      PyErr_SetString(PyExc_TypeError, "parameter must be callable");
1954
 
      return NULL;
1955
 
    }
1956
 
 
1957
 
  Py_INCREF(callable);
1958
 
 
1959
 
  cbinfo=alloccollationcbinfo();
1960
 
  cbinfo->name=name;
1961
 
  cbinfo->func=callable;
1962
 
 
1963
 
  res=sqlite3_create_collation(self->db,
1964
 
                               name,
1965
 
                               SQLITE_UTF8,
1966
 
                               (callable!=Py_None)?cbinfo:NULL,
1967
 
                               (callable!=Py_None)?collation_cb:NULL);
1968
 
  if(res)
1969
 
    {
1970
 
      freecollationcbinfo(cbinfo);
1971
 
      SET_EXC(self->db, res);
1972
 
      return NULL;
1973
 
    }
1974
 
 
1975
 
  if (callable!=Py_None)
1976
 
    {
1977
 
      /* put cbinfo into the linked list */
1978
 
      cbinfo->next=self->collations;
1979
 
      self->collations=cbinfo;
1980
 
    }
1981
 
  else
1982
 
    {
1983
 
      /* destroy info */
1984
 
      freecollationcbinfo(cbinfo);
1985
 
    }
1986
 
  
1987
 
  return Py_BuildValue("");
1988
 
}
1989
 
 
1990
 
 
1991
 
static PyMethodDef Connection_methods[] = {
1992
 
  {"cursor", (PyCFunction)Connection_cursor, METH_NOARGS,
1993
 
   "Create a new cursor" },
1994
 
  {"setbusytimeout", (PyCFunction)Connection_setbusytimeout, METH_VARARGS,
1995
 
   "Sets the sqlite busy timeout in milliseconds.  Use zero to disable the timeout"},
1996
 
  {"interrupt", (PyCFunction)Connection_interrupt, METH_NOARGS,
1997
 
   "Causes any pending database operations to abort at the earliest opportunity"},
1998
 
  {"createscalarfunction", (PyCFunction)Connection_createscalarfunction, METH_VARARGS,
1999
 
   "Creates a scalar function"},
2000
 
  {"createaggregatefunction", (PyCFunction)Connection_createaggregatefunction, METH_VARARGS,
2001
 
   "Creates an aggregate function"},
2002
 
  {"setbusyhandler", (PyCFunction)Connection_setbusyhandler, METH_O,
2003
 
   "Sets the busy handler"},
2004
 
  {"changes", (PyCFunction)Connection_changes, METH_NOARGS, 
2005
 
   "Returns the number of rows changed by last query"},
2006
 
  {"totalchanges", (PyCFunction)Connection_totalchanges, METH_NOARGS, 
2007
 
   "Returns the total number of changes to database since it was opened"},
2008
 
  {"getautocommit", (PyCFunction)Connection_getautocommit, METH_NOARGS, 
2009
 
   "Returns if the database is in auto-commit mode"},
2010
 
  {"createcollation", (PyCFunction)Connection_createcollation, METH_VARARGS,
2011
 
   "Creates a collation function"},
2012
 
  {"last_insert_rowid", (PyCFunction)Connection_last_insert_rowid, METH_NOARGS,
2013
 
   "Returns rowid for last insert"},
2014
 
  {"complete", (PyCFunction)Connection_complete, METH_VARARGS,
2015
 
   "Checks if a SQL statement is complete"},
2016
 
  {"setauthorizer", (PyCFunction)Connection_setauthorizer, METH_O,
2017
 
   "Sets an authorizer function"},
2018
 
  {"setupdatehook", (PyCFunction)Connection_setupdatehook, METH_O,
2019
 
      "Sets an update hook"},
2020
 
  {"setrollbackhook", (PyCFunction)Connection_setrollbackhook, METH_O,
2021
 
   "Sets a callable invoked before each rollback"},
2022
 
#ifdef EXPERIMENTAL
2023
 
  {"setprofile", (PyCFunction)Connection_setprofile, METH_O,
2024
 
   "Sets a callable invoked with profile information after each statement"},
2025
 
  {"setcommithook", (PyCFunction)Connection_setcommithook, METH_O,
2026
 
   "Sets a callable invoked before each commit"},
2027
 
  {"setprogresshandler", (PyCFunction)Connection_setprogresshandler, METH_VARARGS,
2028
 
   "Sets a callback invoked periodically during long running calls"},
2029
 
#endif
2030
 
  {NULL}  /* Sentinel */
2031
 
};
2032
 
 
2033
 
 
2034
 
static PyTypeObject ConnectionType = {
2035
 
    PyObject_HEAD_INIT(NULL)
2036
 
    0,                         /*ob_size*/
2037
 
    "apsw.Connection",         /*tp_name*/
2038
 
    sizeof(Connection),        /*tp_basicsize*/
2039
 
    0,                         /*tp_itemsize*/
2040
 
    (destructor)Connection_dealloc, /*tp_dealloc*/ 
2041
 
    0,                         /*tp_print*/
2042
 
    0,                         /*tp_getattr*/
2043
 
    0,                         /*tp_setattr*/
2044
 
    0,                         /*tp_compare*/
2045
 
    0,                         /*tp_repr*/
2046
 
    0,                         /*tp_as_number*/
2047
 
    0,                         /*tp_as_sequence*/
2048
 
    0,                         /*tp_as_mapping*/
2049
 
    0,                         /*tp_hash */
2050
 
    0,                         /*tp_call*/
2051
 
    0,                         /*tp_str*/
2052
 
    0,                         /*tp_getattro*/
2053
 
    0,                         /*tp_setattro*/
2054
 
    0,                         /*tp_as_buffer*/
2055
 
    Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
2056
 
    "Connection object",       /* tp_doc */
2057
 
    0,                         /* tp_traverse */
2058
 
    0,                         /* tp_clear */
2059
 
    0,                         /* tp_richcompare */
2060
 
    0,                         /* tp_weaklistoffset */
2061
 
    0,                         /* tp_iter */
2062
 
    0,                         /* tp_iternext */
2063
 
    Connection_methods,        /* tp_methods */
2064
 
    0,                         /* tp_members */
2065
 
    0,                         /* tp_getset */
2066
 
    0,                         /* tp_base */
2067
 
    0,                         /* tp_dict */
2068
 
    0,                         /* tp_descr_get */
2069
 
    0,                         /* tp_descr_set */
2070
 
    0,                         /* tp_dictoffset */
2071
 
    (initproc)Connection_init, /* tp_init */
2072
 
    0,                         /* tp_alloc */
2073
 
    Connection_new,            /* tp_new */
2074
 
};
2075
 
 
2076
 
 
2077
 
/* CURSOR CODE */
2078
 
 
2079
 
/* Do finalization and free resources.  Returns the SQLITE error code */
2080
 
static int
2081
 
resetcursor(Cursor *self)
2082
 
{
2083
 
  int res=SQLITE_OK;
2084
 
 
2085
 
  Py_XDECREF(self->bindings);
2086
 
  self->bindings=NULL;
2087
 
  self->bindingsoffset=-1;
2088
 
 
2089
 
  if(self->statement)
2090
 
    {
2091
 
      res=sqlite3_finalize(self->statement);
2092
 
      SET_EXC(self->connection->db, res);
2093
 
      self->statement=0;
2094
 
    }
2095
 
 
2096
 
  if(self->status!=C_DONE && self->zsqlnextpos)
2097
 
    {
2098
 
      if (*self->zsqlnextpos && res==SQLITE_OK)
2099
 
        {
2100
 
          /* We still have more, so this is actually an abort. */
2101
 
          res=SQLITE_ERROR;
2102
 
          if(!PyErr_Occurred())
2103
 
            PyErr_Format(ExcIncomplete, "Error: there are still remaining sql statements to execute");
2104
 
        }
2105
 
    }
2106
 
  self->zsqlnextpos=NULL;
2107
 
  
2108
 
  if(self->status!=C_DONE && self->emiter)
2109
 
    {
2110
 
      PyObject *next=PyIter_Next(self->emiter);
2111
 
      if(next)
2112
 
        {
2113
 
          Py_DECREF(next);
2114
 
          res=SQLITE_ERROR;
2115
 
          if (!PyErr_Occurred())
2116
 
            PyErr_Format(ExcIncomplete, "Error: there are still many remaining sql statements to execute");
2117
 
        }
2118
 
    }
2119
 
     
2120
 
  Py_XDECREF(self->emiter);
2121
 
  self->emiter=NULL;
2122
 
 
2123
 
  if(self->zsql)
2124
 
    {
2125
 
      PyMem_Free((void*)self->zsql);
2126
 
      self->zsql=0;
2127
 
    }
2128
 
 
2129
 
  self->status=C_DONE;
2130
 
 
2131
 
  return res;
2132
 
}
2133
 
 
2134
 
static void
2135
 
Cursor_dealloc(Cursor * self)
2136
 
{
2137
 
  /* thread check - we can't use macro as it returns*/
2138
 
  PyObject *err_type, *err_value, *err_traceback;
2139
 
  int have_error=PyErr_Occurred()?1:0;
2140
 
 
2141
 
  if(self->connection->thread_ident!=PyThread_get_thread_ident())
2142
 
    {
2143
 
      if (have_error)
2144
 
        PyErr_Fetch(&err_type, &err_value, &err_traceback);
2145
 
      PyErr_Format(PyExc_RuntimeError, "The destructor for Cursor is called in a different thread than it"
2146
 
                   "was created in.  All calls must be in the same thread.  It was created in thread %d" 
2147
 
                   "and this is %d.  SQLite is not being closed as a result.",
2148
 
                   (int)(self->connection->thread_ident), (int)(PyThread_get_thread_ident()));            
2149
 
      PyErr_WriteUnraisable((PyObject*)self);
2150
 
      if (have_error)
2151
 
        PyErr_Restore(err_type, err_value, err_traceback);
2152
 
      
2153
 
      return;
2154
 
    }
2155
 
 
2156
 
  /* do our finalisation ... */
2157
 
 
2158
 
  if (have_error)
2159
 
    {
2160
 
      /* remember the existing error so that resetcursor won't immediately return */
2161
 
      PyErr_Fetch(&err_type, &err_value, &err_traceback);
2162
 
      PyErr_Clear();
2163
 
    }
2164
 
 
2165
 
  resetcursor(self);
2166
 
  if(PyErr_Occurred())
2167
 
    PyErr_Clear(); /* clear out any exceptions from resetcursor since we don't care */
2168
 
 
2169
 
  if (have_error)
2170
 
    /* restore earlier error if there was one */
2171
 
    PyErr_Restore(err_type, err_value, err_traceback);
2172
 
 
2173
 
  /* we no longer need connection */
2174
 
  if(self->connection)
2175
 
    {
2176
 
      Py_DECREF(self->connection);
2177
 
      self->connection=0;
2178
 
    }
2179
 
 
2180
 
  /* executemany iterator */
2181
 
  Py_XDECREF(self->emiter);
2182
 
  self->emiter=NULL;
2183
 
 
2184
 
  /* no need for tracing */
2185
 
  Py_XDECREF(self->exectrace);
2186
 
  Py_XDECREF(self->rowtrace);
2187
 
  self->exectrace=self->rowtrace=0;
2188
 
  
2189
 
  self->ob_type->tp_free((PyObject*)self);
2190
 
}
2191
 
 
2192
 
static PyObject *
2193
 
Cursor_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
2194
 
{
2195
 
    Cursor *self;
2196
 
 
2197
 
    self = (Cursor *)type->tp_alloc(type, 0);
2198
 
    if (self != NULL) {
2199
 
      self->connection=0;
2200
 
      self->statement=0;
2201
 
      self->zsql=0;
2202
 
      self->zsqlnextpos=0;
2203
 
      self->status=C_DONE;
2204
 
      self->bindings=0;
2205
 
      self->bindingsoffset=0;
2206
 
      self->emiter=0;
2207
 
      self->exectrace=0;
2208
 
      self->rowtrace=0;
2209
 
    }
2210
 
 
2211
 
    return (PyObject *)self;
2212
 
}
2213
 
 
2214
 
static int
2215
 
Cursor_init(Cursor *self, PyObject *args, PyObject *kwds)
2216
 
{
2217
 
  CHECK_THREAD(self->connection,-1);
2218
 
 
2219
 
  if(!PyArg_ParseTupleAndKeywords(args, kwds, "", NULL))
2220
 
    return -1;
2221
 
  
2222
 
  return 0;
2223
 
}
2224
 
 
2225
 
static PyObject *
2226
 
Cursor_getdescription(Cursor *self)
2227
 
{
2228
 
  int ncols,i;
2229
 
  PyObject *result=NULL;
2230
 
  PyObject *pair=NULL;
2231
 
  PyObject *first=NULL;
2232
 
  PyObject *second=NULL;
2233
 
  const char *str;
2234
 
 
2235
 
  CHECK_THREAD(self->connection,NULL);
2236
 
 
2237
 
  if(!self->statement)
2238
 
    {
2239
 
      PyErr_Format(ExcComplete, "Can't get description for statements that have completed execution");
2240
 
      return NULL;
2241
 
    }
2242
 
  
2243
 
  ncols=sqlite3_column_count(self->statement);
2244
 
  result=PyTuple_New(ncols);
2245
 
  if(!result) goto error;
2246
 
 
2247
 
  for(i=0;i<ncols;i++)
2248
 
    {
2249
 
      pair=PyTuple_New(2);
2250
 
      if(!pair) goto error;
2251
 
 
2252
 
      str=sqlite3_column_name(self->statement, i);
2253
 
      first=convertutf8string(str);
2254
 
 
2255
 
      str=sqlite3_column_decltype(self->statement, i);
2256
 
      second=convertutf8string(str);
2257
 
 
2258
 
      if(!first || !second) goto error;
2259
 
 
2260
 
      PyTuple_SET_ITEM(pair, 0, first);
2261
 
      PyTuple_SET_ITEM(pair, 1, second);
2262
 
 
2263
 
      /* owned by pair now */
2264
 
      first=second=0;
2265
 
 
2266
 
      PyTuple_SET_ITEM(result, i, pair);
2267
 
      /* owned by result now */
2268
 
      pair=0;
2269
 
    }
2270
 
  
2271
 
  return result;
2272
 
 
2273
 
 error:
2274
 
  Py_XDECREF(result);
2275
 
  Py_XDECREF(pair);
2276
 
  Py_XDECREF(first);
2277
 
  Py_XDECREF(second);
2278
 
  return NULL;
2279
 
}
2280
 
 
2281
 
/* internal function - returns SQLite error code (ie SQLITE_OK if all is well) */
2282
 
static int
2283
 
Cursor_dobinding(Cursor *self, int arg, PyObject *obj)
2284
 
{
2285
 
 
2286
 
  /* DUPLICATE(ish) code: this is substantially similar to the code in
2287
 
     set_context_result.  If you fix anything here then do it there as
2288
 
     well. */
2289
 
 
2290
 
  int res=SQLITE_OK;
2291
 
 
2292
 
  if(PyErr_Occurred()) return -1;
2293
 
 
2294
 
  if(obj==Py_None)
2295
 
    res=sqlite3_bind_null(self->statement, arg);
2296
 
  /* Python uses a 'long' for storage of PyInt.  This could
2297
 
     be a 32bit or 64bit quantity depending on the platform. */
2298
 
  else if(PyInt_Check(obj))
2299
 
    res=sqlite3_bind_int64(self->statement, arg, PyInt_AS_LONG(obj));
2300
 
  else if (PyLong_Check(obj))
2301
 
    /* nb: PyLong_AsLongLong can cause Python level error */
2302
 
    res=sqlite3_bind_int64(self->statement, arg, PyLong_AsLongLong(obj));
2303
 
  else if (PyFloat_Check(obj))
2304
 
    res=sqlite3_bind_double(self->statement, arg, PyFloat_AS_DOUBLE(obj));
2305
 
  else if (PyUnicode_Check(obj))
2306
 
    {
2307
 
      const void *badptr=NULL;
2308
 
      UNIDATABEGIN(obj)
2309
 
        badptr=strdata;
2310
 
        if(strdata)
2311
 
          {
2312
 
            if(strbytes>INT32_MAX)
2313
 
              {
2314
 
                PyErr_Format(ExcTooBig, "Unicode object is too large - SQLite only supports up to 2GB");
2315
 
              }
2316
 
            else
2317
 
              {
2318
 
                if(use16)
2319
 
                  res=sqlite3_bind_text16(self->statement, arg, strdata, (int)strbytes, SQLITE_TRANSIENT);
2320
 
                else
2321
 
                  res=sqlite3_bind_text(self->statement, arg, strdata, (int)strbytes, SQLITE_TRANSIENT);
2322
 
              }
2323
 
          }
2324
 
      UNIDATAEND(obj);
2325
 
      if(!badptr) 
2326
 
        {
2327
 
          assert(PyErr_Occurred());
2328
 
          return -1;
2329
 
        }
2330
 
    }
2331
 
  else if (PyString_Check(obj))
2332
 
    {
2333
 
      const char *val=PyString_AS_STRING(obj);
2334
 
      const char *chk=val;
2335
 
      for(;*chk && !((*chk)&0x80); chk++);
2336
 
      if(*chk)
2337
 
        {
2338
 
          const void *badptr=NULL;
2339
 
          PyObject *str2=PyUnicode_FromObject(obj);
2340
 
          if(!str2)
2341
 
            return -1;
2342
 
          UNIDATABEGIN(str2)
2343
 
            badptr=strdata;
2344
 
            if(strdata)
2345
 
              {
2346
 
                if(strbytes>INT32_MAX)
2347
 
                  {
2348
 
                    PyErr_Format(ExcTooBig, "Unicode object is too large - SQLite only supports up to 2GB");
2349
 
                  }
2350
 
                else
2351
 
                  {
2352
 
                    if(use16)
2353
 
                      res=sqlite3_bind_text16(self->statement, arg, strdata, (int)strbytes, SQLITE_TRANSIENT);
2354
 
                    else
2355
 
                      res=sqlite3_bind_text(self->statement, arg, strdata, (int)strbytes, SQLITE_TRANSIENT);
2356
 
                  }
2357
 
              }
2358
 
          UNIDATAEND(str2);
2359
 
          Py_DECREF(str2);
2360
 
          if(!badptr) 
2361
 
            {
2362
 
              assert(PyErr_Occurred());
2363
 
              return -1;
2364
 
            }
2365
 
        }
2366
 
      else
2367
 
        {
2368
 
          size_t lenval=strlen(val);
2369
 
          if(lenval>INT32_MAX)
2370
 
              {
2371
 
                PyErr_Format(ExcTooBig, "String object is too large - SQLite only supports up to 2GB");
2372
 
                return -1;
2373
 
              }
2374
 
          res=sqlite3_bind_text(self->statement, arg, val, (int)lenval, SQLITE_TRANSIENT);
2375
 
        }
2376
 
    }
2377
 
  else if (PyBuffer_Check(obj))
2378
 
    {
2379
 
      const char *buffer;
2380
 
      Py_ssize_t buflen;
2381
 
      if(PyObject_AsCharBuffer(obj, &buffer, &buflen))
2382
 
        return -1;
2383
 
      if (buflen>INT32_MAX)
2384
 
        {
2385
 
          PyErr_Format(ExcTooBig, "Binding object is too large - SQLite only supports up to 2GB");
2386
 
          return -1;
2387
 
        }
2388
 
      res=sqlite3_bind_blob(self->statement, arg, buffer, (int)buflen, SQLITE_TRANSIENT);
2389
 
    }
2390
 
  else 
2391
 
    {
2392
 
      PyObject *strrep=PyObject_Str(obj);
2393
 
      PyErr_Format(PyExc_TypeError, "Bad binding argument type supplied - argument #%d: %s", (int)(arg+self->bindingsoffset), strrep?PyString_AsString(strrep):"<str failed>");
2394
 
      Py_XDECREF(strrep);
2395
 
      return -1;
2396
 
    }
2397
 
  if(res!=SQLITE_OK)
2398
 
    {
2399
 
      SET_EXC(self->connection->db, res);
2400
 
      return -1;
2401
 
    }
2402
 
  if(PyErr_Occurred())
2403
 
    return -1;
2404
 
  return 0;
2405
 
}
2406
 
 
2407
 
/* internal function */
2408
 
static int
2409
 
Cursor_dobindings(Cursor *self)
2410
 
{
2411
 
  int nargs, arg, res, sz=0;
2412
 
  PyObject *obj;
2413
 
 
2414
 
  if(PyErr_Occurred()) return -1;
2415
 
 
2416
 
  assert(self->bindingsoffset>=0);
2417
 
 
2418
 
  nargs=sqlite3_bind_parameter_count(self->statement);
2419
 
 
2420
 
  /* a dictionary? */
2421
 
  if (self->bindings && PyDict_Check(self->bindings))
2422
 
    {
2423
 
      for(arg=1;arg<=nargs;arg++)
2424
 
        {
2425
 
          const char *key=sqlite3_bind_parameter_name(self->statement, arg);
2426
 
          const char *chk;
2427
 
 
2428
 
          if(!key)
2429
 
            {
2430
 
              PyErr_Format(ExcBindings, "Binding %d has no name, but you supplied a dict (which only has names).", arg-1);
2431
 
              return -1;
2432
 
            }
2433
 
 
2434
 
          assert(*key==':' || *key=='$');
2435
 
          key++; /* first char is a colon or dollar which we skip */
2436
 
 
2437
 
          for(chk=key;*chk && !((*chk)&0x80); chk++);
2438
 
          if(*chk)
2439
 
            {
2440
 
              PyObject *keyo=PyUnicode_DecodeUTF8(key, strlen(key), NULL);
2441
 
              if(!keyo) return -1;
2442
 
 
2443
 
              obj=PyDict_GetItem(self->bindings, keyo);
2444
 
              Py_DECREF(keyo);
2445
 
            }
2446
 
            else
2447
 
              obj=PyDict_GetItemString(self->bindings, key);
2448
 
 
2449
 
          if(!obj)
2450
 
            /* this is where we could error on missing keys */
2451
 
            continue;
2452
 
          if(Cursor_dobinding(self,arg,obj))
2453
 
            {
2454
 
              assert(PyErr_Occurred());
2455
 
              return -1;
2456
 
            }
2457
 
        }
2458
 
 
2459
 
      return 0;
2460
 
    }
2461
 
 
2462
 
  /* it must be a fast sequence */
2463
 
  /* verify the number of args supplied */
2464
 
  if (self->bindings)
2465
 
    sz=PySequence_Fast_GET_SIZE(self->bindings);
2466
 
  /* there is another statement after this one ... */
2467
 
  if(*self->zsqlnextpos && sz-self->bindingsoffset<nargs)
2468
 
    {
2469
 
      PyErr_Format(ExcBindings, "Incorrect number of bindings supplied.  The current statement uses %d and there are only %d left.  Current offset is %d",
2470
 
                   nargs, (self->bindings)?sz:0, (int)(self->bindingsoffset));
2471
 
      return -1;
2472
 
    }
2473
 
  /* no more statements */
2474
 
  if(!*self->zsqlnextpos && sz-self->bindingsoffset!=nargs)
2475
 
    {
2476
 
      PyErr_Format(ExcBindings, "Incorrect number of bindings supplied.  The current statement uses %d and there are %d supplied.  Current offset is %d",
2477
 
                   nargs, (self->bindings)?sz:0, (int)(self->bindingsoffset));
2478
 
      return -1;
2479
 
    }
2480
 
  
2481
 
  res=SQLITE_OK;
2482
 
 
2483
 
  /* nb sqlite starts bind args at one not zero */
2484
 
  for(arg=1;arg<=nargs;arg++)
2485
 
    {
2486
 
      obj=PySequence_Fast_GET_ITEM(self->bindings, arg-1+self->bindingsoffset);
2487
 
      if(Cursor_dobinding(self, arg, obj))
2488
 
        {
2489
 
          assert(PyErr_Occurred());
2490
 
          return -1;
2491
 
        }
2492
 
    }
2493
 
 
2494
 
  self->bindingsoffset+=nargs;
2495
 
  assert(res==0);
2496
 
  return 0;
2497
 
}
2498
 
 
2499
 
typedef struct { 
2500
 
  const char *previouszsqlpos;  /* where the begining of the statement was */
2501
 
  Py_ssize_t savedbindingsoffset;      /* where the bindings began */
2502
 
} exectrace_oldstate;
2503
 
  
2504
 
static int
2505
 
Cursor_doexectrace(Cursor *self, exectrace_oldstate *etos)
2506
 
{
2507
 
  PyObject *retval=NULL;
2508
 
  PyObject *args=NULL;
2509
 
  PyObject *sqlcmd=NULL;
2510
 
  PyObject *bindings=NULL;
2511
 
  int result;
2512
 
 
2513
 
  assert(self->exectrace);
2514
 
 
2515
 
  /* make a string of the command */
2516
 
  sqlcmd=convertutf8stringsize(etos->previouszsqlpos, self->zsqlnextpos-etos->previouszsqlpos);
2517
 
 
2518
 
  if(!sqlcmd) return -1;
2519
 
  /* now deal with the bindings */
2520
 
  if(self->bindings)
2521
 
    {
2522
 
      if(PyDict_Check(self->bindings))
2523
 
        {
2524
 
          bindings=self->bindings;
2525
 
          Py_INCREF(self->bindings);
2526
 
        }
2527
 
      else
2528
 
        {
2529
 
          bindings=PySequence_GetSlice(self->bindings, etos->savedbindingsoffset, self->bindingsoffset);
2530
 
          if(!bindings)
2531
 
            {
2532
 
              Py_DECREF(sqlcmd);
2533
 
              return -1;
2534
 
            }
2535
 
        }
2536
 
    }
2537
 
  else
2538
 
    {
2539
 
      bindings=Py_None;
2540
 
      Py_INCREF(bindings);
2541
 
    }
2542
 
  args=PyTuple_New(2);
2543
 
  if(!args)
2544
 
    {
2545
 
      Py_DECREF(sqlcmd);
2546
 
      Py_DECREF(bindings);
2547
 
      return -1;
2548
 
    }
2549
 
  PyTuple_SET_ITEM(args, 0, sqlcmd);
2550
 
  PyTuple_SET_ITEM(args, 1, bindings);
2551
 
  
2552
 
  retval=PyEval_CallObject(self->exectrace, args);
2553
 
  Py_DECREF(args);
2554
 
  if(!retval) 
2555
 
    {
2556
 
      assert(PyErr_Occurred());
2557
 
      return -1;
2558
 
    }
2559
 
  result=PyObject_IsTrue(retval);
2560
 
  Py_DECREF(retval);
2561
 
  assert (result==-1 || result==0 || result ==1);
2562
 
  if(result==-1)
2563
 
    {
2564
 
      assert(PyErr_Occurred());
2565
 
      return -1;
2566
 
    }
2567
 
  if(result)
2568
 
    return 0;
2569
 
 
2570
 
  /* callback didn't want us to continue */
2571
 
  PyErr_Format(ExcTraceAbort, "Aborted by false/null return value of exec tracer");
2572
 
  return -1;
2573
 
}
2574
 
 
2575
 
static PyObject*
2576
 
Cursor_dorowtrace(Cursor *self, PyObject *retval)
2577
 
{
2578
 
  assert(self->rowtrace);
2579
 
 
2580
 
  retval=PyEval_CallObject(self->rowtrace, retval);
2581
 
  if(!retval) return NULL;
2582
 
  
2583
 
  return retval;
2584
 
}
2585
 
 
2586
 
/* Returns a borrowed reference to self if all is ok, else NULL on error */
2587
 
static PyObject *
2588
 
Cursor_step(Cursor *self)
2589
 
{
2590
 
  int res;
2591
 
  exectrace_oldstate etos;
2592
 
 
2593
 
  if(self->status==C_DONE)
2594
 
    {
2595
 
      PyErr_Format(ExcComplete, "The statement(s) have finished or errored, so you can't keep running them");
2596
 
      return NULL;
2597
 
    }
2598
 
 
2599
 
  for(;;)
2600
 
    {
2601
 
      assert(!PyErr_Occurred());
2602
 
      Py_BEGIN_ALLOW_THREADS
2603
 
        res=sqlite3_step(self->statement);
2604
 
      Py_END_ALLOW_THREADS;
2605
 
 
2606
 
      switch(res)
2607
 
        {
2608
 
        case SQLITE_ROW:
2609
 
          self->status=C_ROW;
2610
 
          return (PyErr_Occurred())?(NULL):((PyObject*)self);
2611
 
        case SQLITE_BUSY:
2612
 
          self->status=C_BEGIN;
2613
 
          SET_EXC(self->connection->db,res);
2614
 
          return NULL;
2615
 
 
2616
 
        default:    /* no other value should happen, but we'll
2617
 
                       defensively code and treat them the same as
2618
 
                        SQLITE_ERROR */
2619
 
          /* FALLTHRU */
2620
 
        case SQLITE_ERROR:
2621
 
          /* there was an error - we need to get actual error code from sqlite3_finalize */
2622
 
          self->status=C_DONE;
2623
 
          res=resetcursor(self);  /* this will get the error code for us */
2624
 
          assert(res!=SQLITE_OK);
2625
 
          return NULL;
2626
 
 
2627
 
        case SQLITE_MISUSE:
2628
 
          /* this would be an error in apsw itself */
2629
 
          self->status=C_DONE;
2630
 
          SET_EXC(self->connection->db,res);
2631
 
          resetcursor(self);
2632
 
          return NULL;
2633
 
 
2634
 
        case SQLITE_DONE:
2635
 
          if (PyErr_Occurred())
2636
 
            {
2637
 
              self->status=C_DONE;
2638
 
              return NULL;
2639
 
            }
2640
 
          break;
2641
 
          
2642
 
        }
2643
 
      assert(res==SQLITE_DONE);
2644
 
 
2645
 
      /* done with that statement, are there any more? */
2646
 
      self->status=C_DONE;
2647
 
      if(!self->zsqlnextpos || !*self->zsqlnextpos)
2648
 
        {
2649
 
          PyObject *next;
2650
 
          if(!self->emiter)
2651
 
            {
2652
 
              /* no more so we finalize */
2653
 
              if(resetcursor(self)!=SQLITE_OK)
2654
 
                {
2655
 
                  assert(PyErr_Occurred());
2656
 
                  return NULL; /* exception */
2657
 
                }
2658
 
              return (PyObject*)self;
2659
 
            }
2660
 
          next=PyIter_Next(self->emiter);
2661
 
          if(PyErr_Occurred())
2662
 
            return NULL;
2663
 
          if(!next)
2664
 
            {
2665
 
              /* no more from executemanyiter so we finalize */
2666
 
              if(resetcursor(self)!=SQLITE_OK)
2667
 
                {
2668
 
                  assert(PyErr_Occurred());
2669
 
                  return NULL;
2670
 
                }
2671
 
              return (PyObject*)self;
2672
 
            }
2673
 
          self->zsqlnextpos=self->zsql; /* start at begining of string again */
2674
 
          /* don't need bindings from last round if emiter.next() */
2675
 
          Py_XDECREF(self->bindings);
2676
 
          self->bindings=0;
2677
 
          self->bindingsoffset=0;
2678
 
          /* verify type of next before putting in bindings */
2679
 
          if(PyDict_Check(next))
2680
 
            self->bindings=next;
2681
 
          else
2682
 
            {
2683
 
              self->bindings=PySequence_Fast(next, "You must supply a dict or a sequence");
2684
 
              if(!self->bindings)
2685
 
                {
2686
 
                  Py_DECREF(next);
2687
 
                  return NULL;
2688
 
                }
2689
 
            }
2690
 
          assert(self->bindings);
2691
 
        }
2692
 
 
2693
 
      /* finalise and go again */
2694
 
      res=sqlite3_finalize(self->statement);
2695
 
      self->statement=0;
2696
 
      SET_EXC(self->connection->db,res);
2697
 
      if (res!=SQLITE_OK)
2698
 
        {
2699
 
          assert(res!=SQLITE_BUSY); /* finalize shouldn't be returning busy, only step */
2700
 
          return NULL;
2701
 
        }
2702
 
 
2703
 
      assert(!self->statement);
2704
 
      if(self->exectrace)
2705
 
        {
2706
 
          etos.previouszsqlpos=self->zsqlnextpos;
2707
 
          etos.savedbindingsoffset=self->bindingsoffset;
2708
 
        }
2709
 
      res=sqlite3_prepare(self->connection->db, self->zsqlnextpos, -1, &self->statement, &self->zsqlnextpos);
2710
 
      SET_EXC(self->connection->db,res);
2711
 
      if (res!=SQLITE_OK)
2712
 
        {
2713
 
          assert(res!=SQLITE_BUSY); /* prepare definitely shouldn't be returning busy */
2714
 
          return NULL;
2715
 
        }
2716
 
 
2717
 
      if(self->bindings)
2718
 
        if(Cursor_dobindings(self))
2719
 
          {
2720
 
            assert(PyErr_Occurred());
2721
 
            return NULL;
2722
 
          }
2723
 
 
2724
 
      if(self->exectrace)
2725
 
        {
2726
 
          if(Cursor_doexectrace(self, &etos))
2727
 
            {
2728
 
              assert(self->status==C_DONE);
2729
 
              assert(PyErr_Occurred());
2730
 
              return NULL;
2731
 
            }
2732
 
        }
2733
 
      assert(self->status==C_DONE);
2734
 
      self->status=C_BEGIN;
2735
 
    }
2736
 
 
2737
 
  /* you can't actually get here */
2738
 
  assert(0);
2739
 
  return NULL;
2740
 
}
2741
 
 
2742
 
static PyObject *
2743
 
Cursor_execute(Cursor *self, PyObject *args)
2744
 
{
2745
 
  int res;
2746
 
  PyObject *retval=NULL;
2747
 
  exectrace_oldstate etos;
2748
 
 
2749
 
  CHECK_THREAD(self->connection, NULL);
2750
 
 
2751
 
  res=resetcursor(self);
2752
 
  if(res!=SQLITE_OK)
2753
 
    return NULL;
2754
 
  
2755
 
  assert(!self->bindings);
2756
 
 
2757
 
  if(!PyArg_ParseTuple(args, "es|O:execute(statements,bindings=())", STRENCODING, &self->zsql, &self->bindings))
2758
 
    return NULL;
2759
 
 
2760
 
  if(self->bindings)
2761
 
    {
2762
 
      if(PyDict_Check(self->bindings))
2763
 
        Py_INCREF(self->bindings);
2764
 
      else
2765
 
        {
2766
 
          self->bindings=PySequence_Fast(self->bindings, "You must supply a dict or a sequence");
2767
 
          if(!self->bindings)
2768
 
            return NULL;
2769
 
        }
2770
 
    }
2771
 
 
2772
 
  assert(!self->statement);
2773
 
  if(self->exectrace)
2774
 
    {
2775
 
      etos.previouszsqlpos=self->zsql;
2776
 
      etos.savedbindingsoffset=0;
2777
 
    }
2778
 
  res=sqlite3_prepare(self->connection->db, self->zsql, -1, &self->statement, &self->zsqlnextpos);
2779
 
  SET_EXC(self->connection->db,res);
2780
 
  if (res!=SQLITE_OK)
2781
 
    return NULL;
2782
 
  
2783
 
  self->bindingsoffset=0;
2784
 
  if(Cursor_dobindings(self))
2785
 
    {
2786
 
      assert(PyErr_Occurred());
2787
 
      return NULL;
2788
 
    }
2789
 
 
2790
 
  if(self->exectrace)
2791
 
    {
2792
 
      if(Cursor_doexectrace(self, &etos))
2793
 
        {
2794
 
          assert(PyErr_Occurred());
2795
 
          return NULL;  
2796
 
        }
2797
 
    }
2798
 
 
2799
 
  self->status=C_BEGIN;
2800
 
 
2801
 
  retval=Cursor_step(self);
2802
 
  if (!retval) 
2803
 
    {
2804
 
      assert(PyErr_Occurred());
2805
 
      return NULL;
2806
 
    }
2807
 
  Py_INCREF(retval);
2808
 
  return retval;
2809
 
}
2810
 
 
2811
 
static PyObject *
2812
 
Cursor_executemany(Cursor *self, PyObject *args)
2813
 
{
2814
 
  int res;
2815
 
  PyObject *retval=NULL;
2816
 
  PyObject *theiterable=NULL;
2817
 
  PyObject *next=NULL;
2818
 
  exectrace_oldstate etos;
2819
 
 
2820
 
  CHECK_THREAD(self->connection, NULL);
2821
 
 
2822
 
  res=resetcursor(self);
2823
 
  if(res!=SQLITE_OK)
2824
 
    return NULL;
2825
 
  
2826
 
  assert(!self->bindings);
2827
 
  assert(!self->emiter);
2828
 
  assert(!self->zsql);
2829
 
  assert(self->status=C_DONE);
2830
 
 
2831
 
  if(!PyArg_ParseTuple(args, "esO:executemany(statements, sequenceofbindings)", STRENCODING, &self->zsql, &theiterable))
2832
 
    return NULL;
2833
 
 
2834
 
  self->emiter=PyObject_GetIter(theiterable);
2835
 
  if (!self->emiter)
2836
 
    {
2837
 
      PyErr_Format(PyExc_TypeError, "2nd parameter must be iterable");
2838
 
      return NULL;
2839
 
    }
2840
 
 
2841
 
  next=PyIter_Next(self->emiter);
2842
 
  if(!next && PyErr_Occurred())
2843
 
    return NULL;
2844
 
  if(!next)
2845
 
    {
2846
 
      /* empty list */
2847
 
      Py_INCREF(self);
2848
 
      return (PyObject*)self;
2849
 
    }
2850
 
 
2851
 
  if(PyDict_Check(next))
2852
 
    self->bindings=next;
2853
 
  else
2854
 
    {
2855
 
      self->bindings=PySequence_Fast(next, "You must supply a dict or a sequence");
2856
 
      Py_DECREF(next); /* _Fast makes new reference */
2857
 
      if(!self->bindings)
2858
 
          return NULL;
2859
 
    }
2860
 
 
2861
 
  assert(!self->statement);
2862
 
  if(self->exectrace)
2863
 
    {
2864
 
      etos.previouszsqlpos=self->zsql;
2865
 
      etos.savedbindingsoffset=0;
2866
 
    }
2867
 
  res=sqlite3_prepare(self->connection->db, self->zsql, -1, &self->statement, &self->zsqlnextpos);
2868
 
  SET_EXC(self->connection->db,res);
2869
 
  if (res!=SQLITE_OK)
2870
 
    return NULL;
2871
 
  
2872
 
  self->bindingsoffset=0;
2873
 
  if(Cursor_dobindings(self))
2874
 
    {
2875
 
      assert(PyErr_Occurred());
2876
 
      return NULL;
2877
 
    }
2878
 
 
2879
 
  if(self->exectrace)
2880
 
    {
2881
 
      if(Cursor_doexectrace(self, &etos))
2882
 
        {
2883
 
          assert(PyErr_Occurred());
2884
 
          return NULL;  
2885
 
        }
2886
 
    }
2887
 
 
2888
 
  self->status=C_BEGIN;
2889
 
 
2890
 
  retval=Cursor_step(self);
2891
 
  if (!retval) 
2892
 
    {
2893
 
      assert(PyErr_Occurred());
2894
 
      return NULL;
2895
 
    }
2896
 
  Py_INCREF(retval);
2897
 
  return retval;
2898
 
}
2899
 
 
2900
 
static PyObject *
2901
 
Cursor_next(Cursor *self)
2902
 
{
2903
 
  PyObject *retval;
2904
 
  PyObject *item;
2905
 
  int numcols=-1;
2906
 
  int i;
2907
 
  int coltype;
2908
 
 
2909
 
  CHECK_THREAD(self->connection, NULL);
2910
 
 
2911
 
 again:
2912
 
  if(self->status==C_BEGIN)
2913
 
    if(!Cursor_step(self))
2914
 
      {
2915
 
        assert(PyErr_Occurred());
2916
 
        return NULL;
2917
 
      }
2918
 
  if(self->status==C_DONE)
2919
 
    return NULL;
2920
 
 
2921
 
  assert(self->status==C_ROW);
2922
 
 
2923
 
  self->status=C_BEGIN;
2924
 
  
2925
 
  /* DUPLICATE(ish) code: this is substantially similar to the code in
2926
 
     convert_value_to_pyobject.  If you fix anything here then do it
2927
 
     there as well. */
2928
 
 
2929
 
  /* return the row of data */
2930
 
  numcols=sqlite3_data_count(self->statement);
2931
 
  retval=PyTuple_New(numcols);
2932
 
  if(!retval) return NULL;
2933
 
 
2934
 
  for(i=0;i<numcols;i++)
2935
 
    {
2936
 
      coltype=sqlite3_column_type(self->statement, i);
2937
 
 
2938
 
      switch(coltype)
2939
 
        {
2940
 
        case SQLITE_INTEGER:
2941
 
          {
2942
 
            long long vint=sqlite3_column_int64(self->statement, i);
2943
 
            if(vint<INT32_MIN || vint>INT32_MAX)
2944
 
              item=PyLong_FromLongLong(vint);
2945
 
            else
2946
 
              item=PyInt_FromLong((long)vint);
2947
 
          }
2948
 
          break;
2949
 
 
2950
 
        case SQLITE_FLOAT:
2951
 
          item=PyFloat_FromDouble(sqlite3_column_double(self->statement, i));
2952
 
          break;
2953
 
                
2954
 
        case SQLITE_TEXT:
2955
 
          item=convertutf8string(sqlite3_column_text(self->statement, i));
2956
 
          break;
2957
 
 
2958
 
        case SQLITE_NULL:
2959
 
          Py_INCREF(Py_None);
2960
 
          item=Py_None;
2961
 
          break;
2962
 
 
2963
 
        case SQLITE_BLOB:
2964
 
          {
2965
 
            Py_ssize_t sz=sqlite3_column_bytes(self->statement, i);
2966
 
            item=PyBuffer_New(sz);
2967
 
            if(item)
2968
 
              {
2969
 
                void *buffy=0;
2970
 
                Py_ssize_t sz2=sz;
2971
 
                if(!PyObject_AsWriteBuffer(item, &buffy, &sz2))
2972
 
                  memcpy(buffy, sqlite3_column_blob(self->statement, i), sz);
2973
 
                else
2974
 
                  {
2975
 
                    Py_DECREF(item);
2976
 
                    item=NULL;
2977
 
                  }
2978
 
              }
2979
 
            break;
2980
 
          }
2981
 
 
2982
 
        default:
2983
 
          PyErr_Format(APSWException, "Unknown sqlite column type %d!", coltype);
2984
 
          item=NULL;
2985
 
 
2986
 
        }
2987
 
 
2988
 
      if(!item) return NULL;
2989
 
      PyTuple_SET_ITEM(retval, i, item);
2990
 
    }
2991
 
  if(self->rowtrace)
2992
 
    {
2993
 
      PyObject *r2=Cursor_dorowtrace(self, retval);
2994
 
      Py_DECREF(retval);
2995
 
      if(!r2) return NULL;
2996
 
      if (r2==Py_None)
2997
 
        {
2998
 
          Py_DECREF(r2);
2999
 
          goto again;
3000
 
        }
3001
 
      return r2;
3002
 
    }
3003
 
  return retval;
3004
 
}
3005
 
 
3006
 
static PyObject *
3007
 
Cursor_iter(Cursor *self)
3008
 
{
3009
 
  CHECK_THREAD(self->connection, NULL);
3010
 
 
3011
 
  Py_INCREF(self);
3012
 
  return (PyObject*)self;
3013
 
}
3014
 
 
3015
 
static PyObject *
3016
 
Cursor_setexectrace(Cursor *self, PyObject *func)
3017
 
{
3018
 
  CHECK_THREAD(self->connection, NULL);
3019
 
 
3020
 
  if(func!=Py_None && !PyCallable_Check(func))
3021
 
    {
3022
 
      PyErr_SetString(PyExc_TypeError, "parameter must be callable");
3023
 
      return NULL;
3024
 
    }
3025
 
 
3026
 
  if(func!=Py_None)
3027
 
    Py_INCREF(func);
3028
 
 
3029
 
  Py_XDECREF(self->exectrace);
3030
 
  self->exectrace=(func!=Py_None)?func:NULL;
3031
 
 
3032
 
  return Py_BuildValue("");
3033
 
}
3034
 
 
3035
 
static PyObject *
3036
 
Cursor_setrowtrace(Cursor *self, PyObject *func)
3037
 
{
3038
 
  CHECK_THREAD(self->connection, NULL);
3039
 
 
3040
 
  if(func!=Py_None && !PyCallable_Check(func))
3041
 
    {
3042
 
      PyErr_SetString(PyExc_TypeError, "parameter must be callable");
3043
 
      return NULL;
3044
 
    }
3045
 
 
3046
 
  if(func!=Py_None)
3047
 
    Py_INCREF(func);
3048
 
 
3049
 
  Py_XDECREF(self->rowtrace);
3050
 
  self->rowtrace=(func!=Py_None)?func:NULL;
3051
 
 
3052
 
  return Py_BuildValue("");
3053
 
}
3054
 
 
3055
 
static PyObject *
3056
 
Cursor_getexectrace(Cursor *self)
3057
 
{
3058
 
  PyObject *ret=(self->exectrace)?(self->exectrace):Py_None;
3059
 
  Py_INCREF(ret);
3060
 
  return ret;
3061
 
}
3062
 
 
3063
 
static PyObject *
3064
 
Cursor_getrowtrace(Cursor *self)
3065
 
{
3066
 
  PyObject *ret=(self->rowtrace)?(self->rowtrace):Py_None;
3067
 
  Py_INCREF(ret);
3068
 
  return ret;
3069
 
}
3070
 
 
3071
 
static PyObject *
3072
 
Cursor_getconnection(Cursor *self)
3073
 
{
3074
 
  CHECK_THREAD(self->connection, NULL);
3075
 
 
3076
 
  Py_INCREF(self->connection);
3077
 
  return (PyObject*)self->connection;
3078
 
}
3079
 
 
3080
 
static PyMethodDef Cursor_methods[] = {
3081
 
  {"execute", (PyCFunction)Cursor_execute, METH_VARARGS,
3082
 
   "Executes one or more statements" },
3083
 
  {"executemany", (PyCFunction)Cursor_executemany, METH_VARARGS,
3084
 
   "Repeatedly executes statements on sequence" },
3085
 
  {"next", (PyCFunction)Cursor_next, METH_NOARGS,
3086
 
   "Returns next row returned from query"},
3087
 
  {"setexectrace", (PyCFunction)Cursor_setexectrace, METH_O,
3088
 
   "Installs a function called for every statement executed"},
3089
 
  {"setrowtrace", (PyCFunction)Cursor_setrowtrace, METH_O,
3090
 
   "Installs a function called for every row returned"},
3091
 
  {"getexectrace", (PyCFunction)Cursor_getexectrace, METH_NOARGS,
3092
 
   "Returns the current exec tracer function"},
3093
 
  {"getrowtrace", (PyCFunction)Cursor_getrowtrace, METH_NOARGS,
3094
 
   "Returns the current row tracer function"},
3095
 
  {"getrowtrace", (PyCFunction)Cursor_getrowtrace, METH_NOARGS,
3096
 
   "Returns the current row tracer function"},
3097
 
  {"getconnection", (PyCFunction)Cursor_getconnection, METH_NOARGS,
3098
 
   "Returns the connection object for this cursor"},
3099
 
  {"getdescription", (PyCFunction)Cursor_getdescription, METH_NOARGS,
3100
 
   "Returns the description for the current row"},
3101
 
  {NULL}  /* Sentinel */
3102
 
};
3103
 
 
3104
 
 
3105
 
static PyTypeObject CursorType = {
3106
 
    PyObject_HEAD_INIT(NULL)
3107
 
    0,                         /*ob_size*/
3108
 
    "apsw.Cursor",             /*tp_name*/
3109
 
    sizeof(Cursor),            /*tp_basicsize*/
3110
 
    0,                         /*tp_itemsize*/
3111
 
    (destructor)Cursor_dealloc, /*tp_dealloc*/
3112
 
    0,                         /*tp_print*/
3113
 
    0,                         /*tp_getattr*/
3114
 
    0,                         /*tp_setattr*/
3115
 
    0,                         /*tp_compare*/
3116
 
    0,                         /*tp_repr*/
3117
 
    0,                         /*tp_as_number*/
3118
 
    0,                         /*tp_as_sequence*/
3119
 
    0,                         /*tp_as_mapping*/
3120
 
    0,                         /*tp_hash */
3121
 
    0,                         /*tp_call*/
3122
 
    0,                         /*tp_str*/
3123
 
    0,                         /*tp_getattro*/
3124
 
    0,                         /*tp_setattro*/
3125
 
    0,                         /*tp_as_buffer*/
3126
 
    Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_ITER , /*tp_flags*/
3127
 
    "Cursor object",           /* tp_doc */
3128
 
    0,                         /* tp_traverse */
3129
 
    0,                         /* tp_clear */
3130
 
    0,                         /* tp_richcompare */
3131
 
    0,                         /* tp_weaklistoffset */
3132
 
    (getiterfunc)Cursor_iter,  /* tp_iter */
3133
 
    (iternextfunc)Cursor_next, /* tp_iternext */
3134
 
    Cursor_methods,            /* tp_methods */
3135
 
    0,                         /* tp_members */
3136
 
    0,                         /* tp_getset */
3137
 
    0,                         /* tp_base */
3138
 
    0,                         /* tp_dict */
3139
 
    0,                         /* tp_descr_get */
3140
 
    0,                         /* tp_descr_set */
3141
 
    0,                         /* tp_dictoffset */
3142
 
    (initproc)Cursor_init,     /* tp_init */
3143
 
    0,                         /* tp_alloc */
3144
 
    Cursor_new,                /* tp_new */
3145
 
};
3146
 
 
3147
 
/* MODULE METHODS */
3148
 
static PyObject *
3149
 
getsqliteversion(void)
3150
 
{
3151
 
  return PyString_FromString(sqlite3_libversion());
3152
 
}
3153
 
 
3154
 
static PyObject *
3155
 
getapswversion(void)
3156
 
{
3157
 
  return PyString_FromString(APSW_VERSION);
3158
 
}
3159
 
 
3160
 
static PyObject *
3161
 
enablesharedcache(PyObject *self, PyObject *args)
3162
 
{
3163
 
  int setting,res;
3164
 
  if(!PyArg_ParseTuple(args, "i:enablesharedcache(boolean)", &setting))
3165
 
    return NULL;
3166
 
 
3167
 
  res=sqlite3_enable_shared_cache(setting);
3168
 
  SET_EXC(NULL, res);
3169
 
 
3170
 
  if(res!=SQLITE_OK)
3171
 
    return NULL;
3172
 
 
3173
 
  return Py_BuildValue("");
3174
 
}
3175
 
 
3176
 
static PyMethodDef module_methods[] = {
3177
 
  {"sqlitelibversion", (PyCFunction)getsqliteversion, METH_NOARGS,
3178
 
   "Return the version of the SQLite library"},
3179
 
  {"apswversion", (PyCFunction)getapswversion, METH_NOARGS,
3180
 
   "Return the version of the APSW wrapper"},
3181
 
  {"enablesharedcache", (PyCFunction)enablesharedcache, METH_VARARGS,
3182
 
   "Sets shared cache semantics for this thread"},
3183
 
 
3184
 
    {NULL}  /* Sentinel */
3185
 
};
3186
 
 
3187
 
 
3188
 
 
3189
 
#ifndef PyMODINIT_FUNC  /* declarations for DLL import/export */
3190
 
#define PyMODINIT_FUNC void
3191
 
#endif
3192
 
PyMODINIT_FUNC
3193
 
initapsw(void) 
3194
 
{
3195
 
    PyObject* m;
3196
 
 
3197
 
    assert(sizeof(int)==4);             /* we expect 32 bit ints */
3198
 
    assert(sizeof(long long)==8);             /* we expect 64 bit long long */
3199
 
 
3200
 
    if (PyType_Ready(&ConnectionType) < 0)
3201
 
        return;
3202
 
 
3203
 
    if (PyType_Ready(&CursorType) < 0)
3204
 
        return;
3205
 
 
3206
 
    /* ensure threads are available */
3207
 
    PyEval_InitThreads();
3208
 
 
3209
 
 
3210
 
    m = Py_InitModule3("apsw", module_methods,
3211
 
                       "Another Python SQLite Wrapper.");
3212
 
 
3213
 
    if (m == NULL)
3214
 
      return;
3215
 
 
3216
 
    if(init_exceptions(m))
3217
 
      {
3218
 
        fprintf(stderr, "init_exceptions failed\n");
3219
 
        return;
3220
 
      }
3221
 
 
3222
 
    Py_INCREF(&ConnectionType);
3223
 
    PyModule_AddObject(m, "Connection", (PyObject *)&ConnectionType);
3224
 
 
3225
 
    /* we don't add cursor to the module since users shouldn't be able to instantiate them directly */
3226
 
 
3227
 
    /* add in some constants */
3228
 
 
3229
 
#define ADDINT(v) PyModule_AddObject(m, #v, Py_BuildValue("i", v));
3230
 
 
3231
 
    ADDINT(SQLITE_DENY);
3232
 
    ADDINT(SQLITE_IGNORE);
3233
 
    ADDINT(SQLITE_OK);
3234
 
 
3235
 
    /* authorizer functions */
3236
 
    ADDINT(SQLITE_CREATE_INDEX);
3237
 
    ADDINT(SQLITE_CREATE_TABLE);
3238
 
    ADDINT(SQLITE_CREATE_TEMP_INDEX);
3239
 
    ADDINT(SQLITE_CREATE_TEMP_TABLE);
3240
 
    ADDINT(SQLITE_CREATE_TEMP_TRIGGER);
3241
 
    ADDINT(SQLITE_CREATE_TEMP_VIEW);
3242
 
    ADDINT(SQLITE_CREATE_TRIGGER);
3243
 
    ADDINT(SQLITE_CREATE_VIEW);
3244
 
    ADDINT(SQLITE_DELETE);
3245
 
    ADDINT(SQLITE_DROP_INDEX);
3246
 
    ADDINT(SQLITE_DROP_TABLE);
3247
 
    ADDINT(SQLITE_DROP_TEMP_INDEX);
3248
 
    ADDINT(SQLITE_DROP_TEMP_TABLE);
3249
 
    ADDINT(SQLITE_DROP_TEMP_TRIGGER);
3250
 
    ADDINT(SQLITE_DROP_TEMP_VIEW);
3251
 
    ADDINT(SQLITE_DROP_TRIGGER);
3252
 
    ADDINT(SQLITE_DROP_VIEW);
3253
 
    ADDINT(SQLITE_INSERT);
3254
 
    ADDINT(SQLITE_PRAGMA);
3255
 
    ADDINT(SQLITE_READ);
3256
 
    ADDINT(SQLITE_SELECT);
3257
 
    ADDINT(SQLITE_TRANSACTION);
3258
 
    ADDINT(SQLITE_UPDATE);
3259
 
    ADDINT(SQLITE_ATTACH);
3260
 
    ADDINT(SQLITE_DETACH);
3261
 
    /* these constants were introduced in SQLite 3.1.3 */
3262
 
#ifdef SQLITE_ALTER_TABLE
3263
 
    ADDINT(SQLITE_ALTER_TABLE);
3264
 
#endif
3265
 
#ifdef SQLITE_REINDEX
3266
 
    ADDINT(SQLITE_REINDEX);
3267
 
#endif
3268
 
    /* in 3.2 */
3269
 
#ifdef SQLITE_COPY
3270
 
    ADDINT(SQLITE_COPY);
3271
 
#endif
3272
 
#ifdef SQLITE_ANALYZE
3273
 
    ADDINT(SQLITE_ANALYZE);
3274
 
#endif
3275
 
}
3276
 
 
 
1
/*
 
2
  Another Python Sqlite Wrapper
 
3
 
 
4
  This wrapper aims to be the minimum necessary layer over SQLite 3
 
5
  itself.
 
6
 
 
7
  It assumes we are running as 32 bit int with a 64 bit long long type
 
8
  available.
 
9
 
 
10
  Copyright (C) 2004-2007 Roger Binns <rogerb@rogerbinns.com>
 
11
 
 
12
  This software is provided 'as-is', without any express or implied
 
13
  warranty.  In no event will the authors be held liable for any
 
14
  damages arising from the use of this software.
 
15
 
 
16
  Permission is granted to anyone to use this software for any
 
17
  purpose, including commercial applications, and to alter it and
 
18
  redistribute it freely, subject to the following restrictions:
 
19
 
 
20
  1. The origin of this software must not be misrepresented; you must
 
21
     not claim that you wrote the original software. If you use this
 
22
     software in a product, an acknowledgment in the product
 
23
     documentation would be appreciated but is not required.
 
24
 
 
25
  2. Altered source versions must be plainly marked as such, and must
 
26
     not be misrepresented as being the original software.
 
27
 
 
28
  3. This notice may not be removed or altered from any source
 
29
     distribution.
 
30
 
 
31
*/
 
32
 
 
33
/* system headers */
 
34
#include <assert.h>
 
35
 
 
36
/* Get the version number */
 
37
#include "apswversion.h"
 
38
 
 
39
/* Python headers */
 
40
#include <Python.h>
 
41
#include <pythread.h>
 
42
#include "structmember.h"
 
43
 
 
44
/* Python 2.5 compatibility when size_t types become 64 bit.
 
45
   SQLite3 is limited to 32 bit sizes even on a 64 bit machine. */
 
46
#if PY_VERSION_HEX < 0x02050000
 
47
typedef int Py_ssize_t;
 
48
#endif
 
49
 
 
50
/* A module to augment tracebacks */
 
51
#include "traceback.c"
 
52
 
 
53
/* A list of pointers (used by Connection to keep track of Cursors) */
 
54
#include "pointerlist.c"
 
55
 
 
56
/* SQLite 3 headers */
 
57
#include "sqlite3.h"
 
58
 
 
59
#if SQLITE_VERSION_NUMBER < 3003010
 
60
#error Your SQLite version is too old.  It must be at least 3.3.10
 
61
#endif
 
62
 
 
63
/* Prepared statement caching */
 
64
/* #define SCSTATS */
 
65
#define STATEMENTCACHE_LINKAGE static
 
66
#include "statementcache.c"
 
67
 
 
68
/* used to decide if we will use int or long long */
 
69
#define APSW_INT32_MIN (-2147483647-1)
 
70
#define APSW_INT32_MAX 2147483647
 
71
 
 
72
/* The module object */
 
73
PyObject *apswmodule;
 
74
 
 
75
/* The encoding we use with SQLite.  SQLite supports either utf8 or 16
 
76
   bit unicode (host byte order).  If the latter is used then all
 
77
   functions have "16" appended to their name.  The encoding used also
 
78
   affects how strings are stored in the database.  We use utf8 since
 
79
   it is more space efficient, and Python can't make its mind up about
 
80
   Unicode (it uses 16 or 32 bit unichars and often likes to use Byte
 
81
   Order Markers). */
 
82
#define STRENCODING "utf_8"
 
83
 
 
84
/* Some macros used for frequent operations */
 
85
 
 
86
#define CHECK_THREAD(x,e)                                                \
 
87
  { if(x->thread_ident!=PyThread_get_thread_ident())                                                                                 \
 
88
      {    /* raise exception if we aren't already in one */                                                                         \
 
89
           if (!PyErr_Occurred())                                                                                                    \
 
90
             PyErr_Format(ExcThreadingViolation, "All SQLite objects created in a thread can only be used in that same thread.  "    \
 
91
                         "The object was created in thread id %d and this is %d",                                                    \
 
92
                         (int)(x->thread_ident), (int)(PyThread_get_thread_ident()));                                                \
 
93
           return e;                                                                                                                 \
 
94
      }                                                                                                                              \
 
95
  }
 
96
 
 
97
#define CHECK_CLOSED(connection,e) \
 
98
{ if(!connection->db) { PyErr_Format(ExcConnectionClosed, "The connection has been closed"); return e; } }
 
99
 
 
100
/* EXCEPTION TYPES */
 
101
 
 
102
static PyObject *APSWException;  /* root exception class */
 
103
static PyObject *ExcThreadingViolation; /* thread misuse */
 
104
static PyObject *ExcIncomplete;  /* didn't finish previous query */
 
105
static PyObject *ExcBindings;  /* wrong number of bindings */
 
106
static PyObject *ExcComplete;  /* query is finished */
 
107
static PyObject *ExcTraceAbort; /* aborted by exectrace */
 
108
static PyObject *ExcTooBig; /* object is too large for SQLite */
 
109
static PyObject *ExcExtensionLoading; /* error loading extension */
 
110
static PyObject *ExcConnectionNotClosed; /* connection wasn't closed when destructor called */
 
111
static PyObject *ExcConnectionClosed; /* connection was closed when function called */
 
112
 
 
113
static struct { int code; const char *name; PyObject *cls;}
 
114
exc_descriptors[]=
 
115
  {
 
116
    /* Generic Errors */
 
117
    {SQLITE_ERROR,    "SQL", NULL},       
 
118
    {SQLITE_MISMATCH, "Mismatch", NULL},
 
119
 
 
120
    /* Internal Errors */
 
121
    {SQLITE_INTERNAL, "Internal", NULL},  /* NOT USED */
 
122
    {SQLITE_PROTOCOL, "Protocol", NULL},
 
123
    {SQLITE_MISUSE,   "Misuse", NULL},
 
124
    {SQLITE_RANGE,    "Range", NULL},
 
125
 
 
126
    /* permissions etc */
 
127
    {SQLITE_PERM,     "Permissions", NULL},
 
128
    {SQLITE_READONLY, "ReadOnly", NULL},
 
129
    {SQLITE_CANTOPEN, "CantOpen", NULL},
 
130
    {SQLITE_AUTH,     "Auth", NULL},
 
131
 
 
132
    /* abort/busy/etc */
 
133
    {SQLITE_ABORT,    "Abort", NULL},
 
134
    {SQLITE_BUSY,     "Busy", NULL},
 
135
    {SQLITE_LOCKED,   "Locked", NULL},
 
136
    {SQLITE_INTERRUPT,"Interrupt", NULL},
 
137
    {SQLITE_SCHEMA,   "SchemaChange", NULL}, 
 
138
    {SQLITE_CONSTRAINT, "Constraint", NULL},
 
139
 
 
140
    /* memory/disk/corrupt etc */
 
141
    {SQLITE_NOMEM,    "NoMem", NULL},
 
142
    {SQLITE_IOERR,    "IO", NULL},
 
143
    {SQLITE_CORRUPT,  "Corrupt", NULL},
 
144
    {SQLITE_FULL,     "Full", NULL},
 
145
    /*  {SQLITE_TOOBIG,   "TooBig"},      NOT USED by SQLite any more but there is an apsw equivalent with the same name*/
 
146
    {SQLITE_NOLFS,    "NoLFS", NULL},
 
147
    {SQLITE_EMPTY,    "Empty", NULL},
 
148
    {SQLITE_FORMAT,   "Format", NULL},
 
149
    {SQLITE_NOTADB,   "NotADB", NULL},
 
150
 
 
151
    {-1, 0, 0}
 
152
  };
 
153
 
 
154
 
 
155
/* EXCEPTION CODE */
 
156
 
 
157
static int init_exceptions(PyObject *m)
 
158
{
 
159
  char buffy[100]; /* more than enough for anyone :-) */
 
160
  int i;
 
161
  PyObject *obj;
 
162
 
 
163
  /* PyModule_AddObject uses borrowed reference so we incref whatever
 
164
     we give to it, so we still have a copy to use */
 
165
 
 
166
  /* custom ones first */
 
167
 
 
168
  APSWException=PyErr_NewException("apsw.Error", NULL, NULL);
 
169
  if(!APSWException) return -1;
 
170
  Py_INCREF(APSWException);
 
171
  if(PyModule_AddObject(m, "Error", (PyObject *)APSWException))
 
172
    return -1;
 
173
 
 
174
#define EXC(varname,name) \
 
175
  varname=PyErr_NewException("apsw." name, APSWException, NULL);  \
 
176
  if(!varname) return -1;                                          \
 
177
  Py_INCREF(varname);                                              \
 
178
  if(PyModule_AddObject(m, name, (PyObject *)varname))            \
 
179
    return -1;
 
180
 
 
181
  EXC(ExcThreadingViolation, "ThreadingViolationError");
 
182
  EXC(ExcIncomplete, "IncompleteExecutionError");
 
183
  EXC(ExcBindings, "BindingsError");
 
184
  EXC(ExcComplete, "ExecutionCompleteError");
 
185
  EXC(ExcTraceAbort, "ExecTraceAbort");
 
186
  EXC(ExcTooBig, "TooBigError");
 
187
  EXC(ExcExtensionLoading, "ExtensionLoadingError");
 
188
  EXC(ExcConnectionNotClosed, "ConnectionNotClosedError");
 
189
  EXC(ExcConnectionClosed, "ConnectionClosedError");
 
190
 
 
191
#undef EXC
 
192
 
 
193
  /* all the ones corresponding to SQLITE error codes */
 
194
  for(i=0;exc_descriptors[i].name;i++)
 
195
    {
 
196
      sprintf(buffy, "apsw.%sError", exc_descriptors[i].name);
 
197
      obj=PyErr_NewException(buffy, APSWException, NULL);
 
198
      if(!obj) return -1;
 
199
      Py_INCREF(obj);
 
200
      exc_descriptors[i].cls=obj;
 
201
      sprintf(buffy, "%sError", exc_descriptors[i].name);
 
202
      if(PyModule_AddObject(m, buffy, obj))
 
203
        return -1;
 
204
    }
 
205
  
 
206
  return 0;
 
207
}
 
208
 
 
209
static void make_exception(int res, sqlite3 *db)
 
210
{
 
211
  int i;
 
212
  
 
213
  for(i=0;exc_descriptors[i].name;i++)
 
214
    if (exc_descriptors[i].code==(res&0xff))
 
215
      {
 
216
        PyObject *etype, *eval, *etb;
 
217
        assert(exc_descriptors[i].cls);
 
218
        PyErr_Format(exc_descriptors[i].cls, "%sError: %s", exc_descriptors[i].name, db?(sqlite3_errmsg(db)):"error");
 
219
        PyErr_Fetch(&etype, &eval, &etb);
 
220
        PyErr_NormalizeException(&etype, &eval, &etb);
 
221
        PyObject_SetAttrString(eval, "result", Py_BuildValue("i", res&0xff));
 
222
        PyObject_SetAttrString(eval, "extendedresult", Py_BuildValue("i", res));
 
223
        PyErr_Restore(etype, eval, etb);
 
224
        assert(PyErr_Occurred());
 
225
        return;
 
226
      }
 
227
 
 
228
  /* this line should only be reached if SQLite returns an error code not in the main list */
 
229
  PyErr_Format(APSWException, "Error %d: %s", res, db?(sqlite3_errmsg(db)):"error");  
 
230
}
 
231
 
 
232
/* If res indicates an SQLite error then do all the exception creation
 
233
 work.  We don't overwrite earlier exceptions hence the PyErr_Occurred
 
234
 check */
 
235
#define SET_EXC(db,res)  { if(res != SQLITE_OK && !PyErr_Occurred()) make_exception(res,db); }
 
236
 
 
237
 
 
238
/* The default Python PyErr_WriteUnraiseable is almost useless.  It
 
239
   only prints the str() of the exception and the str() of the object
 
240
   passed in.  This gives the developer no clue whatsoever where in
 
241
   the code it is happening.  It also does funky things to the passed
 
242
   in object which can cause the destructor to fire twice.
 
243
   Consequently we use our version here.  It makes a traceback if
 
244
   necessary, invokes sys.excepthook and if that fails then
 
245
   PyErr_Display. When we return, any error will be cleared. */
 
246
static void 
 
247
apsw_write_unraiseable(void)
 
248
{
 
249
  PyObject *err_type=NULL, *err_value=NULL, *err_traceback=NULL;
 
250
  PyObject *excepthook=NULL;
 
251
  PyObject *args=NULL;
 
252
  PyObject *result=NULL;
 
253
  
 
254
  PyErr_Fetch(&err_type, &err_value, &err_traceback);
 
255
  PyErr_NormalizeException(&err_type, &err_value, &err_traceback);
 
256
 
 
257
  /* err_traceback is normally NULL, so lets fake one */
 
258
  if(!err_traceback)
 
259
    {
 
260
      PyObject *e2t=NULL, *e2v=NULL, *e2tb=NULL;
 
261
      PyFrameObject *frame = PyThreadState_GET()->frame;
 
262
      while(frame)
 
263
        {
 
264
          PyTraceBack_Here(frame);
 
265
          frame=frame->f_back;
 
266
        }
 
267
      PyErr_Fetch(&e2t, &e2v, &e2tb);
 
268
      Py_XDECREF(e2t);
 
269
      Py_XDECREF(e2v);
 
270
      err_traceback=e2tb;
 
271
    }
 
272
 
 
273
  excepthook=PySys_GetObject("excepthook");
 
274
  if(excepthook)
 
275
    args=Py_BuildValue("(OOO)", err_type?err_type:Py_None, err_value?err_value:Py_None, err_traceback?err_traceback:Py_None);
 
276
  if(excepthook && args)
 
277
    result=PyEval_CallObject(excepthook, args);
 
278
  if(!excepthook || !args || !result)
 
279
    PyErr_Display(err_type, err_value, err_traceback);
 
280
 
 
281
  /* excepthook is a borrowed reference */
 
282
  Py_XDECREF(result);
 
283
  Py_XDECREF(args);
 
284
  Py_XDECREF(err_traceback);
 
285
  Py_XDECREF(err_value);
 
286
  Py_XDECREF(err_type);
 
287
  PyErr_Clear();
 
288
}
 
289
 
 
290
/* Turns the current Python exception into an SQLite error code and
 
291
   stores the string in the errmsg field (if not NULL).  The errmsg
 
292
   field is expected to belong to sqlite and hence uses sqlite
 
293
   semantics/ownership - for example see the pzErr parameter to
 
294
   xCreate */
 
295
 
 
296
static int
 
297
MakeSqliteMsgFromPyException(char **errmsg)
 
298
{
 
299
  int res=SQLITE_ERROR;
 
300
  PyObject *str=NULL;
 
301
  PyObject *etype=NULL, *evalue=NULL, *etraceback=NULL;
 
302
 
 
303
  assert(PyErr_Occurred());
 
304
  if(PyErr_Occurred())
 
305
    {
 
306
      /* find out if the exception corresponds to an apsw exception descriptor */
 
307
      int i;
 
308
      for(i=0;exc_descriptors[i].code!=-1;i++)
 
309
        if(PyErr_ExceptionMatches(exc_descriptors[i].cls))
 
310
        {
 
311
          res=exc_descriptors[i].code;
 
312
          break;
 
313
        }
 
314
    }
 
315
 
 
316
  if(errmsg)
 
317
    {
 
318
      /* I just want a string of the error! */
 
319
      
 
320
      PyErr_Fetch(&etype, &evalue, &etraceback);
 
321
      if(!str && evalue)
 
322
        str=PyObject_Str(evalue);
 
323
      if(!str && etype)
 
324
        str=PyObject_Str(etype);
 
325
      if(!str)
 
326
        str=PyString_FromString("python exception with no information");
 
327
      if(etype)
 
328
        PyErr_Restore(etype, evalue, etraceback);
 
329
  
 
330
      if(*errmsg)
 
331
        sqlite3_free(*errmsg);
 
332
      *errmsg=sqlite3_mprintf("%s",PyString_AsString(str));
 
333
 
 
334
      Py_XDECREF(str);
 
335
    }
 
336
 
 
337
  return res;
 
338
}
 
339
 
 
340
/* CALLBACK INFO */
 
341
 
 
342
/* details of a registered function passed as user data to sqlite3_create_function */
 
343
typedef struct _funccbinfo 
 
344
{
 
345
  struct _funccbinfo *next;       /* we use a linked list */
 
346
  char *name;                     /* ascii function name which we uppercased */
 
347
  PyObject *scalarfunc;           /* the function to call for stepping */
 
348
  PyObject *aggregatefactory;     /* factory for aggregate functions */
 
349
} funccbinfo;
 
350
 
 
351
/* a particular aggregate function instance used as sqlite3_aggregate_context */
 
352
typedef struct _aggregatefunctioncontext 
 
353
{
 
354
  PyObject *aggvalue;             /* the aggregation value passed as first parameter */
 
355
  PyObject *stepfunc;             /* step function */
 
356
  PyObject *finalfunc;            /* final function */
 
357
} aggregatefunctioncontext;
 
358
 
 
359
static funccbinfo *freefunccbinfo(funccbinfo *);
 
360
 
 
361
typedef struct _collationcbinfo
 
362
{
 
363
  struct _collationcbinfo *next;  /* we use a linked list */
 
364
  char *name;                     /* ascii collation name which we uppercased */
 
365
  PyObject *func;                 /* the actual function to call */
 
366
} collationcbinfo;
 
367
  
 
368
static collationcbinfo *freecollationcbinfo(collationcbinfo *);
 
369
 
 
370
typedef struct Connection Connection; /* forward declaration */
 
371
 
 
372
typedef struct _vtableinfo
 
373
{
 
374
  struct _vtableinfo *next;       /* we use a linked list */
 
375
  char *name;                     /* module name */
 
376
  PyObject *datasource;           /* object with create/connect methods */
 
377
  Connection *connection;         /* the Connection this is registered against so we don't
 
378
                                     have to have a global table mapping sqlite3_db* to
 
379
                                     Connection* */
 
380
} vtableinfo;
 
381
 
 
382
/* forward declarations */
 
383
static vtableinfo *freevtableinfo(vtableinfo *);
 
384
 
 
385
/* CONNECTION TYPE */
 
386
 
 
387
struct Connection { 
 
388
  PyObject_HEAD
 
389
  sqlite3 *db;                    /* the actual database connection */
 
390
  const char *filename;           /* utf8 filename of the database */
 
391
  int co_linenumber;              /* line number of allocation */
 
392
  PyObject *co_filename;          /* filename of allocation */
 
393
  long thread_ident;              /* which thread we were made in */
 
394
 
 
395
  pointerlist cursors;            /* tracking cursors */
 
396
  StatementCache *stmtcache;      /* prepared statement cache */
 
397
 
 
398
  funccbinfo *functions;          /* linked list of registered functions */
 
399
  collationcbinfo *collations;    /* linked list of registered collations */
 
400
  vtableinfo *vtables;            /* linked list of registered vtables */
 
401
 
 
402
  /* registered hooks/handlers (NULL or callable) */
 
403
  PyObject *busyhandler;     
 
404
  PyObject *rollbackhook;
 
405
  PyObject *profile;
 
406
  PyObject *updatehook;
 
407
  PyObject *commithook;           
 
408
  PyObject *progresshandler;      
 
409
  PyObject *authorizer;
 
410
};
 
411
 
 
412
static PyTypeObject ConnectionType;
 
413
 
 
414
/* CURSOR TYPE */
 
415
 
 
416
typedef struct {
 
417
  PyObject_HEAD
 
418
  Connection *connection;          /* pointer to parent connection */
 
419
  sqlite3_stmt *statement;         /* current compiled statement */
 
420
 
 
421
  /* see sqlite3_prepare_v2 for the origin of these */
 
422
  const char *zsql;               /* current sqlstatement (which may include multiple statements) */
 
423
  const char *zsqlnextpos;        /* the next statement to execute (or NULL if no more) */
 
424
 
 
425
  /* what state we are in */
 
426
  enum { C_BEGIN, C_ROW, C_DONE } status;
 
427
 
 
428
  /* bindings for query */
 
429
  PyObject *bindings;             /* dict or sequence */
 
430
  Py_ssize_t bindingsoffset;             /* for sequence tracks how far along we are when dealing with multiple statements */
 
431
 
 
432
  /* iterator for executemany */
 
433
  PyObject *emiter;
 
434
 
 
435
  /* tracing functions */
 
436
  PyObject *exectrace;
 
437
  PyObject *rowtrace;
 
438
  
 
439
} Cursor;
 
440
 
 
441
static PyTypeObject CursorType;
 
442
 
 
443
/* forward declarations */
 
444
static PyObject *Cursor_close(Cursor *self, PyObject *args);
 
445
 
 
446
 
 
447
/* CONVENIENCE FUNCTIONS */
 
448
 
 
449
/* Convert a NULL terminated UTF-8 string into a Python object.  None
 
450
   is returned if NULL is passed in. */
 
451
static PyObject *
 
452
convertutf8string(const char *str)
 
453
{
 
454
  if(!str)
 
455
    {
 
456
      Py_INCREF(Py_None);
 
457
      return Py_None;
 
458
    }
 
459
 
 
460
  /* new behaviour in 3.3.8 - always return unicode strings */
 
461
  return PyUnicode_DecodeUTF8(str, strlen(str), NULL);
 
462
}
 
463
 
 
464
/* Convert a pointer and size UTF-8 string into a Python object.
 
465
   Pointer must be non-NULL. */
 
466
static PyObject *
 
467
convertutf8stringsize(const char *str, Py_ssize_t size)
 
468
{
 
469
  assert(str);
 
470
  assert(size>=0);
 
471
  
 
472
  /* new behaviour in 3.3.8 - always return Unicode strings */
 
473
  return PyUnicode_DecodeUTF8(str, size, NULL);
 
474
}
 
475
 
 
476
/* Returns a PyString encoded in UTF8 - new reference.
 
477
   Use PyString_AsString on the return value to get a
 
478
   const char * to utf8 bytes */
 
479
static PyObject *
 
480
getutf8string(PyObject *string)
 
481
{
 
482
  PyObject *inunicode=NULL;
 
483
  PyObject *utf8string=NULL;
 
484
 
 
485
  if(PyUnicode_Check(string))
 
486
    {
 
487
      inunicode=string;
 
488
      Py_INCREF(string);
 
489
    }
 
490
  else
 
491
    {
 
492
      inunicode=PyUnicode_FromObject(string);
 
493
      if(!inunicode) 
 
494
        return NULL;
 
495
    }
 
496
  assert(!PyErr_Occurred());
 
497
 
 
498
  utf8string=PyUnicode_AsUTF8String(inunicode);
 
499
  Py_DECREF(inunicode);
 
500
  return utf8string;
 
501
}
 
502
 
 
503
/* 
 
504
   Python's handling of Unicode is horrible.  It can use 2 or 4 byte
 
505
   unicode chars and the conversion routines like to put out BOMs
 
506
   which makes life even harder.  These macros are used in pairs to do
 
507
   the right form of conversion and tell us whether to use the plain
 
508
   or -16 version of the SQLite function that is about to be called.
 
509
*/
 
510
 
 
511
#if Py_UNICODE_SIZE==2
 
512
#define UNIDATABEGIN(obj) \
 
513
{                                                        \
 
514
  const int use16=1;                                     \
 
515
  size_t strbytes=2*PyUnicode_GET_SIZE(obj);             \
 
516
  const void *strdata=PyUnicode_AS_DATA(obj);            
 
517
 
 
518
#define UNIDATAEND(obj)                                  \
 
519
}
 
520
 
 
521
#else  /* Py_UNICODE_SIZE!=2 */
 
522
 
 
523
#define UNIDATABEGIN(obj) \
 
524
{                                                        \
 
525
  const int use16=0;                                     \
 
526
  Py_ssize_t strbytes=0;                                 \
 
527
  const char *strdata=NULL;                              \
 
528
  PyObject *_utf8=NULL;                                  \
 
529
                                                         \
 
530
  _utf8=PyUnicode_AsUTF8String(obj);                     \
 
531
  if(_utf8)                                              \
 
532
    {                                                    \
 
533
      strbytes=PyString_GET_SIZE(_utf8);                 \
 
534
      strdata=PyString_AsString(_utf8);                  \
 
535
    }                      
 
536
 
 
537
#define UNIDATAEND(obj)                                  \
 
538
  Py_XDECREF(_utf8);                                     \
 
539
}
 
540
 
 
541
#endif /* Py_UNICODE_SIZE */
 
542
 
 
543
/* CONNECTION CODE */
 
544
 
 
545
static void
 
546
Connection_internal_cleanup(Connection *self)
 
547
{
 
548
  if(self->filename)
 
549
    {
 
550
      PyMem_Free((void*)self->filename);
 
551
      self->filename=0;
 
552
    }
 
553
 
 
554
  Py_XDECREF(self->co_filename);
 
555
  self->co_filename=0;
 
556
 
 
557
  /* free functions */
 
558
  {
 
559
    funccbinfo *func=self->functions;
 
560
    while((func=freefunccbinfo(func)));
 
561
    self->functions=0;
 
562
  }
 
563
 
 
564
  /* free collations */
 
565
  {
 
566
    collationcbinfo *coll=self->collations;
 
567
    while((coll=freecollationcbinfo(coll)));
 
568
    self->collations=0;
 
569
  }
 
570
 
 
571
  /* free vtables */
 
572
  {
 
573
    vtableinfo *vtinfo=self->vtables;
 
574
    while((vtinfo=freevtableinfo(vtinfo)));
 
575
    self->vtables=0;
 
576
  }
 
577
 
 
578
  Py_XDECREF(self->busyhandler);
 
579
  self->busyhandler=0;
 
580
 
 
581
  Py_XDECREF(self->rollbackhook);
 
582
  self->rollbackhook=0;
 
583
 
 
584
  Py_XDECREF(self->profile);
 
585
  self->profile=0;
 
586
 
 
587
  Py_XDECREF(self->commithook);
 
588
  self->commithook=0;
 
589
 
 
590
  Py_XDECREF(self->progresshandler);
 
591
  self->progresshandler=0;
 
592
  
 
593
  Py_XDECREF(self->authorizer);
 
594
  self->authorizer=0;
 
595
 
 
596
}
 
597
 
 
598
static PyObject *
 
599
Connection_close(Connection *self, PyObject *args)
 
600
{
 
601
  PyObject *cursorcloseargs=NULL;
 
602
  int res;
 
603
  pointerlist_visit plv;
 
604
  int force=0;
 
605
 
 
606
  if(!self->db)
 
607
    goto finally;
 
608
 
 
609
  CHECK_THREAD(self,NULL);
 
610
 
 
611
  assert(!PyErr_Occurred());
 
612
 
 
613
  if(!PyArg_ParseTuple(args, "|i:close(force=False)", &force))
 
614
    return NULL;
 
615
 
 
616
  cursorcloseargs=Py_BuildValue("(i)", force);
 
617
  if(!cursorcloseargs) return NULL;
 
618
 
 
619
  for(pointerlist_visit_begin(&self->cursors, &plv);
 
620
      pointerlist_visit_finished(&plv);
 
621
      pointerlist_visit_next(&plv))
 
622
    {
 
623
      PyObject *closeres=NULL;
 
624
      Cursor *cur=(Cursor*)pointerlist_visit_get(&plv);
 
625
 
 
626
      closeres=Cursor_close(cur, args);
 
627
      Py_XDECREF(closeres);
 
628
      if(!closeres)
 
629
        {
 
630
          Py_DECREF(cursorcloseargs);
 
631
          return NULL;
 
632
        }
 
633
    }
 
634
 
 
635
  Py_DECREF(cursorcloseargs);
 
636
 
 
637
  res=statementcache_free(self->stmtcache);
 
638
  assert(res==0);
 
639
  self->stmtcache=0;
 
640
 
 
641
  Py_BEGIN_ALLOW_THREADS
 
642
    res=sqlite3_close(self->db);
 
643
  Py_END_ALLOW_THREADS;
 
644
 
 
645
  if (res!=SQLITE_OK) 
 
646
    {
 
647
      SET_EXC(self->db, res);
 
648
    }
 
649
 
 
650
  if(PyErr_Occurred())
 
651
    {
 
652
      AddTraceBackHere(__FILE__, __LINE__, "Connection.close", NULL);
 
653
    }
 
654
 
 
655
  /* note: SQLite ignores error returns from vtabDisconnect, so the
 
656
     database still ends up closed and we return an exception! */
 
657
 
 
658
  if(res!=SQLITE_OK)
 
659
      return NULL;
 
660
 
 
661
  self->db=0;
 
662
 
 
663
  Connection_internal_cleanup(self);
 
664
 
 
665
 finally:
 
666
  return PyErr_Occurred()?NULL:Py_BuildValue("");
 
667
  
 
668
}
 
669
 
 
670
static void
 
671
Connection_dealloc(Connection* self)
 
672
{
 
673
  if(self->db)
 
674
    {
 
675
      /* not allowed to clobber existing exception */
 
676
      PyObject *etype=NULL, *evalue=NULL, *etraceback=NULL;
 
677
      PyErr_Fetch(&etype, &evalue, &etraceback);
 
678
 
 
679
      PyErr_Format(ExcConnectionNotClosed, 
 
680
                   "apsw.Connection on \"%s\" at address %p, allocated at %s:%d. The destructor "
 
681
                   "has been called, but you haven't closed the connection.  All connections must "
 
682
                   "be explicitly closed.  The SQLite database object is being leaked.", 
 
683
                   self->filename?self->filename:"NULL", self,
 
684
                   PyString_AsString(self->co_filename), self->co_linenumber);
 
685
 
 
686
      apsw_write_unraiseable();
 
687
      PyErr_Fetch(&etype, &evalue, &etraceback);
 
688
    }
 
689
 
 
690
  assert(self->cursors.numentries==0);
 
691
  pointerlist_free(&self->cursors);
 
692
 
 
693
  Connection_internal_cleanup(self);
 
694
 
 
695
  self->ob_type->tp_free((PyObject*)self);
 
696
}
 
697
 
 
698
static PyObject*
 
699
Connection_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
 
700
{
 
701
    Connection *self;
 
702
 
 
703
    self = (Connection *)type->tp_alloc(type, 0);
 
704
    if (self != NULL) {
 
705
      /* Strictly speaking the memory was already zeroed.  This is
 
706
         just defensive coding. */
 
707
      self->db=0;
 
708
      self->filename=0;
 
709
      self->co_linenumber=0;
 
710
      self->co_filename=0;
 
711
      self->thread_ident=PyThread_get_thread_ident();
 
712
      memset(&self->cursors, 0, sizeof(self->cursors));
 
713
      pointerlist_init(&self->cursors);
 
714
      self->stmtcache=0;
 
715
      self->functions=0;
 
716
      self->collations=0;
 
717
      self->vtables=0;
 
718
      self->busyhandler=0;
 
719
      self->rollbackhook=0;
 
720
      self->profile=0;
 
721
      self->updatehook=0;
 
722
      self->commithook=0;
 
723
      self->progresshandler=0;
 
724
      self->authorizer=0;
 
725
    }
 
726
 
 
727
    return (PyObject *)self;
 
728
}
 
729
 
 
730
static int
 
731
Connection_init(Connection *self, PyObject *args, PyObject *kwds)
 
732
{
 
733
  PyObject *hooks=NULL, *hook=NULL, *iterator=NULL, *hookargs=NULL, *hookresult=NULL;
 
734
  char *filename=NULL;
 
735
  int res=0;
 
736
 
 
737
  CHECK_THREAD(self,-1);
 
738
 
 
739
  if(kwds && PyDict_Size(kwds)!=0)
 
740
    {
 
741
      PyErr_Format(PyExc_TypeError, "Connection constructor does not take keyword arguments");
 
742
      return -1;
 
743
    }
 
744
 
 
745
  if(!PyArg_ParseTuple(args, "es:Connection(filename)", STRENCODING, &filename))
 
746
    return -1;
 
747
  
 
748
  Py_BEGIN_ALLOW_THREADS
 
749
    res=sqlite3_open(filename, &self->db);
 
750
  Py_END_ALLOW_THREADS;
 
751
  SET_EXC(self->db, res);  /* nb sqlite3_open always allocates the db even on error */
 
752
  
 
753
  if(res!=SQLITE_OK)
 
754
      goto pyexception;
 
755
    
 
756
  /* record where it was allocated */
 
757
  PyFrameObject *frame = PyThreadState_GET()->frame;
 
758
  self->co_linenumber=PyCode_Addr2Line(frame->f_code, frame->f_lasti);
 
759
  self->co_filename=frame->f_code->co_filename;
 
760
  Py_INCREF(self->co_filename);
 
761
  self->filename=filename;
 
762
  filename=NULL; /* connection has ownership now */
 
763
 
 
764
  /* get detailed error codes */
 
765
  sqlite3_extended_result_codes(self->db, 1);
 
766
  
 
767
  /* call connection hooks */
 
768
  hooks=PyObject_GetAttrString(apswmodule, "connection_hooks");
 
769
  if(!hooks)
 
770
    goto pyexception;
 
771
 
 
772
  hookargs=Py_BuildValue("(O)", self);
 
773
  if(!hookargs) goto pyexception;
 
774
 
 
775
  iterator=PyObject_GetIter(hooks);
 
776
  if(!iterator)
 
777
    {
 
778
      AddTraceBackHere(__FILE__, __LINE__, "Connection.__init__", "{s: i}", "connection_hooks", hooks);
 
779
      goto pyexception;
 
780
    }
 
781
 
 
782
  while( (hook=PyIter_Next(iterator)) )
 
783
    {
 
784
      hookresult=PyEval_CallObject(hook, hookargs);
 
785
      if(!hookresult) 
 
786
        goto pyexception;
 
787
      Py_DECREF(hook);
 
788
      Py_DECREF(hookresult);
 
789
    }
 
790
 
 
791
  if(!PyErr_Occurred())
 
792
    {
 
793
      res=0;
 
794
      self->stmtcache=statementcache_init(self->db, 32);
 
795
      goto finally;
 
796
    }
 
797
 
 
798
 pyexception:
 
799
  /* clean up db since it is useless - no need for user to call close */
 
800
  res=-1;
 
801
  sqlite3_close(self->db);
 
802
  self->db=0;
 
803
  Connection_internal_cleanup(self);
 
804
 
 
805
finally:
 
806
  if(filename) PyMem_Free(filename);
 
807
  Py_XDECREF(hookargs);
 
808
  Py_XDECREF(iterator);
 
809
  Py_XDECREF(hooks);
 
810
  Py_XDECREF(hook);
 
811
  Py_XDECREF(hookresult);
 
812
  return res;
 
813
}
 
814
 
 
815
static void Cursor_init(Cursor *, Connection *);
 
816
 
 
817
static PyObject *
 
818
Connection_cursor(Connection *self)
 
819
{
 
820
  Cursor* cursor = NULL;
 
821
 
 
822
  CHECK_THREAD(self,NULL);
 
823
  CHECK_CLOSED(self,NULL);
 
824
 
 
825
  cursor = PyObject_New(Cursor, &CursorType);
 
826
  if(!cursor)
 
827
    return NULL;
 
828
 
 
829
  /* incref me since cursor holds a pointer */
 
830
  Py_INCREF((PyObject*)self);
 
831
  pointerlist_add(&self->cursors, cursor);
 
832
  Cursor_init(cursor, self);
 
833
  
 
834
  return (PyObject*)cursor;
 
835
}
 
836
 
 
837
static PyObject *
 
838
Connection_setbusytimeout(Connection *self, PyObject *args)
 
839
{
 
840
  int ms=0;
 
841
  int res;
 
842
 
 
843
  CHECK_THREAD(self,NULL);
 
844
  CHECK_CLOSED(self,NULL);
 
845
 
 
846
  if(!PyArg_ParseTuple(args, "i:setbusytimeout(millseconds)", &ms))
 
847
    return NULL;
 
848
 
 
849
  res=sqlite3_busy_timeout(self->db, ms);
 
850
  SET_EXC(self->db, res);
 
851
  if(res!=SQLITE_OK) return NULL;
 
852
  
 
853
  /* free any explicit busyhandler we may have had */
 
854
  Py_XDECREF(self->busyhandler);
 
855
  self->busyhandler=0;
 
856
 
 
857
  return Py_BuildValue("");
 
858
}
 
859
 
 
860
static PyObject *
 
861
Connection_changes(Connection *self)
 
862
{
 
863
  CHECK_THREAD(self,NULL);
 
864
  CHECK_CLOSED(self,NULL);
 
865
  return Py_BuildValue("i", sqlite3_changes(self->db));
 
866
}
 
867
 
 
868
static PyObject *
 
869
Connection_totalchanges(Connection *self)
 
870
{
 
871
  CHECK_THREAD(self,NULL);
 
872
  CHECK_CLOSED(self,NULL);
 
873
  return Py_BuildValue("i", sqlite3_total_changes(self->db));
 
874
}
 
875
 
 
876
static PyObject *
 
877
Connection_getautocommit(Connection *self)
 
878
{
 
879
  PyObject *res;
 
880
  CHECK_THREAD(self,NULL);
 
881
  CHECK_CLOSED(self,NULL);
 
882
  res=(sqlite3_get_autocommit(self->db))?(Py_True):(Py_False);
 
883
  Py_INCREF(res);
 
884
  return res;
 
885
}
 
886
 
 
887
static PyObject *
 
888
Connection_last_insert_rowid(Connection *self)
 
889
{
 
890
  long long int vint;
 
891
 
 
892
  CHECK_THREAD(self,NULL);
 
893
  CHECK_CLOSED(self,NULL);
 
894
 
 
895
  vint=sqlite3_last_insert_rowid(self->db);
 
896
  
 
897
  if(vint<APSW_INT32_MIN || vint>APSW_INT32_MAX)
 
898
    return PyLong_FromLongLong(vint);
 
899
  else
 
900
    return PyInt_FromLong((long)vint);
 
901
}
 
902
 
 
903
static PyObject *
 
904
Connection_complete(Connection *self, PyObject *args)
 
905
{
 
906
  char *statements=NULL;
 
907
  int res;
 
908
 
 
909
  CHECK_THREAD(self,NULL);
 
910
  CHECK_CLOSED(self,NULL);
 
911
  
 
912
  if(!PyArg_ParseTuple(args, "es:complete(statement)", STRENCODING, &statements))
 
913
    return NULL;
 
914
 
 
915
  res=sqlite3_complete(statements);
 
916
 
 
917
  PyMem_Free(statements);
 
918
 
 
919
  if(res)
 
920
    {
 
921
      Py_INCREF(Py_True);
 
922
      return Py_True;
 
923
    }
 
924
  Py_INCREF(Py_False);
 
925
  return Py_False;
 
926
}
 
927
 
 
928
static PyObject *
 
929
Connection_interrupt(Connection *self)
 
930
{
 
931
  CHECK_THREAD(self, NULL);
 
932
  CHECK_CLOSED(self, NULL);
 
933
 
 
934
  sqlite3_interrupt(self->db);  /* no return value */
 
935
  return Py_BuildValue("");
 
936
}
 
937
 
 
938
static void
 
939
updatecb(void *context, int updatetype, char const *databasename, char const *tablename, sqlite_int64 rowid)
 
940
{
 
941
  /* The hook returns void. That makes it impossible for us to
 
942
     abort immediately due to an error in the callback */
 
943
  
 
944
  PyGILState_STATE gilstate;
 
945
  PyObject *retval=NULL, *args=NULL;
 
946
  Connection *self=(Connection *)context;
 
947
 
 
948
  assert(self);
 
949
  assert(self->updatehook);
 
950
  assert(self->updatehook!=Py_None);
 
951
 
 
952
  gilstate=PyGILState_Ensure();
 
953
 
 
954
  if(PyErr_Occurred())
 
955
    goto finally;  /* abort hook due to outstanding exception */
 
956
 
 
957
  args=Py_BuildValue("(iO&O&L)", updatetype, convertutf8string, databasename, convertutf8string, tablename, rowid);
 
958
  if(!args) goto finally;
 
959
  
 
960
  retval=PyEval_CallObject(self->updatehook, args);
 
961
 
 
962
 finally:
 
963
  Py_XDECREF(retval);
 
964
  Py_XDECREF(args);
 
965
  PyGILState_Release(gilstate);
 
966
}
 
967
 
 
968
static PyObject *
 
969
Connection_setupdatehook(Connection *self, PyObject *callable)
 
970
{
 
971
  /* sqlite3_update_hook doesn't return an error code */
 
972
  
 
973
  CHECK_THREAD(self,NULL);
 
974
  CHECK_CLOSED(self,NULL);
 
975
 
 
976
  if(callable==Py_None)
 
977
    {
 
978
      sqlite3_update_hook(self->db, NULL, NULL);
 
979
      callable=NULL;
 
980
      goto finally;
 
981
    }
 
982
 
 
983
  if(!PyCallable_Check(callable))
 
984
    {
 
985
      PyErr_Format(PyExc_TypeError, "update hook must be callable");
 
986
      return NULL;
 
987
    }
 
988
 
 
989
  sqlite3_update_hook(self->db, updatecb, self);
 
990
 
 
991
  Py_INCREF(callable);
 
992
 
 
993
 finally:
 
994
 
 
995
  Py_XDECREF(self->updatehook);
 
996
  self->updatehook=callable;
 
997
 
 
998
  return Py_BuildValue("");
 
999
}
 
1000
 
 
1001
static void
 
1002
rollbackhookcb(void *context)
 
1003
{
 
1004
  /* The hook returns void. That makes it impossible for us to
 
1005
     abort immediately due to an error in the callback */
 
1006
  
 
1007
  PyGILState_STATE gilstate;
 
1008
  PyObject *retval=NULL;
 
1009
  Connection *self=(Connection *)context;
 
1010
 
 
1011
  assert(self);
 
1012
  assert(self->rollbackhook);
 
1013
  assert(self->rollbackhook!=Py_None);
 
1014
 
 
1015
  gilstate=PyGILState_Ensure();
 
1016
 
 
1017
  if(PyErr_Occurred())
 
1018
    goto finally;  /* abort hook due to outstanding exception */
 
1019
 
 
1020
  retval=PyEval_CallObject(self->rollbackhook, NULL);
 
1021
 
 
1022
 finally:
 
1023
  Py_XDECREF(retval);
 
1024
  PyGILState_Release(gilstate);
 
1025
}
 
1026
 
 
1027
static PyObject *
 
1028
Connection_setrollbackhook(Connection *self, PyObject *callable)
 
1029
{
 
1030
  /* sqlite3_rollback_hook doesn't return an error code */
 
1031
  
 
1032
  CHECK_THREAD(self,NULL);
 
1033
  CHECK_CLOSED(self,NULL);
 
1034
 
 
1035
  if(callable==Py_None)
 
1036
    {
 
1037
      sqlite3_rollback_hook(self->db, NULL, NULL);
 
1038
      callable=NULL;
 
1039
      goto finally;
 
1040
    }
 
1041
 
 
1042
  if(!PyCallable_Check(callable))
 
1043
    {
 
1044
      PyErr_Format(PyExc_TypeError, "rollback hook must be callable");
 
1045
      return NULL;
 
1046
    }
 
1047
 
 
1048
  sqlite3_rollback_hook(self->db, rollbackhookcb, self);
 
1049
 
 
1050
  Py_INCREF(callable);
 
1051
 
 
1052
 finally:
 
1053
 
 
1054
  Py_XDECREF(self->rollbackhook);
 
1055
  self->rollbackhook=callable;
 
1056
 
 
1057
  return Py_BuildValue("");
 
1058
}
 
1059
 
 
1060
#ifdef EXPERIMENTAL /* sqlite3_profile */
 
1061
static void
 
1062
profilecb(void *context, const char *statement, sqlite_uint64 runtime)
 
1063
{
 
1064
  /* The hook returns void. That makes it impossible for us to
 
1065
     abort immediately due to an error in the callback */
 
1066
  
 
1067
  PyGILState_STATE gilstate;
 
1068
  PyObject *retval=NULL, *args=NULL;
 
1069
  Connection *self=(Connection *)context;
 
1070
 
 
1071
  assert(self);
 
1072
  assert(self->profile);
 
1073
  assert(self->profile!=Py_None);
 
1074
 
 
1075
  gilstate=PyGILState_Ensure();
 
1076
 
 
1077
  if(PyErr_Occurred())
 
1078
    goto finally;  /* abort hook due to outstanding exception */
 
1079
 
 
1080
  args=Py_BuildValue("(O&K)", convertutf8string, statement, runtime);
 
1081
  if(!args) goto finally;
 
1082
 
 
1083
  retval=PyEval_CallObject(self->profile, args);
 
1084
 
 
1085
 finally:
 
1086
  Py_XDECREF(retval);
 
1087
  Py_XDECREF(args);
 
1088
  PyGILState_Release(gilstate);
 
1089
}
 
1090
 
 
1091
static PyObject *
 
1092
Connection_setprofile(Connection *self, PyObject *callable)
 
1093
{
 
1094
  /* sqlite3_profile doesn't return an error code */
 
1095
  
 
1096
  CHECK_THREAD(self,NULL);
 
1097
  CHECK_CLOSED(self,NULL);
 
1098
 
 
1099
  if(callable==Py_None)
 
1100
    {
 
1101
      sqlite3_profile(self->db, NULL, NULL);
 
1102
      callable=NULL;
 
1103
      goto finally;
 
1104
    }
 
1105
 
 
1106
  if(!PyCallable_Check(callable))
 
1107
    {
 
1108
      PyErr_Format(PyExc_TypeError, "profile function must be callable");
 
1109
      return NULL;
 
1110
    }
 
1111
 
 
1112
  sqlite3_profile(self->db, profilecb, self);
 
1113
 
 
1114
  Py_INCREF(callable);
 
1115
 
 
1116
 finally:
 
1117
 
 
1118
  Py_XDECREF(self->profile);
 
1119
  self->profile=callable;
 
1120
 
 
1121
  return Py_BuildValue("");
 
1122
}
 
1123
#endif /* EXPERIMENTAL - sqlite3_profile */
 
1124
 
 
1125
 
 
1126
#ifdef EXPERIMENTAL      /* commit hook */
 
1127
static int 
 
1128
commithookcb(void *context)
 
1129
{
 
1130
  /* The hook returns 0 for commit to go ahead and non-zero to abort
 
1131
     commit (turn into a rollback). We return non-zero for errors */
 
1132
  
 
1133
  PyGILState_STATE gilstate;
 
1134
  PyObject *retval=NULL;
 
1135
  int ok=1; /* error state */
 
1136
  Connection *self=(Connection *)context;
 
1137
 
 
1138
  assert(self);
 
1139
  assert(self->commithook);
 
1140
  assert(self->commithook!=Py_None);
 
1141
 
 
1142
  gilstate=PyGILState_Ensure();
 
1143
 
 
1144
  if(PyErr_Occurred())
 
1145
    goto finally;  /* abort hook due to outstanding exception */
 
1146
 
 
1147
  retval=PyEval_CallObject(self->commithook, NULL);
 
1148
 
 
1149
  if(!retval)
 
1150
    goto finally; /* abort hook due to exeception */
 
1151
 
 
1152
  ok=PyObject_IsTrue(retval);
 
1153
  assert(ok==-1 || ok==0 || ok==1);
 
1154
  /* the docs say -1 can be returned, but the code for PyObject_IsTrue always returns 1 or 0.  
 
1155
     this is a defensive check */
 
1156
  if(ok==-1)
 
1157
    {
 
1158
      ok=1;
 
1159
      goto finally;  /* abort due to exception in return value */
 
1160
    }
 
1161
 
 
1162
 finally:
 
1163
  Py_XDECREF(retval);
 
1164
  PyGILState_Release(gilstate);
 
1165
  return ok;
 
1166
}
 
1167
 
 
1168
static PyObject *
 
1169
Connection_setcommithook(Connection *self, PyObject *callable)
 
1170
{
 
1171
  /* sqlite3_commit_hook doesn't return an error code */
 
1172
  
 
1173
  CHECK_THREAD(self,NULL);
 
1174
  CHECK_CLOSED(self,NULL);
 
1175
 
 
1176
  if(callable==Py_None)
 
1177
    {
 
1178
      sqlite3_commit_hook(self->db, NULL, NULL);
 
1179
      callable=NULL;
 
1180
      goto finally;
 
1181
    }
 
1182
 
 
1183
  if(!PyCallable_Check(callable))
 
1184
    {
 
1185
      PyErr_Format(PyExc_TypeError, "commit hook must be callable");
 
1186
      return NULL;
 
1187
    }
 
1188
 
 
1189
  sqlite3_commit_hook(self->db, commithookcb, self);
 
1190
 
 
1191
  Py_INCREF(callable);
 
1192
 
 
1193
 finally:
 
1194
 
 
1195
  Py_XDECREF(self->commithook);
 
1196
  self->commithook=callable;
 
1197
 
 
1198
  return Py_BuildValue("");
 
1199
}
 
1200
#endif  /* EXPERIMENTAL sqlite3_commit_hook */
 
1201
 
 
1202
#ifdef EXPERIMENTAL      /* sqlite3_progress_handler */
 
1203
static int 
 
1204
progresshandlercb(void *context)
 
1205
{
 
1206
  /* The hook returns 0 for continue and non-zero to abort (rollback).
 
1207
     We return non-zero for errors */
 
1208
  
 
1209
  PyGILState_STATE gilstate;
 
1210
  PyObject *retval=NULL;
 
1211
  int ok=1; /* error state */
 
1212
  Connection *self=(Connection *)context;
 
1213
 
 
1214
  assert(self);
 
1215
  assert(self->progresshandler);
 
1216
 
 
1217
  gilstate=PyGILState_Ensure();
 
1218
 
 
1219
  retval=PyEval_CallObject(self->progresshandler, NULL);
 
1220
 
 
1221
  if(!retval)
 
1222
    goto finally; /* abort due to exeception */
 
1223
 
 
1224
  ok=PyObject_IsTrue(retval);
 
1225
 
 
1226
  assert(ok==-1 || ok==0 || ok==1);
 
1227
  /* see earlier comment about PyObject_IsTrue */
 
1228
  if(ok==-1)
 
1229
    {
 
1230
      ok=1;
 
1231
      goto finally;  /* abort due to exception in result */
 
1232
    }
 
1233
 
 
1234
 finally:
 
1235
  Py_XDECREF(retval);
 
1236
 
 
1237
  PyGILState_Release(gilstate);
 
1238
  return ok;
 
1239
}
 
1240
 
 
1241
static PyObject *
 
1242
Connection_setprogresshandler(Connection *self, PyObject *args)
 
1243
{
 
1244
  /* sqlite3_progress_handler doesn't return an error code */
 
1245
  int nsteps=20;
 
1246
  PyObject *callable=NULL;
 
1247
  
 
1248
  CHECK_THREAD(self,NULL);
 
1249
  CHECK_CLOSED(self,NULL);
 
1250
 
 
1251
  if(!PyArg_ParseTuple(args, "O|i:setprogresshandler(callable, nsteps=20)", &callable, &nsteps))
 
1252
    return NULL;
 
1253
 
 
1254
  if(callable==Py_None)
 
1255
    {
 
1256
      sqlite3_progress_handler(self->db, 0, NULL, NULL);
 
1257
      callable=NULL;
 
1258
      goto finally;
 
1259
    }
 
1260
 
 
1261
  if(!PyCallable_Check(callable))
 
1262
    {
 
1263
      PyErr_Format(PyExc_TypeError, "progress handler must be callable");
 
1264
      return NULL;
 
1265
    }
 
1266
 
 
1267
  sqlite3_progress_handler(self->db, nsteps, progresshandlercb, self);
 
1268
  Py_INCREF(callable);
 
1269
 
 
1270
 finally:
 
1271
 
 
1272
  Py_XDECREF(self->progresshandler);
 
1273
  self->progresshandler=callable;
 
1274
 
 
1275
  return Py_BuildValue("");
 
1276
}
 
1277
#endif  /* EXPERIMENTAL sqlite3_progress_handler */
 
1278
 
 
1279
static int 
 
1280
authorizercb(void *context, int operation, const char *paramone, const char *paramtwo, const char *databasename, const char *triggerview)
 
1281
{
 
1282
  /* should return one of SQLITE_OK, SQLITE_DENY, or
 
1283
     SQLITE_IGNORE. (0, 1 or 2 respectively) */
 
1284
 
 
1285
  PyGILState_STATE gilstate;
 
1286
  PyObject *args=NULL, *retval=NULL;
 
1287
  int result=SQLITE_DENY;  /* default to deny */
 
1288
  Connection *self=(Connection *)context;
 
1289
 
 
1290
  assert(self);
 
1291
  assert(self->authorizer);
 
1292
  assert(self->authorizer!=Py_None);
 
1293
 
 
1294
  gilstate=PyGILState_Ensure();
 
1295
 
 
1296
  if(PyErr_Occurred())
 
1297
    goto finally;  /* abort due to earlier exception */
 
1298
 
 
1299
 
 
1300
  args=Py_BuildValue("(iO&O&O&O&)", operation, convertutf8string, paramone, 
 
1301
                     convertutf8string, paramtwo, convertutf8string, databasename, 
 
1302
                     convertutf8string, triggerview);
 
1303
  if(!args) goto finally;
 
1304
 
 
1305
  retval=PyEval_CallObject(self->authorizer, args);
 
1306
 
 
1307
  if(!retval)
 
1308
    goto finally; /* abort due to exeception */
 
1309
 
 
1310
  result=PyInt_AsLong(retval);
 
1311
  if (PyErr_Occurred())
 
1312
    result=SQLITE_DENY;
 
1313
 
 
1314
 finally:
 
1315
  Py_XDECREF(args);
 
1316
  Py_XDECREF(retval);
 
1317
 
 
1318
  PyGILState_Release(gilstate);
 
1319
  return result;
 
1320
}
 
1321
 
 
1322
static PyObject *
 
1323
Connection_setauthorizer(Connection *self, PyObject *callable)
 
1324
{
 
1325
  int res;
 
1326
 
 
1327
  CHECK_THREAD(self,NULL);
 
1328
  CHECK_CLOSED(self,NULL);
 
1329
 
 
1330
  if(callable==Py_None)
 
1331
    {
 
1332
      res=sqlite3_set_authorizer(self->db, NULL, NULL);
 
1333
      callable=NULL;
 
1334
      goto finally;
 
1335
    }
 
1336
 
 
1337
  if(!PyCallable_Check(callable))
 
1338
    {
 
1339
      PyErr_Format(PyExc_TypeError, "authorizer must be callable");
 
1340
      return NULL;
 
1341
    }
 
1342
 
 
1343
  res=sqlite3_set_authorizer(self->db, authorizercb, self);
 
1344
  SET_EXC(self->db, res);
 
1345
 
 
1346
  Py_INCREF(callable);
 
1347
 
 
1348
 finally:
 
1349
  Py_XDECREF(self->authorizer);
 
1350
  self->authorizer=callable;
 
1351
 
 
1352
  return (res==SQLITE_OK)?Py_BuildValue(""):NULL;
 
1353
}
 
1354
 
 
1355
static int 
 
1356
busyhandlercb(void *context, int ncall)
 
1357
{
 
1358
  /* Return zero for caller to get SQLITE_BUSY error. We default to
 
1359
     zero in case of error. */
 
1360
 
 
1361
  PyGILState_STATE gilstate;
 
1362
  PyObject *args, *retval;
 
1363
  int result=0;  /* default to fail with SQLITE_BUSY */
 
1364
  Connection *self=(Connection *)context;
 
1365
 
 
1366
  assert(self);
 
1367
  assert(self->busyhandler);
 
1368
 
 
1369
  gilstate=PyGILState_Ensure();
 
1370
 
 
1371
  args=Py_BuildValue("(i)", ncall);
 
1372
  if(!args)
 
1373
    goto finally; /* abort busy due to memory allocation failure */
 
1374
  
 
1375
  retval=PyEval_CallObject(self->busyhandler, args);
 
1376
  Py_DECREF(args);
 
1377
 
 
1378
  if(!retval)
 
1379
    goto finally; /* abort due to exeception */
 
1380
 
 
1381
  result=PyObject_IsTrue(retval);
 
1382
  assert(result==-1 || result==0 || result==1);
 
1383
  Py_DECREF(retval);
 
1384
 
 
1385
  if(result==-1)
 
1386
    {
 
1387
      result=0;
 
1388
      goto finally;  /* abort due to exception converting retval */
 
1389
    }
 
1390
 
 
1391
 finally:
 
1392
  PyGILState_Release(gilstate);
 
1393
  return result;
 
1394
}
 
1395
 
 
1396
#if defined(EXPERIMENTAL) && !defined(SQLITE_OMIT_LOAD_EXTENSION)  /* extension loading */
 
1397
static PyObject *
 
1398
Connection_enableloadextension(Connection *self, PyObject *enabled)
 
1399
{
 
1400
  int enabledp, res;
 
1401
 
 
1402
  CHECK_THREAD(self, NULL);
 
1403
  CHECK_CLOSED(self, NULL);
 
1404
 
 
1405
  /* get the boolean value */
 
1406
  enabledp=PyObject_IsTrue(enabled);
 
1407
  if(enabledp==-1) return NULL;
 
1408
  if (PyErr_Occurred()) return NULL;
 
1409
 
 
1410
  /* call function */
 
1411
  res=sqlite3_enable_load_extension(self->db, enabledp);
 
1412
  SET_EXC(self->db, res);  /* the function will currently always succeed */
 
1413
 
 
1414
  /* done */
 
1415
  return (res==SQLITE_OK)?Py_BuildValue(""):NULL;
 
1416
}
 
1417
 
 
1418
static PyObject *
 
1419
Connection_loadextension(Connection *self, PyObject *args)
 
1420
{
 
1421
  int res;
 
1422
  char *zfile=NULL, *zproc=NULL, *errmsg=NULL;
 
1423
 
 
1424
  CHECK_THREAD(self, NULL);
 
1425
  CHECK_CLOSED(self, NULL);
 
1426
  
 
1427
  if(!PyArg_ParseTuple(args, "s|z:loadextension(filename, entrypoint=None)", &zfile, &zproc))
 
1428
    return NULL;
 
1429
 
 
1430
  Py_BEGIN_ALLOW_THREADS
 
1431
    res=sqlite3_load_extension(self->db, zfile, zproc, &errmsg);
 
1432
  Py_END_ALLOW_THREADS;
 
1433
 
 
1434
  /* load_extension doesn't set the error message on the db so we have to make exception manually */
 
1435
  if(res!=SQLITE_OK)
 
1436
    {
 
1437
      assert(errmsg);
 
1438
      PyErr_Format(ExcExtensionLoading, "ExtensionLoadingError: %s", errmsg?errmsg:"unspecified");
 
1439
      sqlite3_free(errmsg);
 
1440
      return NULL;
 
1441
    }
 
1442
  return Py_BuildValue("");
 
1443
}
 
1444
 
 
1445
#endif /* EXPERIMENTAL extension loading */
 
1446
 
 
1447
static PyObject *
 
1448
Connection_setbusyhandler(Connection *self, PyObject *callable)
 
1449
{
 
1450
  int res=SQLITE_OK;
 
1451
 
 
1452
  CHECK_THREAD(self,NULL);
 
1453
  CHECK_CLOSED(self,NULL);
 
1454
 
 
1455
  if(callable==Py_None)
 
1456
    {
 
1457
      res=sqlite3_busy_handler(self->db, NULL, NULL);
 
1458
      callable=NULL;
 
1459
      goto finally;
 
1460
    }
 
1461
 
 
1462
  if(!PyCallable_Check(callable))
 
1463
    {
 
1464
      PyErr_Format(PyExc_TypeError, "busyhandler must be callable");
 
1465
      return NULL;
 
1466
    }
 
1467
 
 
1468
  res=sqlite3_busy_handler(self->db, busyhandlercb, self);
 
1469
  SET_EXC(self->db, res);
 
1470
 
 
1471
  Py_INCREF(callable);
 
1472
 
 
1473
 finally:
 
1474
  Py_XDECREF(self->busyhandler);
 
1475
  self->busyhandler=callable;
 
1476
 
 
1477
  return (res==SQLITE_OK)?Py_BuildValue(""):NULL;
 
1478
}
 
1479
 
 
1480
 
 
1481
/* USER DEFINED FUNCTION CODE.*/
 
1482
 
 
1483
/* We store the registered functions in a linked list hooked into the
 
1484
   connection object so we can free them.  There is probably a better
 
1485
   data structure to use but this was most convenient. */
 
1486
 
 
1487
static funccbinfo *
 
1488
freefunccbinfo(funccbinfo *func)
 
1489
{
 
1490
  funccbinfo *fnext;
 
1491
  if(!func) 
 
1492
    return NULL;
 
1493
 
 
1494
  if(func->name)
 
1495
    PyMem_Free(func->name);
 
1496
  Py_XDECREF(func->scalarfunc);
 
1497
  Py_XDECREF(func->aggregatefactory);
 
1498
  fnext=func->next;
 
1499
  PyMem_Free(func);
 
1500
  return fnext;
 
1501
}
 
1502
 
 
1503
static funccbinfo *
 
1504
allocfunccbinfo(void)
 
1505
{
 
1506
  funccbinfo *res=PyMem_Malloc(sizeof(funccbinfo));
 
1507
  if(res)
 
1508
    memset(res, 0, sizeof(funccbinfo));
 
1509
  return res;
 
1510
}
 
1511
 
 
1512
/* Converts sqlite3_value to PyObject.  Returns a new reference. */
 
1513
static PyObject *
 
1514
convert_value_to_pyobject(sqlite3_value *value)
 
1515
{
 
1516
  const int coltype=sqlite3_value_type(value);
 
1517
 
 
1518
  switch(coltype)
 
1519
    {
 
1520
    case SQLITE_INTEGER:
 
1521
      {
 
1522
        long long vint=sqlite3_value_int64(value);
 
1523
        if(vint<APSW_INT32_MIN || vint>APSW_INT32_MAX)
 
1524
          return PyLong_FromLongLong(vint);
 
1525
        else
 
1526
          return PyInt_FromLong((long)vint);
 
1527
      }
 
1528
 
 
1529
    case SQLITE_FLOAT:
 
1530
      return PyFloat_FromDouble(sqlite3_value_double(value));
 
1531
      
 
1532
    case SQLITE_TEXT:
 
1533
      return convertutf8stringsize((const char*)sqlite3_value_text(value), sqlite3_value_bytes(value));
 
1534
 
 
1535
    case SQLITE_NULL:
 
1536
      Py_INCREF(Py_None);
 
1537
      return Py_None;
 
1538
 
 
1539
    case SQLITE_BLOB:
 
1540
      {
 
1541
        PyObject *item;
 
1542
        Py_ssize_t sz=sqlite3_value_bytes(value);
 
1543
        item=PyBuffer_New(sz);
 
1544
        if(item)
 
1545
          {
 
1546
            void *buffy=0;
 
1547
            Py_ssize_t sz2=sz;
 
1548
            if(!PyObject_AsWriteBuffer(item, &buffy, &sz2))
 
1549
              memcpy(buffy, sqlite3_value_blob(value), sz);
 
1550
            else
 
1551
              {
 
1552
                Py_DECREF(item);
 
1553
                return NULL;
 
1554
              }
 
1555
            return item;
 
1556
          }
 
1557
        return NULL;
 
1558
      }
 
1559
 
 
1560
    default:
 
1561
      PyErr_Format(APSWException, "Unknown sqlite column type %d!", coltype);
 
1562
      return NULL;
 
1563
    }
 
1564
  /* can't get here */
 
1565
  assert(0);
 
1566
  return NULL;
 
1567
}
 
1568
 
 
1569
/* converts a python object into a sqlite3_context result */
 
1570
static void
 
1571
set_context_result(sqlite3_context *context, PyObject *obj)
 
1572
{
 
1573
  if(!obj)
 
1574
    {
 
1575
      assert(PyErr_Occurred());
 
1576
      /* TODO: possibly examine exception and return appropriate error
 
1577
         code eg for BusyError set error to SQLITE_BUSY */
 
1578
      sqlite3_result_error(context, "bad object given to set_context_result", -1);
 
1579
      return;
 
1580
    }
 
1581
 
 
1582
  /* DUPLICATE(ish) code: this is substantially similar to the code in
 
1583
     Cursor_dobinding.  If you fix anything here then do it there as
 
1584
     well. */
 
1585
 
 
1586
  if(obj==Py_None)
 
1587
    {
 
1588
      sqlite3_result_null(context);
 
1589
      return;
 
1590
    }
 
1591
  if(PyInt_Check(obj))
 
1592
    {
 
1593
      sqlite3_result_int64(context, PyInt_AS_LONG(obj));
 
1594
      return;
 
1595
    }
 
1596
  if (PyLong_Check(obj))
 
1597
    {
 
1598
      sqlite3_result_int64(context, PyLong_AsLongLong(obj));
 
1599
      return;
 
1600
    }
 
1601
  if (PyFloat_Check(obj))
 
1602
    {
 
1603
      sqlite3_result_double(context, PyFloat_AS_DOUBLE(obj));
 
1604
      return;
 
1605
    }
 
1606
  if (PyUnicode_Check(obj))
 
1607
    {
 
1608
      UNIDATABEGIN(obj)
 
1609
        if(strdata)
 
1610
          {
 
1611
            if(strbytes>APSW_INT32_MAX)
 
1612
              {
 
1613
                PyErr_Format(ExcTooBig, "Unicode object is too large - SQLite only supports up to 2GB");
 
1614
              }
 
1615
            else
 
1616
              {
 
1617
                if(use16)
 
1618
                  sqlite3_result_text16(context, strdata, (int)strbytes, SQLITE_TRANSIENT);
 
1619
                else
 
1620
                  sqlite3_result_text(context, strdata, (int)strbytes, SQLITE_TRANSIENT);
 
1621
              }
 
1622
          }
 
1623
        else
 
1624
          sqlite3_result_error(context, "Unicode conversions failed", -1);
 
1625
      UNIDATAEND(obj);
 
1626
      return;
 
1627
    }
 
1628
  if (PyString_Check(obj))
 
1629
    {
 
1630
      const char *val=PyString_AS_STRING(obj);
 
1631
      const Py_ssize_t lenval=PyString_GET_SIZE(obj);
 
1632
      const char *chk=val;
 
1633
      for(;chk<val+lenval && !((*chk)&0x80); chk++);
 
1634
      if(chk<val+lenval)
 
1635
        {
 
1636
          PyObject *str2=PyUnicode_FromObject(obj);
 
1637
          if(!str2)
 
1638
            {
 
1639
              sqlite3_result_error(context, "PyUnicode_FromObject failed", -1);
 
1640
              return;
 
1641
            }
 
1642
          UNIDATABEGIN(str2)
 
1643
            if(strdata)
 
1644
              {
 
1645
                if(strbytes>APSW_INT32_MAX)
 
1646
                  {
 
1647
                    PyErr_Format(ExcTooBig, "Unicode object is too large - SQLite only supports up to 2GB");
 
1648
                  }
 
1649
                else
 
1650
                  {
 
1651
                    if(use16)
 
1652
                      sqlite3_result_text16(context, strdata, (int)strbytes, SQLITE_TRANSIENT);
 
1653
                    else
 
1654
                      sqlite3_result_text(context, strdata, (int)strbytes, SQLITE_TRANSIENT);
 
1655
                  }
 
1656
              }
 
1657
            else
 
1658
              sqlite3_result_error(context, "Unicode conversions failed", -1);
 
1659
          UNIDATAEND(str2);
 
1660
          Py_DECREF(str2);
 
1661
        }
 
1662
      else
 
1663
        {
 
1664
          if(lenval>APSW_INT32_MAX)
 
1665
              {
 
1666
                PyErr_Format(ExcTooBig, "String object is too large - SQLite only supports up to 2GB");
 
1667
                }
 
1668
          else
 
1669
            sqlite3_result_text(context, val, (int)lenval, SQLITE_TRANSIENT);
 
1670
        }
 
1671
      return;
 
1672
    }
 
1673
  if (PyBuffer_Check(obj))
 
1674
    {
 
1675
      const char *buffer;
 
1676
      Py_ssize_t buflen;
 
1677
      if(PyObject_AsCharBuffer(obj, &buffer, &buflen))
 
1678
        {
 
1679
          sqlite3_result_error(context, "PyObject_AsCharBuffer failed", -1);
 
1680
          return;
 
1681
        }
 
1682
      if (buflen>APSW_INT32_MAX)
 
1683
        sqlite3_result_error(context, "Buffer object is too large for SQLite - only up to 2GB is supported", -1);
 
1684
      else
 
1685
        sqlite3_result_blob(context, buffer, (int)buflen, SQLITE_TRANSIENT);
 
1686
      return;
 
1687
    }
 
1688
 
 
1689
  PyErr_Format(PyExc_TypeError, "Bad return type from function callback");
 
1690
  sqlite3_result_error(context, "Bad return type from function callback", -1);
 
1691
}
 
1692
 
 
1693
/* Returns a new reference to a tuple formed from function parameters */
 
1694
PyObject *
 
1695
getfunctionargs(sqlite3_context *context, PyObject *firstelement, int argc, sqlite3_value **argv)
 
1696
{
 
1697
  PyObject *pyargs=NULL;
 
1698
  int i;
 
1699
  int extra=0;
 
1700
 
 
1701
  /* extra first item */
 
1702
  if(firstelement)
 
1703
    extra=1;
 
1704
 
 
1705
  pyargs=PyTuple_New((long)argc+extra);
 
1706
  if(!pyargs)
 
1707
    {
 
1708
      sqlite3_result_error(context, "PyTuple_New failed", -1);
 
1709
      goto error;
 
1710
    }
 
1711
 
 
1712
  if(extra)
 
1713
    {
 
1714
      Py_INCREF(firstelement);
 
1715
      PyTuple_SET_ITEM(pyargs, 0, firstelement);
 
1716
    }
 
1717
 
 
1718
  for(i=0;i<argc;i++)
 
1719
    {
 
1720
      PyObject *item=convert_value_to_pyobject(argv[i]);
 
1721
      if(!item)
 
1722
        {
 
1723
          Py_DECREF(pyargs);
 
1724
          sqlite3_result_error(context, "convert_value_to_pyobject failed", -1);
 
1725
          goto error;
 
1726
        }
 
1727
      PyTuple_SET_ITEM(pyargs, i+extra, item);
 
1728
    }
 
1729
  
 
1730
  return pyargs;
 
1731
 
 
1732
 error:
 
1733
  Py_XDECREF(pyargs);
 
1734
  return NULL;
 
1735
}
 
1736
 
 
1737
 
 
1738
/* dispatches scalar function */
 
1739
static void
 
1740
cbdispatch_func(sqlite3_context *context, int argc, sqlite3_value **argv)
 
1741
{
 
1742
  PyGILState_STATE gilstate;
 
1743
  PyObject *pyargs;
 
1744
  PyObject *retval;
 
1745
  funccbinfo *cbinfo=(funccbinfo*)sqlite3_user_data(context);
 
1746
  assert(cbinfo);
 
1747
 
 
1748
  gilstate=PyGILState_Ensure();
 
1749
 
 
1750
  assert(cbinfo->scalarfunc);
 
1751
 
 
1752
  if(PyErr_Occurred())
 
1753
    {
 
1754
      sqlite3_result_error(context, "Prior Python Error", -1);
 
1755
      goto finalfinally;
 
1756
    }
 
1757
 
 
1758
  pyargs=getfunctionargs(context, NULL, argc, argv);
 
1759
  if(!pyargs)
 
1760
      goto finally;
 
1761
 
 
1762
  assert(!PyErr_Occurred());
 
1763
  retval=PyEval_CallObject(cbinfo->scalarfunc, pyargs);
 
1764
 
 
1765
  Py_DECREF(pyargs);
 
1766
  set_context_result(context, retval);
 
1767
  Py_XDECREF(retval);
 
1768
 
 
1769
 finally:
 
1770
  if (PyErr_Occurred())
 
1771
    {
 
1772
      char *funname=sqlite3_mprintf("user-defined-scalar-%s", cbinfo->name);
 
1773
      AddTraceBackHere(__FILE__, __LINE__, funname, "{s: i}", "NumberOfArguments", argc);
 
1774
      sqlite3_free(funname);
 
1775
    }
 
1776
 finalfinally:
 
1777
   PyGILState_Release(gilstate);
 
1778
}
 
1779
 
 
1780
static aggregatefunctioncontext *
 
1781
getaggregatefunctioncontext(sqlite3_context *context)
 
1782
{
 
1783
  aggregatefunctioncontext *aggfc=sqlite3_aggregate_context(context, sizeof(aggregatefunctioncontext));
 
1784
  funccbinfo *cbinfo;
 
1785
  PyObject *retval;
 
1786
  /* have we seen it before? */
 
1787
  if(aggfc->aggvalue) 
 
1788
    return aggfc;
 
1789
  
 
1790
  /* fill in with Py_None so we know it is valid */
 
1791
  aggfc->aggvalue=Py_None;
 
1792
  Py_INCREF(Py_None);
 
1793
 
 
1794
  cbinfo=(funccbinfo*)sqlite3_user_data(context);
 
1795
  assert(cbinfo);
 
1796
  assert(cbinfo->aggregatefactory);
 
1797
 
 
1798
  /* call the aggregatefactory to get our working objects */
 
1799
  retval=PyEval_CallObject(cbinfo->aggregatefactory, NULL);
 
1800
 
 
1801
  if(!retval)
 
1802
    return aggfc;
 
1803
  /* it should have returned a tuple of 3 items: object, stepfunction and finalfunction */
 
1804
  if(!PyTuple_Check(retval))
 
1805
    {
 
1806
      PyErr_Format(PyExc_TypeError, "Aggregate factory should return tuple of (object, stepfunction, finalfunction)");
 
1807
      goto finally;
 
1808
    }
 
1809
  if(PyTuple_GET_SIZE(retval)!=3)
 
1810
    {
 
1811
      PyErr_Format(PyExc_TypeError, "Aggregate factory should return 3 item tuple of (object, stepfunction, finalfunction)");
 
1812
      goto finally;
 
1813
    }
 
1814
  /* we don't care about the type of the zeroth item (object) ... */
 
1815
 
 
1816
  /* stepfunc */
 
1817
  if (!PyCallable_Check(PyTuple_GET_ITEM(retval,1)))
 
1818
    {
 
1819
      PyErr_Format(PyExc_TypeError, "stepfunction must be callable");
 
1820
      goto finally;
 
1821
    }
 
1822
  
 
1823
  /* finalfunc */
 
1824
  if (!PyCallable_Check(PyTuple_GET_ITEM(retval,2)))
 
1825
    {
 
1826
      PyErr_Format(PyExc_TypeError, "final function must be callable");
 
1827
      goto finally;
 
1828
    }
 
1829
 
 
1830
  aggfc->aggvalue=PyTuple_GET_ITEM(retval,0);
 
1831
  aggfc->stepfunc=PyTuple_GET_ITEM(retval,1);
 
1832
  aggfc->finalfunc=PyTuple_GET_ITEM(retval,2);
 
1833
 
 
1834
  Py_INCREF(aggfc->aggvalue);
 
1835
  Py_INCREF(aggfc->stepfunc);
 
1836
  Py_INCREF(aggfc->finalfunc);
 
1837
      
 
1838
  Py_DECREF(Py_None);  /* we used this earlier as a sentinel */
 
1839
 
 
1840
 finally:
 
1841
  assert(retval);
 
1842
  Py_DECREF(retval);
 
1843
  return aggfc;
 
1844
}
 
1845
 
 
1846
 
 
1847
/*
 
1848
  Note that we can't call sqlite3_result_error in the step function as
 
1849
  SQLite doesn't want to you to do that (and core dumps!)
 
1850
  Consequently if an error is returned, we will still be repeatedly
 
1851
  called.
 
1852
*/
 
1853
 
 
1854
static void
 
1855
cbdispatch_step(sqlite3_context *context, int argc, sqlite3_value **argv)
 
1856
{
 
1857
  PyGILState_STATE gilstate;
 
1858
  PyObject *pyargs;
 
1859
  PyObject *retval;
 
1860
  aggregatefunctioncontext *aggfc=NULL;
 
1861
 
 
1862
  gilstate=PyGILState_Ensure();
 
1863
 
 
1864
  if (PyErr_Occurred())
 
1865
    goto finalfinally;
 
1866
 
 
1867
  aggfc=getaggregatefunctioncontext(context);
 
1868
 
 
1869
  if (PyErr_Occurred())
 
1870
    goto finally;
 
1871
 
 
1872
  assert(aggfc);
 
1873
  
 
1874
  pyargs=getfunctionargs(context, aggfc->aggvalue, argc, argv);
 
1875
  if(!pyargs)
 
1876
    goto finally;
 
1877
 
 
1878
  assert(!PyErr_Occurred());
 
1879
  retval=PyEval_CallObject(aggfc->stepfunc, pyargs);
 
1880
  Py_DECREF(pyargs);
 
1881
  Py_XDECREF(retval);
 
1882
 
 
1883
  if(!retval)
 
1884
    {
 
1885
      assert(PyErr_Occurred());
 
1886
    }
 
1887
 
 
1888
 finally:
 
1889
  if(PyErr_Occurred())
 
1890
    {
 
1891
      char *funname=0;
 
1892
      funccbinfo *cbinfo=(funccbinfo*)sqlite3_user_data(context);
 
1893
      assert(cbinfo);
 
1894
      funname=sqlite3_mprintf("user-defined-aggregate-step-%s", cbinfo->name);
 
1895
      AddTraceBackHere(__FILE__, __LINE__, funname, "{s: i}", "NumberOfArguments", argc);
 
1896
      sqlite3_free(funname);
 
1897
    }
 
1898
 finalfinally:
 
1899
  PyGILState_Release(gilstate);
 
1900
}
 
1901
 
 
1902
/* this is somewhat similar to cbdispatch_step, except we also have to
 
1903
   do some cleanup of the aggregatefunctioncontext */
 
1904
static void
 
1905
cbdispatch_final(sqlite3_context *context)
 
1906
{
 
1907
  PyGILState_STATE gilstate;
 
1908
  PyObject *pyargs=NULL;
 
1909
  PyObject *retval=NULL;
 
1910
  aggregatefunctioncontext *aggfc=NULL;
 
1911
  PyObject *err_type=NULL, *err_value=NULL, *err_traceback=NULL;
 
1912
 
 
1913
  gilstate=PyGILState_Ensure();
 
1914
 
 
1915
  PyErr_Fetch(&err_type, &err_value, &err_traceback);
 
1916
  PyErr_Clear();
 
1917
 
 
1918
  aggfc=getaggregatefunctioncontext(context);
 
1919
 
 
1920
  assert(aggfc);
 
1921
  
 
1922
  if((err_type||err_value||err_traceback) || PyErr_Occurred() || !aggfc->finalfunc)
 
1923
    {
 
1924
      sqlite3_result_error(context, "Prior Python Error in step function", -1);
 
1925
      goto finally;
 
1926
    }
 
1927
 
 
1928
  pyargs=Py_BuildValue("(O)", aggfc->aggvalue);
 
1929
  if(!pyargs) goto finally;
 
1930
 
 
1931
  retval=PyEval_CallObject(aggfc->finalfunc, pyargs);
 
1932
  Py_DECREF(pyargs);
 
1933
  set_context_result(context, retval);
 
1934
  Py_XDECREF(retval);
 
1935
 
 
1936
 finally:
 
1937
  /* we also free the aggregatefunctioncontext here */
 
1938
  assert(aggfc->aggvalue);  /* should always be set, perhaps to Py_None */
 
1939
  Py_XDECREF(aggfc->aggvalue);
 
1940
  Py_XDECREF(aggfc->stepfunc);
 
1941
  Py_XDECREF(aggfc->finalfunc);
 
1942
 
 
1943
  if(PyErr_Occurred() && (err_type||err_value||err_traceback))
 
1944
    {
 
1945
      PyErr_Format(PyExc_StandardError, "An exception happened during cleanup of an aggregate function, but there was already error in the step function so only that can be returned");
 
1946
      apsw_write_unraiseable();
 
1947
    }
 
1948
 
 
1949
  if(err_type||err_value||err_traceback)
 
1950
    PyErr_Restore(err_type, err_value, err_traceback);
 
1951
 
 
1952
  if(PyErr_Occurred())
 
1953
    {
 
1954
      char *funname=0;
 
1955
      funccbinfo *cbinfo=(funccbinfo*)sqlite3_user_data(context);
 
1956
      assert(cbinfo);
 
1957
      funname=sqlite3_mprintf("user-defined-aggregate-final-%s", cbinfo->name);
 
1958
      AddTraceBackHere(__FILE__, __LINE__, funname, NULL);
 
1959
      sqlite3_free(funname);
 
1960
    }
 
1961
 
 
1962
  /* sqlite3 frees the actual underlying memory we used (aggfc itself) */
 
1963
 
 
1964
  PyGILState_Release(gilstate);
 
1965
}
 
1966
 
 
1967
 
 
1968
static PyObject *
 
1969
Connection_createscalarfunction(Connection *self, PyObject *args)
 
1970
{
 
1971
  int numargs=-1;
 
1972
  PyObject *callable;
 
1973
  char *name=0;
 
1974
  char *chk;
 
1975
  funccbinfo *cbinfo;
 
1976
  int res;
 
1977
 
 
1978
  CHECK_THREAD(self,NULL);
 
1979
  CHECK_CLOSED(self,NULL);
 
1980
 
 
1981
  if(!PyArg_ParseTuple(args, "esO|i:createscalarfunction(name,callback, numargs=-1)", STRENCODING, &name, &callable, &numargs))
 
1982
    return NULL;
 
1983
 
 
1984
  assert(name);
 
1985
  assert(callable);
 
1986
 
 
1987
  /* there isn't a C api to get a (potentially unicode) string and
 
1988
     make it uppercase so we hack around  */
 
1989
 
 
1990
  /* validate the name */
 
1991
  for(chk=name;*chk && !((*chk)&0x80);chk++);
 
1992
  if(*chk)
 
1993
    {
 
1994
      PyMem_Free(name);
 
1995
      PyErr_SetString(PyExc_TypeError, "function name must be ascii characters only");
 
1996
      return NULL;
 
1997
    }
 
1998
 
 
1999
  /* convert name to upper case */
 
2000
  for(chk=name;*chk;chk++)
 
2001
    if(*chk>='a' && *chk<='z')
 
2002
      *chk-='a'-'A';
 
2003
 
 
2004
  /* ::TODO:: check if name points to already defined function and free relevant funccbinfo */
 
2005
 
 
2006
  if(callable!=Py_None && !PyCallable_Check(callable))
 
2007
    {
 
2008
      PyMem_Free(name);
 
2009
      PyErr_SetString(PyExc_TypeError, "parameter must be callable");
 
2010
      return NULL;
 
2011
    }
 
2012
 
 
2013
  Py_INCREF(callable);
 
2014
 
 
2015
  cbinfo=allocfunccbinfo();
 
2016
  cbinfo->name=name;
 
2017
  cbinfo->scalarfunc=callable;
 
2018
 
 
2019
  res=sqlite3_create_function(self->db,
 
2020
                              name,
 
2021
                              numargs,
 
2022
                              SQLITE_UTF8,  /* it isn't very clear what this parameter does */
 
2023
                              (callable!=Py_None)?cbinfo:NULL,
 
2024
                              (callable!=Py_None)?cbdispatch_func:NULL,
 
2025
                              NULL,
 
2026
                              NULL);
 
2027
 
 
2028
  if(res)
 
2029
    {
 
2030
      freefunccbinfo(cbinfo);
 
2031
      SET_EXC(self->db, res);
 
2032
      return NULL;
 
2033
    }
 
2034
 
 
2035
  if(callable!=Py_None)
 
2036
    {
 
2037
      /* put cbinfo into the linked list */
 
2038
      cbinfo->next=self->functions;
 
2039
      self->functions=cbinfo;
 
2040
    }
 
2041
  else
 
2042
    {
 
2043
      /* free it since we cancelled the function */
 
2044
      freefunccbinfo(cbinfo);
 
2045
    }
 
2046
  
 
2047
  return Py_BuildValue("");
 
2048
}
 
2049
 
 
2050
static PyObject *
 
2051
Connection_createaggregatefunction(Connection *self, PyObject *args)
 
2052
{
 
2053
  int numargs=-1;
 
2054
  PyObject *callable;
 
2055
  char *name=0;
 
2056
  char *chk;
 
2057
  funccbinfo *cbinfo;
 
2058
  int res;
 
2059
 
 
2060
  CHECK_THREAD(self,NULL);
 
2061
  CHECK_CLOSED(self,NULL);
 
2062
 
 
2063
  if(!PyArg_ParseTuple(args, "esO|i:createaggregatefunction(name, factorycallback, numargs=-1)", STRENCODING, &name, &callable, &numargs))
 
2064
    return NULL;
 
2065
 
 
2066
  assert(name);
 
2067
  assert(callable);
 
2068
 
 
2069
  /* there isn't a C api to get a (potentially unicode) string and make it uppercase so we hack around  */
 
2070
 
 
2071
  /* validate the name */
 
2072
  for(chk=name;*chk && !((*chk)&0x80);chk++);
 
2073
  if(*chk)
 
2074
    {
 
2075
      PyMem_Free(name);
 
2076
      PyErr_SetString(PyExc_TypeError, "function name must be ascii characters only");
 
2077
      return NULL;
 
2078
    }
 
2079
 
 
2080
  /* convert name to upper case */
 
2081
  for(chk=name;*chk;chk++)
 
2082
    if(*chk>='a' && *chk<='z')
 
2083
      *chk-='a'-'A';
 
2084
 
 
2085
  /* ::TODO:: check if name points to already defined function and free relevant funccbinfo */
 
2086
 
 
2087
  if(callable!=Py_None && !PyCallable_Check(callable))
 
2088
    {
 
2089
      PyMem_Free(name);
 
2090
      PyErr_SetString(PyExc_TypeError, "parameter must be callable");
 
2091
      return NULL;
 
2092
    }
 
2093
 
 
2094
  Py_INCREF(callable);
 
2095
 
 
2096
  cbinfo=allocfunccbinfo();
 
2097
  cbinfo->name=name;
 
2098
  cbinfo->aggregatefactory=callable;
 
2099
 
 
2100
  res=sqlite3_create_function(self->db,
 
2101
                              name,
 
2102
                              numargs,
 
2103
                              SQLITE_UTF8,  /* it isn't very clear what this parameter does */
 
2104
                              (callable!=Py_None)?cbinfo:NULL,
 
2105
                              NULL,
 
2106
                              (callable!=Py_None)?cbdispatch_step:NULL,
 
2107
                              (callable!=Py_None)?cbdispatch_final:NULL);
 
2108
 
 
2109
  if(res)
 
2110
    {
 
2111
      freefunccbinfo(cbinfo);
 
2112
      SET_EXC(self->db, res);
 
2113
      return NULL;
 
2114
    }
 
2115
 
 
2116
  if(callable!=Py_None)
 
2117
    {
 
2118
      /* put cbinfo into the linked list */
 
2119
      cbinfo->next=self->functions;
 
2120
      self->functions=cbinfo;
 
2121
    }
 
2122
  else
 
2123
    {
 
2124
      /* free things up */
 
2125
      freefunccbinfo(cbinfo);
 
2126
    }
 
2127
  
 
2128
  return Py_BuildValue("");
 
2129
}
 
2130
 
 
2131
/* USER DEFINED COLLATION CODE.*/
 
2132
 
 
2133
/*  We store the registered collations in a linked list hooked into
 
2134
   the connection object so we can free them.  There is probably a
 
2135
   better data structure to use but this was most convenient. */
 
2136
 
 
2137
static collationcbinfo *
 
2138
freecollationcbinfo(collationcbinfo *collation)
 
2139
{
 
2140
  collationcbinfo *cnext;
 
2141
  if(!collation) 
 
2142
    return NULL;
 
2143
 
 
2144
  if(collation->name)
 
2145
    PyMem_Free(collation->name);
 
2146
  Py_XDECREF(collation->func);
 
2147
  cnext=collation->next;
 
2148
  PyMem_Free(collation);
 
2149
  return cnext;
 
2150
}
 
2151
 
 
2152
static collationcbinfo *
 
2153
alloccollationcbinfo(void)
 
2154
{
 
2155
  collationcbinfo *res=PyMem_Malloc(sizeof(collationcbinfo));
 
2156
  memset(res, 0, sizeof(collationcbinfo));
 
2157
  return res;
 
2158
}
 
2159
 
 
2160
static int 
 
2161
collation_cb(void *context, 
 
2162
             int stringonelen, const void *stringonedata,
 
2163
             int stringtwolen, const void *stringtwodata)
 
2164
{
 
2165
  PyGILState_STATE gilstate;
 
2166
  collationcbinfo *cbinfo=(collationcbinfo*)context;
 
2167
  PyObject *pys1=NULL, *pys2=NULL, *retval=NULL, *pyargs=NULL;
 
2168
  int result=0;
 
2169
 
 
2170
  assert(cbinfo);
 
2171
 
 
2172
  gilstate=PyGILState_Ensure();
 
2173
 
 
2174
  if(PyErr_Occurred()) goto finally;  /* outstanding error */
 
2175
 
 
2176
  pys1=convertutf8stringsize(stringonedata, stringonelen);
 
2177
  pys2=convertutf8stringsize(stringtwodata, stringtwolen);
 
2178
 
 
2179
  if(!pys1 || !pys2)  
 
2180
    goto finally;   /* failed to allocate strings */
 
2181
 
 
2182
  pyargs=Py_BuildValue("(NN)", pys1, pys2);
 
2183
  if(!pyargs) 
 
2184
    goto finally; /* failed to allocate arg tuple */
 
2185
 
 
2186
  pys1=pys2=NULL;  /* pyargs owns them now */
 
2187
 
 
2188
  assert(!PyErr_Occurred());
 
2189
 
 
2190
  retval=PyEval_CallObject(cbinfo->func, pyargs);
 
2191
 
 
2192
  if(!retval) goto finally;  /* execution failed */
 
2193
 
 
2194
  result=PyInt_AsLong(retval);
 
2195
  if(PyErr_Occurred())
 
2196
      result=0;
 
2197
 
 
2198
 
 
2199
 finally:
 
2200
  Py_XDECREF(pys1);
 
2201
  Py_XDECREF(pys2);
 
2202
  Py_XDECREF(retval);
 
2203
  Py_XDECREF(pyargs);
 
2204
  PyGILState_Release(gilstate);
 
2205
  return result;
 
2206
 
 
2207
}
 
2208
 
 
2209
static PyObject *
 
2210
Connection_createcollation(Connection *self, PyObject *args)
 
2211
{
 
2212
  PyObject *callable;
 
2213
  char *name=0;
 
2214
  char *chk;
 
2215
  collationcbinfo *cbinfo;
 
2216
  int res;
 
2217
 
 
2218
  CHECK_THREAD(self,NULL);
 
2219
  CHECK_CLOSED(self,NULL);
 
2220
  
 
2221
  if(!PyArg_ParseTuple(args, "esO:createcollation(name,callback)", STRENCODING, &name, &callable))
 
2222
    return NULL;
 
2223
 
 
2224
  assert(name);
 
2225
  assert(callable);
 
2226
 
 
2227
  /* there isn't a C api to get a (potentially unicode) string and make it uppercase so we hack around  */
 
2228
 
 
2229
  /* validate the name */
 
2230
  for(chk=name;*chk && !((*chk)&0x80);chk++);
 
2231
  if(*chk)
 
2232
    {
 
2233
      PyMem_Free(name);
 
2234
      PyErr_SetString(PyExc_TypeError, "function name must be ascii characters only");
 
2235
      return NULL;
 
2236
    }
 
2237
 
 
2238
  /* convert name to upper case */
 
2239
  for(chk=name;*chk;chk++)
 
2240
    if(*chk>='a' && *chk<='z')
 
2241
      *chk-='a'-'A';
 
2242
 
 
2243
  /* ::TODO:: check if name points to already defined collation and free relevant collationcbinfo */
 
2244
 
 
2245
  if(callable!=Py_None && !PyCallable_Check(callable))
 
2246
    {
 
2247
      PyMem_Free(name);
 
2248
      PyErr_SetString(PyExc_TypeError, "parameter must be callable");
 
2249
      return NULL;
 
2250
    }
 
2251
 
 
2252
  Py_INCREF(callable);
 
2253
 
 
2254
  cbinfo=alloccollationcbinfo();
 
2255
  cbinfo->name=name;
 
2256
  cbinfo->func=callable;
 
2257
 
 
2258
  res=sqlite3_create_collation(self->db,
 
2259
                               name,
 
2260
                               SQLITE_UTF8,
 
2261
                               (callable!=Py_None)?cbinfo:NULL,
 
2262
                               (callable!=Py_None)?collation_cb:NULL);
 
2263
  if(res)
 
2264
    {
 
2265
      freecollationcbinfo(cbinfo);
 
2266
      SET_EXC(self->db, res);
 
2267
      return NULL;
 
2268
    }
 
2269
 
 
2270
  if (callable!=Py_None)
 
2271
    {
 
2272
      /* put cbinfo into the linked list */
 
2273
      cbinfo->next=self->collations;
 
2274
      self->collations=cbinfo;
 
2275
    }
 
2276
  else
 
2277
    {
 
2278
      /* destroy info */
 
2279
      freecollationcbinfo(cbinfo);
 
2280
    }
 
2281
  
 
2282
  return Py_BuildValue("");
 
2283
}
 
2284
 
 
2285
/* Virtual table code */
 
2286
 
 
2287
/* this function is outside of experimental since it is always called by the destructor */
 
2288
static vtableinfo *
 
2289
freevtableinfo(vtableinfo *vtinfo)
 
2290
{
 
2291
  vtableinfo *next;
 
2292
  if(!vtinfo)
 
2293
    return NULL;
 
2294
 
 
2295
  if(vtinfo->name)
 
2296
    PyMem_Free(vtinfo->name);
 
2297
  Py_XDECREF(vtinfo->datasource);
 
2298
  /* connection was a borrowed reference so no decref needed */
 
2299
 
 
2300
  next=vtinfo->next;
 
2301
  PyMem_Free(vtinfo);
 
2302
  return next;
 
2303
}
 
2304
 
 
2305
 
 
2306
#ifdef EXPERIMENTAL
 
2307
 
 
2308
/* Calls the named method of object with the provided args */
 
2309
static PyObject*
 
2310
Call_PythonMethod(PyObject *obj, const char *methodname, PyObject *args, int mandatory)
 
2311
{
 
2312
  PyObject *method=NULL;
 
2313
  PyObject *res=NULL;
 
2314
 
 
2315
  /* we may be called when there is already an error.  eg if you return an error in
 
2316
     a cursor method, then SQLite calls vtabClose which calls us.  We don't want to 
 
2317
     clear pre-existing errors, but we do want to clear ones when the function doesn't
 
2318
     exist but is optional */
 
2319
  PyObject *etype=NULL, *evalue=NULL, *etraceback=NULL;
 
2320
  void *pyerralreadyoccurred=PyErr_Occurred();
 
2321
  if(pyerralreadyoccurred)
 
2322
    PyErr_Fetch(&etype, &evalue, &etraceback);
 
2323
 
 
2324
 
 
2325
  /* we should only be called with ascii methodnames so no need to do
 
2326
     character set conversions etc */
 
2327
#if PY_VERSION_HEX < 0x02050000
 
2328
  method=PyObject_GetAttrString(obj, (char*)methodname);
 
2329
#else
 
2330
  method=PyObject_GetAttrString(obj, methodname);
 
2331
#endif
 
2332
  if (!method)
 
2333
    {
 
2334
      if(!mandatory)
 
2335
        {
 
2336
          /* pretend method existed and returned None */
 
2337
          PyErr_Clear();
 
2338
          res=Py_None;
 
2339
          Py_INCREF(res);
 
2340
        }
 
2341
      goto finally;
 
2342
    }
 
2343
 
 
2344
  res=PyEval_CallObject(method, args);
 
2345
 
 
2346
 finally:
 
2347
  if(pyerralreadyoccurred)
 
2348
    PyErr_Restore(etype, evalue, etraceback);
 
2349
  Py_XDECREF(method);
 
2350
  return res;
 
2351
}
 
2352
 
 
2353
 
 
2354
 
 
2355
typedef struct {
 
2356
  sqlite3_vtab used_by_sqlite; /* I don't touch this */
 
2357
  PyObject *vtable;            /* object implementing vtable */
 
2358
} apsw_vtable;
 
2359
 
 
2360
static struct {
 
2361
  const char *methodname;
 
2362
  const char *declarevtabtracebackname;
 
2363
  const char *pyexceptionname;
 
2364
} create_or_connect_strings[]=
 
2365
  {
 
2366
    {
 
2367
      "Create",
 
2368
      "VirtualTable.xCreate.sqlite3_declare_vtab",
 
2369
      "VirtualTable.xCreate"
 
2370
    },
 
2371
    {
 
2372
      "Connect",
 
2373
      "VirtualTable.xConnect.sqlite3_declare_vtab",
 
2374
      "VirtualTable.xConnect"
 
2375
    }
 
2376
  };
 
2377
 
 
2378
static int 
 
2379
vtabCreateOrConnect(sqlite3 *db, 
 
2380
                    void *pAux, 
 
2381
                    int argc, 
 
2382
                    const char *const *argv,
 
2383
                    sqlite3_vtab **pVTab,
 
2384
                    char **errmsg,
 
2385
                    /* args above are to Create/Connect method */
 
2386
                    int stringindex)
 
2387
{
 
2388
  PyGILState_STATE gilstate;
 
2389
  vtableinfo *vti;
 
2390
  PyObject *args=NULL, *res=NULL, *schema=NULL, *vtable=NULL;
 
2391
  apsw_vtable *avi=NULL;
 
2392
  int sqliteres=SQLITE_OK;
 
2393
  int i;
 
2394
  
 
2395
  gilstate=PyGILState_Ensure();
 
2396
 
 
2397
  vti=(vtableinfo*) pAux;
 
2398
  assert(db==vti->connection->db);
 
2399
 
 
2400
  args=PyTuple_New(1+argc);
 
2401
  if(!args) goto pyexception;
 
2402
 
 
2403
  Py_INCREF((PyObject*)(vti->connection));
 
2404
  PyTuple_SET_ITEM(args, 0, (PyObject*)(vti->connection));
 
2405
  for(i=0;i<argc;i++)
 
2406
    {
 
2407
      PyObject *str=convertutf8string(argv[i]);
 
2408
      if(!str) 
 
2409
        goto pyexception;
 
2410
      PyTuple_SET_ITEM(args, 1+i, str);
 
2411
    }
 
2412
 
 
2413
  res=Call_PythonMethod(vti->datasource, create_or_connect_strings[stringindex].methodname, args, 1);
 
2414
  if(!res)
 
2415
    goto pyexception;
 
2416
 
 
2417
  /* res should be a tuple of two values - a string of sql describing
 
2418
     the table and an object implementing it */
 
2419
  if(!PySequence_Check(res) || PySequence_Size(res)!=2)
 
2420
    {
 
2421
      PyErr_Format(PyExc_TypeError, "Expected two values - a string with the table schema and a vtable object implementing it");
 
2422
      goto pyexception;
 
2423
    }
 
2424
  
 
2425
  vtable=PySequence_GetItem(res, 1);
 
2426
  if(!vtable)
 
2427
    goto pyexception;
 
2428
 
 
2429
  avi=PyMem_Malloc(sizeof(apsw_vtable));
 
2430
  if(!avi) goto pyexception;
 
2431
  assert((void*)avi==(void*)&(avi->used_by_sqlite)); /* detect if wierd padding happens */
 
2432
  memset(avi, 0, sizeof(apsw_vtable));
 
2433
 
 
2434
  schema=PySequence_GetItem(res, 0);
 
2435
  if(!schema) goto pyexception;
 
2436
 
 
2437
  {
 
2438
    PyObject *utf8schema=getutf8string(schema);
 
2439
    if(!utf8schema) 
 
2440
      goto pyexception;
 
2441
    sqliteres=sqlite3_declare_vtab(db, PyString_AsString(utf8schema));
 
2442
    Py_DECREF(utf8schema);
 
2443
    if(sqliteres!=SQLITE_OK)
 
2444
      {
 
2445
        SET_EXC(db, sqliteres);
 
2446
        AddTraceBackHere(__FILE__, __LINE__,  create_or_connect_strings[stringindex].declarevtabtracebackname, "{s: O}", "schema", schema);
 
2447
        goto finally;
 
2448
      }
 
2449
  }
 
2450
  
 
2451
  assert(sqliteres==SQLITE_OK);
 
2452
  *pVTab=(sqlite3_vtab*)avi;
 
2453
  avi->vtable=vtable;
 
2454
  Py_INCREF(avi->vtable);
 
2455
  avi=NULL;
 
2456
  goto finally;
 
2457
 
 
2458
 pyexception: /* we had an exception in python code */
 
2459
  sqliteres=MakeSqliteMsgFromPyException(errmsg);
 
2460
  AddTraceBackHere(__FILE__, __LINE__, create_or_connect_strings[stringindex].pyexceptionname, 
 
2461
                   "{s: s, s: s, s: s, s: O}", "modulename", argv[0], "database", argv[1], "tablename", argv[2], "schema", schema?schema:Py_None);
 
2462
 
 
2463
 finally: /* cleanup */
 
2464
  Py_XDECREF(args);  
 
2465
  Py_XDECREF(res);
 
2466
  Py_XDECREF(schema);
 
2467
  Py_XDECREF(vtable);
 
2468
  if(avi)
 
2469
    PyMem_Free(avi);
 
2470
 
 
2471
  PyGILState_Release(gilstate);
 
2472
  return sqliteres;
 
2473
}
 
2474
 
 
2475
static int 
 
2476
vtabCreate(sqlite3 *db, 
 
2477
           void *pAux, 
 
2478
           int argc, 
 
2479
           const char *const *argv,
 
2480
           sqlite3_vtab **pVTab,
 
2481
           char **errmsg)
 
2482
{
 
2483
  return vtabCreateOrConnect(db, pAux, argc, argv, pVTab, errmsg, 0);
 
2484
}
 
2485
 
 
2486
static int 
 
2487
vtabConnect(sqlite3 *db, 
 
2488
           void *pAux, 
 
2489
           int argc, 
 
2490
           const char *const *argv,
 
2491
           sqlite3_vtab **pVTab,
 
2492
           char **errmsg)
 
2493
{
 
2494
  return vtabCreateOrConnect(db, pAux, argc, argv, pVTab, errmsg, 1);
 
2495
}
 
2496
 
 
2497
 
 
2498
static struct
 
2499
{
 
2500
  const char *methodname;
 
2501
  const char *pyexceptionname;
 
2502
} destroy_disconnect_strings[]=
 
2503
  {
 
2504
    {
 
2505
      "Destroy",
 
2506
      "VirtualTable.xDestroy"
 
2507
    },
 
2508
    {
 
2509
      "Disconnect",
 
2510
      "VirtualTable.xDisconnect"
 
2511
    }
 
2512
  };
 
2513
 
 
2514
/* See SQLite ticket 2099 */
 
2515
static int
 
2516
vtabDestroyOrDisconnect(sqlite3_vtab *pVtab, int stringindex)
 
2517
 
2518
  PyObject *vtable, *res=NULL;
 
2519
  PyGILState_STATE gilstate;
 
2520
  int sqliteres=SQLITE_OK;
 
2521
 
 
2522
  gilstate=PyGILState_Ensure();
 
2523
  vtable=((apsw_vtable*)pVtab)->vtable;
 
2524
 
 
2525
  /* mandatory for Destroy, optional for Disconnect */
 
2526
  res=Call_PythonMethod(vtable, destroy_disconnect_strings[stringindex].methodname, NULL, (stringindex==0));
 
2527
  /* sqlite 3.3.8 ignore return code for disconnect so we always free */
 
2528
  if (res || stringindex==1)
 
2529
    {
 
2530
      /* see SQLite ticket 2127 */
 
2531
      if(pVtab->zErrMsg)
 
2532
        sqlite3_free(pVtab->zErrMsg);
 
2533
      
 
2534
      Py_DECREF(vtable);
 
2535
      PyMem_Free(pVtab);
 
2536
      goto finally;
 
2537
    }
 
2538
 
 
2539
  if(stringindex==0)
 
2540
    {
 
2541
      /* ::TODO:: waiting on ticket 2099 to know if the pVtab should also be freed in case of error return with Destroy. */
 
2542
#if 0
 
2543
      /* see SQLite ticket 2127 */
 
2544
      if(pVtab->zErrMsg)
 
2545
        sqlite3_free(pVtab->zErrMsg);
 
2546
      
 
2547
      Py_DECREF(vtable);
 
2548
      PyMem_Free(pVtab);    
 
2549
#endif
 
2550
    }
 
2551
 
 
2552
  /* pyexception:  we had an exception in python code */
 
2553
  sqliteres=MakeSqliteMsgFromPyException(&(pVtab->zErrMsg));
 
2554
  AddTraceBackHere(__FILE__, __LINE__,  destroy_disconnect_strings[stringindex].pyexceptionname, "{s: O}", "self", vtable);
 
2555
 
 
2556
 finally:
 
2557
  Py_XDECREF(res);
 
2558
 
 
2559
  PyGILState_Release(gilstate);
 
2560
  return sqliteres;
 
2561
}
 
2562
 
 
2563
static int
 
2564
vtabDestroy(sqlite3_vtab *pVTab)
 
2565
{
 
2566
  return vtabDestroyOrDisconnect(pVTab, 0);
 
2567
}
 
2568
 
 
2569
static int
 
2570
vtabDisconnect(sqlite3_vtab *pVTab)
 
2571
{
 
2572
  return vtabDestroyOrDisconnect(pVTab, 1);
 
2573
}
 
2574
 
 
2575
 
 
2576
static int
 
2577
vtabBestIndex(sqlite3_vtab *pVtab, sqlite3_index_info *indexinfo)
 
2578
{
 
2579
  PyGILState_STATE gilstate;
 
2580
  PyObject *vtable;
 
2581
  PyObject *constraints=NULL, *orderbys=NULL;
 
2582
  PyObject *args=NULL, *res=NULL, *indices=NULL;
 
2583
  int i,j;
 
2584
  int nconstraints=0;
 
2585
  int sqliteres=SQLITE_OK;
 
2586
 
 
2587
  gilstate=PyGILState_Ensure();
 
2588
 
 
2589
  vtable=((apsw_vtable*)pVtab)->vtable;
 
2590
  
 
2591
  /* count how many usable constraints there are */
 
2592
  for(i=0;i<indexinfo->nConstraint;i++)
 
2593
    if (indexinfo->aConstraint[i].usable)
 
2594
      nconstraints++;
 
2595
 
 
2596
  constraints=PyTuple_New(nconstraints);
 
2597
  if(!constraints) goto pyexception;
 
2598
  
 
2599
  /* fill them in */
 
2600
  for(i=0, j=0;i<indexinfo->nConstraint;i++)
 
2601
    {
 
2602
      PyObject *constraint=NULL;
 
2603
      if(!indexinfo->aConstraint[i].usable) continue;
 
2604
      
 
2605
      constraint=Py_BuildValue("(iB)", indexinfo->aConstraint[i].iColumn, indexinfo->aConstraint[i].op);
 
2606
      if(!constraint) goto pyexception;
 
2607
 
 
2608
      PyTuple_SET_ITEM(constraints, j, constraint);
 
2609
      j++;
 
2610
    }
 
2611
 
 
2612
  /* group bys */
 
2613
  orderbys=PyTuple_New(indexinfo->nOrderBy);
 
2614
  if(!orderbys) goto pyexception;
 
2615
 
 
2616
  /* fill them in */
 
2617
  for(i=0;i<indexinfo->nOrderBy;i++)
 
2618
    {
 
2619
      PyObject *order=NULL;
 
2620
 
 
2621
      order=Py_BuildValue("(iN)", indexinfo->aOrderBy[i].iColumn, PyBool_FromLong(indexinfo->aOrderBy[i].desc));
 
2622
      if(!order) goto pyexception;
 
2623
 
 
2624
      PyTuple_SET_ITEM(orderbys, i, order);
 
2625
    }
 
2626
 
 
2627
  /* actually call the function */
 
2628
  args=Py_BuildValue("(NN)", constraints, orderbys);
 
2629
  if(!args)
 
2630
    goto pyexception;
 
2631
  constraints=orderbys=NULL; /* owned by the tuple now */
 
2632
 
 
2633
  res=Call_PythonMethod(vtable, "BestIndex", args, 1);
 
2634
  if(!res)
 
2635
    goto pyexception;
 
2636
 
 
2637
  /* do we have useful index information? */
 
2638
  if(res==Py_None)
 
2639
    goto finally;
 
2640
 
 
2641
  /* check we have a sequence */
 
2642
  if(!PySequence_Check(res) || PySequence_Size(res)>5)
 
2643
    {
 
2644
      PyErr_Format(PyExc_TypeError, "Bad result from BestIndex.  It should be a sequence of up to 5 items");
 
2645
      AddTraceBackHere(__FILE__, __LINE__, "VirtualTable.xBestIndex.result_check", "{s: O, s: O}", "self", vtable, "result", res);
 
2646
      goto pyexception;
 
2647
    }
 
2648
 
 
2649
  /* dig the argv indices out */
 
2650
  if(PySequence_Size(res)==0)
 
2651
    goto finally;
 
2652
 
 
2653
  indices=PySequence_GetItem(res, 0);
 
2654
  if(indices!=Py_None)
 
2655
    {
 
2656
      if(!PySequence_Check(indices) || PySequence_Size(indices)!=nconstraints)
 
2657
        {
 
2658
          PyErr_Format(PyExc_TypeError, "Bad constraints (item 0 in BestIndex return).  It should be a sequence the same length as the constraints passed in (%d) items", nconstraints);
 
2659
          AddTraceBackHere(__FILE__, __LINE__, "VirtualTable.xBestIndex.result_indices", "{s: O, s: O, s: O}", 
 
2660
                           "self", vtable, "result", res, "indices", indices);
 
2661
          goto pyexception;
 
2662
        }
 
2663
      /* iterate through the items - i is the SQLite sequence number and j is the apsw one (usable entries) */
 
2664
      for(i=0,j=0;i<indexinfo->nConstraint;i++)
 
2665
        {
 
2666
          PyObject *constraint=NULL, *argvindex=NULL, *omit=NULL;
 
2667
          int omitv;
 
2668
          if(!indexinfo->aConstraint[i].usable) continue;
 
2669
          constraint=PySequence_GetItem(indices, j);
 
2670
          if(!constraint) goto pyexception;
 
2671
          j++;
 
2672
          /* it can be None */
 
2673
          if(constraint==Py_None)
 
2674
            {
 
2675
              Py_DECREF(constraint);
 
2676
              continue;
 
2677
            }
 
2678
          /* or an integer */
 
2679
          if(PyInt_Check(constraint))
 
2680
            {
 
2681
              indexinfo->aConstraintUsage[i].argvIndex=PyInt_AsLong(constraint);
 
2682
              Py_DECREF(constraint);
 
2683
              continue;
 
2684
            }
 
2685
          /* or a sequence two items long */
 
2686
          if(!PySequence_Check(constraint) || PySequence_Size(constraint)!=2)
 
2687
            {
 
2688
              PyErr_Format(PyExc_TypeError, "Bad constraint (#%d) - it should be one of None, an integer or a tuple of an integer and a boolean", j);
 
2689
              AddTraceBackHere(__FILE__, __LINE__, "VirtualTable.xBestIndex.result_constraint", "{s: O, s: O, s: O, s: O}", 
 
2690
                               "self", vtable, "result", res, "indices", indices, "constraint", constraint);
 
2691
              Py_DECREF(constraint);
 
2692
              goto pyexception;
 
2693
            }
 
2694
          argvindex=PySequence_GetItem(constraint, 0);
 
2695
          omit=PySequence_GetItem(constraint, 1);
 
2696
          if(!argvindex || !omit) goto constraintfail;
 
2697
          if(!PyInt_Check(argvindex))
 
2698
            {
 
2699
              PyErr_Format(PyExc_TypeError, "argvindex for constraint #%d should be an integer", j);
 
2700
              AddTraceBackHere(__FILE__, __LINE__, "VirtualTable.xBestIndex.result_constraint_argvindex", "{s: O, s: O, s: O, s: O, s: O}", 
 
2701
                               "self", vtable, "result", res, "indices", indices, "constraint", constraint, "argvindex", argvindex);
 
2702
              goto constraintfail;
 
2703
            }
 
2704
          omitv=PyObject_IsTrue(omit);
 
2705
          if(omitv==-1) goto constraintfail;
 
2706
          indexinfo->aConstraintUsage[i].argvIndex=PyInt_AsLong(argvindex);
 
2707
          indexinfo->aConstraintUsage[i].omit=omitv;
 
2708
          Py_DECREF(constraint);
 
2709
          Py_DECREF(argvindex);
 
2710
          Py_DECREF(omit);
 
2711
          continue;
 
2712
 
 
2713
        constraintfail:
 
2714
          Py_DECREF(constraint);
 
2715
          Py_XDECREF(argvindex);
 
2716
          Py_XDECREF(omit);
 
2717
          goto pyexception;
 
2718
        }
 
2719
    }
 
2720
 
 
2721
  /* item #1 is idxnum */
 
2722
  if(PySequence_Size(res)<2)
 
2723
    goto finally;
 
2724
  {
 
2725
    PyObject *idxnum=PySequence_GetItem(res, 1);
 
2726
    if(!idxnum) goto pyexception;
 
2727
    if(idxnum!=Py_None)
 
2728
      {
 
2729
        if(!PyInt_Check(idxnum))
 
2730
          {
 
2731
            PyErr_Format(PyExc_TypeError, "idxnum must be an integer");
 
2732
              AddTraceBackHere(__FILE__, __LINE__, "VirtualTable.xBestIndex.result_indexnum", "{s: O, s: O, s: O}", "self", vtable, "result", res, "indexnum", idxnum);
 
2733
            Py_DECREF(idxnum);
 
2734
            goto pyexception;
 
2735
          }
 
2736
        indexinfo->idxNum=PyInt_AsLong(idxnum);
 
2737
      }
 
2738
    Py_DECREF(idxnum);
 
2739
  }
 
2740
 
 
2741
  /* item #2 is idxStr */
 
2742
  if(PySequence_Size(res)<3)
 
2743
    goto finally;
 
2744
  {
 
2745
    PyObject *utf8str=NULL, *idxstr=NULL;
 
2746
    idxstr=PySequence_GetItem(res, 2);
 
2747
    if(!idxstr) goto pyexception;
 
2748
    if(idxstr!=Py_None)
 
2749
      {
 
2750
        utf8str=getutf8string(idxstr);
 
2751
        if(!utf8str)
 
2752
          {
 
2753
            Py_DECREF(idxstr);
 
2754
            goto pyexception;
 
2755
          }
 
2756
        indexinfo->idxStr=sqlite3_mprintf("%s", PyString_AsString(utf8str));
 
2757
        indexinfo->needToFreeIdxStr=1;
 
2758
      }
 
2759
    Py_XDECREF(utf8str);
 
2760
    Py_DECREF(idxstr);
 
2761
  }
 
2762
 
 
2763
  /* item 3 is orderByConsumed */
 
2764
  if(PySequence_Size(res)<4)
 
2765
    goto finally;
 
2766
  {
 
2767
    PyObject *orderbyconsumed=NULL;
 
2768
    int iorderbyconsumed;
 
2769
    orderbyconsumed=PySequence_GetItem(res, 3);
 
2770
    if(!orderbyconsumed) goto pyexception;
 
2771
    if(orderbyconsumed!=Py_None)
 
2772
      {
 
2773
        iorderbyconsumed=PyObject_IsTrue(orderbyconsumed);
 
2774
        if(iorderbyconsumed==-1)
 
2775
          {
 
2776
            Py_DECREF(orderbyconsumed);
 
2777
            goto pyexception;
 
2778
          }
 
2779
        indexinfo->orderByConsumed=iorderbyconsumed;
 
2780
      }
 
2781
    Py_DECREF(orderbyconsumed);
 
2782
  }
 
2783
 
 
2784
  /* item 4 (final) is estimated cost */
 
2785
  if(PySequence_Size(res)<5)
 
2786
    goto finally;
 
2787
  assert(PySequence_Size(res)==5);
 
2788
  {
 
2789
    PyObject *estimatedcost=NULL, *festimatedcost=NULL;
 
2790
    estimatedcost=PySequence_GetItem(res,4);
 
2791
    if(!estimatedcost) goto pyexception;
 
2792
    if(estimatedcost!=Py_None)
 
2793
      {
 
2794
        festimatedcost=PyNumber_Float(estimatedcost);
 
2795
        if(!festimatedcost)
 
2796
          {
 
2797
            Py_DECREF(estimatedcost);
 
2798
            goto pyexception;
 
2799
          }
 
2800
        indexinfo->estimatedCost=PyFloat_AsDouble(festimatedcost);
 
2801
      }
 
2802
    Py_XDECREF(festimatedcost);
 
2803
    Py_DECREF(estimatedcost);
 
2804
  }
 
2805
 
 
2806
  goto finally;
 
2807
 
 
2808
 pyexception: /* we had an exception in python code */
 
2809
  assert(PyErr_Occurred());
 
2810
  sqliteres=MakeSqliteMsgFromPyException(&(pVtab->zErrMsg));
 
2811
  AddTraceBackHere(__FILE__, __LINE__, "VirtualTable.xBestIndex", "{s: O, s: O, s: O}", "self", vtable, "result", res?res:Py_None, "args", args?args:Py_None);
 
2812
 
 
2813
 finally:
 
2814
  Py_XDECREF(indices);
 
2815
  Py_XDECREF(args);
 
2816
  Py_XDECREF(res);
 
2817
  Py_XDECREF(constraints);
 
2818
  Py_XDECREF(orderbys);
 
2819
  PyGILState_Release(gilstate);
 
2820
  return sqliteres;
 
2821
}
 
2822
 
 
2823
struct {
 
2824
  const char *methodname;
 
2825
  const char *pyexceptionname;
 
2826
} transaction_strings[]=
 
2827
  {
 
2828
    {
 
2829
      "Begin",
 
2830
      "VirtualTable.Begin"
 
2831
    },
 
2832
    {
 
2833
      "Sync",
 
2834
      "VirtualTable.Sync"
 
2835
    },
 
2836
    {
 
2837
      "Commit",
 
2838
      "VirtualTable.Commit"
 
2839
    },
 
2840
    {
 
2841
      "Rollback",
 
2842
      "VirtualTable.Rollback"
 
2843
    },
 
2844
 
 
2845
  };
 
2846
 
 
2847
static int
 
2848
vtabTransactionMethod(sqlite3_vtab *pVtab, int stringindex)
 
2849
{
 
2850
  PyObject *vtable, *res=NULL;
 
2851
  PyGILState_STATE gilstate;
 
2852
  int sqliteres=SQLITE_OK;
 
2853
 
 
2854
  gilstate=PyGILState_Ensure();
 
2855
  vtable=((apsw_vtable*)pVtab)->vtable;
 
2856
 
 
2857
  res=Call_PythonMethod(vtable, transaction_strings[stringindex].methodname, NULL, 0);
 
2858
  if(res) goto finally;
 
2859
 
 
2860
  /*  pyexception: we had an exception in python code */
 
2861
  sqliteres=MakeSqliteMsgFromPyException(&(pVtab->zErrMsg));
 
2862
  AddTraceBackHere(__FILE__, __LINE__,  transaction_strings[stringindex].pyexceptionname, "{s: O}", "self", vtable);
 
2863
 
 
2864
 finally:
 
2865
  Py_XDECREF(res);
 
2866
 
 
2867
  PyGILState_Release(gilstate);
 
2868
  return sqliteres;
 
2869
}
 
2870
 
 
2871
static int 
 
2872
vtabBegin(sqlite3_vtab *pVtab) 
 
2873
 
2874
  return vtabTransactionMethod(pVtab, 0);
 
2875
}
 
2876
 
 
2877
static int 
 
2878
vtabSync(sqlite3_vtab *pVtab) 
 
2879
 
2880
  return vtabTransactionMethod(pVtab, 1);
 
2881
}
 
2882
 
 
2883
static int 
 
2884
vtabCommit(sqlite3_vtab *pVtab) 
 
2885
 
2886
  return vtabTransactionMethod(pVtab, 2);
 
2887
}
 
2888
 
 
2889
static int 
 
2890
vtabRollback(sqlite3_vtab *pVtab) 
 
2891
 
2892
  return vtabTransactionMethod(pVtab, 3);
 
2893
}
 
2894
 
 
2895
typedef struct {
 
2896
  sqlite3_vtab_cursor used_by_sqlite;   /* I don't touch this */
 
2897
  PyObject *cursor;                     /* Object implementing cursor */
 
2898
} apsw_vtable_cursor;
 
2899
 
 
2900
 
 
2901
static int
 
2902
vtabOpen(sqlite3_vtab *pVtab, sqlite3_vtab_cursor **ppCursor)
 
2903
 
2904
  PyObject *vtable=NULL, *res=NULL;
 
2905
  PyGILState_STATE gilstate;
 
2906
  apsw_vtable_cursor *avc=NULL;
 
2907
  int sqliteres=SQLITE_OK;
 
2908
 
 
2909
  gilstate=PyGILState_Ensure();
 
2910
 
 
2911
  vtable=((apsw_vtable*)pVtab)->vtable;
 
2912
 
 
2913
  res=Call_PythonMethod(vtable, "Open", NULL, 1);
 
2914
  if(!res)
 
2915
    goto pyexception;
 
2916
  avc=PyMem_Malloc(sizeof(apsw_vtable_cursor));
 
2917
  assert((void*)avc==(void*)&(avc->used_by_sqlite)); /* detect if wierd padding happens */
 
2918
  memset(avc, 0, sizeof(apsw_vtable_cursor));
 
2919
 
 
2920
  avc->cursor=res;
 
2921
  res=NULL;
 
2922
  *ppCursor=(sqlite3_vtab_cursor*)avc;
 
2923
  goto finally;
 
2924
 
 
2925
 pyexception: /* we had an exception in python code */
 
2926
  assert(PyErr_Occurred());
 
2927
  sqliteres=MakeSqliteMsgFromPyException(&(pVtab->zErrMsg));
 
2928
  AddTraceBackHere(__FILE__, __LINE__, "VirtualTable.xOpen", "{s: O}", "self", vtable);
 
2929
 
 
2930
 finally:
 
2931
  Py_XDECREF(res);
 
2932
  PyGILState_Release(gilstate);
 
2933
  return sqliteres;
 
2934
}
 
2935
 
 
2936
static int
 
2937
vtabFilter(sqlite3_vtab_cursor *pCursor, int idxNum, const char *idxStr,
 
2938
                  int argc, sqlite3_value **sqliteargv)
 
2939
 
2940
  PyObject *cursor, *args=NULL, *argv=NULL, *res=NULL;
 
2941
  PyGILState_STATE gilstate;
 
2942
  int sqliteres=SQLITE_OK;
 
2943
  int i;
 
2944
 
 
2945
  gilstate=PyGILState_Ensure();
 
2946
 
 
2947
  cursor=((apsw_vtable_cursor*)pCursor)->cursor;
 
2948
 
 
2949
 
 
2950
  argv=PyTuple_New(argc);
 
2951
  if(!argv) goto pyexception;
 
2952
  for(i=0;i<argc;i++)
 
2953
    {
 
2954
      PyObject *value=convert_value_to_pyobject(sqliteargv[i]);
 
2955
      if(!value) goto pyexception;
 
2956
      PyTuple_SET_ITEM(argv, i, value);
 
2957
    }
 
2958
 
 
2959
  args=Py_BuildValue("(iO&N)", idxNum, convertutf8string, idxStr, argv);
 
2960
  if(!args) goto pyexception;
 
2961
 
 
2962
  argv=NULL; /* owned by args now */
 
2963
 
 
2964
  res=Call_PythonMethod(cursor, "Filter", args, 1);
 
2965
  if(res) goto finally; /* result is ignored */
 
2966
 
 
2967
 pyexception: /* we had an exception in python code */
 
2968
  assert(PyErr_Occurred());
 
2969
  sqliteres=MakeSqliteMsgFromPyException(&(pCursor->pVtab->zErrMsg)); /* SQLite flaw: errMsg should be on the cursor not the table! */
 
2970
  AddTraceBackHere(__FILE__, __LINE__, "VirtualTable.xFilter", "{s: O}", "self", cursor);
 
2971
 
 
2972
 finally:
 
2973
  Py_XDECREF(args);
 
2974
  Py_XDECREF(argv);
 
2975
  Py_XDECREF(res);
 
2976
 
 
2977
  PyGILState_Release(gilstate);
 
2978
  return sqliteres;
 
2979
}
 
2980
 
 
2981
/* note that we can only return true/false and cannot indicate there was an error */
 
2982
static int
 
2983
vtabEof(sqlite3_vtab_cursor *pCursor)
 
2984
 
2985
  PyObject *cursor, *res=NULL;
 
2986
  PyGILState_STATE gilstate;
 
2987
  int sqliteres=0; /* nb a true/false value not error code */
 
2988
 
 
2989
  gilstate=PyGILState_Ensure();
 
2990
 
 
2991
  /* is there already an error? */
 
2992
  if(PyErr_Occurred()) goto finally;
 
2993
 
 
2994
  cursor=((apsw_vtable_cursor*)pCursor)->cursor;
 
2995
 
 
2996
  res=Call_PythonMethod(cursor, "Eof", NULL, 1);
 
2997
  if(!res) goto pyexception;
 
2998
 
 
2999
  sqliteres=PyObject_IsTrue(res);
 
3000
  if(sqliteres==0 || sqliteres==1)
 
3001
    goto finally;
 
3002
 
 
3003
  sqliteres=0; /* we say there are no more records on error */
 
3004
  
 
3005
 pyexception: /* we had an exception in python code */
 
3006
  assert(PyErr_Occurred());
 
3007
  sqliteres=MakeSqliteMsgFromPyException(&(pCursor->pVtab->zErrMsg)); /* SQLite flaw: errMsg should be on the cursor not the table! */
 
3008
  AddTraceBackHere(__FILE__, __LINE__, "VirtualTable.xEof", "{s: O}", "self", cursor);
 
3009
 
 
3010
 finally:
 
3011
  Py_XDECREF(res);
 
3012
 
 
3013
  PyGILState_Release(gilstate);
 
3014
  return sqliteres;
 
3015
}
 
3016
 
 
3017
static int
 
3018
vtabColumn(sqlite3_vtab_cursor *pCursor, sqlite3_context *result, int ncolumn)
 
3019
 
3020
  PyObject *cursor, *args=NULL, *res=NULL;
 
3021
  PyGILState_STATE gilstate;
 
3022
  int sqliteres=SQLITE_OK; 
 
3023
 
 
3024
  gilstate=PyGILState_Ensure();
 
3025
 
 
3026
  cursor=((apsw_vtable_cursor*)pCursor)->cursor;
 
3027
 
 
3028
  args=Py_BuildValue("(i)", ncolumn);
 
3029
  if(!args) goto pyexception;
 
3030
  
 
3031
  res=Call_PythonMethod(cursor, "Column", args, 1);
 
3032
  if(!res) goto pyexception;
 
3033
 
 
3034
  set_context_result(result, res);
 
3035
  if(!PyErr_Occurred()) goto finally;
 
3036
  
 
3037
 pyexception: /* we had an exception in python code */
 
3038
  assert(PyErr_Occurred());
 
3039
  sqliteres=MakeSqliteMsgFromPyException(&(pCursor->pVtab->zErrMsg)); /* SQLite flaw: errMsg should be on the cursor not the table! */
 
3040
  AddTraceBackHere(__FILE__, __LINE__, "VirtualTable.xColumn", "{s: O}", "self", cursor);
 
3041
 
 
3042
 finally:
 
3043
  Py_XDECREF(args);
 
3044
  Py_XDECREF(res);
 
3045
 
 
3046
  PyGILState_Release(gilstate);
 
3047
  return sqliteres;
 
3048
 
3049
 
 
3050
static int
 
3051
vtabNext(sqlite3_vtab_cursor *pCursor)
 
3052
 
3053
  PyObject *cursor, *res=NULL;
 
3054
  PyGILState_STATE gilstate;
 
3055
  int sqliteres=SQLITE_OK;
 
3056
 
 
3057
  gilstate=PyGILState_Ensure();
 
3058
 
 
3059
  cursor=((apsw_vtable_cursor*)pCursor)->cursor;
 
3060
 
 
3061
  res=Call_PythonMethod(cursor, "Next", NULL, 1);
 
3062
  if(res) goto finally;
 
3063
 
 
3064
  /* pyexception:  we had an exception in python code */
 
3065
  assert(PyErr_Occurred());
 
3066
  sqliteres=MakeSqliteMsgFromPyException(&(pCursor->pVtab->zErrMsg)); /* SQLite flaw: errMsg should be on the cursor not the table! */
 
3067
  AddTraceBackHere(__FILE__, __LINE__, "VirtualTable.xNext", "{s: O}", "self", cursor);
 
3068
 
 
3069
 finally:
 
3070
  Py_XDECREF(res);
 
3071
 
 
3072
  PyGILState_Release(gilstate);
 
3073
  return sqliteres; 
 
3074
}
 
3075
 
 
3076
static int
 
3077
vtabClose(sqlite3_vtab_cursor *pCursor)
 
3078
{
 
3079
  PyObject *cursor, *res=NULL;
 
3080
  PyGILState_STATE gilstate;
 
3081
  char **zErrMsgLocation=&(pCursor->pVtab->zErrMsg); /* we free pCursor but still need this field */
 
3082
  int sqliteres=SQLITE_OK;
 
3083
 
 
3084
  gilstate=PyGILState_Ensure();
 
3085
 
 
3086
  cursor=((apsw_vtable_cursor*)pCursor)->cursor;
 
3087
 
 
3088
  res=Call_PythonMethod(cursor, "Close", NULL, 1);
 
3089
  PyMem_Free(pCursor); /* always free */
 
3090
  if(res) goto finally;
 
3091
 
 
3092
  /* pyexception: we had an exception in python code */
 
3093
  assert(PyErr_Occurred());
 
3094
  sqliteres=MakeSqliteMsgFromPyException(zErrMsgLocation); /* SQLite flaw: errMsg should be on the cursor not the table! */
 
3095
  AddTraceBackHere(__FILE__, __LINE__, "VirtualTable.xClose", "{s: O}", "self", cursor);
 
3096
 
 
3097
 finally:
 
3098
  Py_DECREF(cursor);  /* this is where cursor gets freed */
 
3099
  Py_XDECREF(res);
 
3100
 
 
3101
  PyGILState_Release(gilstate);
 
3102
  return sqliteres; 
 
3103
}
 
3104
 
 
3105
static int
 
3106
vtabRowid(sqlite3_vtab_cursor *pCursor, sqlite_int64 *pRowid)
 
3107
 
3108
  PyObject *cursor, *res=NULL, *pyrowid=NULL;
 
3109
  PyGILState_STATE gilstate;
 
3110
  int sqliteres=SQLITE_OK; 
 
3111
 
 
3112
  gilstate=PyGILState_Ensure();
 
3113
 
 
3114
  cursor=((apsw_vtable_cursor*)pCursor)->cursor;
 
3115
 
 
3116
  res=Call_PythonMethod(cursor, "Rowid", NULL, 1);
 
3117
  if(!res) goto pyexception;
 
3118
  
 
3119
  /* extract result */
 
3120
  pyrowid=PyNumber_Long(res);
 
3121
  if(!pyrowid) 
 
3122
    goto pyexception;
 
3123
  *pRowid=PyLong_AsLongLong(pyrowid);
 
3124
  if(!PyErr_Occurred()) /* could be bigger than 64 bits */
 
3125
    goto finally;
 
3126
  
 
3127
 pyexception: /* we had an exception in python code */
 
3128
  assert(PyErr_Occurred());
 
3129
  sqliteres=MakeSqliteMsgFromPyException(&(pCursor->pVtab->zErrMsg)); /* SQLite flaw: errMsg should be on the cursor not the table! */
 
3130
  AddTraceBackHere(__FILE__, __LINE__, "VirtualTable.xRowid", "{s: O}", "self", cursor);
 
3131
 
 
3132
 finally:
 
3133
  Py_XDECREF(pyrowid);
 
3134
  Py_XDECREF(res);
 
3135
 
 
3136
  PyGILState_Release(gilstate);
 
3137
  return sqliteres;
 
3138
}
 
3139
 
 
3140
static int
 
3141
vtabUpdate(sqlite3_vtab *pVtab, int argc, sqlite3_value **argv, sqlite_int64 *pRowid)
 
3142
{
 
3143
  PyObject *vtable, *args=NULL, *res=NULL;
 
3144
  PyGILState_STATE gilstate;
 
3145
  int sqliteres=SQLITE_OK; 
 
3146
  int i;
 
3147
  const char *methodname="unknown";
 
3148
  
 
3149
  assert(argc); /* should always be >0 */
 
3150
  
 
3151
  gilstate=PyGILState_Ensure();
 
3152
 
 
3153
  vtable=((apsw_vtable*)pVtab)->vtable;
 
3154
 
 
3155
  /* case 1 - argc=1 means delete row */
 
3156
  if(argc==1)
 
3157
    {
 
3158
      methodname="UpdateDeleteRow";
 
3159
      args=Py_BuildValue("(O&)", convert_value_to_pyobject, argv[0]);
 
3160
      if(!args) goto pyexception;
 
3161
    }
 
3162
  /* case 2 - insert a row */
 
3163
  else if(sqlite3_value_type(argv[0])==SQLITE_NULL)
 
3164
    {
 
3165
      PyObject *newrowid;
 
3166
      methodname="UpdateInsertRow";
 
3167
      args=PyTuple_New(2);
 
3168
      if(!args) goto pyexception;
 
3169
      if(sqlite3_value_type(argv[1])==SQLITE_NULL)
 
3170
        {
 
3171
          newrowid=Py_None;
 
3172
          Py_INCREF(newrowid);
 
3173
        }
 
3174
      else
 
3175
        {
 
3176
          newrowid=convert_value_to_pyobject(argv[1]);
 
3177
          if(!newrowid) goto pyexception;
 
3178
        }
 
3179
      PyTuple_SET_ITEM(args, 0, newrowid);
 
3180
    }
 
3181
  /* otherwise changing a row */
 
3182
  else
 
3183
    {
 
3184
      PyObject *oldrowid=NULL, *newrowid=NULL;
 
3185
      methodname="UpdateChangeRow";
 
3186
      args=PyTuple_New(3);
 
3187
      oldrowid=convert_value_to_pyobject(argv[0]);
 
3188
      if(sqlite3_value_type(argv[1])==SQLITE_NULL)
 
3189
        {
 
3190
          newrowid=Py_None;
 
3191
          Py_INCREF(newrowid);
 
3192
        }
 
3193
      else
 
3194
        {
 
3195
          newrowid=convert_value_to_pyobject(argv[1]);
 
3196
        }
 
3197
      if(!oldrowid || !newrowid)
 
3198
        {
 
3199
          Py_XDECREF(oldrowid);
 
3200
          Py_XDECREF(newrowid);
 
3201
          goto pyexception;
 
3202
        }
 
3203
      PyTuple_SET_ITEM(args,0,oldrowid);
 
3204
      PyTuple_SET_ITEM(args,1,newrowid);
 
3205
    }
 
3206
 
 
3207
  /* new row values */
 
3208
  if(argc!=1)
 
3209
    {
 
3210
      PyObject *fields=NULL;
 
3211
      fields=PyTuple_New(argc-2);
 
3212
      if(!fields) goto pyexception;
 
3213
      for(i=0;i+2<argc;i++)
 
3214
        {
 
3215
          PyObject *field=convert_value_to_pyobject(argv[i+2]);
 
3216
          if(!field)
 
3217
            {
 
3218
              Py_DECREF(fields);
 
3219
              goto pyexception;
 
3220
            }
 
3221
          PyTuple_SET_ITEM(fields, i, field);
 
3222
        }
 
3223
      PyTuple_SET_ITEM(args, PyTuple_GET_SIZE(args)-1, fields);
 
3224
    }
 
3225
 
 
3226
  res=Call_PythonMethod(vtable, methodname, args, 1);
 
3227
  if(!res) 
 
3228
    goto pyexception;
 
3229
 
 
3230
  /* if row deleted then we don't care about return */
 
3231
  if(argc==1) 
 
3232
    goto finally;
 
3233
 
 
3234
  if(sqlite3_value_type(argv[0])==SQLITE_NULL && sqlite3_value_type(argv[1])==SQLITE_NULL)
 
3235
    {
 
3236
      /* did an insert and must provide a row id */
 
3237
      PyObject *rowid=PyNumber_Long(res);
 
3238
      if(!rowid) goto pyexception;
 
3239
 
 
3240
      *pRowid=PyLong_AsLongLong(rowid);
 
3241
      Py_DECREF(rowid);
 
3242
      if(PyErr_Occurred()) 
 
3243
        {
 
3244
          AddTraceBackHere(__FILE__, __LINE__, "VirtualTable.xUpdateInsertRow.ReturnedValue", "{s: O}", "result", rowid);
 
3245
          goto pyexception;
 
3246
        }
 
3247
    }
 
3248
  
 
3249
  goto finally;
 
3250
 
 
3251
 pyexception: /* we had an exception in python code */
 
3252
  assert(PyErr_Occurred());
 
3253
  sqliteres=MakeSqliteMsgFromPyException(&pVtab->zErrMsg);
 
3254
  AddTraceBackHere(__FILE__, __LINE__, "VirtualTable.xUpdate", "{s: O, s: i, s: s, s: O}", "self", vtable, "argc", argc, "methodname", methodname, "args", args?args:Py_None);
 
3255
 
 
3256
 finally:
 
3257
  Py_XDECREF(args);
 
3258
  Py_XDECREF(res);
 
3259
 
 
3260
  PyGILState_Release(gilstate);
 
3261
  return sqliteres;
 
3262
}
 
3263
 
 
3264
 
 
3265
#if 0
 
3266
/* I can't implement this yet since I need to know when
 
3267
   ppArg will get freed.  See SQLite ticket 2095. */
 
3268
 
 
3269
static int 
 
3270
vtabFindFunction(sqlite3_vtab *pVtab, int nArg, const char *zName,
 
3271
                 void (**pxFunc)(sqlite3_context*,int,sqlite3_value**),
 
3272
                 void **ppArg)
 
3273
 
3274
  return 0;
 
3275
}
 
3276
#endif
 
3277
 
 
3278
 
 
3279
/* it would be nice to use C99 style initializers here ... */
 
3280
static struct sqlite3_module apsw_vtable_module=
 
3281
  {
 
3282
    1,                    /* version */
 
3283
    vtabCreate,           /* methods */
 
3284
    vtabConnect,          
 
3285
    vtabBestIndex,        
 
3286
    vtabDisconnect,
 
3287
    vtabDestroy,
 
3288
    vtabOpen,
 
3289
    vtabClose, 
 
3290
    vtabFilter, 
 
3291
    vtabNext, 
 
3292
    vtabEof, 
 
3293
    vtabColumn,
 
3294
    vtabRowid, 
 
3295
    vtabUpdate, 
 
3296
    vtabBegin, 
 
3297
    vtabSync, 
 
3298
    vtabCommit, 
 
3299
    vtabRollback,
 
3300
    0,                /* vtabFindFunction */
 
3301
  };
 
3302
 
 
3303
static vtableinfo *
 
3304
allocvtableinfo(void)
 
3305
{
 
3306
  vtableinfo *res=PyMem_Malloc(sizeof(vtableinfo));
 
3307
  if(res)
 
3308
    memset(res, 0, sizeof(vtableinfo));
 
3309
  return res;
 
3310
}
 
3311
 
 
3312
static PyObject *
 
3313
Connection_createmodule(Connection *self, PyObject *args)
 
3314
{
 
3315
  char *name=NULL;
 
3316
  PyObject *datasource=NULL;
 
3317
  vtableinfo *vti;
 
3318
  int res;
 
3319
 
 
3320
  CHECK_THREAD(self, NULL);
 
3321
  CHECK_CLOSED(self, NULL);
 
3322
 
 
3323
  if(!PyArg_ParseTuple(args, "esO:createmodule(name, datasource)", "utf_8", &name, &datasource))
 
3324
    return NULL;
 
3325
 
 
3326
  Py_INCREF(datasource);
 
3327
  vti=allocvtableinfo();
 
3328
  vti->connection=self;
 
3329
  vti->name=name;
 
3330
  vti->datasource=datasource;
 
3331
 
 
3332
  /* ::TODO:: - can we call this with NULL to unregister a module? */
 
3333
  res=sqlite3_create_module(self->db, name, &apsw_vtable_module, vti);
 
3334
  SET_EXC(self->db, res);
 
3335
 
 
3336
  if(res!=SQLITE_OK)
 
3337
    {
 
3338
      freevtableinfo(vti);
 
3339
      return NULL;
 
3340
    }
 
3341
 
 
3342
  /* add vti to linked list */
 
3343
  vti->next=self->vtables;
 
3344
  self->vtables=vti;
 
3345
  
 
3346
  return Py_BuildValue("");
 
3347
}
 
3348
 
 
3349
#endif /* EXPERIMENTAL */
 
3350
/* end of Virtual table code */
 
3351
 
 
3352
 
 
3353
static PyMethodDef Connection_methods[] = {
 
3354
  {"cursor", (PyCFunction)Connection_cursor, METH_NOARGS,
 
3355
   "Create a new cursor" },
 
3356
  {"close",  (PyCFunction)Connection_close, METH_VARARGS,
 
3357
   "Closes the connection" },
 
3358
  {"setbusytimeout", (PyCFunction)Connection_setbusytimeout, METH_VARARGS,
 
3359
   "Sets the sqlite busy timeout in milliseconds.  Use zero to disable the timeout"},
 
3360
  {"interrupt", (PyCFunction)Connection_interrupt, METH_NOARGS,
 
3361
   "Causes any pending database operations to abort at the earliest opportunity"},
 
3362
  {"createscalarfunction", (PyCFunction)Connection_createscalarfunction, METH_VARARGS,
 
3363
   "Creates a scalar function"},
 
3364
  {"createaggregatefunction", (PyCFunction)Connection_createaggregatefunction, METH_VARARGS,
 
3365
   "Creates an aggregate function"},
 
3366
  {"setbusyhandler", (PyCFunction)Connection_setbusyhandler, METH_O,
 
3367
   "Sets the busy handler"},
 
3368
  {"changes", (PyCFunction)Connection_changes, METH_NOARGS, 
 
3369
   "Returns the number of rows changed by last query"},
 
3370
  {"totalchanges", (PyCFunction)Connection_totalchanges, METH_NOARGS, 
 
3371
   "Returns the total number of changes to database since it was opened"},
 
3372
  {"getautocommit", (PyCFunction)Connection_getautocommit, METH_NOARGS, 
 
3373
   "Returns if the database is in auto-commit mode"},
 
3374
  {"createcollation", (PyCFunction)Connection_createcollation, METH_VARARGS,
 
3375
   "Creates a collation function"},
 
3376
  {"last_insert_rowid", (PyCFunction)Connection_last_insert_rowid, METH_NOARGS,
 
3377
   "Returns rowid for last insert"},
 
3378
  {"complete", (PyCFunction)Connection_complete, METH_VARARGS,
 
3379
   "Checks if a SQL statement is complete"},
 
3380
  {"setauthorizer", (PyCFunction)Connection_setauthorizer, METH_O,
 
3381
   "Sets an authorizer function"},
 
3382
  {"setupdatehook", (PyCFunction)Connection_setupdatehook, METH_O,
 
3383
      "Sets an update hook"},
 
3384
  {"setrollbackhook", (PyCFunction)Connection_setrollbackhook, METH_O,
 
3385
   "Sets a callable invoked before each rollback"},
 
3386
#ifdef EXPERIMENTAL
 
3387
  {"setprofile", (PyCFunction)Connection_setprofile, METH_O,
 
3388
   "Sets a callable invoked with profile information after each statement"},
 
3389
  {"setcommithook", (PyCFunction)Connection_setcommithook, METH_O,
 
3390
   "Sets a callable invoked before each commit"},
 
3391
  {"setprogresshandler", (PyCFunction)Connection_setprogresshandler, METH_VARARGS,
 
3392
   "Sets a callback invoked periodically during long running calls"},
 
3393
#if !defined(SQLITE_OMIT_LOAD_EXTENSION)
 
3394
  {"enableloadextension", (PyCFunction)Connection_enableloadextension, METH_O,
 
3395
   "Enables loading of SQLite extensions from shared libraries"},
 
3396
  {"loadextension", (PyCFunction)Connection_loadextension, METH_VARARGS,
 
3397
   "loads SQLite extension"},
 
3398
#endif
 
3399
  {"createmodule", (PyCFunction)Connection_createmodule, METH_VARARGS,
 
3400
   "registers a virtual table"},
 
3401
#endif
 
3402
  {0, 0, 0, 0}  /* Sentinel */
 
3403
};
 
3404
 
 
3405
 
 
3406
static PyTypeObject ConnectionType = {
 
3407
    PyObject_HEAD_INIT(NULL)
 
3408
    0,                         /*ob_size*/
 
3409
    "apsw.Connection",         /*tp_name*/
 
3410
    sizeof(Connection),        /*tp_basicsize*/
 
3411
    0,                         /*tp_itemsize*/
 
3412
    (destructor)Connection_dealloc, /*tp_dealloc*/ 
 
3413
    0,                         /*tp_print*/
 
3414
    0,                         /*tp_getattr*/
 
3415
    0,                         /*tp_setattr*/
 
3416
    0,                         /*tp_compare*/
 
3417
    0,                         /*tp_repr*/
 
3418
    0,                         /*tp_as_number*/
 
3419
    0,                         /*tp_as_sequence*/
 
3420
    0,                         /*tp_as_mapping*/
 
3421
    0,                         /*tp_hash */
 
3422
    0,                         /*tp_call*/
 
3423
    0,                         /*tp_str*/
 
3424
    0,                         /*tp_getattro*/
 
3425
    0,                         /*tp_setattro*/
 
3426
    0,                         /*tp_as_buffer*/
 
3427
    Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
 
3428
    "Connection object",       /* tp_doc */
 
3429
    0,                         /* tp_traverse */
 
3430
    0,                         /* tp_clear */
 
3431
    0,                         /* tp_richcompare */
 
3432
    0,                         /* tp_weaklistoffset */
 
3433
    0,                         /* tp_iter */
 
3434
    0,                         /* tp_iternext */
 
3435
    Connection_methods,        /* tp_methods */
 
3436
    0,                         /* tp_members */
 
3437
    0,                         /* tp_getset */
 
3438
    0,                         /* tp_base */
 
3439
    0,                         /* tp_dict */
 
3440
    0,                         /* tp_descr_get */
 
3441
    0,                         /* tp_descr_set */
 
3442
    0,                         /* tp_dictoffset */
 
3443
    (initproc)Connection_init, /* tp_init */
 
3444
    0,                         /* tp_alloc */
 
3445
    Connection_new,            /* tp_new */
 
3446
    0,                         /* tp_free */
 
3447
    0,                         /* tp_is_gc */
 
3448
    0,                         /* tp_bases */
 
3449
    0,                         /* tp_mro */
 
3450
    0,                         /* tp_cache */
 
3451
    0,                         /* tp_subclasses */
 
3452
    0,                         /* tp_weaklist */
 
3453
    0,                         /* tp_del */
 
3454
};
 
3455
 
 
3456
 
 
3457
/* CURSOR CODE */
 
3458
 
 
3459
/* Do finalization and free resources.  Returns the SQLITE error code */
 
3460
static int
 
3461
resetcursor(Cursor *self, int force)
 
3462
{
 
3463
  int res=SQLITE_OK;
 
3464
 
 
3465
  Py_XDECREF(self->bindings);
 
3466
  self->bindings=NULL;
 
3467
  self->bindingsoffset=-1;
 
3468
 
 
3469
  if(self->statement)
 
3470
    {
 
3471
      res=statementcache_finalize(self->connection->stmtcache, self->statement);
 
3472
      if(!force) /* we don't care about errors when forcing */
 
3473
        SET_EXC(self->connection->db, res);
 
3474
      self->statement=0;
 
3475
    }
 
3476
 
 
3477
  if(!force && (self->status!=C_DONE && self->zsqlnextpos))
 
3478
    {
 
3479
      if (*self->zsqlnextpos && res==SQLITE_OK)
 
3480
        {
 
3481
          /* We still have more, so this is actually an abort. */
 
3482
          res=SQLITE_ERROR;
 
3483
          if(!PyErr_Occurred())
 
3484
            PyErr_Format(ExcIncomplete, "Error: there are still remaining sql statements to execute");
 
3485
        }
 
3486
    }
 
3487
  self->zsqlnextpos=NULL;
 
3488
  
 
3489
  if(!force && self->status!=C_DONE && self->emiter)
 
3490
    {
 
3491
      PyObject *next=PyIter_Next(self->emiter);
 
3492
      if(next)
 
3493
        {
 
3494
          Py_DECREF(next);
 
3495
          res=SQLITE_ERROR;
 
3496
          if (!PyErr_Occurred())
 
3497
            /* Technically this line won't get executed since the
 
3498
               block above will already have set ExcIncomplete.
 
3499
               Leaving it in as defensive coding. */
 
3500
            PyErr_Format(ExcIncomplete, "Error: there are still many remaining sql statements to execute");
 
3501
        }
 
3502
    }
 
3503
     
 
3504
  Py_XDECREF(self->emiter);
 
3505
  self->emiter=NULL;
 
3506
 
 
3507
  if(self->zsql)
 
3508
    {
 
3509
      PyMem_Free((void*)self->zsql);
 
3510
      self->zsql=0;
 
3511
    }
 
3512
 
 
3513
  self->status=C_DONE;
 
3514
 
 
3515
  if (PyErr_Occurred())
 
3516
    {
 
3517
      assert(res);
 
3518
      AddTraceBackHere(__FILE__, __LINE__, "resetcursor", NULL);
 
3519
    }
 
3520
 
 
3521
  return res;
 
3522
}
 
3523
 
 
3524
static void
 
3525
Cursor_dealloc(Cursor * self)
 
3526
{
 
3527
  /* thread check - we can't use macro as it returns*/
 
3528
  PyObject *err_type, *err_value, *err_traceback;
 
3529
  int have_error=PyErr_Occurred()?1:0;
 
3530
 
 
3531
  if( (self->status!=C_DONE || self->statement || self->zsqlnextpos || self->emiter) &&  /* only whine if there was anything left in the Cursor */
 
3532
      self->connection->thread_ident!=PyThread_get_thread_ident())
 
3533
    {
 
3534
      if (have_error)
 
3535
        PyErr_Fetch(&err_type, &err_value, &err_traceback);
 
3536
      PyErr_Format(ExcThreadingViolation, "The destructor for Cursor is called in a different thread than it"
 
3537
                   "was created in.  All calls must be in the same thread.  It was created in thread %d " 
 
3538
                   "and this is %d.  SQLite is not being closed as a result.",
 
3539
                   (int)(self->connection->thread_ident), (int)(PyThread_get_thread_ident()));            
 
3540
      apsw_write_unraiseable();
 
3541
      if (have_error)
 
3542
        PyErr_Restore(err_type, err_value, err_traceback);
 
3543
      
 
3544
      return;
 
3545
    }
 
3546
 
 
3547
  /* do our finalisation ... */
 
3548
 
 
3549
  if (have_error)
 
3550
    {
 
3551
      /* remember the existing error so that resetcursor won't immediately return */
 
3552
      PyErr_Fetch(&err_type, &err_value, &err_traceback);
 
3553
      PyErr_Clear();
 
3554
    }
 
3555
 
 
3556
  resetcursor(self, 1);
 
3557
  if(PyErr_Occurred())
 
3558
    PyErr_Clear(); /* clear out any exceptions from resetcursor since we don't care */
 
3559
 
 
3560
  if (have_error)
 
3561
    /* restore earlier error if there was one */
 
3562
    PyErr_Restore(err_type, err_value, err_traceback);
 
3563
 
 
3564
  /* we no longer need connection */
 
3565
  if(self->connection)
 
3566
    {
 
3567
      pointerlist_remove(&self->connection->cursors, self);
 
3568
      Py_DECREF(self->connection);
 
3569
      self->connection=0;
 
3570
    }
 
3571
 
 
3572
  /* executemany iterator */
 
3573
  Py_XDECREF(self->emiter);
 
3574
  self->emiter=NULL;
 
3575
 
 
3576
  /* no need for tracing */
 
3577
  Py_XDECREF(self->exectrace);
 
3578
  Py_XDECREF(self->rowtrace);
 
3579
  self->exectrace=self->rowtrace=0;
 
3580
  
 
3581
  self->ob_type->tp_free((PyObject*)self);
 
3582
}
 
3583
 
 
3584
static void
 
3585
Cursor_init(Cursor *self, Connection *connection)
 
3586
{
 
3587
  self->connection=connection;
 
3588
  self->statement=0;
 
3589
  self->zsql=0;
 
3590
  self->zsqlnextpos=0;
 
3591
  self->status=C_DONE;
 
3592
  self->bindings=0;
 
3593
  self->bindingsoffset=0;
 
3594
  self->emiter=0;
 
3595
  self->exectrace=0;
 
3596
  self->rowtrace=0;
 
3597
}
 
3598
 
 
3599
static PyObject *
 
3600
Cursor_getdescription(Cursor *self)
 
3601
{
 
3602
  int ncols,i;
 
3603
  PyObject *result=NULL;
 
3604
  PyObject *pair=NULL;
 
3605
 
 
3606
  CHECK_THREAD(self->connection,NULL);
 
3607
  CHECK_CLOSED(self->connection,NULL);
 
3608
 
 
3609
  if(!self->statement)
 
3610
    {
 
3611
      PyErr_Format(ExcComplete, "Can't get description for statements that have completed execution");
 
3612
      return NULL;
 
3613
    }
 
3614
  
 
3615
  ncols=sqlite3_column_count(self->statement);
 
3616
  result=PyTuple_New(ncols);
 
3617
  if(!result) goto error;
 
3618
 
 
3619
  for(i=0;i<ncols;i++)
 
3620
    {
 
3621
      pair=Py_BuildValue("(O&O&)", 
 
3622
                         convertutf8string, sqlite3_column_name(self->statement, i),
 
3623
                         convertutf8string, sqlite3_column_decltype(self->statement, i));
 
3624
                         
 
3625
      if(!pair) goto error;
 
3626
 
 
3627
      PyTuple_SET_ITEM(result, i, pair);
 
3628
      /* owned by result now */
 
3629
      pair=0;
 
3630
    }
 
3631
  
 
3632
  return result;
 
3633
 
 
3634
 error:
 
3635
  Py_XDECREF(result);
 
3636
  Py_XDECREF(pair);
 
3637
  return NULL;
 
3638
}
 
3639
 
 
3640
/* internal function - returns SQLite error code (ie SQLITE_OK if all is well) */
 
3641
static int
 
3642
Cursor_dobinding(Cursor *self, int arg, PyObject *obj)
 
3643
{
 
3644
 
 
3645
  /* DUPLICATE(ish) code: this is substantially similar to the code in
 
3646
     set_context_result.  If you fix anything here then do it there as
 
3647
     well. */
 
3648
 
 
3649
  int res=SQLITE_OK;
 
3650
 
 
3651
  if(PyErr_Occurred()) 
 
3652
    return -1;
 
3653
 
 
3654
  if(obj==Py_None)
 
3655
    res=sqlite3_bind_null(self->statement, arg);
 
3656
  /* Python uses a 'long' for storage of PyInt.  This could
 
3657
     be a 32bit or 64bit quantity depending on the platform. */
 
3658
  else if(PyInt_Check(obj))
 
3659
    res=sqlite3_bind_int64(self->statement, arg, PyInt_AS_LONG(obj));
 
3660
  else if (PyLong_Check(obj))
 
3661
    /* nb: PyLong_AsLongLong can cause Python level error */
 
3662
    res=sqlite3_bind_int64(self->statement, arg, PyLong_AsLongLong(obj));
 
3663
  else if (PyFloat_Check(obj))
 
3664
    res=sqlite3_bind_double(self->statement, arg, PyFloat_AS_DOUBLE(obj));
 
3665
  else if (PyUnicode_Check(obj))
 
3666
    {
 
3667
      const void *badptr=NULL;
 
3668
      UNIDATABEGIN(obj)
 
3669
        badptr=strdata;
 
3670
        if(strdata)
 
3671
          {
 
3672
            if(strbytes>APSW_INT32_MAX)
 
3673
              {
 
3674
                PyErr_Format(ExcTooBig, "Unicode object is too large - SQLite only supports up to 2GB");
 
3675
              }
 
3676
            else
 
3677
              {
 
3678
                if(use16)
 
3679
                  res=sqlite3_bind_text16(self->statement, arg, strdata, (int)strbytes, SQLITE_TRANSIENT);
 
3680
                else
 
3681
                  res=sqlite3_bind_text(self->statement, arg, strdata, (int)strbytes, SQLITE_TRANSIENT);
 
3682
              }
 
3683
          }
 
3684
      UNIDATAEND(obj);
 
3685
      if(!badptr) 
 
3686
        {
 
3687
          assert(PyErr_Occurred());
 
3688
          return -1;
 
3689
        }
 
3690
    }
 
3691
  else if (PyString_Check(obj))
 
3692
    {
 
3693
      const char *val=PyString_AS_STRING(obj);
 
3694
      const size_t lenval=PyString_GET_SIZE(obj);
 
3695
      const char *chk=val;
 
3696
      for(;chk<val+lenval && !((*chk)&0x80); chk++);
 
3697
      if(chk<val+lenval)
 
3698
        {
 
3699
          const void *badptr=NULL;
 
3700
          PyObject *str2=PyUnicode_FromObject(obj);
 
3701
          if(!str2)
 
3702
            return -1;
 
3703
          UNIDATABEGIN(str2)
 
3704
            badptr=strdata;
 
3705
            if(strdata)
 
3706
              {
 
3707
                if(strbytes>APSW_INT32_MAX)
 
3708
                  {
 
3709
                    PyErr_Format(ExcTooBig, "Unicode object is too large - SQLite only supports up to 2GB");
 
3710
                  }
 
3711
                else
 
3712
                  {
 
3713
                    if(use16)
 
3714
                      res=sqlite3_bind_text16(self->statement, arg, strdata, (int)strbytes, SQLITE_TRANSIENT);
 
3715
                    else
 
3716
                      res=sqlite3_bind_text(self->statement, arg, strdata, (int)strbytes, SQLITE_TRANSIENT);
 
3717
                  }
 
3718
              }
 
3719
          UNIDATAEND(str2);
 
3720
          Py_DECREF(str2);
 
3721
          if(!badptr) 
 
3722
            {
 
3723
              assert(PyErr_Occurred());
 
3724
              return -1;
 
3725
            }
 
3726
        }
 
3727
      else
 
3728
        {
 
3729
          if(lenval>APSW_INT32_MAX)
 
3730
              {
 
3731
                PyErr_Format(ExcTooBig, "String object is too large - SQLite only supports up to 2GB");
 
3732
                return -1;
 
3733
              }
 
3734
          res=sqlite3_bind_text(self->statement, arg, val, (int)lenval, SQLITE_TRANSIENT);
 
3735
        }
 
3736
    }
 
3737
  else if (PyBuffer_Check(obj))
 
3738
    {
 
3739
      const char *buffer;
 
3740
      Py_ssize_t buflen;
 
3741
      if(PyObject_AsCharBuffer(obj, &buffer, &buflen))
 
3742
        return -1;
 
3743
      if (buflen>APSW_INT32_MAX)
 
3744
        {
 
3745
          PyErr_Format(ExcTooBig, "Binding object is too large - SQLite only supports up to 2GB");
 
3746
          return -1;
 
3747
        }
 
3748
      res=sqlite3_bind_blob(self->statement, arg, buffer, (int)buflen, SQLITE_TRANSIENT);
 
3749
    }
 
3750
  else 
 
3751
    {
 
3752
      PyObject *strrep=PyObject_Str(obj);
 
3753
      PyErr_Format(PyExc_TypeError, "Bad binding argument type supplied - argument #%d: %s", (int)(arg+self->bindingsoffset), strrep?PyString_AsString(strrep):"<str failed>");
 
3754
      Py_XDECREF(strrep);
 
3755
      return -1;
 
3756
    }
 
3757
  if(res!=SQLITE_OK)
 
3758
    {
 
3759
      SET_EXC(self->connection->db, res);
 
3760
      return -1;
 
3761
    }
 
3762
  if(PyErr_Occurred())
 
3763
    return -1;
 
3764
  return 0;
 
3765
}
 
3766
 
 
3767
/* internal function */
 
3768
static int
 
3769
Cursor_dobindings(Cursor *self)
 
3770
{
 
3771
  int nargs, arg, res, sz=0;
 
3772
  PyObject *obj;
 
3773
 
 
3774
  if(PyErr_Occurred()) 
 
3775
    return -1;
 
3776
 
 
3777
  assert(self->bindingsoffset>=0);
 
3778
 
 
3779
  nargs=sqlite3_bind_parameter_count(self->statement);
 
3780
 
 
3781
  if (nargs>0 && !self->bindings)
 
3782
    {
 
3783
      PyErr_Format(ExcBindings, "Statement has %d bindings but you didn't supply any!", nargs);
 
3784
      return -1;
 
3785
    }
 
3786
 
 
3787
  /* a dictionary? */
 
3788
  if (self->bindings && PyDict_Check(self->bindings))
 
3789
    {
 
3790
      for(arg=1;arg<=nargs;arg++)
 
3791
        {
 
3792
          PyObject *keyo=NULL;
 
3793
          const char *key=sqlite3_bind_parameter_name(self->statement, arg);
 
3794
 
 
3795
          if(!key)
 
3796
            {
 
3797
              PyErr_Format(ExcBindings, "Binding %d has no name, but you supplied a dict (which only has names).", arg-1);
 
3798
              return -1;
 
3799
            }
 
3800
 
 
3801
          assert(*key==':' || *key=='$');
 
3802
          key++; /* first char is a colon or dollar which we skip */
 
3803
 
 
3804
          keyo=PyUnicode_DecodeUTF8(key, strlen(key), NULL);
 
3805
          if(!keyo) return -1;
 
3806
 
 
3807
          obj=PyDict_GetItem(self->bindings, keyo);
 
3808
          Py_DECREF(keyo);
 
3809
 
 
3810
          if(!obj)
 
3811
            /* this is where we could error on missing keys */
 
3812
            continue;
 
3813
          if(Cursor_dobinding(self,arg,obj))
 
3814
            {
 
3815
              assert(PyErr_Occurred());
 
3816
              return -1;
 
3817
            }
 
3818
        }
 
3819
 
 
3820
      return 0;
 
3821
    }
 
3822
 
 
3823
  /* it must be a fast sequence */
 
3824
  /* verify the number of args supplied */
 
3825
  if (self->bindings)
 
3826
    sz=PySequence_Fast_GET_SIZE(self->bindings);
 
3827
  /* there is another statement after this one ... */
 
3828
  if(*self->zsqlnextpos && sz-self->bindingsoffset<nargs)
 
3829
    {
 
3830
      PyErr_Format(ExcBindings, "Incorrect number of bindings supplied.  The current statement uses %d and there are only %d left.  Current offset is %d",
 
3831
                   nargs, (self->bindings)?sz:0, (int)(self->bindingsoffset));
 
3832
      return -1;
 
3833
    }
 
3834
  /* no more statements */
 
3835
  if(!*self->zsqlnextpos && sz-self->bindingsoffset!=nargs)
 
3836
    {
 
3837
      PyErr_Format(ExcBindings, "Incorrect number of bindings supplied.  The current statement uses %d and there are %d supplied.  Current offset is %d",
 
3838
                   nargs, (self->bindings)?sz:0, (int)(self->bindingsoffset));
 
3839
      return -1;
 
3840
    }
 
3841
  
 
3842
  res=SQLITE_OK;
 
3843
 
 
3844
  /* nb sqlite starts bind args at one not zero */
 
3845
  for(arg=1;arg<=nargs;arg++)
 
3846
    {
 
3847
      obj=PySequence_Fast_GET_ITEM(self->bindings, arg-1+self->bindingsoffset);
 
3848
      if(Cursor_dobinding(self, arg, obj))
 
3849
        {
 
3850
          assert(PyErr_Occurred());
 
3851
          return -1;
 
3852
        }
 
3853
    }
 
3854
 
 
3855
  self->bindingsoffset+=nargs;
 
3856
  assert(res==0);
 
3857
  return 0;
 
3858
}
 
3859
 
 
3860
typedef struct { 
 
3861
  const char *previouszsqlpos;  /* where the begining of the statement was */
 
3862
  Py_ssize_t savedbindingsoffset;      /* where the bindings began */
 
3863
} exectrace_oldstate;
 
3864
  
 
3865
static int
 
3866
Cursor_doexectrace(Cursor *self, exectrace_oldstate *etos)
 
3867
{
 
3868
  PyObject *retval=NULL;
 
3869
  PyObject *args=NULL;
 
3870
  PyObject *sqlcmd=NULL;
 
3871
  PyObject *bindings=NULL;
 
3872
  int result;
 
3873
 
 
3874
  assert(self->exectrace);
 
3875
 
 
3876
  /* make a string of the command */
 
3877
  sqlcmd=convertutf8stringsize(etos->previouszsqlpos, self->zsqlnextpos-etos->previouszsqlpos);
 
3878
 
 
3879
  if(!sqlcmd) return -1;
 
3880
 
 
3881
  /* now deal with the bindings */
 
3882
  if(self->bindings)
 
3883
    {
 
3884
      if(PyDict_Check(self->bindings))
 
3885
        {
 
3886
          bindings=self->bindings;
 
3887
          Py_INCREF(self->bindings);
 
3888
        }
 
3889
      else
 
3890
        {
 
3891
          bindings=PySequence_GetSlice(self->bindings, etos->savedbindingsoffset, self->bindingsoffset);
 
3892
          if(!bindings)
 
3893
            {
 
3894
              /* I couldn't work anything into the test suite to actually make this fail! */
 
3895
              Py_DECREF(sqlcmd);
 
3896
              return -1;
 
3897
            }
 
3898
        }
 
3899
    }
 
3900
  else
 
3901
    {
 
3902
      bindings=Py_None;
 
3903
      Py_INCREF(bindings);
 
3904
    }
 
3905
  args=Py_BuildValue("(NN)", sqlcmd, bindings); /* owns sqlcmd and bindings now */
 
3906
  if(!args)
 
3907
    {
 
3908
      Py_DECREF(sqlcmd);
 
3909
      Py_DECREF(bindings);
 
3910
      return -1;
 
3911
    }
 
3912
  
 
3913
  retval=PyEval_CallObject(self->exectrace, args);
 
3914
  Py_DECREF(args);
 
3915
  if(!retval) 
 
3916
    {
 
3917
      assert(PyErr_Occurred());
 
3918
      return -1;
 
3919
    }
 
3920
  result=PyObject_IsTrue(retval);
 
3921
  Py_DECREF(retval);
 
3922
  assert (result==-1 || result==0 || result ==1);
 
3923
  if(result==-1)
 
3924
    {
 
3925
      assert(PyErr_Occurred());
 
3926
      return -1;
 
3927
    }
 
3928
  if(result)
 
3929
    return 0;
 
3930
 
 
3931
  /* callback didn't want us to continue */
 
3932
  PyErr_Format(ExcTraceAbort, "Aborted by false/null return value of exec tracer");
 
3933
  return -1;
 
3934
}
 
3935
 
 
3936
static PyObject*
 
3937
Cursor_dorowtrace(Cursor *self, PyObject *retval)
 
3938
{
 
3939
  assert(self->rowtrace);
 
3940
 
 
3941
  retval=PyEval_CallObject(self->rowtrace, retval);
 
3942
  if(!retval) 
 
3943
    return NULL;
 
3944
  
 
3945
  return retval;
 
3946
}
 
3947
 
 
3948
/* Returns a borrowed reference to self if all is ok, else NULL on error */
 
3949
static PyObject *
 
3950
Cursor_step(Cursor *self)
 
3951
{
 
3952
  int res;
 
3953
  exectrace_oldstate etos;
 
3954
 
 
3955
  if(self->status==C_DONE)
 
3956
    {
 
3957
      PyErr_Format(ExcComplete, "The statement(s) have finished or errored, so you can't keep running them");
 
3958
      return NULL;
 
3959
    }
 
3960
 
 
3961
  for(;;)
 
3962
    {
 
3963
      assert(!PyErr_Occurred());
 
3964
      Py_BEGIN_ALLOW_THREADS
 
3965
        res=(self->statement)?(sqlite3_step(self->statement)):(SQLITE_DONE);
 
3966
      Py_END_ALLOW_THREADS;
 
3967
 
 
3968
      switch(res&0xff)
 
3969
        {
 
3970
        case SQLITE_MISUSE:
 
3971
          /* this would be an error in apsw itself */
 
3972
          self->status=C_DONE;
 
3973
          SET_EXC(self->connection->db,res);
 
3974
          resetcursor(self, 0);
 
3975
          return NULL;
 
3976
 
 
3977
        case SQLITE_ROW:
 
3978
          self->status=C_ROW;
 
3979
          return (PyErr_Occurred())?(NULL):((PyObject*)self);
 
3980
 
 
3981
        case SQLITE_DONE:
 
3982
          if (PyErr_Occurred())
 
3983
            {
 
3984
              self->status=C_DONE;
 
3985
              return NULL;
 
3986
            }
 
3987
          break;
 
3988
 
 
3989
 
 
3990
        case SQLITE_SCHEMA:
 
3991
          /* This is typically because a cached statement has become
 
3992
             invalid.  We figure this out by re-preparing.  See SQLite
 
3993
             ticket 2158.
 
3994
           */
 
3995
          {
 
3996
            sqlite3_stmt *newstmt;
 
3997
            res=statementcache_dup(self->connection->stmtcache, self->statement, &newstmt);
 
3998
            if(newstmt)
 
3999
              {
 
4000
                assert(res==SQLITE_OK);
 
4001
                sqlite3_transfer_bindings(self->statement, newstmt);
 
4002
                statementcache_finalize(self->connection->stmtcache, self->statement);
 
4003
                self->statement=newstmt;
 
4004
                continue;
 
4005
              }
 
4006
            SET_EXC(self->connection->db,res);
 
4007
            self->status=C_DONE;
 
4008
            resetcursor(self, 0); /* we have handled error */
 
4009
            return NULL;
 
4010
          }
 
4011
 
 
4012
        default: /* sqlite3_prepare_v2 introduced in 3.3.9 means the
 
4013
                    error code is returned from step as well as
 
4014
                    finalize/reset */
 
4015
          /* FALLTHRU */
 
4016
        case SQLITE_ERROR:  /* SQLITE_BUSY is handled here as well */
 
4017
          /* there was an error - we need to get actual error code from sqlite3_finalize */
 
4018
          self->status=C_DONE;
 
4019
          res=resetcursor(self, 0);  /* this will get the error code for us */
 
4020
          assert(res!=SQLITE_OK);
 
4021
          return NULL;
 
4022
 
 
4023
          
 
4024
        }
 
4025
      assert(res==SQLITE_DONE);
 
4026
 
 
4027
      /* done with that statement, are there any more? */
 
4028
      self->status=C_DONE;
 
4029
      if(!self->zsqlnextpos || !*self->zsqlnextpos)
 
4030
        {
 
4031
          PyObject *next;
 
4032
          if(!self->emiter)
 
4033
            {
 
4034
              /* no more so we finalize */
 
4035
              if(resetcursor(self, 0)!=SQLITE_OK)
 
4036
                {
 
4037
                  assert(PyErr_Occurred());
 
4038
                  return NULL; /* exception */
 
4039
                }
 
4040
              return (PyObject*)self;
 
4041
            }
 
4042
          next=PyIter_Next(self->emiter);
 
4043
          if(PyErr_Occurred())
 
4044
            return NULL;
 
4045
          if(!next)
 
4046
            {
 
4047
              /* no more from executemanyiter so we finalize */
 
4048
              if(resetcursor(self, 0)!=SQLITE_OK)
 
4049
                {
 
4050
                  assert(PyErr_Occurred());
 
4051
                  return NULL;
 
4052
                }
 
4053
              return (PyObject*)self;
 
4054
            }
 
4055
          self->zsqlnextpos=self->zsql; /* start at begining of string again */
 
4056
          /* don't need bindings from last round if emiter.next() */
 
4057
          Py_XDECREF(self->bindings);
 
4058
          self->bindings=0;
 
4059
          self->bindingsoffset=0;
 
4060
          /* verify type of next before putting in bindings */
 
4061
          if(PyDict_Check(next))
 
4062
            self->bindings=next;
 
4063
          else
 
4064
            {
 
4065
              self->bindings=PySequence_Fast(next, "You must supply a dict or a sequence");
 
4066
              /* we no longer need next irrespective of what happens in line above */
 
4067
              Py_DECREF(next);
 
4068
              if(!self->bindings)
 
4069
                return NULL;
 
4070
            }
 
4071
          assert(self->bindings);
 
4072
        }
 
4073
 
 
4074
      /* finalise and go again */
 
4075
      res=statementcache_finalize(self->connection->stmtcache, self->statement);
 
4076
      self->statement=0;
 
4077
      SET_EXC(self->connection->db,res);
 
4078
      if (res!=SQLITE_OK)
 
4079
        {
 
4080
          assert((res&0xff)!=SQLITE_BUSY); /* finalize shouldn't be returning busy, only step */
 
4081
          return NULL;
 
4082
        }
 
4083
 
 
4084
      assert(!self->statement);
 
4085
      if(self->exectrace)
 
4086
        {
 
4087
          etos.previouszsqlpos=self->zsqlnextpos;
 
4088
          etos.savedbindingsoffset=self->bindingsoffset;
 
4089
        }
 
4090
      assert(!PyErr_Occurred());
 
4091
      res=statementcache_prepare(self->connection->stmtcache, self->connection->db, self->zsqlnextpos, -1, &self->statement, &self->zsqlnextpos);
 
4092
      SET_EXC(self->connection->db,res);
 
4093
      if (res!=SQLITE_OK)
 
4094
        {
 
4095
          assert((res&0xff)!=SQLITE_BUSY); /* prepare definitely shouldn't be returning busy */
 
4096
          return NULL;
 
4097
        }
 
4098
      assert(!PyErr_Occurred());
 
4099
      if(Cursor_dobindings(self))
 
4100
        {
 
4101
          assert(PyErr_Occurred());
 
4102
          return NULL;
 
4103
        }
 
4104
 
 
4105
      if(self->exectrace)
 
4106
        {
 
4107
          if(Cursor_doexectrace(self, &etos))
 
4108
            {
 
4109
              assert(self->status==C_DONE);
 
4110
              assert(PyErr_Occurred());
 
4111
              return NULL;
 
4112
            }
 
4113
        }
 
4114
      assert(self->status==C_DONE);
 
4115
      self->status=C_BEGIN;
 
4116
    }
 
4117
 
 
4118
  /* you can't actually get here */
 
4119
  assert(0);
 
4120
  return NULL;
 
4121
}
 
4122
 
 
4123
static PyObject *
 
4124
Cursor_execute(Cursor *self, PyObject *args)
 
4125
{
 
4126
  int res;
 
4127
  PyObject *retval=NULL;
 
4128
  exectrace_oldstate etos;
 
4129
 
 
4130
  CHECK_THREAD(self->connection, NULL);
 
4131
  CHECK_CLOSED(self->connection, NULL);
 
4132
 
 
4133
  res=resetcursor(self, 0);
 
4134
  if(res!=SQLITE_OK)
 
4135
    {
 
4136
      assert(PyErr_Occurred());
 
4137
      return NULL;
 
4138
    }
 
4139
  
 
4140
  assert(!self->bindings);
 
4141
 
 
4142
  if(!PyArg_ParseTuple(args, "es|O:execute(statements,bindings=())", STRENCODING, &self->zsql, &self->bindings))
 
4143
    return NULL;
 
4144
 
 
4145
  if(self->bindings)
 
4146
    {
 
4147
      if(PyDict_Check(self->bindings))
 
4148
        Py_INCREF(self->bindings);
 
4149
      else
 
4150
        {
 
4151
          self->bindings=PySequence_Fast(self->bindings, "You must supply a dict or a sequence");
 
4152
          if(!self->bindings)
 
4153
            return NULL;
 
4154
        }
 
4155
    }
 
4156
 
 
4157
  assert(!self->statement);
 
4158
  if(self->exectrace)
 
4159
    {
 
4160
      etos.previouszsqlpos=self->zsql;
 
4161
      etos.savedbindingsoffset=0;
 
4162
    }
 
4163
  assert(!PyErr_Occurred());
 
4164
  res=statementcache_prepare(self->connection->stmtcache, self->connection->db, self->zsql, -1, &self->statement, &self->zsqlnextpos);
 
4165
  SET_EXC(self->connection->db,res);
 
4166
  if (res!=SQLITE_OK)
 
4167
    {
 
4168
      AddTraceBackHere(__FILE__, __LINE__, "Cursor_execute.sqlite3_prepare_v2", "{s: O, s: N}", 
 
4169
                       "Connection", self->connection, 
 
4170
                       "statement", PyUnicode_DecodeUTF8(self->zsql, strlen(self->zsql), "strict"));
 
4171
      return NULL;
 
4172
    }
 
4173
  assert(!PyErr_Occurred());
 
4174
 
 
4175
  self->bindingsoffset=0;
 
4176
  if(Cursor_dobindings(self))
 
4177
    {
 
4178
      assert(PyErr_Occurred());
 
4179
      return NULL;
 
4180
    }
 
4181
 
 
4182
  if(self->exectrace)
 
4183
    {
 
4184
      if(Cursor_doexectrace(self, &etos))
 
4185
        {
 
4186
          assert(PyErr_Occurred());
 
4187
          return NULL;  
 
4188
        }
 
4189
    }
 
4190
 
 
4191
  self->status=C_BEGIN;
 
4192
 
 
4193
  retval=Cursor_step(self);
 
4194
  if (!retval) 
 
4195
    {
 
4196
      assert(PyErr_Occurred());
 
4197
      return NULL;
 
4198
    }
 
4199
  Py_INCREF(retval);
 
4200
  return retval;
 
4201
}
 
4202
 
 
4203
static PyObject *
 
4204
Cursor_executemany(Cursor *self, PyObject *args)
 
4205
{
 
4206
  int res;
 
4207
  PyObject *retval=NULL;
 
4208
  PyObject *theiterable=NULL;
 
4209
  PyObject *next=NULL;
 
4210
  exectrace_oldstate etos;
 
4211
 
 
4212
  CHECK_THREAD(self->connection, NULL);
 
4213
  CHECK_CLOSED(self->connection, NULL);
 
4214
 
 
4215
  res=resetcursor(self, 0);
 
4216
  if(res!=SQLITE_OK)
 
4217
    {
 
4218
      assert(PyErr_Occurred());
 
4219
      return NULL;
 
4220
    }
 
4221
  
 
4222
  assert(!self->bindings);
 
4223
  assert(!self->emiter);
 
4224
  assert(!self->zsql);
 
4225
  assert(self->status=C_DONE);
 
4226
 
 
4227
  if(!PyArg_ParseTuple(args, "esO:executemany(statements, sequenceofbindings)", STRENCODING, &self->zsql, &theiterable))
 
4228
    return NULL;
 
4229
 
 
4230
  self->emiter=PyObject_GetIter(theiterable);
 
4231
  if (!self->emiter)
 
4232
    {
 
4233
      PyErr_Format(PyExc_TypeError, "2nd parameter must be iterable");
 
4234
      return NULL;
 
4235
    }
 
4236
 
 
4237
  next=PyIter_Next(self->emiter);
 
4238
  if(!next && PyErr_Occurred())
 
4239
    return NULL;
 
4240
  if(!next)
 
4241
    {
 
4242
      /* empty list */
 
4243
      Py_INCREF(self);
 
4244
      return (PyObject*)self;
 
4245
    }
 
4246
 
 
4247
  if(PyDict_Check(next))
 
4248
    self->bindings=next;
 
4249
  else
 
4250
    {
 
4251
      self->bindings=PySequence_Fast(next, "You must supply a dict or a sequence");
 
4252
      Py_DECREF(next); /* _Fast makes new reference */
 
4253
      if(!self->bindings)
 
4254
          return NULL;
 
4255
    }
 
4256
 
 
4257
  assert(!self->statement);
 
4258
  if(self->exectrace)
 
4259
    {
 
4260
      etos.previouszsqlpos=self->zsql;
 
4261
      etos.savedbindingsoffset=0;
 
4262
    }
 
4263
  assert(!PyErr_Occurred());
 
4264
  res=statementcache_prepare(self->connection->stmtcache, self->connection->db, self->zsql, -1, &self->statement, &self->zsqlnextpos);
 
4265
  SET_EXC(self->connection->db,res);
 
4266
  if (res!=SQLITE_OK)
 
4267
    return NULL;
 
4268
  assert(!PyErr_Occurred());
 
4269
 
 
4270
  self->bindingsoffset=0;
 
4271
  if(Cursor_dobindings(self))
 
4272
    {
 
4273
      assert(PyErr_Occurred());
 
4274
      return NULL;
 
4275
    }
 
4276
 
 
4277
  if(self->exectrace)
 
4278
    {
 
4279
      if(Cursor_doexectrace(self, &etos))
 
4280
        {
 
4281
          assert(PyErr_Occurred());
 
4282
          return NULL;  
 
4283
        }
 
4284
    }
 
4285
 
 
4286
  self->status=C_BEGIN;
 
4287
 
 
4288
  retval=Cursor_step(self);
 
4289
  if (!retval) 
 
4290
    {
 
4291
      assert(PyErr_Occurred());
 
4292
      return NULL;
 
4293
    }
 
4294
  Py_INCREF(retval);
 
4295
  return retval;
 
4296
}
 
4297
 
 
4298
static PyObject *
 
4299
Cursor_close(Cursor *self, PyObject *args)
 
4300
{
 
4301
  int res;
 
4302
  int force=0;
 
4303
 
 
4304
  CHECK_THREAD(self->connection, NULL);
 
4305
  if (!self->connection->db) /* if connection is closed, then we must also be closed */
 
4306
    return Py_BuildValue("");
 
4307
 
 
4308
  if(!PyArg_ParseTuple(args, "|i:close(force=False)", &force))
 
4309
    return NULL;
 
4310
 
 
4311
  res=resetcursor(self, force);
 
4312
  if(res!=SQLITE_OK)
 
4313
    {
 
4314
      assert(PyErr_Occurred());
 
4315
      return NULL;
 
4316
    }
 
4317
  
 
4318
  return Py_BuildValue("");
 
4319
}
 
4320
 
 
4321
static PyObject *
 
4322
Cursor_next(Cursor *self)
 
4323
{
 
4324
  PyObject *retval;
 
4325
  PyObject *item;
 
4326
  int numcols=-1;
 
4327
  int i;
 
4328
 
 
4329
  CHECK_THREAD(self->connection, NULL);
 
4330
  CHECK_CLOSED(self->connection, NULL);
 
4331
 
 
4332
 again:
 
4333
  if(self->status==C_BEGIN)
 
4334
    if(!Cursor_step(self))
 
4335
      {
 
4336
        assert(PyErr_Occurred());
 
4337
        return NULL;
 
4338
      }
 
4339
  if(self->status==C_DONE)
 
4340
    return NULL;
 
4341
 
 
4342
  assert(self->status==C_ROW);
 
4343
 
 
4344
  self->status=C_BEGIN;
 
4345
  
 
4346
  /* DUPLICATE(ish) code: this is substantially similar to the code in
 
4347
     convert_value_to_pyobject.  If you fix anything here then do it
 
4348
     there as well. */
 
4349
 
 
4350
  /* return the row of data */
 
4351
  numcols=sqlite3_data_count(self->statement);
 
4352
  retval=PyTuple_New(numcols);
 
4353
  if(!retval) 
 
4354
    return NULL;
 
4355
 
 
4356
  for(i=0;i<numcols;i++)
 
4357
    {
 
4358
      item=convert_value_to_pyobject(sqlite3_column_value(self->statement, i));
 
4359
      if(!item) 
 
4360
        return NULL;
 
4361
      PyTuple_SET_ITEM(retval, i, item);
 
4362
    }
 
4363
  if(self->rowtrace)
 
4364
    {
 
4365
      PyObject *r2=Cursor_dorowtrace(self, retval);
 
4366
      Py_DECREF(retval);
 
4367
      if(!r2) 
 
4368
        return NULL;
 
4369
      if (r2==Py_None)
 
4370
        {
 
4371
          Py_DECREF(r2);
 
4372
          goto again;
 
4373
        }
 
4374
      return r2;
 
4375
    }
 
4376
  return retval;
 
4377
}
 
4378
 
 
4379
static PyObject *
 
4380
Cursor_iter(Cursor *self)
 
4381
{
 
4382
  CHECK_THREAD(self->connection, NULL);
 
4383
  CHECK_CLOSED(self->connection, NULL);
 
4384
 
 
4385
  Py_INCREF(self);
 
4386
  return (PyObject*)self;
 
4387
}
 
4388
 
 
4389
static PyObject *
 
4390
Cursor_setexectrace(Cursor *self, PyObject *func)
 
4391
{
 
4392
  CHECK_THREAD(self->connection, NULL);
 
4393
  CHECK_CLOSED(self->connection, NULL);
 
4394
 
 
4395
  if(func!=Py_None && !PyCallable_Check(func))
 
4396
    {
 
4397
      PyErr_SetString(PyExc_TypeError, "parameter must be callable");
 
4398
      return NULL;
 
4399
    }
 
4400
 
 
4401
  if(func!=Py_None)
 
4402
    Py_INCREF(func);
 
4403
 
 
4404
  Py_XDECREF(self->exectrace);
 
4405
  self->exectrace=(func!=Py_None)?func:NULL;
 
4406
 
 
4407
  return Py_BuildValue("");
 
4408
}
 
4409
 
 
4410
static PyObject *
 
4411
Cursor_setrowtrace(Cursor *self, PyObject *func)
 
4412
{
 
4413
  CHECK_THREAD(self->connection, NULL);
 
4414
  CHECK_CLOSED(self->connection, NULL);
 
4415
 
 
4416
  if(func!=Py_None && !PyCallable_Check(func))
 
4417
    {
 
4418
      PyErr_SetString(PyExc_TypeError, "parameter must be callable");
 
4419
      return NULL;
 
4420
    }
 
4421
 
 
4422
  if(func!=Py_None)
 
4423
    Py_INCREF(func);
 
4424
 
 
4425
  Py_XDECREF(self->rowtrace);
 
4426
  self->rowtrace=(func!=Py_None)?func:NULL;
 
4427
 
 
4428
  return Py_BuildValue("");
 
4429
}
 
4430
 
 
4431
static PyObject *
 
4432
Cursor_getexectrace(Cursor *self)
 
4433
{
 
4434
  PyObject *ret;
 
4435
 
 
4436
  CHECK_THREAD(self->connection, NULL);
 
4437
  CHECK_CLOSED(self->connection, NULL);
 
4438
 
 
4439
  ret=(self->exectrace)?(self->exectrace):Py_None;
 
4440
  Py_INCREF(ret);
 
4441
  return ret;
 
4442
}
 
4443
 
 
4444
static PyObject *
 
4445
Cursor_getrowtrace(Cursor *self)
 
4446
{
 
4447
  PyObject *ret;
 
4448
  CHECK_THREAD(self->connection, NULL);
 
4449
  CHECK_CLOSED(self->connection, NULL);
 
4450
  ret =(self->rowtrace)?(self->rowtrace):Py_None;
 
4451
  Py_INCREF(ret);
 
4452
  return ret;
 
4453
}
 
4454
 
 
4455
static PyObject *
 
4456
Cursor_getconnection(Cursor *self)
 
4457
{
 
4458
  CHECK_THREAD(self->connection, NULL);
 
4459
  CHECK_CLOSED(self->connection, NULL);
 
4460
 
 
4461
  Py_INCREF(self->connection);
 
4462
  return (PyObject*)self->connection;
 
4463
}
 
4464
 
 
4465
static PyMethodDef Cursor_methods[] = {
 
4466
  {"execute", (PyCFunction)Cursor_execute, METH_VARARGS,
 
4467
   "Executes one or more statements" },
 
4468
  {"executemany", (PyCFunction)Cursor_executemany, METH_VARARGS,
 
4469
   "Repeatedly executes statements on sequence" },
 
4470
  {"next", (PyCFunction)Cursor_next, METH_NOARGS,
 
4471
   "Returns next row returned from query"},
 
4472
  {"setexectrace", (PyCFunction)Cursor_setexectrace, METH_O,
 
4473
   "Installs a function called for every statement executed"},
 
4474
  {"setrowtrace", (PyCFunction)Cursor_setrowtrace, METH_O,
 
4475
   "Installs a function called for every row returned"},
 
4476
  {"getexectrace", (PyCFunction)Cursor_getexectrace, METH_NOARGS,
 
4477
   "Returns the current exec tracer function"},
 
4478
  {"getrowtrace", (PyCFunction)Cursor_getrowtrace, METH_NOARGS,
 
4479
   "Returns the current row tracer function"},
 
4480
  {"getrowtrace", (PyCFunction)Cursor_getrowtrace, METH_NOARGS,
 
4481
   "Returns the current row tracer function"},
 
4482
  {"getconnection", (PyCFunction)Cursor_getconnection, METH_NOARGS,
 
4483
   "Returns the connection object for this cursor"},
 
4484
  {"getdescription", (PyCFunction)Cursor_getdescription, METH_NOARGS,
 
4485
   "Returns the description for the current row"},
 
4486
  {"close", (PyCFunction)Cursor_close, METH_VARARGS,
 
4487
   "Closes the cursor" },
 
4488
  {0, 0, 0, 0}  /* Sentinel */
 
4489
};
 
4490
 
 
4491
 
 
4492
static PyTypeObject CursorType = {
 
4493
    PyObject_HEAD_INIT(NULL)
 
4494
    0,                         /*ob_size*/
 
4495
    "apsw.Cursor",             /*tp_name*/
 
4496
    sizeof(Cursor),            /*tp_basicsize*/
 
4497
    0,                         /*tp_itemsize*/
 
4498
    (destructor)Cursor_dealloc, /*tp_dealloc*/
 
4499
    0,                         /*tp_print*/
 
4500
    0,                         /*tp_getattr*/
 
4501
    0,                         /*tp_setattr*/
 
4502
    0,                         /*tp_compare*/
 
4503
    0,                         /*tp_repr*/
 
4504
    0,                         /*tp_as_number*/
 
4505
    0,                         /*tp_as_sequence*/
 
4506
    0,                         /*tp_as_mapping*/
 
4507
    0,                         /*tp_hash */
 
4508
    0,                         /*tp_call*/
 
4509
    0,                         /*tp_str*/
 
4510
    0,                         /*tp_getattro*/
 
4511
    0,                         /*tp_setattro*/
 
4512
    0,                         /*tp_as_buffer*/
 
4513
    Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_ITER , /*tp_flags*/
 
4514
    "Cursor object",           /* tp_doc */
 
4515
    0,                         /* tp_traverse */
 
4516
    0,                         /* tp_clear */
 
4517
    0,                         /* tp_richcompare */
 
4518
    0,                         /* tp_weaklistoffset */
 
4519
    (getiterfunc)Cursor_iter,  /* tp_iter */
 
4520
    (iternextfunc)Cursor_next, /* tp_iternext */
 
4521
    Cursor_methods,            /* tp_methods */
 
4522
    0,                         /* tp_members */
 
4523
    0,                         /* tp_getset */
 
4524
    0,                         /* tp_base */
 
4525
    0,                         /* tp_dict */
 
4526
    0,                         /* tp_descr_get */
 
4527
    0,                         /* tp_descr_set */
 
4528
    0,                         /* tp_dictoffset */
 
4529
    0,                         /* tp_init */
 
4530
    0,                         /* tp_alloc */
 
4531
    0,                         /* tp_new */
 
4532
    0,                         /* tp_free */
 
4533
    0,                         /* tp_is_gc */
 
4534
    0,                         /* tp_bases */
 
4535
    0,                         /* tp_mro */
 
4536
    0,                         /* tp_cache */
 
4537
    0,                         /* tp_subclasses */
 
4538
    0,                         /* tp_weaklist */
 
4539
    0,                         /* tp_del */
 
4540
};
 
4541
 
 
4542
/* MODULE METHODS */
 
4543
static PyObject *
 
4544
getsqliteversion(void)
 
4545
{
 
4546
  return PyString_FromString(sqlite3_libversion());
 
4547
}
 
4548
 
 
4549
static PyObject *
 
4550
getapswversion(void)
 
4551
{
 
4552
  return PyString_FromString(APSW_VERSION);
 
4553
}
 
4554
 
 
4555
static PyObject *
 
4556
enablesharedcache(PyObject *self, PyObject *args)
 
4557
{
 
4558
  int setting,res;
 
4559
  if(!PyArg_ParseTuple(args, "i:enablesharedcache(boolean)", &setting))
 
4560
    return NULL;
 
4561
 
 
4562
  res=sqlite3_enable_shared_cache(setting);
 
4563
  SET_EXC(NULL, res);
 
4564
 
 
4565
  if(res!=SQLITE_OK)
 
4566
    return NULL;
 
4567
 
 
4568
  return Py_BuildValue("");
 
4569
}
 
4570
 
 
4571
static PyMethodDef module_methods[] = {
 
4572
  {"sqlitelibversion", (PyCFunction)getsqliteversion, METH_NOARGS,
 
4573
   "Return the version of the SQLite library"},
 
4574
  {"apswversion", (PyCFunction)getapswversion, METH_NOARGS,
 
4575
   "Return the version of the APSW wrapper"},
 
4576
  {"enablesharedcache", (PyCFunction)enablesharedcache, METH_VARARGS,
 
4577
   "Sets shared cache semantics for this thread"},
 
4578
 
 
4579
    {0, 0, 0, 0}  /* Sentinel */
 
4580
};
 
4581
 
 
4582
 
 
4583
 
 
4584
#ifndef PyMODINIT_FUNC  /* declarations for DLL import/export */
 
4585
#define PyMODINIT_FUNC void
 
4586
#endif
 
4587
PyMODINIT_FUNC
 
4588
initapsw(void) 
 
4589
{
 
4590
    PyObject *m;
 
4591
    PyObject *thedict;
 
4592
    PyObject *hooks;
 
4593
 
 
4594
    assert(sizeof(int)==4);             /* we expect 32 bit ints */
 
4595
    assert(sizeof(long long)==8);             /* we expect 64 bit long long */
 
4596
 
 
4597
    if (PyType_Ready(&ConnectionType) < 0)
 
4598
        return;
 
4599
 
 
4600
    if (PyType_Ready(&CursorType) < 0)
 
4601
        return;
 
4602
 
 
4603
    /* ensure threads are available */
 
4604
    PyEval_InitThreads();
 
4605
 
 
4606
 
 
4607
    m = apswmodule = Py_InitModule3("apsw", module_methods,
 
4608
                       "Another Python SQLite Wrapper.");
 
4609
 
 
4610
    if (m == NULL)  return;
 
4611
 
 
4612
    if(init_exceptions(m)) return;
 
4613
 
 
4614
    Py_INCREF(&ConnectionType);
 
4615
    PyModule_AddObject(m, "Connection", (PyObject *)&ConnectionType);
 
4616
 
 
4617
    /* we don't add cursor to the module since users shouldn't be able to instantiate them directly */
 
4618
 
 
4619
    hooks=PyList_New(0);
 
4620
    if(!hooks) return;
 
4621
    PyModule_AddObject(m, "connection_hooks", hooks);
 
4622
    
 
4623
 
 
4624
    /* add in some constants and also put them in a corresponding mapping dictionary */
 
4625
 
 
4626
#define ADDINT(v) PyModule_AddObject(m, #v, Py_BuildValue("i", v)); \
 
4627
         PyDict_SetItemString(thedict, #v, Py_BuildValue("i", v));  \
 
4628
         PyDict_SetItem(thedict, Py_BuildValue("i", v), Py_BuildValue("s", #v));
 
4629
 
 
4630
    thedict=PyDict_New();
 
4631
    if(!thedict) return;
 
4632
 
 
4633
    ADDINT(SQLITE_DENY);
 
4634
    ADDINT(SQLITE_IGNORE);
 
4635
    ADDINT(SQLITE_OK);
 
4636
    
 
4637
    PyModule_AddObject(m, "mapping_authorizer_return", thedict);
 
4638
 
 
4639
    /* authorizer functions */
 
4640
    thedict=PyDict_New();
 
4641
    if(!thedict) return;
 
4642
 
 
4643
    ADDINT(SQLITE_CREATE_INDEX);
 
4644
    ADDINT(SQLITE_CREATE_TABLE);
 
4645
    ADDINT(SQLITE_CREATE_TEMP_INDEX);
 
4646
    ADDINT(SQLITE_CREATE_TEMP_TABLE);
 
4647
    ADDINT(SQLITE_CREATE_TEMP_TRIGGER);
 
4648
    ADDINT(SQLITE_CREATE_TEMP_VIEW);
 
4649
    ADDINT(SQLITE_CREATE_TRIGGER);
 
4650
    ADDINT(SQLITE_CREATE_VIEW);
 
4651
    ADDINT(SQLITE_DELETE);
 
4652
    ADDINT(SQLITE_DROP_INDEX);
 
4653
    ADDINT(SQLITE_DROP_TABLE);
 
4654
    ADDINT(SQLITE_DROP_TEMP_INDEX);
 
4655
    ADDINT(SQLITE_DROP_TEMP_TABLE);
 
4656
    ADDINT(SQLITE_DROP_TEMP_TRIGGER);
 
4657
    ADDINT(SQLITE_DROP_TEMP_VIEW);
 
4658
    ADDINT(SQLITE_DROP_TRIGGER);
 
4659
    ADDINT(SQLITE_DROP_VIEW);
 
4660
    ADDINT(SQLITE_INSERT);
 
4661
    ADDINT(SQLITE_PRAGMA);
 
4662
    ADDINT(SQLITE_READ);
 
4663
    ADDINT(SQLITE_SELECT);
 
4664
    ADDINT(SQLITE_TRANSACTION);
 
4665
    ADDINT(SQLITE_UPDATE);
 
4666
    ADDINT(SQLITE_ATTACH);
 
4667
    ADDINT(SQLITE_DETACH);
 
4668
    ADDINT(SQLITE_ALTER_TABLE);
 
4669
    ADDINT(SQLITE_REINDEX);
 
4670
    ADDINT(SQLITE_COPY);
 
4671
    ADDINT(SQLITE_ANALYZE);
 
4672
    ADDINT(SQLITE_CREATE_VTABLE);
 
4673
    ADDINT(SQLITE_DROP_VTABLE);
 
4674
    ADDINT(SQLITE_FUNCTION);
 
4675
 
 
4676
    PyModule_AddObject(m, "mapping_authorizer_function", thedict);
 
4677
 
 
4678
    /* Version number */
 
4679
    PyModule_AddObject(m, "SQLITE_VERSION_NUMBER", Py_BuildValue("i", SQLITE_VERSION_NUMBER));
 
4680
 
 
4681
    /* vtable best index constraints */
 
4682
#if defined(SQLITE_INDEX_CONSTRAINT_EQ) && defined(SQLITE_INDEX_CONSTRAINT_MATCH)
 
4683
 
 
4684
    thedict=PyDict_New();
 
4685
    if(!thedict) return;
 
4686
 
 
4687
    ADDINT(SQLITE_INDEX_CONSTRAINT_EQ);
 
4688
    ADDINT(SQLITE_INDEX_CONSTRAINT_GT);
 
4689
    ADDINT(SQLITE_INDEX_CONSTRAINT_LE);
 
4690
    ADDINT(SQLITE_INDEX_CONSTRAINT_LT);
 
4691
    ADDINT(SQLITE_INDEX_CONSTRAINT_GE);
 
4692
    ADDINT(SQLITE_INDEX_CONSTRAINT_MATCH);
 
4693
 
 
4694
    PyModule_AddObject(m, "mapping_bestindex_constraints", thedict);
 
4695
 
 
4696
#endif /* constraints */
 
4697
 
 
4698
    /* extendended result codes */
 
4699
    thedict=PyDict_New();
 
4700
    if(!thedict) return;
 
4701
 
 
4702
    ADDINT(SQLITE_IOERR_READ);
 
4703
    ADDINT(SQLITE_IOERR_SHORT_READ);
 
4704
    ADDINT(SQLITE_IOERR_WRITE);
 
4705
    ADDINT(SQLITE_IOERR_FSYNC);
 
4706
    ADDINT(SQLITE_IOERR_DIR_FSYNC);
 
4707
    ADDINT(SQLITE_IOERR_TRUNCATE);
 
4708
    ADDINT(SQLITE_IOERR_FSTAT);
 
4709
    ADDINT(SQLITE_IOERR_UNLOCK);
 
4710
    ADDINT(SQLITE_IOERR_RDLOCK);
 
4711
    PyModule_AddObject(m, "mapping_extended_result_codes", thedict);
 
4712
 
 
4713
    /* error codes */
 
4714
    thedict=PyDict_New();
 
4715
    if(!thedict) return;
 
4716
 
 
4717
    ADDINT(SQLITE_OK);
 
4718
    ADDINT(SQLITE_ERROR);
 
4719
    ADDINT(SQLITE_INTERNAL);
 
4720
    ADDINT(SQLITE_PERM);
 
4721
    ADDINT(SQLITE_ABORT);
 
4722
    ADDINT(SQLITE_BUSY);
 
4723
    ADDINT(SQLITE_LOCKED);
 
4724
    ADDINT(SQLITE_NOMEM);
 
4725
    ADDINT(SQLITE_READONLY);
 
4726
    ADDINT(SQLITE_INTERRUPT);
 
4727
    ADDINT(SQLITE_IOERR);
 
4728
    ADDINT(SQLITE_CORRUPT);
 
4729
    ADDINT(SQLITE_FULL);
 
4730
    ADDINT(SQLITE_CANTOPEN);
 
4731
    ADDINT(SQLITE_PROTOCOL);
 
4732
    ADDINT(SQLITE_EMPTY);
 
4733
    ADDINT(SQLITE_SCHEMA);
 
4734
    ADDINT(SQLITE_CONSTRAINT);
 
4735
    ADDINT(SQLITE_MISMATCH);
 
4736
    ADDINT(SQLITE_MISUSE);
 
4737
    ADDINT(SQLITE_NOLFS);
 
4738
    ADDINT(SQLITE_AUTH);
 
4739
    ADDINT(SQLITE_FORMAT);
 
4740
    ADDINT(SQLITE_RANGE);
 
4741
    ADDINT(SQLITE_NOTADB);
 
4742
 
 
4743
    PyModule_AddObject(m, "mapping_result_codes", thedict);
 
4744
 
 
4745
}
 
4746