1 /*****************************************************************************
2 * vlc_sql.h: SQL abstraction layer
3 *****************************************************************************
4 * Copyright (C) 2009 the VideoLAN team
7 * Authors: Antoine Lejeune <phytos@videolan.org>
8 * Jean-Philippe André <jpeg@videolan.org>
9 * Srikanth Raju <srikiraju@gmail.com>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24 *****************************************************************************/
26 #if !defined( __LIBVLC__ )
27 # error You are not libvlc or one of its plugins. You cannot include this file
38 /*****************************************************************************
39 * General structure: SQL object.
40 *****************************************************************************/
43 * Return values for the function @see sql_Run()
46 #define VLC_SQL_DONE 2
48 typedef struct sql_t sql_t;
49 typedef struct sql_sys_t sql_sys_t;
50 typedef struct sql_stmt_t sql_stmt_t;
52 typedef int ( *sql_query_callback_t ) ( void*, int, char**, char** );
78 /** Module properties */
81 /** Connection Data */
82 char *psz_host; /**< Location or host of the database */
83 char *psz_user; /**< Username used to connect to database */
84 char *psz_pass; /**< Password used to connect to database */
85 int i_port; /**< Port on which database is running */
90 /** All the functions are implemented as threadsafe functions */
91 /** Perform a query with a row-by-row callback function */
92 int (*pf_query_callback) ( sql_t *, const char *, sql_query_callback_t, void * );
94 /** Perform a query and return result directly */
95 int (*pf_query) ( sql_t *, const char *, char ***, int *, int * );
97 /** Get database tables */
98 int (*pf_get_tables) ( sql_t *, char *** );
100 /** Free result of a call to sql_Query or sql_GetTables */
101 void (*pf_free) ( sql_t *, char ** );
103 /** vmprintf replacement for SQL */
104 char* (*pf_vmprintf) ( const char*, va_list args );
106 /** Begin transaction */
107 int (*pf_begin) ( sql_t* );
109 /** Commit transaction */
110 int (*pf_commit) ( sql_t* );
112 /** Rollback transaction */
113 int (*pf_rollback) ( sql_t* );
115 /** Create a statement object */
116 sql_stmt_t* (*pf_prepare) ( sql_t* p_sql, const char* p_fmt,
119 /** Bind parameters to a statement */
120 int (*pf_bind) ( sql_t* p_sql, sql_stmt_t* p_stmt, int i_pos,
121 unsigned int type, const sql_value_t* p_value );
123 /** Run the prepared statement */
124 int (*pf_run) ( sql_t* p_sql, sql_stmt_t* p_stmt );
126 /** Reset the prepared statement */
127 int (*pf_reset) ( sql_t* p_sql, sql_stmt_t* p_stmt );
129 /** Destroy the statement object */
130 int (*pf_finalize) ( sql_t* p_sql, sql_stmt_t* p_stmt );
132 /** Get the datatype for a specified column */
133 int (*pf_gettype) ( sql_t* p_sql, sql_stmt_t* p_stmt, int i_col,
136 /** Get the data from a specified column */
137 int (*pf_getcolumn) ( sql_t* p_sql, sql_stmt_t* p_stmt, int i_col,
138 int type, sql_value_t *p_res );
141 /*****************************************************************************
142 * SQL Function headers
143 *****************************************************************************/
146 * @brief Create a new SQL object.
147 * @param p_this Parent object to attach the SQL object to.
148 * @param psz_host URL to the database
149 * @param i_port Port on which the database is running
150 * @param psz_user Username to access the database
151 * @param psz_pass Password for the database
152 * @return The VLC SQL object, type sql_t.
154 VLC_EXPORT( sql_t*, sql_Create, ( vlc_object_t *p_this, const char *psz_name,
155 const char* psz_host, int i_port,
156 const char* psz_user, const char* psz_pass ) );
157 #define sql_Create( a, b, c, d, e, f ) sql_Create( VLC_OBJECT(a), b, c, d, e, f )
161 * @brief Destructor for p_sql object
162 * @param obj This p_sql object
165 VLC_EXPORT( void, sql_Destroy, ( vlc_object_t *obj ) );
166 #define sql_Destroy( a ) sql_Destroy( VLC_OBJECT( a ) )
170 * @brief Perform a query using a callback function
171 * @param p_sql This SQL object.
172 * @param psz_query The SQL query string.
173 * @param pf_callback A callback function that will be called for each row of
174 * the result: 1st argument is be p_opaque,
175 * 2nd argument is the number of columns,
176 * 3rd is the result columns (array of strings),
177 * 4th is the columns names (array of strings).
178 * @param p_opaque Any pointer to an object you may need in the callback.
179 * @return VLC_SUCCESS or VLC_EGENERIC.
180 * @note The query will not necessarily be processed in a separate thread, but
183 static inline int sql_QueryCallback( sql_t *p_sql, const char *psz_query,
184 sql_query_callback_t pf_callback,
187 return p_sql->pf_query_callback( p_sql, psz_query, pf_callback, p_opaque );
191 * @brief Perform a query directly
192 * @param p_sql This SQL object.
193 * @param psz_query The SQL query string.
194 * @param pppsz_result A pointer to a array of strings: result of the query.
195 * Dynamically allocated.
196 * @param pi_rows Pointer to an integer that will receive the number of result
198 * @param pi_cols Pointer to an integer that will receive the number of result
200 * @return VLC_SUCCESS or VLC_EGENERIC.
201 * @note pppsz_result will point to an array of strings, ppsz_result.
202 * This array of strings contains actually a 2D-matrix of strings where the
203 * first row (row 0) contains the SQL table header names.
204 * *pi_rows will be the number of result rows, so that the number of text rows
205 * in ppsz_result will be (*pi_rows + 1) (because of row 0).
206 * To get result[row,col] use (*pppsz_result)[ (row+1) * (*pi_cols) + col ].
207 * This function is threadsafe
209 static inline int sql_Query( sql_t *p_sql, const char *psz_query,
210 char ***pppsz_result, int *pi_rows, int *pi_cols )
212 return p_sql->pf_query( p_sql, psz_query, pppsz_result, pi_rows, pi_cols );
216 * @brief Get database table name list
217 * @param p_sql This SQL object.
218 * @param pppsz_tables Pointer to an array of strings. Dynamically allocated.
219 * Similar to pppsz_result of sql_Query but with only one row.
220 * @return Number of tables or <0 in case of error.
221 * @note This function is threadsafe
223 static inline int sql_GetTables( sql_t *p_sql, char ***pppsz_tables )
225 return p_sql->pf_get_tables( p_sql, pppsz_tables );
229 * @brief Free the result of a query.
230 * @param p_sql This SQL object.
231 * @param ppsz_result The result of sql_Query or sql_GetTables. See above.
233 * @note This function is threadsafe
235 static inline void sql_Free( sql_t *p_sql, char **ppsz_result )
237 p_sql->pf_free( p_sql, ppsz_result );
241 * @brief printf-like function that can escape forbidden/reserved characters.
242 * @param p_sql This SQL object.
243 * @param psz_fmt Format of the string (with %q, %Q and %z enabled).
244 * @param ... Printf arguments
245 * @return Dynamically allocated string or NULL in case of error.
246 * @note Refer to SQLite documentation for more details about %q, %Q and %z.
248 static inline char* sql_Printf( sql_t *p_sql, const char *psz_fmt, ... )
251 va_start( args, psz_fmt );
252 char *r = p_sql->pf_vmprintf( psz_fmt, args );
258 * @brief vprintf replacement for SQL queries, escaping forbidden characters
259 * @param p_sql This SQL object
260 * @param psz_fmt Format of the string
261 * @param arg Variable list of arguments
262 * @return Dynamically allocated string or NULL in case of error.
264 static inline char* sql_VPrintf( sql_t *p_sql, const char *psz_fmt,
267 return p_sql->pf_vmprintf( psz_fmt, arg );
271 * @brief Begin a SQL transaction
272 * @param p_sql The SQL object
273 * @return VLC error code or success
274 * @note This function is threadsafe
276 static inline int sql_BeginTransaction( sql_t *p_sql )
278 return p_sql->pf_begin( p_sql );
282 * @brief Commit a SQL transaction
283 * @param p_sql The SQL object
284 * @return VLC error code or success
285 * @note This function is threadsafe
287 static inline int sql_CommitTransaction( sql_t *p_sql )
289 return p_sql->pf_commit( p_sql );
293 * @brief Rollback a SQL transaction
294 * @param p_sql The SQL object
295 * @return VLC error code or success
296 * @note This function is threadsafe
298 static inline int sql_RollbackTransaction( sql_t *p_sql )
300 return p_sql->pf_rollback( p_sql );
304 * @brief Prepare an sql statement
305 * @param p_sql The SQL object
306 * @param p_fmt SQL query string
307 * @param i_length length of the string. If negative, length will be
308 * considered upto the first \0 character equivalent to strlen(p_fmt).
309 * Otherwise the first i_length bytes will be used
310 * @return a sql_stmt_t pointer or NULL on failure
312 static inline sql_stmt_t* sql_Prepare( sql_t* p_sql, const char* p_fmt,
315 return p_sql->pf_prepare( p_sql, p_fmt, i_length );
319 * @brief Bind arguments to a sql_stmt_t object
320 * @param p_sql The SQL object
321 * @param p_stmt Statement Object
322 * @param type Data type of the value
323 * @param p_value Value to be bound
324 * @param i_pos Position at which the parameter should be bound
325 * @return VLC_SUCCESS or VLC_EGENERIC
327 static inline int sql_BindGeneric( sql_t* p_sql, sql_stmt_t* p_stmt,
328 int i_pos, int type, const sql_value_t* p_value )
330 return p_sql->pf_bind( p_sql, p_stmt, i_pos, type, p_value );
334 * @brief Bind a NULL value to a position
335 * @param p_sql The SQL object
336 * @param p_stmt Statement Object
337 * @param i_pos Position at which the parameter should be bound
338 * @return VLC_SUCCESS or VLC_EGENERIC
340 static inline int sql_BindNull( sql_t *p_sql, sql_stmt_t* p_stmt, int i_pos )
342 int i_ret = sql_BindGeneric( p_sql, p_stmt, i_pos, SQL_NULL, NULL );
347 * @brief Bind an integer to the statement object at some position
348 * @param p_sql The SQL object
349 * @param p_stmt Statement Object
350 * @param i_pos Position at which the parameter should be bound
351 * @param i_int Value to be bound
352 * @return VLC_SUCCESS or VLC_EGENERIC
354 static inline int sql_BindInteger( sql_t *p_sql, sql_stmt_t* p_stmt,
355 int i_pos, int i_int )
359 value.value.i = i_int;
360 int i_ret = sql_BindGeneric( p_sql, p_stmt, i_pos, SQL_INT, &value );
365 * @brief Bind a double to the statement object at some position
366 * @param p_sql The SQL object
367 * @param p_stmt Statement Object
368 * @param i_pos Position at which the parameter should be bound
369 * @param d_dbl Value to be bound
370 * @return VLC_SUCCESS or VLC_EGENERIC
372 static inline int sql_BindDouble( sql_t *p_sql, sql_stmt_t* p_stmt,
373 int i_pos, double d_dbl )
377 value.value.dbl = d_dbl;
378 int i_ret = sql_BindGeneric( p_sql, p_stmt, i_pos, SQL_INT, &value );
383 * @brief Bind Text to the statement
384 * @param p_sql The SQL object
385 * @param p_stmt Statement Object
386 * @param i_pos Position at which the parameter should be bound
387 * @param p_fmt Value to be bound
388 * @param i_length Length of text. If -ve text upto the first null char
390 * @return VLC_SUCCESS or VLC_EGENERIC
392 static inline int sql_BindText( sql_t *p_sql, sql_stmt_t* p_stmt, int i_pos,
393 char* p_fmt, int i_length )
396 value.length = i_length;
397 value.value.psz = p_fmt;
398 int i_ret = sql_BindGeneric( p_sql, p_stmt, i_pos, SQL_TEXT, &value );
403 * @brief Bind a binary object to the statement
404 * @param p_sql The SQL object
405 * @param p_stmt Statement Object
406 * @param i_pos Position at which the parameter should be bound
407 * @param p_ptr Value to be bound
408 * @param i_length Size of the blob to read
409 * @return VLC_SUCCESS or VLC_EGENERIC
411 static inline int sql_BindBlob( sql_t *p_sql, sql_stmt_t* p_stmt, int i_pos,
412 void* p_ptr, int i_length )
415 value.length = i_length;
416 value.value.ptr = p_ptr;
417 int i_ret = sql_BindGeneric( p_sql, p_stmt, i_pos, SQL_INT, &value );
422 * @brief Run the SQL statement. If the statement fetches data, then only
423 * one row of the data is fetched at a time. Run this function again to
424 * fetch the next row.
425 * @param p_sql The SQL object
426 * @param p_stmt The statement
427 * @return VLC_SQL_DONE if done fetching all rows or there are no rows to fetch
428 * VLC_SQL_ROW if a row was fetched for this statement.
429 * VLC_EGENERIC if this function failed
431 static inline int sql_Run( sql_t* p_sql, sql_stmt_t* p_stmt )
433 return p_sql->pf_run( p_sql, p_stmt );
437 * @brief Reset the SQL statement. Resetting the statement will unbind all
438 * the values that were bound on this statement
439 * @param p_sql The SQL object
440 * @param p_stmt The sql statement object
441 * @return VLC_SUCCESS or VLC_EGENERIC
443 static inline int sql_Reset( sql_t* p_sql, sql_stmt_t* p_stmt )
445 return p_sql->pf_reset( p_sql, p_stmt );
449 * @brief Destroy the sql statement object. This will free memory.
450 * @param p_sql The SQL object
451 * @param p_stmt The statement object
452 * @return VLC_SUCCESS or VLC_EGENERIC
454 static inline int sql_Finalize( sql_t* p_sql, sql_stmt_t* p_stmt )
456 return p_sql->pf_finalize( p_sql, p_stmt );
460 * @brief Get the datatype of the result of the column
461 * @param p_sql The SQL object
462 * @param p_stmt The sql statement object
463 * @param i_col The column
464 * @param type pointer to datatype of the given column
465 * @return VLC_SUCCESS or VLC_EGENERIC
467 static inline int sql_GetColumnType( sql_t* p_sql, sql_stmt_t* p_stmt,
468 int i_col, int* type )
470 return p_sql->pf_gettype( p_sql, p_stmt, i_col, type );
474 * @brief Get the column data
475 * @param p_sql The SQL object
476 * @param p_stmt The statement object
477 * @param i_col The column number
478 * @param type Datatype of result
479 * @param p_res The structure which contains the value of the result
480 * @return VLC_SUCCESS or VLC_EGENERIC
482 static inline int sql_GetColumn( sql_t* p_sql, sql_stmt_t* p_stmt,
483 int i_col, int type, sql_value_t *p_res )
485 return p_sql->pf_getcolumn( p_sql, p_stmt, i_col, type, p_res );
489 * @brief Get an integer from the results of a statement
490 * @param p_sql The SQL object
491 * @param p_stmt The statement object
492 * @param i_col The column number
493 * @param i_res Pointer of the location for result to be stored
494 * @return VLC_SUCCESS or VLC_EGENERIC
496 static inline int sql_GetColumnInteger( sql_t* p_sql, sql_stmt_t* p_stmt,
497 int i_col, int* pi_res )
500 int i_ret = p_sql->pf_getcolumn( p_sql, p_stmt, i_col, SQL_INT, &tmp );
501 if( i_ret == VLC_SUCCESS )
502 *pi_res = tmp.value.i;
507 * @brief Get a double from the results of a statement
508 * @param p_sql The SQL object
509 * @param p_stmt The statement object
510 * @param i_col The column number
511 * @param d_res Pointer of the location for result to be stored
512 * @return VLC_SUCCESS or VLC_EGENERIC
514 static inline int sql_GetColumnDouble( sql_t* p_sql, sql_stmt_t* p_stmt,
515 int i_col, double* pd_res )
518 int i_ret = p_sql->pf_getcolumn( p_sql, p_stmt, i_col, SQL_DOUBLE, &tmp );
519 if( i_ret == VLC_SUCCESS )
520 *pd_res = tmp.value.dbl;
525 * @brief Get some text from the results of a statement
526 * @param p_sql The SQL object
527 * @param p_stmt The statement object
528 * @param i_col The column number
529 * @param pp_res Pointer of the location for result to be stored
530 * @return VLC_SUCCESS or VLC_EGENERIC
532 static inline int sql_GetColumnText( sql_t* p_sql, sql_stmt_t* p_stmt,
533 int i_col, char** pp_res )
536 int i_ret = p_sql->pf_getcolumn( p_sql, p_stmt, i_col, SQL_TEXT, &tmp );
537 if( i_ret == VLC_SUCCESS )
538 *pp_res = tmp.value.psz;
543 * @brief Get a blob from the results of a statement
544 * @param p_sql The SQL object
545 * @param p_stmt The statement object
546 * @param i_col The column number
547 * @param pp_res Pointer of the location for result to be stored
548 * @return VLC_SUCCESS or VLC_EGENERIC
550 static inline int sql_GetColumnBlob( sql_t* p_sql, sql_stmt_t* p_stmt,
551 int i_col, void** pp_res )
554 int i_ret = p_sql->pf_getcolumn( p_sql, p_stmt, i_col, SQL_BLOB, &tmp );
555 if( i_ret == VLC_SUCCESS )
556 *pp_res = tmp.value.ptr;
562 # endif /* C++ extern "C" */
564 #endif /* VLC_SQL_H */