Skip to content

Commit e51a566

Browse files
committed
Added more features to Sybase driver
1 parent 0fbb076 commit e51a566

9 files changed

+1604
-599
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ This project is to create an easy to use, database agnostic, modular, and easily
55

66
This is work in progress and not a production-ready code that may have bugs!
77

8-
Currently only prmitive Sybase driver is implemented.
8+
Currently only a limited featured Sybase driver is implemented, see sybase_example.cpp
99

10-
Any volanteers are welcomed to contribute.
10+
Any volunteers are welcomed to contribute.
1111

1212
TODO:
13-
- add support for more features to Sybase driver
13+
- add support for more data types, stored procs, and cursor to Sybase driver
1414
- write unit tests
1515
- eventually add support for more databases

connection.hpp

Lines changed: 56 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
*/
1818

1919
#ifndef CONNECTION_HPP
20-
#define CONNECTION_HPP
20+
#define CONNECTION_HPP
2121

2222
#include <memory>
2323
#include "utilities.hpp"
@@ -44,6 +44,7 @@ struct iconnection
4444
virtual void disconnect() = 0;
4545
virtual void commit() = 0;
4646
virtual void rollback() = 0;
47+
virtual void autocommit(bool ac) = 0;
4748
virtual bool connected() const = 0;
4849
virtual bool alive() const = 0;
4950
virtual istatement* get_statement(iconnection&) = 0;
@@ -63,62 +64,114 @@ struct iconnection
6364
class connection {
6465

6566
public:
67+
/**
68+
* Move constructor
69+
* @param conn
70+
*/
6671
connection(connection&& conn) : conn_impl(std::move(conn.conn_impl))
6772
{
6873
}
6974

75+
/**
76+
* Move assign operator
77+
* @param conn
78+
* @return
79+
*/
7080
connection& operator=(connection&& conn)
7181
{
7282
if (this != &conn)
7383
conn_impl = std::move(conn.conn_impl);
7484
return *this;
7585
}
7686

87+
/**
88+
* Destructor
89+
*/
7790
~connection()
7891
{
7992
disconnect();
8093
}
8194

95+
/**
96+
* Function opens connection to the database
97+
* @return true on success, false otherwise
98+
*/
8299
bool connect()
83100
{
84101
return conn_impl->connect();
85102
}
86103

104+
/**
105+
* Function closes connection to the database
106+
*/
87107
void disconnect()
88108
{
89109
conn_impl->disconnect();
90110
}
91111

112+
/**
113+
* Function sets autocommit option, default is 'true'
114+
* @param ac - true to set autocommit ON, false - OFF
115+
*/
116+
void autocommit(bool ac)
117+
{
118+
return conn_impl->autocommit(ac);
119+
}
120+
121+
/**
122+
* Function commits changes to the database
123+
*/
92124
void commit()
93125
{
94126
conn_impl->commit();
95127
}
96128

129+
/**
130+
* Function rolls back uncommitted changes
131+
*/
97132
void rollback()
98133
{
99134
conn_impl->rollback();
100135
}
101136

137+
/**
138+
* Function checks if connection to the database is opened
139+
* @return true on success, false otherwise
140+
*/
102141
bool connected() const
103142
{
104143
return conn_impl->connected();
105144
}
106145

146+
/**
147+
* Function checks if connection to the database is alive
148+
* @return true on success, false otherwise
149+
*/
107150
bool alive() const
108151
{
109152
return conn_impl->alive();
110153
}
111154

155+
/**
156+
* Function returns statement object for the connection
157+
* @return
158+
*/
112159
statement get_statement()
113160
{
114161
return statement(conn_impl->get_statement(*(conn_impl.get())));
115162
}
116163

164+
/**
165+
* Conversion operator to the concrete database connection implementation
166+
* @return
167+
*/
117168
template <typename T>
118-
T& get_native_connection()
169+
explicit operator T&() const
119170
{
120171
return dynamic_cast<T&>(*conn_impl);
121172
}
173+
174+
122175

123176
private:
124177
template <typename T> friend class dbd::driver;
@@ -135,5 +188,5 @@ class connection {
135188

136189
} } } // namespace vgi::dbconn::dbi
137190

138-
#endif // CONNECTION_HPP
191+
#endif // CONNECTION_HPP
139192

driver.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
*/
1818

1919
#ifndef DRIVER_HPP
20-
#define DRIVER_HPP
20+
#define DRIVER_HPP
2121

