Skip to content

Commit 17bf502

Browse files
committed
Minor fixes, refactoring, rm copypaste
1 parent fd06bf2 commit 17bf502

11 files changed

Lines changed: 159 additions & 236 deletions

OdbcConnection.cpp

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1028,12 +1028,12 @@ SQLRETURN OdbcConnection::sqlNativeSql( SQLCHAR * inStatementText, SQLINTEGER te
10281028
}
10291029
catch (const SQLException &ex)
10301030
{
1031-
postError( "HY000", ex );
1031+
postError("HY000", ex);
10321032
return SQL_ERROR;
10331033
}
10341034
catch (const std::exception &ex)
10351035
{
1036-
postError( "HY000", ex );
1036+
postError("HY000", ex);
10371037
return SQL_ERROR;
10381038
}
10391039

@@ -1129,6 +1129,14 @@ SQLRETURN OdbcConnection::sqlDisconnect()
11291129
if (connection->getTransactionPending())
11301130
return sqlReturn (SQL_ERROR, "25000", "Invalid transaction state");
11311131

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+
11321140
try
11331141
{
11341142
connection->commit();
@@ -1138,17 +1146,11 @@ SQLRETURN OdbcConnection::sqlDisconnect()
11381146
}
11391147
catch (const SQLException &ex)
11401148
{
1141-
postError ("01002", ex);
1142-
connection = NULL;
1143-
connected = false;
1144-
return SQL_SUCCESS_WITH_INFO;
1149+
return handle_error("01002", ex);
11451150
}
11461151
catch (const std::exception &ex)
11471152
{
1148-
postError ("01002", ex);
1149-
connection = NULL;
1150-
connected = false;
1151-
return SQL_SUCCESS_WITH_INFO;
1153+
return handle_error("01002", ex);
11521154
}
11531155

11541156
return sqlSuccess();

