@Override public boolean isMultiTenant() { return table.getColumn(TENANT_ID_FIELD) != null; }
protected void postProcessClusterNodes() { Collection<Column> columns = table.getColumns(); Insert insert = new Insert(table); for (Column column : columns) { insert.addColumn(column); } createClusterNodeSql = insert.getStatement(); createClusterNodeColumns = new ArrayList<>(columns); Delete delete = new Delete(table); Column column = table.getColumn(Model.CLUSTER_NODES_NODEID_KEY); delete.setWhere(column.getQuotedName() + " = ?"); deleteClusterNodeSql = delete.getStatement(); deleteClusterNodeColumn = column; }
public Column getDirectoryColumn(Table table, boolean nativeCase) { return table.getColumn(column); }
protected void postProcessClusterInvalidations() { clusterInvalidationsColumns = Arrays.asList(table.getColumn(Model.CLUSTER_INVALS_NODEID_KEY), table.getColumn(Model.CLUSTER_INVALS_ID_KEY), table.getColumn(Model.CLUSTER_INVALS_FRAGMENTS_KEY), table.getColumn(Model.CLUSTER_INVALS_KIND_KEY)); Delete delete = new Delete(table); Column column = table.getColumn(Model.CLUSTER_INVALS_NODEID_KEY); delete.setWhere(column.getQuotedName() + " = ?"); deleteClusterInvalsSql = delete.getStatement(); deleteClusterInvalsColumn = column; }
@Override public Column getColumn(String name) { return new Column(table.getColumn(name), this); }
public void removeLinksFor(String column, String entryId, SQLSession session) { String sql = String.format("DELETE FROM %s WHERE %s = ?", table.getQuotedName(), table.getColumn(column) .getQuotedName()); if (session.logger.isLogEnabled()) { session.logger.logSQL(sql, Collections.<Serializable> singleton(entryId)); } try (PreparedStatement ps = session.sqlConnection.prepareStatement(sql)) { ps.setString(1, entryId); ps.execute(); } catch (SQLException e) { throw new DirectoryException("error remove links to " + entryId, e); } }
protected void addJoin(int kind, String alias, Table table, String column, Table contextTable, String contextColumn, String name, int index, String primaryType) { Column column1 = contextTable.getColumn(contextColumn); Column column2 = table.getColumn(column); Join join = new Join(kind, table.getRealTable().getQuotedName(), alias, null, column1, column2); if (name != null) { String nameCol = table.getColumn(Model.HIER_CHILD_NAME_KEY).getFullQuotedName(); join.addWhereClause(nameCol + " = ?", name); } if (index != -1) { String posCol = table.getColumn(Model.HIER_CHILD_POS_KEY).getFullQuotedName(); join.addWhereClause(posCol + " = ?", Long.valueOf(index)); } if (primaryType != null) { String typeCol = table.getColumn(Model.MAIN_PRIMARY_TYPE_KEY).getFullQuotedName(); join.addWhereClause(typeCol + " = ?", primaryType); } joins.add(join); }
protected void visitExpressionStartsWithPath(String path) { // find the id from the path Serializable id = pathResolver.getIdForPath(path); if (id == null) { // no such path, always return a false // TODO remove the expression more intelligently from the parse // tree buf.append("0=1"); } else { // id is always valid, no need to pass it as argument to getInTreeSql buf.append(dialect.getInTreeSql(hierTable.getColumn(Model.MAIN_KEY).getFullQuotedName(), null)); whereParams.add(id); } }
/** * Returns null or {@code AND isdeleted IS NULL} if soft delete is activated. * * @return the clause, or null */ public String getSoftDeleteClause() { if (softDeleteEnabled) { return database.getTable(Model.HIER_TABLE_NAME).getColumn(Model.MAIN_IS_DELETED_KEY).getFullQuotedName() + " IS NULL"; } else { return null; } }
protected void generateExistsStart(StringBuilder buf, Table table) { String tableName; if (table.isAlias()) { tableName = table.getRealTable().getQuotedName() + " " + table.getQuotedName(); } else { tableName = table.getQuotedName(); } buf.append(String.format("EXISTS (SELECT 1 FROM %s WHERE %s = %s AND ", tableName, dataHierTable.getColumn(Model.MAIN_KEY).getFullQuotedName(), table.getColumn(Model.MAIN_KEY).getFullQuotedName())); }
protected Column getColumn(String name) { Column column = directory.getTable().getColumn(name); if (column == null) { throw new QueryParseException("No column: " + name + " for directory: " + directory.getName()); } return column; }
/** * Returns the clause used to match a given row by id in the given table. * <p> * Takes into account soft deletes. * * @param tableName the table name * @return the clause, like {@code table.id = ?} */ public String getIdEqualsClause(String tableName) { return database.getTable(tableName).getColumn(Model.MAIN_KEY).getQuotedName() + " = ?" + getSoftDeleteClause(tableName); }
public Update getUpdateByIdForKeys(String tableName, List<String> keys) { Table table = database.getTable(tableName); List<Column> columns = new LinkedList<>(); for (String key : keys) { columns.add(table.getColumn(key)); } Update update = new Update(table); update.setUpdatedColumns(columns); update.setWhere(getIdEqualsClause(tableName)); return update; }
@Override public Column getSubQueryIdColumn() { Column column = fakeSubqueryTableAlias.getColumn(Model.MAIN_KEY); return new ArraySubQueryPostgreSQLColumn(column.getPhysicalName(), column.getType()); }
/** * Select all ancestors ids for several fragments. * <p> * Fast alternative to the slowest iterative {@link #getSelectParentIds}. * * @return null if it's not possible in one call in this dialect */ public SQLInfoSelect getSelectAncestorsIds() { String sql = dialect.getAncestorsIdsSql(); if (sql == null) { return null; } Table table = database.getTable(Model.HIER_TABLE_NAME); Column mainColumn = table.getColumn(Model.MAIN_KEY); // no soft-delete check needed, as ancestors of a non-deleted doc // aren't deleted either return new SQLInfoSelect(sql, Collections.singletonList(mainColumn), null, null); }
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); } }
protected String getArrayOpSql(Column arrayColumn, String refName, boolean positive, Table dataHierTable, String op) { Table table = arrayColumn.getTable(); String tableAliasName = openQuote() + getTableName(refName) + closeQuote(); String sql = String.format("EXISTS (SELECT 1 FROM %s AS %s WHERE %s = %s AND %s %s ?)", getArraySubQuery(arrayColumn, tableAliasName).toSql(), tableAliasName, dataHierTable.getColumn(Model.MAIN_KEY).getFullQuotedName(), tableAliasName + '.' + table.getColumn(Model.MAIN_KEY).getQuotedName(), tableAliasName + '.' + Model.COLL_TABLE_VALUE_KEY, op); if (!positive) { sql = "NOT(" + sql + ")"; } return sql; }
/** * Gets the arraySubquery for the given arrayColumn in the given contextKey, and maybe adds a JOIN if one is not * already done. * <p> * LEFT JOIN (SELECT id, UNNEST(somecol) AS item, generate_subscripts(somecol, 1) AS pos FROM someschema) _A1 ON * _A1.id = hierarchy.id */ protected ArraySubQuery getArraySubQuery(Table contextHier, String contextKey, Column arrayColumn, boolean skipJoin) { ArraySubQuery arraySubQuery = propertyArraySubQueries.get(contextKey); if (arraySubQuery == null) { String alias = SUBQUERY_ARRAY_ALIAS + ++arraySubQueryJoinCount; arraySubQuery = dialect.getArraySubQuery(arrayColumn, alias); propertyArraySubQueries.put(contextKey, arraySubQuery); if (!skipJoin) { Join join = new Join(Join.LEFT, arraySubQuery.toSql(), alias, null, arraySubQuery.getSubQueryIdColumn().getFullQuotedName(), contextHier.getColumn(Model.MAIN_KEY).getFullQuotedName()); joins.add(join); } } return arraySubQuery; }
@Override public String toSql() { Table table = arrayColumn.getTable(); return String.format("(SELECT %s, UNNEST(%s) AS %s, generate_subscripts(%s, 1) AS %s FROM %s) ", table.getColumn(Model.MAIN_KEY).getQuotedName(), arrayColumn.getQuotedName(), Model.COLL_TABLE_VALUE_KEY, arrayColumn.getQuotedName(), Model.COLL_TABLE_POS_KEY, table.getRealTable().getQuotedName()); } }
@Override public FulltextMatchInfo getFulltextScoredMatchInfo(String fulltextQuery, String indexName, int nthMatch, Column mainColumn, Model model, Database database) { String indexSuffix = model.getFulltextIndexSuffix(indexName); Table ft = database.getTable(Model.FULLTEXT_TABLE_NAME); Column ftMain = ft.getColumn(Model.MAIN_KEY); Column ftColumn = ft.getColumn(Model.FULLTEXT_FULLTEXT_KEY + indexSuffix); String score = String.format("SCORE(%d)", nthMatch); String nthSuffix = nthMatch == 1 ? "" : String.valueOf(nthMatch); FulltextMatchInfo info = new FulltextMatchInfo(); if (nthMatch == 1) { // Need only one JOIN involving the fulltext table info.joins = Collections.singletonList(new Join(Join.INNER, ft.getQuotedName(), null, null, ftMain.getFullQuotedName(), mainColumn.getFullQuotedName())); } info.whereExpr = String.format("CONTAINS(%s, ?, %d) > 0", ftColumn.getFullQuotedName(), nthMatch); info.whereExprParam = fulltextQuery; info.scoreExpr = String.format("(%s / 100)", score); info.scoreAlias = openQuote() + "_nxscore" + nthSuffix + closeQuote(); info.scoreCol = new Column(mainColumn.getTable(), null, ColumnType.DOUBLE, null); return info; }