From d7540d7921f007d3bb1405be5ce60db07adb26da Mon Sep 17 00:00:00 2001
From: Srikanth Raju <srikiraju@gmail.com>
Date: Wed, 16 Sep 2009 02:11:08 +0530
Subject: [PATCH] Sql Core

This includes support for a SQL-based modules in Core VLC
---
 include/vlc_sql.h  |  247 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 src/Makefile.am    |    2 +
 src/libvlccore.sym |    2 +
 src/misc/sql.c     |   95 ++++++++++++++++++++
 4 files changed, 346 insertions(+), 0 deletions(-)
 create mode 100644 include/vlc_sql.h
 create mode 100644 src/misc/sql.c

diff --git a/include/vlc_sql.h b/include/vlc_sql.h
new file mode 100644
index 0000000..be1be75
--- /dev/null
+++ b/include/vlc_sql.h
@@ -0,0 +1,247 @@
+/*****************************************************************************
+ * 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 ( *pf_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 *, pf_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, char *psz_name,
+            char* psz_host, int i_port, char* psz_user, 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,
+                                     pf_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 */
diff --git a/src/Makefile.am b/src/Makefile.am
index 78a92bf..e3198a4 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -86,6 +86,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 \
@@ -428,6 +429,7 @@ SOURCES_libvlc_common = \
 	misc/filter.c \
 	misc/filter_chain.c \
 	misc/http_auth.c \
+	misc/sql.c \
 	$(NULL)
 
 SOURCES_libvlc_httpd = \
diff --git a/src/libvlccore.sym b/src/libvlccore.sym
index 01cbbb4..2d020be 100644
--- a/src/libvlccore.sym
+++ b/src/libvlccore.sym
@@ -372,6 +372,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
index 0000000..9ea0fcb
--- /dev/null
+++ b/src/misc/sql.c
@@ -0,0 +1,95 @@
+/*****************************************************************************
+ * 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
+/*
+ * @brief Create an instance of an SQL connection
+ * @param p_this Parent object
+ * @param psz_name Name which is passed to module_need (not needed)
+ * @return p_sql created and attached, module loaded
+ */
+sql_t *sql_Create( vlc_object_t *p_this, char *psz_name, char* psz_host,
+       int i_port, char* psz_user, 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, false );
+    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
+/**
+ * @brief Destructor for p_sql object
+ * @param obj This p_sql object
+ * @return Nothing
+ */
+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 );
+}
-- 
1.6.0.4

