/*
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version. Alternatively, you may use the original license
reproduced below.
Copyright 1999 by Comstar.net, Inc., Atlanta, GA, US.
All Rights Reserved
Permission to use, copy, modify, and distribute this software and its
documentation for any purpose and without fee is hereby granted,
provided that the above copyright notice appear in all copies and that
both that copyright notice and this permission notice appear in
supporting documentation, and that the name of Comstar.net, Inc.
or COMSTAR not be used in advertising or publicity pertaining to
distribution of the software without specific, written prior permission.
COMSTAR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
EVENT SHALL COMSTAR BE LIABLE FOR ANY SPECIAL, INDIRECT OR
CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "pymemcompat.h"
#include "structmember.h"
#if defined(MS_WINDOWS)
#include <winsock2.h>
#include <windows.h>
#include <config-win.h>
#else
#include "my_config.h"
#endif
#include "mysql.h"
#include "mysqld_error.h"
#include "errmsg.h"
#if PY_VERSION_HEX < 0x02020000
# define MyTuple_Resize(t,n,d) _PyTuple_Resize(t, n, d)
# define MyMember(a,b,c,d,e) {a,b,c,d}
# define MyMemberlist(x) struct memberlist x
# define MyAlloc(s,t) PyObject_New(s,&t)
# define MyFree(o) PyObject_Del(o)
#else
# define MyTuple_Resize(t,n,d) _PyTuple_Resize(t, n)
# define MyMember(a,b,c,d,e) {a,b,c,d,e}
# define MyMemberlist(x) struct PyMemberDef x
# define MyAlloc(s,t) (s *) t.tp_alloc(&t,0)
# define MyFree(ob) ob->ob_type->tp_free((PyObject *)ob)
#endif
#if PY_VERSION_HEX < 0x02050000 && !defined(PY_SSIZE_T_MIN)
typedef int Py_ssize_t;
#define PY_SSIZE_T_MAX INT_MAX
#define PY_SSIZE_T_MIN INT_MIN
#endif
static PyObject *_mysql_MySQLError;
static PyObject *_mysql_Warning;
static PyObject *_mysql_Error;
static PyObject *_mysql_DatabaseError;
static PyObject *_mysql_InterfaceError;
static PyObject *_mysql_DataError;
static PyObject *_mysql_OperationalError;
static PyObject *_mysql_IntegrityError;
static PyObject *_mysql_InternalError;
static PyObject *_mysql_ProgrammingError;
static PyObject *_mysql_NotSupportedError;
typedef struct {
PyObject_HEAD
MYSQL connection;
int open;
PyObject *converter;
} _mysql_ConnectionObject;
#define check_connection(c) if (!(c->open)) return _mysql_Exception(c)
#define result_connection(r) ((_mysql_ConnectionObject *)r->conn)
#define check_result_connection(r) check_connection(result_connection(r))
extern PyTypeObject _mysql_ConnectionObject_Type;
typedef struct {
PyObject_HEAD
PyObject *conn;
MYSQL_RES *result;
int nfields;
int use;
PyObject *converter;
} _mysql_ResultObject;
extern PyTypeObject _mysql_ResultObject_Type;
static int _mysql_server_init_done = 0;
#if MYSQL_VERSION_ID >= 40000
#define check_server_init(x) if (!_mysql_server_init_done) { if (mysql_server_init(0, NULL, NULL)) { _mysql_Exception(NULL); return x; } else { _mysql_server_init_done = 1;} }
#else
#define check_server_init(x) if (!_mysql_server_init_done) _mysql_server_init_done = 1
#endif
PyObject *
_mysql_Exception(_mysql_ConnectionObject *c)
{
PyObject *t, *e;
int merr;
if (!(t = PyTuple_New(2))) return NULL;
if (!_mysql_server_init_done) {
e = _mysql_InternalError;
PyTuple_SET_ITEM(t, 0, PyInt_FromLong(-1L));
PyTuple_SET_ITEM(t, 1, PyString_FromString("server not initialized"));
PyErr_SetObject(e, t);
Py_DECREF(t);
return NULL;
}
merr = mysql_errno(&(c->connection));
if (!merr)
e = _mysql_InterfaceError;
else if (merr > CR_MAX_ERROR) {
PyTuple_SET_ITEM(t, 0, PyInt_FromLong(-1L));
PyTuple_SET_ITEM(t, 1, PyString_FromString("error totally whack"));
PyErr_SetObject(_mysql_InterfaceError, t);
Py_DECREF(t);
return NULL;
}
else switch (merr) {
case CR_COMMANDS_OUT_OF_SYNC:
case ER_DB_CREATE_EXISTS:
case ER_SYNTAX_ERROR:
case ER_PARSE_ERROR:
case ER_NO_SUCH_TABLE:
case ER_WRONG_DB_NAME:
case ER_WRONG_TABLE_NAME:
case ER_FIELD_SPECIFIED_TWICE:
case ER_INVALID_GROUP_FUNC_USE:
case ER_UNSUPPORTED_EXTENSION:
case ER_TABLE_MUST_HAVE_COLUMNS:
#ifdef ER_CANT_DO_THIS_DURING_AN_TRANSACTION
case ER_CANT_DO_THIS_DURING_AN_TRANSACTION:
#endif
e = _mysql_ProgrammingError;
break;
#ifdef WARN_DATA_TRUNCATED
case WARN_DATA_TRUNCATED:
#ifdef WARN_NULL_TO_NOTNULL
case WARN_NULL_TO_NOTNULL:
#endif
#ifdef ER_WARN_DATA_OUT_OF_RANGE
case ER_WARN_DATA_OUT_OF_RANGE:
#endif
#ifdef ER_NO_DEFAULT
case ER_NO_DEFAULT:
#endif
#ifdef ER_PRIMARY_CANT_HAVE_NULL
case ER_PRIMARY_CANT_HAVE_NULL:
#endif
#ifdef ER_DATA_TOO_LONG
case ER_DATA_TOO_LONG:
#endif
#ifdef ER_DATETIME_FUNCTION_OVERFLOW
case ER_DATETIME_FUNCTION_OVERFLOW:
#endif
e = _mysql_DataError;
break;
#endif
case ER_DUP_ENTRY:
#ifdef ER_DUP_UNIQUE
case ER_DUP_UNIQUE:
#endif
#ifdef ER_NO_REFERENCED_ROW
case ER_NO_REFERENCED_ROW:
#endif
#ifdef ER_NO_REFERENCED_ROW_2
case ER_NO_REFERENCED_ROW_2:
#endif
#ifdef ER_ROW_IS_REFERENCED
case ER_ROW_IS_REFERENCED:
#endif
#ifdef ER_ROW_IS_REFERENCED_2
case ER_ROW_IS_REFERENCED_2:
#endif
#ifdef ER_CANNOT_ADD_FOREIGN
case ER_CANNOT_ADD_FOREIGN:
#endif
e = _mysql_IntegrityError;
break;
#ifdef ER_WARNING_NOT_COMPLETE_ROLLBACK
case ER_WARNING_NOT_COMPLETE_ROLLBACK:
#endif
#ifdef ER_NOT_SUPPORTED_YET
case ER_NOT_SUPPORTED_YET:
#endif
#ifdef ER_FEATURE_DISABLED
case ER_FEATURE_DISABLED:
#endif
#ifdef ER_UNKNOWN_STORAGE_ENGINE
case ER_UNKNOWN_STORAGE_ENGINE:
#endif
e = _mysql_NotSupportedError;
break;
default:
if (merr < 1000)
e = _mysql_InternalError;
else
e = _mysql_OperationalError;
break;
}
PyTuple_SET_ITEM(t, 0, PyInt_FromLong((long)merr));
PyTuple_SET_ITEM(t, 1, PyString_FromString(mysql_error(&(c->connection))));
PyErr_SetObject(e, t);
Py_DECREF(t);
return NULL;
}
static char _mysql_server_init__doc__[] =
"Initialize embedded server. If this client is not linked against\n\
the embedded server library, this function does nothing.\n\
\n\
args -- sequence of command-line arguments\n\
groups -- sequence of groups to use in defaults files\n\
";
static PyObject *_mysql_server_init(
PyObject *self,
PyObject *args,
PyObject *kwargs) {
static char *kwlist[] = {"args", "groups", NULL};
char **cmd_args_c=NULL, **groups_c=NULL, *s;
int cmd_argc=0, i, groupc;
PyObject *cmd_args=NULL, *groups=NULL, *ret=NULL, *item;
if (_mysql_server_init_done) {
PyErr_SetString(_mysql_ProgrammingError,
"already initialized");
return NULL;
}
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|OO", kwlist,
&cmd_args, &groups))
return NULL;
#if MYSQL_VERSION_ID >= 40000
if (cmd_args) {
if (!PySequence_Check(cmd_args)) {
PyErr_SetString(PyExc_TypeError,
"args must be a sequence");
goto finish;
}
cmd_argc = PySequence_Size(cmd_args);
if (cmd_argc == -1) {
PyErr_SetString(PyExc_TypeError,
"args could not be sized");
goto finish;
}
cmd_args_c = (char **) PyMem_Malloc(cmd_argc*sizeof(char *));
for (i=0; i< cmd_argc; i++) {
item = PySequence_GetItem(cmd_args, i);
s = PyString_AsString(item);
Py_DECREF(item);
if (!s) {
PyErr_SetString(PyExc_TypeError,
"args must contain strings");
goto finish;
}
cmd_args_c[i] = s;
}
}
if (groups) {
if (!PySequence_Check(groups)) {
PyErr_SetString(PyExc_TypeError,
"groups must be a sequence");
goto finish;
}
groupc = PySequence_Size(groups);
if (groupc == -1) {
PyErr_SetString(PyExc_TypeError,
"groups could not be sized");
goto finish;
}
groups_c = (char **) PyMem_Mallo