From 02b587551ca591feefb7166b089202855587a489 Mon Sep 17 00:00:00 2001 From: Zac Spitzer Date: Thu, 21 Sep 2023 09:10:31 +0200 Subject: [PATCH] optimize column retrieval --- system/services/database/DbInfoService.cfc | 4 ++ .../presideObjects/SqlSchemaSynchronizer.cfc | 40 +++++++++++++++++-- 2 files changed, 41 insertions(+), 3 deletions(-) diff --git a/system/services/database/DbInfoService.cfc b/system/services/database/DbInfoService.cfc index c39d24e030..a38ba1e71e 100644 --- a/system/services/database/DbInfoService.cfc +++ b/system/services/database/DbInfoService.cfc @@ -106,4 +106,8 @@ component { return constraints; } + + public boolean function hasModernDbInfo(){ + return variables.hasModernDbInfo; + } } \ No newline at end of file diff --git a/system/services/presideObjects/SqlSchemaSynchronizer.cfc b/system/services/presideObjects/SqlSchemaSynchronizer.cfc index fa6b71795d..30b6dfaa4f 100644 --- a/system/services/presideObjects/SqlSchemaSynchronizer.cfc +++ b/system/services/presideObjects/SqlSchemaSynchronizer.cfc @@ -167,7 +167,7 @@ component { ) { var adapter = _getAdapter( dsn = arguments.dsn ); var tableExists = _tableExists( tableName=arguments.tableName, dsn=arguments.dsn ); - var tableColumns = tableExists ? QueryColumnData( _getTableColumns( tableName=arguments.tableName, dsn=arguments.dsn ), "COLUMN_NAME" ) : []; + var tableColumns = tableExists ? _getTableColumns( tableName=arguments.tableName, dsn=arguments.dsn ) : []; var columnSql = ""; var colName = ""; var column = ""; @@ -346,7 +346,7 @@ component { , required boolean skipSync ) { - var columnsFromDb = _getTableColumns( tableName=arguments.tableName, dsn=arguments.dsn, detail=true ); + var columnsFromDb = _getTableColumnsWithForeignKeys( tableName=arguments.tableName, dsn=arguments.dsn, detail=true ); var indexesFromDb = _getTableIndexes( tableName=arguments.tableName, dsn=arguments.dsn ); var dbColumnNames = ValueList( columnsFromDb.column_name ); var colsSql = arguments.generatedSql.columns; @@ -702,7 +702,29 @@ component { return variables._tableCache[ arguments.dsn ][ arguments.tableName ] ?: false; } - private query function _getTableColumns() { + private array function _getTableColumns() { + try { + if ( _getDbInfoService().hasModernDbInfo() ){ + // with the faster version, we can fetch the entire schema in one call. + if ( !StructKeyExists( variables, "columnCache" )){ + variables.columnCache = _getTableColumnCache( argumentCollection = arguments ); + } + if ( StructKeyExists( variables.columnCache, arguments.tableName ) ) { + return variables.columnCache[ arguments.tableName ]; + } else { + return []; + } + } + return _getDbInfoService().getTableColumns( argumentCollection = arguments ); + } catch( any e ) { + if ( e.message contains "there is no table that match the following pattern" ) { + return []; + } + rethrow; + } + } + + private query function _getTableColumnsWithForeignKeys() { try { return _getDbInfoService().getTableColumns( argumentCollection = arguments ); } catch( any e ) { @@ -771,6 +793,18 @@ component { } } + private struct function _getTableColumnCache(){ + var srcColumns = _getDbInfoService().getTableColumns( tableName="%", dsn=arguments.dsn, detail=false ); + var columns = {}; + loop query=srcColumns { + if ( !StructKeyExists( columns, srcColumns.table_name ) ) { + columns[ srcColumns.table_name ] = []; + } + ArrayAppend( columns[ srcColumns.table_name ], srcColumns.column_name ); + } + return columns; + } + // GETTERS AND SETTERS