1 /* -*- Mode: C ; c-basic-offset: 2 -*- */
2 /*****************************************************************************
4 * $Id: mysql.c,v 1.3 2004/05/02 20:12:11 nedko Exp $
7 * MySQL backend implementation.
10 * Nedko Arnaudov <nedko@users.sourceforge.net>
13 * GNU GENERAL PUBLIC LICENSE version 2
15 *****************************************************************************/
25 #define MEDIADB_MYSQL_MEDIA_TABLE "media"
26 #define MEDIADB_MYSQL_FILES_TABLE "files"
28 //#define DUMMY_SQL_QUERIES
29 #define FORMATTING_BUFFER_SIZE 10240
35 const char *pszFormat
,
41 char Buffer
[FORMATTING_BUFFER_SIZE
];
43 va_start(argList
, pszFormat
);
44 vsnprintf(Buffer
, FORMATTING_BUFFER_SIZE
-1, pszFormat
, argList
);
49 r
= maybe_enlarge_buffer(
50 &pDB
->pErrorMsgBuffer
,
51 &pDB
->sizeErrorMsgBuffer
,
53 if (MEDIADB_IS_ERROR(r
))
55 if (pDB
->sizeErrorMsgBuffer
> 0)
57 pDB
->pErrorMsgBuffer
[0] = 0;
61 memcpy(pDB
->pErrorMsgBuffer
, Buffer
, s
+ 1);
69 if (pDB
->pErrorMsgBuffer
!= NULL
)
71 pDB
->pErrorMsgBuffer
[0] = 0;
76 mediadb_mysql_get_error_message(
79 if (pDB
->pErrorMsgBuffer
== NULL
)
84 return pDB
->pErrorMsgBuffer
;
97 pMYSQL
= mysql_init(NULL
);
102 "Cannot create MYSQL object. Error is \"%s\"",
103 mysql_error(pMYSQL
));
107 if (mysql_real_connect(
119 "Failed to connect to database. Error: %s",
120 mysql_error(pMYSQL
));
125 pDB
->pMYSQL
= pMYSQL
;
126 pDB
->pszSQLQueryBuffer
= NULL
;
127 pDB
->sizeSQLQueryBuffer
= 0;
128 pDB
->pErrorMsgBuffer
= NULL
;
129 pDB
->sizeErrorMsgBuffer
= 0;
131 reset_error_message(pDB
);
137 mediadb_mysql_media_add_new(
140 const char *pszComment
,
141 mediadb_mediatype nType
,
142 mediadb_uint
*pnNewMediaID
)
145 size_t sizeName
, sizeComment
, sizePart
;
147 my_ulonglong nMediaID
;
149 /* Ensure we have large enough buffer */
151 sizeName
= strlen(pszName
);
152 sizeComment
= strlen(pszComment
);
154 r
= maybe_enlarge_buffer(
155 &pDB
->pszSQLQueryBuffer
,
156 &pDB
->sizeSQLQueryBuffer
,
157 sizeName
*2 + sizeComment
*2 + 1024);
158 if (MEDIADB_IS_ERROR(r
))
162 "Cannot enlarge buffer");
166 /* Compose SQL query string */
168 psz
= pDB
->pszSQLQueryBuffer
;
170 pszPart
= "INSERT INTO " MEDIADB_MYSQL_MEDIA_TABLE
" (name,type,comment) VALUES ('";
171 sizePart
= strlen(pszPart
);
172 memcpy(psz
, pszPart
, sizePart
);
175 sizePart
= mysql_real_escape_string(
182 sizePart
= sprintf(psz
, "',%u,'", (unsigned int)nType
);
185 sizePart
= mysql_real_escape_string(
193 sizePart
= strlen(pszPart
);
194 memcpy(psz
, pszPart
, sizePart
);
199 /* Execute SQL query */
201 #ifdef DUMMY_SQL_QUERIES
202 printf("MySQL query \"%s\"\n", pDB
->pszSQLQueryBuffer
);
205 if (mysql_query(pDB
->pMYSQL
, pDB
->pszSQLQueryBuffer
) != 0)
209 "Failed to execute query \"%s\" Error: %s",
210 pDB
->pszSQLQueryBuffer
,
211 mysql_error(pDB
->pMYSQL
));
215 nMediaID
= mysql_insert_id(pDB
->pMYSQL
);
216 if (nMediaID
== 0ULL)
220 "Invalid media ID 0 after media INSERT");
225 *pnNewMediaID
= nMediaID
;
227 reset_error_message(pDB
);
233 mediadb_mysql_media_update_properties(
235 mediadb_uint nMediaID
,
236 mediadb_uint nTimeAdded
,
237 mediadb_uint nTotalFiles
,
238 mediadb_uint nTotalSize
)
242 /* Ensure we have large enough buffer */
244 r
= maybe_enlarge_buffer(
245 &pDB
->pszSQLQueryBuffer
,
246 &pDB
->sizeSQLQueryBuffer
,
248 if (MEDIADB_IS_ERROR(r
))
252 "Cannot enlarge buffer");
256 /* Compose SQL query string */
259 pDB
->pszSQLQueryBuffer
,
260 "UPDATE " MEDIADB_MYSQL_MEDIA_TABLE
" SET added=%u, info1=%u, info2=%u WHERE mediaid = %u",
261 (unsigned int)nTimeAdded
,
262 (unsigned int)nTotalFiles
,
263 (unsigned int)nTotalSize
,
264 (unsigned int)nMediaID
);
266 /* Execute SQL query */
268 #ifdef DUMMY_SQL_QUERIES
269 printf("MySQL query \"%s\"\n", pDB
->pszSQLQueryBuffer
);
271 if (mysql_query(pDB
->pMYSQL
, pDB
->pszSQLQueryBuffer
) != 0)
275 "Failed to execute query \"%s\" Error: %s",
276 pDB
->pszSQLQueryBuffer
,
277 mysql_error(pDB
->pMYSQL
));
282 reset_error_message(pDB
);
288 mediadb_mysql_file_add_new(
290 mediadb_uint nMediaID
,
291 mediadb_filetype Filetype
,
294 mediadb_uint nFileSize
,
295 mediadb_uint nFileTime
)
298 size_t sizePath
, sizeName
, sizePart
;
301 /* Ensure we have large enough buffer */
303 sizePath
= strlen(pszPath
);
304 sizeName
= strlen(pszName
);
306 r
= maybe_enlarge_buffer(
307 &pDB
->pszSQLQueryBuffer
,
308 &pDB
->sizeSQLQueryBuffer
,
309 sizePath
*2 + sizeName
*2 + 1024);
310 if (MEDIADB_IS_ERROR(r
))
314 "Cannot enlarge buffer");
318 /* Compose SQL query string */
320 psz
= pDB
->pszSQLQueryBuffer
;
324 "INSERT INTO " MEDIADB_MYSQL_FILES_TABLE
" (mediaid,size,time,path,name) VALUES (%u,%u,%u,'",
325 (unsigned int)nMediaID
,
326 (unsigned int)nFileSize
,
327 (unsigned int)nFileTime
);
330 sizePart
= mysql_real_escape_string(
338 sizePart
= strlen(pszPart
);
339 memcpy(psz
, pszPart
, sizePart
);
342 sizePart
= mysql_real_escape_string(
349 if (Filetype
== MEDIADB_FILETYPE_DIR
)
352 sizePart
= strlen(pszPart
);
353 memcpy(psz
, pszPart
, sizePart
);
356 else if (Filetype
!= MEDIADB_FILETYPE_FILE
)
360 "Cannot add file of unknown type");
361 return MEDIADB_INVAL_ARG
;
365 sizePart
= strlen(pszPart
);
366 memcpy(psz
, pszPart
, sizePart
);
371 /* Execute SQL query */
373 #ifdef DUMMY_SQL_QUERIES
374 printf("MySQL query \"%s\"\n", pDB
->pszSQLQueryBuffer
);
376 if (mysql_query(pDB
->pMYSQL
, pDB
->pszSQLQueryBuffer
) != 0)
380 "Failed to execute query \"%s\" Error: %s",
381 pDB
->pszSQLQueryBuffer
,
382 mysql_error(pDB
->pMYSQL
));
387 reset_error_message(pDB
);
396 if (pDB
->pszSQLQueryBuffer
!= NULL
)
398 free(pDB
->pszSQLQueryBuffer
);
401 mysql_close(pDB
->pMYSQL
);
406 /*****************************************************************************
410 * !!! WARNING !!! Following lines are automatically updated by the CVS system.
413 * Revision 1.3 2004/05/02 20:12:11 nedko
414 * Improve error dumps.
416 * Revision 1.2 2004/05/02 13:18:36 nedko
417 * Supply filetype when adding new file.
418 * Initialize mediaid when in dummy sql queries mode.
420 * Revision 1.1 2004/04/27 09:12:28 nedko
423 *****************************************************************************/