Support SQLite generated columns in database libraries

SQLite ODBC drivers use PRAGMA table_info to implement SQLColumns,
which doesn't return generated columns. This caused KiCad to silently
ignore any generated column configured in a .kicad_dbl file.

Now CacheTableInfo trusts user-configured column names and adds them
to the cache even if the ODBC driver doesn't report them. If a column
truly doesn't exist, the subsequent query will fail with a clear error
rather than silently omitting the data.

Also adds a test case with a table containing a virtual generated column.

Fixes https://gitlab.com/kicad/code/kicad/-/issues/16952
This commit is contained in:
Seth Hillbrand
2026-01-29 14:44:09 -08:00
parent acbcb5e014
commit 8c34640520
3 changed files with 63 additions and 1 deletions
+24 -1
View File
@@ -233,14 +233,37 @@ bool DATABASE_CONNECTION::CacheTableInfo( const std::string& aTable,
nanodbc::catalog::columns columns =
catalog.find_columns( NANODBC_TEXT( "" ), tables.table_name() );
std::set<std::string> columnsInCatalog;
while( columns.next() )
{
std::string columnKey = toUTF8( columns.column_name() );
std::string columnKeyLower = boost::to_lower_copy( columnKey );
if( aColumns.count( boost::to_lower_copy( columnKey ) ) )
if( aColumns.count( columnKeyLower ) )
{
m_columnCache[key][columnKey] = columns.data_type();
columnsInCatalog.insert( columnKeyLower );
}
}
// Some ODBC drivers (notably SQLite) don't report all columns via SQLColumns.
// For example, SQLite's PRAGMA table_info used by its ODBC driver doesn't return
// generated columns. Trust the user's configuration and add any requested columns
// that weren't found in the catalog. The actual query will fail with a clear error
// if the column doesn't exist, which is better than silently ignoring it.
for( const std::string& requestedCol : aColumns )
{
if( !columnsInCatalog.count( requestedCol ) && !requestedCol.empty() )
{
wxLogTrace( traceDatabase,
wxT( "CacheTableInfo: column '%s' not found in catalog for table "
"'%s', adding anyway" ),
requestedCol, key );
m_columnCache[key][requestedCol] = SQL_VARCHAR;
}
}
}
catch( nanodbc::database_error& e )
{
Binary file not shown.
+39
View File
@@ -75,4 +75,43 @@ BOOST_AUTO_TEST_CASE( Connect )
BOOST_CHECK_EQUAL( std::any_cast<std::string>( result.at( "Cost" ) ), "1.95" );
}
/**
* Test that generated columns in SQLite are properly handled.
*
* SQLite ODBC drivers use PRAGMA table_info to implement SQLColumns, which doesn't
* return generated columns. This test verifies that KiCad can still query tables
* with generated columns by trusting user-configured column names.
*
* See https://gitlab.com/kicad/code/kicad/-/issues/16952
*/
BOOST_AUTO_TEST_CASE( GeneratedColumns )
{
std::string cs = fmt::format( "Driver={{SQLite3}};Database={}/database.sqlite",
QA_DATABASE_FILE_LOCATION );
DATABASE_CONNECTION dc( cs, 2 );
BOOST_CHECK( dc.IsConnected() );
// Cache table info including the generated column "FullDesc"
// The SQLite ODBC driver won't report this column via SQLColumns,
// but our fix should add it to the cache anyway
dc.CacheTableInfo( "GeneratedTest", { "part id", "value", "resistance", "fulldesc" } );
DATABASE_CONNECTION::ROW result;
BOOST_CHECK( dc.SelectOne( "GeneratedTest", std::make_pair( "Part ID", "GEN-001" ), result ) );
BOOST_CHECK( !result.empty() );
// Verify we can read the generated column value
BOOST_CHECK( result.count( "FullDesc" ) );
BOOST_CHECK_NO_THROW( std::any_cast<std::string>( result.at( "FullDesc" ) ) );
BOOST_CHECK_EQUAL( std::any_cast<std::string>( result.at( "FullDesc" ) ), "10K - 10000ohm" );
// Also verify regular columns still work
BOOST_CHECK( result.count( "Value" ) );
BOOST_CHECK_EQUAL( std::any_cast<std::string>( result.at( "Value" ) ), "10K" );
}
BOOST_AUTO_TEST_SUITE_END()