/* This file belongs to the Servoy development and deployment environment, Copyright (C) 1997-2010 Servoy BV This program is free software; you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version. This program 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 Affero General Public License for more details. You should have received a copy of the GNU Affero General Public License along with this program; if not, see http://www.gnu.org/licenses or write to the Free Software Foundation,Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 */ package com.servoy.j2db.query; import java.util.concurrent.atomic.AtomicLong; import com.servoy.base.query.BaseQueryTable; import com.servoy.j2db.util.Immutable; import com.servoy.j2db.util.serialize.IWriteReplaceExtended; import com.servoy.j2db.util.serialize.ReplacedObject; import com.servoy.j2db.util.visitor.IVisitor; /** * Table class in the query structure. The alias is generated by this class in the constructor. * * @author rgansevles * */ public class QueryTable extends BaseQueryTable implements IQueryElement, Immutable, IWriteReplaceExtended { private static AtomicLong aliasCounter = new AtomicLong(0); /** * @param name table name as used in sql, may be quoted */ public QueryTable(String name, String dataSource, String catalogName, String schemaName) { super(name, dataSource, catalogName, schemaName); } /** * @param name table name as used in sql, may be quoted */ public QueryTable(String name, String dataSource, String catalogName, String schemaName, boolean needsQuoting) { super(name, dataSource, catalogName, schemaName, needsQuoting); } public QueryTable(String name, String dataSource, String catalogName, String schemaName, String alias) { super(name, dataSource, catalogName, schemaName, alias); } public QueryTable(String name, String dataSource, String catalogName, String schemaName, String alias, boolean needsQuoting) { super(name, dataSource, catalogName, schemaName, alias, needsQuoting); } /** * QueryTable with all fields, only for internal use. * @param name * @param dataSource * @param alias * @param needsQuoting * @param catalogName * @param schemaName * @param generatedAlias * @param isComplete */ protected QueryTable(String name, String dataSource, String alias, boolean needsQuoting, String catalogName, String schemaName, boolean generatedAlias, boolean isComplete) { super(name, dataSource, alias, needsQuoting, catalogName, schemaName, generatedAlias, isComplete); } @Override public Object shallowClone() throws CloneNotSupportedException { return super.clone(); } public void acceptVisitor(IVisitor visitor) { } @Override protected long getNextAliasCounter() { return aliasCounter.incrementAndGet(); } ///////// serialization //////////////// public Object writeReplace() { return writeReplace(false); } public ReplacedObject writeReplace(boolean full) { // Note: when this serialized structure changes, make sure that old data (maybe saved as serialized xml) can still be deserialized! // just need to serialize the name, the alias can be regenerated. // Note: this only works if the query object was packed before serialization! // catalogName and schemaName will be regenerated on the server Object replaced; if (full) { replaced = generatedAlias ? new Object[] { name, dataSource, Boolean.valueOf(needsQuoting), catalogName, schemaName } : new Object[] { name, dataSource, Boolean.valueOf(needsQuoting), catalogName, schemaName, alias }; } else { replaced = generatedAlias ? new Object[] { name, dataSource, Boolean.valueOf(needsQuoting) } : new Object[] { name, dataSource, Boolean.valueOf(needsQuoting), alias }; } return new ReplacedObject(AbstractBaseQuery.QUERY_SERIALIZE_DOMAIN, getClass(), replaced); } /* * Dummy call from legacy QueryTable1 deserialisation */ protected QueryTable() { } public QueryTable(ReplacedObject s) { // catalogName and schemaName will be regenerated on the server isComplete = false; catalogName = null; schemaName = null; Object[] members = (Object[])s.getObject(); int i = 0; name = (String)members[i++]; dataSource = (String)members[i++]; needsQuoting = ((Boolean)members[i++]).booleanValue(); if (members.length == 3) { // [name, dataSource, needsQuoting] alias = generateAlias(name); generatedAlias = true; } else if (members.length == 4) { // [name, dataSource, needsQuoting, alias] alias = (String)members[i++]; generatedAlias = false; } else if (members.length == 5) { // [name, dataSource, needsQuoting, catalog, schema] catalogName = (String)members[i++]; schemaName = (String)members[i++]; isComplete = true; alias = generateAlias(name); generatedAlias = true; } else if (members.length == 6) { // [name, dataSource, needsQuoting, catalog, schema, alias] catalogName = (String)members[i++]; schemaName = (String)members[i++]; isComplete = true; alias = (String)members[i++]; generatedAlias = false; } else { // should never happen! throw new IllegalStateException("unexpected serialized table object"); } } /** * Update the fields that have not been set in serialization */ public void update(String catalogName, String schemaName) { this.catalogName = catalogName; this.schemaName = schemaName; isComplete = true; } }