OdbcDesc.cpp

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1253,13 +1253,10 @@ SQLRETURN OdbcDesc::sqlSetDescRec( SQLSMALLINT recNumber,
12531253
if ( bDefined == false )
12541254
return sqlReturn (SQL_ERROR, "HY091", "Invalid descriptor field identifier");
12551255

1256-
if (recNumber)
1257-
{
1258-
if ( recNumber > headCount )
1259-
return sqlReturn (SQL_NO_DATA_FOUND, "HY021", "Inconsistent descriptor information");
1256+
if ( recNumber > headCount )
1257+
return sqlReturn (SQL_NO_DATA_FOUND, "HY021", "Inconsistent descriptor information");
12601258

1261-
record = getDescRecord (recNumber);
1262-
}
1259+
record = getDescRecord (recNumber);
12631260

12641261
try
12651262
{

OdbcJdbcSetup/DsnDialog.cpp

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -743,6 +743,14 @@ void CDsnDialog::OnTestConnection( HWND hDlg )
743743

744744
GetWindowText( hDlg, strHeadDlg, sizeof ( strHeadDlg ) );
745745

746+
auto handle_error = [&](const char* text)
747+
{
748+
char buffer[2048];
749+
sprintf(buffer, "%s\n%s", _TR(IDS_MESSAGE_02, "Connection failed!"), text);
750+
removeNameFileDBfromMessage(buffer);
751+
MessageBox(hDlg, TEXT(buffer), TEXT(strHeadDlg), MB_ICONERROR | MB_OK);
752+
};
753+
746754
try
747755
{
748756
CServiceClient services;
@@ -791,23 +799,11 @@ void CDsnDialog::OnTestConnection( HWND hDlg )
791799
}
792800
catch (const SQLException &ex)
793801
{
794-
char buffer[2048];
795-
JString text = ex.getText();
796-
797-
sprintf( buffer, "%s\n%s", _TR( IDS_MESSAGE_02, "Connection failed!" ), (const char*)text );
798-
removeNameFileDBfromMessage ( buffer );
799-
800-
MessageBox( hDlg, TEXT( buffer ), TEXT( strHeadDlg ), MB_ICONERROR | MB_OK );
802+
handle_error(ex.getText());
801803
}
802804
catch (const std::exception &ex)
803805
{
804-
char buffer[2048];
805-
JString text = ex.what();
806-
807-
sprintf( buffer, "%s\n%s", _TR( IDS_MESSAGE_02, "Connection failed!" ), (const char*)text );
808-
removeNameFileDBfromMessage ( buffer );
809-
810-
MessageBox( hDlg, TEXT( buffer ), TEXT( strHeadDlg ), MB_ICONERROR | MB_OK );
806+
handle_error(ex.what());
811807
}
812808
}
813809
#endif

OdbcJdbcSetup/ServiceClient.cpp

Lines changed: 3 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -84,21 +84,8 @@ bool CServiceClient::initServices( const char *sharedLibrary )
8484
if ( !properties )
8585
return false;
8686
}
87-
catch (const SQLException &ex)
87+
catch (const std::exception &ex) // SQLException is derived from the std::exception and should be also caught here
8888
{
89-
JString text = ex.getText();
90-
91-
if ( services )
92-
{
93-
services->release();
94-
services = NULL;
95-
return false;
96-
}
97-
}
98-
catch (const std::exception &ex)
99-
{
100-
JString text = ex.what();
101-
10289
if ( services )
10390
{
10491
services->release();
@@ -153,19 +140,8 @@ bool CServiceClient::createDatabase()
153140
properties );
154141
connection->close();
155142
}
156-
catch (const SQLException &ex)
157-
{
158-
JString text = ex.getText();
159-
160-
if ( connection )
161-
connection->close();
162-
163-
return false;
164-
}
165-
catch (const std::exception &ex)
143+
catch (const std::exception &ex) // SQLException is derived from the std::exception and should be also caught here
166144
{
167-
JString text = ex.what();
168-
169145
if ( connection )
170146
connection->close();
171147

@@ -205,19 +181,8 @@ bool CServiceClient::dropDatabase()
205181
properties );
206182
connection->close();
207183
}
208-
catch (const SQLException &ex)
184+
catch (const std::exception &ex) // SQLException is derived from the std::exception and should be also caught here
209185
{
210-
JString text = ex.getText();
211-
212-
if ( connection )
213-
connection->close();
214-
215-
return false;
216-
}
217-
catch (const std::exception &ex)
218-
{
219-
JString text = ex.what();
220-
221186
if ( connection )
222187
connection->close();
223188

OdbcJdbcSetup/ServiceTabBackup.cpp

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,17 @@ void CServiceTabBackup::onStartBackup()
179179
return;
180180
}
181181

182+
auto handle_error = [&](const char* text, int sqlcode = 0, int fbcode = 0)
183+
{
184+
writeFooterToLogFile();
185+
EnableWindow(GetDlgItem(hDlg, IDOK), TRUE);
186+
EnableWindow(GetDlgItem(hDlg, IDC_BUTTON_VIEW_LOG), !logPathFile.IsEmpty());
187+
188+
char buffer[1024];
189+
sprintf(buffer, "sqlcode %d, fbcode %d - %s", sqlcode, fbcode, text);
190+
MessageBox(NULL, buffer, TEXT("Error!"), MB_ICONERROR | MB_OK);
191+
};
192+
182193
try
183194
{
184195
DWORD dwWritten;
@@ -236,25 +247,11 @@ void CServiceTabBackup::onStartBackup()
236247
}
237248
catch (const SQLException &ex)
238249
{
239-
writeFooterToLogFile();
240-
EnableWindow( GetDlgItem( hDlg, IDOK ), TRUE );
241-
EnableWindow( GetDlgItem( hDlg, IDC_BUTTON_VIEW_LOG ), !logPathFile.IsEmpty() );
242-
243-
char buffer[1024];
244-
JString text = ex.getText();
245-
sprintf(buffer, "sqlcode %d, fbcode %d - %s", ex.getSqlcode(), ex.getFbcode(), (const char*)text );
246-
MessageBox( NULL, buffer, TEXT( "Error!" ), MB_ICONERROR | MB_OK );
250+
handle_error(ex.getText(), ex.getSqlcode(), ex.getFbcode());
247251
}
248252
catch (const std::exception &ex)
249253
{
250-
writeFooterToLogFile();
251-
EnableWindow( GetDlgItem( hDlg, IDOK ), TRUE );
252-
EnableWindow( GetDlgItem( hDlg, IDC_BUTTON_VIEW_LOG ), !logPathFile.IsEmpty() );
253-
254-
char buffer[1024];
255-
JString text = ex.what();
256-
sprintf(buffer, "sqlcode %d, fbcode %d - %s", 0, 0, (const char*)text );
257-
MessageBox( NULL, buffer, TEXT( "Error!" ), MB_ICONERROR | MB_OK );
254+
handle_error(ex.what());
258255
}
259256

