private void addMissingColumns() { try (Statement stmt = connection.createStatement()) { for (Column column : getMissingColumns(false)) { String alter = table.getAddColumnSql(column); if (logger.isLogEnabled()) { logger.log(alter); } stmt.execute(alter); } } catch (SQLException e) { throw new DirectoryException(String.format("Table '%s' alteration failed: %s", table, e.getMessage()), e); } }
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); } }
@Override public Collection<Column> getColumns() { Collection<Column> columns = table.getColumns(); List<Column> result = new ArrayList<Column>(columns.size()); for (Column column : columns) { result.add(new Column(column, this)); } return result; }
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())); }
@Override public List<String> getCustomPostCreateSqls(Table table, Model model) { if (!needsClusteredColumn(table)) { return Collections.emptyList(); } String quotedIndexName = getIndexName(table.getKey(), Collections.singletonList(CLUSTER_INDEX_COL)); String sql = String.format("CREATE UNIQUE CLUSTERED INDEX [%s] ON %s ([%s])", quotedIndexName, table.getQuotedName(), CLUSTER_INDEX_COL); return Collections.singletonList(sql); }
public String getStatement() { StringBuilder buf = new StringBuilder(128); buf.append("UPDATE "); buf.append(table.getQuotedName()); buf.append(" SET "); buf.append(newValues); if (from != null) { buf.append(" FROM "); if (table.getDialect().doesUpdateFromRepeatSelf()) { buf.append(table.getQuotedName()); buf.append(", "); } buf.append(StringUtils.join(from, ", ")); } if (where != null) { buf.append(" WHERE "); buf.append(where); } else { throw new IllegalArgumentException("unexpected empty WHERE"); } return buf.toString(); } }
String tableName = getTableName(table.getPhysicalName()); if (!tableNames.contains(tableName.toUpperCase())) { ddlCollector.add(table.getCreateSql()); ddlCollector.addAll(table.getPostCreateSqls(model)); added.put(table.getKey(), null); // null = table created for (Column column : table.getColumns()) { String upperName = column.getPhysicalName().toUpperCase(); Integer type = columnTypes.remove(upperName); if (type == null) { log.warn("Adding missing column in database: " + column.getFullQuotedName()); ddlCollector.add(table.getAddColumnSql(column)); ddlCollector.addAll(table.getPostAddSqls(column, model)); addedColumns.add(column); } else { log.warn("Database contains additional unused columns for table " + table.getQuotedName() + ": " + String.join(", ", columnTypes.keySet())); if (added.containsKey(table.getKey())) { throw new AssertionError(); added.put(table.getKey(), addedColumns);
List<String> whats = new LinkedList<>(); List<Column> opaqueColumns = new LinkedList<>(); for (Column column : table.getColumns()) { if (column.isOpaque()) { opaqueColumns.add(column); Column whereColumn = table.getColumn(Model.MAIN_KEY); StringBuilder wherebuf = new StringBuilder(whereColumn.getQuotedName()); wherebuf.append(" IN ("); 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<>(); for (String orderBy : orderBys) { orders.add(table.getColumn(orderBy).getQuotedName());
@Override public boolean isMultiTenant() { return table.getColumn(TENANT_ID_FIELD) != null; }
List<String> whats = new LinkedList<>(); List<String> wheres = new LinkedList<>(); for (Column column : table.getColumns()) { String qname = fullQuotedName ? column.getFullQuotedName() : column.getQuotedName(); if (freeColumnsList.contains(column.getKey())) { whats.add(table.getColumn(Model.MAIN_KEY).getQuotedName()); select.setWhat(String.join(", ", whats)); if (from == null) { from = table.getQuotedName(); String where = String.join(" AND ", wheres) + getSoftDeleteClause(table.getKey()); select.setWhere(where); List<String> orders = new LinkedList<>(); String name = orderBys[i++]; String ascdesc = orderBys[i].equals(ORDER_DESC) ? " " + ORDER_DESC : ""; Column col = table.getColumn(name); String qcol = fullQuotedName ? col.getFullQuotedName() : col.getQuotedName(); orders.add(qcol + ascdesc);
@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()); } }
select.setFrom(table.getQuotedName()); List<Column> whatColumns = new ArrayList<>(2); whatColumns.add(table.getColumn(getPasswordField())); if (isMultiTenant()) { whatColumns.add(table.getColumn(TENANT_ID_FIELD)); String whereClause = table.getPrimaryColumn().getQuotedName() + " = ?"; whereClause = addFilterWhereClause(whereClause); select.setWhere(whereClause); setFieldValue(ps, 1, table.getPrimaryColumn(), id); addFilterValues(ps, 2); try (ResultSet rs = ps.executeQuery()) { String entryTenantId = (String) getFieldValue(rs, table.getColumn(TENANT_ID_FIELD)); if (!StringUtils.isBlank(entryTenantId)) { if (!entryTenantId.equals(tenantId)) { String password = (String) getFieldValue(rs, table.getColumn(getPasswordField())); if (logger.isLogEnabled()) { String value = HIDE_PASSWORD_IN_LOGS ? "********" : password;
Column fc = ft.getColumn(column.getForeignKey()); String constraintName = dialect.openQuote() + dialect.getForeignKeyConstraintName(key, column.getPhysicalName(), ft.getPhysicalName()) + dialect.closeQuote(); StringBuilder buf = new StringBuilder(); buf.append(getQuotedName()); buf.append(dialect.getAddForeignKeyConstraintString(constraintName, new String[] { column.getQuotedName() }, ft.getQuotedName(), new String[] { fc.getQuotedName() }, true)); if (dialect.supportsCircularCascadeDeleteConstraints()
hierTable.addIndex(Model.MAIN_IS_VERSION_KEY); versionTable.addIndex(Model.VERSION_VERSIONABLE_KEY); proxyTable.addIndex(Model.PROXY_VERSIONABLE_KEY); proxyTable.addIndex(Model.PROXY_TARGET_KEY); int ftic = dialect.getFulltextIndexedColumns(); if (ftic == 1) { table.addIndex(indexName, IndexType.FULLTEXT, Model.FULLTEXT_FULLTEXT_KEY + suffix); } else if (ftic == 2) { table.addIndex(indexName, IndexType.FULLTEXT, Model.FULLTEXT_SIMPLETEXT_KEY + suffix, Model.FULLTEXT_BINARYTEXT_KEY + suffix); for (String key : e.getValue()) { Select select = new Select(table); Column col = table.getColumn(key); // key = name for now select.setWhat("DISTINCT " + col.getQuotedName()); select.setFrom(table.getQuotedName()); getBinariesSql.add(select.getStatement());
private void createTable(boolean drop) { try (Statement stmt = connection.createStatement()) { if (drop) { // drop table String dropSql = table.getDropSql(); if (logger.isLogEnabled()) { logger.log(dropSql); } stmt.execute(dropSql); } String createSql = table.getCreateSql(); if (logger.isLogEnabled()) { logger.log(createSql); } stmt.execute(createSql); for (String sql : table.getPostCreateSqls(null)) { if (logger.isLogEnabled()) { logger.log(sql); } stmt.execute(sql); } } catch (SQLException e) { throw new DirectoryException(String.format("Table '%s' creation failed: %s", table, e.getMessage()), e); } }
public SQLHelper(Connection connection, Table table, String policy) { this.table = table; this.connection = connection; this.policy = policy; tableName = table.getPhysicalName(); }
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; }
@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); } }
List<Serializable> values = new ArrayList<>(1 + map.size()); whereClause.append(table.getPrimaryColumn().getQuotedName()); whereClause.append(" = ?"); values.add(id); String value = e.getValue(); whereClause.append(" AND "); Column col = table.getColumn(key); if (col == null) { throw new IllegalArgumentException("Unknown column " + key); for (int i = 0; i < values.size(); i++) { if (i == 0) { setFieldValue(ps, 1, table.getPrimaryColumn(), values.get(i)); } else { ps.setString(1 + i, (String) values.get(i));
@Override public List<String> getCustomPostCreateSqls(Table table, Model model) { List<String> sqls = new ArrayList<>(); String key = table.getKey(); if (childNameUniqueConstraintEnabled && key.equals(Model.HIER_TABLE_NAME)) { } else if (collectionUniqueConstraintEnabled && model.isCollectionFragment(key)) { String name = table.getPhysicalName(); String indexName = makeName(name, "", "_unique_pos", getMaxIndexNameSize()); sqls.add(String.format("CREATE UNIQUE INDEX \"%s\" ON \"%s\" (\"%s\", \"%s\")", indexName, name,