3 docCopyright("Jeremy Cowgar", 2006)
4 docLicense("BSD revised")
5 docCategory("Databases")
7 docDescription("An object for wrapping libdbi to allow Io access to
8 a multitude of database systems transparently.")
12 #include "IoDBIConn.h"
18 #define DATA(self) ((IoDBIData *)IoObject_dataPointer(self))
20 void ReportDBIError(dbi_conn conn
, void *state
, IoMessage
*m
)
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);
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
[] = {
51 {"initWithDriversPath", IoDBI_initWithDriversPath
},
52 {"version", IoDBI_version
},
53 {"drivers", IoDBI_drivers
},
57 IoObject_addMethodTable_(self
, methodTable
);
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;
71 void IoDBI_free(IoDBI
*self
)
74 free(IoObject_dataPointer(self
));
77 //void IoDBI_mark(IoDBI *self) {}
79 /* ---------------------------------------------------------------- */
81 IoObject
*IoDBI_version(IoDBI
*self
, IoObject
*locals
, IoMessage
*m
)
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
)
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");
101 DATA(self
)->didInit
= 1;
104 return IONUMBER(DATA(self
)->driverCount
);
107 IoObject
*IoDBI_initWithDriversPath(IoDBI
*self
, IoObject
*locals
,
111 docSlot("initWithDriversPath", "Initialize the DBI environment with the
112 specified libdbi driver path")
114 IoObject
*dir
= IoMessage_locals_valueArgAt_(m
, locals
, 0);
118 DATA(self
)->driverCount
= dbi_initialize(CSTRING(dir
));
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");
132 DATA(self
)->didInit
= 1;
135 return IONUMBER(DATA(self
)->driverCount
);
138 IoObject
*IoDBI_drivers(IoDBI
*self
, IoObject
*locals
, IoMessage
*m
)
141 docSlot("drivers", "Get a list of drivers and it's associated information:
148 <li>date compiled</li>
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
);
173 IoObject
*IoDBI_with(IoDBI
*self
, IoObject
*locals
, IoMessage
*m
)
176 docSlot("with(driverName)", "Get a new connection with the given driver")
178 IoObject
*name
= IoMessage_locals_valueArgAt_(m
, locals
, 0);
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
));
186 if (DATA(self
)->didInit
!= 1)
188 IoDBI_init(self
, locals
, m
);
191 dbi_conn c
= dbi_conn_new(CSTRING(name
));
194 IoState_error_(IOSTATE
, m
, "libdbi error during dbi_conn_new\n");
198 return IoDBIConn_new(IOSTATE
, c
);