1 /* -*- Mode: C ; c-basic-offset: 2 -*- */
2 /*****************************************************************************
4 * $Id: mysql.c,v 1.12 2004/10/03 22:03:11 nedko Exp $
7 * MySQL backend implementation.
10 * Nedko Arnaudov <nedko@users.sourceforge.net>
13 * GNU GENERAL PUBLIC LICENSE version 2
15 *****************************************************************************/
23 #include "../result.h"
27 #define MEDIADB_MYSQL_MEDIA_TABLE "media"
28 #define MEDIADB_MYSQL_FILES_TABLE "files"
29 #define MEDIADB_MYSQL_LOCATIONS_TABLE "locations"
30 #define MEDIADB_MYSQL_LOCATION_TYPES_TABLE "location_types"
32 //#define DUMMY_SQL_QUERIES
33 #define FORMATTING_BUFFER_SIZE 10240
35 #define MEDIADB_MYSQL_PMM_EXACT_CASE_INSENSITIVE 1
36 #define MEDIADB_MYSQL_PMM_EXACT_CASE_SENSITIVE 2
37 #define MEDIADB_MYSQL_PMM_LIKE_CASE_INSENSITIVE 3
38 #define MEDIADB_MYSQL_PMM_LIKE_CASE_SENSITIVE 4
39 #define MEDIADB_MYSQL_PMM_REGEXP_CASE_INSENSITIVE 5
40 #define MEDIADB_MYSQL_PMM_REGEXP_CASE_SENSITIVE 6
42 static struct mediadb_pattern_match_method
43 mediadb_mysql_pattern_match_methods
[] =
46 MEDIADB_MYSQL_PMM_REGEXP_CASE_INSENSITIVE
,
47 "ERE - case insensitive",
48 "Extended regular expressions, ignore case",
49 "Extended regular expressions are powerful pattern matching method.\n"
50 "Some characteristics of extended regular expressions are:\n"
51 " * `.' matches any single character.\n"
52 " * A character class `[...]' matches any character within the brackets. "
53 " For example, `[abc]' matches `a', `b', or `c'. To name a range of characters, "
54 " use a dash. `[a-z]' matches any letter, whereas `[0-9]' matches any digit.\n"
55 " * `*' matches zero or more instances of the thing preceding it. "
56 " For example, `x*' matches any number of `x' characters, `[0-9]*' matches any "
57 " number of digits, and `.*' matches any number of anything.\n"
58 " * A REGEXP pattern match succeed if the pattern matches anywhere in the value "
60 " * To anchor a pattern so that it must match the beginning or end of the value "
61 " being tested, use `^' at the beginning or `$' at the end of the pattern."
64 MEDIADB_MYSQL_PMM_REGEXP_CASE_SENSITIVE
,
65 "ERE - case sensitive",
66 "Extended regular expressions, do not ignore case",
67 "Extended regular expressions are powerful pattern matching method.\n"
68 "Some characteristics of extended regular expressions are:\n"
69 " * `.' matches any single character.\n"
70 " * A character class `[...]' matches any character within the brackets. "
71 " For example, `[abc]' matches `a', `b', or `c'. To name a range of characters, "
72 " use a dash. `[a-z]' matches any letter, whereas `[0-9]' matches any digit.\n"
73 " * `*' matches zero or more instances of the thing preceding it. "
74 " For example, `x*' matches any number of `x' characters, `[0-9]*' matches any "
75 " number of digits, and `.*' matches any number of anything.\n"
76 " * A REGEXP pattern match succeed if the pattern matches anywhere in the value "
78 " * To anchor a pattern so that it must match the beginning or end of the value "
79 " being tested, use `^' at the beginning or `$' at the end of the pattern."
82 MEDIADB_MYSQL_PMM_LIKE_CASE_INSENSITIVE
,
83 "SQL \"LIKE\" - case insensitive",
84 "Traditional SQL \"LIKE\" oprator pattern matching, ignore case",
85 "SQL pattern matching allows you to use `_' to match any single character "
86 "and `%' to match an arbitrary number of characters (including zero characters). "
87 "LIKE pattern match succeeds only if the pattern matches the entire value."
90 MEDIADB_MYSQL_PMM_LIKE_CASE_SENSITIVE
,
91 "SQL \"LIKE\" - case sensitive",
92 "Traditional SQL \"LIKE\" oprator pattern matching, do not ignore case",
93 "SQL pattern matching allows you to use `_' to match any single character "
94 "and `%' to match an arbitrary number of characters (including zero characters). "
95 "LIKE pattern match succeeds only if the pattern matches the entire value."
98 MEDIADB_MYSQL_PMM_EXACT_CASE_INSENSITIVE
,
99 "exact - case insensitive",
100 "Match exactly, ignore case",
104 MEDIADB_MYSQL_PMM_EXACT_CASE_SENSITIVE
,
105 "exact - case sensitive",
106 "Match exactly, do not ignore case",
120 const char *pszFormat
,
126 char Buffer
[FORMATTING_BUFFER_SIZE
];
128 va_start(argList
, pszFormat
);
129 vsnprintf(Buffer
, FORMATTING_BUFFER_SIZE
-1, pszFormat
, argList
);
134 r
= maybe_enlarge_buffer(
135 &pDB
->pErrorMsgBuffer
,
136 &pDB
->sizeErrorMsgBuffer
,
138 if (MEDIADB_IS_ERROR(r
))
140 if (pDB
->sizeErrorMsgBuffer
> 0)
142 pDB
->pErrorMsgBuffer
[0] = 0;
146 memcpy(pDB
->pErrorMsgBuffer
, Buffer
, s
+ 1);
154 if (pDB
->pErrorMsgBuffer
!= NULL
)
156 pDB
->pErrorMsgBuffer
[0] = 0;
161 mediadb_mysql_get_error_message(
164 if (pDB
->pErrorMsgBuffer
== NULL
)
169 return pDB
->pErrorMsgBuffer
;
182 pDB
->pszSQLQueryBuffer
= NULL
;
183 pDB
->sizeSQLQueryBuffer
= 0;
184 pDB
->pErrorMsgBuffer
= NULL
;
185 pDB
->sizeErrorMsgBuffer
= 0;
187 pMYSQL
= mysql_init(NULL
);
192 "Cannot create MYSQL object. Error is \"%s\"",
193 mysql_error(pMYSQL
));
197 if (mysql_real_connect(
209 "Failed to connect to database. Error: %s",
210 mysql_error(pMYSQL
));
215 pDB
->pMYSQL
= pMYSQL
;
217 reset_error_message(pDB
);
223 mediadb_mysql_media_add_new(
226 const char *pszComment
,
227 mediadb_mediatype nType
,
228 mediadb_uint
*pnNewMediaID
)
231 size_t sizeName
, sizeComment
, sizePart
;
233 my_ulonglong nMediaID
;
235 /* Ensure we have large enough buffer */
237 sizeName
= strlen(pszName
);
238 sizeComment
= strlen(pszComment
);
240 r
= maybe_enlarge_buffer(
241 &pDB
->pszSQLQueryBuffer
,
242 &pDB
->sizeSQLQueryBuffer
,
243 sizeName
*2 + sizeComment
*2 + 1024);
244 if (MEDIADB_IS_ERROR(r
))
248 "Cannot enlarge buffer");
252 /* Compose SQL query string */
254 psz
= pDB
->pszSQLQueryBuffer
;
256 pszPart
= "INSERT INTO " MEDIADB_MYSQL_MEDIA_TABLE
" (name,type,comment) VALUES ('";
257 sizePart
= strlen(pszPart
);
258 memcpy(psz
, pszPart
, sizePart
);
261 sizePart
= mysql_real_escape_string(
268 sizePart
= sprintf(psz
, "',%u,'", (unsigned int)nType
);
271 sizePart
= mysql_real_escape_string(
279 sizePart
= strlen(pszPart
);
280 memcpy(psz
, pszPart
, sizePart
);
285 /* Execute SQL query */
287 #ifdef DUMMY_SQL_QUERIES
288 printf("MySQL query \"%s\"\n", pDB
->pszSQLQueryBuffer
);
291 if (mysql_query(pDB
->pMYSQL
, pDB
->pszSQLQueryBuffer
) != 0)
295 "Failed to execute query \"%s\" Error: %s",
296 pDB
->pszSQLQueryBuffer
,
297 mysql_error(pDB
->pMYSQL
));
301 nMediaID
= mysql_insert_id(pDB
->pMYSQL
);
302 if (nMediaID
== 0ULL)
306 "Invalid media ID 0 after media INSERT");
311 *pnNewMediaID
= nMediaID
;
313 reset_error_message(pDB
);
319 mediadb_mysql_media_update_properties(
321 mediadb_uint nMediaID
,
322 mediadb_uint nTimeAdded
,
323 mediadb_uint nTotalFiles
,
324 mediadb_uint nTotalSize
)
328 /* Ensure we have large enough buffer */
330 r
= maybe_enlarge_buffer(
331 &pDB
->pszSQLQueryBuffer
,
332 &pDB
->sizeSQLQueryBuffer
,
334 if (MEDIADB_IS_ERROR(r
))
338 "Cannot enlarge buffer");
342 /* Compose SQL query string */
345 pDB
->pszSQLQueryBuffer
,
346 "UPDATE " MEDIADB_MYSQL_MEDIA_TABLE
" SET added=%u, info1=%u, info2=%u WHERE mediaid = %u",
347 (unsigned int)nTimeAdded
,
348 (unsigned int)nTotalFiles
,
349 (unsigned int)nTotalSize
,
350 (unsigned int)nMediaID
);
352 /* Execute SQL query */
354 #ifdef DUMMY_SQL_QUERIES
355 printf("MySQL query \"%s\"\n", pDB
->pszSQLQueryBuffer
);
357 if (mysql_query(pDB
->pMYSQL
, pDB
->pszSQLQueryBuffer
) != 0)
361 "Failed to execute query \"%s\" Error: %s",
362 pDB
->pszSQLQueryBuffer
,
363 mysql_error(pDB
->pMYSQL
));
368 reset_error_message(pDB
);
374 mediadb_mysql_file_add_new(
376 mediadb_uint nMediaID
,
377 mediadb_filetype Filetype
,
380 mediadb_uint nFileSize
,
381 mediadb_uint nFileTime
)
384 size_t sizePath
, sizeName
, sizePart
;
387 /* Ensure we have large enough buffer */
389 sizePath
= strlen(pszPath
);
390 sizeName
= strlen(pszName
);
392 r
= maybe_enlarge_buffer(
393 &pDB
->pszSQLQueryBuffer
,
394 &pDB
->sizeSQLQueryBuffer
,
395 sizePath
*2 + sizeName
*2 + 1024);
396 if (MEDIADB_IS_ERROR(r
))
400 "Cannot enlarge buffer");
404 /* Compose SQL query string */
406 psz
= pDB
->pszSQLQueryBuffer
;
410 "INSERT INTO " MEDIADB_MYSQL_FILES_TABLE
" (mediaid,size,time,path,name) VALUES (%u,%u,%u,'",
411 (unsigned int)nMediaID
,
412 (unsigned int)nFileSize
,
413 (unsigned int)nFileTime
);
416 sizePart
= mysql_real_escape_string(
424 sizePart
= strlen(pszPart
);
425 memcpy(psz
, pszPart
, sizePart
);
428 sizePart
= mysql_real_escape_string(
435 if (Filetype
== MEDIADB_FILETYPE_DIR
)
438 sizePart
= strlen(pszPart
);
439 memcpy(psz
, pszPart
, sizePart
);
442 else if (Filetype
!= MEDIADB_FILETYPE_FILE
)
446 "Cannot add file of unknown type");
447 return MEDIADB_INVAL_ARG
;
451 sizePart
= strlen(pszPart
);
452 memcpy(psz
, pszPart
, sizePart
);
457 /* Execute SQL query */
459 #ifdef DUMMY_SQL_QUERIES
460 printf("MySQL query \"%s\"\n", pDB
->pszSQLQueryBuffer
);
462 if (mysql_query(pDB
->pMYSQL
, pDB
->pszSQLQueryBuffer
) != 0)
466 "Failed to execute query \"%s\" Error: %s",
467 pDB
->pszSQLQueryBuffer
,
468 mysql_error(pDB
->pMYSQL
));
473 reset_error_message(pDB
);
479 mediadb_mysql_media_get_properties(
481 mediadb_uint nMediaID
,
482 mediadb_uint
*pnTimeAdded
,
483 mediadb_mediatype
*pnType
,
484 mediadb_uint
*pnLocationID
,
488 MYSQL_RES
* sqlresult
;
489 my_ulonglong nRowCount
;
492 /* Ensure we have large enough buffer */
494 r
= maybe_enlarge_buffer(
495 &pDB
->pszSQLQueryBuffer
,
496 &pDB
->sizeSQLQueryBuffer
,
498 if (MEDIADB_IS_ERROR(r
))
502 "Cannot enlarge buffer");
506 /* Compose SQL query string */
509 pDB
->pszSQLQueryBuffer
,
510 "SELECT added, type, name, location FROM " MEDIADB_MYSQL_MEDIA_TABLE
" WHERE mediaid = %u",
511 (unsigned int)nMediaID
);
513 /* Execute SQL query */
515 #ifdef DUMMY_SQL_QUERIES
516 printf("MySQL query \"%s\"\n", pDB
->pszSQLQueryBuffer
);
518 if (mysql_query(pDB
->pMYSQL
, pDB
->pszSQLQueryBuffer
) != 0)
522 "Failed to execute query \"%s\" Error: %s",
523 pDB
->pszSQLQueryBuffer
,
524 mysql_error(pDB
->pMYSQL
));
529 sqlresult
= mysql_store_result(pDB
->pMYSQL
);
530 if (sqlresult
== NULL
)
534 "Failed to store result after query \"%s\" Error: %s",
535 pDB
->pszSQLQueryBuffer
,
536 mysql_error(pDB
->pMYSQL
));
540 nRowCount
= mysql_num_rows(sqlresult
);
545 "%u rows, insted of 1, after query \"%s\"",
546 (unsigned int)nRowCount
,
547 pDB
->pszSQLQueryBuffer
);
549 mysql_free_result(sqlresult
);
554 sqlrow
= mysql_fetch_row(sqlresult
);
559 "Failed to fetch row after query \"%s\"",
560 pDB
->pszSQLQueryBuffer
);
562 mysql_free_result(sqlresult
);
567 if (sqlrow
[0] == NULL
)
573 *pnTimeAdded
= strtoull(sqlrow
[0], NULL
, 10);
576 if (sqlrow
[1] == NULL
)
578 *pnType
= MEDIADB_MT_UNKNOWN
;
582 *pnType
= strtoull(sqlrow
[1], NULL
, 10);
585 if (sqlrow
[2] == NULL
)
587 *ppszTitle
= strdup("");
591 *ppszTitle
= strdup(sqlrow
[2]);
594 if (sqlrow
[3] == NULL
)
600 *pnLocationID
= strtoull(sqlrow
[3], NULL
, 10);
603 mysql_free_result(sqlresult
);
605 if (*ppszTitle
== NULL
)
613 reset_error_message(pDB
);
619 mediadb_mysql_location_get_properties(
621 mediadb_uint nLocationID
,
622 mediadb_uint
*pnLocationTypeID
,
623 char **ppszDescription
)
626 MYSQL_RES
* sqlresult
;
627 my_ulonglong nRowCount
;
630 /* Ensure we have large enough buffer */
632 r
= maybe_enlarge_buffer(
633 &pDB
->pszSQLQueryBuffer
,
634 &pDB
->sizeSQLQueryBuffer
,
636 if (MEDIADB_IS_ERROR(r
))
640 "Cannot enlarge buffer");
644 /* Compose SQL query string */
647 pDB
->pszSQLQueryBuffer
,
648 "SELECT type, description FROM " MEDIADB_MYSQL_LOCATIONS_TABLE
" WHERE id = %u",
649 (unsigned int)nLocationID
);
651 /* Execute SQL query */
653 #ifdef DUMMY_SQL_QUERIES
654 printf("MySQL query \"%s\"\n", pDB
->pszSQLQueryBuffer
);
656 if (mysql_query(pDB
->pMYSQL
, pDB
->pszSQLQueryBuffer
) != 0)
660 "Failed to execute query \"%s\" Error: %s",
661 pDB
->pszSQLQueryBuffer
,
662 mysql_error(pDB
->pMYSQL
));
667 sqlresult
= mysql_store_result(pDB
->pMYSQL
);
668 if (sqlresult
== NULL
)
672 "Failed to store result after query \"%s\" Error: %s",
673 pDB
->pszSQLQueryBuffer
,
674 mysql_error(pDB
->pMYSQL
));
678 nRowCount
= mysql_num_rows(sqlresult
);
683 "%u rows, insted of 1, after query \"%s\"",
684 (unsigned int)nRowCount
,
685 pDB
->pszSQLQueryBuffer
);
687 mysql_free_result(sqlresult
);
692 sqlrow
= mysql_fetch_row(sqlresult
);
697 "Failed to fetch row after query \"%s\"",
698 pDB
->pszSQLQueryBuffer
);
700 mysql_free_result(sqlresult
);
705 if (sqlrow
[0] == NULL
)
707 *pnLocationTypeID
= 0;
711 *pnLocationTypeID
= strtoull(sqlrow
[0], NULL
, 10);
714 if (sqlrow
[1] == NULL
)
716 *ppszDescription
= strdup("");
720 *ppszDescription
= strdup(sqlrow
[1]);
723 mysql_free_result(sqlresult
);
725 if (*ppszDescription
== NULL
)
733 reset_error_message(pDB
);
739 mediadb_mysql_location_type_get_properties(
741 mediadb_uint nLocationTypeID
,
742 char **ppszDescription
)
745 MYSQL_RES
* sqlresult
;
746 my_ulonglong nRowCount
;
749 /* Ensure we have large enough buffer */
751 r
= maybe_enlarge_buffer(
752 &pDB
->pszSQLQueryBuffer
,
753 &pDB
->sizeSQLQueryBuffer
,
755 if (MEDIADB_IS_ERROR(r
))
759 "Cannot enlarge buffer");
763 /* Compose SQL query string */
766 pDB
->pszSQLQueryBuffer
,
767 "SELECT description FROM " MEDIADB_MYSQL_LOCATION_TYPES_TABLE
" WHERE id = %u",
768 (unsigned int)nLocationTypeID
);
770 /* Execute SQL query */
772 #ifdef DUMMY_SQL_QUERIES
773 printf("MySQL query \"%s\"\n", pDB
->pszSQLQueryBuffer
);
775 if (mysql_query(pDB
->pMYSQL
, pDB
->pszSQLQueryBuffer
) != 0)
779 "Failed to execute query \"%s\" Error: %s",
780 pDB
->pszSQLQueryBuffer
,
781 mysql_error(pDB
->pMYSQL
));
786 sqlresult
= mysql_store_result(pDB
->pMYSQL
);
787 if (sqlresult
== NULL
)
791 "Failed to store result after query \"%s\" Error: %s",
792 pDB
->pszSQLQueryBuffer
,
793 mysql_error(pDB
->pMYSQL
));
797 nRowCount
= mysql_num_rows(sqlresult
);
802 "%u rows, insted of 1, after query \"%s\"",
803 (unsigned int)nRowCount
,
804 pDB
->pszSQLQueryBuffer
);
806 mysql_free_result(sqlresult
);
811 sqlrow
= mysql_fetch_row(sqlresult
);
816 "Failed to fetch row after query \"%s\"",
817 pDB
->pszSQLQueryBuffer
);
819 mysql_free_result(sqlresult
);
824 if (sqlrow
[0] == NULL
)
826 *ppszDescription
= strdup("");
830 *ppszDescription
= strdup(sqlrow
[0]);
833 mysql_free_result(sqlresult
);
835 if (*ppszDescription
== NULL
)
843 reset_error_message(pDB
);
849 mediadb_mysql_media_get_properties_data(
851 mediadb_uint nMediaID
,
852 mediadb_uint
*pnTotalFiles
,
853 mediadb_uint
*pnTotalSize
)
856 MYSQL_RES
* sqlresult
;
857 my_ulonglong nRowCount
;
860 /* Ensure we have large enough buffer */
862 r
= maybe_enlarge_buffer(
863 &pDB
->pszSQLQueryBuffer
,
864 &pDB
->sizeSQLQueryBuffer
,
866 if (MEDIADB_IS_ERROR(r
))
870 "Cannot enlarge buffer");
874 /* Compose SQL query string */
877 pDB
->pszSQLQueryBuffer
,
878 "SELECT info1, info2 FROM " MEDIADB_MYSQL_MEDIA_TABLE
" WHERE mediaid = %u",
879 (unsigned int)nMediaID
);
881 /* Execute SQL query */
883 #ifdef DUMMY_SQL_QUERIES
884 printf("MySQL query \"%s\"\n", pDB
->pszSQLQueryBuffer
);
886 if (mysql_query(pDB
->pMYSQL
, pDB
->pszSQLQueryBuffer
) != 0)
890 "Failed to execute query \"%s\" Error: %s",
891 pDB
->pszSQLQueryBuffer
,
892 mysql_error(pDB
->pMYSQL
));
897 sqlresult
= mysql_store_result(pDB
->pMYSQL
);
898 if (sqlresult
== NULL
)
902 "Failed to store result after query \"%s\" Error: %s",
903 pDB
->pszSQLQueryBuffer
,
904 mysql_error(pDB
->pMYSQL
));
908 nRowCount
= mysql_num_rows(sqlresult
);
913 "%u rows, insted of 1, after query \"%s\"",
914 (unsigned int)nRowCount
,
915 pDB
->pszSQLQueryBuffer
);
917 mysql_free_result(sqlresult
);
922 sqlrow
= mysql_fetch_row(sqlresult
);
927 "Failed to fetch row after query \"%s\"",
928 pDB
->pszSQLQueryBuffer
);
930 mysql_free_result(sqlresult
);
935 if (sqlrow
[0] == NULL
)
941 *pnTotalFiles
= strtoull(sqlrow
[0], NULL
, 10);
944 if (sqlrow
[1] == NULL
)
950 *pnTotalSize
= strtoull(sqlrow
[1], NULL
, 10);
953 mysql_free_result(sqlresult
);
955 reset_error_message(pDB
);
961 mediadb_mysql_delete_media_files(
963 mediadb_uint nMediaID
)
967 /* Ensure we have large enough buffer */
969 r
= maybe_enlarge_buffer(
970 &pDB
->pszSQLQueryBuffer
,
971 &pDB
->sizeSQLQueryBuffer
,
973 if (MEDIADB_IS_ERROR(r
))
977 "Cannot enlarge buffer");
981 /* Compose SQL query string */
984 pDB
->pszSQLQueryBuffer
,
985 "DELETE FROM " MEDIADB_MYSQL_FILES_TABLE
" WHERE mediaid = %u",
986 (unsigned int)nMediaID
);
988 /* Execute SQL query */
990 #ifdef DUMMY_SQL_QUERIES
991 printf("MySQL query \"%s\"\n", pDB
->pszSQLQueryBuffer
);
993 if (mysql_query(pDB
->pMYSQL
, pDB
->pszSQLQueryBuffer
) != 0)
997 "Failed to execute query \"%s\" Error: %s",
998 pDB
->pszSQLQueryBuffer
,
999 mysql_error(pDB
->pMYSQL
));
1000 return MEDIADB_FAIL
;
1004 reset_error_message(pDB
);
1010 mediadb_mysql_media_update_name(
1012 mediadb_uint nMediaID
,
1013 const char *pszName
)
1016 size_t sizeName
, sizePart
;
1017 char *psz
, *pszPart
;
1019 /* Ensure we have large enough buffer */
1021 sizeName
= strlen(pszName
);
1023 r
= maybe_enlarge_buffer(
1024 &pDB
->pszSQLQueryBuffer
,
1025 &pDB
->sizeSQLQueryBuffer
,
1027 if (MEDIADB_IS_ERROR(r
))
1031 "Cannot enlarge buffer");
1035 /* Compose SQL query string */
1037 psz
= pDB
->pszSQLQueryBuffer
;
1039 pszPart
= "UPDATE " MEDIADB_MYSQL_MEDIA_TABLE
" SET name = '";
1040 sizePart
= strlen(pszPart
);
1041 memcpy(psz
, pszPart
, sizePart
);
1044 sizePart
= mysql_real_escape_string(
1051 sizePart
= sprintf(psz
, "' WHERE mediaid = %u", (unsigned int)nMediaID
);
1054 /* Execute SQL query */
1056 #ifdef DUMMY_SQL_QUERIES
1057 printf("MySQL query \"%s\"\n", pDB
->pszSQLQueryBuffer
);
1059 if (mysql_query(pDB
->pMYSQL
, pDB
->pszSQLQueryBuffer
) != 0)
1063 "Failed to execute query \"%s\" Error: %s",
1064 pDB
->pszSQLQueryBuffer
,
1065 mysql_error(pDB
->pMYSQL
));
1066 return MEDIADB_FAIL
;
1070 reset_error_message(pDB
);
1076 mediadb_mysql_close(
1079 if (pDB
->pszSQLQueryBuffer
!= NULL
)
1081 free(pDB
->pszSQLQueryBuffer
);
1084 if (pDB
->pErrorMsgBuffer
!= NULL
)
1086 free(pDB
->pErrorMsgBuffer
);
1089 mysql_close(pDB
->pMYSQL
);
1095 mediadb_mysql_media_get_all(
1097 mediadb_media_callback pCallback
,
1101 MYSQL_RES
* sqlresult
;
1102 my_ulonglong nRowCount
;
1104 mediadb_uint nMediaID
;
1105 const char *pszName
;
1106 const char *pszComment
;
1107 mediadb_uint nAdded
;
1108 mediadb_uint nTotalFiles
;
1109 mediadb_uint nTotalSize
;
1110 mediadb_uint nLocationID
;
1111 mediadb_mediatype nType
;
1114 /* Ensure we have large enough buffer */
1116 r
= maybe_enlarge_buffer(
1117 &pDB
->pszSQLQueryBuffer
,
1118 &pDB
->sizeSQLQueryBuffer
,
1120 if (MEDIADB_IS_ERROR(r
))
1124 "Cannot enlarge buffer");
1128 /* Compose SQL query string */
1130 strcpy(pDB
->pszSQLQueryBuffer
,
1131 "SELECT m.mediaid, m.added, m.info1, m.info2, "
1132 "m.name, m.comment, l.id, l.description, m.type FROM "
1133 MEDIADB_MYSQL_MEDIA_TABLE
" AS m, "
1134 MEDIADB_MYSQL_LOCATIONS_TABLE
" AS l "
1135 "WHERE m.location = l.id "
1136 "ORDER BY m.mediaid ASC");
1138 /* Execute SQL query */
1140 #ifdef DUMMY_SQL_QUERIES
1141 printf("MySQL query \"%s\"\n", pDB
->pszSQLQueryBuffer
);
1143 if (mysql_query(pDB
->pMYSQL
, pDB
->pszSQLQueryBuffer
) != 0)
1147 "Failed to execute query \"%s\" Error: %s",
1148 pDB
->pszSQLQueryBuffer
,
1149 mysql_error(pDB
->pMYSQL
));
1150 return MEDIADB_FAIL
;
1154 sqlresult
= mysql_store_result(pDB
->pMYSQL
);
1155 if (sqlresult
== NULL
)
1159 "Failed to store result after query \"%s\" Error: %s",
1160 pDB
->pszSQLQueryBuffer
,
1161 mysql_error(pDB
->pMYSQL
));
1162 return MEDIADB_FAIL
;
1165 nRowCount
= mysql_num_rows(sqlresult
);
1169 sqlrow
= mysql_fetch_row(sqlresult
);
1174 "Failed to fetch row after query \"%s\"",
1175 pDB
->pszSQLQueryBuffer
);
1177 mysql_free_result(sqlresult
);
1179 return MEDIADB_FAIL
;
1183 if (sqlrow
[0] == NULL
)
1189 nMediaID
= strtoull(sqlrow
[0], NULL
, 10);
1193 if (sqlrow
[1] == NULL
)
1199 nAdded
= strtoull(sqlrow
[1], NULL
, 10);
1203 if (sqlrow
[2] == NULL
)
1209 nTotalFiles
= strtoull(sqlrow
[2], NULL
, 10);
1213 if (sqlrow
[3] == NULL
)
1219 nTotalSize
= strtoull(sqlrow
[3], NULL
, 10);
1223 if (sqlrow
[4] == NULL
)
1229 pszName
= sqlrow
[4];
1233 if (sqlrow
[5] == NULL
)
1239 pszComment
= sqlrow
[5];
1243 if (sqlrow
[6] == NULL
)
1249 nLocationID
= strtoull(sqlrow
[6], NULL
, 10);
1253 if (sqlrow
[7] == NULL
)
1259 pszLocation
= sqlrow
[7];
1263 if (sqlrow
[8] == NULL
)
1265 nType
= MEDIADB_MT_EMPTY
;
1269 nType
= strtoull(sqlrow
[8], NULL
, 10);
1285 mysql_free_result(sqlresult
);
1287 reset_error_message(pDB
);
1293 mediadb_mysql_files_get(
1295 mediadb_uint nMediaID
,
1296 const char *pszPath
,
1297 mediadb_files_callback pCallback
,
1301 MYSQL_RES
* sqlresult
;
1302 my_ulonglong nRowCount
;
1308 mediadb_filetype nType
;
1309 size_t sizePath
, sizePart
, s
;
1310 char *psz
, *pszPart
;
1312 if (pszPath
== NULL
)
1317 sizePath
= strlen(pszPath
);
1319 /* Ensure we have large enough buffer */
1321 r
= maybe_enlarge_buffer(
1322 &pDB
->pszSQLQueryBuffer
,
1323 &pDB
->sizeSQLQueryBuffer
,
1325 if (MEDIADB_IS_ERROR(r
))
1329 "Cannot enlarge buffer");
1333 /* Compose SQL query string */
1335 psz
= pDB
->pszSQLQueryBuffer
;
1339 "SELECT name, size, time, path FROM "
1340 MEDIADB_MYSQL_FILES_TABLE
1341 " WHERE mediaid = %u AND path = '", (unsigned int)nMediaID
);
1344 sizePart
= mysql_real_escape_string(
1352 sizePart
= strlen(pszPart
);
1353 memcpy(psz
, pszPart
, sizePart
);
1358 /* Execute SQL query */
1360 #ifdef DUMMY_SQL_QUERIES
1361 printf("MySQL query \"%s\"\n", pDB
->pszSQLQueryBuffer
);
1363 if (mysql_query(pDB
->pMYSQL
, pDB
->pszSQLQueryBuffer
) != 0)
1367 "Failed to execute query \"%s\" Error: %s",
1368 pDB
->pszSQLQueryBuffer
,
1369 mysql_error(pDB
->pMYSQL
));
1370 return MEDIADB_FAIL
;
1374 sqlresult
= mysql_store_result(pDB
->pMYSQL
);
1375 if (sqlresult
== NULL
)
1379 "Failed to store result after query \"%s\" Error: %s",
1380 pDB
->pszSQLQueryBuffer
,
1381 mysql_error(pDB
->pMYSQL
));
1382 return MEDIADB_FAIL
;
1385 nRowCount
= mysql_num_rows(sqlresult
);
1389 sqlrow
= mysql_fetch_row(sqlresult
);
1394 "Failed to fetch row after query \"%s\"",
1395 pDB
->pszSQLQueryBuffer
);
1397 mysql_free_result(sqlresult
);
1399 return MEDIADB_FAIL
;
1403 if (sqlrow
[0] == NULL
)
1409 pszName
= sqlrow
[0];
1413 if (sqlrow
[1] == NULL
)
1419 nSize
= strtoull(sqlrow
[1], NULL
, 10);
1423 if (sqlrow
[2] == NULL
)
1429 nTime
= strtoull(sqlrow
[2], NULL
, 10);
1433 if (sqlrow
[3] == NULL
)
1439 pszPath1
= sqlrow
[3];
1442 s
= strlen(pszName
);
1444 if (s
> 1 && pszName
[s
-1] == '/')
1446 nType
= MEDIADB_FILETYPE_DIR
;
1451 nType
= MEDIADB_FILETYPE_FILE
;
1463 mysql_free_result(sqlresult
);
1465 reset_error_message(pDB
);
1471 mediadb_mysql_get_match_keyword(mediadb_uint nPMMID
)
1475 case MEDIADB_MYSQL_PMM_EXACT_CASE_INSENSITIVE
:
1477 case MEDIADB_MYSQL_PMM_EXACT_CASE_SENSITIVE
:
1478 return " = BINARY ";
1479 case MEDIADB_MYSQL_PMM_LIKE_CASE_INSENSITIVE
:
1481 case MEDIADB_MYSQL_PMM_LIKE_CASE_SENSITIVE
:
1482 return " LIKE BINARY ";
1483 case MEDIADB_MYSQL_PMM_REGEXP_CASE_INSENSITIVE
:
1485 case MEDIADB_MYSQL_PMM_REGEXP_CASE_SENSITIVE
:
1486 return " REGEXP BINARY ";
1490 return "XXX_BUG_XXX";
1495 mediadb_mysql_get_pattern_match_methods(
1497 const struct mediadb_pattern_match_method
**ppPMM
)
1499 *ppPMM
= mediadb_mysql_pattern_match_methods
;
1504 mediadb_mysql_files_search(
1506 mediadb_uint nFilenamePMMID
,
1507 const char *pszFilenamePattern
,
1508 mediadb_uint nPathPMMID
,
1509 const char *pszPathPattern
,
1510 const mediadb_uint
*pnMinSize
,
1511 const mediadb_uint
*pnMaxSize
,
1512 mediadb_files_search_callback pCallback
,
1519 size_t sizeFilenamePattern
;
1520 size_t sizePathPattern
;
1522 MYSQL_RES
* sqlresult
;
1523 my_ulonglong nRowCount
;
1525 mediadb_uint nMediaID
;
1529 char *pszMediaLocation
;
1533 mediadb_filetype nType
;
1538 if (nFilenamePMMID
!= MEDIADB_PMM_NULL
)
1541 sizeFilenamePattern
= strlen(pszFilenamePattern
);
1545 sizeFilenamePattern
= 0;
1548 if (nPathPMMID
!= MEDIADB_PMM_NULL
)
1551 sizePathPattern
= strlen(pszPathPattern
);
1555 sizePathPattern
= 0;
1558 if (pnMinSize
!= NULL
)
1563 if (pnMaxSize
!= NULL
)
1570 /* Nothing to search - empty result */
1574 /* Ensure we have large enough buffer */
1576 r
= maybe_enlarge_buffer(
1577 &pDB
->pszSQLQueryBuffer
,
1578 &pDB
->sizeSQLQueryBuffer
,
1579 sizeFilenamePattern
*2 + sizePathPattern
+ 1024);
1580 if (MEDIADB_IS_ERROR(r
))
1584 "Cannot enlarge buffer");
1588 /* Compose SQL query string */
1590 psz
= pDB
->pszSQLQueryBuffer
;
1594 "SELECT f.mediaid, f.path, f.name, f.size, f.time, m.name, l.description FROM "
1595 MEDIADB_MYSQL_FILES_TABLE
" AS f, "
1596 MEDIADB_MYSQL_MEDIA_TABLE
" AS m, "
1597 MEDIADB_MYSQL_LOCATIONS_TABLE
" AS l "
1598 "WHERE f.mediaid = m.mediaid AND m.location = l.id AND ");
1601 if (nFilenamePMMID
!= MEDIADB_PMM_NULL
)
1608 mediadb_mysql_get_match_keyword(nFilenamePMMID
));
1611 sizePart
= mysql_real_escape_string(
1615 sizeFilenamePattern
);
1621 nCount
== 0?"":pszAND
);
1625 if (nPathPMMID
!= MEDIADB_PMM_NULL
)
1632 mediadb_mysql_get_match_keyword(nPathPMMID
));
1635 sizePart
= mysql_real_escape_string(
1645 nCount
== 0?"":pszAND
);
1649 if (pnMinSize
!= NULL
)
1656 (unsigned int)*pnMinSize
,
1657 nCount
== 0?"":pszAND
);
1661 if (pnMaxSize
!= NULL
)
1668 (unsigned int)*pnMaxSize
,
1669 nCount
== 0?"":pszAND
);
1673 assert(nCount
== 0);
1677 /* Execute SQL query */
1679 #ifdef DUMMY_SQL_QUERIES
1680 printf("MySQL query \"%s\"\n", pDB
->pszSQLQueryBuffer
);
1682 if (mysql_query(pDB
->pMYSQL
, pDB
->pszSQLQueryBuffer
) != 0)
1686 "Failed to execute query \"%s\" Error: %s",
1687 pDB
->pszSQLQueryBuffer
,
1688 mysql_error(pDB
->pMYSQL
));
1689 return MEDIADB_FAIL
;
1693 sqlresult
= mysql_store_result(pDB
->pMYSQL
);
1694 if (sqlresult
== NULL
)
1698 "Failed to store result after query \"%s\" Error: %s",
1699 pDB
->pszSQLQueryBuffer
,
1700 mysql_error(pDB
->pMYSQL
));
1701 return MEDIADB_FAIL
;
1704 nRowCount
= mysql_num_rows(sqlresult
);
1708 sqlrow
= mysql_fetch_row(sqlresult
);
1713 "Failed to fetch row after query \"%s\"",
1714 pDB
->pszSQLQueryBuffer
);
1716 mysql_free_result(sqlresult
);
1718 return MEDIADB_FAIL
;
1722 if (sqlrow
[0] == NULL
)
1728 nMediaID
= strtoull(sqlrow
[0], NULL
, 10);
1732 if (sqlrow
[1] == NULL
)
1738 pszPath
= sqlrow
[1];
1742 if (sqlrow
[2] == NULL
)
1748 pszName
= sqlrow
[2];
1752 if (sqlrow
[3] == NULL
)
1758 nSize
= strtoull(sqlrow
[3], NULL
, 10);
1762 if (sqlrow
[4] == NULL
)
1768 nTime
= strtoull(sqlrow
[4], NULL
, 10);
1772 if (sqlrow
[5] == NULL
)
1778 pszMediaName
= sqlrow
[5];
1781 /* media location */
1782 if (sqlrow
[6] == NULL
)
1784 pszMediaLocation
= "";
1788 pszMediaLocation
= sqlrow
[6];
1791 s
= strlen(pszName
);
1793 if (s
> 1 && pszName
[s
-1] == '/')
1795 nType
= MEDIADB_FILETYPE_DIR
;
1800 nType
= MEDIADB_FILETYPE_FILE
;
1812 (mediadb_uint
)nSize
,
1813 (mediadb_uint
)nTime
);
1816 mysql_free_result(sqlresult
);
1818 reset_error_message(pDB
);
1823 /*****************************************************************************
1825 * Modifications log:
1827 * !!! WARNING !!! Following lines are automatically updated by the CVS system.
1830 * Revision 1.12 2004/10/03 22:03:11 nedko
1831 * Reorder patern match methods, "ERE" first, "LIKE", "exact" last
1833 * Revision 1.11 2004/10/03 21:57:52 nedko
1834 * Implement file search for MySQL backend.
1836 * Revision 1.10 2004/08/31 22:40:15 nedko
1837 * Partitally implemented search feature.
1839 * Revision 1.9 2004/08/08 00:47:42 nedko
1840 * Get more info for media from database.
1842 * Revision 1.8 2004/05/22 00:15:09 nedko
1843 * Implement mediadb_mysql_files_get()
1845 * Revision 1.7 2004/05/21 23:40:46 nedko
1846 * New functionality: mediadb_files_get()
1848 * Revision 1.6 2004/05/16 18:57:12 nedko
1849 * media_get_all functionality implemented.
1851 * Revision 1.5 2004/05/11 01:15:19 nedko
1854 * Revision 1.4 2004/05/03 20:47:15 nedko
1855 * Update mode for cui
1857 * Revision 1.3 2004/05/02 20:12:11 nedko
1858 * Improve error dumps.
1860 * Revision 1.2 2004/05/02 13:18:36 nedko
1861 * Supply filetype when adding new file.
1862 * Initialize mediaid when in dummy sql queries mode.
1864 * Revision 1.1 2004/04/27 09:12:28 nedko
1867 *****************************************************************************/