?? mysql.c
字號(hào):
/* Copyright (C) 2008, 2009 Bozhin Zafirov 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 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>.*/#include <Python.h>#include <structmember.h>/* includes */#include <mysql/mysql.h>#define refprint(x) printf("rc: %d\n", (int)((PyObject *)(x)->ob_refcnt))/* types */typedef enum {false=0, true=1} bool;#define RETURN_PYNONE \ Py_INCREF(Py_None); \ return (Py_None);#define RETURN_PYTRUE \ Py_INCREF(Py_True); \ return (Py_True);#define RETURN_PYFALSE \ Py_INCREF(Py_False); \ return (Py_False);#define raise_error(s) \ PyErr_SetString(MySQLError, s); \ return NULL; \#define RETURN_BOOL(x) \ if (x) \ { \ RETURN_PYFALSE \ } else { \ RETURN_PYTRUE \ }#define _m_member(name, type, offset, flags, docstr) \ {name, type, offsetof(MySQLconn, offset), flags, docstr}/* function definitions */#define _r_header(name) \static PyObject *name(MySQLres *r, PyObject *args, PyObject *kwds)#define _r_header_noargs(name) \static PyObject *name(MySQLres *r, PyObject *unused)/* result function definition */#define _r_func(name, body) \_r_header(name) \{ \ body \}#define _m_header(name) \static PyObject *name(MySQLconn *m, PyObject *args, PyObject *kwds)#define _m_header_noargs(name) \static PyObject *name(MySQLconn *m, PyObject *unused)#define _m_func(name, body) \_m_header(name) \{ \ body \}#define _m_funcbool(name, cond) \_m_func(name, RETURN_BOOL(cond))#define _m_funcboolparam(name, ptype, pname, type, cond) \_m_func(name, \ ptype pname; \ if (!PyArg_ParseTuple(args, type, &pname)) return NULL; \ RETURN_BOOL(cond) \)/* forward declarations */static PyTypeObject MySQLResObjectType;static PyTypeObject MySQLIterObjectType;/* error object */static PyObject *MySQLError;/* object type definition */typedef struct { PyObject_HEAD /* MySQL properties */ MYSQL *mysql; /* use dictionary results */ bool use_dict; /* informational properties */ char *host; long port; char *user; char *pass; char *name; /* other properties */ /* Returns a string that represents the client library version */ char *_client_info; /* Returns an integer that represents the client library version. */ char *_client_version; /* Returns a string that represents the server version number. */ char *_server_info; /* Returns the version number of the server as an integer. */ int _server_version; /* Returns a string describing the type of connection in use, including the server hostname. */ char *_host_info; /* Returns the protocol version used by current connection. */ int _proto_info; /* Return default character set name for the current connection */ char *_character_set_name; /* Returns the SSL cipher used for the given connection to the server */ char *_ssl_cipher; /* Returns the error code for the most recently invoked API function that can succeed or fail */ int _errno; /* Returns a string containing the error message for the most recently invoked API function that failed */ char *_error; /* get number of rows fetched by last select query */ long _affected_rows; /* Returns the value generated for an AUTO_INCREMENT column by the previous INSERT or UPDATE statement */ long _insert_id; /* Returns a null-terminated string containing the SQLSTATE error code for the most recently executed SQL statement. */ char *_sqlstate; /* Retrieves a string providing information about the most recently executed statement, but only for the statements listed here. */ char *_info;} MySQLconn;/* result object definition */typedef struct { PyObject_HEAD /* result properties */ MYSQL *mysql; PyObject *fields; PyObject *data; /* other properties */ int field_count; int num_fields; int num_rows; /* name of result object */ PyObject *query;} MySQLres;/* result iterator object definition */typedef struct { PyObject_HEAD Py_ssize_t index; MySQLres *r;} MySQLresIter;/* mysql table map */typedef struct { PyObject_HEAD } MySQLmap;/* ***************************** METHODS ***************************** *//* class constructor */static PyObject *m_new(PyTypeObject *type, PyObject *args, PyObject *kwds){ /* object variable */ MySQLconn *m = (MySQLconn *)type->tp_alloc(type, 0); if (m == NULL) return NULL; /* parse arguments */ static char *kwlist[] = {"username", "password", "database", "hostname", "port", NULL}; if (!PyArg_ParseTupleAndKeywords(args, kwds, "sss|sdO", kwlist, &m->user, &m->pass, &m->name, &m->host, &m->port)) return NULL; /* allocate memory */ m->mysql = mysql_init(NULL); /* connect to MySQL server */ if (mysql_real_connect(m->mysql, m->host, m->user, m->pass, m->name, m->port, NULL, 0) == NULL) { Py_DECREF(m); raise_error(mysql_error(m->mysql)) } /* set up other properties */ m->_client_info = (char *)mysql_get_client_info(); m->_client_version = (char *)mysql_get_client_version(); m->_server_info = (char *)mysql_get_server_info(m->mysql); m->_server_version = mysql_get_server_version(m->mysql); m->_host_info = (char *)mysql_get_host_info(m->mysql); m->_proto_info = mysql_get_proto_info(m->mysql); m->_character_set_name = (char *)mysql_character_set_name(m->mysql); m->_ssl_cipher = (char *)mysql_get_ssl_cipher(m->mysql); m->use_dict = false; /* return new object */ return (PyObject *)m;}/* class destructor */static void m_dealloc(MySQLconn *m){ mysql_close(m->mysql); Py_TYPE(m)->tp_free((PyObject *)m);}/* mysql object representation */static PyObject *m_repr(MySQLconn *m){ char *host = m->host; if (host == NULL) host = "localhost"; return PyUnicode_FromFormat("<MySQL :: %s>", host);}/* Sets autocommit mode on if mode is 1, off if mode is 0 */PyDoc_STRVAR(_mdoc_autocommit,"autocommit(mode) -> set autocommit mode on or off\n\\n\If mode is True, turn autocommit mode on. Otherwise turn it off.\n\Returns status of autocommit mode.");_m_funcboolparam(m_autocommit, int, mode, "i", mysql_autocommit(m->mysql, mode))/* Change user and current database */PyDoc_STRVAR(_mdoc_change_user,"change_user(username, password, dbname) -> change current user and database\n\\n\Changes the user and causes the database specified by dbname to become the default (current) database.\n\In subsequent queries, the database is the default for table references that do not include explicit\n\database specifier.\n\Returns True on success or False on fail.");_m_header(m_change_user){ const char *user, *pass, *name; /* parse arguments */ if (!PyArg_ParseTuple(args, "sss", &user, &pass, &name)) return NULL; RETURN_BOOL(mysql_change_user(m->mysql, user, pass, name))}/* Commits the current transaction */PyDoc_STRVAR(_mdoc_commit,"commit() -> commits the current transaction\n\\n\Returns True on success or False on fail.");_m_funcbool(m_commit, mysql_commit(m->mysql))/* Creates the database named by given parameter */PyDoc_STRVAR(_mdoc_create_db,"create_db(dbname) -> creates the database specified by dbname\n\\n\Returns True on success or False on fail.");_m_header(m_create_db){ PyObject *query; const char *q; /* parse arguments */ if (!PyArg_ParseTuple(args, "s", &q)) return NULL; query = PyBytes_FromFormat("create database %s;", q); int r = mysql_real_query(m->mysql, PyBytes_AsString(query), Py_SIZE(query)); Py_DECREF(query); RETURN_BOOL(r);}/* Drops the database named by the given parameter */PyDoc_STRVAR(_mdoc_drop_db,"drop_db(dbname) -> drops the database specified by dbname\n\\n\Returns True on success or False on fail.");_m_header(m_drop_db){ PyObject *query; const char *q; /* parse arguments */ if (!PyArg_ParseTuple(args, "s", &q)) return NULL; query = PyBytes_FromFormat("drop database %s;", q); int r = mysql_real_query(m->mysql, PyBytes_AsString(query), Py_SIZE(query)); Py_DECREF(query); RETURN_BOOL(r);}/* This function is used to create a legal SQL string that you can use in an SQL statement */PyDoc_STRVAR(_mdoc_escape_string,"escape_string(str) -> escape SQL specific characters\n\\n\This function is used to create a legal SQL string that you can use in an SQL statement.\n\The string str is encoded to an escaped SQL string taking into account the current character set.");_m_header(m_escape_string){ PyObject *result; const char *from; int len; char *to; /* parse arguments */ if (!PyArg_ParseTuple(args, "s#", &from, &len)) return NULL; to = (char *)malloc(len*2+1); mysql_real_escape_string(m->mysql, to, from, len); result = Py_BuildValue("s", to); free(to); return result;}/* This function is used to create a legal SQL string that you can use in an SQL statement. */PyDoc_STRVAR(_mdoc_hex_string,"hex_string(str) -> \n\\n\This function is used to create a legal SQL string that you can use in an SQL statement.\n\The string in from is encoded to hexadecimal format, with each character encoded as two hexadecimal digits\n\and the result is returned.");_m_header(m_hex_string){ PyObject *result; const char *from; int len; char *to; /* parse arguments */ if (!PyArg_ParseTuple(args, "s#", &from, &len)) return NULL; to = (char *)malloc(len*2+1); mysql_hex_string(to, from, len); result = PyUnicode_FromFormat("0x%s", to); free(to); return result;}/* Asks the server to kill the thread specified by pid */PyDoc_STRVAR(_mdoc_kill,"kill(pid) -> kill server thread specifeid by pid\n\\n\This function asks the server to kill the thread specified by pid.\n\Returns True on success or False on fail.");_m_funcboolparam(m_kill, int, pid, "i", mysql_kill(m->mysql, pid))/* Returns a result set consisting of database names on the server that match the simple regular expression specified by the wild parameter */PyDoc_STRVAR(_mdoc_list_dbs,"list_dbs(wild) -> list databases\n\\n\Returns a result set consisting of database names on the server that match the simple regular expression\n\specified by the wild parameter. wild may contain the wildcard characters “%” or “_”, or may be omitted\n\to match all databases.");_m_header(m_list_dbs){ const char *wild = NULL; MYSQL_RES *res; /* parse arguments */ if (!PyArg_ParseTuple(args, "|s*", &wild)) return NULL; res = mysql_list_dbs(m->mysql, wild); if (res != NULL) { int i; PyObject *result = PyTuple_New(mysql_num_rows(res)); MYSQL_ROW row; /* prepare list of databases */ for (i=0; i<mysql_num_rows(res); i++) {
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -