All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.databene.dbsanity.parser.IdentityParser Maven / Gradle / Ivy

Go to download

DB Sanity is a tool for verifying a database's sanity and data integrity.

There is a newer version: 0.9.4
Show newest version
/*
 * (c) Copyright 2010-2011 by Volker Bergmann. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, is permitted under the terms of the
 * GNU General Public License (GPL).
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * WITHOUT A WARRANTY OF ANY KIND. ALL EXPRESS OR IMPLIED CONDITIONS,
 * REPRESENTATIONS AND WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE
 * HEREBY EXCLUDED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

package org.databene.dbsanity.parser;

import java.util.Set;

import org.databene.commons.CollectionUtil;
import org.databene.commons.ConfigurationError;
import org.databene.commons.ObjectNotFoundException;
import org.databene.commons.StringUtil;
import org.databene.commons.version.Versions;
import org.databene.commons.xml.XMLUtil;
import org.databene.dbsanity.model.SanityCheckFile;
import org.databene.dbsanity.model.nk.IdentityCheck;
import org.databene.jdbacl.identity.IdentityModel;
import org.databene.jdbacl.identity.IdentityProvider;
import org.databene.jdbacl.identity.NaturalPkIdentity;
import org.databene.jdbacl.identity.NkPkQueryIdentity;
import org.databene.jdbacl.identity.SubNkPkQueryIdentity;
import org.databene.jdbacl.identity.UniqueKeyIdentity;
import org.databene.jdbacl.model.DBTable;
import org.databene.jdbacl.model.Database;
import org.w3c.dom.Element;

/**
 * Parses an <identity> element in a DB Sanity XML file.

* Created: 05.12.2010 14:39:48 * @since 0.4 * @author Volker Bergmann */ public class IdentityParser extends DbSanityXMLParser { private static final Set REQUIRED_ATTRIBUTES = CollectionUtil.toSet("type", "table"); private static final Set OPTIONAL_ATTRIBUTES = CollectionUtil.toSet("nk-pk-query", "sub-nk-pk-query", "parents", "unique-key", "natural-pk", "columns"); public IdentityParser() { super("identity", REQUIRED_ATTRIBUTES, OPTIONAL_ATTRIBUTES, SanityCheckFile.class); } @Override public Object parse(Element element, Object[] parentPath, DbSanityParseContext context) { String type = getRequiredAttribute("type", element); String tableName; /* Object parent = (SanityCheckFile) parent(parentPath); if (parent instanceof DbsTable) { DbsTable dbsTable = (DbsTable) parent; tableName = dbsTable.getName(); } else */ tableName = getRequiredAttribute("table", element); // if (tableName == null) // throw new ConfigurationError("No table defined for identity: " + element); Database database = context.getDatabase(); DBTable table = null; table = database.getTable(tableName, false); if (table == null) throw new ObjectNotFoundException("Table '" + tableName + "' " + "not found in database '" + database.getName() + "'"); IdentityModel identity; if ("nk-pk-query".equals(type)) identity = parseNkPkQuery(element, table); else if ("sub-nk-pk-query".equals(type)) identity = parseSubNkPkQuery(element, context.getIdentityProvider(), table); else if ("unique-key".equals(type)) identity = parseUniqueKey(element, table); else if ("natural-pk".equals(type)) identity = parseNaturalPk(element, table); else throw new ConfigurationError("Not a supported type: " + type); // TODO v0.x parseUnimportantColumnsOfTable(element, tableConfig, module); // dbsTable.setIdentity(identity); context.getIdentityProvider().registerIdentity(identity, tableName); SanityCheckFile file = ParseUtil.getParentSanityCheckFile(parentPath); Versions affectedVersions = Versions.valueOf(getOptionalAttribute("versions", element)); file.addCheck(new IdentityCheck(createCheckName(tableName, type), identity, affectedVersions, database.getName(), context.getSourceFile(), file.getTempFolder(), file.getReportFolder(), file.getDocPage(), context.getKeyMapper())); return identity; } public static String createCheckName(String tableName, String type) { return tableName + "-identity-" + type; } private IdentityModel parseNkPkQuery(Element element, DBTable table) { NkPkQueryIdentity identity = new NkPkQueryIdentity(table); identity.setNkPkQuery(XMLUtil.getWholeText(element)); return identity; } private IdentityModel parseNaturalPk(Element element, DBTable table) { return new NaturalPkIdentity(table); } private IdentityModel parseUniqueKey(Element element, DBTable table) { String[] columnNames = getRequiredAttribute("columns", element).split(","); columnNames = StringUtil.trimAll(columnNames); UniqueKeyIdentity identity = new UniqueKeyIdentity(table, columnNames); return identity; } private IdentityModel parseSubNkPkQuery(Element element, IdentityProvider identityProvider, DBTable table) { String[] parentTableNames = getRequiredAttribute("parents", element).split(","); SubNkPkQueryIdentity identity = new SubNkPkQueryIdentity(identityProvider, table, parentTableNames); identity.setSubNkPkQuery(XMLUtil.getWholeText(element)); return identity; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy