1 /* -*- Mode: C ; c-basic-offset: 2 -*- */
2 /*****************************************************************************
4 * $Id: sqlite.c,v 1.11 2004/09/18 21:32:34 nedko Exp $
7 * SQLite backend implementation.
10 * Nedko Arnaudov <nedko@users.sourceforge.net>
13 * GNU GENERAL PUBLIC LICENSE version 2
15 *****************************************************************************/
23 #include "../result.h"
27 #define MEDIADB_SQLITE_MEDIA_TABLE "media"
28 #define MEDIADB_SQLITE_FILES_TABLE "files"
29 #define MEDIADB_SQLITE_LOCATIONS_TABLE "locations"
30 #define MEDIADB_SQLITE_LOCATION_TYPES_TABLE "location_types"
32 //#define DUMMY_SQL_QUERIES
33 #define FORMATTING_BUFFER_SIZE 10240
35 #define MEDIADB_SQLITE_PMM_EXACT_CASE_INSENSITIVE 1
36 #define MEDIADB_SQLITE_PMM_EXACT_CASE_SENSITIVE 2
37 #define MEDIADB_SQLITE_PMM_LIKE_CASE_INSENSITIVE 3
38 #define MEDIADB_SQLITE_PMM_LIKE_CASE_SENSITIVE 4
39 #define MEDIADB_SQLITE_PMM_REGEXP_CASE_INSENSITIVE 5
40 #define MEDIADB_SQLITE_PMM_REGEXP_CASE_SENSITIVE 6
42 static struct mediadb_pattern_match_method
43 mediadb_sqlite_pattern_match_methods
[] =
46 MEDIADB_SQLITE_PMM_EXACT_CASE_INSENSITIVE
,
47 "exact - case insensitive",
48 "Match exactly, ignore case",
53 MEDIADB_SQLITE_PMM_EXACT_CASE_SENSITIVE,
54 "exact - case sensitive",
55 "Match exactly, do not ignore case",
60 MEDIADB_SQLITE_PMM_LIKE_CASE_INSENSITIVE
,
61 "SQL \"LIKE\" - case insensitive",
62 "Traditional SQL \"LIKE\" oprator pattern matching, ignore case",
63 "SQL pattern matching allows you to use `_' to match any single character "
64 "and `%' to match an arbitrary number of characters (including zero characters). "
65 "LIKE pattern match succeeds only if the pattern matches the entire value."
69 MEDIADB_SQLITE_PMM_LIKE_CASE_SENSITIVE,
70 "SQL \"LIKE\" - case sensitive",
71 "Traditional SQL \"LIKE\" oprator pattern matching, do not ignore case",
72 "SQL pattern matching allows you to use `_' to match any single character "
73 "and `%' to match an arbitrary number of characters (including zero characters). "
74 "LIKE pattern match succeeds only if the pattern matches the entire value."
88 const char *pszFormat
,
94 char Buffer
[FORMATTING_BUFFER_SIZE
];
96 va_start(argList
, pszFormat
);
97 vsnprintf(Buffer
, FORMATTING_BUFFER_SIZE
-1, pszFormat
, argList
);
102 r
= maybe_enlarge_buffer(
103 &pDB
->pErrorMsgBuffer
,
104 &pDB
->sizeErrorMsgBuffer
,
106 if (MEDIADB_IS_ERROR(r
))
108 if (pDB
->sizeErrorMsgBuffer
> 0)
110 pDB
->pErrorMsgBuffer
[0] = 0;
114 memcpy(pDB
->pErrorMsgBuffer
, Buffer
, s
+ 1);
122 if (pDB
->pErrorMsgBuffer
!= NULL
)
124 pDB
->pErrorMsgBuffer
[0] = 0;
129 mediadb_sqlite_get_error_message(
132 if (pDB
->pErrorMsgBuffer
== NULL
)
137 return pDB
->pErrorMsgBuffer
;
151 pDB
->pErrorMsgBuffer
= NULL
;
152 pDB
->sizeErrorMsgBuffer
= 0;
162 "Failed to open database. Error: %s",
172 reset_error_message(pDB
);
178 mediadb_sqlite_close(
181 if (pDB
->pErrorMsgBuffer
!= NULL
)
183 free(pDB
->pErrorMsgBuffer
);
186 sqlite_close(pDB
->hDB
);
192 mediadb_sqlite_media_add_new(
195 const char *pszComment
,
196 mediadb_mediatype nType
,
197 mediadb_uint
*pnNewMediaID
)
201 my_ulonglong nMediaID
;
203 nRet
= sqlite_exec_printf(
205 "INSERT INTO " MEDIADB_SQLITE_MEDIA_TABLE
206 " (catid,type,added,info1,info2,name,comment,location)"
207 " VALUES (0,%u,0,0,0,'%q','%q','0')",
214 if (nRet
!= SQLITE_OK
)
218 "Failed to execute SQL query. Error: %s",
226 nMediaID
= sqlite_last_insert_rowid(pDB
->hDB
);
227 if (nMediaID
== 0ULL)
231 "Invalid media ID 0 after media INSERT");
235 *pnNewMediaID
= nMediaID
;
237 reset_error_message(pDB
);
243 mediadb_sqlite_media_update_properties(
245 mediadb_uint nMediaID
,
246 mediadb_uint nTimeAdded
,
247 mediadb_uint nTotalFiles
,
248 mediadb_uint nTotalSize
)
253 nRet
= sqlite_exec_printf(
255 "UPDATE " MEDIADB_SQLITE_MEDIA_TABLE
" SET added=%u, info1=%u, info2=%u WHERE mediaid = %u",
259 (unsigned int)nTimeAdded
,
260 (unsigned int)nTotalFiles
,
261 (unsigned int)nTotalSize
,
262 (unsigned int)nMediaID
);
263 if (nRet
!= SQLITE_OK
)
267 "Failed to execute SQL query. Error: %s",
275 reset_error_message(pDB
);
281 mediadb_sqlite_file_add_new(
283 mediadb_uint nMediaID
,
284 mediadb_filetype Filetype
,
287 mediadb_uint nFileSize
,
288 mediadb_uint nFileTime
)
293 if (Filetype
!= MEDIADB_FILETYPE_FILE
&&
294 Filetype
!= MEDIADB_FILETYPE_DIR
)
298 "Cannot add file of unknown type");
299 return MEDIADB_INVAL_ARG
;
302 nRet
= sqlite_exec_printf(
304 "INSERT INTO " MEDIADB_SQLITE_FILES_TABLE
" (mediaid,size,time,path,name) VALUES (%u,%u,%u,'%q','%q%s')",
308 (unsigned int)nMediaID
,
309 (unsigned int)nFileSize
,
310 (unsigned int)nFileTime
,
313 (Filetype
== MEDIADB_FILETYPE_DIR
)?"/":"");
314 if (nRet
!= SQLITE_OK
)
318 "Failed to execute SQL query. Error: %s",
326 reset_error_message(pDB
);
332 mediadb_sqlite_media_get_properties(
334 mediadb_uint nMediaID
,
335 mediadb_uint
*pnTimeAdded
,
336 mediadb_mediatype
*pnType
,
337 mediadb_uint
*pnLocationID
,
345 nRet
= sqlite_get_table_printf(
347 "SELECT added, type, name, location FROM " MEDIADB_SQLITE_MEDIA_TABLE
" WHERE mediaid = %u",
352 (unsigned int)nMediaID
);
353 if (nRet
!= SQLITE_OK
)
357 "Failed to execute SQL query. Error: %s",
370 "SQL query returned unexpected result (%d, %d).",
374 sqlite_free_table(pResult
);
381 if (pResult
[4] == NULL
)
387 *pnTimeAdded
= strtoull(pResult
[4], NULL
, 10);
390 if (pResult
[5] == NULL
)
392 *pnType
= MEDIADB_MT_UNKNOWN
;
396 *pnType
= strtoull(pResult
[5], NULL
, 10);
399 if (pResult
[6] == NULL
)
401 *ppszTitle
= strdup("");
405 *ppszTitle
= strdup(pResult
[6]);
408 if (pResult
[7] == NULL
)
410 *pnLocationID
= MEDIADB_MT_UNKNOWN
;
414 *pnLocationID
= strtoull(pResult
[7], NULL
, 10);
417 sqlite_free_table(pResult
);
419 reset_error_message(pDB
);
425 mediadb_sqlite_location_get_properties(
427 mediadb_uint nLocationID
,
428 mediadb_uint
*pnLocationTypeID
,
429 char **ppszDescription
)
436 nRet
= sqlite_get_table_printf(
438 "SELECT type, description FROM " MEDIADB_SQLITE_LOCATIONS_TABLE
" WHERE id = %u",
443 (unsigned int)nLocationID
);
444 if (nRet
!= SQLITE_OK
)
448 "Failed to execute SQL query. Error: %s",
461 "SQL query returned unexpected result (%d, %d).",
465 sqlite_free_table(pResult
);
472 if (pResult
[2] == NULL
)
474 *pnLocationTypeID
= 0;
478 *pnLocationTypeID
= strtoull(pResult
[2], NULL
, 10);
481 if (pResult
[3] == NULL
)
483 *ppszDescription
= strdup("");
487 *ppszDescription
= strdup(pResult
[3]);
490 sqlite_free_table(pResult
);
492 reset_error_message(pDB
);
498 mediadb_sqlite_location_type_get_properties(
500 mediadb_uint nLocationTypeID
,
501 char **ppszDescription
)
508 nRet
= sqlite_get_table_printf(
510 "SELECT description FROM " MEDIADB_SQLITE_LOCATION_TYPES_TABLE
" WHERE id = %u",
515 (unsigned int)nLocationTypeID
);
516 if (nRet
!= SQLITE_OK
)
520 "Failed to execute SQL query. Error: %s",
533 "SQL query returned unexpected result (%d, %d).",
537 sqlite_free_table(pResult
);
544 if (pResult
[1] == NULL
)
546 *ppszDescription
= strdup("");
550 *ppszDescription
= strdup(pResult
[1]);
553 sqlite_free_table(pResult
);
555 reset_error_message(pDB
);
561 mediadb_sqlite_media_get_properties_data(
563 mediadb_uint nMediaID
,
564 mediadb_uint
*pnTotalFiles
,
565 mediadb_uint
*pnTotalSize
)
572 nRet
= sqlite_get_table_printf(
574 "SELECT info1, info2 FROM " MEDIADB_SQLITE_MEDIA_TABLE
" WHERE mediaid = %u",
579 (unsigned int)nMediaID
);
580 if (nRet
!= SQLITE_OK
)
584 "Failed to execute SQL query. Error: %s",
597 "SQL query returned unexpected result (%d, %d).",
601 sqlite_free_table(pResult
);
608 if (pResult
[2] == NULL
)
614 *pnTotalFiles
= strtoull(pResult
[2], NULL
, 10);
617 if (pResult
[3] == NULL
)
623 *pnTotalSize
= strtoull(pResult
[3], NULL
, 10);
626 sqlite_free_table(pResult
);
628 reset_error_message(pDB
);
634 mediadb_sqlite_delete_media_files(
636 mediadb_uint nMediaID
)
641 nRet
= sqlite_exec_printf(
643 "DELETE FROM " MEDIADB_SQLITE_FILES_TABLE
" WHERE mediaid = %u",
647 (unsigned int)nMediaID
);
648 if (nRet
!= SQLITE_OK
)
652 "Failed to execute SQL query. Error: %s",
660 reset_error_message(pDB
);
666 mediadb_sqlite_media_update_name(
668 mediadb_uint nMediaID
,
674 nRet
= sqlite_exec_printf(
676 "UPDATE " MEDIADB_SQLITE_MEDIA_TABLE
" SET name = '%q' WHERE mediaid = %u",
681 (unsigned int)nMediaID
);
682 if (nRet
!= SQLITE_OK
)
686 "Failed to execute SQL query. Error: %s",
694 reset_error_message(pDB
);
699 struct mediadb_media_callback_context
701 mediadb_media_callback pCallback
;
707 mediadb_sqlite_media_get_callback(
715 const char *pszComment
;
720 mediadb_mediatype nType
;
721 char strLocation
[1000];
723 char *pLocationDescriptionBuffer
;
730 pLocationDescriptionBuffer
= NULL
;
742 nMediaID
= atol(argv
[0]);
752 nType
= atol(argv
[2]);
755 nType
= MEDIADB_MT_EMPTY
;
762 nAdded
= atol(argv
[3]);
769 nTotalFiles
= atol(argv
[4]);
776 nTotalSize
= atol(argv
[5]);
790 pszComment
= argv
[7];
795 nLocationID
= atol(argv
[8]);
799 nRet
= sqlite_get_table_printf(
800 ((struct mediadb_media_callback_context
*) pArg
)->hDB
,
801 "SELECT description FROM " MEDIADB_SQLITE_LOCATIONS_TABLE
" WHERE id = %u",
806 (unsigned int)nLocationID
);
807 if (nRet
!= SQLITE_OK
)
810 "Cannot get description for location with ID \"%ld\" (SQLite error \"%s\")",
816 pszLocation
= strLocation
;
818 else if (nColumns
!= 1 ||
822 "Cannot get description for location with ID \"%ld\" (SQLite query returned unexpected result (%d, %d).",
826 sqlite_free_table(pResult
);
827 pszLocation
= strLocation
;
829 else if (pResult
[1] == NULL
)
832 "Cannot get description for location with ID \"%ld\" (SQLite query returned NULL)",
834 sqlite_free_table(pResult
);
835 pszLocation
= strLocation
;
839 s
= strlen(pResult
[1]);
841 if (s
< sizeof(strLocation
))
843 memcpy(strLocation
, pResult
[1], s
);
845 pszLocation
= strLocation
;
849 pLocationDescriptionBuffer
= malloc(s
+1);
850 if (pLocationDescriptionBuffer
== NULL
)
853 "Cannot get description for location with ID \"%ld\" (out of memory)",
855 pszLocation
= strLocation
;
859 memcpy(pLocationDescriptionBuffer
, pResult
[1], s
);
860 pLocationDescriptionBuffer
[s
] = 0;
861 pszLocation
= pLocationDescriptionBuffer
;
865 sqlite_free_table(pResult
);
868 ((struct mediadb_media_callback_context
*) pArg
)->pCallback(
869 ((struct mediadb_media_callback_context
*) pArg
)->pUserContext
,
870 (mediadb_uint
)nMediaID
,
874 (mediadb_uint
)nAdded
,
875 (mediadb_uint
)nTotalFiles
,
876 (mediadb_uint
)nTotalSize
,
877 (mediadb_uint
)nLocationID
,
880 if (pLocationDescriptionBuffer
!= NULL
)
882 free(pLocationDescriptionBuffer
);
889 mediadb_sqlite_media_get_all(
891 mediadb_media_callback pCallback
,
894 struct mediadb_media_callback_context context
;
898 context
.pCallback
= pCallback
;
899 context
.pUserContext
= pUserContext
;
900 context
.hDB
= pDB
->hDB
;
904 "SELECT * FROM " MEDIADB_SQLITE_MEDIA_TABLE
" ORDER BY mediaid ASC",
905 mediadb_sqlite_media_get_callback
,
908 if (nRet
!= SQLITE_OK
)
912 "Failed to execute SQL query. Error: %s",
920 reset_error_message(pDB
);
925 struct mediadb_files_callback_context
927 mediadb_files_callback pCallback
;
932 mediadb_sqlite_files_get_callback(
942 mediadb_filetype Filetype
= MEDIADB_FILETYPE_FILE
;
967 nSize
= atol(argv
[2]);
973 nTime
= atol(argv
[3]);
977 if (s
> 1 && pszName
[s
-1] == '/')
979 Filetype
= MEDIADB_FILETYPE_DIR
;
983 ((struct mediadb_files_callback_context
*) pArg
)->pCallback(
984 ((struct mediadb_files_callback_context
*) pArg
)->pUserContext
,
989 (mediadb_uint
)nTime
);
995 mediadb_sqlite_files_get(
997 mediadb_uint nMediaID
,
999 mediadb_files_callback pCallback
,
1002 struct mediadb_files_callback_context context
;
1006 context
.pCallback
= pCallback
;
1007 context
.pUserContext
= pUserContext
;
1009 nRet
= sqlite_exec_printf(
1011 "SELECT path, name, size, time FROM " MEDIADB_SQLITE_FILES_TABLE
" WHERE mediaid = %u AND path = '%q/'",
1012 mediadb_sqlite_files_get_callback
,
1015 (unsigned int)nMediaID
,
1016 pszPath
?pszPath
:"");
1017 if (nRet
!= SQLITE_OK
)
1021 "Failed to execute SQL query. Error: %s",
1026 return MEDIADB_FAIL
;
1029 reset_error_message(pDB
);
1035 mediadb_sqlite_get_pattern_match_methods(
1036 mediadb_sqlite
*pDB
,
1037 const struct mediadb_pattern_match_method
**ppPMM
)
1039 *ppPMM
= mediadb_sqlite_pattern_match_methods
;
1043 struct mediadb_files_search_callback_context
1045 mediadb_files_search_callback pCallback
;
1051 mediadb_sqlite_files_search_callback(
1063 mediadb_filetype Filetype
= MEDIADB_FILETYPE_FILE
;
1065 char strMediaName
[1000];
1067 char *pMediaNameBuffer
;
1068 char strLocation
[1000];
1070 char *pLocationDescriptionBuffer
;
1074 int nRows
, nColumns
;
1076 pMediaNameBuffer
= NULL
;
1077 pLocationDescriptionBuffer
= NULL
;
1083 return SQLITE_ABORT
;
1086 if (argv
[0] == NULL
)
1087 return SQLITE_ABORT
;
1089 nMediaID
= atol(argv
[0]);
1092 if (argv
[1] == NULL
)
1093 return SQLITE_ABORT
;
1098 if (argv
[2] == NULL
)
1099 return SQLITE_ABORT
;
1104 if (argv
[3] == NULL
)
1105 return SQLITE_ABORT
;
1107 nSize
= atol(argv
[3]);
1110 if (argv
[4] == NULL
)
1111 return SQLITE_ABORT
;
1113 nTime
= atol(argv
[4]);
1115 s
= strlen(pszName
);
1117 if (s
> 1 && pszName
[s
-1] == '/')
1119 Filetype
= MEDIADB_FILETYPE_DIR
;
1125 nRet
= sqlite_get_table_printf(
1126 ((struct mediadb_files_search_callback_context
*) pArg
)->hDB
,
1127 "SELECT name, location FROM " MEDIADB_SQLITE_MEDIA_TABLE
" WHERE mediaid = %u",
1132 (unsigned int)nMediaID
);
1133 if (nRet
!= SQLITE_OK
)
1135 sprintf(strMediaName
,
1136 "Cannot get name of media with ID \"%ld\" (SQLite error \"%s\")",
1142 pszMediaName
= strMediaName
;
1144 else if (nColumns
!= 2 ||
1147 sprintf(strMediaName
,
1148 "Cannot get name of media ID \"%ld\" (SQLite query returned unexpected result (%d, %d).",
1152 sqlite_free_table(pResult
);
1153 pszMediaName
= strMediaName
;
1157 if (pResult
[2] == NULL
)
1159 sprintf(strMediaName
,
1160 "Cannot get name of media ID \"%ld\" (SQLite query returned NULL)",
1162 sqlite_free_table(pResult
);
1163 pszMediaName
= strMediaName
;
1167 s
= strlen(pResult
[2]);
1169 if (s
< sizeof(strMediaName
))
1171 memcpy(strMediaName
, pResult
[2], s
);
1172 strMediaName
[s
] = 0;
1173 pszMediaName
= strMediaName
;
1177 pMediaNameBuffer
= malloc(s
+1);
1178 if (pMediaNameBuffer
== NULL
)
1180 sprintf(strMediaName
,
1181 "Cannot get description for location with ID \"%ld\" (out of memory)",
1183 pszMediaName
= strMediaName
;
1187 memcpy(pMediaNameBuffer
, pResult
[2], s
);
1188 pMediaNameBuffer
[s
] = 0;
1189 pszMediaName
= pMediaNameBuffer
;
1194 if (pResult
[3] != NULL
)
1196 nLocationID
= atol(pResult
[3]);
1199 sqlite_free_table(pResult
);
1202 if (nLocationID
>= 0)
1204 nRet
= sqlite_get_table_printf(
1205 ((struct mediadb_media_callback_context
*) pArg
)->hDB
,
1206 "SELECT description FROM " MEDIADB_SQLITE_LOCATIONS_TABLE
" WHERE id = %u",
1211 (unsigned int)nLocationID
);
1212 if (nRet
!= SQLITE_OK
)
1214 sprintf(strLocation
,
1215 "Cannot get description for location with ID \"%ld\" (SQLite error \"%s\")",
1221 pszLocation
= strLocation
;
1223 else if (nColumns
!= 1 ||
1226 sprintf(strLocation
,
1227 "Cannot get description for location with ID \"%ld\" (SQLite query returned unexpected result (%d, %d).",
1231 sqlite_free_table(pResult
);
1232 pszLocation
= strLocation
;
1234 else if (pResult
[1] == NULL
)
1236 sprintf(strLocation
,
1237 "Cannot get description for location with ID \"%ld\" (SQLite query returned NULL)",
1239 sqlite_free_table(pResult
);
1240 pszLocation
= strLocation
;
1244 s
= strlen(pResult
[1]);
1246 if (s
< sizeof(strLocation
))
1248 memcpy(strLocation
, pResult
[1], s
);
1250 pszLocation
= strLocation
;
1254 pLocationDescriptionBuffer
= malloc(s
+1);
1255 if (pLocationDescriptionBuffer
== NULL
)
1257 sprintf(strLocation
,
1258 "Cannot get description for location with ID \"%ld\" (out of memory)",
1260 pszLocation
= strLocation
;
1264 memcpy(pLocationDescriptionBuffer
, pResult
[1], s
);
1265 pLocationDescriptionBuffer
[s
] = 0;
1266 pszLocation
= pLocationDescriptionBuffer
;
1270 sqlite_free_table(pResult
);
1275 pszLocation
= "Cannot retrieve location from database";
1278 ((struct mediadb_files_search_callback_context
*) pArg
)->pCallback(
1279 ((struct mediadb_files_search_callback_context
*) pArg
)->pUserContext
,
1287 (mediadb_uint
)nSize
,
1288 (mediadb_uint
)nTime
);
1290 if (pMediaNameBuffer
!= NULL
)
1292 free(pMediaNameBuffer
);
1295 if (pLocationDescriptionBuffer
!= NULL
)
1297 free(pLocationDescriptionBuffer
);
1304 mediadb_sqlite_get_match_keyword(mediadb_uint nPMMID
)
1308 case MEDIADB_SQLITE_PMM_EXACT_CASE_INSENSITIVE
:
1310 case MEDIADB_SQLITE_PMM_LIKE_CASE_INSENSITIVE
:
1315 return "XXX_BUG_XXX";
1319 mediadb_sqlite_files_search(
1320 mediadb_sqlite
*pDB
,
1321 mediadb_uint nFilenamePMMID
,
1322 const char *pszFilenamePattern
,
1323 mediadb_uint nPathPMMID
,
1324 const char *pszPathPattern
,
1325 const mediadb_uint
*pnMinSize
,
1326 const mediadb_uint
*pnMaxSize
,
1327 mediadb_files_search_callback pCallback
,
1330 struct mediadb_files_search_callback_context context
;
1335 size_t sizeWhereAllocated
;
1336 size_t sizeWhereUsed
;
1337 size_t sizeSearchCriteria
;
1343 context
.pCallback
= pCallback
;
1344 context
.pUserContext
= pUserContext
;
1345 context
.hDB
= pDB
->hDB
;
1347 /* Compose SQL WHERE expression */
1350 sizeWhereAllocated
= 0;
1352 sizeSearchCriteria
= 0;
1355 sizeAND
= strlen(pszAND
);
1357 if (nFilenamePMMID
!= MEDIADB_PMM_NULL
)
1362 if (nPathPMMID
!= MEDIADB_PMM_NULL
)
1367 if (pnMinSize
!= NULL
)
1372 if (pnMaxSize
!= NULL
)
1377 if (nFilenamePMMID
!= MEDIADB_PMM_NULL
)
1381 pszTemp
= sqlite_mprintf(
1383 mediadb_sqlite_get_match_keyword(nFilenamePMMID
),
1384 pszFilenamePattern
);
1385 if (pszTemp
== NULL
)
1391 sizeSearchCriteria
= strlen(pszTemp
);
1392 r
= maybe_enlarge_buffer(
1394 &sizeWhereAllocated
,
1395 sizeWhereUsed
+ sizeSearchCriteria
+ (nCount
== 0?0:sizeAND
));
1396 if (MEDIADB_IS_ERROR(r
))
1401 sizeWhereUsed
+= sprintf(
1402 pszWhere
+ sizeWhereUsed
,
1405 nCount
== 0?"":pszAND
);
1407 sqlite_freemem(pszTemp
);
1410 if (nPathPMMID
!= MEDIADB_PMM_NULL
)
1414 pszTemp
= sqlite_mprintf(
1416 mediadb_sqlite_get_match_keyword(nPathPMMID
),
1418 if (pszTemp
== NULL
)
1424 sizeSearchCriteria
= strlen(pszTemp
);
1425 r
= maybe_enlarge_buffer(
1427 &sizeWhereAllocated
,
1428 sizeWhereUsed
+ sizeSearchCriteria
+ (nCount
== 0?0:sizeAND
));
1429 if (MEDIADB_IS_ERROR(r
))
1434 sizeWhereUsed
+= sprintf(
1435 pszWhere
+ sizeWhereUsed
,
1438 nCount
== 0?"":pszAND
);
1440 sqlite_freemem(pszTemp
);
1443 if (pnMinSize
!= NULL
)
1447 pszTemp
= sqlite_mprintf(
1449 (unsigned int)*pnMinSize
);
1450 if (pszTemp
== NULL
)
1456 sizeSearchCriteria
= strlen(pszTemp
);
1457 r
= maybe_enlarge_buffer(
1459 &sizeWhereAllocated
,
1460 sizeWhereUsed
+ sizeSearchCriteria
+ (nCount
== 0?0:sizeAND
));
1461 if (MEDIADB_IS_ERROR(r
))
1466 sizeWhereUsed
+= sprintf(
1467 pszWhere
+ sizeWhereUsed
,
1470 nCount
== 0?"":pszAND
);
1472 sqlite_freemem(pszTemp
);
1475 if (pnMaxSize
!= NULL
)
1479 pszTemp
= sqlite_mprintf(
1481 (unsigned int)*pnMaxSize
);
1482 if (pszTemp
== NULL
)
1488 sizeSearchCriteria
= strlen(pszTemp
);
1489 r
= maybe_enlarge_buffer(
1491 &sizeWhereAllocated
,
1492 sizeWhereUsed
+ sizeSearchCriteria
+ (nCount
== 0?0:sizeAND
));
1493 if (MEDIADB_IS_ERROR(r
))
1498 sizeWhereUsed
+= sprintf(
1499 pszWhere
+ sizeWhereUsed
,
1502 nCount
== 0?"":pszAND
);
1504 sqlite_freemem(pszTemp
);
1507 if (pszWhere
== NULL
)
1509 /* Nothing to search - empty result */
1513 assert(nCount
== 0);
1515 /* Execute SQl query */
1516 nRet
= sqlite_exec_printf(
1518 "SELECT mediaid, path, name, size, time FROM " MEDIADB_SQLITE_FILES_TABLE
" WHERE %s",
1519 mediadb_sqlite_files_search_callback
,
1523 if (nRet
!= SQLITE_OK
)
1527 "Failed to execute SQL query. Error: %s",
1533 return MEDIADB_FAIL
;
1538 reset_error_message(pDB
);
1543 sqlite_freemem(pszTemp
);
1546 if (pszWhere
!= NULL
)
1554 /*****************************************************************************
1556 * Modifications log:
1558 * !!! WARNING !!! Following lines are automatically updated by the CVS system.
1560 * $Log: sqlite.c,v $
1561 * Revision 1.11 2004/09/18 21:32:34 nedko
1562 * Use min and max size when searching files.
1563 * Return media name, media type and media location when searching files.
1565 * Revision 1.10 2004/09/01 05:02:17 nedko
1566 * Implement search by filename and path
1568 * Revision 1.9 2004/08/31 22:40:15 nedko
1569 * Partitally implemented search feature.
1571 * Revision 1.8 2004/08/08 00:47:42 nedko
1572 * Get more info for media from database.
1574 * Revision 1.7 2004/05/21 23:40:46 nedko
1575 * New functionality: mediadb_files_get()
1577 * Revision 1.6 2004/05/16 18:57:12 nedko
1578 * media_get_all functionality implemented.
1580 * Revision 1.5 2004/05/11 01:18:53 nedko
1581 * Implement SQLite backend.
1583 * Revision 1.4 2004/05/03 20:47:15 nedko
1584 * Update mode for cui
1586 * Revision 1.3 2004/05/02 20:12:11 nedko
1587 * Improve error dumps.
1589 * Revision 1.2 2004/05/02 13:16:58 nedko
1590 * Supply filetype when adding new file
1592 * Revision 1.1 2004/04/27 09:12:28 nedko
1595 *****************************************************************************/