comparison src/fields.c @ 55:e606fd52e866 MySQLdb

make things a little cleaner by moving to a src directory for the C code
author kylev
date Fri, 27 Feb 2009 19:14:09 +0000
parents _mysql_fields.c@28e9be1ca559
children 1e1e24fddc74
comparison
equal deleted inserted replaced
54:6e31278d3433 55:e606fd52e866
1 /* -*- mode: C; indent-tabs-mode: t; c-basic-offset: 8; -*- */
2
3 #include "mysqlmod.h"
4
5 static char _mysql_FieldObject__doc__[] =
6 "";
7
8 int
9 _mysql_FieldObject_Initialize(
10 _mysql_FieldObject *self,
11 PyObject *args,
12 PyObject *kwargs)
13 {
14 static char *kwlist[] = {"result", "index", NULL};
15 _mysql_ResultObject *result=NULL;
16 MYSQL_FIELD *field;
17 unsigned int index;
18 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "Oi", kwlist,
19 &result, &index))
20 return -1;
21
22 self->index = index;
23 field = mysql_fetch_field_direct(result->result, index);
24 if (!field) return -1;
25 self->field = *field;
26 self->result = (PyObject *) result;
27 Py_INCREF(result);
28 return 0;
29 }
30
31 static int
32 _mysql_FieldObject_traverse(
33 _mysql_FieldObject *self,
34 visitproc visit,
35 void *arg)
36 {
37 if (self->result)
38 return visit(self->result, arg);
39 return 0;
40 }
41
42 static int
43 _mysql_FieldObject_clear(
44 _mysql_FieldObject *self)
45 {
46 Py_XDECREF(self->result);
47 self->result = NULL;
48 return 0;
49 }
50
51 static void
52 _mysql_FieldObject_dealloc(
53 _mysql_FieldObject *self)
54 {
55 PyObject_GC_UnTrack((PyObject *)self);
56 _mysql_FieldObject_clear(self);
57 MyFree(self);
58 }
59
60 static PyObject *
61 _mysql_FieldObject_repr(
62 _mysql_FieldObject *self)
63 {
64 char buf[300];
65 snprintf(buf, 300, "<_mysql.field object at %lx>", (long)self);
66 return PyString_FromString(buf);
67 }
68
69 static PyMethodDef _mysql_FieldObject_methods[] = {
70 {NULL, NULL} /* sentinel */
71 };
72
73 static struct PyMemberDef _mysql_FieldObject_memberlist[] = {
74 {
75 "result",
76 T_OBJECT,
77 offsetof(_mysql_FieldObject, result),
78 RO,
79 "Result set"
80 },
81 {
82 "name",
83 T_STRING,
84 offsetof(_mysql_FieldObject, field.name),
85 RO,
86 "The name of the field. If the field was given\n\
87 an alias with an AS clause, the value of name is the alias."
88 },
89 {
90 "org_name",
91 T_STRING,
92 offsetof(_mysql_FieldObject, field.org_name),
93 RO,
94 "The name of the field. Aliases are ignored."
95 },
96 {
97 "table",
98 T_STRING,
99 offsetof(_mysql_FieldObject, field.table),
100 RO,
101 "The name of the table containing this field,\n\
102 if it isn't a calculated field. For calculated fields,\n\
103 the table value is an empty string. If the column is selected from a view,\n\
104 table names the view. If the table or view was given an alias with an AS clause,\n\
105 the value of table is the alias.\n"
106 },
107 {
108 "org_table",
109 T_STRING,
110 offsetof(_mysql_FieldObject, field.org_table),
111 RO,
112 "The name of the table. Aliases are ignored.\n\
113 If the column is selected from a view, org_table names the underlying table.\n"
114 },
115 {
116 "db",
117 T_STRING,
118 offsetof(_mysql_FieldObject, field.db),
119 RO,
120 "The name of the database that the field comes from.\n\
121 If the field is a calculated field, db is an empty string."
122 },
123 {
124 "catalog",
125 T_STRING,
126 offsetof(_mysql_FieldObject, field.catalog),
127 RO,
128 "The catalog name. This value is always \"def\"."
129 },
130 {
131 "length",
132 T_ULONG,
133 offsetof(_mysql_FieldObject, field.length),
134 RO,
135 "The width of the field.\n\
136 as specified in the table definition.\n"
137 },
138 {
139 "max_length",
140 T_ULONG,
141 offsetof(_mysql_FieldObject, field.max_length),
142 RO,
143 "The maximum width of the field for the result set\n\
144 (the length of the longest field value for the rows actually in the\n\
145 result set). If you use conn.store_result(), this contains the\n\
146 maximum length for the field. If you use conn.use_result(),\n\
147 the value of this variable is zero.\n"
148 },
149 {
150 "decimals",
151 T_UINT,
152 offsetof(_mysql_FieldObject, field.decimals),
153 RO,
154 "The number of decimals for numeric fields.\n"
155 },
156 {
157 "charsetnr",
158 T_UINT,
159 offsetof(_mysql_FieldObject, field.charsetnr),
160 RO,
161 "The character set number for the field."
162 },
163 {
164 "flags",
165 T_UINT,
166 offsetof(_mysql_FieldObject, field.flags),
167 RO,
168 "Different bit-flags for the field.\n\
169 The bits are enumerated in MySQLdb.constants.FLAG.\n\
170 The flags value may have zero or more of these bits set.\n"
171 },
172 {
173 "type",
174 T_UINT,
175 offsetof(_mysql_FieldObject, field.type),
176 RO,
177 "The type of the field. The type values\n\
178 are enumerated in MySQLdb.constants.FIELD_TYPE.\n"
179 },
180 {NULL} /* Sentinel */
181 };
182
183 static PyObject *
184 _mysql_FieldObject_getattr(
185 _mysql_FieldObject *self,
186 char *name)
187 {
188 PyObject *res;
189 struct PyMemberDef *l;
190
191 res = Py_FindMethod(_mysql_FieldObject_methods, (PyObject *)self, name);
192 if (res != NULL)
193 return res;
194 PyErr_Clear();
195
196 for (l = _mysql_FieldObject_memberlist; l->name != NULL; l++) {
197 if (strcmp(l->name, name) == 0)
198 return PyMember_GetOne((char *)self, l);
199 }
200
201 PyErr_SetString(PyExc_AttributeError, name);
202 return NULL;
203 }
204
205 static int
206 _mysql_FieldObject_setattr(
207 _mysql_FieldObject *self,
208 char *name,
209 PyObject *v)
210 {
211 struct PyMemberDef *l;
212
213 if (v == NULL) {
214 PyErr_SetString(PyExc_AttributeError,
215 "can't delete attributes");
216 return -1;
217 }
218
219
220 for (l = _mysql_FieldObject_memberlist; l->name != NULL; l++)
221 if (strcmp(l->name, name) == 0)
222 return PyMember_SetOne((char *)self, l, v);
223
224 PyErr_SetString(PyExc_AttributeError, name);
225 return -1;
226 }
227
228 PyTypeObject _mysql_FieldObject_Type = {
229 PyObject_HEAD_INIT(NULL)
230 0,
231 "_mysql.field",
232 sizeof(_mysql_FieldObject),
233 0,
234 (destructor)_mysql_FieldObject_dealloc, /* tp_dealloc */
235 0, /*tp_print*/
236 (getattrfunc)_mysql_FieldObject_getattr, /* tp_getattr */
237 (setattrfunc)_mysql_FieldObject_setattr, /* tp_setattr */
238 0, /*tp_compare*/
239 (reprfunc)_mysql_FieldObject_repr, /* tp_repr */
240
241 /* Method suites for standard classes */
242
243 0, /* (PyNumberMethods *) tp_as_number */
244 0, /* (PySequenceMethods *) tp_as_sequence */
245 0, /* (PyMappingMethods *) tp_as_mapping */
246
247 /* More standard operations (here for binary compatibility) */
248
249 0, /* (hashfunc) tp_hash */
250 0, /* (ternaryfunc) tp_call */
251 0, /* (reprfunc) tp_str */
252 0, /* (getattrofunc) tp_getattro */
253 0, /* (setattrofunc) tp_setattro */
254
255 /* Functions to access object as input/output buffer */
256 0, /* (PyBufferProcs *) tp_as_buffer */
257
258 /* Flags to define presence of optional/expanded features */
259 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_BASETYPE,
260
261 _mysql_FieldObject__doc__, /* (char *) tp_doc Documentation string */
262
263 /* call function for all accessible objects */
264 (traverseproc)_mysql_FieldObject_traverse, /* tp_traverse */
265
266 /* delete references to contained objects */
267 (inquiry)_mysql_FieldObject_clear, /* tp_clear */
268
269 /* rich comparisons */
270 0, /* (richcmpfunc) tp_richcompare */
271
272 /* weak reference enabler */
273 0, /* (long) tp_weaklistoffset */
274
275 /* Iterators */
276 0, /* (getiterfunc) tp_iter */
277 0, /* (iternextfunc) tp_iternext */
278
279 /* Attribute descriptor and subclassing stuff */
280 (struct PyMethodDef *)_mysql_FieldObject_methods, /* tp_methods */
281 (struct PyMemberDef *)_mysql_FieldObject_memberlist, /*tp_members */
282 0, /* (struct getsetlist *) tp_getset; */
283 0, /* (struct _typeobject *) tp_base; */
284 0, /* (PyObject *) tp_dict */
285 0, /* (descrgetfunc) tp_descr_get */
286 0, /* (descrsetfunc) tp_descr_set */
287 0, /* (long) tp_dictoffset */
288 (initproc)_mysql_FieldObject_Initialize, /* tp_init */
289 NULL, /* tp_alloc */
290 NULL, /* tp_new */
291 NULL, /* tp_free Low-level free-memory routine */
292 0, /* (PyObject *) tp_bases */
293 0, /* (PyObject *) tp_mro method resolution order */
294 0, /* (PyObject *) tp_defined */
295 };