From c39d12d4ec8fb7c092a62ff085ae080316153f75 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.

---
 include/vlc_sql.h |  317 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 317 insertions(+), 0 deletions(-)

diff --git a/include/vlc_sql.h b/include/vlc_sql.h
index 14c2a21..1e4e452 100644
--- a/include/vlc_sql.h
+++ b/include/vlc_sql.h
@@ -30,6 +30,8 @@
 #ifndef VLC_SQL_H
 # define VLC_SQL_H
 
+#include <assert.h>
+
 # ifdef __cplusplus
 extern "C" {
 # endif
@@ -39,11 +41,39 @@ extern "C" {
  * General structure: SQL object.
  *****************************************************************************/
 
+/**
+ * Return values for the function @see sql_Run()
+ */
+#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 int ( *sql_query_callback_t ) ( void*, int, char**, char** );
 
+typedef enum {
+    SQL_NULL,
+    SQL_INT,
+    SQL_DOUBLE,
+    SQL_TEXT,
+    SQL_BLOB
+} sql_type_e;
+
+typedef struct
+{
+    int length;
+    union
+    {
+        int i;
+        double dbl;
+        char* psz;
+        void* ptr;
+    } value;
+} sql_value_t;
+
 struct sql_t
 {
     VLC_COMMON_MEMBERS
@@ -84,8 +114,34 @@ 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* p_fmt,
+                                int i_length );
+
+    /** Bind parameters to a statement */
+    int (*pf_bind) ( sql_t* p_sql, sql_stmt_t* p_stmt, int i_pos,
+                    unsigned int type, const sql_value_t* p_value );
+
+    /** 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,
+                        int* type );
+
+    /** Get the data from a specified column */
+    int (*pf_getcolumn) ( sql_t* p_sql, sql_stmt_t* p_stmt, int i_col,
+                          int type, sql_value_t *p_res );
 };
 
+struct sql_stmt_t;
 
 /*****************************************************************************
  * SQL Function headers
@@ -249,6 +305,267 @@ 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. If negative, length will be
+ * considered upto the first \0 character equivalent to strlen(psz_fmt).
+ * Otherwise the first i_length bytes will be used 
+ * @return a sql_stmt_t pointer or NULL on failure
+ */
+static inline sql_stmt_t* sql_Prepare( sql_t* p_sql, const char* p_fmt,
+        int i_length )
+{
+    return p_sql->pf_prepare( p_sql, p_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_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,
+        int i_pos, int type, const sql_value_t* p_value )
+{
+    return p_sql->pf_bind( p_sql, p_stmt, i_pos, type, p_value );
+}
+
+/**
+ * @brief Bind a NULL value to a position
+ * @param p_sql The SQL object
+ * @param p_stmt Statement Object
+ * @param i_pos Position at which the parameter should be bound
+ * @return VLC_SUCCESS or VLC_EGENERIC
+ */
+static inline int sql_BindNull( sql_t *p_sql, sql_stmt_t* p_stmt, int i_pos )
+{
+    int i_ret = sql_BindGeneric( p_sql, p_stmt, i_pos, SQL_NULL, NULL );
+    return i_ret;
+}
+
+/**
+ * @brief Bind an integer to the statement object at some position
+ * @param p_sql The SQL object
+ * @param p_stmt Statement Object
+ * @param i_pos Position at which the parameter should be bound
+ * @param i_int Value to be bound
+ * @return VLC_SUCCESS or VLC_EGENERIC
+ */
+static inline int sql_BindInteger( sql_t *p_sql, sql_stmt_t* p_stmt,
+                                   int i_pos, int i_int )
+{
+    sql_value_t p_value;
+    p_value.length = 0;
+    p_value.value.i = i_int;
+    int i_ret = sql_BindGeneric( p_sql, p_stmt, i_pos, SQL_INT, &p_value );
+    return i_ret;
+}
+
+/**
+ * @brief Bind a double to the statement object at some position
+ * @param p_sql The SQL object
+ * @param p_stmt Statement Object
+ * @param i_pos Position at which the parameter should be bound
+ * @param d_dbl Value to be bound
+ * @return VLC_SUCCESS or VLC_EGENERIC
+ */
+static inline int sql_BindDouble( sql_t *p_sql, sql_stmt_t* p_stmt,
+                                  int i_pos, double d_dbl )
+{
+    sql_value_t p_value;
+    p_value.length = 0;
+    p_value.value.dbl = d_dbl;
+    int i_ret = sql_BindGeneric( p_sql, p_stmt, i_pos, SQL_INT, &p_value );
+    return i_ret;
+}
+
+/**
+ * @brief Bind Text to the statement
+ * @param p_sql The SQL object
+ * @param p_stmt Statement Object
+ * @param i_pos Position at which the parameter should be bound
+ * @param p_fmt Value to be bound
+ * @param i_length Length of text. If -ve text upto the first null char
+ * will be selected.
+ * @return VLC_SUCCESS or VLC_EGENERIC
+ */
+static inline int sql_BindText( sql_t *p_sql, sql_stmt_t* p_stmt, int i_pos,
+                                   char* p_fmt, int i_length )
+{
+    sql_value_t p_value;
+    p_value.length = i_length;
+    p_value.value.psz = p_fmt;
+    int i_ret = sql_BindGeneric( p_sql, p_stmt, i_pos, SQL_TEXT, &p_value );
+    return i_ret;
+}
+
+/**
+ * @brief Bind a binary object to the statement
+ * @param p_sql The SQL object
+ * @param p_stmt Statement Object
+ * @param i_pos Position at which the parameter should be bound
+ * @param p_ptr Value to be bound
+ * @param i_length Size of the blob to read
+ * @return VLC_SUCCESS or VLC_EGENERIC
+ */
+static inline int sql_BindBlob( sql_t *p_sql, sql_stmt_t* p_stmt, int i_pos,
+                                   void* p_ptr, int i_length )
+{
+    sql_value_t p_value;
+    p_value.length = i_length;
+    p_value.value.ptr = p_ptr;
+    int i_ret = sql_BindGeneric( p_sql, p_stmt, i_pos, SQL_INT, &p_value );
+    return i_ret;
+}
+
+/**
+ * @brief Run the SQL statement. If the statement fetches data, then only
+ * one row of the data is fetched at a time. Run this function again to
+ * fetch the next row.
+ * @param p_sql The SQL object
+ * @param p_stmt The statement
+ * @return VLC_SQL_DONE if done fetching all rows or there are no rows to fetch
+ * VLC_SQL_ROW if a row was fetched for this statement.
+ * VLC_EGENERIC if this function failed
+ */
+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. Resetting the statement will unbind all
+ * the values that were bound on this 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. This will free memory.
+ * @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, int* 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, int type, sql_value_t *p_res )
+{
+    return p_sql->pf_getcolumn( p_sql, p_stmt, i_col, type, p_res );
+}
+
+/**
+ * @brief Get an integer from the results of a statement
+ * @param p_sql The SQL object
+ * @param p_stmt The statement object
+ * @param i_col The column number
+ * @param i_res Pointer of the location for result to be stored
+ * @return VLC_SUCCESS or VLC_EGENERIC
+ */
+static inline int sql_GetColumnInteger( sql_t* p_sql, sql_stmt_t* p_stmt,
+        int i_col, int* i_res )
+{
+    assert( i_res != NULL );
+    sql_value_t p_tmp;
+    int i_ret = p_sql->pf_getcolumn( p_sql, p_stmt, i_col, SQL_INT, &p_tmp );
+    if( i_ret == VLC_SUCCESS )
+        *i_res = p_tmp.value.i;
+    return i_ret;
+}
+
+/**
+ * @brief Get a double from the results of a statement
+ * @param p_sql The SQL object
+ * @param p_stmt The statement object
+ * @param i_col The column number
+ * @param d_res Pointer of the location for result to be stored
+ * @return VLC_SUCCESS or VLC_EGENERIC
+ */
+static inline int sql_GetColumnDouble( sql_t* p_sql, sql_stmt_t* p_stmt,
+        int i_col, double* d_res )
+{
+    assert( d_res != NULL );
+    sql_value_t p_tmp;
+    int i_ret = p_sql->pf_getcolumn( p_sql, p_stmt, i_col, SQL_DOUBLE, &p_tmp );
+    if( i_ret == VLC_SUCCESS )
+        *d_res = p_tmp.value.dbl;
+    return i_ret;
+}
+
+/**
+ * @brief Get some text from the results of a statement
+ * @param p_sql The SQL object
+ * @param p_stmt The statement object
+ * @param i_col The column number
+ * @param p_res Pointer of the location for result to be stored
+ * @return VLC_SUCCESS or VLC_EGENERIC
+ */
+static inline int sql_GetColumnText( sql_t* p_sql, sql_stmt_t* p_stmt,
+        int i_col, char** p_res )
+{
+    assert( p_res != NULL );
+    sql_value_t p_tmp;
+    int i_ret = p_sql->pf_getcolumn( p_sql, p_stmt, i_col, SQL_TEXT, &p_tmp );
+    if( i_ret == VLC_SUCCESS )
+        *p_res = p_tmp.value.psz;
+    return i_ret;
+}
+
+/**
+ * @brief Get a blob from the results of a statement
+ * @param p_sql The SQL object
+ * @param p_stmt The statement object
+ * @param i_col The column number
+ * @param p_res Pointer of the location for result to be stored
+ * @return VLC_SUCCESS or VLC_EGENERIC
+ */
+static inline int sql_GetColumnBlob( sql_t* p_sql, sql_stmt_t* p_stmt,
+        int i_col, void** p_res )
+{
+    assert( p_res != NULL );
+    sql_value_t p_tmp;
+    int i_ret = p_sql->pf_getcolumn( p_sql, p_stmt, i_col, SQL_BLOB, &p_tmp );
+    if( i_ret == VLC_SUCCESS )
+        *p_res = p_tmp.value.ptr;
+    return i_ret;
+}
+
 # ifdef __cplusplus
 }
 # endif /* C++ extern "C" */
-- 
1.6.3.3