260257
services.closeService();

OdbcJdbcSetup/ServiceTabRepair.cpp

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,17 @@ void CServiceTabRepair::startRepairDatabase()
200200
return;
201201
}
202202

203+
auto handle_error = [&](const char* text, int sqlcode = 0, int fbcode = 0)
204+
{
205+
writeFooterToLogFile();
206+
EnableWindow(GetDlgItem(hDlg, IDOK), TRUE);
207+
EnableWindow(GetDlgItem(hDlg, IDC_BUTTON_VIEW_LOG), !logPathFile.IsEmpty());
208+
209+
char buffer[1024];
210+
sprintf(buffer, "sqlcode %d, fbcode %d - %s", sqlcode, fbcode, text);
211+
MessageBox(NULL, buffer, TEXT("Error!"), MB_ICONERROR | MB_OK);
212+
};
213+
203214
try
204215
{
205216
DWORD dwWritten;
@@ -303,25 +314,11 @@ void CServiceTabRepair::startRepairDatabase()
303314
}
304315
catch (const SQLException &ex)
305316
{
306-
writeFooterToLogFile();
307-
EnableWindow( GetDlgItem( hDlg, IDOK ), TRUE );
308-
EnableWindow( GetDlgItem( hDlg, IDC_BUTTON_VIEW_LOG ), !logPathFile.IsEmpty() );
309-
310-
char buffer[1024];
311-
JString text = ex.getText();
312-
sprintf(buffer, "sqlcode %d, fbcode %d - %s", ex.getSqlcode(), ex.getFbcode(), (const char*)text );
313-
MessageBox( NULL, buffer, TEXT( "Error!" ), MB_ICONERROR | MB_OK );
317+
handle_error(ex.getText(), ex.getSqlcode(), ex.getFbcode());
314318
}
315319
catch (const std::exception &ex)
316320
{
317-
writeFooterToLogFile();
318-
EnableWindow( GetDlgItem( hDlg, IDOK ), TRUE );
319-
EnableWindow( GetDlgItem( hDlg, IDC_BUTTON_VIEW_LOG ), !logPathFile.IsEmpty() );
320-
321-
char buffer[1024];
322-
JString text = ex.what();
323-
sprintf(buffer, "sqlcode %d, fbcode %d - %s", 0, 0, (const char*)text );
324-
MessageBox( NULL, buffer, TEXT( "Error!" ), MB_ICONERROR | MB_OK );
321+
handle_error(ex.what());
325322
}
326323

327324
services.closeService();

OdbcJdbcSetup/ServiceTabRestore.cpp

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,17 @@ void CServiceTabRestore::onStartRestore()
205205
return;
206206
}
207207

