X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=modules%2Fmisc%2Fsqlite.c;h=360577f447289abb69f5bd86e3323a21b8551301;hb=99f02e92782d40eb07808b687b572faf5f6129f5;hp=7eb182fabd8c52448d8769a32eae47ec8b9fd9c4;hpb=eeb1d41bb3a95a0908026c79036a1168e46aec7c;p=vlc diff --git a/modules/misc/sqlite.c b/modules/misc/sqlite.c index 7eb182fabd..360577f447 100644 --- a/modules/misc/sqlite.c +++ b/modules/misc/sqlite.c @@ -31,7 +31,6 @@ #endif #include -#include #include #include @@ -104,6 +103,9 @@ static int GetColumnTypeFromStatement( sql_t* p_sql, sql_stmt_t* p_stmt, int i_col, int* pi_type ); +static int GetColumnSize( sql_t* p_sql, + sql_stmt_t* p_stmt, + int i_col ); /***************************************************************************** * Module description @@ -160,6 +162,7 @@ static int load( vlc_object_t *p_this ) p_sql->pf_finalize = StatementFinalize; p_sql->pf_gettype = GetColumnTypeFromStatement; p_sql->pf_getcolumn = GetColumnFromStatement; + p_sql->pf_getcolumnsize = GetColumnSize; return VLC_SUCCESS; } @@ -205,7 +208,7 @@ static int OpenDatabase( sql_t *p_sql ) { assert( p_sql->psz_host && *p_sql->psz_host ); - if( sqlite3_threadsafe() != 0 ) + if( sqlite3_threadsafe() == 0 ) { msg_Err( p_sql, "Sqlite library on your system is not threadsafe" ); return VLC_EGENERIC; @@ -448,9 +451,10 @@ static int CommitTransaction( sql_t* p_sql ) msg_Warn( p_sql, "sqlite3 error: %d: %s", sqlite3_errcode( p_sql->p_sys->db ), sqlite3_errmsg( p_sql->p_sys->db ) ); - vlc_mutex_unlock( &p_sql->p_sys->trans_lock ); i_ret = VLC_EGENERIC; } + else + vlc_mutex_unlock( &p_sql->p_sys->trans_lock ); vlc_mutex_unlock( &p_sql->p_sys->lock ); return i_ret; } @@ -657,6 +661,9 @@ static int GetColumnFromStatement( sql_t* p_sql, sql_stmt_t* p_stmt, int i_col, assert( p_stmt->p_sqlitestmt ); int i_ret = VLC_SUCCESS; vlc_mutex_lock( &p_sql->p_sys->lock ); + const unsigned char* psz; + const void* ptr; + int size; switch( type ) { case SQL_INT: @@ -666,10 +673,22 @@ static int GetColumnFromStatement( sql_t* p_sql, sql_stmt_t* p_stmt, int i_col, p_res->value.dbl = sqlite3_column_double( p_stmt->p_sqlitestmt, i_col ); break; case SQL_TEXT: - p_res->value.psz = sqlite3_column_text( p_stmt->p_sqlitestmt, i_col ); + psz = sqlite3_column_text( p_stmt->p_sqlitestmt, i_col ); + if( psz ) + p_res->value.psz = strdup( (const char* ) psz ); break; case SQL_BLOB: - p_res->value.ptr = sqlite3_column_blob( p_stmt->p_sqlitestmt, i_col ); + ptr = sqlite3_column_blob( p_stmt->p_sqlitestmt, i_col ); + size = sqlite3_column_bytes( p_stmt->p_sqlitestmt, i_col ); + if( ptr ) + { + p_res->value.ptr = malloc( size ); + p_res->length = size; + if( p_res->value.ptr ) + memcpy( p_res->value.ptr, ptr, size ); + else + i_ret = VLC_ENOMEM; + } break; case SQL_NULL: default: @@ -720,3 +739,17 @@ static int GetColumnTypeFromStatement( sql_t* p_sql, sql_stmt_t* p_stmt, int i_c vlc_mutex_unlock( &p_sql->p_sys->lock ); return i_ret; } + +/** + * @brief Get the size of the column in bytes + * @param p_sql The SQL object + * @param p_stmt The sql statement object + * @param i_col The column + * @return Size of the column in bytes, undefined for invalid columns + */ +static int GetColumnSize( sql_t* p_sql, sql_stmt_t* p_stmt, int i_col ) +{ + assert( p_sql->p_sys->db ); + assert( p_stmt->p_sqlitestmt ); + return sqlite3_column_bytes( p_stmt->p_sqlitestmt, i_col ); +}