DbLib: support columns with numeric data

Fixes https://gitlab.com/kicad/code/kicad/-/issues/15662
This commit is contained in:
Jon Evans 2023-09-16 22:37:12 -04:00
parent 2de38f8d75
commit 6f9af10912
4 changed files with 55 additions and 3 deletions

View File

@ -435,7 +435,22 @@ bool DATABASE_CONNECTION::SelectOne( const std::string& aTable,
for( short i = 0; i < results.columns(); ++i )
{
std::string column = toUTF8( results.column_name( i ) );
aResult[ column ] = toUTF8( results.get<nanodbc::string>( i, NANODBC_TEXT( "" ) ) );
switch( results.column_datatype( i ) )
{
case SQL_DOUBLE:
case SQL_FLOAT:
case SQL_REAL:
case SQL_DECIMAL:
case SQL_NUMERIC:
{
aResult[column] = fmt::format( "{:G}", results.get<double>( i ) );
break;
}
default:
aResult[column] = toUTF8( results.get<nanodbc::string>( i, NANODBC_TEXT( "" ) ) );
}
}
}
catch( nanodbc::database_error& e )
@ -534,7 +549,23 @@ bool DATABASE_CONNECTION::SelectAll( const std::string& aTable, const std::strin
for( short j = 0; j < results.columns(); ++j )
{
std::string column = toUTF8( results.column_name( j ) );
result[column] = toUTF8( results.get<nanodbc::string>( j, NANODBC_TEXT( "" ) ) );
switch( results.column_datatype( j ) )
{
case SQL_DOUBLE:
case SQL_FLOAT:
case SQL_REAL:
case SQL_DECIMAL:
case SQL_NUMERIC:
{
result[column] = fmt::format( "{:G}", results.get<double>( j ) );
break;
}
default:
result[column] = toUTF8( results.get<nanodbc::string>( j,
NANODBC_TEXT( "" ) ) );
}
}
aResults.emplace_back( std::move( result ) );

Binary file not shown.

View File

@ -102,6 +102,18 @@
"name": "Voltage Rating",
"visible_on_add": true,
"visible_in_chooser": true
},
{
"column": "Cost",
"name": "Cost",
"visible_on_add": false,
"visible_in_chooser": true
},
{
"column": "Mass",
"name": "Mass",
"visible_on_add": false,
"visible_in_chooser": true
}
],
"properties": {
@ -109,4 +121,4 @@
}
}
]
}
}

View File

@ -54,6 +54,8 @@ BOOST_AUTO_TEST_CASE( Connect )
}
dc.Connect();
dc.CacheTableInfo( "Resistors", { "Part ID", "MPN" } );
dc.CacheTableInfo( "Capacitors", { "Part ID", "Cost" } );
BOOST_CHECK( dc.IsConnected() );
DATABASE_CONNECTION::ROW result;
@ -64,6 +66,13 @@ BOOST_AUTO_TEST_CASE( Connect )
BOOST_CHECK( result.count( "MPN" ) );
BOOST_CHECK_NO_THROW( std::any_cast<std::string>( result.at( "MPN" ) ) );
BOOST_CHECK_EQUAL( std::any_cast<std::string>( result.at( "MPN" ) ), "RC0603FR-0710KL" );
BOOST_CHECK( dc.SelectOne( "Capacitors", std::make_pair( "Part ID", "CAP-003" ), result ) );
BOOST_CHECK( !result.empty() );
BOOST_CHECK( result.count( "Cost" ) );
BOOST_CHECK_NO_THROW( std::any_cast<std::string>( result.at( "Cost" ) ) );
BOOST_CHECK_EQUAL( std::any_cast<std::string>( result.at( "Cost" ) ), "1.95" );
}
BOOST_AUTO_TEST_SUITE_END()