File: _kicore_cursor.c

package info (click to toggle)
python-kinterbasdb 3.1-1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 1,044 kB
  • ctags: 1,157
  • sloc: ansic: 6,879; python: 2,517; makefile: 77
file content (430 lines) | stat: -rw-r--r-- 13,629 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
/* KInterbasDB Python Package - Implementation of Cursor
**
** Version 3.1
**
** The following contributors hold Copyright (C) over their respective
** portions of code (see license.txt for details):
**
** [Original Author (maintained through version 2.0-0.3.1):]
**   1998-2001 [alex]  Alexander Kuznetsov   <alexan@users.sourceforge.net>
** [Maintainers (after version 2.0-0.3.1):]
**   2001-2002 [maz]   Marek Isalski         <kinterbasdb@maz.nu>
**   2002-2004 [dsr]   David Rushby          <woodsplitter@rocketmail.com>
** [Contributors:]
**   2001      [eac]   Evgeny A. Cherkashin  <eugeneai@icc.ru>
**   2001-2002 [janez] Janez Jere            <janez.jere@void.si>
*/


/* Remodelled this function to fail cleanly and consistently in case of a
** memory allocation error. */
static CursorObject *new_cursor(ConnectionObject *connection) {
  CursorObject *cursor;

  Py_INCREF(connection); /* It's important that this statement remain BEFORE
    ** the CursorObject instantiation. */

  cursor = PyObject_New(CursorObject, &CursorType);
  if (cursor == NULL) {
    goto NEW_CURSOR_FAIL;
  }

  cursor->connection = connection; /* Already incremented ref count above. */

  cursor->in_sqlda = NULL;
  if ( -1 == reallocate_sqlda( &(cursor->in_sqlda), TRUE ) ) {
    goto NEW_CURSOR_FAIL;
  }

  cursor->name = NULL;
  cursor->in_var_orig_spec = NULL;

  cursor->out_sqlda = NULL;
  if ( -1 == reallocate_sqlda( &(cursor->out_sqlda), FALSE ) ) {
    goto NEW_CURSOR_FAIL;
  }

  cursor->out_buffer = NULL;

  cursor->previous_sql = NULL;
  /* 2003.01.26: */
  cursor->statement_type = NULL_STATEMENT_TYPE;

  /* 2003.09.06b: */
  cursor->objects_to_release_after_execute = PyList_New(0);
  if (cursor->objects_to_release_after_execute == NULL) {
    goto NEW_CURSOR_FAIL;
  }

  cursor->exec_proc_results = NULL;
  cursor->last_fetch_status = 0;
  cursor->_state = CURSOR_STATE_OPEN;

  memcpy( &(cursor->status_vector),
      &(connection->status_vector),
      sizeof(ISC_STATUS) * STATUS_VECTOR_SIZE
    );

  /* 2002.08.17: New approach:  defer statement allocation until actually
  ** needed (that is, until function _prepare_statement_if_necessary is called). */
  cursor->stmt_handle = NULL;

  /* 2003.03.30: */
  cursor->type_trans_in = NULL;
  cursor->type_trans_out = NULL;

  /* 2003.10.16: */
  cursor->output_type_trans_return_type_dict = NULL;

  return cursor;

NEW_CURSOR_FAIL:
  Py_DECREF(connection);
  if (cursor != NULL) {
    PyObject_Del(cursor);
  }

  return NULL;
} /* new_cursor */


/* 2002.02.24:_cur_require_open
** If self is not an open cursor, raises the supplied error message
** (or a default if no error message is supplied).
** Returns 0 if the cursor was open; -1 if it failed the test. */
static int _cur_require_open(CursorObject *self, char *failure_message) {
  if ( self != NULL ) {
    char *conn_failure_message = "Invalid cursor state.  The connection"
      " associated with this cursor is not open, and therefore the cursor"
      " should not be open either.";
    if ( _conn_require_open(self->connection, conn_failure_message) != 0 ) {
      return -1;
    } else if ( self->_state == CURSOR_STATE_OPEN) {
      return 0;
    }
  }

  if (failure_message == NULL) {
      failure_message = "Invalid cursor state.  The cursor must be"
        " OPEN to perform this operation.";
  }
  raise_exception(ProgrammingError, failure_message);
  return -1;
} /* _cur_require_open */


static void free_cursor_cache(CursorObject *cursor) {
  Py_XDECREF(cursor->previous_sql);
  cursor->previous_sql = NULL;

  /* 2003.01.26: */
  cursor->statement_type = NULL_STATEMENT_TYPE;

  /* 2003.03.31: */
  if (cursor->in_var_orig_spec != NULL) {
    kimem_plain_free(cursor->in_var_orig_spec);
    cursor->in_var_orig_spec = NULL;
  }

  _free_cursor_exec_proc_results_cache(cursor);
} /* free_cursor_cache */


static void _free_cursor_exec_proc_results_cache(CursorObject *cursor) {
  /* Free ONLY those cursor variables that pertain to the cache of
  ** EXECUTE PROCEDURE results that was introduced in answer to bug #520793.
  ** This function is separate from free_cursor_cache so that it can be
  ** called separately, when it makes sense to free the cached EXEC PROC row,
  ** but not to free the cursor's memory as a whole. */

  if (cursor->exec_proc_results != NULL) {
    /* This block will only be reached if the client executed a result-
    ** returning stored procedure with the EXECUTE PROCEDURE statement
    ** (via Cursor.execute), but the client never retrieved the results
    ** (via Cursor.fetch*). */
    Py_DECREF(cursor->exec_proc_results);
    /* 2003.02.17c: PyObject_Del should not be necessary here.
    ** exec_proc_results is a tuple, so its destructor should call PyObject_Del
    ** when its reference count falls low enough.  The DECREF above should be
    ** all we need. */
    /* PyObject_Del(cursor->exec_proc_results); */
    cursor->exec_proc_results = NULL;
  }
} /* _free_cursor_exec_proc_results_cache */


static void clear_cursor(CursorObject *cursor, PyObject *sql_about_to_be_executed) {
  /* $sql_about_to_be_executed should be the incoming SQL string if this
  ** function is being called from pyob_execute, NULL if this function is being
  ** called from anywhere else. */

  /* This action might be thought of as a "retaining close". */
  _free_cursor_exec_proc_results_cache(cursor);

  /* Quote from IB 6 API Guide p334:
  ** "A cursor need only be closed [with DSQL_close before DSQL_drop] if it was
  ** previously opened and associated with stmt_handle by
  ** isc_dsql_set_cursor_name().
  ** DSQL_close closes a cursor, but the statement it was associated with
  ** remains available for further execution."
  **
  ** Additional wrinkle: if we're reusing an old statement handle (i.e., if the
  ** new SQL string sent for execution is the same as the previous SQL string),
  ** we must
  **   isc_dsql_free_statement(..., ..., DSQL_close);
  ** the handle, but not set it NULL (which would prevent reuse). */
  if (cursor->stmt_handle != NULL) {
    boolean new_sql_same_as_old =
      (
           sql_about_to_be_executed != NULL && cursor->previous_sql != NULL
        &&
           /* If the PyObject pointers point to the same memory location, the
           ** two objects are certainly equal--in fact, they're IDentical
           ** (id(sql_about_to_be_executed) == id(cursor->previous_sql)).  If
           ** the pointers refer to different memory locations, the two objects
           ** are still equal if their contents match. */
          (   sql_about_to_be_executed == cursor->previous_sql
           || PyObject_Compare(sql_about_to_be_executed, cursor->previous_sql) == 0
          )
      );
    boolean cursor_name_specified = cursor->name != NULL;
    if (new_sql_same_as_old || cursor_name_specified) {
      /* 2003.10.06:
      ** The Python layer of kinterbasdb shouldn't have allowed the connection to
      ** close without closing its cursors first, but if that *was* allowed to
      ** happen, there's nothing we can do about except avoid calling the
      ** now-dangerous isc_dsql_free_statement. */
      if (cursor->connection->_state != CONNECTION_STATE_CLOSED) {
        ENTER_DB
        isc_dsql_free_statement( cursor->status_vector, &(cursor->stmt_handle),
            DSQL_close
          );
        LEAVE_DB
      }
      if (!new_sql_same_as_old) {
        cursor->stmt_handle = NULL;
      }

      if (cursor_name_specified) {
        Py_DECREF(cursor->name);
        cursor->name = NULL;
      }
    }
  }

  /* Clear the fetch status flag because we might be about to deal with a new
  ** result set.  In any case, we will never again fetch from the old result
  ** set (if any). */
  cursor->last_fetch_status = 0;

  cursor->_state = CURSOR_STATE_CLOSED;
} /* clear_cursor */


