1 |
|
/* |
2 |
|
* DotGNU Execution Environment Core |
3 |
|
* (c)2003 netFluid Technology Ltd - http://www.nfluid.com |
4 |
|
* |
5 |
|
* Database Object Encapsulation |
6 |
|
* |
7 |
|
* This program is free software; you can redistribute it and/or modify |
8 |
|
* it under the terms of the GNU General Public License as published by |
9 |
|
* the Free Software Foundation; either version 2 of the License, or |
10 |
|
* (at your option) any later version. |
11 |
|
* |
12 |
|
* This program is distributed in the hope that it will be useful, |
13 |
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 |
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 |
|
* GNU General Public License for more details. |
16 |
|
* |
17 |
|
* You should have received a copy of the GNU General Public License |
18 |
|
* along with this program; if not, write to the Free Software |
19 |
|
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA |
20 |
|
* |
21 |
|
* $Revision$ $Date$ |
22 |
|
* |
23 |
|
* -------------------------------------------------------------------------- |
24 |
|
*/ |
25 |
|
#include <stdio.h> |
26 |
|
#include <stdlib.h> |
27 |
|
|
28 |
|
#include "dbObject.h" |
29 |
|
#include "dgBDB.h" |
30 |
|
|
31 |
|
int db_Load( dbObject *this ); |
32 |
|
int db_Save( dbObject *this ); |
33 |
|
int db_Delete( dbObject *this ); |
34 |
|
int db_CompareKey( dbObject *this, dbObject *that ); |
35 |
|
|
36 |
|
int db_setProperty ( dbObject *this, char *property, char *data ); |
37 |
|
char *db_getProperty ( dbObject *this, char *property ); |
38 |
|
|
39 |
|
static int db_initTable( dbObject *this ); |
40 |
|
void db_destroy( dbObject **this_p ); |
41 |
|
|
42 |
|
|
43 |
|
/* ------------------------------------------------------------------------- */ |
44 |
|
|
45 |
|
dbObject * |
46 |
|
db_newObject( dbObjDesc *def ) |
47 |
|
{ |
48 |
|
dbObject *this = NULL; |
49 |
|
|
50 |
|
this = (dbObject*)malloc( def->objSize ); |
51 |
|
memset( (char*)this, 0, def->objSize ); |
52 |
|
|
53 |
|
/* Setup method pointers */ |
54 |
|
this->method.Load = &db_Load; |
55 |
|
this->method.Save = &db_Save; |
56 |
|
this->method.Delete = &db_Delete; |
57 |
|
this->method.Set = &db_setProperty; |
58 |
|
this->method.Get = &db_getProperty; |
59 |
|
this->method.Destroy = &db_destroy; |
60 |
|
this->method.CompareKey = &db_CompareKey; |
61 |
|
|
62 |
|
/* Initialise private properties */ |
63 |
|
this->private.dirty = 0; |
64 |
|
this->private.newobj = 1; |
65 |
|
|
66 |
|
this->private.keyProperties = def->keyProperties; |
67 |
|
this->private.dataProperties = def->dataProperties; |
68 |
|
this->private.dataSize = def->dataSize; |
69 |
|
this->private.keySize = def->keySize; |
70 |
|
this->private.keyOffset = sizeof(dbObject); |
71 |
|
this->private.dataOffset = sizeof(dbObject) + def->keySize; |
72 |
|
this->private.tableName = def->tableName; |
73 |
|
this->private.uniqueKey = def->keyIsUnique; |
74 |
|
this->private.dbh = def->dbh; |
75 |
|
this->private.objSize = def->objSize; |
76 |
|
|
77 |
|
if( db_initTable( this ) < 0 ) { |
78 |
|
free(this); |
79 |
|
return NULL; |
80 |
|
} |
81 |
|
|
82 |
|
this->private.newobj = 1; |
83 |
|
this->private.dirty = 0; |
84 |
|
|
85 |
|
return this; |
86 |
|
} |
87 |
|
|
88 |
|
/* ------------------------------------------------------------------------- */ |
89 |
|
|
90 |
|
void |
91 |
|
db_destroy( dbObject **this_p ) |
92 |
|
{ |
93 |
|
if( *this_p ) { |
94 |
|
free( *this_p ); |
95 |
|
*this_p = NULL; |
96 |
|
} |
97 |
|
} |
98 |
|
|
99 |
|
/* ------------------------------------------------------------------------- */ |
100 |
|
|
101 |
|
int |
102 |
|
db_initTable( dbObject *this ) |
103 |
|
{ |
104 |
|
char *ldbh = NULL; |
105 |
|
|
106 |
|
if( dgdb_init_dbengine() < 0 ) return -1; |
107 |
|
|
108 |
|
if( *(this->private.dbh) == NULL ) { |
109 |
|
/* Need a database connection */ |
110 |
|
if( (ldbh = dgdb_open_db( this->private.tableName, this->private.uniqueKey )) == NULL ) { |
111 |
|
return -1; |
112 |
|
} |
113 |
|
*(this->private.dbh) = ldbh; |
114 |
|
} |
115 |
|
|
116 |
|
return 0; |
117 |
|
} |
118 |
|
|
119 |
|
/* ------------------------------------------------------------------------- */ |
120 |
|
/* This method should be used to set properties of the object so that |
121 |
|
* 'dirty' handling can be performed on save etc. |
122 |
|
* |
123 |
|
* The data is always passed in as an address ptr |
124 |
|
*/ |
125 |
|
int |
126 |
|
db_setProperty( dbObject *this, char *property, char * data ) |
127 |
|
{ |
128 |
|
dbProperty *prop = NULL; |
129 |
|
int offset = -1; |
130 |
|
|
131 |
|
if( data == NULL ) return -1; |
132 |
|
|
133 |
|
/* Find property ... */ |
134 |
|
prop = this->private.dataProperties; |
135 |
|
while( prop->property != NULL ) { |
136 |
|
if( strcmp( prop->property, property ) == 0 ) { |
137 |
|
/* Found */ |
138 |
|
offset = prop->offset; |
139 |
|
break; |
140 |
|
} |
141 |
|
prop++; |
142 |
|
} |
143 |
|
if( offset >= 0 ) { |
144 |
|
switch( prop->type ) { |
145 |
|
case DBF_CHAR: |
146 |
|
strncpy( ((char*)this) + this->private.dataOffset + offset, |
147 |
|
(char*)data, prop->size ); |
148 |
|
break; |
149 |
|
|
150 |
|
case DBF_INT: |
151 |
|
{ |
152 |
|
unsigned char *ptr = ((unsigned char*)this) + |
153 |
|
this->private.dataOffset + offset; |
154 |
|
memcpy( ptr, data, sizeof(int) ); |
155 |
|
} |
156 |
|
break; |
157 |
|
|
158 |
|
case DBF_LONG: |
159 |
|
{ |
160 |
|
unsigned char *ptr = ((unsigned char*)this) + |
161 |
|
this->private.dataOffset + offset; |
162 |
|
memcpy( ptr, data, sizeof(long) ); |
163 |
|
} |
164 |
|
break; |
165 |
|
} |
166 |
|
this->private.dirty = 1; |
167 |
|
return 0; |
168 |
|
} |
169 |
|
|
170 |
|
prop = this->private.keyProperties; |
171 |
|
while( prop->property != NULL ) { |
172 |
|
if( strcmp( prop->property, property ) == 0 ) { |
173 |
|
/* Found */ |
174 |
|
offset = prop->offset; |
175 |
|
break; |
176 |
|
} |
177 |
|
prop++; |
178 |
|
} |
179 |
|
if( offset >= 0 ) { |
180 |
|
switch( prop->type ) { |
181 |
|
case DBF_CHAR: |
182 |
|
strncpy( ((char*)this) + this->private.keyOffset + offset, |
183 |
|
data, prop->size ); |
184 |
|
break; |
185 |
|
case DBF_INT: |
186 |
|
{ |
187 |
|
unsigned char *ptr = ((unsigned char*)this) + |
188 |
|
this->private.dataOffset + offset; |
189 |
|
memcpy( ptr, data, sizeof(int) ); |
190 |
|
} |
191 |
|
break; |
192 |
|
|
193 |
|
case DBF_LONG: |
194 |
|
{ |
195 |
|
unsigned char *ptr = ((unsigned char*)this) + |
196 |
|
this->private.dataOffset + offset; |
197 |
|
memcpy( ptr, data, sizeof(long) ); |
198 |
|
} |
199 |
|
break; |
200 |
|
} |
201 |
|
this->private.dirty = 1; |
202 |
|
return 0; |
203 |
|
} |
204 |
|
|
205 |
|
return -1; /* Property not found */ |
206 |
|
} |
207 |
|
|
208 |
|
/* ------------------------------------------------------------------------- */ |
209 |
|
/* |
210 |
|
* The data is always passed in as an address ptr |
211 |
|
*/ |
212 |
|
char * |
213 |
|
db_getProperty( dbObject *this, char *property ) |
214 |
|
{ |
215 |
|
dbProperty *prop = NULL; |
216 |
|
int offset = -1; |
217 |
|
|
218 |
|
/* Find property ... */ |
219 |
|
prop = this->private.dataProperties; |
220 |
|
while( prop->property != NULL ) { |
221 |
|
if( strcmp( prop->property, property ) == 0 ) { |
222 |
|
/* Found */ |
223 |
|
offset = prop->offset; |
224 |
|
break; |
225 |
|
} |
226 |
|
prop++; |
227 |
|
} |
228 |
|
if( offset >= 0 ) { |
229 |
|
switch( prop->type ) { |
230 |
|
case DBF_CHAR: |
231 |
|
return ((char*)this) + this->private.dataOffset + offset; |
232 |
|
break; |
233 |
|
|
234 |
|
case DBF_INT: |
235 |
|
{ |
236 |
|
unsigned char *ptr = ((unsigned char*)this) + |
237 |
|
this->private.dataOffset + offset; |
238 |
|
return (char*)(*((int*)ptr)); |
239 |
|
} |
240 |
|
break; |
241 |
|
|
242 |
|
case DBF_LONG: |
243 |
|
{ |
244 |
|
unsigned char *ptr = ((unsigned char*)this) + |
245 |
|
this->private.dataOffset + offset; |
246 |
|
return (char*)(*((long*)ptr)); |
247 |
|
} |
248 |
|
break; |
249 |
|
} |
250 |
|
} |
251 |
|
|
252 |
|
prop = this->private.keyProperties; |
253 |
|
while( prop->property != NULL ) { |
254 |
|
if( strcmp( prop->property, property ) == 0 ) { |
255 |
|
/* Found */ |
256 |
|
offset = prop->offset; |
257 |
|
break; |
258 |
|
} |
259 |
|
prop++; |
260 |
|
} |
261 |
|
if( offset >= 0 ) { |
262 |
|
switch( prop->type ) { |
263 |
|
case DBF_CHAR: |
264 |
|
return ((char*)this) + this->private.dataOffset + offset; |
265 |
|
break; |
266 |
|
|
267 |
|
case DBF_INT: |
268 |
|
{ |
269 |
|
unsigned char *ptr = ((unsigned char*)this) + |
270 |
|
this->private.dataOffset + offset; |
271 |
|
return (char*)(*((int*)ptr)); |
272 |
|
} |
273 |
|
break; |
274 |
|
|
275 |
|
case DBF_LONG: |
276 |
|
{ |
277 |
|
unsigned char *ptr = ((unsigned char*)this) + |
278 |
|
this->private.dataOffset + offset; |
279 |
|
return (char*)(*((long*)ptr)); |
280 |
|
} |
281 |
|
break; |
282 |
|
} |
283 |
|
} |
284 |
|
|
285 |
|
return NULL; /* Property not found */ |
286 |
|
} |
287 |
|
|
288 |
|
/* ------------------------------------------------------------------------- */ |
289 |
|
|
290 |
|
int |
291 |
|
db_Load( dbObject *this ) |
292 |
|
{ |
293 |
|
dbRow tRow; |
294 |
|
int ret = -1; |
295 |
|
|
296 |
|
SET_ROW_KEY_PTR_LEN (tRow, ((char*)this) + this->private.keyOffset, this->private.keySize ); |
297 |
|
SET_ROW_DATA_PTR_LEN(tRow, ((char*)this) + this->private.dataOffset, this->private.dataSize ); |
298 |
|
|
299 |
|
ret = dgdb_read_row( &tRow, *(this->private.dbh), NULL ); |
300 |
|
|
301 |
|
this->private.newobj = 0; |
302 |
|
this->private.dirty = 0; |
303 |
|
|
304 |
|
return ret; |
305 |
|
} |
306 |
|
|
307 |
|
/* ------------------------------------------------------------------------- */ |
308 |
|
|
309 |
|
int |
310 |
|
db_Save( dbObject *this ) |
311 |
|
{ |
312 |
|
dbRow tRow; |
313 |
|
|
314 |
|
/* Nothing to do if object is not dirty. |
315 |
|
*/ |
316 |
|
if( this->private.dirty == 0 ) return 0; |
317 |
|
|
318 |
|
SET_ROW_KEY_PTR_LEN (tRow, ((char*)this) + this->private.keyOffset, this->private.keySize ); |
319 |
|
SET_ROW_DATA_PTR_LEN(tRow, ((char*)this) + this->private.dataOffset, this->private.dataSize ); |
320 |
|
|
321 |
|
/* If object is new, then INSERT (1) otherwise UPDATE (0) |
322 |
|
*/ |
323 |
|
if( dgdb_store_row( &tRow, *(this->private.dbh), this->private.newobj ? 1 : 0, NULL ) < 0 ) { |
324 |
|
printf( "Failed to add row to DB %s\n", dgdberrstr ); |
325 |
|
return -1; |
326 |
|
} |
327 |
|
|
328 |
|
this->private.newobj = 0; |
329 |
|
this->private.dirty = 0; |
330 |
|
|
331 |
|
return 0; |
332 |
|
} |
333 |
|
|
334 |
|
/* ------------------------------------------------------------------------- */ |
335 |
|
|
336 |
|
int |
337 |
|
db_Delete( dbObject *this ) |
338 |
|
{ |
339 |
|
dbRow tRow; |
340 |
|
|
341 |
|
SET_ROW_KEY_PTR_LEN (tRow, ((char*)this) + this->private.keyOffset, this->private.keySize ); |
342 |
|
|
343 |
|
if( dgdb_delete_row( &tRow, *(this->private.dbh), NULL ) < 0 ) { |
344 |
|
printf( "Failed to delete row\n" ); |
345 |
|
return -1; |
346 |
|
} |
347 |
|
|
348 |
|
this->private.dirty = 1; |
349 |
|
|
350 |
|
return 0; |
351 |
|
} |
352 |
|
|
353 |
|
/* ------------------------------------------------------------------------- */ |
354 |
|
|
355 |
|
int |
356 |
|
db_CompareKey( dbObject *this, dbObject *that ) |
357 |
|
{ |
358 |
|
return memcmp( ((char*)this) + this->private.keyOffset, |
359 |
|
((char*)that) + that->private.keyOffset, this->private.keySize ); |
360 |
|
} |
361 |
|
|
362 |
|
/* ------------------------------------------------------------------------- */ |
363 |
|
|
364 |
|
void |
365 |
|
db_configure( char * dbroot ) { |
366 |
|
dgdb_configure( dbroot ); |
367 |
|
} |
368 |
|
|
369 |
|
/* ------------------------------------------------------------------------- */ |
370 |
|
/* end */ |