Skip to content

Commit

Permalink
minor, refine rest call and print more logs in odbc
Browse files Browse the repository at this point in the history
  • Loading branch information
lidongsjtu authored and Hongbin Ma committed Jun 13, 2017
1 parent 450246a commit 0ed5910
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 43 deletions.
7 changes: 2 additions & 5 deletions odbc/Common/QueryCache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ int findInAlwaysFailQuery ( const wchar_t* sql )
return findQuery ( sql, alwaysFailQueries, sizeof ( alwaysFailQueries ) / sizeof ( wchar_t*) );
}

unique_ptr <SQLResponse> loadCache ( const wchar_t* query )
const wchar_t* loadCache ( const wchar_t* query )
{
int index = 0;

Expand All @@ -86,10 +86,7 @@ unique_ptr <SQLResponse> loadCache ( const wchar_t* query )

else if ( ( index = findInAlwaysSuccessQuery ( query ) ) >= 0 )
{
web::json::value v = web::json::value::parse ( alwaysSuccessResults[index] );
std::unique_ptr <SQLResponse> r = SQLResponseFromJSON ( v );
//overwrite(r.get());
return r;
return alwaysSuccessResults[index];
}

return NULL;
Expand Down
2 changes: 1 addition & 1 deletion odbc/Common/QueryCache.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@

#include "MsgTypes.h"

unique_ptr <SQLResponse> loadCache ( const wchar_t* query );
const wchar_t* loadCache ( const wchar_t* query );

67 changes: 43 additions & 24 deletions odbc/Common/REST.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ http_request makeRequest ( const char* username, const char* passwd, const wchar
request . set_method ( method );
request . set_request_uri ( uri ( uri::encode_uri ( uriStr ) ) );
request . headers () . add ( header_names::authorization, string2wstring ( "Basic " + b64 ) );
request . headers () . add ( header_names::accept, "application/json" );
request . headers () . add ( header_names::content_type, "application/json" );
return request;
}
Expand Down Expand Up @@ -369,15 +370,46 @@ wstring getBodyString ( http_response& response )
return ret;
}

