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

com.aoindustries.aoserv.client.SchemaColumn 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-2013, 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 com.aoindustries.io.CompressedDataInputStream;
import com.aoindustries.io.CompressedDataOutputStream;
import com.aoindustries.util.InternUtils;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

/**
 * Meta-data for every field of every AOServObject is available as
 * a SchemaColumn.   This allows AOServObjects to be
 * treated in a uniform manner, while still accessing all of their attributes.
 *
 * @see  SchemaTable
 * @see  AOServObject
 *
 * @author  AO Industries, Inc.
 */
final public class SchemaColumn extends GlobalObjectIntegerKey {

	static final int COLUMN_PKEY=0;

	String table_name;
	String column_name;
	private int index;
	private String type;
	private boolean
		is_nullable,
		is_unique,
		is_public
	;
	private String description;
	private String since_version;
	private String last_version;

	public SchemaColumn() {
	}

	public SchemaColumn(
		int pkey,
		String table_name,
		String column_name,
		int index,
		String type,
		boolean is_nullable,
		boolean is_unique,
		boolean is_public,
		String description,
		String since_version,
		String last_version
	) {
		this.pkey=pkey;
		this.table_name=table_name;
		this.column_name=column_name;
		this.index=index;
		this.type=type;
		this.is_nullable=is_nullable;
		this.is_unique=is_unique;
		this.is_public=is_public;
		this.description=description;
		this.since_version=since_version;
		this.last_version=last_version;
	}

	public String getColumnName() {
		return column_name;
	}

	@Override
	Object getColumnImpl(int i) {
		switch(i) {
			case COLUMN_PKEY: return pkey;
			case 1: return table_name;
			case 2: return column_name;
			case 3: return index;
			case 4: return type;
			case 5: return is_nullable;
			case 6: return is_unique;
			case 7: return is_public;
			case 8: return description;
			case 9: return since_version;
			case 10: return last_version;
			default: throw new IllegalArgumentException("Invalid index: "+i);
		}
	}

	public String getDescription() {
		return description;
	}

	public String getSinceVersion() {
		return since_version;
	}

	public String getLastVersion() {
		return last_version;
	}

	public int getIndex() {
		return index;
	}

	public List getReferencedBy(AOServConnector connector) throws IOException, SQLException {
		return connector.getSchemaForeignKeys().getSchemaForeignKeysReferencing(this);
	}

	public List getReferences(AOServConnector connector) throws IOException, SQLException {
		return connector.getSchemaForeignKeys().getSchemaForeignKeysReferencedBy(this);
	}

	public SchemaTable getSchemaTable(AOServConnector connector) throws SQLException, IOException {
		SchemaTable obj=connector.getSchemaTables().get(table_name);
		if(obj==null) throw new SQLException("Unable to find SchemaTable: "+table_name);
		return obj;
	}

	public String getSchemaTableName() {
		return table_name;
	}

	public SchemaType getSchemaType(AOServConnector connector) throws SQLException, IOException {
		SchemaType obj=connector.getSchemaTypes().get(type);
		if(obj==null) throw new SQLException("Unable to find SchemaType: "+type);
		return obj;
	}

	public String getSchemaTypeName() {
		return type;
	}

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

	@Override
	public void init(ResultSet result) throws SQLException {
		pkey=result.getInt(1);
		table_name=result.getString(2);
		column_name=result.getString(3);
		index=result.getInt(4);
		type=result.getString(5);
		is_nullable=result.getBoolean(6);
		is_unique=result.getBoolean(7);
		is_public=result.getBoolean(8);
		description=result.getString(9);
		since_version=result.getString(10);
		last_version=result.getString(11);
	}

	public boolean isNullable() {
		return is_nullable;
	}

	public boolean isPublic() {
		return is_public;
	}

	public boolean isUnique() {
		return is_unique;
	}

	@Override
	public void read(CompressedDataInputStream in) throws IOException {
		pkey=in.readCompressedInt();
		table_name=in.readUTF().intern();
		column_name=in.readUTF().intern();
		index=in.readCompressedInt();
		type=in.readUTF().intern();
		is_nullable=in.readBoolean();
		is_unique=in.readBoolean();
		is_public=in.readBoolean();
		description=in.readUTF();
		since_version=in.readUTF().intern();
		last_version=InternUtils.intern(in.readNullUTF());
	}

	@Override
	String toStringImpl() {
		return table_name+'.'+column_name;
	}

	@Override
	public void write(CompressedDataOutputStream out, AOServProtocol.Version version) throws IOException {
		out.writeCompressedInt(pkey);
		out.writeUTF(table_name);
		out.writeUTF(column_name);
		out.writeCompressedInt(index);
		out.writeUTF(type);
		out.writeBoolean(is_nullable);
		out.writeBoolean(is_unique);
		out.writeBoolean(is_public);
		out.writeUTF(description);
		if(version.compareTo(AOServProtocol.Version.VERSION_1_0_A_101)>=0) out.writeUTF(since_version);
		if(version.compareTo(AOServProtocol.Version.VERSION_1_0_A_104)>=0) out.writeNullUTF(last_version);
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy