Removed all code that uses OpenGL from Image.
[io/quag.git] / addons / DBI / source / IoDBI.c
blob9bb97c59e7b4e535e5299a9089549d708042c053
1 /*#io
2 DBI ioDoc(
3 docCopyright("Jeremy Cowgar", 2006)
4 docLicense("BSD revised")
5 docCategory("Databases")
6 docObject("DBI")
7 docDescription("An object for wrapping libdbi to allow Io access to
8 a multitude of database systems transparently.")
9 */
11 #include "IoDBI.h"
12 #include "IoDBIConn.h"
13 #include "IoState.h"
14 #include "IoNumber.h"
15 #include "IoList.h"
16 #include "IoMap.h"
18 #define DATA(self) ((IoDBIData *)IoObject_dataPointer(self))
20 void ReportDBIError(dbi_conn conn, void *state, IoMessage *m)
22 const char *error;
23 int errorCode = dbi_conn_error(conn, &error);
25 IoState_error_(state, m, "libdbi: %i: %s\n", errorCode, error);
28 IoTag *IoDBI_newTag(void *state)
30 IoTag *tag = IoTag_newWithName_("DBI");
31 IoTag_state_(tag, state);
32 IoTag_cloneFunc_(tag, (IoTagCloneFunc *)IoDBI_rawClone);
33 IoTag_freeFunc_(tag, (IoTagFreeFunc *)IoDBI_free);
34 //IoTag_markFunc_(tag, (IoTagMarkFunc *)IoDBI_mark);
35 return tag;
38 IoDBI *IoDBI_proto(void *state)
40 IoObject *self = IoObject_new(state);
41 IoObject_tag_(self, IoDBI_newTag(state));
43 IoObject_setDataPointer_(self, calloc(1, sizeof(IoDBIData)));
44 DATA(self)->driverCount = 0;
46 IoState_registerProtoWithFunc_(state, self, IoDBI_proto);
49 IoMethodTable methodTable[] = {
50 {"init", IoDBI_init},
51 {"initWithDriversPath", IoDBI_initWithDriversPath},
52 {"version", IoDBI_version},
53 {"drivers", IoDBI_drivers},
54 {"with", IoDBI_with},
55 {NULL, NULL},
57 IoObject_addMethodTable_(self, methodTable);
60 return self;
63 IoDBI *IoDBI_rawClone(IoDBI *proto)
65 IoDBI *self = IoObject_rawClonePrimitive(proto);
66 IoObject_setDataPointer_(self, calloc(1, sizeof(IoDBIData)));
67 DATA(self)->driverCount = 0;
68 return self;
71 void IoDBI_free(IoDBI *self)
73 dbi_shutdown();
74 free(IoObject_dataPointer(self));
77 //void IoDBI_mark(IoDBI *self) {}
79 /* ---------------------------------------------------------------- */
81 IoObject *IoDBI_version(IoDBI *self, IoObject *locals, IoMessage *m)
83 /*#io
84 docSlot("version", "Return string version of libdbi being used")
86 return IOSYMBOL(dbi_version());
89 IoObject *IoDBI_init(IoDBI *self, IoObject *locals, IoMessage *m)
91 /*#io
92 docSlot("init", "Initialize the DBI environment with the default driver path")
94 DATA(self)->driverCount = dbi_initialize(NULL);
95 if (DATA(self)->driverCount == -1)
97 IoState_error_(IOSTATE, m, "*** IoDBI error during dbi_initialize\n");
99 else
101 DATA(self)->didInit = 1;
104 return IONUMBER(DATA(self)->driverCount);
107 IoObject *IoDBI_initWithDriversPath(IoDBI *self, IoObject *locals,
108 IoMessage *m)
110 /*#io
111 docSlot("initWithDriversPath", "Initialize the DBI environment with the
112 specified libdbi driver path")
114 IoObject *dir = IoMessage_locals_valueArgAt_(m, locals, 0);
116 if (ISSYMBOL(dir))
118 DATA(self)->driverCount = dbi_initialize(CSTRING(dir));
120 else
122 IoState_error_(IOSTATE, m, "argument 0 to method '%s' must be a Symbol, not a '%s'\n",
123 CSTRING(IoMessage_name(m)), IoObject_name(dir));
126 if (DATA(self)->driverCount == -1)
128 IoState_error_(IOSTATE, m, "*** IoDBI error during dbi_initialize\n");
130 else
132 DATA(self)->didInit = 1;
135 return IONUMBER(DATA(self)->driverCount);
138 IoObject *IoDBI_drivers(IoDBI *self, IoObject *locals, IoMessage *m)
140 /*#io
141 docSlot("drivers", "Get a list of drivers and it's associated information:
143 <ol>
144 <li>name</li>
145 <li>description</li>
146 <li>filename</li>
147 <li>version</li>
148 <li>date compiled</li>
149 <li>maintainer</li>
150 <li>url</li>
151 </ol>")
153 IoList *list = IOREF(IoList_new(IOSTATE));
154 dbi_driver driver = NULL;
156 while((driver = dbi_driver_list(driver)) != NULL)
158 IoList *dlist = IOREF(IoList_new(IOSTATE));
159 IoList_rawAppend_(dlist, IOSYMBOL(dbi_driver_get_name(driver)));
160 IoList_rawAppend_(dlist, IOSYMBOL(dbi_driver_get_description(driver)));
161 IoList_rawAppend_(dlist, IOSYMBOL(dbi_driver_get_filename(driver)));
162 IoList_rawAppend_(dlist, IOSYMBOL(dbi_driver_get_version(driver)));
163 IoList_rawAppend_(dlist, IOSYMBOL(dbi_driver_get_date_compiled(driver)));
164 IoList_rawAppend_(dlist, IOSYMBOL(dbi_driver_get_maintainer(driver)));
165 IoList_rawAppend_(dlist, IOSYMBOL(dbi_driver_get_url(driver)));
167 IoList_rawAppend_(list, dlist);
170 return list;
173 IoObject *IoDBI_with(IoDBI *self, IoObject *locals, IoMessage *m)
175 /*#io
176 docSlot("with(driverName)", "Get a new connection with the given driver")
178 IoObject *name = IoMessage_locals_valueArgAt_(m, locals, 0);
179 if (!ISSYMBOL(name))
181 IoState_error_(IOSTATE, m, "argument 0 to method '%s' must be a Symbol, not a '%s'\n",
182 CSTRING(IoMessage_name(m)), IoObject_name(name));
183 return IONIL(self);
186 if (DATA(self)->didInit != 1)
188 IoDBI_init(self, locals, m);
191 dbi_conn c = dbi_conn_new(CSTRING(name));
192 if (c == NULL)
194 IoState_error_(IOSTATE, m, "libdbi error during dbi_conn_new\n");
195 return IONIL(self);
198 return IoDBIConn_new(IOSTATE, c);