mirror of
https://github.com/PyMySQL/mysqlclient.git
synced 2025-08-16 03:50:43 +08:00
Move escape_dict functionality into _mysql.
str(long) in Python 2.0 doesn't add a trailing L.
This commit is contained in:
@ -32,8 +32,9 @@ try:
|
||||
except ImportError:
|
||||
_threading = None
|
||||
|
||||
def Long2Int(l): return str(l)[:-1] # drop the trailing L
|
||||
def Long2Int(l): s = str(l); return s[-1] == 'L' and s[:-1] or s
|
||||
def None2NULL(d): return "NULL"
|
||||
def Thing2Literal(o): return string_literal(str(o))
|
||||
|
||||
# MySQL-3.23.xx now has a new escape_string function that uses
|
||||
# the connection to determine what character set is in use and
|
||||
@ -140,12 +141,6 @@ def Binary(x): return str(x)
|
||||
|
||||
insert_values = re.compile(r'values\s(\(.+\))', re.IGNORECASE)
|
||||
|
||||
def escape_dict(d, qc):
|
||||
d2 = {}
|
||||
for k,v in d.items():
|
||||
d2[k] = qc.get(type(v), String2Literal)(v)
|
||||
return d2
|
||||
|
||||
def _fetchall(result, *args):
|
||||
rows = r = list(apply(result.fetch_row, args))
|
||||
while 1:
|
||||
|
@ -586,15 +586,18 @@ _mysql_string_literal(
|
||||
_mysql_ConnectionObject *self,
|
||||
PyObject *args)
|
||||
{
|
||||
PyObject *str;
|
||||
PyObject *str, *s, *o;
|
||||
char *in, *out;
|
||||
int len, size;
|
||||
if (!PyArg_ParseTuple(args, "s#:string_literal", &in, &size)) return NULL;
|
||||
if (!PyArg_ParseTuple(args, "O:string_literal", &o)) return NULL;
|
||||
s = PyObject_Str(o);
|
||||
in = PyString_AsString(s);
|
||||
size = PyString_GET_SIZE(s);
|
||||
str = PyString_FromStringAndSize((char *) NULL, size*2+3);
|
||||
if (!str) return PyErr_NoMemory();
|
||||
out = PyString_AS_STRING(str);
|
||||
#if MYSQL_VERSION_ID < 32321
|
||||
len = mysql_escape_string(out+1, in, size);
|
||||
len = mysql_escape_string(out+1, s, size);
|
||||
#else
|
||||
if (self)
|
||||
len = mysql_real_escape_string(&(self->connection), out+1, in, size);
|
||||
@ -603,26 +606,19 @@ _mysql_string_literal(
|
||||
#endif
|
||||
*out = *(out+len+1) = '\'';
|
||||
if (_PyString_Resize(&str, len+2) < 0) return NULL;
|
||||
Py_DECREF(s);
|
||||
return (str);
|
||||
}
|
||||
|
||||
static PyObject *_mysql_NULL;
|
||||
|
||||
static PyObject *
|
||||
_mysql_escape_row(
|
||||
PyObject *self,
|
||||
PyObject *args)
|
||||
_escape_item(
|
||||
PyObject *item,
|
||||
PyObject *d)
|
||||
{
|
||||
PyObject *o=NULL, *d=NULL, *r=NULL, *item, *quoted,
|
||||
*itemtype, *itemconv;
|
||||
PyObject *quoted, *itemtype, *itemconv;
|
||||
int i, n;
|
||||
if (!PyArg_ParseTuple(args, "O!O!:escape_row", &PyTuple_Type, &o,
|
||||
&PyDict_Type, &d))
|
||||
goto error;
|
||||
if (!(n = PyObject_Length(o))) goto error;
|
||||
if (!(r = PyTuple_New(n))) goto error;
|
||||
for (i=0; i<n; i++) {
|
||||
item = PyTuple_GET_ITEM(o, i);
|
||||
if (!(itemtype = PyObject_Type(item)))
|
||||
goto error;
|
||||
itemconv = PyObject_GetItem(d, itemtype);
|
||||
@ -640,6 +636,27 @@ _mysql_escape_row(
|
||||
quoted = PyObject_CallFunction(itemconv, "O", item);
|
||||
Py_DECREF(itemconv);
|
||||
if (!quoted) goto error;
|
||||
return quoted;
|
||||
error:
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static PyObject *
|
||||
_mysql_escape_row(
|
||||
PyObject *self,
|
||||
PyObject *args)
|
||||
{
|
||||
PyObject *o=NULL, *d=NULL, *r=NULL, *item, *quoted;
|
||||
int i, n;
|
||||
if (!PyArg_ParseTuple(args, "O!O!:escape_row", &PyTuple_Type, &o,
|
||||
&PyDict_Type, &d))
|
||||
goto error;
|
||||
if (!(n = PyObject_Length(o))) goto error;
|
||||
if (!(r = PyTuple_New(n))) goto error;
|
||||
for (i=0; i<n; i++) {
|
||||
item = PyTuple_GET_ITEM(o, i);
|
||||
quoted = _escape_item(item, d);
|
||||
if (!quoted) goto error;
|
||||
PyTuple_SET_ITEM(r, i, quoted);
|
||||
}
|
||||
return r;
|
||||
@ -648,6 +665,29 @@ _mysql_escape_row(
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static PyObject *
|
||||
_mysql_escape_dict(
|
||||
PyObject *self,
|
||||
PyObject *args)
|
||||
{
|
||||
PyObject *o=NULL, *d=NULL, *r=NULL, *item, *quoted, *pkey;
|
||||
int ppos = 0;
|
||||
int i, n;
|
||||
if (!PyArg_ParseTuple(args, "O!O!:escape_dict", &PyDict_Type, &o,
|
||||
&PyDict_Type, &d))
|
||||
goto error;
|
||||
if (!(r = PyDict_New())) goto error;
|
||||
while (PyDict_Next(o, &ppos, &pkey, &item)) {
|
||||
quoted = _escape_item(item, d);
|
||||
if (!quoted) goto error;
|
||||
if (PyDict_SetItem(r, pkey, quoted)==-1) goto error;
|
||||
}
|
||||
return r;
|
||||
error:
|
||||
Py_XDECREF(r);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
static PyObject *
|
||||
_mysql_ResultObject_describe(
|
||||
@ -1478,6 +1518,7 @@ _mysql_methods[] = {
|
||||
{ "connect", (PyCFunction)_mysql_connect, METH_VARARGS | METH_KEYWORDS },
|
||||
{ "debug", (PyCFunction)_mysql_debug, METH_VARARGS },
|
||||
{ "escape_row", (PyCFunction)_mysql_escape_row, METH_VARARGS },
|
||||
{ "escape_dict", (PyCFunction)_mysql_escape_dict, METH_VARARGS },
|
||||
{ "escape_string", (PyCFunction)_mysql_escape_string, METH_VARARGS },
|
||||
{ "string_literal", (PyCFunction)_mysql_string_literal, METH_VARARGS },
|
||||
{ "get_client_info", (PyCFunction)_mysql_get_client_info },
|
||||
|
Reference in New Issue
Block a user