@Override public int getTableSize(String tableName) { return sqlInfo.getDatabase().getTable(tableName).getColumns().size(); }
/** * Adds a more general JOIN: * <p> * (LEFT) JOIN fragmentName _F123 ON contextTable.id = _F123.fragmentColumn */ protected Table getFragmentTable(int joinKind, Table contextTable, String contextKey, String fragmentName, String fragmentColumn, int index, boolean skipJoin, String primaryType) { Table table = propertyFragmentTables.get(contextKey); if (table == null) { Table baseTable = database.getTable(fragmentName); String alias = TABLE_FRAG_ALIAS + ++fragJoinCount; table = new TableAlias(baseTable, alias); propertyFragmentTables.put(contextKey, table); if (!skipJoin) { addJoin(joinKind, alias, table, fragmentColumn, contextTable, Model.MAIN_KEY, null, index, primaryType); } } return table; }
/** * 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; } }
/** * 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; }
/** * 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); }
Table table = database.getTable(tableName); Delete delete = new Delete(table); String where = null;
@Override public FulltextMatchInfo getFulltextScoredMatchInfo(String fulltextQuery, String indexName, int nthMatch, Column mainColumn, Model model, Database database) { String phftname = database.getTable(Model.FULLTEXT_TABLE_NAME).getPhysicalName(); String fullIndexName = "PUBLIC_" + phftname + "_" + indexName; String nthSuffix = nthMatch == 1 ? "" : String.valueOf(nthMatch); String tableAlias = "_NXFTTBL" + nthSuffix; String quotedTableAlias = openQuote() + tableAlias + closeQuote(); FulltextMatchInfo info = new FulltextMatchInfo(); info.joins = Collections.singletonList( // new Join(Join.LEFT, // String.format("NXFT_SEARCH('%s', ?)", fullIndexName), tableAlias, // alias fulltextQuery, // param String.format("%s.KEY", quotedTableAlias), // on1 mainColumn.getFullQuotedName() // on2 )); info.whereExpr = String.format("%s.KEY IS NOT NULL", quotedTableAlias); info.scoreExpr = "1"; info.scoreAlias = "_NXSCORE" + nthSuffix; info.scoreCol = new Column(mainColumn.getTable(), null, ColumnType.DOUBLE, null); return info; }
/** * 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); }
Table table = database.getTable(tableName); Update update = new Update(table);
Table hierTable = database.getTable(Model.HIER_TABLE_NAME); Column mainColumn = hierTable.getColumn(Model.MAIN_KEY); List<Column> whatColumns = new ArrayList<>(); Table proxyTable = null; if (proxiesEnabled) { proxyTable = database.getTable(Model.PROXY_TABLE_NAME); whatColumns.add(proxyTable.getColumn(Model.PROXY_TARGET_KEY)); whatColumns.add(proxyTable.getColumn(Model.PROXY_VERSIONABLE_KEY));
Column mainColumn, Model model, Database database) { Table ft = database.getTable(Model.FULLTEXT_TABLE_NAME); Column ftMain = ft.getColumn(Model.MAIN_KEY); String nthSuffix = nthMatch == 1 ? "" : String.valueOf(nthMatch);
public SQLInfoSelect getCopyHier(boolean explicitName, boolean resetVersion) { Table table = database.getTable(Model.HIER_TABLE_NAME); Collection<Column> columns = table.getColumns(); List<String> selectWhats = new ArrayList<>(columns.size());
/** * 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); } }
@Override public FulltextMatchInfo getFulltextScoredMatchInfo(String fulltextQuery, String indexName, int nthMatch, Column mainColumn, Model model, Database database) { // TODO multiple indexes Table ft = database.getTable(model.FULLTEXT_TABLE_NAME); Column ftMain = ft.getColumn(model.MAIN_KEY); Column ftColumn = ft.getColumn(model.FULLTEXT_FULLTEXT_KEY); String nthSuffix = nthMatch == 1 ? "" : String.valueOf(nthMatch); String ftColumnName = ftColumn.getFullQuotedName(); if (ftColumn.getJdbcType() == Types.CLOB) { String colFmt = getClobCast(false); if (colFmt != null) { ftColumnName = String.format(colFmt, ftColumnName, Integer.valueOf(255)); } } FulltextMatchInfo info = new FulltextMatchInfo(); info.joins = new ArrayList<Join>(1); if (nthMatch == 1) { // Need only one JOIN involving the fulltext table info.joins.add(new Join(Join.INNER, ft.getQuotedName(), null, null, ftMain.getFullQuotedName(), mainColumn.getFullQuotedName())); } info.whereExpr = String.format("NX_CONTAINS(%s, ?) = 1", ftColumnName); info.whereExprParam = fulltextQuery; info.scoreExpr = "1"; info.scoreAlias = "NXSCORE" + nthSuffix; info.scoreCol = new Column(mainColumn.getTable(), null, ColumnType.DOUBLE, null); return info; }
@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; }
public SQLInfoSelection(SelectionType selType) { this.type = selType; Table table = database.getTable(selType.tableName); SQLInfoSelect selectAll; SQLInfoSelect selectFiltered; String from = table.getQuotedName(); List<String> clauses; if (selType.tableName.equals(Model.HIER_TABLE_NAME)) { // clause already added by makeSelect clauses = null; } else { 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); String clause = getSoftDeleteClause(); clauses = clause == null ? null : Collections.singletonList(clause); } if (selType.criterionKey == null) { selectAll = makeSelect(table, from, clauses, NO_ORDER_BY, selType.selKey); selectFiltered = makeSelect(table, from, clauses, NO_ORDER_BY, selType.selKey, selType.filterKey); } else { selectAll = makeSelect(table, from, clauses, NO_ORDER_BY, selType.selKey, selType.criterionKey); selectFiltered = makeSelect(table, from, clauses, NO_ORDER_BY, selType.selKey, selType.filterKey, selType.criterionKey); } this.selectAll = selectAll; this.selectFiltered = selectFiltered; }
@Override public FulltextMatchInfo getFulltextScoredMatchInfo(String fulltextQuery, String indexName, int nthMatch, Column mainColumn, Model model, Database database) { String nthSuffix = nthMatch == 1 ? "" : String.valueOf(nthMatch); String indexSuffix = model.getFulltextIndexSuffix(indexName); Table ft = database.getTable(Model.FULLTEXT_TABLE_NAME); Column ftMain = ft.getColumn(Model.MAIN_KEY); Column stColumn = ft.getColumn(Model.FULLTEXT_SIMPLETEXT_KEY + indexSuffix); Column btColumn = ft.getColumn(Model.FULLTEXT_BINARYTEXT_KEY + indexSuffix); String match = String.format("MATCH (%s, %s)", stColumn.getFullQuotedName(), btColumn.getFullQuotedName()); 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("%s AGAINST (? IN BOOLEAN MODE)", match); info.whereExprParam = fulltextQuery; // Note: using the boolean query in non-boolean mode gives approximate // results but it's the best we have as MySQL does not provide a score // in boolean mode. // Note: dividing by 10 is arbitrary, but MySQL cannot really // normalize scores. info.scoreExpr = String.format("(%s AGAINST (?) / 10)", match); info.scoreExprParam = fulltextQuery; info.scoreAlias = "_nxscore" + nthSuffix; info.scoreCol = new Column(mainColumn.getTable(), null, ColumnType.DOUBLE, null); return info; }
protected Column newColumn(String columnName, ColumnType type) { Column column = table.addColumn(columnName, type, columnName, model); if (type == ColumnType.NODEID) { // column.setIdentity(true); if idGenPolicy identity column.setNullable(false); column.setPrimary(true); } if (type == ColumnType.NODEIDFK || type == ColumnType.NODEIDPK) { column.setNullable(false); column.setPrimary(true); } if (type == ColumnType.NODEIDFKMUL) { column.setNullable(false); table.addIndex(columnName); } if (type == ColumnType.NODEIDFK || type == ColumnType.NODEIDFKNP || type == ColumnType.NODEIDFKNULL || type == ColumnType.NODEIDFKMUL) { column.setReferences(database.getTable(Model.HIER_TABLE_NAME), Model.MAIN_KEY); } return column; }