void close_cursor(CursorObject *cursor) {
  clear_cursor(cursor, NULL);

  /* Moved this operation here from delete_cursor in cursor state management
  ** reorganization. */
  if (cursor->stmt_handle != NULL) {
    /* 2003.10.06:
    ** The Python layer of kinterbasdb shouldn't have allowed the connection to
    ** close without closing its cursors first, but if that *was* allowed to
    ** happen, there's nothing we can do about except avoid calling the
    ** now-dangerous isc_dsql_free_statement. */
    if (cursor->connection->_state != CONNECTION_STATE_CLOSED) {
      ENTER_DB
      isc_dsql_free_statement( cursor->status_vector, &(cursor->stmt_handle),
          DSQL_drop /* DSQL_drop means "free resources allocated for this statement." */
        );
      LEAVE_DB
    }
    cursor->stmt_handle = NULL;
  }

  if (cursor->out_buffer != NULL) {
    kimem_main_free(cursor->out_buffer);
    cursor->out_buffer = NULL;
  }

  if (cursor->name != NULL) {
    Py_DECREF(cursor->name);
    cursor->name = NULL;
  }

  free_cursor_cache(cursor);
} /* close_cursor */


/* 2003.02.17c: */
void close_cursor_with_error(CursorObject *cursor) {
  /* Close the cursor because one of its operation resulted in an exception,
  ** but flag the cursor as ready to be used again. */
  close_cursor(cursor); /* 2003.02.17c: No change needed. */
  cursor->_state = CURSOR_STATE_OPEN;
} /* close_cursor_with_error */


static void delete_cursor(CursorObject *cursor) {
  assert (cursor->connection != NULL);
  close_cursor(cursor); /* 2003.02.17c: No change needed. */

  Py_DECREF(cursor->connection);
  cursor->connection = NULL;

  if (cursor->in_sqlda != NULL) {
    /* 2003.02.13:  Moved deallocation of sqlinds here. */
    int i;
    for ( i = 0; i < cursor->in_sqlda->sqln; i++ ) {
      XSQLVAR *sqlvar = cursor->in_sqlda->sqlvar + i;

      assert (sqlvar->sqlind != NULL);
      kimem_main_free(sqlvar->sqlind); /* The NULL indicator flag. */
      sqlvar->sqlind = NULL;
    }

    /* 2003.03.31: */
    kimem_xsqlda_free(cursor->in_sqlda);
    cursor->in_sqlda = NULL;
  }

  if (cursor->out_sqlda != NULL) {
    /* 2003.03.31: */
    kimem_xsqlda_free(cursor->out_sqlda);
    cursor->out_sqlda = NULL;
  }

  /* 2003.09.06b: */
  Py_XDECREF(cursor->objects_to_release_after_execute);

  /* 2003.03.30: */
  Py_XDECREF(cursor->type_trans_in);
  Py_XDECREF(cursor->type_trans_out);

  /* 2003.10.16: */
  Py_XDECREF(cursor->output_type_trans_return_type_dict);
} /* delete_cursor */


