]> git.sesse.net Git - vlc/commitdiff
Sql Core
authorSrikanth Raju <srikiraju@gmail.com>
Tue, 15 Sep 2009 20:41:08 +0000 (02:11 +0530)
committerLaurent Aimar <fenrir@videolan.org>
Wed, 23 Sep 2009 17:51:52 +0000 (19:51 +0200)
This includes support for a SQL-based modules in Core VLC

Signed-off-by: Laurent Aimar <fenrir@videolan.org>
include/vlc_sql.h [new file with mode: 0644]
src/Makefile.am
src/libvlccore.sym
src/misc/sql.c [new file with mode: 0644]

diff --git a/include/vlc_sql.h b/include/vlc_sql.h
new file mode 100644 (file)
index 0000000..6d97fd1
--- /dev/null
@@ -0,0 +1,248 @@
+/*****************************************************************************
+ * vlc_sql.h: SQL abstraction layer
+ *****************************************************************************
+ * Copyright (C) 2009 the VideoLAN team
+ * $Id$
+ *
+ * Authors: Antoine Lejeune <phytos@videolan.org>
+ *          Jean-Philippe AndrĂ© <jpeg@videolan.org>
+ *          Srikanth Raju <srikiraju@gmail.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
+ *****************************************************************************/
+
+#if !defined( __LIBVLC__ )
+# error You are not libvlc or one of its plugins. You cannot include this file
+#endif
+
+#ifndef VLC_SQL_H
+# define VLC_SQL_H
+
+# ifdef __cplusplus
+extern "C" {
+# endif
+
+
+/*****************************************************************************
+ * General structure: SQL object.
+ *****************************************************************************/
+
+typedef struct sql_t sql_t;
+typedef struct sql_sys_t sql_sys_t;
+
+typedef int ( *sql_query_callback_t ) ( void*, int, char**, char** );
+
+struct sql_t
+{
+    VLC_COMMON_MEMBERS
+
+    /** Module properties */
+    module_t  *p_module;
+
+    /** Connection Data */
+    char *psz_host;         /**< Location or host of the database */
+    char *psz_user;         /**< Username used to connect to database */
+    char *psz_pass;         /**< Password used to connect to database */
+    int i_port;             /**< Port on which database is running */
+
+    /** Internal data */
+    sql_sys_t *p_sys;
+
+    /** Perform a query with a row-by-row callback function */
+    int (*pf_query_callback) ( sql_t *, const char *, sql_query_callback_t, void * );
+
+    /** Perform a query and return result directly */
+    int (*pf_query) ( sql_t *, const char *, char ***, int *, int * );
+
+    /** Get database tables */
+    int (*pf_get_tables) ( sql_t *, char *** );
+
+    /** Free result of a call to sql_Query or sql_GetTables */
+    void (*pf_free) ( sql_t *, char ** );
+
+    /** vmprintf replacement for SQL */
+    char* (*pf_vmprintf) ( const char*, va_list args );
+
+    /** Begin transaction */
+    int (*pf_begin) ( sql_t* );
+
+    /** Commit transaction */
+    void (*pf_commit) ( sql_t* );
+
+    /** Rollback transaction */
+    void (*pf_rollback) ( sql_t* );
+};
+
+
+/*****************************************************************************
+ * SQL Function headers
+ *****************************************************************************/
+
+/**
+ * @brief Create a new SQL object.
+ * @param p_this Parent object to attach the SQL object to.
+ * @param psz_host URL to the database
+ * @param i_port Port on which the database is running
+ * @param psz_user Username to access the database
+ * @param psz_pass Password for the database
+ * @return The VLC SQL object, type sql_t.
+ **/
+VLC_EXPORT( sql_t*, sql_Create, ( vlc_object_t *p_this, const char *psz_name,
+            const char* psz_host, int i_port,
+            const char* psz_user, const char* psz_pass ) );
+#define sql_Create( a, b, c, d, e, f ) sql_Create( VLC_OBJECT(a), b, c, d, e, f )
+
+
+/**
+ * @brief Destructor for p_sql object
+ * @param obj This p_sql object
+ * @return Nothing
+ */
+VLC_EXPORT( void, sql_Destroy, ( vlc_object_t *obj ) );
+#define sql_Destroy( a ) sql_Destroy( VLC_OBJECT( a ) )
+
+
+/**
+ * @brief Perform a query using a callback function
+ * @param p_sql This SQL object.
+ * @param psz_query The SQL query string.
+ * @param pf_callback A callback function that will be called for each row of
+ * the result: 1st argument is be p_opaque,
+ *             2nd argument is the number of columns,
+ *             3rd is the result columns (array of strings),
+ *             4th is the columns names (array of strings).
+ * @param p_opaque Any pointer to an object you may need in the callback.
+ * @return VLC_SUCCESS or VLC_EGENERIC.
+ * @note The query will not necessarily be processed in a separate thread!
+ **/
+static inline int sql_QueryCallback( sql_t *p_sql, const char *psz_query,
+                                     sql_query_callback_t pf_callback,
+                                     void *p_opaque )
+{
+    return p_sql->pf_query_callback( p_sql, psz_query, pf_callback, p_opaque );
+}
+
+/**
+ * @brief Perform a query directly
+ * @param p_sql This SQL object.
+ * @param psz_query The SQL query string.
+ * @param pppsz_result A pointer to a array of strings: result of the query.
+ * Dynamically allocated.
+ * @param pi_rows Pointer to an integer that will receive the number of result
+ * rows written.
+ * @param pi_cols Pointer to an integer that will receive the number of result
+ * columns written.
+ * @return VLC_SUCCESS or VLC_EGENERIC.
+ * @note pppsz_result will point to an array of strings, ppsz_result.
+ * This array of strings contains actually a 2D-matrix of strings where the
+ * first row (row 0) contains the SQL table header names.
+ * *pi_rows will be the number of result rows, so that the number of text rows
+ * in ppsz_result will be (*pi_rows + 1) (because of row 0).
+ * To get result[row,col] use (*pppsz_result)[ (row+1) * (*pi_cols) + col ].
+ **/
+static inline int sql_Query( sql_t *p_sql, const char *psz_query,
+                             char ***pppsz_result, int *pi_rows, int *pi_cols )
+{
+    return p_sql->pf_query( p_sql, psz_query, pppsz_result, pi_rows, pi_cols );
+}
+
+/**
+ * @brief Get database table name list
+ * @param p_sql This SQL object.
+ * @param pppsz_tables Pointer to an array of strings. Dynamically allocated.
+ * Similar to pppsz_result of sql_Query but with only one row.
+ * @return Number of tables or <0 in case of error.
+ **/
+static inline int sql_GetTables( sql_t *p_sql, char ***pppsz_tables )
+{
+    return p_sql->pf_get_tables( p_sql, pppsz_tables );
+}
+
+/**
+ * @brief Free the result of a query.
+ * @param p_sql This SQL object.
+ * @param ppsz_result The result of sql_Query or sql_GetTables. See above.
+ * @return Nothing.
+ **/
+static inline void sql_Free( sql_t *p_sql, char **ppsz_result )
+{
+    p_sql->pf_free( p_sql, ppsz_result );
+}
+
+/**
+ * @brief printf-like function that can escape forbidden/reserved characters.
+ * @param p_sql This SQL object.
+ * @param psz_fmt Format of the string (with %q, %Q and %z enabled).
+ * @param ... Printf arguments
+ * @return Dynamically allocated string or NULL in case of error.
+ * @note Refer to SQLite documentation for more details about %q, %Q and %z.
+ **/
+static inline char* sql_Printf( sql_t *p_sql, const char *psz_fmt, ... )
+{
+    va_list args;
+    va_start( args, psz_fmt );
+    char *r = p_sql->pf_vmprintf( psz_fmt, args );
+    va_end( args );
+    return r;
+}
+
+/**
+ * @brief vprintf replacement for SQL queries, escaping forbidden characters
+ * @param p_sql This SQL object
+ * @param psz_fmt Format of the string
+ * @param arg Variable list of arguments
+ * @return Dynamically allocated string or NULL in case of error.
+ **/
+static inline char* sql_VPrintf( sql_t *p_sql, const char *psz_fmt,
+                                 va_list arg )
+{
+    return p_sql->pf_vmprintf( psz_fmt, arg );
+}
+
+/**
+ * @brief Begin a SQL transaction
+ * @param p_sql The SQL object
+ * @return VLC error code or success
+ **/
+static inline int sql_BeginTransaction( sql_t *p_sql )
+{
+    return p_sql->pf_begin( p_sql );
+}
+
+/**
+ * @brief Commit a SQL transaction
+ * @param p_sql The SQL object
+ * @return VLC error code or success
+ **/
+static inline void sql_CommitTransaction( sql_t *p_sql )
+{
+    p_sql->pf_commit( p_sql );
+}
+
+/**
+ * @brief Rollback a SQL transaction
+ * @param p_sql The SQL object
+ * @return VLC error code or success
+ **/
+static inline void sql_RollbackTransaction( sql_t *p_sql )
+{
+    p_sql->pf_rollback( p_sql );
+}
+
+# ifdef __cplusplus
+}
+# endif /* C++ extern "C" */
+
+#endif /* VLC_SQL_H */
index 5f4528757219d34c5c3e5bd7d8ee12d3d52d86e5..d49d292f9ff1d3354e7c29c2c1baf4af9ba22c13 100644 (file)
@@ -89,6 +89,7 @@ pluginsinclude_HEADERS = \
        ../include/vlc_plugin.h \
        ../include/vlc_rand.h \
        ../include/vlc_services_discovery.h \
+       ../include/vlc_sql.h \
        ../include/vlc_sout.h \
        ../include/vlc_stream.h \
        ../include/vlc_strings.h \
@@ -436,6 +437,7 @@ SOURCES_libvlc_common = \
        misc/filter.c \
        misc/filter_chain.c \
        misc/http_auth.c \
+       misc/sql.c \
        $(NULL)
 
 SOURCES_libvlc_httpd = \
index ef24ff34b38deb8aab227895940a96f605448ae6..7590fdc4740d6901f5e8765d68e051562843be4b 100644 (file)
@@ -373,6 +373,8 @@ spu_DisplaySubpicture
 spu_Init
 spu_RenderSubpictures
 spu_SortSubpictures
+sql_Create
+sql_Destroy
 __stats_TimerClean
 __stats_TimerDump
 __stats_TimersCleanAll
diff --git a/src/misc/sql.c b/src/misc/sql.c
new file mode 100644 (file)
index 0000000..b2177bd
--- /dev/null
@@ -0,0 +1,86 @@
+/*****************************************************************************
+ * sql.c: SQL Connection: Creators and destructors
+ *****************************************************************************
+ * Copyright (C) 2008-2009 the VideoLAN team
+ * $Id$
+ *
+ * Authors: Srikanth Raju <srikiraju at gmail dot com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
+ *****************************************************************************/
+
+#if !defined( __LIBVLC__ )
+  #error You are not libvlc or one of its plugins. You cannot include this file
+#endif
+
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#include <vlc_common.h>
+#include <vlc_sql.h>
+#include <assert.h>
+#include "libvlc.h"
+
+#undef sql_Create
+sql_t *sql_Create( vlc_object_t *p_this, const char *psz_name,
+        const char* psz_host, int i_port,
+        const char* psz_user, const char* psz_pass )
+{
+    sql_t *p_sql;
+
+    p_sql = ( sql_t * ) vlc_custom_create( p_this, sizeof( sql_t ),
+                                           VLC_OBJECT_GENERIC, "sql" );
+    if( !p_sql )
+    {
+        msg_Err( p_this, "unable to create sql object" );
+        return NULL;
+    }
+    vlc_object_attach( p_sql, p_this );
+
+    p_sql->psz_host = strdup( psz_host );
+    p_sql->psz_user = strdup( psz_user );
+    p_sql->psz_pass = strdup( psz_pass );
+    p_sql->i_port = i_port;
+
+    p_sql->p_module = module_need( p_sql, "sql", psz_name,
+                                   psz_name && *psz_name );
+    if( !p_sql->p_module )
+    {
+        free( p_sql->psz_host );
+        free( p_sql->psz_user );
+        free( p_sql->psz_pass );
+        vlc_object_release( p_sql );
+        msg_Err( p_this, "SQL provider not found" );
+        return NULL;
+    }
+
+    return p_sql;
+}
+
+#undef sql_Destroy
+void sql_Destroy( vlc_object_t* obj )
+{
+    sql_t *p_sql = (sql_t *)obj;
+    assert( p_sql );
+
+    free( p_sql->psz_host );
+    free( p_sql->psz_user );
+    free( p_sql->psz_pass );
+
+    module_unneed( p_sql, p_sql->p_module );
+
+    vlc_object_release( obj );
+}