2222
#include <memory>
2323
#include <functional>
@@ -68,5 +68,5 @@ class driver : public T
6868

6969
}}} // namespace vgi::dbconn::dbd
7070

71-
#endif // DRIVER_HPP
71+
#endif // DRIVER_HPP
7272

result_set.hpp

Lines changed: 103 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
*/
1818

1919
#ifndef RESULT_SET_HPP
20-
#define RESULT_SET_HPP
20+
#define RESULT_SET_HPP
2121

2222
namespace vgi { namespace dbconn { namespace dbi {
2323

@@ -34,29 +34,30 @@ struct iresult_set
3434
virtual bool has_data() = 0;
3535
virtual bool more_results() = 0;
3636
virtual size_t row_count() const = 0;
37+
virtual size_t rows_affected() const = 0;
3738
virtual size_t column_count() const = 0;
3839
virtual bool next() = 0;
39-
virtual std::string column_name(size_t index) = 0;
40+
virtual std::string column_name(size_t col_idx) = 0;
4041
virtual int column_index(const std::string& col_name) = 0;
41-
virtual bool is_null(unsigned int colidx) = 0;
42-
virtual int16_t get_short(unsigned int colidx) = 0;
43-
virtual uint16_t get_ushort(unsigned int colidx) = 0;
44-
virtual int32_t get_int(unsigned int colidx) = 0;
45-
virtual uint32_t get_uint(unsigned int colidx) = 0;
46-
virtual int64_t get_long(unsigned int colidx) = 0;
47-
virtual uint64_t get_ulong(unsigned int colidx) = 0;
48-
virtual float get_float(unsigned int colidx) = 0;
49-
virtual double get_double(unsigned int colidx) = 0;
50-
virtual long double get_ldouble(unsigned int colidx) = 0;
51-
virtual bool get_bool(unsigned int colidx) = 0;
52-
virtual char get_char(unsigned int colidx) = 0;
53-
virtual std::string get_string(unsigned int colidx) = 0;
54-
virtual int get_date(unsigned int colidx) = 0;
55-
virtual double get_time(unsigned int colidx) = 0;
56-
virtual time_t get_datetime(unsigned int colidx) = 0;
57-
virtual char16_t get_unichar(unsigned int colidx) = 0;
58-
virtual std::u16string get_unistring(unsigned int colidx) = 0;
59-
virtual std::vector<uint8_t> get_image(unsigned int colidx) = 0;
42+
virtual bool is_null(size_t col_idx) = 0;
43+
virtual int16_t get_short(size_t col_idx) = 0;
44+
virtual uint16_t get_ushort(size_t col_idx) = 0;
45+
virtual int32_t get_int(size_t col_idx) = 0;
46+
virtual uint32_t get_uint(size_t col_idx) = 0;
47+
virtual int64_t get_long(size_t col_idx) = 0;
48+
virtual uint64_t get_ulong(size_t col_idx) = 0;
49+
virtual float get_float(size_t col_idx) = 0;
50+
virtual double get_double(size_t col_idx) = 0;
51+
virtual long double get_ldouble(size_t col_idx) = 0;
52+
virtual bool get_bool(size_t col_idx) = 0;
53+
virtual char get_char(size_t col_idx) = 0;
54+
virtual std::string get_string(size_t col_idx) = 0;
55+
virtual int get_date(size_t col_idx) = 0;
56+
virtual double get_time(size_t col_idx) = 0;
57+
virtual time_t get_datetime(size_t col_idx) = 0;
58+
virtual char16_t get_unichar(size_t col_idx) = 0;
59+
virtual std::u16string get_unistring(size_t col_idx) = 0;
60+
virtual std::vector<uint8_t> get_image(size_t col_idx) = 0;
6061
// TODO: add xml, binary, money, lob
6162
};
6263

@@ -74,75 +75,149 @@ struct iresult_set
7475
class result_set
7576
{
7677
public:
78+
/**
79+
* Copy constructor
80+
* @param rs
81+
*/
7782
result_set(const result_set& rs) : rs_impl(rs.rs_impl)
7883
{
7984
}
8085

86+
/**
87+
* Move constructor
88+
* @param rs
89+
*/
8190
result_set(result_set&& rs) : rs_impl(std::move(rs.rs_impl))
8291
{
8392
}
8493

94+
/**
95+
* Assignment copy operator
96+
* @param rs
97+
* @return
98+
*/
8599
result_set& operator=(const result_set& rs)
86100
{
87101
if (this != &rs)
88102
rs_impl = rs.rs_impl;
89103
return *this;
90104
}
91105

106+
/**
107+
* Assignment move operator
108+
* @param rs
109+
* @return
110+
*/
92111
result_set& operator=(result_set&& rs)
93112
{
94113
if (this != &rs)
95114
rs_impl = std::move(rs.rs_impl);
96115
return *this;
97116
}
98117

118+
/**
119+
* Function checks if current result set contains data.
120+
* Even after whole data set has been iterated through using next() the
121+
* has_data() function would still return true - it is only reset after call
122+
* to more_results() function which will close current result set.
123+
* @return true if there is data in current result set, false otherwise
124+
*/
99125
bool has_data()
100126
{
101127
return rs_impl->has_data();
102128
}
103129

130+
/**
131+
* Function checks if there is another data set or status.
132+
* @return true if there is another result set, false otherwise
133+
*/
104134
bool more_results()
105135
{
106136
return rs_impl->more_results();
107137
}
108138

139+
/**
140+
* Function returns current row count while iterating through the result set
141+
* and total number of rows after data set was processed
142+
*/
109143
size_t row_count() const
110144
{
111145
return rs_impl->row_count();
112146
}
113147

148+
/**
149+
* Function returns number of affected rows after execution of command
150+
* @return number of rows
151+
*/
152+
size_t rows_affected() const
153+
{
154+
return rs_impl->rows_affected();
155+
}
156+
157+
/**
158+
* Function returns number of columns of current result set or zero
159+
* @return number of columns or zero otherwise
160+
*/
114161
size_t column_count() const
115162
{
116163
return rs_impl->column_count();
117164
}
118165

119-
const std::string column_name(size_t index)
166+
/**
167+
* Function returns column name by column index or throws an exception if
168+
* index is invalid
169+
* @param col_idx
170+
* @return column name string or exception is thrown if index is invalid
171+
*/
172+
const std::string column_name(size_t col_idx)
120173
{
121-
return rs_impl->column_name(index);
174+
return std::move(rs_impl->column_name(col_idx));
122175
}
123176

177+
/**
178+
* Function returns column index by column name, if column name is invalid
179+
* then -1 is returned
180+
* @param col_name
181+
* @return index or -1 on invalid column name
182+
*/
124183
int column_index(const std::string& col_name)
125184
{
126185
return rs_impl->column_index(col_name);
127186
}
128187

188+
/**
189+
* Function moves iterator to the next row of the current result data set
190+
* @return true on success, or false if there is no more rows
191+
*/
129192
bool next()
130193
{
131194
return rs_impl->next();
132195
}
133196

134-
bool is_null(unsigned int colidx)
197+
/**
198+
* Function checks if cell data is NULL by column index or throws an exception
199+
* if index is invalid.
200+
* @param col_idx
201+
* @return true if cell data is NULL, false otherwise
202+
*/
203+
bool is_null(size_t col_idx)
135204
{
136-
return rs_impl->is_null(colidx);
205+
return rs_impl->is_null(col_idx);
137206
}
138207

208+
/**
209+
* Function checks if cell data is NULL by column name or throws an exception
210+
* if index is invalid.
211+
* @param colname
212+
* @return
213+
*/
139214
bool is_null(const std::string& colname)
140215
{
141216
return rs_impl->is_null(rs_impl->column_index(colname));
142217
}
143218

144-
#define get_type_by_index(t) get_##t(unsigned int colidx) { return rs_impl->get_##t(colidx); }
145-
#define get_type_by_name(t) get_##t(const std::string& colname) { return rs_impl->get_##t(rs_impl->column_index(colname)); }
219+
#define get_type_by_index(t) get_##t(size_t col_idx) { return rs_impl->get_##t(col_idx); }
220+
#define get_type_by_name(t) get_##t(const std::string& colname) { return rs_impl->get_##t(rs_impl->column_index(colname)); }
146221

147222
int16_t get_type_by_index(short);
148223
int16_t get_type_by_name(short);
@@ -193,5 +268,5 @@ class result_set
193268

194269
} } } // namespace vgi::dbconn::dbi
195270

196-
#endif // RESULT_SET_HPP
271+
#endif // RESULT_SET_HPP
197272

0 commit comments

Comments
 (0)