From debb382da72a2d93bc23ee45544d3fc667e26559 Mon Sep 17 00:00:00 2001
From: Srikanth Raju <srikiraju@gmail.com>
Date: Thu, 24 Sep 2009 12:21:55 +0530
Subject: [PATCH] Sql: Include interface for prepared statements.

pf_commit and pf_rollback should have a return code
Include some documentation
---
 include/vlc_sql.h |  157 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 157 insertions(+), 0 deletions(-)

diff --git a/include/vlc_sql.h b/include/vlc_sql.h
index 14c2a21..32b3948 100644
--- a/include/vlc_sql.h
+++ b/include/vlc_sql.h
@@ -39,11 +39,37 @@ extern "C" {
  * General structure: SQL object.
  *****************************************************************************/
 
+#define VLC_SQL_ROW 1
+#define VLC_SQL_DONE 2
+
 typedef struct sql_t sql_t;
 typedef struct sql_sys_t sql_sys_t;
+typedef struct sql_stmt_t sql_stmt_t;
+typedef struct sql_stmt_sys_t sql_stmt_sys_t;
+typedef struct sql_value_t sql_value_t;
 
 typedef int ( *sql_query_callback_t ) ( void*, int, char**, char** );
 
+typedef enum {
+    SQL_INT,
+    SQL_DOUBLE,
+    SQL_TEXT,
+    SQL_BLOB,
+    SQL_NULL
+} sql_type_e;
+
+struct sql_value_t
+{
+    int length;
+    union
+    {
+        int i;
+        double dbl;
+        char* psz;
+        void* ptr;
+    } value;
+};
+
 struct sql_t
 {
     VLC_COMMON_MEMBERS
@@ -84,8 +110,38 @@ struct sql_t
 
     /** Rollback transaction */
     int (*pf_rollback) ( sql_t* );
+
+    /** Create a statement object */
+    sql_stmt_t* (*pf_prepare) ( sql_t* p_sql, const char* psz_fmt,
+                                int i_length );
+
+    /** Bind parameters to a statement */
+    int (*pf_bind) ( sql_t* p_sql, sql_stmt_t* p_stmt, sql_type_e type,
+                             sql_value_t* p_value, int i_pos );
+
+    /** Run the prepared statement */
+    int (*pf_run) ( sql_t* p_sql, sql_stmt_t* p_stmt );
+
+    /** Reset the prepared statement */
+    int (*pf_reset) ( sql_t* p_sql, sql_stmt_t* p_stmt );
+
+    /** Destroy the statement object */
+    int (*pf_finalize) ( sql_t* p_sql, sql_stmt_t* p_stmt );
+
+    /** Get the datatype for a specified column */
+    int (*pf_gettype) ( sql_t* p_sql, sql_stmt_t* p_stmt, int i_col,
+                        sql_type_e* type );
+
+    /** Get the data from a specified column */
+    int (*pf_getcolumn) ( sql_t* p_sql, sql_stmt_t* p_stmt, int i_col,
+                          sql_type_e type, sql_value_t *p_res );
 };
 
+struct sql_stmt_t
+{
+    sql_t* p_sql;
+    sql_stmt_sys_t *p_sys;
+};
 
 /*****************************************************************************
  * SQL Function headers
@@ -249,6 +305,107 @@ static inline int sql_RollbackTransaction( sql_t *p_sql )
     return p_sql->pf_rollback( p_sql );
 }
 
+/**
+ * @brief Prepare an sql statement
+ * @param p_sql The SQL object
+ * @param psz_fmt SQL query string
+ * @param i_length length of the string
+ * @return a sql_stmt_t pointer or NULL on failure
+ */
+static inline sql_stmt_t* sql_Prepare( sql_t* p_sql, const char* psz_fmt,
+        int i_length )
+{
+    return p_sql->pf_prepare( p_sql, psz_fmt, i_length );
+}
+
+/**
+ * @brief Bind arguments to a sql_stmt_t object
+ * @param p_sql The SQL object
+ * @param p_stmt Statement Object
+ * @param type Data type of the value
+ * @param p_value Value to be bound
+ * @param i_length For values which require a length
+ * @param i_pos Position at which the parameter should be bound
+ * @return VLC_SUCCESS or VLC_EGENERIC
+ */
+static inline int sql_BindGeneric( sql_t* p_sql, sql_stmt_t* p_stmt,
+        sql_type_e type, sql_value_t* p_value, int i_pos )
+{
+    return p_sql->pf_bind( p_sql, p_stmt, type, p_value, i_pos );
+}
+#define sql_BindInteger( A, B, C, D ) sql_BindGeneric( A, B, SQL_INT, C, D )
+#define sql_BindDouble( A, B, C, D )  sql_BindGeneric( A, B, SQL_DOUBLE, C, D )
+#define sql_BindText( A, B, C, D )    sql_BindGeneric( A, B, SQL_TEXT, C, D )
+#define sql_BindBlob( A, B, C, D )    sql_BindGeneric( A, B, SQL_BLOB, C, D )
+#define sql_BindNull( A, B, C )       sql_BindGeneric( A, B, SQL_NULL, NULL, C )
+
+/**
+ * @brief Run the SQL statement
+ * @param p_sql The SQL object
+ * @param p_stmt The statement
+ * @return VLC_SUCCESS or VLC_EGENERIC
+ */
+static inline int sql_Run( sql_t* p_sql, sql_stmt_t* p_stmt )
+{
+    return p_sql->pf_run( p_sql, p_stmt );
+}
+
+/**
+ * @brief Reset the SQL statement
+ * @param p_sql The SQL object
+ * @param p_stmt The sql statement object
+ * @return VLC_SUCCESS or VLC_EGENERIC
+ */
+static inline int sql_Reset( sql_t* p_sql, sql_stmt_t* p_stmt )
+{
+    return p_sql->pf_reset( p_sql, p_stmt );
+}
+
+/**
+ * @brief Destroy the sql statement object
+ * @param p_sql The SQL object
+ * @param p_stmt The statement object
+ * @return VLC_SUCCESS or VLC_EGENERIC
+ */
+static inline int sql_Finalize( sql_t* p_sql, sql_stmt_t* p_stmt )
+{
+    return p_sql->pf_finalize( p_sql, p_stmt );
+}
+
+/**
+ * @brief Get the datatype of the result of the column
+ * @param p_sql The SQL object
+ * @param p_stmt The sql statement object
+ * @param i_col The column
+ * @param type pointer to datatype of the given column
+ * @return VLC_SUCCESS or VLC_EGENERIC
+ */
+static inline int sql_GetColumnType( sql_t* p_sql, sql_stmt_t* p_stmt,
+        int i_col, sql_type_e* type )
+{
+    return p_sql->pf_gettype( p_sql, p_stmt, i_col, type );
+}
+
+/**
+ * @brief Get the column data
+ * @param p_sql The SQL object
+ * @param p_stmt The statement object
+ * @param i_col The column number
+ * @param type Datatype of result
+ * @param p_res The structure which contains the value of the result
+ * @return VLC_SUCCESS or VLC_EGENERIC
+ */
+static inline int sql_GetColumn( sql_t* p_sql, sql_stmt_t* p_stmt,
+        int i_col, sql_type_e type, sql_value_t *p_res )
+{
+    return p_sql->pf_getcolumn( p_sql, p_stmt, i_col, type, p_res );
+}
+
+#define sql_GetIntegerColumn( A, B, C, D ) sql_GetColumn( A, B, C, SQL_INT, D )
+#define sql_GetDoubleColumn( A, B, C, D ) sql_GetColumn( A, B, C, SQL_DOUBLE, D )
+#define sql_GetTextColumn( A, B, C, D ) sql_GetColumn( A, B, C, SQL_TEXT, D )
+#define sql_GetBlobColumn( A, B, C, D ) sql_GetColumn( A, B, C, SQL_BLOB, D )
+
 # ifdef __cplusplus
 }
 # endif /* C++ extern "C" */
-- 
1.6.0.4

