|
| 1 | +/* vim:expandtab:shiftwidth=2:tabstop=2:smarttab: |
| 2 | + * Copyright 2014 Hewlett-Packard Development Company, L.P. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 5 | + * not use this file except in compliance with the License. You may obtain |
| 6 | + * a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | + * License for the specific language governing permissions and limitations |
| 14 | + * under the License. |
| 15 | + * |
| 16 | + */ |
| 17 | + |
| 18 | +#include "module.h" |
| 19 | + |
| 20 | +static PyMethodDef _attachsql_ConnectionObject_methods[]= { |
| 21 | + { |
| 22 | + "connection_id", |
| 23 | + (PyCFunction)_attachsql_ConnectionObject_connection_id, |
| 24 | + METH_NOARGS, |
| 25 | + "Gets the current connection ID" |
| 26 | + }, |
| 27 | + {NULL, NULL} |
| 28 | +}; |
| 29 | + |
| 30 | +/* |
| 31 | +static struct PyMemberDef _attachsql_ConnectionObject_memberlist[]= { |
| 32 | + {NULL} |
| 33 | +}; |
| 34 | +*/ |
| 35 | + |
| 36 | +int _attachsql_ConnectionObject_Initialize(_attachsql_ConnectionObject *self, PyObject *args, PyObject *kwargs) |
| 37 | +{ |
| 38 | + char *host= NULL, *user= NULL, *pass= NULL, *db= NULL; |
| 39 | + in_port_t port; |
| 40 | + static char *kwlist[]= {"host", "user", "pass", "db", "port", NULL}; |
| 41 | + attachsql_error_t *error= NULL; |
| 42 | + |
| 43 | + if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ssssi:connect", |
| 44 | + kwlist, |
| 45 | + &host, &user, &pass, &db, &port |
| 46 | + )) |
| 47 | + { |
| 48 | + return -1; |
| 49 | + } |
| 50 | + self->conn= attachsql_connect_create(host, port, user, pass, db, &error); |
| 51 | + if (!self->conn) |
| 52 | + { |
| 53 | + _attachsql_Exception(error); |
| 54 | + return -1; |
| 55 | + } |
| 56 | + return 0; |
| 57 | +} |
| 58 | + |
| 59 | +PyObject *_attachsql_ConnectionObject_connection_id(_attachsql_ConnectionObject *self, PyObject *unused) |
| 60 | +{ |
| 61 | + uint32_t conn_id; |
| 62 | + |
| 63 | + Py_BEGIN_ALLOW_THREADS |
| 64 | + conn_id= attachsql_connect_get_connection_id(self->conn); |
| 65 | + Py_END_ALLOW_THREADS |
| 66 | + return PyInt_FromLong((long)conn_id); |
| 67 | +} |
| 68 | + |
| 69 | +void _attachsql_ConnectionObject_dealloc(_attachsql_ConnectionObject *self) |
| 70 | +{ |
| 71 | + PyObject_GC_UnTrack(self); |
| 72 | + attachsql_connect_destroy(self->conn); |
| 73 | + self->ob_type->tp_free(self); |
| 74 | +} |
| 75 | + |
| 76 | +PyObject *_attachsql_ConnectionObject_getattr(_attachsql_ConnectionObject *self, char *name) |
| 77 | +{ |
| 78 | + PyObject *res; |
| 79 | + struct PyMemberDef *def; |
| 80 | + |
| 81 | + res= Py_FindMethod(_attachsql_ConnectionObject_methods, (PyObject *)self, name); |
| 82 | + if (res != NULL) |
| 83 | + { |
| 84 | + return res; |
| 85 | + } |
| 86 | + PyErr_SetString(PyExc_AttributeError, name); |
| 87 | + return NULL; |
| 88 | +} |
| 89 | + |
| 90 | +int _attachsql_ConnectionObject_setattr(_attachsql_ConnectionObject *self, char *name, PyObject *v) |
| 91 | +{ |
| 92 | + struct PyMemberDef *def; |
| 93 | + |
| 94 | + if (v == NULL) |
| 95 | + { |
| 96 | + PyErr_SetString(PyExc_AttributeError, "can't set attribute"); |
| 97 | + return -1; |
| 98 | + } |
| 99 | + /* No members yet */ |
| 100 | + /* |
| 101 | + for (def= _attachsql_ConnectionObject_memberlist; def->name != NULL; def++) |
| 102 | + { |
| 103 | + if (strcmp(def->name, name) == 0) |
| 104 | + { |
| 105 | + return PyMember_SetOne((char *)self, def, v); |
| 106 | + } |
| 107 | + } |
| 108 | + */ |
| 109 | + PyErr_SetString(PyExc_AttributeError, name); |
| 110 | + return -1; |
| 111 | +} |
| 112 | + |
| 113 | +PyObject *_attachsql_ConnectionObject_repr(_attachsql_ConnectionObject *self) |
| 114 | +{ |
| 115 | + char buffer[300]; |
| 116 | + /* TODO: store the host name / port for this */ |
| 117 | + //snprintf(buffer, 300, "<attachsql.connection to '%.256s' port %d at %lx>", self->conn->con->host, self->conn->con->port); |
| 118 | + snprintf(buffer, 300, "<attachsql.connection at %lx>", (long)self); |
| 119 | + return PyString_FromString(buffer); |
| 120 | +} |
| 121 | + |
| 122 | +int _attachsql_ConnectionObject_traverse(_attachsql_ConnectionObject *self, visitproc visit, void *arg) |
| 123 | +{ |
| 124 | + return 0; |
| 125 | +} |
| 126 | + |
| 127 | +int _attachsql_ConnectionObject_clear(_attachsql_ConnectionObject *self) |
| 128 | +{ |
| 129 | + return 0; |
| 130 | +} |
| 131 | + |
| 132 | +PyTypeObject _attachsql_ConnectionObject_Type= { |
| 133 | + PyObject_HEAD_INIT(NULL) |
| 134 | + 0, |
| 135 | + "attachsql.connection", |
| 136 | + sizeof(_attachsql_ConnectionObject), |
| 137 | + 0, |
| 138 | + (destructor)_attachsql_ConnectionObject_dealloc, |
| 139 | + 0, |
| 140 | + (getattrfunc)_attachsql_ConnectionObject_getattr, |
| 141 | + (setattrfunc)_attachsql_ConnectionObject_setattr, |
| 142 | + 0, |
| 143 | + (reprfunc)_attachsql_ConnectionObject_repr, |
| 144 | + |
| 145 | + 0, |
| 146 | + 0, |
| 147 | + 0, |
| 148 | + |
| 149 | + 0, |
| 150 | + 0, |
| 151 | + 0, |
| 152 | + 0, |
| 153 | + 0, |
| 154 | + |
| 155 | + 0, |
| 156 | + |
| 157 | + Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_BASETYPE, |
| 158 | + "Returns an AttachSQL connection object", |
| 159 | + (traverseproc)_attachsql_ConnectionObject_traverse, |
| 160 | + (inquiry)_attachsql_ConnectionObject_clear, |
| 161 | + |
| 162 | + 0, |
| 163 | + |
| 164 | + 0, |
| 165 | + |
| 166 | + 0, |
| 167 | + 0, |
| 168 | + |
| 169 | + (struct PyMethodDef *)_attachsql_ConnectionObject_methods, |
| 170 | + //(struct PyMemberDef *)_attachsql_ConnectionObject_memberlist, |
| 171 | + 0, |
| 172 | + |
| 173 | + 0, |
| 174 | + 0, |
| 175 | + 0, |
| 176 | + 0, |
| 177 | + 0, |
| 178 | + 0, |
| 179 | + (initproc)_attachsql_ConnectionObject_Initialize, |
| 180 | + NULL, |
| 181 | + NULL, |
| 182 | + NULL, |
| 183 | + 0, |
| 184 | + 0, |
| 185 | + 0 |
| 186 | +}; |
0 commit comments