Package org.voltdb.catalog

Examples of org.voltdb.catalog.Constraint


     */
    public static Collection<Constraint> getConstraints(Iterable<ConstraintRef> map) {
        List<Constraint> ret = new ArrayList<Constraint>();
        if (map != null) {
            for (ConstraintRef ref : map) {
                Constraint catalog_item = ref.getConstraint();
                assert(catalog_item != null);
                ret.add(catalog_item);
            }
        }
        return (ret);
View Full Code Here


     * @throws Exception if the table does not define a primary key
     */
    public static Index getPrimaryKeyIndex(Table catalogTable) throws Exception {

        // We first need to find the pkey constraint
        Constraint catalog_constraint = null;
        for (Constraint c : catalogTable.getConstraints()) {
            if (c.getType() == ConstraintType.PRIMARY_KEY.getValue()) {
                catalog_constraint = c;
                break;
            }
        }
        if (catalog_constraint == null) {
            throw new Exception("ERROR: Table '" + catalogTable.getTypeName() + "' does not have a PRIMARY KEY constraint");
        }

        // And then grab the index that it is using
        return (catalog_constraint.getIndex());
    }
View Full Code Here

            }
            if (isNullable == false) ret += " NOT NULL";

            // Single-column constraints
            for (ConstraintRef catalog_const_ref : catalog_col.getConstraints()) {
                Constraint catalog_const = catalog_const_ref.getConstraint();
                ConstraintType const_type = ConstraintType.get(catalog_const.getType());
                if (const_type == ConstraintType.FOREIGN_KEY && include_fkeys == false) continue;

                // Check if there is another column in our table with the same constraint
                // If there is, then we need to add it to the end of the table definition
                boolean found = false;
                for (Column catalog_other_col : catalog_tbl.getColumns()) {
                    if (catalog_other_col.equals(catalog_col)) continue;
                    if (catalog_other_col.getConstraints().getIgnoreCase(catalog_const.getTypeName()) != null) {
                        found = true;
                        break;
                    }
                }
                if (!found) {
                    switch (const_type) {
                        case FOREIGN_KEY: {
                            Table catalog_fkey_tbl = catalog_const.getForeignkeytable();
                            Column catalog_fkey_col = null;
                            for (ColumnRef ref : catalog_const.getForeignkeycols()) {
                                catalog_fkey_col = ref.getColumn();
                                break; // Nasty hack to get first item
                            }

                            assert(catalog_fkey_col != null);
                            ret += " REFERENCES " + catalog_fkey_tbl.getTypeName() + " (" + catalog_fkey_col.getTypeName() + ")";
                            skip_constraints.add(catalog_const);
                            break;
                        }
                        default:
                            // Nothing for now
                    }
                }
            }

            add = ",\n";
        }

        // Constraints
        for (Constraint catalog_const : catalog_tbl.getConstraints()) {
            if (skip_constraints.contains(catalog_const)) continue;
            ConstraintType const_type = ConstraintType.get(catalog_const.getType());
            if (const_type == ConstraintType.FOREIGN_KEY && include_fkeys == false) continue;

            // Primary Keys / Unique Constraints
            if (const_type == ConstraintType.PRIMARY_KEY || const_type == ConstraintType.UNIQUE) {
                Index catalog_idx = catalog_const.getIndex();
                IndexType idx_type = IndexType.get(catalog_idx.getType());
                String idx_suffix = idx_type.getSQLSuffix();

                ret += add + spacer +
                       (!idx_suffix.isEmpty() ? "CONSTRAINT " + catalog_const.getTypeName() + " " : "") +
                       (const_type == ConstraintType.PRIMARY_KEY ? "PRIMARY KEY" : "UNIQUE") + " (";

                String col_add = "";
                for (ColumnRef catalog_colref : CatalogUtil.getSortedCatalogItems(catalog_idx.getColumns(), "index")) {
                    ret += col_add + catalog_colref.getColumn().getTypeName();
                    col_add = ", ";
                } // FOR
                ret += ")";
                skip_indexes.add(catalog_idx);

            // Foreign Key
            } else if (const_type == ConstraintType.FOREIGN_KEY) {
                Table catalog_fkey_tbl = catalog_const.getForeignkeytable();
                String col_add = "";
                String our_columns = "";
                String fkey_columns = "";
                for (ColumnRef catalog_colref : catalog_const.getForeignkeycols()) {
                    // The name of the ColumnRef is the column in our base table
                    Column our_column = catalog_tbl.getColumns().getIgnoreCase(catalog_colref.getTypeName());
                    assert(our_column != null);
                    our_columns += col_add + our_column.getTypeName();

                    Column fkey_column = catalog_colref.getColumn();
                    assert(fkey_column != null);
                    fkey_columns += col_add + fkey_column.getTypeName();

                    col_add = ", ";
                }
                ret += add + spacer + "CONSTRAINT " + catalog_const.getTypeName() + " " +
                                      "FOREIGN KEY (" + our_columns + ") " +
                                      "REFERENCES " + catalog_fkey_tbl.getTypeName() + " (" + fkey_columns + ")";
            }
            skip_constraints.add(catalog_const);
        }
