Skip to content

Commit

Permalink
Added stored proc and cursor, minor changes to interface
Browse files Browse the repository at this point in the history
  • Loading branch information
vimsh committed Jul 29, 2016
1 parent 798c866 commit 33eb670
Show file tree
Hide file tree
Showing 9 changed files with 1,105 additions and 418 deletions.
2 changes: 1 addition & 1 deletion Makefile_12.5
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ INCLUDES = -I/opt/sybase12_5_64/OCS/include

# compiler path and flags
CC = /opt/gcc/bin/g++
CFLAGS = -std=c++11 -Wall -Wno-unused -Wno-sequence-point -Wno-parentheses -c -ggdb3 -m64 -pthread -DSYB_LP64 -D_REENTRANT
CFLAGS = -std=c++1y -Wall -Wno-unused -Wno-sequence-point -Wno-parentheses -c -ggdb3 -m64 -pthread -DSYB_LP64 -D_REENTRANT

# sybase libraries to include in the build
CTLIB = -lct_r64 # client library
Expand Down
2 changes: 1 addition & 1 deletion Makefile_15.0
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ INCLUDES = -I/opt/sybase15/OCS-15_0/include

# compiler path and flags
CC = /opt/gcc/bin/g++
CFLAGS = -std=c++11 -Wall -Wno-unused -Wno-sequence-point -Wno-parentheses -c -ggdb3 -m64 -pthread -DSYB_LP64 -D_REENTRANT
CFLAGS = -std=c++1y -Wall -Wno-unused -Wno-sequence-point -Wno-parentheses -c -ggdb3 -m64 -pthread -DSYB_LP64 -D_REENTRANT

# sybase libraries to include in the build
CTLIB = -lsybct64 # client library
Expand Down
8 changes: 1 addition & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,7 @@ Header files based C++ database connection library.

This project is to create an easy to use, database agnostic, modular, and easily extendable C++ database connection library.

At the moment this is work in progress and not a production-ready code that may have bugs!

Currently only a limited featured native Sybase driver is implemented, see sybase_example.cpp
Currently only a native Sybase ASE driver is implemented, see sybase_example.cpp

If someone would like to contribute please ping me.

TODO:
- add support for more data types, stored procs, and cursor to Sybase driver
- write unit tests
- eventually add support for more databases
6 changes: 2 additions & 4 deletions connection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@
#define CONNECTION_HPP

#include <memory>
#include "utilities.hpp"
#include "driver.hpp"
#include "statement.hpp"

// forward declaration
Expand Down Expand Up @@ -110,8 +108,8 @@ class connection {
}

/**
* Function sets autocommit option, default is 'true'
* @param ac - true to set autocommit ON, false - OFF
* Function sets auto-commit option, default is 'true'
* @param ac - true to set auto-commit ON, false - OFF
*/
void autocommit(bool ac)
{
Expand Down
2 changes: 1 addition & 1 deletion driver.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ template <typename T>
class driver : public T
{
public:
// c++11 guaranties no threading issues
// c++1y guarantees no threading issues
static T& load()
{
static driver<T> d;
Expand Down
49 changes: 40 additions & 9 deletions result_set.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
#ifndef RESULT_SET_HPP
#define RESULT_SET_HPP

#include "utilities.hpp"

namespace vgi { namespace dbconn { namespace dbi {

class statement;
Expand All @@ -37,6 +39,9 @@ struct iresult_set
virtual size_t rows_affected() const = 0;
virtual size_t column_count() const = 0;
virtual bool next() = 0;
virtual bool prev() = 0;
virtual bool first() = 0;
virtual bool last() = 0;
virtual std::string column_name(size_t col_idx) = 0;
virtual int column_index(const std::string& col_name) = 0;
virtual bool is_null(size_t col_idx) = 0;
Expand All @@ -48,7 +53,6 @@ struct iresult_set
virtual uint64_t get_ulong(size_t col_idx) = 0;
virtual float get_float(size_t col_idx) = 0;
virtual double get_double(size_t col_idx) = 0;
virtual long double get_ldouble(size_t col_idx) = 0;
virtual bool get_bool(size_t col_idx) = 0;
virtual char get_char(size_t col_idx) = 0;
virtual std::string get_string(size_t col_idx) = 0;
Expand All @@ -57,8 +61,7 @@ struct iresult_set
virtual time_t get_datetime(size_t col_idx) = 0;
virtual char16_t get_unichar(size_t col_idx) = 0;
virtual std::u16string get_unistring(size_t col_idx) = 0;
virtual std::vector<uint8_t> get_image(size_t col_idx) = 0;
// TODO: add xml, binary, money, lob
virtual std::vector<uint8_t> get_binary(size_t col_idx) = 0;
};


Expand Down Expand Up @@ -165,8 +168,8 @@ class result_set

/**
* Function returns column name by column index or throws an exception if
* index is invalid
* @param col_idx
* index is invalid
* @param col_idx
* @return column name string or exception is thrown if index is invalid
*/
const std::string column_name(size_t col_idx)
Expand Down Expand Up @@ -194,6 +197,36 @@ class result_set
return rs_impl->next();
}

/**
* Function moves iterator to the previous row of the current result data set
* This function can only be used with scrollable cursor.
* @return true on success, or false if there is no more rows
*/
bool prev()
{
return rs_impl->prev();
}

/**
* Function moves iterator to the first row of the current result data set
* This function can only be used with scrollable cursor.
* @return true on success, or false if there is no more rows
*/
bool first()
{
return rs_impl->first();
}

/**
* Function moves iterator to the last row of the current result data set
* This function can only be used with scrollable cursor.
* @return true on success, or false if there is no more rows
*/
bool last()
{
return rs_impl->last();
}

/**
* Function checks if cell data is NULL by column index or throws an exception
* if index is invalid.
Expand Down Expand Up @@ -235,8 +268,6 @@ class result_set
float get_type_by_name(float);
double get_type_by_index(double);
double get_type_by_name(double);
long double get_type_by_index(ldouble);
long double get_type_by_name(ldouble);
bool get_type_by_index(bool);
bool get_type_by_name(bool);
char get_type_by_index(char);
Expand All @@ -253,8 +284,8 @@ class result_set
char16_t get_type_by_name(unichar);
std::u16string get_type_by_index(unistring);
std::u16string get_type_by_name(unistring);
std::vector<uint8_t> get_type_by_index(image);
std::vector<uint8_t> get_type_by_name(image);
std::vector<uint8_t> get_type_by_index(binary);
std::vector<uint8_t> get_type_by_name(binary);


private:
Expand Down
46 changes: 31 additions & 15 deletions statement.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,11 @@ struct istatement
{
virtual ~istatement() { };
virtual void prepare(const std::string& sql) = 0;
virtual iresult_set* execute(const std::string& sql) = 0;
virtual void call(const std::string& sql) = 0;
virtual iresult_set* execute(const std::string& sql, bool cursor = false, bool scrollable = false) = 0;
virtual iresult_set* execute() = 0;
virtual bool cancel() = 0;
virtual bool cancel_all() = 0;
virtual int proc_retval() = 0;
virtual void set_null(size_t col_idx) = 0;
virtual void set_short(size_t col_idx, int16_t val) = 0;
virtual void set_ushort(size_t col_idx, uint16_t val) = 0;
Expand All @@ -46,7 +47,6 @@ struct istatement
virtual void set_ulong(size_t col_idx, uint64_t val) = 0;
virtual void set_float(size_t col_idx, float val) = 0;
virtual void set_double(size_t col_idx, double val) = 0;
virtual void set_ldouble(size_t col_idx, long double val) = 0;
virtual void set_bool(size_t col_idx, bool val) = 0;
virtual void set_char(size_t col_idx, char val) = 0;
virtual void set_string(size_t col_idx, const std::string& val) = 0;
Expand All @@ -55,7 +55,7 @@ struct istatement
virtual void set_datetime(size_t col_idx, time_t val) = 0;
virtual void set_unichar(size_t col_idx, char16_t val) = 0;
virtual void set_unistring(size_t col_idx, const std::u16string& val) = 0;
virtual void set_image(size_t col_idx, const std::vector<uint8_t>& val) = 0;
virtual void set_binary(size_t col_idx, const std::vector<uint8_t>& val) = 0;
};


Expand Down Expand Up @@ -101,6 +101,15 @@ class statement
stmt_impl->prepare(sql);
}

/**
* Function prepares SQL stored procedure
* @param sql stored procedure to be executed
*/
void call(const std::string& proc)
{
stmt_impl->call(proc);
}

/**
* Function runs last executed SQL statement
* @return result set object
Expand All @@ -113,22 +122,35 @@ class statement
/**
* Function runs SQL statement
* @param sql statement to be executed
* @param cursor = true to use cursor with select statements
* @param scrollable = true to use scrollable cursor
* @return result set object
*/
result_set execute(const std::string& sql)
result_set execute(const std::string& sql, bool cursor = false, bool scrollable = false)
{
return result_set(stmt_impl->execute(sql));
return result_set(stmt_impl->execute(sql, cursor, scrollable));
}

/**
* Function cancels currently running SQL statement
* Function cancels currently running SQL statements
* @return true if canceled, false otherwise
*/
bool cancel()
{
return stmt_impl->cancel();
}

/**
* Function returns stored procedure return value.
* This function must be called after all result sets from stored proc select
* statements had been processed
* @return int
*/
int proc_retval()
{
return stmt_impl->proc_retval();
}

virtual void set_null(size_t col_idx)
{
stmt_impl->set_null(col_idx);
Expand Down Expand Up @@ -174,11 +196,6 @@ class statement
stmt_impl->set_double(col_idx, val);
}

virtual void set_ldouble(size_t col_idx, long double val)
{
stmt_impl->set_ldouble(col_idx, val);
}

virtual void set_bool(size_t col_idx, bool val)
{
stmt_impl->set_bool(col_idx, val);
Expand Down Expand Up @@ -219,11 +236,10 @@ class statement
stmt_impl->set_unistring(col_idx, val);
}

virtual void set_image(size_t col_idx, const std::vector<uint8_t>& val)
virtual void set_binary(size_t col_idx, const std::vector<uint8_t>& val)
{
stmt_impl->set_image(col_idx, val);
stmt_impl->set_binary(col_idx, val);
}
// TODO: add xml, binary, money, lob

private:
friend class connection;
Expand Down
Loading

0 comments on commit 33eb670

Please sign in to comment.