std::unique_ptr <SQLResponse> restQuery ( wchar_t* rawSql, char* serverAddr, long port, char* username,
std::unique_ptr <SQLResponse> convertToSQLResponse ( int statusFlag,
wstring responseStr )
{
if ( statusFlag == 1 )
{
//convert to json
web::json::value actualRes = web::json::value::parse ( responseStr );
std::unique_ptr <SQLResponse> r = SQLResponseFromJSON ( actualRes );

if ( r -> isException == true )
{
string expMsg = wstring2string ( r -> exceptionMessage );
throw exception ( expMsg . c_str () );
}

overwrite ( r . get () );
return r;
}

else if ( statusFlag == 0 )
{
std::unique_ptr <ErrorMessage> em = ErrorMessageFromJSON ( web::json::value::parse ( responseStr ) );
string expMsg = wstring2string ( em -> msg );
throw exception ( expMsg . c_str () );
}

return NULL;
}

wstring requestQuery ( wchar_t* rawSql, char* serverAddr, long port, char* username,
char* passwd,
char* project )
char* project,
int* statusFlag)
{
//using local cache to intercept probing queries
std::unique_ptr <SQLResponse> cachedQueryRes = loadCache ( rawSql );
const wchar_t* cachedQueryRes = loadCache ( rawSql );

if ( cachedQueryRes != NULL )
{
*statusFlag = 1;
return cachedQueryRes;
}

Expand All @@ -392,10 +424,10 @@ std::unique_ptr <SQLResponse> restQuery ( wchar_t* rawSql, char* serverAddr, lon
wss << L"{ \"acceptPartial\": false, \"project\" : \"" << project << L"\", " << " \"sql\" : \"" << sql << L"\" }" ;
request . set_body ( wss . str (), L"application/json" );
request . headers () . add ( header_names::accept_encoding, "gzip,deflate" );
http::status_code status;
http_response response;
http::status_code status;

try
try
{
response = session . request ( request ) . get ();
status = response . status_code ();
Expand All @@ -408,36 +440,23 @@ std::unique_ptr <SQLResponse> restQuery ( wchar_t* rawSql, char* serverAddr, lon
throw exception ( ss . str () . c_str () );
}

wstring ret = getBodyString ( response );

if ( status == status_codes::OK )
if ( status == status_codes::OK )
{
//convert to json
web::json::value actualRes = web::json::value::parse ( ret );
std::unique_ptr <SQLResponse> r = SQLResponseFromJSON ( actualRes );

if ( r -> isException == true )
{
string expMsg = wstring2string ( r -> exceptionMessage );
throw exception ( expMsg . c_str () );
}

overwrite ( r . get () );
return r;
*statusFlag = 1;
}

else if ( status == status_codes::InternalError )
{
std::unique_ptr <ErrorMessage> em = ErrorMessageFromJSON ( web::json::value::parse ( ret ) );
string expMsg = wstring2string ( em -> msg );
throw exception ( expMsg . c_str () );
*statusFlag = 0;
}

else
{
throw exception ( "Unknown exception in rest query with return code " + status );
}

return NULL;
wstring ret = getBodyString ( response );

return ret;
}

13 changes: 9 additions & 4 deletions odbc/Common/REST.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,21 @@

#include "vld.h"
#include "MsgTypes.h"
#include <xstring>


//REST
bool restAuthenticate ( char* serverAddr, long port, char* username, char* passwd );

void restListProjects ( char* serverAddr, long port, char* username, char* passwd, std::vector <string>& holder );

std::unique_ptr <SQLResponse> restQuery ( wchar_t* rawSql, char* serverAddr, long port, char* username,
char* passwd,
char* project );

std::unique_ptr <MetadataResponse> restGetMeta ( char* serverAddr, long port, char* username, char* passwd,
char* project );

std::unique_ptr <SQLResponse> convertToSQLResponse ( int status,
wstring responseStr );

wstring requestQuery ( wchar_t* rawSql, char* serverAddr, long port, char* username,
char* passwd,
char* project,
int* statusFlag);
14 changes: 10 additions & 4 deletions odbc/Driver/KO_EXEC.CPP
Original file line number Diff line number Diff line change
Expand Up @@ -121,12 +121,18 @@ RETCODE SQL_API _SQLExecStmtFromReq ( pODBCStmt pStmt, bool pPrepared ) {
__ODBCLOG ( _ODBCLogMsg ( LogLevel_INFO, "================================================================" ) );
__ODBCLOG ( _ODBCLogMsg ( LogLevel_INFO, "start exec the query: " ) );
__ODBCLOG ( _ODBCLogMsg ( LogLevel_INFO, pStmt->Stmt ) );

std::unique_ptr<SQLResponse> p;

wstring response;
int status;

try {
p = restQuery ( pStmt->Stmt, pStmt->Conn->Server, pStmt->Conn->ServerPort, pStmt->Conn->UserName, pStmt->Conn->Password,
pStmt->Conn->Project );
__ODBCLOG ( _ODBCLogMsg ( LogLevel_DEBUG, "The REST request succeed" ) );
__ODBCLOG ( _ODBCLogMsg ( LogLevel_DEBUG, "start to call rest api" ) );
response = requestQuery ( pStmt->Stmt, pStmt->Conn->Server, pStmt->Conn->ServerPort, pStmt->Conn->UserName, pStmt->Conn->Password,
pStmt->Conn->Project, &status );
__ODBCLOG ( _ODBCLogMsg ( LogLevel_DEBUG, "received and uncompressed rest response:" ) );
p = convertToSQLResponse(status, response);
__ODBCLOG ( _ODBCLogMsg ( LogLevel_DEBUG, "parsed to SQLResponse" ) );
}

catch ( const exception& e ) {
Expand Down
14 changes: 9 additions & 5 deletions odbc/TestDLL/SimpleQueryTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ void simpleQueryTest ()
{
//Intercept query test
{
std::unique_ptr <SQLResponse> y = restQuery ( L"SELECT 1", KServerAddr, KPort, KUserName, KPassword, KDefaultProject );
int status;
wstring s = requestQuery ( L"SELECT 1", KServerAddr, KPort, KUserName, KPassword, KDefaultProject, &status );
std::unique_ptr <SQLResponse> y = convertToSQLResponse(status, s);

if ( ( int ) y -> results . size () != 1 )
{
Expand All @@ -31,8 +33,9 @@ void simpleQueryTest ()
}
//Ungzipped Query Test
{
std::unique_ptr <SQLResponse> y = restQuery ( L"select cal_dt from test_kylin_fact limit 1", KServerAddr, KPort,
KUserName, KPassword, KDefaultProject );
int status;
wstring s = requestQuery ( L"select cal_dt from test_kylin_fact limit 1", KServerAddr, KPort, KUserName, KPassword, KDefaultProject, &status );
std::unique_ptr <SQLResponse> y = convertToSQLResponse(status, s);

if ( ( int ) y -> results . size () != 1 )
{
Expand All @@ -41,8 +44,9 @@ void simpleQueryTest ()
}
//zipped Query Test
{
std::unique_ptr <SQLResponse> y = restQuery ( L"select * from test_kylin_fact limit 12", KServerAddr, KPort, KUserName,
KPassword, KDefaultProject );
int status;
wstring s = requestQuery ( L"select * from test_kylin_fact limit 12", KServerAddr, KPort, KUserName, KPassword, KDefaultProject, &status );
std::unique_ptr <SQLResponse> y = convertToSQLResponse(status, s);

if ( ( int ) y -> results . size () != 12 )
{
Expand Down

0 comments on commit 0ed5910

Please sign in to comment.