View Full Code Here

      Column column = CatalogUtil.getColumn(db, tableName, colName);
      CatalogMap<ConstraintRef> constraintMap = column.getConstraints();

      if(!constraintMap.isEmpty()){
        String foreignKey = null;
        Constraint constraint = CollectionUtil.first(constraintMap).getConstraint();
        Column foreignKeyCol = CollectionUtil.first(constraint.getForeignkeycols()).getColumn();
        foreignKey = foreignKeyCol.fullName();       
        if(foreignKey!=null){
          Map<String, String> foreignKeyValueMap = new HashMap<String, String>();
          foreignKeyValueMap.put(foreignKey, value);
          ArrayList<Long> timestamps;
View Full Code Here

                throw new Exception("ERROR: The foreign key table for '" + fkey[0] + "." + fkey[1] + "' is null");
            } else if (catalog_fkey_col == null) {
                throw new Exception("ERROR: The foreign key column for '" + fkey[0] + "." + fkey[1] + "' is null");
            }

            Constraint catalog_const = null;
            if (table_const_map.containsKey(catalog_fkey_tbl)) {
                catalog_const = table_const_map.get(catalog_fkey_tbl);
            } else {
                String fkey_name = "SYS_FK_" + FKEY_COUNTER++;
                catalog_const = catalog_tbl.getConstraints().get(fkey_name);
                if (catalog_const == null) {
                    catalog_const = catalog_tbl.getConstraints().add(fkey_name);
                    catalog_const.setType(ConstraintType.FOREIGN_KEY.getValue());
                    catalog_const.setForeignkeytable(catalog_fkey_tbl);
                } else {
                    LOG.info("Foreign Key '" + fkey_name + "' already exists for table '" + catalog_tbl.getName() + "'. Skipping...");
                    continue;
                }
            }

            //
            // Add the parent ColumnRef to the Constraint
            // The name of the column ref is the name of the column in this
            // table, which then points
            // to the column in the foreign table
            //
            ColumnRef catalog_fkey_col_ref = catalog_const.getForeignkeycols().add(catalog_col.getName());
            catalog_fkey_col_ref.setColumn(catalog_fkey_col);

            //
            // Add the ConstraintRef to the child Column
            //
            ConstraintRef catalog_const_ref = catalog_col.getConstraints().add(catalog_const.getName());
            catalog_const_ref.setConstraint(catalog_const);

            LOG.debug("Added foreign key constraint from '" + catalog_tbl.getName() + "." + column + "' to '" + fkey[0] + "." + fkey[1] + "'");
            table_const_map.put(catalog_fkey_tbl, catalog_const);
        } // FOR