static void pyob_cursor_del(PyObject *cursor) {
  delete_cursor( (CursorObject *) cursor );
  PyObject_Del( cursor );
} /* pyob_cursor_del */


static PyObject *pyob_cursor(PyObject *self, PyObject *args) {
  ConnectionObject *connection;

  if ( !PyArg_ParseTuple( args, "O!", &ConnectionType, &connection ) ) {
    return NULL;
  }

  CONN_REQUIRE_OPEN(connection);

  return (PyObject *) new_cursor(connection);
} /* pyob_cursor */


static PyObject *pyob_close_cursor(PyObject *self, PyObject *args) {
  CursorObject *cursor;

  if ( !PyArg_ParseTuple( args, "O!", &CursorType, &cursor ) ) {
    return NULL;
  }

  CUR_REQUIRE_OPEN(cursor);

  close_cursor(cursor); /* 2003.02.17c: No change needed. */

  RETURN_PY_NONE;
} /* pyob_close_cusor */


static PyObject *pyob_set_cursor_name(PyObject *self, PyObject *args) {
  CursorObject *cursor;
  PyObject *name;

  if ( !PyArg_ParseTuple( args, "O!O!", &CursorType, &cursor, &PyString_Type, &name ) ) {
    return NULL;
  }
  CUR_REQUIRE_OPEN(cursor);

  if (cursor->stmt_handle == NULL) {
    raise_exception_with_numeric_error_code(ProgrammingError, -901,
        "This cursor has not yet executed a statement, so setting its 'name'"
        " would be meaningless."
      );
    return NULL;
  }

  if (cursor->name != NULL) {
    /* Cannot reset the cursor's name while operating in the context of the
    ** same statement for which the original name was declared. */
    raise_exception_with_numeric_error_code(ProgrammingError, -502,
        "Cannot set this cursor's name, because its name has already been"
        " declared in the context of the statement that the cursor is"
        " currently executing."
      );
    return NULL;
  }

  /* Now make the association inside the database. */
  {
    char *name_ptr = PyString_AsString(name);

    ENTER_DB
    isc_dsql_set_cursor_name(cursor->status_vector,
        &(cursor->stmt_handle), name_ptr, (unsigned short)NULL
      );
    LEAVE_DB
  }

  if ( DB_API_ERROR(cursor->status_vector) ) {
    raise_sql_exception( OperationalError,
        "Could not set cursor name: ", cursor->status_vector
      );
    return NULL;
  }

  /* Store the name for retrieval from the Python level. */
  Py_INCREF(name);
  cursor->name = name;

  RETURN_PY_NONE;
} /* pyob_set_cursor_name */


static PyObject *pyob_get_cursor_name(PyObject *self, PyObject *args) {
  CursorObject *cursor;

  if ( !PyArg_ParseTuple( args, "O!", &CursorType, &cursor ) ) {
    return NULL;
  }
  CUR_REQUIRE_OPEN(cursor);

  if (cursor->name == NULL) {
    RETURN_PY_NONE;
  } else {
    Py_INCREF(cursor->name);
    return cursor->name;
  }
} /* pyob_get_cursor_name */


/* 2003.10.06: */
static PyObject *pyob_is_purportedly_open(PyObject *self, PyObject *args) {
  PyObject *incoming;

  if ( !PyArg_ParseTuple( args, "O", &incoming ) ) {
    return NULL;
  }

  if ( PyObject_TypeCheck(incoming, &ConnectionType) ) {
    return PyBool_FromLong( ((ConnectionObject *) incoming)->_state == CONNECTION_STATE_OPEN );
  } else if ( PyObject_TypeCheck(incoming, &CursorType) ) {
    return PyBool_FromLong( ((CursorObject *) incoming)->_state == CURSOR_STATE_OPEN );
  } else {
    PyErr_SetString(PyExc_TypeError, "Object must be of type ConnectionType or CursorType.");
    return NULL;
  }
} /* pyob_get_cursor_name */