Add basic connection class template · libattachsql/pyattachsql@ca5bb10 · GitHub
Skip to content

Commit ca5bb10

Browse files
committed
Add basic connection class template
1 parent 806257a commit ca5bb10

5 files changed

Lines changed: 266 additions & 3 deletions

File tree

.gitignore

Lines changed: 2 additions & 2 deletions

setup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
ext_modules=[
1818
Extension("attachsql",
1919
sources=['src/module.c',
20+
'src/connection.c',
2021
'src/util.c'
2122
],
2223
libraries=['attachsql']),

src/connection.c

Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
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+
};

src/module.c

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,44 @@ static PyMethodDef _attachsql_methods[] = {
2323
(PyCFunction)_attachsql_get_library_version,
2424
METH_NOARGS,
2525
"get_library_version() -- Returns a string representing the library version."
26-
}
26+
},
27+
{NULL}
2728
};
2829

30+
PyObject *_attachsql_Exception(attachsql_error_t *error)
31+
{
32+
PyObject *tuple, *err;
33+
int err_code;
34+
if (!(tuple= PyTuple_New(2)))
35+
{
36+
return NULL;
37+
}
38+
if (!error)
39+
{
40+
err= _attachsql_InternalError;
41+
PyTuple_SET_ITEM(tuple, 0, PyInt_FromLong(-1L));
42+
PyTuple_SET_ITEM(tuple, 1, PyString_FromString("Unknown Error"));
43+
PyErr_SetObject(err, tuple);
44+
Py_DECREF(tuple);
45+
return NULL;
46+
}
47+
err_code= attachsql_error_code(error);
48+
if (err_code >= 2000)
49+
{
50+
err= _attachsql_ClientError;
51+
}
52+
else
53+
{
54+
err= _attachsql_ServerError;
55+
}
56+
PyTuple_SET_ITEM(tuple, 0, PyInt_FromLong(err_code));
57+
PyTuple_SET_ITEM(tuple, 1, PyString_FromString(attachsql_error_message(error)));
58+
PyErr_SetObject(err, tuple);
59+
Py_DECREF(tuple);
60+
attachsql_error_free(error);
61+
return NULL;
62+
}
63+
2964
PyMODINIT_FUNC
3065
initattachsql(void)
3166
{
@@ -37,4 +72,19 @@ initattachsql(void)
3772
return;
3873
}
3974
/* TODO: add types */
75+
_attachsql_Error= PyErr_NewException("attachsql.Error", NULL, NULL);
76+
Py_INCREF(_attachsql_Error);
77+
PyModule_AddObject(module, "Error", _attachsql_Error);
78+
79+
_attachsql_InternalError= PyErr_NewException("attachsql.InternalError", NULL, NULL);
80+
Py_INCREF(_attachsql_InternalError);
81+
PyModule_AddObject(module, "InternalError", _attachsql_InternalError);
82+
83+
_attachsql_ClientError= PyErr_NewException("attachsql.ClientError", NULL, NULL);
84+
Py_INCREF(_attachsql_ClientError);
85+
PyModule_AddObject(module, "ClientError", _attachsql_ClientError);
86+
87+
_attachsql_ServerError= PyErr_NewException("attachsql.ServerError", NULL, NULL);
88+
Py_INCREF(_attachsql_ServerError);
89+
PyModule_AddObject(module, "ServerError", _attachsql_ServerError);
4090
}

src/module.h

Lines changed: 26 additions & 0 deletions

0 commit comments

Comments
 (0)