@@ -1501,6 +1501,11 @@ SQLRETURN config_dbc(esodbc_dbc_st *dbc, esodbc_dsn_attrs_st *attrs)
15011501 /* early execution */
15021502 dbc -> early_exec = wstr2bool (& attrs -> early_exec );
15031503 INFOH (dbc , "early execution: %s." , dbc -> early_exec ? "true" : "false" );
1504+ /* default current catalog */
1505+ if (attrs -> catalog .cnt &&
1506+ (! SQL_SUCCEEDED (set_current_catalog (dbc , & attrs -> catalog )))) {
1507+ goto err ;
1508+ }
15041509
15051510 /* how to print the floats? */
15061511 assert (1 <= attrs -> sci_floats .cnt ); /* default should apply */
@@ -1657,13 +1662,9 @@ void cleanup_dbc(esodbc_dbc_st *dbc)
16571662 dbc -> srv_ver .str = NULL ;
16581663 dbc -> srv_ver .cnt = 0 ;
16591664 }
1660- if (dbc -> catalog .str ) {
1661- free (dbc -> catalog .str );
1662- dbc -> catalog .str = NULL ;
1663- dbc -> catalog .cnt = 0 ;
1664- } else {
1665- assert (dbc -> catalog .cnt == 0 );
1666- }
1665+ free_current_catalog (dbc );
1666+ assert (dbc -> catalog .w .cnt == 0 );
1667+ assert (dbc -> catalog .c .cnt == 0 );
16671668 if (dbc -> varchar_limit_str .str ) {
16681669 free (dbc -> varchar_limit_str .str );
16691670 dbc -> varchar_limit_str .str = NULL ;
@@ -3266,38 +3267,6 @@ SQLRETURN EsSQLDisconnect(SQLHDBC ConnectionHandle)
32663267 return SQL_SUCCESS ;
32673268}
32683269
3269- /* ES/SQL doesn't support catalogs (yet). This function checks that a
3270- * previously retrieved (and cached) catalog value is the same with what the
3271- * app currently tries to set it to.
3272- * Ideally, the app provided value would be cached here too (as per the spec:
3273- * "SQL_ATTR_CURRENT_CATALOG can be set before or after connecting"), in case
3274- * there's no connection "established" yet and checked at "establishment"
3275- * time. But there's no client reported yet setting a catalog value before
3276- * connecting. */
3277- static SQLRETURN check_catalog_name (esodbc_dbc_st * dbc , SQLWCHAR * name ,
3278- SQLINTEGER len )
3279- {
3280- wstr_st catalog ;
3281- catalog .str = name ;
3282- if (len < 0 ) {
3283- catalog .cnt = wcslen (name );
3284- } else {
3285- catalog .cnt = ((size_t )len )/sizeof (SQLWCHAR );
3286- }
3287- if (! EQ_WSTR (& dbc -> catalog , & catalog )) {
3288- if (! dbc -> catalog .cnt ) {
3289- /* this will happen if the app tries to set a value that it
3290- * discovered over a different connection.
3291- * TODO on a first reported issue. */
3292- WARNH (dbc , "connection's current catalog not yet set!" );
3293- }
3294- ERRH (dbc , "setting catalog name not supported." );
3295- RET_HDIAGS (dbc , SQL_STATE_HYC00 );
3296- }
3297- WARNH (dbc , "ignoring attempt to set the current catalog." );
3298- return SQL_SUCCESS ;
3299- }
3300-
33013270/*
33023271 * https://docs.microsoft.com/en-us/sql/odbc/reference/develop-app/unicode-drivers :
33033272 * """
@@ -3325,6 +3294,7 @@ SQLRETURN EsSQLSetConnectAttrW(
33253294 _In_reads_bytes_opt_ (StringLength ) SQLPOINTER Value ,
33263295 SQLINTEGER StringLength )
33273296{
3297+ wstr_st catalog ;
33283298 esodbc_dbc_st * dbc = DBCH (ConnectionHandle );
33293299
33303300 switch (Attribute ) {
@@ -3434,11 +3404,13 @@ SQLRETURN EsSQLSetConnectAttrW(
34343404 RET_HDIAGS (dbc , SQL_STATE_IM009 );
34353405
34363406 case SQL_ATTR_CURRENT_CATALOG :
3437- INFOH (dbc , "setting current catalog to: `" LWPDL "`." ,
3438- /* string should be 0-term'd */
3439- 0 <= StringLength ? StringLength /sizeof (SQLWCHAR ) : SHRT_MAX ,
3440- (SQLWCHAR * )Value );
3441- return check_catalog_name (dbc , (SQLWCHAR * )Value , StringLength );
3407+ if (StringLength < 0 ) {
3408+ ERRH (dbc , "invalid catalog name lenght: %ld" , StringLength );
3409+ RET_HDIAGS (dbc , SQL_STATE_HY090 );
3410+ }
3411+ catalog .str = (SQLWCHAR * )Value ;
3412+ catalog .cnt = StringLength /sizeof (SQLWCHAR );
3413+ return set_current_catalog (dbc , & catalog );
34423414
34433415 case SQL_ATTR_TRACE :
34443416 case SQL_ATTR_TRACEFILE : /* DM-only */
@@ -3504,7 +3476,16 @@ SQLRETURN EsSQLGetConnectAttrW(
35043476 ERRH (dbc , "no connection active." );
35053477 RET_HDIAGS (dbc , SQL_STATE_08003 );
35063478 }
3507- if ((used = fetch_server_attr (dbc , SQL_ATTR_CURRENT_CATALOG ,
3479+ if (dbc -> catalog .w .cnt ) {
3480+ if (! SQL_SUCCEEDED (write_wstr (dbc , (SQLWCHAR * )ValuePtr ,
3481+ & dbc -> catalog .w , (SQLSMALLINT )BufferLength ,
3482+ & used ))) {
3483+ ERRH (dbc , "failed to copy current catalog out." );
3484+ RET_STATE (dbc -> hdr .diag .state );
3485+ }
3486+ used = SHRT_MAX <= dbc -> catalog .w .cnt ? SHRT_MAX :
3487+ (SQLSMALLINT )dbc -> catalog .w .cnt ;
3488+ } else if ((used = fetch_server_attr (dbc , SQL_ATTR_CURRENT_CATALOG ,
35083489 (SQLWCHAR * )ValuePtr ,
35093490 (SQLSMALLINT )BufferLength )) < 0 ) {
35103491 ERRH (dbc , "failed to get current catalog." );
@@ -3583,14 +3564,14 @@ SQLRETURN EsSQLGetConnectAttrW(
35833564 /* MS Access/Jet proprietary info type */
35843565 case 30002 :
35853566 ERRH (dbc , "unsupported info type." );
3586- RET_HDIAGS (DBCH ( ConnectionHandle ) , SQL_STATE_HY092 );
3567+ RET_HDIAGS (dbc , SQL_STATE_HY092 );
35873568#endif
35883569
35893570 default :
35903571 ERRH (dbc , "unknown Attribute type %ld." , Attribute );
35913572 // FIXME: add the other attributes
35923573 FIXME ;
3593- RET_HDIAGS (DBCH ( ConnectionHandle ) , SQL_STATE_HY092 );
3574+ RET_HDIAGS (dbc , SQL_STATE_HY092 );
35943575 }
35953576
35963577 return SQL_SUCCESS ;
0 commit comments