Various untracked changes.
[mediadatabase.git] / libdb / libdb.c
blob8d3493153741e3d4913e8a698427a16f07a61b49
1 /* -*- Mode: C ; c-basic-offset: 2 -*- */
2 /*****************************************************************************
4 * $Id: libdb.c,v 1.3 2004/05/02 20:12:11 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>
20 #include "internal.h"
21 #include "memory.h"
23 mediadb_result
24 mediadb_open(
25 unsigned int nDBType,
26 const char *pszHost,
27 const char *pszUser,
28 const char *pszPass,
29 const char *pszDB,
30 mediadb *phDB)
32 mediadb_result r;
33 mediadb_impl *pImpl;
35 pImpl = malloc(sizeof(mediadb_impl));
36 if (pImpl == NULL)
38 r = MEDIADB_MEM;
39 goto Fail;
42 pImpl->nDBType = nDBType;
44 if (nDBType == MEDIADB_DBTYPE_MYSQL)
46 r = mediadb_mysql_open(
47 &pImpl->data.mysql,
48 pszHost,
49 pszUser,
50 pszPass,
51 pszDB);
52 if (MEDIADB_IS_ERROR(r))
54 goto FailFreeMem;
57 else if (nDBType == MEDIADB_DBTYPE_SQLITE)
59 r = mediadb_sqlite_open(
60 &pImpl->data.sqlite,
61 pszHost,
62 pszUser,
63 pszPass,
64 pszDB);
65 if (MEDIADB_IS_ERROR(r))
67 goto FailFreeMem;
70 else
72 r = MEDIADB_NOT_IMPL;
73 goto FailFreeMem;
76 *phDB = (mediadb)pImpl;
78 return MEDIADB_OK;
80 FailFreeMem:
81 free(pImpl);
82 Fail:
83 return r;
86 #define pDB ((mediadb_impl *)hDB)
88 mediadb_result
89 mediadb_media_add_new(
90 mediadb hDB,
91 const char *pszName,
92 const char *pszComment,
93 mediadb_mediatype nType,
94 mediadb_uint *pnNewMediaID)
96 mediadb_result r;
98 if (pDB->nDBType == MEDIADB_DBTYPE_MYSQL)
100 r = mediadb_mysql_media_add_new(
101 &pDB->data.mysql,
102 pszName,
103 pszComment,
104 nType,
105 pnNewMediaID);
107 else if (pDB->nDBType == MEDIADB_DBTYPE_SQLITE)
109 r = mediadb_sqlite_media_add_new(
110 &pDB->data.sqlite,
111 pszName,
112 pszComment,
113 nType,
114 pnNewMediaID);
116 else
118 r = MEDIADB_INVAL_ARG;
119 assert(0);
122 return r;
125 mediadb_result
126 mediadb_media_update_properties(
127 mediadb hDB,
128 mediadb_uint nMediaID,
129 mediadb_uint nTimeAdded,
130 mediadb_uint nTotalFiles,
131 mediadb_uint nTotalSize)
133 mediadb_result r;
135 if (pDB->nDBType == MEDIADB_DBTYPE_MYSQL)
137 r = mediadb_mysql_media_update_properties(
138 &pDB->data.mysql,
139 nMediaID,
140 nTimeAdded,
141 nTotalFiles,
142 nTotalSize);
144 else if (pDB->nDBType == MEDIADB_DBTYPE_SQLITE)
146 r = mediadb_sqlite_media_update_properties(
147 &pDB->data.sqlite,
148 nMediaID,
149 nTimeAdded,
150 nTotalFiles,
151 nTotalSize);
153 else
155 r = MEDIADB_INVAL_ARG;
156 assert(0);
159 return r;
162 mediadb_result
163 mediadb_file_add_new(
164 mediadb hDB,
165 mediadb_uint nMediaID,
166 mediadb_filetype Filetype,
167 const char *pszPath,
168 const char *pszName,
169 mediadb_uint nFileSize,
170 mediadb_uint nFileTime)
172 mediadb_result r;
174 if (pDB->nDBType == MEDIADB_DBTYPE_MYSQL)
176 r = mediadb_mysql_file_add_new(
177 &pDB->data.mysql,
178 nMediaID,
179 Filetype,
180 pszPath,
181 pszName,
182 nFileSize,
183 nFileTime);
185 else if (pDB->nDBType == MEDIADB_DBTYPE_SQLITE)
187 r = mediadb_sqlite_file_add_new(
188 &pDB->data.sqlite,
189 nMediaID,
190 Filetype,
191 pszPath,
192 pszName,
193 nFileSize,
194 nFileTime);
196 else
198 r = MEDIADB_INVAL_ARG;
199 assert(0);
202 return r;
205 mediadb_result
206 mediadb_close(
207 mediadb hDB)
209 mediadb_result r;
211 if (pDB->nDBType == MEDIADB_DBTYPE_MYSQL)
213 r = mediadb_mysql_close(
214 &pDB->data.mysql);
216 else if (pDB->nDBType == MEDIADB_DBTYPE_SQLITE)
218 r = mediadb_sqlite_close(
219 &pDB->data.sqlite);
221 else
223 r = MEDIADB_INVAL_ARG;
224 assert(0);
227 if (MEDIADB_IS_ERROR(r))
229 return r;
232 free(pDB);
234 return r;
237 const char *
238 mediadb_get_error_message(
239 mediadb hDB)
241 if (pDB->nDBType == MEDIADB_DBTYPE_MYSQL)
243 return mediadb_mysql_get_error_message(&pDB->data.mysql);
246 if (pDB->nDBType == MEDIADB_DBTYPE_SQLITE)
248 return mediadb_sqlite_get_error_message(&pDB->data.sqlite);
251 assert(0);
252 return "enexpected behaviour";
255 /*****************************************************************************
257 * Modifications log:
259 * !!! WARNING !!! Following lines are automatically updated by the CVS system.
261 * $Log: libdb.c,v $
262 * Revision 1.3 2004/05/02 20:12:11 nedko
263 * Improve error dumps.
265 * Revision 1.2 2004/05/02 13:16:58 nedko
266 * Supply filetype when adding new file
268 * Revision 1.1 2004/04/27 09:12:28 nedko
269 * Initial revision.
271 *****************************************************************************/