@@ -1322,18 +1322,30 @@ void DatabaseContext::Private::attachExtraDatabases(
13221322 auto l_handle = handle ();
13231323 assert (l_handle);
13241324
1325- auto tables =
1326- run (" SELECT name FROM sqlite_master WHERE type IN ('table', 'view') "
1327- " AND name NOT LIKE 'sqlite_stat%'" );
1328- std::map<std::string, std::vector<std::string>> tableStructure;
1325+ auto tables = run (" SELECT name, type, sql FROM sqlite_master WHERE type IN "
1326+ " ('table', 'view') "
1327+ " AND name NOT LIKE 'sqlite_stat%'" );
1328+
1329+ struct TableStructure {
1330+ std::string name{};
1331+ bool isTable = false ;
1332+ std::string sql{};
1333+ std::vector<std::string> columns{};
1334+ };
1335+ std::vector<TableStructure> tablesStructure;
13291336 for (const auto &rowTable : tables) {
1330- const auto &tableName = rowTable[0 ];
1331- auto tableInfo = run (" PRAGMA table_info(\" " +
1332- replaceAll (tableName, " \" " , " \"\" " ) + " \" )" );
1337+ TableStructure tableStructure;
1338+ tableStructure.name = rowTable[0 ];
1339+ tableStructure.isTable = rowTable[1 ] == " table" ;
1340+ tableStructure.sql = rowTable[2 ];
1341+ auto tableInfo =
1342+ run (" PRAGMA table_info(\" " +
1343+ replaceAll (tableStructure.name , " \" " , " \"\" " ) + " \" )" );
13331344 for (const auto &rowCol : tableInfo) {
13341345 const auto &colName = rowCol[1 ];
1335- tableStructure[tableName] .push_back (colName);
1346+ tableStructure. columns .push_back (colName);
13361347 }
1348+ tablesStructure.push_back (std::move (tableStructure));
13371349 }
13381350
13391351 const int nLayoutVersionMajor = l_handle->getLayoutVersionMajor ();
@@ -1370,38 +1382,46 @@ void DatabaseContext::Private::attachExtraDatabases(
13701382 attachedDbName + ' .' );
13711383 }
13721384
1373- for (const auto &pair : tableStructure) {
1374- std::string sql (" CREATE TEMP VIEW " );
1375- sql += pair.first ;
1376- sql += " AS " ;
1377- for (size_t i = 0 ; i <= auxiliaryDatabasePaths.size (); ++i) {
1378- std::string selectFromAux (" SELECT " );
1379- bool firstCol = true ;
1380- for (const auto &colName : pair.second ) {
1381- if (!firstCol) {
1382- selectFromAux += " , " ;
1385+ for (const auto &tableStructure : tablesStructure) {
1386+ if (tableStructure.isTable ) {
1387+ std::string sql (" CREATE TEMP VIEW " );
1388+ sql += tableStructure.name ;
1389+ sql += " AS " ;
1390+ for (size_t i = 0 ; i <= auxiliaryDatabasePaths.size (); ++i) {
1391+ std::string selectFromAux (" SELECT " );
1392+ bool firstCol = true ;
1393+ for (const auto &colName : tableStructure.columns ) {
1394+ if (!firstCol) {
1395+ selectFromAux += " , " ;
1396+ }
1397+ firstCol = false ;
1398+ selectFromAux += colName;
13831399 }
1384- firstCol = false ;
1385- selectFromAux += colName;
1386- }
1387- selectFromAux += " FROM db_" ;
1388- selectFromAux += toString (static_cast <int >(i));
1389- selectFromAux += " ." ;
1390- selectFromAux += pair.first ;
1400+ selectFromAux += " FROM db_" ;
1401+ selectFromAux += toString (static_cast <int >(i));
1402+ selectFromAux += " ." ;
1403+ selectFromAux += tableStructure.name ;
13911404
1392- try {
1393- // Check that the request will succeed. In case of 'sparse'
1394- // databases...
1395- run (selectFromAux + " LIMIT 0" );
1396-
1397- if (i > 0 ) {
1398- sql += " UNION ALL " ;
1405+ try {
1406+ // Check that the request will succeed. In case of 'sparse'
1407+ // databases...
1408+ run (selectFromAux + " LIMIT 0" );
1409+
1410+ if (i > 0 ) {
1411+ if (tableStructure.name == " conversion_method" )
1412+ sql += " UNION " ;
1413+ else
1414+ sql += " UNION ALL " ;
1415+ }
1416+ sql += selectFromAux;
1417+ } catch (const std::exception &) {
13991418 }
1400- sql += selectFromAux;
1401- } catch (const std::exception &) {
14021419 }
1420+ run (sql);
1421+ } else {
1422+ run (replaceAll (tableStructure.sql , " CREATE VIEW" ,
1423+ " CREATE TEMP VIEW" ));
14031424 }
1404- run (sql);
14051425 }
14061426}
14071427
0 commit comments