⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 _mysql.c

📁 python联接mysql驱动 python联接mysql驱动
💻 C
📖 第 1 页 / 共 5 页
字号:
/*This program is free software; you can redistribute it and/or modifyit under the terms of the GNU General Public License as published bythe Free Software Foundation; either version 2, or (at your option)any later version. Alternatively, you may use the original licensereproduced below.Copyright 1999 by Comstar.net, Inc., Atlanta, GA, US.                        All Rights ReservedPermission to use, copy, modify, and distribute this software and itsdocumentation for any purpose and without fee is hereby granted,provided that the above copyright notice appear in all copies and thatboth that copyright notice and this permission notice appear insupporting documentation, and that the name of Comstar.net, Inc.or COMSTAR not be used in advertising or publicity pertaining todistribution 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 NOEVENT SHALL COMSTAR BE LIABLE FOR ANY SPECIAL, INDIRECT ORCONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OFUSE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OROTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE ORPERFORMANCE OF THIS SOFTWARE.*/#include "pymemcompat.h"#include "structmember.h"#if defined(_WIN32) || defined(_WIN64) || defined(__WIN32__) || defined(WIN32) || defined(MS_WIN32)#include <windows.h>#include <config-win.h>#else#include "my_config.h"#endif#ifndef uint#define uint unsigned int#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#endifstatic 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#endifPyObject *_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_Malloc((1+groupc)*sizeof(char *));		for (i=0; i< groupc; i++) {			item = PySequence_GetItem(groups, i);			s = PyString_AsString(item);			Py_DECREF(item);			if (!s) {				PyErr_SetString(PyExc_TypeError,						"groups must contain strings");				goto finish;			}			groups_c[i] = s;		}		groups_c[groupc] = (char *)NULL;	}	/* even though this may block, don't give up the interpreter lock	   so that the server can't be initialized multiple times. */	if (mysql_server_init(cmd_argc, cmd_args_c, groups_c)) {		_mysql_Exception(NULL);		goto finish;	}#endif	ret = Py_None;	Py_INCREF(Py_None);	_mysql_server_init_done = 1;  finish:	PyMem_Free(groups_c);	PyMem_Free(cmd_args_c);	return ret;}static char _mysql_server_end__doc__[] ="Shut down embedded server. If not using an embedded server, this\n\does nothing.";static PyObject *_mysql_server_end(	PyObject *self,	PyObject *args) {	if (_mysql_server_init_done) {#if MYSQL_VERSION_ID >= 40000		mysql_server_end();#endif		_mysql_server_init_done = 0;		Py_INCREF(Py_None);		return Py_None;	}	return _mysql_Exception(NULL);}	 #if MYSQL_VERSION_ID >= 32314static char _mysql_thread_safe__doc__[] ="Indicates whether the client is compiled as thread-safe.";static PyObject *_mysql_thread_safe(	PyObject *self,	PyObject *args) {	PyObject *flag;	if (!PyArg_ParseTuple(args, "")) return NULL;	check_server_init(NULL);	if (!(flag=PyInt_FromLong((long)mysql_thread_safe()))) return NULL;	return flag;}#endifstatic char _mysql_ResultObject__doc__[] ="result(connection, use=0, converter={}) -- Result set from a query.\n\\n\Creating instances of this class directly is an excellent way to\n\shoot yourself in the foot. If using _mysql.connection directly,\n\use connection.store_result() or connection.use_result() instead.\n\If using MySQLdb.Connection, this is done by the cursor class.\n\Just forget you ever saw this. Forget... FOR-GET...";static int_mysql_ResultObject_Initialize(	_mysql_ResultObject *self,	PyObject *args,	PyObject *kwargs){	static char *kwlist[] = {"connection", "use", "converter", NULL};	MYSQL_RES *result; 	_mysql_ConnectionObject *conn=NULL;	int use=0; 	PyObject *conv=NULL;	int n, i;	MYSQL_FIELD *fields;	if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|iO", kwlist,					  &conn, &use, &conv))		return -1;	if (!conv) conv = PyDict_New();	if (!conv) return -1;	self->conn = (PyObject *) conn;	Py_INCREF(conn);	self->use = use;	Py_BEGIN_ALLOW_THREADS ;	if (use)		result = mysql_use_result(&(conn->connection));	else		result = mysql_store_result(&(conn->connection));	self->result = result;	Py_END_ALLOW_THREADS ;	if (!result) {		self->converter = PyTuple_New(0);		return 0;	}	n = mysql_num_fields(result);	self->nfields = n;	if (!(self->converter = PyTuple_New(n))) return -1;	fields = mysql_fetch_fields(result);	for (i=0; i<n; i++) {		PyObject *tmp, *fun;		tmp = PyInt_FromLong((long) fields[i].type);		if (!tmp) return -1;		fun = PyObject_GetItem(conv, tmp);		Py_DECREF(tmp);		if (!fun) {			PyErr_Clear();			fun = Py_None;			Py_INCREF(Py_None);		}		if (PySequence_Check(fun)) {			int j, n2=PySequence_Size(fun);			PyObject *fun2=NULL;			for (j=0; j<n2; j++) {				PyObject *t = PySequence_GetItem(fun, j);				if (!t) continue;				if (!PyTuple_Check(t)) goto cleanup;				if (PyTuple_GET_SIZE(t) == 2) {					long mask;					PyObject *pmask=NULL;					pmask = PyTuple_GET_ITEM(t, 0);					fun2 = PyTuple_GET_ITEM(t, 1);					if (PyInt_Check(pmask)) {						mask = PyInt_AS_LONG(pmask);						if (mask & fields[i].flags) {							break;						}						else {							continue;						}					} else {						break;					}				}			  cleanup:				Py_DECREF(t);			}			if (!fun2) fun2 = Py_None;			Py_INCREF(fun2);			Py_DECREF(fun);			fun = fun2;		}		PyTuple_SET_ITEM(self->converter, i, fun);	}	return 0;}#if PY_VERSION_HEX >= 0x02020000static int _mysql_ResultObject_traverse(	_mysql_ResultObject *self,	visitproc visit,	void *arg){	int r;	if (self->converter) {		if (!(r = visit(self->converter, arg))) return r;	}	if (self->conn)		return visit(self->conn, arg);	return 0;}#endifstatic int _mysql_ResultObject_clear(	_mysql_ResultObject *self){	Py_XDECREF(self->converter);	self->converter = NULL;	Py_XDECREF(self->conn);	self->conn = NULL;	return 0;}static int_mysql_ConnectionObject_Initialize(	_mysql_ConnectionObject *self,	PyObject *args,	PyObject *kwargs){	MYSQL *conn = NULL;	PyObject *conv = NULL;	PyObject *ssl = NULL;#if HAVE_OPENSSL	char *key = NULL, *cert = NULL, *ca = NULL,		*capath = NULL, *cipher = NULL;#endif	char *host = NULL, *user = NULL, *passwd = NULL,		*db = NULL, *unix_socket = NULL;	uint port = MYSQL_PORT;	uint client_flag = 0;	static char *kwlist[] = { "host", "user", "passwd", "db", "port",				  "unix_socket", "conv",				  "connect_timeout", "compress",				  "named_pipe", "init_command",				  "read_default_file", "read_default_group",				  "client_flag", "ssl",				  "local_infile",				  NULL } ;	int connect_timeout = 0;	int compress = -1, named_pipe = -1, local_infile = -1;	char *init_command=NULL,	     *read_default_file=NULL,	     *read_default_group=NULL;		self->converter = NULL;	self->open = 0;	check_server_init(-1);	if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ssssisOiiisssiOi:connect",					 kwlist,					 &host, &user, &passwd, &db,					 &port, &unix_socket, &conv,					 &connect_timeout,					 &compress, &named_pipe,					 &init_command, &read_default_file,					 &read_default_group,

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -