Skip to content

Commit

Permalink
optimize column retrieval
Browse files Browse the repository at this point in the history
  • Loading branch information
zspitzer committed Sep 21, 2023
1 parent 21494b5 commit b23cdfc
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 3 deletions.
4 changes: 4 additions & 0 deletions system/services/database/DbInfoService.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -106,4 +106,8 @@ component {

return constraints;
}

public boolean function asModernDbInfo(){
return variables.hasModernDbInfo;
}
}
38 changes: 35 additions & 3 deletions system/services/presideObjects/SqlSchemaSynchronizer.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "";
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -702,7 +702,27 @@ 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 ( variables.columnCache ?: false ){
variables.columnCache = _getTableColumnCache( argumentCollection = arguments );
}
if ( structKeyExists( variables.columnCache, arguments.tableName ) ) {
return variables.columnCache[ arguments.tableName ];
}
}
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 ) {
Expand Down Expand Up @@ -771,6 +791,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( srcColumns.table_name, srcColumns.column_name );
}
return columns;
}



// GETTERS AND SETTERS
Expand Down

0 comments on commit b23cdfc

Please sign in to comment.