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); }
wherebuf.append(getSoftDeleteClause(tableName)); Select select = new Select(table); select.setWhat(String.join(", ", whats)); select.setFrom(table.getQuotedName()); select.setWhere(wherebuf.toString());
select.setWhat(String.join(", ", whats)); if (from == null) { from = table.getQuotedName();
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); } } }
Select select = new Select(null); select.setFrom(table.getQuotedName()); select.setWhat(String.join(", ", selectWhats)); select.setWhere(whereColumn.getQuotedName() + " = ?"); insert.setValues(select.getStatement());
select.setWhat(selectWhats); String from = hierTable.getQuotedName(); if (proxiesEnabled) {
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 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(); }
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 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.setWhat(idColumn.getQuotedName()); select.setFrom(table.getQuotedName()); select.setWhere(whereClause);
select.setWhat(getReadColumnsSQL());
select.setWhat(what); String whereClause = table.getPrimaryColumn().getQuotedName() + " = ?"; whereClause = addFilterWhereClause(whereClause);
select.setWhat(whats); String from = hierTable.getQuotedName(); if (proxiesEnabled) {
@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); } }
withTables.add(withTable); Select withSelect = new Select(null); withSelect.setWhat("*"); String withFrom = withTable; for (Join j : securityJoins) { select.setWhat(selectWhat); selectParams.addAll(whatNamesParams); selectWhat = "DISTINCT " + selectWhat; select.setWhat(selectWhat); select.setWith(with); Select withSelect = withSelects.get(0); select.setWhat(withSelect.getWhat()); select.setFrom(withSelect.getFrom()); select.setWhere(withSelect.getWhere());
Select select = new Select(table); select.setWhat("DISTINCT " + col.getQuotedName()); select.setFrom(table.getQuotedName()); getBinariesSql.add(select.getStatement());