protected void postProcessRootIdSelect() { String what = null; String where = null; for (Column column : table.getColumns()) { String key = column.getKey(); String qname = column.getQuotedName(); if (key.equals(Model.MAIN_KEY)) { what = qname; selectRootIdWhatColumn = column; } else if (key.equals(Model.REPOINFO_REPONAME_KEY)) { where = qname + " = ?"; } else { throw new RuntimeException(column.toString()); } } Select select = new Select(table); select.setWhat(what); select.setFrom(table.getQuotedName()); select.setWhere(where); selectRootIdSql = select.getStatement(); }
+ dialect.closeQuote(); withTables.add(withTable); Select withSelect = new Select(null); withSelect.setWhat("*"); String withFrom = withTable; for (Join j : securityJoins) { withFrom += j.toSql(dialect); withSelect.setFrom(withFrom); withSelect.setWhere(securityClause); withSelects.add(withSelect); withSelectsStatements.add(withSelect.getStatement()); withParams.addAll(securityParams); } else { select = new Select(null); select.setWhat(selectWhat); selectParams.addAll(whatNamesParams); select.setFrom(fromb.toString()); select.setWhere(StringUtils.join(whereClauses, " AND ")); selectParams.addAll(whereParams); statements.add(select.getStatement()); select = new Select(null); select.setWhat(selectWhat);
@Override protected void fixSelect(Select select) { if (type == COUNT_SOURCE) { // add a GROUP BY on first col String name; if (dialect.needsOriginalColumnInGroupBy()) { name = firstSelectedColumn.getFullQuotedName(); } else { name = dialect.openQuote() + COL_ALIAS_PREFIX + "1" + dialect.closeQuote(); } select.setGroupBy(name); } }
public boolean exists(String sourceId, String targetId, SQLSession session) { // "SELECT COUNT(*) FROM %s WHERE %s = ? AND %s = ?", tableName, sourceColumn, targetColumn Select select = new Select(table); select.setFrom(table.getQuotedName()); select.setWhat("count(*)"); String whereString = String.format("%s = ? and %s = ?", table.getColumn(sourceColumn).getQuotedName(), table.getColumn(targetColumn).getQuotedName()); select.setWhere(whereString); String selectSql = select.getStatement(); if (session.logger.isLogEnabled()) { session.logger.logSQL(selectSql, Arrays.<Serializable> asList(sourceId, targetId)); } try (PreparedStatement ps = session.sqlConnection.prepareStatement(selectSql)) { ps.setString(1, sourceId); ps.setString(2, targetId); try (ResultSet rs = ps.executeQuery()) { rs.next(); return rs.getInt(1) > 0; } } catch (SQLException e) { throw new DirectoryException(String.format("error reading link from %s to %s", sourceId, targetId), e); } }
/** * Select parentid by ids for all values of several fragments. */ public SQLInfoSelect getSelectParentIds(int nids) { Table table = database.getTable(Model.HIER_TABLE_NAME); Column whatColumn = table.getColumn(Model.HIER_PARENT_KEY); Column whereColumn = table.getColumn(Model.MAIN_KEY); StringBuilder wherebuf = new StringBuilder(whereColumn.getQuotedName()); wherebuf.append(" IN ("); for (int i = 0; i < nids; i++) { if (i != 0) { wherebuf.append(", "); } wherebuf.append('?'); } wherebuf.append(')'); wherebuf.append(getSoftDeleteClause(Model.HIER_TABLE_NAME)); Select select = new Select(table); select.setWhat("DISTINCT " + whatColumn.getQuotedName()); select.setFrom(table.getQuotedName()); select.setWhere(wherebuf.toString()); return new SQLInfoSelect(select.getStatement(), Collections.singletonList(whatColumn), Collections.singletonList(whereColumn), null); }
protected List<String> getIdsFor(String valueColumn, String filterColumn, String filterValue) { try (SQLSession session = getSQLSession()) { // "SELECT %s FROM %s WHERE %s = ?", table.getColumn(valueColumn), tableName, filterColumn Select select = new Select(table); select.setWhat(table.getColumn(valueColumn).getQuotedName()); select.setFrom(table.getQuotedName()); select.setWhere(table.getColumn(filterColumn).getQuotedName() + " = ?"); String sql = select.getStatement(); if (session.logger.isLogEnabled()) { session.logger.logSQL(sql, Collections.<Serializable> singleton(filterValue)); } List<String> ids = new LinkedList<String>(); try (PreparedStatement ps = session.sqlConnection.prepareStatement(sql)) { ps.setString(1, filterValue); try (ResultSet rs = ps.executeQuery()) { while (rs.next()) { ids.add(rs.getString(valueColumn)); } return ids; } } catch (SQLException e) { throw new DirectoryException("error fetching reference values: ", e); } } }
protected void postProcessCopy() { Collection<Column> columns = table.getColumns(); List<String> selectWhats = new ArrayList<>(columns.size()); Column copyIdColumn = table.getColumn(Model.MAIN_KEY); Insert insert = new Insert(table); for (Column column : columns) { if (column.isIdentity()) { // identity column is never copied continue; } insert.addColumn(column); if (column == copyIdColumn) { // explicit value selectWhats.add("?"); } else { // otherwise copy value selectWhats.add(column.getQuotedName()); } } Select select = new Select(table); select.setWhat(String.join(", ", selectWhats)); select.setFrom(table.getQuotedName()); select.setWhere(copyIdColumn.getQuotedName() + " = ?"); insert.setValues(select.getStatement()); copySqlMap.put(tableName, insert.getStatement()); copyIdColumnMap.put(tableName, copyIdColumn); }
protected void postProcessSelectChildrenIdsAndTypes() { List<Column> whatColumns = new ArrayList<>(2); List<String> whats = new ArrayList<>(2); Column column = table.getColumn(Model.MAIN_KEY); whatColumns.add(column); whats.add(column.getQuotedName()); column = table.getColumn(Model.MAIN_PRIMARY_TYPE_KEY); whatColumns.add(column); whats.add(column.getQuotedName()); column = table.getColumn(Model.MAIN_MIXIN_TYPES_KEY); whatColumns.add(column); whats.add(column.getQuotedName()); Select select = new Select(table); select.setWhat(String.join(", ", whats)); select.setFrom(table.getQuotedName()); String where = table.getColumn(Model.HIER_PARENT_KEY).getQuotedName() + " = ?" + getSoftDeleteClause(tableName); select.setWhere(where); selectChildrenIdsAndTypesSql = select.getStatement(); selectChildrenIdsAndTypesWhatColumns = whatColumns; // now only complex properties where += " AND " + table.getColumn(Model.HIER_CHILD_ISPROPERTY_KEY).getQuotedName() + " = " + dialect.toBooleanValueString(true); select.setWhere(where); selectComplexChildrenIdsAndTypesSql = select.getStatement(); }
Select select = new Select(table); select.setWhat(String.join(", ", whats)); select.setFrom(table.getQuotedName()); select.setWhere(wherebuf.toString()); if (orderBys != null) { List<String> orders = new LinkedList<>(); orders.add(table.getColumn(orderBy).getQuotedName()); select.setOrderBy(String.join(", ", orders)); return new SQLInfoSelect(select.getStatement(), whatColumns, Collections.singletonList(whereColumn), opaqueColumns.isEmpty() ? null : opaqueColumns);
@Override public boolean hasEntry(String id) { acquireConnection(); Select select = new Select(table); select.setFrom(table.getQuotedName()); select.setWhat("1"); select.setWhere(table.getPrimaryColumn().getQuotedName() + " = ?"); String sql = select.getStatement(); if (logger.isLogEnabled()) { logger.logSQL(sql, Collections.singleton(id)); } try (PreparedStatement ps = sqlConnection.prepareStatement(sql)) { setFieldValue(ps, 1, table.getPrimaryColumn(), id); try (ResultSet rs = ps.executeQuery()) { boolean has = rs.next(); if (logger.isLogEnabled()) { logger.logCount(has ? 1 : 0); } return has; } } catch (SQLException e) { throw new DirectoryException("hasEntry failed", e); } }
Select select = new Select(null); select.setFrom(table.getQuotedName()); select.setWhat(String.join(", ", selectWhats)); select.setWhere(whereColumn.getQuotedName() + " = ?"); insert.setValues(select.getStatement()); String sql = insert.getStatement(); return new SQLInfoSelect(sql, selectWhatColumns, Collections.singletonList(whereColumn), null);
wheres.addAll(clauses); Select select = new Select(table); select.setWhat(String.join(", ", whats)); if (from == null) { from = table.getQuotedName(); select.setFrom(from); String where = String.join(" AND ", wheres) + getSoftDeleteClause(table.getKey()); select.setWhere(where); List<String> orders = new LinkedList<>(); for (int i = 0; i < orderBys.length; i++) { orders.add(qcol + ascdesc); select.setOrderBy(String.join(", ", orders)); return new SQLInfoSelect(select.getStatement(), whatColumns, whereColumns, opaqueColumns.isEmpty() ? null : opaqueColumns);
/** * Select selection ids for multiple values. */ public SQLInfoSelect getSelectSelectionIds(int nids) { Table table = database.getTable(type.tableName); String from = table.getQuotedName(); Table hierTable = database.getTable(Model.HIER_TABLE_NAME); Join join = new Join(Join.INNER, hierTable.getQuotedName(), null, null, hierTable.getColumn(Model.MAIN_KEY), table.getColumn(Model.MAIN_KEY)); from += join.toSql(dialect); Column whatColumn = table.getColumn(Model.MAIN_KEY); Column whereColumn = table.getColumn(type.selKey); StringBuilder wherebuf = new StringBuilder(whereColumn.getQuotedName()); wherebuf.append(" IN ("); for (int i = 0; i < nids; i++) { if (i != 0) { wherebuf.append(", "); } wherebuf.append('?'); } wherebuf.append(')'); wherebuf.append(getSoftDeleteClause(Model.HIER_TABLE_NAME)); Select select = new Select(table); select.setWhat(whatColumn.getFullQuotedName()); select.setFrom(from); select.setWhere(wherebuf.toString()); return new SQLInfoSelect(select.getStatement(), Collections.singletonList(whatColumn), Collections.singletonList(whereColumn), null); } }
Select select = new Select(null); select.setWhat(selectWhats); String from = hierTable.getQuotedName(); if (proxiesEnabled) { + proxyTable.getColumn(Model.MAIN_KEY).getFullQuotedName(); select.setFrom(from); Column whereColumn = hierTable.getColumn(Model.HIER_PARENT_KEY); StringBuilder wherebuf = new StringBuilder(whereColumn.getFullQuotedName()); wherebuf.append(" = ").append(dialect.toBooleanValueString(false)); // not complex wherebuf.append(getSoftDeleteClause(Model.HIER_TABLE_NAME)); select.setWhere(wherebuf.toString()); return new SQLInfoSelect(select.getStatement(), whatColumns, Collections.singletonList(whereColumn), null);
Select select = new Select(null); select.setWhat(whats); String from = hierTable.getQuotedName(); if (proxiesEnabled) { + proxyTable.getColumn(Model.MAIN_KEY).getFullQuotedName(); select.setFrom(from); String where = dialect.getInTreeSql(mainColumn.getFullQuotedName(), null); where += getSoftDeleteClause(Model.HIER_TABLE_NAME); select.setWhere(where); selectDescendantsInfoSql = select.getStatement(); selectDescendantsInfoWhatColumns = whatCols;
acquireConnection(); Select select = new Select(table); select.setFrom(table.getQuotedName()); List<Column> whatColumns = new ArrayList<>(2); whatColumns.add(table.getColumn(getPasswordField())); select.setWhat(what); String whereClause = table.getPrimaryColumn().getQuotedName() + " = ?"; whereClause = addFilterWhereClause(whereClause); select.setWhere(whereClause); String sql = select.getStatement();
Select select = new Select(table); select.setWhat(idColumn.getQuotedName()); select.setFrom(table.getQuotedName()); select.setWhere(whereClause); select.setOrderBy(orderBy.toString()); String query = select.getStatement(); if (limit != 0 || offset != 0) { if (!dialect.supportsPaging()) {
acquireConnection(); Select select = new Select(table); select.setWhat(getReadColumnsSQL()); select.setFrom(table.getQuotedName()); select.setWhere(whereClause); select.setOrderBy(orderBy.toString()); String query = select.getStatement(); if (limit != 0 || offset != 0) { if (!dialect.supportsPaging()) { if (countTotal) { Select selectCount = new Select(table); selectCount.setWhat("COUNT(*)"); selectCount.setFrom(table.getQuotedName()); selectCount.setWhere(whereClause); String countQuery = selectCount.getStatement(); if (logger.isLogEnabled()) { List<Serializable> values = builder.params.stream()
Select select = new Select(table); select.setFrom(table.getQuotedName()); select.setWhat(getReadColumnsSQL()); select.setWhere(whereClause); String sql = select.getStatement();
if (queryLimitSize != 0 && (limit <= 0 || limit > queryLimitSize)) { Select select = new Select(table); select.setWhat("count(*)"); select.setFrom(table.getQuotedName()); select.setWhere(where); String countQuery = select.getStatement(); if (logger.isLogEnabled()) { List<Serializable> values = new ArrayList<>(orderedColumns.size()); Select select = new Select(table); select.setWhat(getReadColumnsSQL()); select.setFrom(table.getQuotedName()); select.setWhere(where); select.setOrderBy(orderby.toString()); String query = select.getStatement(); boolean manualLimitOffset; if (limit <= 0) {