Make debug-logging SQL controlled by system property

This commit is contained in:
2023-02-24 16:15:56 -06:00
parent 5074ed1867
commit 4db174b66d
8 changed files with 49 additions and 9 deletions

View File

@ -39,6 +39,7 @@ import com.kingsrook.qqq.backend.core.actions.QBackendTransaction;
import com.kingsrook.qqq.backend.core.actions.interfaces.QActionInterface; import com.kingsrook.qqq.backend.core.actions.interfaces.QActionInterface;
import com.kingsrook.qqq.backend.core.exceptions.QException; import com.kingsrook.qqq.backend.core.exceptions.QException;
import com.kingsrook.qqq.backend.core.exceptions.QValueException; import com.kingsrook.qqq.backend.core.exceptions.QValueException;
import com.kingsrook.qqq.backend.core.logging.LogPair;
import com.kingsrook.qqq.backend.core.logging.QLogger; import com.kingsrook.qqq.backend.core.logging.QLogger;
import com.kingsrook.qqq.backend.core.model.actions.AbstractTableActionInput; import com.kingsrook.qqq.backend.core.model.actions.AbstractTableActionInput;
import com.kingsrook.qqq.backend.core.model.actions.tables.aggregate.Aggregate; import com.kingsrook.qqq.backend.core.model.actions.tables.aggregate.Aggregate;
@ -68,6 +69,7 @@ import com.kingsrook.qqq.backend.module.rdbms.jdbc.ConnectionManager;
import com.kingsrook.qqq.backend.module.rdbms.jdbc.QueryManager; import com.kingsrook.qqq.backend.module.rdbms.jdbc.QueryManager;
import com.kingsrook.qqq.backend.module.rdbms.model.metadata.RDBMSBackendMetaData; import com.kingsrook.qqq.backend.module.rdbms.model.metadata.RDBMSBackendMetaData;
import com.kingsrook.qqq.backend.module.rdbms.model.metadata.RDBMSTableBackendDetails; import com.kingsrook.qqq.backend.module.rdbms.model.metadata.RDBMSTableBackendDetails;
import static com.kingsrook.qqq.backend.core.logging.LogUtils.logPair;
/******************************************************************************* /*******************************************************************************
@ -938,4 +940,29 @@ public abstract class AbstractRDBMSAction implements QActionInterface
return (String.format(groupBy.getFormatString(), fullFieldName)); return (String.format(groupBy.getFormatString(), fullFieldName));
} }
} }
/*******************************************************************************
**
*******************************************************************************/
protected void logSQL(CharSequence sql, List<?> params)
{
if(System.getProperty("qqq.rdbms.logSQL", "false").equals("true"))
{
try
{
LogPair paramsLogPair = params == null ? null :
params.size() <= 100 ? logPair("params", params) :
logPair("first100Params", params.subList(0, 99));
LOG.debug("Running SQL", logPair("sql", sql), paramsLogPair);
}
catch(Exception e)
{
LOG.debug("Error logging sql...", e);
}
}
}
} }

View File

@ -86,7 +86,8 @@ public class RDBMSAggregateAction extends AbstractRDBMSAction implements Aggrega
} }
// todo sql customization - can edit sql and/or param list // todo sql customization - can edit sql and/or param list
LOG.debug(sql); // todo not commit - downgrade to trace
logSQL(sql, params);
AggregateOutput rs = new AggregateOutput(); AggregateOutput rs = new AggregateOutput();
List<AggregateResult> results = new ArrayList<>(); List<AggregateResult> results = new ArrayList<>();

View File

@ -64,9 +64,10 @@ public class RDBMSCountAction extends AbstractRDBMSAction implements CountInterf
QQueryFilter filter = countInput.getFilter(); QQueryFilter filter = countInput.getFilter();
List<Serializable> params = new ArrayList<>(); List<Serializable> params = new ArrayList<>();
sql += " WHERE " + makeWhereClause(countInput.getInstance(), countInput.getSession(), table, joinsContext, filter, params); sql += " WHERE " + makeWhereClause(countInput.getInstance(), countInput.getSession(), table, joinsContext, filter, params);
// todo sql customization - can edit sql and/or param list // todo sql customization - can edit sql and/or param list
logSQL(sql, params);
CountOutput rs = new CountOutput(); CountOutput rs = new CountOutput();
try(Connection connection = getConnection(countInput)) try(Connection connection = getConnection(countInput))

View File

