]> git.sesse.net Git - vlc/blob - include/vlc_sql.h
Sql: Include interface for prepared statements.
[vlc] / include / vlc_sql.h
1 /*****************************************************************************
2  * vlc_sql.h: SQL abstraction layer
3  *****************************************************************************
4  * Copyright (C) 2009 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Antoine Lejeune <phytos@videolan.org>
8  *          Jean-Philippe AndrĂ© <jpeg@videolan.org>
9  *          Srikanth Raju <srikiraju@gmail.com>
10  *
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.
15  *
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.
20  *
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  *****************************************************************************/
25
26 #if !defined( __LIBVLC__ )
27 # error You are not libvlc or one of its plugins. You cannot include this file
28 #endif
29
30 #ifndef VLC_SQL_H
31 # define VLC_SQL_H
32
33 # ifdef __cplusplus
34 extern "C" {
35 # endif
36
37
38 /*****************************************************************************
39  * General structure: SQL object.
40  *****************************************************************************/
41
42 /**
43  * Return values for the function @see sql_Run()
44  */
45 #define VLC_SQL_ROW 1
46 #define VLC_SQL_DONE 2
47
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;
51
52 typedef int ( *sql_query_callback_t ) ( void*, int, char**, char** );
53
54 typedef enum {
55     SQL_NULL,
56     SQL_INT,
57     SQL_DOUBLE,
58     SQL_TEXT,
59     SQL_BLOB
60 } sql_type_e;
61
62 typedef struct
63 {
64     int length;
65     union
66     {
67         int i;
68         double dbl;
69         char* psz;
70         void* ptr;
71     } value;
72 } sql_value_t;
73
74 struct sql_t
75 {
76     VLC_COMMON_MEMBERS
77
78     /** Module properties */
79     module_t  *p_module;
80
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 */
86
87     /** Internal data */
88     sql_sys_t *p_sys;
89
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 * );
93
94     /** Perform a query and return result directly */
95     int (*pf_query) ( sql_t *, const char *, char ***, int *, int * );
96
97     /** Get database tables */
98     int (*pf_get_tables) ( sql_t *, char *** );
99
100     /** Free result of a call to sql_Query or sql_GetTables */
101     void (*pf_free) ( sql_t *, char ** );
102
103     /** vmprintf replacement for SQL */
104     char* (*pf_vmprintf) ( const char*, va_list args );
105
106     /** Begin transaction */
107     int (*pf_begin) ( sql_t* );
108
109     /** Commit transaction */
110     int (*pf_commit) ( sql_t* );
111
112     /** Rollback transaction */
113     int (*pf_rollback) ( sql_t* );
114
115     /** Create a statement object */
116     sql_stmt_t* (*pf_prepare) ( sql_t* p_sql, const char* p_fmt,
117                                 int i_length );
118
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 );
122
123     /** Run the prepared statement */
124     int (*pf_run) ( sql_t* p_sql, sql_stmt_t* p_stmt );
125
126     /** Reset the prepared statement */
127     int (*pf_reset) ( sql_t* p_sql, sql_stmt_t* p_stmt );
128
129     /** Destroy the statement object */
130     int (*pf_finalize) ( sql_t* p_sql, sql_stmt_t* p_stmt );
131
132     /** Get the datatype for a specified column */
133     int (*pf_gettype) ( sql_t* p_sql, sql_stmt_t* p_stmt, int i_col,
134                         int* type );
135
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 );
139 };
140
141 /*****************************************************************************
142  * SQL Function headers
143  *****************************************************************************/
144
145 /**
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.
153  **/
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 )
158
159
160 /**
161  * @brief Destructor for p_sql object
162  * @param obj This p_sql object
163  * @return Nothing
164  */
165 VLC_EXPORT( void, sql_Destroy, ( vlc_object_t *obj ) );
166 #define sql_Destroy( a ) sql_Destroy( VLC_OBJECT( a ) )
167
168
169 /**
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
181  * it is threadsafe
182  **/
183 static inline int sql_QueryCallback( sql_t *p_sql, const char *psz_query,
184                                      sql_query_callback_t pf_callback,
185                                      void *p_opaque )
186 {
187     return p_sql->pf_query_callback( p_sql, psz_query, pf_callback, p_opaque );
188 }
189
190 /**
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
197  * rows written.
198  * @param pi_cols Pointer to an integer that will receive the number of result
199  * columns written.
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
208  **/
209 static inline int sql_Query( sql_t *p_sql, const char *psz_query,
210                              char ***pppsz_result, int *pi_rows, int *pi_cols )
211 {
212     return p_sql->pf_query( p_sql, psz_query, pppsz_result, pi_rows, pi_cols );
213 }
214
215 /**
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
222  **/
223 static inline int sql_GetTables( sql_t *p_sql, char ***pppsz_tables )
224 {
225     return p_sql->pf_get_tables( p_sql, pppsz_tables );
226 }
227
228 /**
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.
232  * @return Nothing.
233  * @note This function is threadsafe
234  **/
235 static inline void sql_Free( sql_t *p_sql, char **ppsz_result )
236 {
237     p_sql->pf_free( p_sql, ppsz_result );
238 }
239
240 /**
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.
247  **/
248 static inline char* sql_Printf( sql_t *p_sql, const char *psz_fmt, ... )
249 {
250     va_list args;
251     va_start( args, psz_fmt );
252     char *r = p_sql->pf_vmprintf( psz_fmt, args );
253     va_end( args );
254     return r;
255 }
256
257 /**
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.
263  **/
264 static inline char* sql_VPrintf( sql_t *p_sql, const char *psz_fmt,
265                                  va_list arg )
266 {
267     return p_sql->pf_vmprintf( psz_fmt, arg );
268 }
269
270 /**
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
275  **/
276 static inline int sql_BeginTransaction( sql_t *p_sql )
277 {
278     return p_sql->pf_begin( p_sql );
279 }
280
281 /**
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
286  **/
287 static inline int sql_CommitTransaction( sql_t *p_sql )
288 {
289     return p_sql->pf_commit( p_sql );
290 }
291
292 /**
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
297  **/
298 static inline int sql_RollbackTransaction( sql_t *p_sql )
299 {
300     return p_sql->pf_rollback( p_sql );
301 }
302
303 /**
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
311  */
312 static inline sql_stmt_t* sql_Prepare( sql_t* p_sql, const char* p_fmt,
313         int i_length )
314 {
315     return p_sql->pf_prepare( p_sql, p_fmt, i_length );
316 }
317
318 /**
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
326  */
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 )
329 {
330     return p_sql->pf_bind( p_sql, p_stmt, i_pos, type, p_value );
331 }
332
333 /**
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
339  */
340 static inline int sql_BindNull( sql_t *p_sql, sql_stmt_t* p_stmt, int i_pos )
341 {
342     int i_ret = sql_BindGeneric( p_sql, p_stmt, i_pos, SQL_NULL, NULL );
343     return i_ret;
344 }
345
346 /**
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
353  */
354 static inline int sql_BindInteger( sql_t *p_sql, sql_stmt_t* p_stmt,
355                                    int i_pos, int i_int )
356 {
357     sql_value_t value;
358     value.length = 0;
359     value.value.i = i_int;
360     int i_ret = sql_BindGeneric( p_sql, p_stmt, i_pos, SQL_INT, &value );
361     return i_ret;
362 }
363
364 /**
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
371  */
372 static inline int sql_BindDouble( sql_t *p_sql, sql_stmt_t* p_stmt,
373                                   int i_pos, double d_dbl )
374 {
375     sql_value_t value;
376     value.length = 0;
377     value.value.dbl = d_dbl;
378     int i_ret = sql_BindGeneric( p_sql, p_stmt, i_pos, SQL_INT, &value );
379     return i_ret;
380 }
381
382 /**
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
389  * will be selected.
390  * @return VLC_SUCCESS or VLC_EGENERIC
391  */
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 )
394 {
395     sql_value_t value;
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 );
399     return i_ret;
400 }
401
402 /**
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
410  */
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 )
413 {
414     sql_value_t value;
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 );
418     return i_ret;
419 }
420
421 /**
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
430  */
431 static inline int sql_Run( sql_t* p_sql, sql_stmt_t* p_stmt )
432 {
433     return p_sql->pf_run( p_sql, p_stmt );
434 }
435
436 /**
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
442  */
443 static inline int sql_Reset( sql_t* p_sql, sql_stmt_t* p_stmt )
444 {
445     return p_sql->pf_reset( p_sql, p_stmt );
446 }
447
448 /**
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
453  */
454 static inline int sql_Finalize( sql_t* p_sql, sql_stmt_t* p_stmt )
455 {
456     return p_sql->pf_finalize( p_sql, p_stmt );
457 }
458
459 /**
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
466  */
467 static inline int sql_GetColumnType( sql_t* p_sql, sql_stmt_t* p_stmt,
468         int i_col, int* type )
469 {
470     return p_sql->pf_gettype( p_sql, p_stmt, i_col, type );
471 }
472
473 /**
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
481  */
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 )
484 {
485     return p_sql->pf_getcolumn( p_sql, p_stmt, i_col, type, p_res );
486 }
487
488 /**
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
495  */
496 static inline int sql_GetColumnInteger( sql_t* p_sql, sql_stmt_t* p_stmt,
497         int i_col, int* pi_res )
498 {
499     sql_value_t tmp;
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;
503     return i_ret;
504 }
505
506 /**
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
513  */
514 static inline int sql_GetColumnDouble( sql_t* p_sql, sql_stmt_t* p_stmt,
515         int i_col, double* pd_res )
516 {
517     sql_value_t tmp;
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;
521     return i_ret;
522 }
523
524 /**
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
531  */
532 static inline int sql_GetColumnText( sql_t* p_sql, sql_stmt_t* p_stmt,
533         int i_col, char** pp_res )
534 {
535     sql_value_t tmp;
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;
539     return i_ret;
540 }
541
542 /**
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
549  */
550 static inline int sql_GetColumnBlob( sql_t* p_sql, sql_stmt_t* p_stmt,
551         int i_col, void** pp_res )
552 {
553     sql_value_t tmp;
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;
557     return i_ret;
558 }
559
560 # ifdef __cplusplus
561 }
562 # endif /* C++ extern "C" */
563
564 #endif /* VLC_SQL_H */