3333#include < cstring>
3434#include < sstream>
3535#include < string>
36+ #include < string_view>
3637#include < vector>
3738
3839#ifdef _WIN32
@@ -207,9 +208,11 @@ inline bool IsDSValueNull(DSValue const& value) {
207208// getting scale ,precision, sign and the value from sting parameter
208209odbc_internal::StatusRecord GetNumericDetailsFromStr (
209210 std::string const & src_dsval, SQL_NUMERIC_STRUCT & numst);
210- inline void StringToDSValue (std::string const & str, DSValue& value) {
211+ inline void StringToDSValue (std::string_view str, DSValue& value) {
211212 value.resize (str.size ());
212- std::copy (str.begin (), str.end (), value.begin ());
213+ if (!str.empty ()) {
214+ std::memcpy (value.data (), str.data (), str.size ());
215+ }
213216}
214217
215218// Func to decode a Base64-encoded string into a vector of bytes.
@@ -457,8 +460,6 @@ inline void ArrayJsonToDSValue(std::string const& str, DSValue& value,
457460 BQDataType array_type) {
458461 nlohmann::json json_data = nlohmann::json::parse (str);
459462 std::string str_data;
460- nlohmann::json obj;
461- obj[" v" ] = json_data;
462463
463464 switch (array_type) {
464465 case BQDataType::kString :
@@ -474,34 +475,40 @@ inline void ArrayJsonToDSValue(std::string const& str, DSValue& value,
474475 case BQDataType::kJson :
475476 case BQDataType::kFloat64 : {
476477 std::vector<std::string> result;
477- for (auto const & item : obj[" v" ]) {
478+ if (json_data.is_array ()) {
479+ result.reserve (json_data.size ());
480+ }
481+ for (auto & item : json_data) {
478482 if (item.contains (" v" ) && item[" v" ].is_string ()) {
479- result.emplace_back (item[" v" ].get <std::string>( ));
483+ result.emplace_back (std::move ( item[" v" ].get_ref <std::string&>() ));
480484 }
481485 }
482486 str_data = nlohmann::json (result).dump ();
483487 break ;
484488 }
485489 case BQDataType::kStruct : {
486490 nlohmann::json str_result = nlohmann::json::array ();
487- for (auto & item : obj[ " v " ] ) {
491+ for (auto & item : json_data ) {
488492 std::string val = item[" v" ][" f" ][0 ][" v" ].get <std::string>();
489493 nlohmann::json obj;
490- obj[" value" ] = val;
491- str_result.emplace_back (obj);
494+ obj[" value" ] = std::move ( val) ;
495+ str_result.emplace_back (std::move ( obj) );
492496 }
493497 str_data = str_result.dump ();
494498 break ;
495499 }
496500 case BQDataType::kInt64 : {
497501 std::vector<std::string> result;
498- for (auto & item : obj[" v" ]) {
502+ if (json_data.is_array ()) {
503+ result.reserve (json_data.size ());
504+ }
505+ for (auto & item : json_data) {
499506 if (item.contains (" v" ) && !item[" v" ].is_null ()) {
500- try {
501- result.emplace_back (std::to_string (item[" v" ].get < int64_t >()));
502- } catch (nlohmann::json::exception const & e) {
507+ if (item[ " v " ]. is_string ()) {
508+ result.emplace_back (std::move (item[" v" ].get_ref <std::string& >()));
509+ } else {
503510 try {
504- result.emplace_back (item[" v" ].get <std::string>( ));
511+ result.emplace_back (std::to_string ( item[" v" ].get <int64_t >() ));
505512 } catch (...) {
506513 result.emplace_back (" 0" );
507514 }
@@ -515,15 +522,18 @@ inline void ArrayJsonToDSValue(std::string const& str, DSValue& value,
515522 }
516523 case BQDataType::kTimeStamp : {
517524 std::vector<std::string> result;
518- for (auto const & item : obj[" v" ]) {
525+ if (json_data.is_array ()) {
526+ result.reserve (json_data.size ());
527+ }
528+ for (auto const & item : json_data) {
519529 if (item.is_object () && item.contains (" v" ) && item[" v" ].is_string ()) {
520530 double unix_timestamp = std::stod (item[" v" ].get <std::string>());
521531 SQL_TIMESTAMP_STRUCT timestamp_struct;
522532 auto status_record = ConvertUnixTimestampToTimestampStruct (
523533 unix_timestamp, timestamp_struct);
524534 if (status_record.ok ()) {
525535 auto timestamp_string = FormatTimestampToString (timestamp_struct);
526- result.emplace_back (timestamp_string);
536+ result.emplace_back (std::move ( timestamp_string) );
527537 } else {
528538 result.emplace_back (" NULL" );
529539 }
@@ -537,8 +547,7 @@ inline void ArrayJsonToDSValue(std::string const& str, DSValue& value,
537547 break ;
538548 }
539549 }
540- value.resize (str_data.size ());
541- std::copy (str_data.begin (), str_data.end (), value.begin ());
550+ StringToDSValue (str_data, value);
542551}
543552
544553// This is the result populated by performing a bq query API.
0 commit comments