@ -190,6 +190,8 @@ public class RDBMSDeleteAction extends AbstractRDBMSAction implements DeleteInte
+ " WHERE " + " WHERE "
+ escapeIdentifier(primaryKeyName) + " = ?"; + escapeIdentifier(primaryKeyName) + " = ?";
logSQL(sql, List.of(primaryKey));
try try
{ {
int rowCount = QueryManager.executeUpdateForRowCount(connection, sql, primaryKey); int rowCount = QueryManager.executeUpdateForRowCount(connection, sql, primaryKey);
@ -237,6 +239,7 @@ public class RDBMSDeleteAction extends AbstractRDBMSAction implements DeleteInte
+ ")"; + ")";
// todo sql customization - can edit sql and/or param list // todo sql customization - can edit sql and/or param list
logSQL(sql, primaryKeys);
Integer rowCount = QueryManager.executeUpdateForRowCount(connection, sql, primaryKeys); Integer rowCount = QueryManager.executeUpdateForRowCount(connection, sql, primaryKeys);
deleteOutput.addToDeletedRecordCount(rowCount); deleteOutput.addToDeletedRecordCount(rowCount);
@ -267,6 +270,7 @@ public class RDBMSDeleteAction extends AbstractRDBMSAction implements DeleteInte
+ escapeIdentifier(tableName) + " AS " + escapeIdentifier(table.getName()) + escapeIdentifier(tableName) + " AS " + escapeIdentifier(table.getName())
+ " WHERE " + " WHERE "
+ whereClause; + whereClause;
logSQL(sql, params);
try try
{ {

View File

@ -153,6 +153,8 @@ public class RDBMSInsertAction extends AbstractRDBMSAction implements InsertInte
continue; continue;
} }
logSQL(sql, params);
/////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////
// execute the insert, then foreach record in the input, // // execute the insert, then foreach record in the input, //
// add it to the output, and set its generated id too. // // add it to the output, and set its generated id too. //

View File

@ -137,7 +137,8 @@ public class RDBMSQueryAction extends AbstractRDBMSAction implements QueryInterf
// execute the query - iterate over results // // execute the query - iterate over results //
////////////////////////////////////////////// //////////////////////////////////////////////
QueryOutput queryOutput = new QueryOutput(queryInput); QueryOutput queryOutput = new QueryOutput(queryInput);
// System.out.println(sql); logSQL(sql, params);
PreparedStatement statement = createStatement(connection, sql.toString(), queryInput); PreparedStatement statement = createStatement(connection, sql.toString(), queryInput);
QueryManager.executeStatement(statement, ((ResultSet resultSet) -> QueryManager.executeStatement(statement, ((ResultSet resultSet) ->
{ {

View File

@ -72,13 +72,13 @@ public class RDBMSTransaction extends QBackendTransaction
{ {
try try
{ {
RDBMSTransaction.LOG.debug("Committing transaction"); LOG.debug("Committing transaction");
connection.commit(); connection.commit();
RDBMSTransaction.LOG.debug("Commit complete"); LOG.debug("Commit complete");
} }
catch(Exception e) catch(Exception e)
{ {
RDBMSTransaction.LOG.error("Error committing transaction", e); LOG.error("Error committing transaction", e);
throw new QException("Error committing transaction: " + e.getMessage(), e); throw new QException("Error committing transaction: " + e.getMessage(), e);
} }
} }
@ -93,13 +93,13 @@ public class RDBMSTransaction extends QBackendTransaction
{ {
try try
{ {
RDBMSTransaction.LOG.info("Rolling back transaction"); LOG.info("Rolling back transaction");
connection.rollback(); connection.rollback();
RDBMSTransaction.LOG.info("Rollback complete"); LOG.info("Rollback complete");
} }
catch(Exception e) catch(Exception e)
{ {
RDBMSTransaction.LOG.error("Error rolling back transaction", e); LOG.error("Error rolling back transaction", e);
throw new QException("Error rolling back transaction: " + e.getMessage(), e); throw new QException("Error rolling back transaction: " + e.getMessage(), e);
} }
} }

View File

@ -204,6 +204,8 @@ public class RDBMSUpdateAction extends AbstractRDBMSAction implements UpdateInte
rowValues.add(record.getValue(table.getPrimaryKeyField())); rowValues.add(record.getValue(table.getPrimaryKeyField()));
} }
logSQL(sql, values);
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
// let query manager do the batch updates - note that it will internally page // // let query manager do the batch updates - note that it will internally page //
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
@ -261,6 +263,8 @@ public class RDBMSUpdateAction extends AbstractRDBMSAction implements UpdateInte
params.add(record.getValue(table.getPrimaryKeyField())); params.add(record.getValue(table.getPrimaryKeyField()));
} }
logSQL(sql, params);
///////////////////////////////////// /////////////////////////////////////
// let query manager do the update // // let query manager do the update //
///////////////////////////////////// /////////////////////////////////////