208+
auto handle_error = [&](const char* text, int sqlcode = 0, int fbcode = 0)
209+
{
210+
writeFooterToLogFile();
211+
EnableWindow(GetDlgItem(hDlg, IDOK), TRUE);
212+
EnableWindow(GetDlgItem(hDlg, IDC_BUTTON_VIEW_LOG), !logPathFile.IsEmpty());
213+
214+
char buffer[1024];
215+
sprintf(buffer, "sqlcode %d, fbcode %d - %s", sqlcode, fbcode, text);
216+
MessageBox(NULL, buffer, TEXT("Error!"), MB_ICONERROR | MB_OK);
217+
};
218+
208219
try
209220
{
210221
DWORD dwWritten;
@@ -265,25 +276,11 @@ void CServiceTabRestore::onStartRestore()
265276
}
266277
catch (const SQLException &ex)
267278
{
268-
writeFooterToLogFile();
269-
EnableWindow( GetDlgItem( hDlg, IDOK ), TRUE );
270-
EnableWindow( GetDlgItem( hDlg, IDC_BUTTON_VIEW_LOG ), !logPathFile.IsEmpty() );
271-
272-
char buffer[1024];
273-
JString text = ex.getText();
274-
sprintf(buffer, "sqlcode %d, fbcode %d - %s", ex.getSqlcode(), ex.getFbcode(), (const char*)text );
275-
MessageBox( NULL, buffer, TEXT( "Error!" ), MB_ICONERROR | MB_OK );
279+
handle_error(ex.getText(), ex.getSqlcode(), ex.getFbcode());
276280
}
277281
catch (const std::exception &ex)
278282
{
279-
writeFooterToLogFile();
280-
EnableWindow( GetDlgItem( hDlg, IDOK ), TRUE );
281-
EnableWindow( GetDlgItem( hDlg, IDC_BUTTON_VIEW_LOG ), !logPathFile.IsEmpty() );
282-
283-
char buffer[1024];
284-
JString text = ex.what();
285-
sprintf(buffer, "sqlcode %d, fbcode %d - %s", 0, 0, (const char*)text );
286-
MessageBox( NULL, buffer, TEXT( "Error!" ), MB_ICONERROR | MB_OK );
283+
handle_error(ex.what());
287284
}
288285

289286
services.closeService();

OdbcJdbcSetup/ServiceTabStatistics.cpp

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,17 @@ void CServiceTabStatistics::onStartStatistics()
177177
return;
178178
}
179179

180+
auto handle_error = [&](const char* text, int sqlcode = 0, int fbcode = 0)
181+
{
182+
writeFooterToLogFile();
183+
EnableWindow(GetDlgItem(hDlg, IDOK), TRUE);
184+
EnableWindow(GetDlgItem(hDlg, IDC_BUTTON_VIEW_LOG), !logPathFile.IsEmpty());
185+
186+
char buffer[1024];
187+
sprintf(buffer, "sqlcode %d, fbcode %d - %s", sqlcode, fbcode, text);
188+
MessageBox(NULL, buffer, TEXT("Error!"), MB_ICONERROR | MB_OK);
189+
};
190+
180191
try
181192
{
182193
DWORD dwWritten;
@@ -221,25 +232,11 @@ void CServiceTabStatistics::onStartStatistics()
221232
}
222233
catch (const SQLException &ex)
223234
{
224-
writeFooterToLogFile();
225-
EnableWindow( GetDlgItem( hDlg, IDOK ), TRUE );
226-
EnableWindow( GetDlgItem( hDlg, IDC_BUTTON_VIEW_LOG ), !logPathFile.IsEmpty() );
227-
228-
char buffer[1024];
229-
JString text = ex.getText();
230-
sprintf(buffer, "sqlcode %d, fbcode %d - %s", ex.getSqlcode(), ex.getFbcode(), (const char*)text );
231-
MessageBox( NULL, buffer, TEXT( "Error!" ), MB_ICONERROR | MB_OK );
235+
handle_error(ex.getText(), ex.getSqlcode(), ex.getFbcode());
232236
}
233237
catch (const std::exception &ex)
234238
{
235-
writeFooterToLogFile();
236-
EnableWindow( GetDlgItem( hDlg, IDOK ), TRUE );
237-
EnableWindow( GetDlgItem( hDlg, IDC_BUTTON_VIEW_LOG ), !logPathFile.IsEmpty() );
238-
239-
char buffer[1024];
240-
JString text = ex.what();
241-
sprintf(buffer, "sqlcode %d, fbcode %d - %s", 0, 0, (const char*)text );
242-
MessageBox( NULL, buffer, TEXT( "Error!" ), MB_ICONERROR | MB_OK );
239+
handle_error(ex.what());
243240
}
244241

245242
services.closeService();

0 commit comments

Comments
 (0)