Commit 484f5608 authored by Srikanth Raju's avatar Srikanth Raju Committed by Jean-Baptiste Kempf

SQL/SQLite: Fix GetColumn functions in SQLite and implement a GetColumnSize function

Add a GetColumnSize function in the SQL API.
Signed-off-by: default avatarJean-Baptiste Kempf <jb@videolan.org>
parent 30d7534d
...@@ -136,6 +136,9 @@ struct sql_t ...@@ -136,6 +136,9 @@ struct sql_t
/** Get the data from a specified column */ /** Get the data from a specified column */
int (*pf_getcolumn) ( sql_t* p_sql, sql_stmt_t* p_stmt, int i_col, int (*pf_getcolumn) ( sql_t* p_sql, sql_stmt_t* p_stmt, int i_col,
int type, sql_value_t *p_res ); int type, sql_value_t *p_res );
/** Get column size of a specified column */
int (*pf_getcolumnsize) ( sql_t* p_sql, sql_stmt_t* p_stmt, int i_col );
}; };
/***************************************************************************** /*****************************************************************************
...@@ -557,6 +560,19 @@ static inline int sql_GetColumnBlob( sql_t* p_sql, sql_stmt_t* p_stmt, ...@@ -557,6 +560,19 @@ static inline int sql_GetColumnBlob( sql_t* p_sql, sql_stmt_t* p_stmt,
return i_ret; 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, excluding the zero terminator
*/
static inline int sql_GetColumnSize( sql_t* p_sql, sql_stmt_t* p_stmt,
int i_col )
{
return p_sql->pf_getcolumnsize( p_sql, p_stmt, i_col );
}
# ifdef __cplusplus # ifdef __cplusplus
} }
# endif /* C++ extern "C" */ # endif /* C++ extern "C" */
......
...@@ -104,6 +104,9 @@ static int GetColumnTypeFromStatement( sql_t* p_sql, ...@@ -104,6 +104,9 @@ static int GetColumnTypeFromStatement( sql_t* p_sql,
sql_stmt_t* p_stmt, sql_stmt_t* p_stmt,
int i_col, int i_col,
int* pi_type ); int* pi_type );
static int GetColumnSize( sql_t* p_sql,
sql_stmt_t* p_stmt,
int i_col );
/***************************************************************************** /*****************************************************************************
* Module description * Module description
...@@ -160,6 +163,7 @@ static int load( vlc_object_t *p_this ) ...@@ -160,6 +163,7 @@ static int load( vlc_object_t *p_this )
p_sql->pf_finalize = StatementFinalize; p_sql->pf_finalize = StatementFinalize;
p_sql->pf_gettype = GetColumnTypeFromStatement; p_sql->pf_gettype = GetColumnTypeFromStatement;
p_sql->pf_getcolumn = GetColumnFromStatement; p_sql->pf_getcolumn = GetColumnFromStatement;
p_sql->pf_getcolumnsize = GetColumnSize;
return VLC_SUCCESS; return VLC_SUCCESS;
} }
...@@ -657,6 +661,9 @@ static int GetColumnFromStatement( sql_t* p_sql, sql_stmt_t* p_stmt, int i_col, ...@@ -657,6 +661,9 @@ static int GetColumnFromStatement( sql_t* p_sql, sql_stmt_t* p_stmt, int i_col,
assert( p_stmt->p_sqlitestmt ); assert( p_stmt->p_sqlitestmt );
int i_ret = VLC_SUCCESS; int i_ret = VLC_SUCCESS;
vlc_mutex_lock( &p_sql->p_sys->lock ); vlc_mutex_lock( &p_sql->p_sys->lock );
const unsigned char* psz;
const void* ptr;
int size;
switch( type ) switch( type )
{ {
case SQL_INT: case SQL_INT:
...@@ -666,10 +673,22 @@ static int GetColumnFromStatement( sql_t* p_sql, sql_stmt_t* p_stmt, int i_col, ...@@ -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 ); p_res->value.dbl = sqlite3_column_double( p_stmt->p_sqlitestmt, i_col );
break; break;
case SQL_TEXT: 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; break;
case SQL_BLOB: 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; break;
case SQL_NULL: case SQL_NULL:
default: default:
...@@ -720,3 +739,17 @@ static int GetColumnTypeFromStatement( sql_t* p_sql, sql_stmt_t* p_stmt, int i_c ...@@ -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 ); vlc_mutex_unlock( &p_sql->p_sys->lock );
return i_ret; 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 );
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment