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

com.aoindustries.aoserv.client.SchemaForeignKeyTable Maven / Gradle / Ivy

There is a newer version: 1.92.0
Show newest version
/*
 * aoserv-client - Java client for the AOServ platform.
 * Copyright (C) 2001-2009, 2016  AO Industries, Inc.
 *     [email protected]
 *     7262 Bull Pen Cir
 *     Mobile, AL 36695
 *
 * This file is part of aoserv-client.
 *
 * aoserv-client is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * aoserv-client is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with aoserv-client.  If not, see .
 */
package com.aoindustries.aoserv.client;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @see  SchemaForeignKey
 *
 * @author  AO Industries, Inc.
 */
final public class SchemaForeignKeyTable extends GlobalTableIntegerKey {

	private static final Map> tableKeys=new HashMap<>();
	private static final Map> referencesHash=new HashMap<>();
	private static final Map> referencedByHash=new HashMap<>();

	SchemaForeignKeyTable(AOServConnector connector) {
		super(connector, SchemaForeignKey.class);
	}

	private static final OrderBy[] defaultOrderBy = {
		new OrderBy(SchemaForeignKey.COLUMN_PKEY_name, ASCENDING)
	};
	@Override
	OrderBy[] getDefaultOrderBy() {
		return defaultOrderBy;
	}

	@Override
	public void clearCache() {
		super.clearCache();
		synchronized(SchemaForeignKeyTable.class) {
			tableKeys.clear();
			referencesHash.clear();
			referencedByHash.clear();
		}
	}

	@Override
	public SchemaForeignKey get(int pkey) throws IOException, SQLException {
		return getUniqueRow(SchemaForeignKey.COLUMN_PKEY, pkey);
	}

	List getSchemaForeignKeys(SchemaTable table) throws IOException, SQLException {
		synchronized(SchemaForeignKeyTable.class) {
			if(tableKeys.isEmpty()) {
				List cached=getRows();
				int size=cached.size();
				for(int c=0;c keys=tableKeys.get(tableName);
					if(keys==null) tableKeys.put(tableName, keys=new ArrayList<>());
					keys.add(key);
				}
			}
			List matches=tableKeys.get(table.getName());
			if(matches!=null) return matches;
			return Collections.emptyList();
		}
	}

	private void rebuildReferenceHashes() throws IOException, SQLException {
		if(
			referencedByHash.isEmpty()
			|| referencesHash.isEmpty()
		) {
			// All methods that call this are already synched
			List cached=getRows();
			int size=cached.size();
			for(int c=0;c referencedBy=referencedByHash.get(keyColumnPKey);
				if(referencedBy==null) referencedByHash.put(keyColumnPKey, referencedBy=new ArrayList<>());
				referencedBy.add(key);

				// References
				List references=referencesHash.get(foreignColumnPKey);
				if(references==null) referencesHash.put(foreignColumnPKey, references=new ArrayList<>());
				references.add(key);
			}
		}
	}

	List getSchemaForeignKeysReferencedBy(SchemaColumn column) throws IOException, SQLException {
		synchronized(SchemaForeignKeyTable.class) {
			rebuildReferenceHashes();
			List matches=referencedByHash.get(column.getPkey());
			if(matches!=null) return matches;
			else return Collections.emptyList();
		}
	}

	List getSchemaForeignKeysReferencing(SchemaColumn column) throws IOException, SQLException {
		synchronized(SchemaForeignKeyTable.class) {
			rebuildReferenceHashes();
			List matches=referencesHash.get(column.getPkey());
			if(matches!=null) return matches;
			else return Collections.emptyList();
		}
	}

	@Override
	public SchemaTable.TableID getTableID() {
		return SchemaTable.TableID.SCHEMA_FOREIGN_KEYS;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy