-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVarchar.cc
261 lines (212 loc) · 4.85 KB
/
Varchar.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
//////////////////////////////////////////////////////////////////////////////
//
// Ora++ -- a C++ interface to Oracle based on the Oracle Call Interface
// Copyright (C) 2000-1 James Edwin Cain <[email protected]>
//
// This library is free software; you can redistribute it and/or modify it
// under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation; either version 2.1 of the License, or any
// later version.
//
// This library 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 Lesser
// General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with this library; if not, write to the Free Software Foundation,
// Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
//
//////////////////////////////////////////////////////////////////////////////
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <sstream>
#include "Oracle.h"
#include "Varchar.h"
#include <oci.h>
Oracle::Varchar::Varchar() throw()
: Nullable(), vc(0), max_sz(0)
{
}
Oracle::Varchar::Varchar(const int n) throw()
: Nullable(), vc(new char[n + 1]), max_sz(n + 1)
{
*vc = 0;
}
Oracle::Varchar::Varchar(const char* s) throw()
: Nullable(), vc(new char[std::strlen(s) + 1]), max_sz(std::strlen(s) + 1)
{
// copy s into new array of same size
std::strcpy(vc, s);
ind = 0;
}
Oracle::Varchar::Varchar(const std::string& s) throw()
: Nullable(), vc(new char[s.length() + 1]), max_sz(s.length() + 1)
{
// copy into new array of same size
s.copy(vc, s.length());
vc[s.length()] = 0;
ind = 0;
}
Oracle::Varchar::Varchar(const Oracle::Varchar& v) throw()
: Nullable()
{
// if v is not null, initialize this Varchar with v's string
if ((ind = v.ind) == 0)
{
vc = new char[strlen(v.vc) + 1];
strcpy(vc, v.vc);
max_sz = strlen(v.vc) + 1;
}
}
Oracle::Varchar::~Varchar() throw()
{
// free any space that was allocated
if (vc)
delete [] vc;
max_sz = 0;
ind = -1;
}
inline
int
Oracle::Varchar::sqlt() const throw()
{
return SQLT_STR;
}
std::string
Oracle::Varchar::str() const throw(Oracle::Error)
{
if (ind == -1)
return Nullable::str();
return std::string(vc);
}
std::string
Oracle::Varchar::str(const std::string& s) const throw()
{
if (ind == -1)
return s;
return std::string(vc);
}
std::string
Oracle::Varchar::str(const std::string& s, const std::string& f) const throw()
{
// ignore format
if (ind == -1)
return s;
return std::string(vc);
}
std::string
Oracle::Varchar::sql_str() const throw()
{
if (ind == -1)
return Nullable::sql_str();
return "'" + sqlquote(vc) + "'";
}
long
Oracle::Varchar::lng() const throw(Oracle::Error)
{
if (ind == -1)
return Nullable::lng();
char *p;
long l = std::strtol(vc, &p, 10);
if(p == vc)
throw Value_Error("Varchar::lng()", "The object does not contain a valid long");
else
return l;
}
long
Oracle::Varchar::lng(const long n) const throw(Oracle::Error)
{
if (ind == -1)
return n;
char *p;
long l = std::strtol(vc, &p, 10);
if(p == vc)
throw Value_Error("Varchar::lng(const long)", "The object does not contain a valid long");
else
return l;
}
double
Oracle::Varchar::dbl() const throw(Oracle::Error)
{
if (ind == -1)
return Nullable::dbl();
char *p;
double d = std::strtod(vc, &p);
if(p == vc)
throw Value_Error("Varchar::dbl()", "The object does not contain a valid double");
else
return d;
}
double
Oracle::Varchar::dbl(const double n) const throw(Oracle::Error)
{
if (ind == -1)
return n;
char *p;
double d = std::strtod(vc, &p);
if(p == vc)
throw Value_Error("Varchar::dbl(const double)", "The object does not contain a valid double");
else
return d;
}
Oracle::Varchar&
Oracle::Varchar::operator=(const char* s) throw()
{
if (strlen(s) >= max_sz)
{
if (vc)
delete [] vc;
vc = new char[strlen(s) + 1];
max_sz = strlen(s) + 1;
}
strcpy(vc, s);
ind = 0;
return *this;
}
Oracle::Varchar&
Oracle::Varchar::operator=(const std::string& s) throw()
{
this->operator=(s.c_str());
return *this;
}
Oracle::Varchar&
Oracle::Varchar::operator=(const Oracle::Varchar& rhs) throw()
{
if (&rhs != this)
{
ind = rhs.ind;
if (ind == 0)
this->operator=(rhs.vc);
}
return *this;
}
Oracle::Varchar&
Oracle::Varchar::operator=(const long rhs) throw()
{
std::ostringstream ost;
ost << rhs;
this->operator=(ost.str());
return *this;
}
Oracle::Varchar&
Oracle::Varchar::operator=(const double rhs) throw()
{
std::ostringstream ost;
ost << rhs;
this->operator=(ost.str());
return *this;
}
const bool
Oracle::operator==(const Oracle::Varchar& v1, const Oracle::Varchar& v2)
{
if (v1.is_null() || v2.is_null())
return false;
else if (&v1 == &v2)
return true;
else if (v1.str() == v2.str())
return true;
else
return false;
}