From dcd1de7f58e3d6874c5d517441b9ccaca83766db Mon Sep 17 00:00:00 2001 From: Matthew Bellew Date: Tue, 4 Aug 2020 12:22:37 -0700 Subject: [PATCH] fix getSelectName() assert add assert to WrappedColumnInfo.getSelectName() --- .../org/labkey/api/data/StatementUtils.java | 25 +++++++++++++------ .../labkey/api/data/WrappedColumnInfo.java | 1 + 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/api/src/org/labkey/api/data/StatementUtils.java b/api/src/org/labkey/api/data/StatementUtils.java index c746e31427a..c75bf37f5e4 100644 --- a/api/src/org/labkey/api/data/StatementUtils.java +++ b/api/src/org/labkey/api/data/StatementUtils.java @@ -71,7 +71,7 @@ public enum Operation {insert, update, merge} // configuration parameters private Operation _operation = Operation.insert; private SqlDialect _dialect; - private TableInfo _table; + private TableInfo _targetTable; private Set _keyColumnNames = null; // override the primary key of _table private Set _skipColumnNames = null; private Set _dontUpdateColumnNames = new CaseInsensitiveHashSet(); @@ -97,7 +97,7 @@ public StatementUtils(@NotNull Operation op, @NotNull TableInfo table) { _operation = op; _dialect = table.getSqlDialect(); - _table = table; + _targetTable = table; } public StatementUtils dialect(SqlDialect dialect) @@ -465,10 +465,10 @@ public void setObjectUriPreselect(SQLFragment sqlfPreselectObject, TableInfo tab public ParameterMapStatement createStatement(Connection conn, @Nullable Container c, User user) throws SQLException { - if (!(_table instanceof UpdateableTableInfo)) + if (!(_targetTable instanceof UpdateableTableInfo)) throw new IllegalArgumentException("Table must be an UpdateableTableInfo"); - UpdateableTableInfo updatable = (UpdateableTableInfo)_table; + UpdateableTableInfo updatable = (UpdateableTableInfo) _targetTable; TableInfo table = updatable.getSchemaTableInfo(); if (table.getTableType() != DatabaseTableType.TABLE || null == table.getMetaDataName()) @@ -534,8 +534,17 @@ public ParameterMapStatement createStatement(Connection conn, @Nullable Containe keys.put(col.getFieldKey(), col); else { - for (ColumnInfo pk : _table.getPkColumns()) - keys.put(pk.getFieldKey(), pk); + // see 26661 and 41053 + // NOTE: IMO we should not be using updatable.getPkColumns() here! If the caller doesn't want to use the + // 'real' PK from the SchemaTableInfo for update/merge, then the alternate keys should be explicitly specified + // using StatementUtils.keys() + for (String pkName : updatable.getPkColumnNames()) + { + col = table.getColumn(pkName); + if (null == col) + throw new IllegalStateException("pk column not found: " + pkName); + keys.put(col.getFieldKey(), col); + } } } @@ -550,8 +559,8 @@ public ParameterMapStatement createStatement(Connection conn, @Nullable Containe SQLFragment sqlfObjectProperty = new SQLFragment(); SQLFragment sqlfDelete = new SQLFragment(); - Domain domain = _table.getDomain(); - DomainKind domainKind = _table.getDomainKind(); + Domain domain = updatable.getDomain(); + DomainKind domainKind = updatable.getDomainKind(); List properties = Collections.emptyList(); boolean hasObjectURIColumn = objectURIColumnName != null && table.getColumn(objectURIColumnName) != null; diff --git a/api/src/org/labkey/api/data/WrappedColumnInfo.java b/api/src/org/labkey/api/data/WrappedColumnInfo.java index 85c37dfbd6c..d926bbd0d65 100644 --- a/api/src/org/labkey/api/data/WrappedColumnInfo.java +++ b/api/src/org/labkey/api/data/WrappedColumnInfo.java @@ -114,6 +114,7 @@ public SQLFragment getValueSql(String tableAlias) @Override public String getSelectName() { + assert getParentTable() instanceof SchemaTableInfo : "Use getValueSql()"; return sourceColumnInfo.getSelectName(); }