new functionality - file_get_properties()
[mediadatabase.git] / libdb / libdb.c
blob7577df1369c6912ea4099ea38bd0e49ed4bd1806
1 /* -*- Mode: C ; c-basic-offset: 2 -*- */
2 /*****************************************************************************
4 * $Id: libdb.c,v 1.10 2005/03/05 21:39:44 nedko Exp $
6 * DESCRIPTION:
7 * Unified access to database.
9 * AUTHOR:
10 * Nedko Arnaudov <nedko@users.sourceforge.net>
12 * LICENSE:
13 * GNU GENERAL PUBLIC LICENSE version 2
15 *****************************************************************************/
17 #include <stdlib.h>
18 #include <assert.h>
19 #include <string.h>
21 #include "../result.h"
22 #include "internal.h"
23 #include "memory.h"
25 char *pszErrorMsg = NULL;
27 mediadb_result
28 mediadb_open(
29 unsigned int nDBType,
30 const char *pszHost,
31 const char *pszUser,
32 const char *pszPass,
33 const char *pszDB,
34 mediadb *phDB)
36 mediadb_result r;
37 mediadb_impl *pImpl;
39 pImpl = malloc(sizeof(mediadb_impl));
40 if (pImpl == NULL)
42 r = MEDIADB_MEM;
43 goto Fail;
46 pImpl->nDBType = nDBType;
48 if (nDBType == MEDIADB_DBTYPE_MYSQL)
50 r = mediadb_mysql_open(
51 &pImpl->data.mysql,
52 pszHost,
53 pszUser,
54 pszPass,
55 pszDB);
56 if (MEDIADB_IS_ERROR(r))
58 if (pszErrorMsg != NULL)
59 free(pszErrorMsg);
60 pszErrorMsg = strdup(mediadb_mysql_get_error_message(&pImpl->data.mysql));
61 goto FailFreeMem;
64 else if (nDBType == MEDIADB_DBTYPE_SQLITE)
66 r = mediadb_sqlite_open(
67 &pImpl->data.sqlite,
68 pszHost,
69 pszUser,
70 pszPass,
71 pszDB);
72 if (MEDIADB_IS_ERROR(r))
74 if (pszErrorMsg != NULL)
75 free(pszErrorMsg);
76 pszErrorMsg = strdup(mediadb_sqlite_get_error_message(&pImpl->data.sqlite));
77 goto FailFreeMem;
80 else
82 r = MEDIADB_NOT_IMPL;
83 goto FailFreeMem;
86 *phDB = (mediadb)pImpl;
88 return MEDIADB_OK;
90 FailFreeMem:
91 free(pImpl);
92 Fail:
93 return r;
96 #define pDB ((mediadb_impl *)hDB)
98 mediadb_result
99 mediadb_media_add_new(
100 mediadb hDB,
101 const char *pszName,
102 const char *pszComment,
103 mediadb_mediatype nType,
104 mediadb_uint *pnNewMediaID)
106 mediadb_result r;
108 if (pDB->nDBType == MEDIADB_DBTYPE_MYSQL)
110 r = mediadb_mysql_media_add_new(
111 &pDB->data.mysql,
112 pszName,
113 pszComment,
114 nType,
115 pnNewMediaID);
117 else if (pDB->nDBType == MEDIADB_DBTYPE_SQLITE)
119 r = mediadb_sqlite_media_add_new(
120 &pDB->data.sqlite,
121 pszName,
122 pszComment,
123 nType,
124 pnNewMediaID);
126 else
128 r = MEDIADB_INVAL_ARG;
129 assert(0);
132 return r;
135 mediadb_result
136 mediadb_media_update_properties(
137 mediadb hDB,
138 mediadb_uint nMediaID,
139 mediadb_uint nTimeAdded,
140 mediadb_uint nTotalFiles,
141 mediadb_uint nTotalSize)
143 mediadb_result r;
145 if (pDB->nDBType == MEDIADB_DBTYPE_MYSQL)
147 r = mediadb_mysql_media_update_properties(
148 &pDB->data.mysql,
149 nMediaID,
150 nTimeAdded,
151 nTotalFiles,
152 nTotalSize);
154 else if (pDB->nDBType == MEDIADB_DBTYPE_SQLITE)
156 r = mediadb_sqlite_media_update_properties(
157 &pDB->data.sqlite,
158 nMediaID,
159 nTimeAdded,
160 nTotalFiles,
161 nTotalSize);
163 else
165 r = MEDIADB_INVAL_ARG;
166 assert(0);
169 return r;
172 mediadb_result
173 mediadb_file_add_new(
174 mediadb hDB,
175 mediadb_uint nMediaID,
176 mediadb_filetype Filetype,
177 const char *pszPath,
178 const char *pszName,
179 mediadb_uint nFileSize,
180 mediadb_uint nFileTime)
182 mediadb_result r;
184 if (pDB->nDBType == MEDIADB_DBTYPE_MYSQL)
186 r = mediadb_mysql_file_add_new(
187 &pDB->data.mysql,
188 nMediaID,
189 Filetype,
190 pszPath,
191 pszName,
192 nFileSize,
193 nFileTime);
195 else if (pDB->nDBType == MEDIADB_DBTYPE_SQLITE)
197 r = mediadb_sqlite_file_add_new(
198 &pDB->data.sqlite,
199 nMediaID,
200 Filetype,
201 pszPath,
202 pszName,
203 nFileSize,
204 nFileTime);
206 else
208 r = MEDIADB_INVAL_ARG;
209 assert(0);
212 return r;
215 mediadb_result
216 mediadb_media_get_properties(
217 mediadb hDB,
218 mediadb_uint nMediaID,
219 mediadb_uint *pnTimeAdded,
220 mediadb_mediatype *pnType,
221 mediadb_uint *pnLocationID,
222 char **ppszTitle)
224 mediadb_result r;
226 if (pDB->nDBType == MEDIADB_DBTYPE_MYSQL)
228 r = mediadb_mysql_media_get_properties(
229 &pDB->data.mysql,
230 nMediaID,
231 pnTimeAdded,
232 pnType,
233 pnLocationID,
234 ppszTitle);
236 else if (pDB->nDBType == MEDIADB_DBTYPE_SQLITE)
238 r = mediadb_sqlite_media_get_properties(
239 &pDB->data.sqlite,
240 nMediaID,
241 pnTimeAdded,
242 pnType,
243 pnLocationID,
244 ppszTitle);
246 else
248 r = MEDIADB_INVAL_ARG;
249 assert(0);
252 return r;
255 mediadb_result
256 mediadb_location_get_properties(
257 mediadb hDB,
258 mediadb_uint nLocationID,
259 mediadb_uint *pnLocationTypeID,
260 char **ppszDescription)
262 mediadb_result r;
264 if (pDB->nDBType == MEDIADB_DBTYPE_MYSQL)
266 r = mediadb_mysql_location_get_properties(
267 &pDB->data.mysql,
268 nLocationID,
269 pnLocationTypeID,
270 ppszDescription);
272 else if (pDB->nDBType == MEDIADB_DBTYPE_SQLITE)
274 r = mediadb_sqlite_location_get_properties(
275 &pDB->data.sqlite,
276 nLocationID,
277 pnLocationTypeID,
278 ppszDescription);
280 else
282 r = MEDIADB_INVAL_ARG;
283 assert(0);
286 return r;
289 mediadb_result
290 mediadb_location_type_get_properties(
291 mediadb hDB,
292 mediadb_uint nLocationTypeID,
293 char **ppszDescription)
295 mediadb_result r;
297 if (pDB->nDBType == MEDIADB_DBTYPE_MYSQL)
299 r = mediadb_mysql_location_type_get_properties(
300 &pDB->data.mysql,
301 nLocationTypeID,
302 ppszDescription);
304 else if (pDB->nDBType == MEDIADB_DBTYPE_SQLITE)
306 r = mediadb_sqlite_location_type_get_properties(
307 &pDB->data.sqlite,
308 nLocationTypeID,
309 ppszDescription);
311 else
313 r = MEDIADB_INVAL_ARG;
314 assert(0);
317 return r;
320 mediadb_result
321 mediadb_media_get_properties_data(
322 mediadb hDB,
323 mediadb_uint nMediaID,
324 mediadb_uint *pnTotalFiles,
325 mediadb_uint *pnTotalSize)
327 mediadb_result r;
329 if (pDB->nDBType == MEDIADB_DBTYPE_MYSQL)
331 r = mediadb_mysql_media_get_properties_data(
332 &pDB->data.mysql,
333 nMediaID,
334 pnTotalFiles,
335 pnTotalSize);
337 else if (pDB->nDBType == MEDIADB_DBTYPE_SQLITE)
339 r = mediadb_sqlite_media_get_properties_data(
340 &pDB->data.sqlite,
341 nMediaID,
342 pnTotalFiles,
343 pnTotalSize);
345 else
347 r = MEDIADB_INVAL_ARG;
348 assert(0);
351 return r;
354 mediadb_result
355 mediadb_delete_media_files(
356 mediadb hDB,
357 mediadb_uint nMediaID)
359 mediadb_result r;
361 if (pDB->nDBType == MEDIADB_DBTYPE_MYSQL)
363 r = mediadb_mysql_delete_media_files(
364 &pDB->data.mysql,
365 nMediaID);
367 else if (pDB->nDBType == MEDIADB_DBTYPE_SQLITE)
369 r = mediadb_sqlite_delete_media_files(
370 &pDB->data.sqlite,
371 nMediaID);
373 else
375 r = MEDIADB_INVAL_ARG;
376 assert(0);
379 return r;
382 mediadb_result
383 mediadb_media_update_name(
384 mediadb hDB,
385 mediadb_uint nMediaID,
386 const char *pszName)
388 mediadb_result r;
390 if (pDB->nDBType == MEDIADB_DBTYPE_MYSQL)
392 r = mediadb_mysql_media_update_name(
393 &pDB->data.mysql,
394 nMediaID,
395 pszName);
397 else if (pDB->nDBType == MEDIADB_DBTYPE_SQLITE)
399 r = mediadb_sqlite_media_update_name(
400 &pDB->data.sqlite,
401 nMediaID,
402 pszName);
404 else
406 r = MEDIADB_INVAL_ARG;
407 assert(0);
410 return r;
413 mediadb_result
414 mediadb_close(
415 mediadb hDB)
417 mediadb_result r;
419 if (pDB->nDBType == MEDIADB_DBTYPE_MYSQL)
421 r = mediadb_mysql_close(
422 &pDB->data.mysql);
424 else if (pDB->nDBType == MEDIADB_DBTYPE_SQLITE)
426 r = mediadb_sqlite_close(
427 &pDB->data.sqlite);
429 else
431 r = MEDIADB_INVAL_ARG;
432 assert(0);
435 if (MEDIADB_IS_ERROR(r))
437 return r;
440 free(pDB);
442 return r;
445 const char *
446 mediadb_get_error_message(
447 mediadb hDB)
449 if (pDB == NULL)
451 if (pszErrorMsg == NULL)
453 return "";
455 else
457 return pszErrorMsg;
461 if (pDB->nDBType == MEDIADB_DBTYPE_MYSQL)
463 return mediadb_mysql_get_error_message(&pDB->data.mysql);
466 if (pDB->nDBType == MEDIADB_DBTYPE_SQLITE)
468 return mediadb_sqlite_get_error_message(&pDB->data.sqlite);
471 assert(0);
472 return "enexpected behaviour";
475 mediadb_result
476 mediadb_media_get_all(
477 mediadb hDB,
478 mediadb_media_callback pCallback,
479 void *pUserContext)
481 mediadb_result r;
483 if (pDB->nDBType == MEDIADB_DBTYPE_MYSQL)
485 r = mediadb_mysql_media_get_all(
486 &pDB->data.mysql,
487 pCallback,
488 pUserContext);
490 else if (pDB->nDBType == MEDIADB_DBTYPE_SQLITE)
492 r = mediadb_sqlite_media_get_all(
493 &pDB->data.sqlite,
494 pCallback,
495 pUserContext);
497 else
499 r = MEDIADB_INVAL_ARG;
500 assert(0);
503 return r;
506 mediadb_result
507 mediadb_files_get(
508 mediadb hDB,
509 mediadb_uint nMediaID,
510 const char *pszPath,
511 mediadb_files_callback pCallback,
512 void *pUserContext)
514 mediadb_result r;
516 if (pDB->nDBType == MEDIADB_DBTYPE_MYSQL)
518 r = mediadb_mysql_files_get(
519 &pDB->data.mysql,
520 nMediaID,
521 pszPath,
522 pCallback,
523 pUserContext);
525 else if (pDB->nDBType == MEDIADB_DBTYPE_SQLITE)
527 r = mediadb_sqlite_files_get(
528 &pDB->data.sqlite,
529 nMediaID,
530 pszPath,
531 pCallback,
532 pUserContext);
534 else
536 r = MEDIADB_INVAL_ARG;
537 assert(0);
540 return r;
543 mediadb_result
544 mediadb_get_pattern_match_methods(
545 mediadb hDB,
546 const struct mediadb_pattern_match_method **ppPMM)
548 mediadb_result r;
550 if (pDB->nDBType == MEDIADB_DBTYPE_MYSQL)
552 r = mediadb_mysql_get_pattern_match_methods(
553 &pDB->data.mysql,
554 ppPMM);
556 else if (pDB->nDBType == MEDIADB_DBTYPE_SQLITE)
558 r = mediadb_sqlite_get_pattern_match_methods(
559 &pDB->data.sqlite,
560 ppPMM);
562 else
564 r = MEDIADB_INVAL_ARG;
565 assert(0);
568 return r;
571 mediadb_result
572 mediadb_files_search(
573 mediadb hDB,
574 mediadb_uint nFilenamePMMID,
575 const char *pszFilenamePattern,
576 mediadb_uint nPathPMMID,
577 const char *pszPathPattern,
578 const mediadb_uint *pnMinSize,
579 const mediadb_uint *pnMaxSize,
580 mediadb_files_search_callback pCallback,
581 void *pUserContext)
583 mediadb_result r;
585 if (pDB->nDBType == MEDIADB_DBTYPE_MYSQL)
587 r = mediadb_mysql_files_search(
588 &pDB->data.mysql,
589 nFilenamePMMID,
590 pszFilenamePattern,
591 nPathPMMID,
592 pszPathPattern,
593 pnMinSize,
594 pnMaxSize,
595 pCallback,
596 pUserContext);
598 else if (pDB->nDBType == MEDIADB_DBTYPE_SQLITE)
600 r = mediadb_sqlite_files_search(
601 &pDB->data.sqlite,
602 nFilenamePMMID,
603 pszFilenamePattern,
604 nPathPMMID,
605 pszPathPattern,
606 pnMinSize,
607 pnMaxSize,
608 pCallback,
609 pUserContext);
611 else
613 r = MEDIADB_INVAL_ARG;
614 assert(0);
617 return r;
620 mediadb_result
621 mediadb_file_get_properties(
622 mediadb hDB,
623 mediadb_uint nMediaID,
624 const char *pszPath,
625 const char *pszName,
626 mediadb_filetype *pFiletype,
627 mediadb_uint *pnSize,
628 mediadb_uint *pnTime)
630 mediadb_result r;
632 if (pDB->nDBType == MEDIADB_DBTYPE_MYSQL)
634 r = mediadb_mysql_file_get_properties(
635 &pDB->data.mysql,
636 nMediaID,
637 pszPath,
638 pszName,
639 pFiletype,
640 pnSize,
641 pnTime);
643 else if (pDB->nDBType == MEDIADB_DBTYPE_SQLITE)
645 r = mediadb_sqlite_file_get_properties(
646 &pDB->data.sqlite,
647 nMediaID,
648 pszPath,
649 pszName,
650 pFiletype,
651 pnSize,
652 pnTime);
654 else
656 r = MEDIADB_INVAL_ARG;
657 assert(0);
660 return r;
663 /*****************************************************************************
665 * Modifications log:
667 * !!! WARNING !!! Following lines are automatically updated by the CVS system.
669 * $Log: libdb.c,v $
670 * Revision 1.10 2005/03/05 21:39:44 nedko
671 * new functionality - file_get_properties()
673 * Revision 1.9 2004/08/31 22:40:15 nedko
674 * Partitally implemented search feature.
676 * Revision 1.8 2004/08/08 00:47:42 nedko
677 * Get more info for media from database.
679 * Revision 1.7 2004/05/21 23:40:46 nedko
680 * New functionality: mediadb_files_get()
682 * Revision 1.6 2004/05/16 18:57:12 nedko
683 * media_get_all functionality implemented.
685 * Revision 1.5 2004/05/11 01:17:42 nedko
686 * Allow mediadb_get_error_message() to work after failed database open.
688 * Revision 1.4 2004/05/03 20:47:14 nedko
689 * Update mode for cui
691 * Revision 1.3 2004/05/02 20:12:11 nedko
692 * Improve error dumps.
694 * Revision 1.2 2004/05/02 13:16:58 nedko
695 * Supply filetype when adding new file
697 * Revision 1.1 2004/04/27 09:12:28 nedko
698 * Initial revision.
700 *****************************************************************************/