Skip to content

Commit 35fae3e

Browse files
authored
Merge pull request #298 from fdcastel/fix/sqldriverconnect-diag-record
Fix garbled diagnostic record on failed SQLDriverConnect
2 parents b64c011 + 17bf502 commit 35fae3e

20 files changed

Lines changed: 704 additions & 309 deletions

IscDbc/JString.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ const char* JString::getString()
199199
return (string) ? string : "";
200200
}
201201

202-
JString::operator const char* ()
202+
JString::operator const char* () const
203203
{
204204
/**************************************
205205
*

IscDbc/JString.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ class JString
6161

6262
void Format (const char*, ...);
6363
const char *getString();
64-
operator const char*();
64+
operator const char*() const;
6565
JString& operator = (const char *string);
6666
JString& operator = (const JString& string);
6767
JString& operator+=(const char *string);

IscDbc/SQLError.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ SQLError::~SQLError () throw()
112112

113113
}
114114

115-
int SQLError::getSqlcode ()
115+
int SQLError::getSqlcode () const
116116
{
117117
/**************************************
118118
*
@@ -128,7 +128,7 @@ int SQLError::getSqlcode ()
128128
return sqlcode;
129129
}
130130

131-
int SQLError::getFbcode ()
131+
int SQLError::getFbcode () const
132132
{
133133
/**************************************
134134
*
@@ -144,7 +144,7 @@ int SQLError::getFbcode ()
144144
return fbcode;
145145
}
146146

147-
const char *SQLError::getText ()
147+
const char *SQLError::getText () const
148148
{
149149
/**************************************
150150
*
@@ -160,7 +160,7 @@ const char *SQLError::getText ()
160160
return text;
161161
}
162162

163-
SQLError::operator const char* ()
163+
SQLError::operator const char* () const
164164
{
165165
/**************************************
166166
*
@@ -193,7 +193,7 @@ SQLError::SQLError( int code, __int64 codefb, const char * txt, ...)
193193
fbcode = (int) codefb;
194194
}
195195

196-
const char* SQLError::getTrace()
196+
const char* SQLError::getTrace() const
197197
{
198198
return stackTrace;
199199
}

IscDbc/SQLError.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,18 +33,18 @@ class SQLError : public SQLException
3333
public:
3434
virtual int release();
3535
virtual void addRef();
36-
virtual const char* getTrace();
36+
virtual const char* getTrace() const;
3737
SQLError (int sqlcode, __int64 fbcode, const char *text, ...);
3838
SQLError (SqlCode sqlcode, const char *text, ...);
3939
SQLError (Stream *trace, SqlCode code, const char *txt,...);
4040
~SQLError() throw();
4141

42-
virtual int getFbcode ();
43-
virtual int getSqlcode ();
44-
virtual const char *getText();
42+
virtual int getFbcode () const;
43+
virtual int getSqlcode () const;
44+
virtual const char *getText() const;
4545

4646
//void Delete();
47-
operator const char*();
47+
operator const char*() const;
4848

4949
int fbcode;
5050
int sqlcode;

IscDbc/SQLException.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,10 @@ class DllExport SQLException : public std::exception
4242
public:
4343
//virtual void addRef() = 0;
4444
//virtual int release() = 0;
45-
virtual int getFbcode () = 0;
46-
virtual int getSqlcode () = 0;
47-
virtual const char *getText() = 0;
48-
virtual const char *getTrace() = 0;
45+
virtual int getFbcode () const = 0;
46+
virtual int getSqlcode () const = 0;
47+
virtual const char *getText() const = 0;
48+
virtual const char *getTrace() const = 0;
4949
};
5050

5151
}; // end namespace IscDbcLibrary

OdbcConnection.cpp

Lines changed: 76 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1026,10 +1026,14 @@ SQLRETURN OdbcConnection::sqlNativeSql( SQLCHAR * inStatementText, SQLINTEGER te
10261026
outText = (const char *)tempNative;
10271027

10281028
}
1029-
catch ( std::exception &ex )
1029+
catch (const SQLException &ex)
10301030
{
1031-
SQLException &exception = (SQLException&)ex;
1032-
postError( "HY000", exception );
1031+
postError("HY000", ex);
1032+
return SQL_ERROR;
1033+
}
1034+
catch (const std::exception &ex)
1035+
{
1036+
postError("HY000", ex);
10331037
return SQL_ERROR;
10341038
}
10351039

@@ -1125,20 +1129,28 @@ SQLRETURN OdbcConnection::sqlDisconnect()
11251129
if (connection->getTransactionPending())
11261130
return sqlReturn (SQL_ERROR, "25000", "Invalid transaction state");
11271131

1132+
auto handle_error = [&](const char* state, auto& ex) -> SQLRETURN
1133+
{
1134+
postError("01002", ex);
1135+
connection = NULL;
1136+
connected = false;
1137+
return SQL_SUCCESS_WITH_INFO;
1138+
};
1139+
11281140
try
11291141
{
11301142
connection->commit();
11311143
releaseObjects();
11321144
connection = NULL;
11331145
connected = false;
11341146
}
1135-
catch ( std::exception &ex )
1147+
catch (const SQLException &ex)
11361148
{
1137-
SQLException &exception = (SQLException&)ex;
1138-
postError ("01002", exception);
1139-
connection = NULL;
1140-
connected = false;
1141-
return SQL_SUCCESS_WITH_INFO;
1149+
return handle_error("01002", ex);
1150+
}
1151+
catch (const std::exception &ex)
1152+
{
1153+
return handle_error("01002", ex);
11421154
}
11431155

11441156
return sqlSuccess();
@@ -1632,6 +1644,21 @@ SQLRETURN OdbcConnection::connect(const char *sharedLibrary, const char * databa
16321644
{
16331645
Properties *properties = NULL;
16341646

1647+
// Shared rollback for both catch handlers below. `connection` may be
1648+
// NULL if createConnection() itself threw before assignment, so it has
1649+
// to be checked before close().
1650+
auto rollbackPartialConnect = [&]() {
1651+
if ( env->envShare )
1652+
env->envShare = NULL;
1653+
if ( properties )
1654+
properties->release();
1655+
if ( connection )
1656+
{
1657+
connection->close();
1658+
connection = NULL;
1659+
}
1660+
};
1661+
16351662
try
16361663
{
16371664
connection = createConnection();
@@ -1709,19 +1736,15 @@ SQLRETURN OdbcConnection::connect(const char *sharedLibrary, const char * databa
17091736
WcsToMbs = connection->getConnectionWcsToMbs();
17101737
MbsToWcs = connection->getConnectionMbsToWcs();
17111738
}
1712-
catch ( std::exception &ex )
1739+
catch (const SQLException &ex)
17131740
{
1714-
SQLException &exception = (SQLException&)ex;
1715-
if ( env->envShare )
1716-
env->envShare = NULL;
1717-
1718-
if ( properties )
1719-
properties->release();
1720-
1721-
connection->close();
1722-
connection = NULL;
1723-
1724-
return sqlReturn( SQL_ERROR, "08004", exception.getText(), exception.getSqlcode() );
1741+
rollbackPartialConnect();
1742+
return sqlReturn( SQL_ERROR, "08004", ex.getText(), ex.getSqlcode() );
1743+
}
1744+
catch (const std::exception &ex)
1745+
{
1746+
rollbackPartialConnect();
1747+
return sqlReturn( SQL_ERROR, "08004", ex.what(), 0 );
17251748
}
17261749

17271750
connected = true;
@@ -1746,10 +1769,14 @@ SQLRETURN OdbcConnection::sqlEndTran(int operation)
17461769
connection->rollbackAuto();
17471770
}
17481771
}
1749-
catch ( std::exception &ex )
1772+
catch (const SQLException &ex)
17501773
{
1751-
SQLException &exception = (SQLException&)ex;
1752-
postError ("S1000", exception);
1774+
postError ("S1000", ex);
1775+
return SQL_ERROR;
1776+
}
1777+
catch (const std::exception &ex)
1778+
{
1779+
postError ("S1000", ex);
17531780
return SQL_ERROR;
17541781
}
17551782

@@ -1764,10 +1791,14 @@ SQLRETURN OdbcConnection::sqlExecuteCreateDatabase(const char * sqlString)
17641791
{
17651792
connection->sqlExecuteCreateDatabase( sqlString );
17661793
}
1767-
catch ( std::exception &ex )
1794+
catch (const SQLException &ex)
1795+
{
1796+
postError( "HY000", ex );
1797+
return SQL_ERROR;
1798+
}
1799+
catch (const std::exception &ex)
17681800
{
1769-
SQLException &exception = (SQLException&)ex;
1770-
postError( "HY000", exception );
1801+
postError( "HY000", ex );
17711802
return SQL_ERROR;
17721803
}
17731804

@@ -2178,10 +2209,13 @@ void OdbcConnection::initUserEvents( PODBC_EVENTS_BLOCK_INFO infoEvents )
21782209
userEventsInterfase->events = infoEvents->events;
21792210
userEventsInterfase->count = infoEvents->count;
21802211
}
2181-
catch ( std::exception &ex )
2212+
catch (const SQLException &ex)
21822213
{
2183-
SQLException &exception = (SQLException&)ex;
2184-
postError( "HY000", exception );
2214+
postError( "HY000", ex );
2215+
}
2216+
catch (const std::exception &ex)
2217+
{
2218+
postError( "HY000", ex );
21852219
}
21862220
}
21872221

@@ -2199,10 +2233,13 @@ void OdbcConnection::updateResultEvents( char *updated )
21992233
nextNameEvent->changed = userEvents->isChanged( i );
22002234
}
22012235
}
2202-
catch ( std::exception &ex )
2236+
catch (const SQLException &ex)
2237+
{
2238+
postError( "HY000", ex );
2239+
}
2240+
catch (const std::exception &ex)
22032241
{
2204-
SQLException &exception = (SQLException&)ex;
2205-
postError( "HY000", exception );
2242+
postError( "HY000", ex );
22062243
}
22072244
}
22082245

@@ -2212,10 +2249,13 @@ void OdbcConnection::requeueEvents()
22122249
{
22132250
userEvents->queEvents( userEventsInterfase );
22142251
}
2215-
catch ( std::exception &ex )
2252+
catch (const SQLException &ex)
2253+
{
2254+
postError( "HY000", ex );
2255+
}
2256+
catch (const std::exception &ex)
22162257
{
2217-
SQLException &exception = (SQLException&)ex;
2218-
postError( "HY000", exception );
2258+
postError( "HY000", ex );
22192259
}
22202260
}
22212261

OdbcDesc.cpp

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -806,10 +806,14 @@ SQLRETURN OdbcDesc::sqlGetDescField(int recNumber, int fieldId, SQLPOINTER ptr,
806806
return returnStringInfo (ptr, bufferLength, lengthPtr, (char*)string);
807807

808808
}
809-
catch ( std::exception &ex )
809+
catch (const SQLException &ex)
810810
{
811-
SQLException &exception = (SQLException&)ex;
812-
postError ("HY000", exception);
811+
postError ("HY000", ex);
812+
return SQL_ERROR;
813+
}
814+
catch (const std::exception &ex)
815+
{
816+
postError ("HY000", ex);
813817
return SQL_ERROR;
814818
}
815819

@@ -1217,10 +1221,14 @@ SQLRETURN OdbcDesc::sqlGetDescRec( SQLSMALLINT recNumber,
12171221
*scalePtr = record->scale;
12181222
*nullablePtr = record->nullable;
12191223
}
1220-
catch ( std::exception &ex )
1224+
catch (const SQLException &ex)
1225+
{
1226+
postError ("HY000", ex);
1227+
return SQL_ERROR;
1228+
}
1229+
catch (const std::exception &ex)
12211230
{
1222-
SQLException &exception = (SQLException&)ex;
1223-
postError ("HY000", exception);
1231+
postError ("HY000", ex);
12241232
return SQL_ERROR;
12251233
}
12261234

@@ -1246,13 +1254,10 @@ SQLRETURN OdbcDesc::sqlSetDescRec( SQLSMALLINT recNumber,
12461254
if ( bDefined == false )
12471255
return sqlReturn (SQL_ERROR, "HY091", "Invalid descriptor field identifier");
12481256

1249-
if (recNumber)
1250-
{
1251-
if ( recNumber > headCount )
1252-
return sqlReturn (SQL_NO_DATA_FOUND, "HY021", "Inconsistent descriptor information");
1257+
if ( recNumber > headCount )
1258+
return sqlReturn (SQL_NO_DATA_FOUND, "HY021", "Inconsistent descriptor information");
12531259

1254-
record = getDescRecord (recNumber);
1255-
}
1260+
record = getDescRecord (recNumber);
12561261

12571262
try
12581263
{
@@ -1265,10 +1270,14 @@ SQLRETURN OdbcDesc::sqlSetDescRec( SQLSMALLINT recNumber,
12651270
record->octetLengthPtr = stringLengthPtr;
12661271
record->indicatorPtr = indicatorPtr;
12671272
}
1268-
catch ( std::exception &ex )
1273+
catch (const SQLException &ex)
1274+
{
1275+
postError ("HY000", ex);
1276+
return SQL_ERROR;
1277+
}
1278+
catch (const std::exception &ex)
12691279
{
1270-
SQLException &exception = (SQLException&)ex;
1271-
postError ("HY000", exception);
1280+
postError ("HY000", ex);
12721281
return SQL_ERROR;
12731282
}
12741283

0 commit comments

Comments
 (0)