1 /* MiniDLNA media server
2 * Copyright (C) 2008-2009 Justin Maggard
4 * This file is part of MiniDLNA.
6 * MiniDLNA is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
10 * MiniDLNA is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with MiniDLNA. If not, see <http://www.gnu.org/licenses/>.
23 #include "upnpglobalvars.h"
27 sql_exec(sqlite3
*db
, const char *fmt
, ...)
33 //DPRINTF(E_DEBUG, L_DB_SQL, "SQL: %s\n", sql);
37 sql
= sqlite3_vmprintf(fmt
, ap
);
38 ret
= sqlite3_exec(db
, sql
, 0, 0, &errMsg
);
39 if( ret
!= SQLITE_OK
)
41 DPRINTF(E_ERROR
, L_DB_SQL
, "SQL ERROR %d [%s]\n%s\n", ret
, errMsg
, sql
);
51 sql_get_table(sqlite3
*db
, const char *sql
, char ***pazResult
, int *pnRow
, int *pnColumn
)
55 //DPRINTF(E_DEBUG, L_DB_SQL, "SQL: %s\n", sql);
57 ret
= sqlite3_get_table(db
, sql
, pazResult
, pnRow
, pnColumn
, &errMsg
);
58 if( ret
!= SQLITE_OK
)
60 DPRINTF(E_ERROR
, L_DB_SQL
, "SQL ERROR %d [%s]\n%s\n", ret
, errMsg
, sql
);
69 sql_get_int_field(sqlite3
*db
, const char *fmt
, ...)
79 sql
= sqlite3_vmprintf(fmt
, ap
);
81 //DPRINTF(E_DEBUG, L_DB_SQL, "sql: %s\n", sql);
83 switch (sqlite3_prepare_v2(db
, sql
, -1, &stmt
, NULL
))
88 DPRINTF(E_ERROR
, L_DB_SQL
, "prepare failed: %s\n%s\n", sqlite3_errmsg(db
), sql
);
94 ((result
= sqlite3_step(stmt
)) == SQLITE_BUSY
|| result
== SQLITE_LOCKED
) && counter
< 2;
96 /* While SQLITE_BUSY has a built in timeout,
97 SQLITE_LOCKED does not, so sleep */
98 if (result
== SQLITE_LOCKED
)
105 /* no rows returned */
109 if (sqlite3_column_type(stmt
, 0) == SQLITE_NULL
)
114 ret
= sqlite3_column_int(stmt
, 0);
117 DPRINTF(E_WARN
, L_DB_SQL
, "%s: step failed: %s\n%s\n", __func__
, sqlite3_errmsg(db
), sql
);
123 sqlite3_finalize(stmt
);
128 sql_get_text_field(sqlite3
*db
, const char *fmt
, ...)
131 int counter
, result
, len
;
140 DPRINTF(E_WARN
, L_DB_SQL
, "db is NULL\n");
144 sql
= sqlite3_vmprintf(fmt
, ap
);
146 //DPRINTF(E_DEBUG, L_DB_SQL, "sql: %s\n", sql);
148 switch (sqlite3_prepare_v2(db
, sql
, -1, &stmt
, NULL
))
153 DPRINTF(E_ERROR
, L_DB_SQL
, "prepare failed: %s\n%s\n", sqlite3_errmsg(db
), sql
);
160 ((result
= sqlite3_step(stmt
)) == SQLITE_BUSY
|| result
== SQLITE_LOCKED
) && counter
< 2;
163 /* While SQLITE_BUSY has a built in timeout,
164 * SQLITE_LOCKED does not, so sleep */
165 if (result
== SQLITE_LOCKED
)
172 /* no rows returned */
177 if (sqlite3_column_type(stmt
, 0) == SQLITE_NULL
)
183 len
= sqlite3_column_bytes(stmt
, 0);
184 if ((str
= sqlite3_malloc(len
+ 1)) == NULL
)
186 DPRINTF(E_ERROR
, L_DB_SQL
, "malloc failed\n");
190 strncpy(str
, (char *)sqlite3_column_text(stmt
, 0), len
+ 1);
194 DPRINTF(E_WARN
, L_DB_SQL
, "SQL step failed: %s\n", sqlite3_errmsg(db
));
199 sqlite3_finalize(stmt
);
204 db_upgrade(sqlite3
*db
)
209 db_vers
= sql_get_int_field(db
, "PRAGMA user_version");
211 if (db_vers
== DB_VERSION
)
213 if (db_vers
> DB_VERSION
)
221 DPRINTF(E_WARN
, L_DB_SQL
, "Updating DB version to v%d.\n", 6);
222 ret
= sql_exec(db
, "CREATE TABLE BOOKMARKS ("
223 "ID INTEGER PRIMARY KEY, "
225 if( ret
!= SQLITE_OK
)
230 DPRINTF(E_WARN
, L_DB_SQL
, "Updating DB version to v%d.\n", 7);
231 ret
= sql_exec(db
, "ALTER TABLE DETAILS ADD rotation INTEGER");
232 if( ret
!= SQLITE_OK
)
237 DPRINTF(E_WARN
, L_DB_SQL
, "Updating DB version to v%d.\n", 8);
238 ret
= sql_exec(db
, "UPDATE DETAILS set DLNA_PN = replace(DLNA_PN, ';DLNA.ORG_OP=01;DLNA.ORG_CI=0', '')");
239 if( ret
!= SQLITE_OK
)
242 sql_exec(db
, "PRAGMA user_version = %d", DB_VERSION
);