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
|
/* KInterbasDB Python Package - Implementation of Parameter Conversion DB->Py
**
** 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>
*/
/* This source file is designed to be directly included in _kiconversion.c,
** without the involvement of a header file. */
static PyObject *conv_out_char(char *data, int size) {
return PyString_FromStringAndSize(data, size);
} /* conv_out_char */
static PyObject *conv_out_varchar(char *data) {
/* The first sizeof(short) bytes contain the size of the string. */
return PyString_FromStringAndSize(
data + sizeof(short),
(int) *( (short *)data )
);
} /* conv_out_varchar */
static PyObject *_conv_out_integer_types(
PyObject *py_raw, boolean is_fixed_point, short scale
)
{
if (!is_fixed_point) {
/* Simply return the integer value. */
return py_raw;
} else {
/* We're converting a fixed-point rather than an integer; return a 2-tuple
** of the form: (value, scale) */
PyObject *fixed_tuple = PyTuple_New(2);
PyObject *py_scale;
if (fixed_tuple == NULL) {
return NULL;
}
py_scale = PyInt_FromLong(scale);
if (py_scale == NULL) {
Py_DECREF(fixed_tuple);
return NULL;
}
PyTuple_SET_ITEM(fixed_tuple, 0, py_raw); /* "steals" ref to py_raw */
PyTuple_SET_ITEM(fixed_tuple, 1, py_scale); /* "steals" ref to py_scale */
return fixed_tuple;
}
} /* _conv_out_integer_types_return_integer_or_fixed_tuple */
static PyObject *conv_out_short_long(
char *data,
short data_type, boolean is_fixed_point, short scale
)
{
/* 2004.04.16:64BC: On x86_64/1.5.1pre1, ISC_LONG is actually an int. */
long conv_long = (long)(
data_type == SQL_SHORT ?
( *((ISC_SHORT *) data) )
: ( *((ISC_LONG *) data) )
);
PyObject *py_int = PyInt_FromLong(conv_long);
return _conv_out_integer_types(py_int, is_fixed_point, scale);
} /* conv_out_short_long */
#ifdef INTERBASE6_OR_LATER
static PyObject *conv_out_int64(char *data,
boolean is_fixed_point, short scale
)
{
/* Python's long type, which has unlimited range, is computationally
** expensive relative to Python's int type, which wraps a native long. So:
** 1. On platforms where C's long is 64 bits or larger, unconditionally
** return a Python int.
** 2. On platforms where C's long is smaller than 64 bits, test the outgoing
** value at runtime to see if it's within the range of a Python int; if
** so, return a Python int rather than a Python long. It is expected that
** the cost of the test is less than the cost of using a Python long. */
#if defined(_MSC_VER) || defined(__BORLANDC__)
/* MSVC 6 and BCPP 5.5 won't accept the LL suffix. */
#define NativeLongIsAtLeast64Bits \
(LONG_MAX >= 9223372036854775807i64 && LONG_MIN <= (-9223372036854775807i64 - 1))
#else
#define NativeLongIsAtLeast64Bits \
(LONG_MAX >= 9223372036854775807LL && LONG_MIN <= (-9223372036854775807LL - 1))
#endif
#if NativeLongIsAtLeast64Bits
#define PythonIntOrLongFrom64BitValue(x) PyInt_FromLong((long) x)
#else
#define PythonIntOrLongFrom64BitValue(x) \
( (x < LONG_MIN || x > LONG_MAX) ? PyLong_FromLongLong(x) : PyInt_FromLong((long) x) )
#endif
const LONG_LONG dataLL = *((LONG_LONG *) data);
PyObject *py_int = PythonIntOrLongFrom64BitValue(dataLL);
return _conv_out_integer_types(py_int, is_fixed_point, scale);
} /* conv_out_int64 */
#endif /* INTERBASE6_OR_LATER */
static PyObject *conv_out_float(char *data) {
return PyFloat_FromDouble( *( (float *) data ) );
} /* conv_out_float */
static PyObject *conv_out_double(char *data) {
return PyFloat_FromDouble( *( (double *) data ) );
} /* conv_out_double */
/* Date/time types: */
static PyObject *conv_out_timestamp(char *data) {
struct tm c_tm;
ENTER_DB
isc_decode_timestamp( (ISC_TIMESTAMP *)data, &c_tm );
LEAVE_DB
return Py_BuildValue("(iiiiii)",
c_tm.tm_year+1900, c_tm.tm_mon+1, c_tm.tm_mday,
c_tm.tm_hour, c_tm.tm_min, c_tm.tm_sec
);
} /* conv_out_timestamp */
#ifdef INTERBASE6_OR_LATER
static PyObject *conv_out_date(char *data) {
struct tm c_tm;
ENTER_DB
isc_decode_sql_date( (ISC_DATE *)data, &c_tm );
LEAVE_DB
return Py_BuildValue("(iii)", c_tm.tm_year+1900, c_tm.tm_mon+1, c_tm.tm_mday);
} /* conv_out_date */
static PyObject *conv_out_time(char *data) {
struct tm c_tm;
ENTER_DB
isc_decode_sql_time( (ISC_TIME *)data, &c_tm );
LEAVE_DB
return Py_BuildValue("(iii)", c_tm.tm_hour, c_tm.tm_min, c_tm.tm_sec);
} /* conv_out_time */
#endif /* INTERBASE6_OR_LATER */
|