View Full Code Here

                // it to the ColumnSet
                Collection<Constraint> catalog_consts = CatalogUtil.getConstraints(catalog_col.getConstraints());
                catalog_consts = CatalogUtil.findAll(catalog_consts, "type", ConstraintType.FOREIGN_KEY.getValue());
                if (!catalog_consts.isEmpty()) {
                    assert (catalog_consts.size() == 1) : CatalogUtil.getDisplayName(catalog_col) + " has " + catalog_consts.size() + " foreign key constraints: " + catalog_consts;
                    Constraint catalog_const = CollectionUtil.first(catalog_consts);
                    Table catalog_fkey_tbl = catalog_const.getForeignkeytable();
                    assert (catalog_fkey_tbl != null);

                    // Important! We only want to include tables that are
                    // actually referenced in this procedure
                    if (proc_tables.contains(catalog_fkey_tbl)) {
                        Column catalog_fkey_col = CollectionUtil.first(catalog_const.getForeignkeycols()).getColumn();
                        assert (catalog_fkey_col != null);

                        // TODO: Use real ExpressionType from the entry
                        if (!fkey_column_xrefs.containsKey(catalog_fkey_tbl)) {
                            fkey_column_xrefs.put(catalog_fkey_tbl, new PredicatePairs());
View Full Code Here

            // For each vertex, get the list of foreign key references to point
            // to it and
            // make a new edge between the parent and child
            DesignerVertex vertex = graph.getVertex(catalog_table);
            for (int ctr = 0, cnt = fkey_ref_consts.get(vertex).size(); ctr < cnt; ctr++) {
                Constraint catalog_const = fkey_ref_consts.get(vertex).get(ctr);
                Column catalog_col = fkey_ref_cols.get(vertex).get(ctr);

                //
                // Grab the table object used in this foreign key constraint
                // We then get the vertex that we're using to represent it
                //
                Table catalog_fkey_table = catalog_const.getForeignkeytable();
                DesignerVertex other_vertex = graph.getVertex(catalog_fkey_table);
                if (other_vertex == null) {
                    throw new Exception("ERROR: The constraint '" + catalog_const + "' on '" + vertex + "' uses an unknown table '" + catalog_fkey_table.getName() + "'");
                }
                PredicatePairs cset = new PredicatePairs();
View Full Code Here

            updateColumnsRefs((Table) src_idx.getParent(), src_idx.getColumns(), (Table) dest_idx.getParent(), dest_idx.getColumns());

        // Constraint
        } else if (src_item instanceof Constraint) {
            // ColumnRefs
            Constraint src_cons = (Constraint) src_item;
            Constraint dest_cons = (Constraint) clone;

            Table src_fkey_tbl = src_cons.getForeignkeytable();
            if (src_fkey_tbl != null) {
                Database dest_db = (Database) dest_cons.getParent().getParent();
                Table dest_fkey_tbl = dest_db.getTables().get(src_fkey_tbl.getName());
                if (dest_fkey_tbl != null) {
                    dest_cons.setForeignkeytable(dest_fkey_tbl);
                    for (ColumnRef src_cref : ((Constraint) src_item).getForeignkeycols()) {
                        CatalogCloner.clone(src_cref, dest_catalog);

                        // Correct what it's pointing to
                        ColumnRef dest_colref = dest_cons.getForeignkeycols().get(src_cref.getName());
                        assert (dest_colref != null);
                        dest_colref.setColumn(dest_fkey_tbl.getColumns().get(src_cref.getColumn().getName()));
                    } // FOR
                }
            }

            // Important: We have to add ConstraintRefs to Columns *after* we add the columns
            Table src_tbl = (Table) src_cons.getParent();
            Table dest_tbl = (Table) dest_cons.getParent();
            for (Column src_col : src_tbl.getColumns()) {
                Column dest_col = dest_tbl.getColumns().get(src_col.getName());
                assert (dest_col != null);
                for (ConstraintRef src_conref : src_col.getConstraints()) {
                    if (!src_conref.getConstraint().equals(src_cons))
                        continue;
                    CatalogCloner.clone(src_conref, dest_catalog);

                    // Correct what it's pointing to
                    ConstraintRef dest_conref = dest_col.getConstraints().get(src_conref.getName());
                    assert (dest_conref != null);
                    // System.out.println("dest_tbl: " + dest_tbl);
                    // System.out.println("dest_tbl.getConstraints(): " +
                    // CatalogUtil.debug(dest_tbl.getConstraints()));
                    // System.out.println("src_confref: " + src_conref);
                    // System.out.println("src_confref.getConstraint(): " +
                    // src_conref.getConstraint());
                    dest_conref.setConstraint(dest_tbl.getConstraints().get(src_conref.getConstraint().getName()));
                } // FOR
            } // FOR

            Index src_index = src_cons.getIndex();
            if (src_index != null) {
                Index dest_index = dest_tbl.getIndexes().get(src_index.getName());
                dest_cons.setIndex(dest_index);
            }

        // StmtParameter
        } else if (src_item instanceof StmtParameter) {
            // We need to fix the reference to the ProcParameter (if one exists)
View Full Code Here

            if (dest_tbl != null) {
                for (Constraint src_cons : src_tbl.getConstraints()) {
                    // Only clone FKEY constraint if the other table is in the catalog
                    ConstraintType cons_type = ConstraintType.get(src_cons.getType());
                    if (cons_type != ConstraintType.FOREIGN_KEY || (cons_type == ConstraintType.FOREIGN_KEY && dest_db.getTables().get(src_cons.getForeignkeytable().getName()) != null)) {
                        Constraint dest_cons = clone(src_cons, dest_catalog);
                        assert (dest_cons != null);
                    }
                } // FOR
            }
        } // FOR
View Full Code Here

            Collection<Column> cols = CatalogUtil.getColumns(CatalogUtil.getSortedCatalogItems(catalog_idx.getColumns(), "index"));
            m[0].put("columns", CatalogUtil.getDisplayNames(cols));
        }
        // CONSTRAINT
        else if (catalog_obj instanceof Constraint) {
            Constraint catalog_const = (Constraint)catalog_obj;
            Collection<Column> cols = null;
            if (catalog_const.getType() == ConstraintType.FOREIGN_KEY.getValue()) {
                cols = CatalogUtil.getColumns(catalog_const.getForeignkeycols());   
            } else {
                Index catalog_idx = catalog_const.getIndex();
                cols = CatalogUtil.getColumns(catalog_idx.getColumns());
            }
            m[0].put("columns", CatalogUtil.getDisplayNames(cols));
        }
        // COLUMN
View Full Code Here

TOP

Related Classes of org.voltdb.catalog.Constraint

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.