http://git-wip-us.apache.org/repos/asf/polygene-java/blob/a36086b6/libraries/sql-generator/src/main/java/org/apache/polygene/library/sql/generator/implementation/grammar/query/TableReferenceImpl.java ---------------------------------------------------------------------- diff --git a/libraries/sql-generator/src/main/java/org/apache/polygene/library/sql/generator/implementation/grammar/query/TableReferenceImpl.java b/libraries/sql-generator/src/main/java/org/apache/polygene/library/sql/generator/implementation/grammar/query/TableReferenceImpl.java new file mode 100644 index 0000000..762c002 --- /dev/null +++ b/libraries/sql-generator/src/main/java/org/apache/polygene/library/sql/generator/implementation/grammar/query/TableReferenceImpl.java @@ -0,0 +1,44 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * + */ +package org.apache.polygene.library.sql.generator.implementation.grammar.query; + +import org.apache.polygene.library.sql.generator.Typeable; +import org.apache.polygene.library.sql.generator.grammar.query.TableReference; +import org.apache.polygene.library.sql.generator.implementation.grammar.common.SQLSyntaxElementBase; +import org.apache.polygene.library.sql.generator.implementation.transformation.spi.SQLProcessorAggregator; + +/** + * @author Stanislav Muhametsin + */ +public abstract class TableReferenceImpl<TypeableType extends Typeable<? super TypeableType>, RealType extends TypeableType> + extends SQLSyntaxElementBase<TypeableType, RealType> + implements TableReference +{ + + public TableReferenceImpl( SQLProcessorAggregator processor, Class<? extends RealType> expressionClass ) + { + super( processor, expressionClass ); + } + + public Typeable<?> asTypeable() + { + return this; + } +}
http://git-wip-us.apache.org/repos/asf/polygene-java/blob/a36086b6/libraries/sql-generator/src/main/java/org/apache/polygene/library/sql/generator/implementation/grammar/query/TableReferencePrimaryImpl.java ---------------------------------------------------------------------- diff --git a/libraries/sql-generator/src/main/java/org/apache/polygene/library/sql/generator/implementation/grammar/query/TableReferencePrimaryImpl.java b/libraries/sql-generator/src/main/java/org/apache/polygene/library/sql/generator/implementation/grammar/query/TableReferencePrimaryImpl.java new file mode 100644 index 0000000..9423618 --- /dev/null +++ b/libraries/sql-generator/src/main/java/org/apache/polygene/library/sql/generator/implementation/grammar/query/TableReferencePrimaryImpl.java @@ -0,0 +1,54 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * + */ +package org.apache.polygene.library.sql.generator.implementation.grammar.query; + +import org.apache.polygene.library.sql.generator.grammar.query.TableAlias; +import org.apache.polygene.library.sql.generator.grammar.query.TableReferencePrimary; +import org.apache.polygene.library.sql.generator.implementation.TypeableImpl; +import org.apache.polygene.library.sql.generator.implementation.transformation.spi.SQLProcessorAggregator; + +/** + * @author Stanislav Muhametsin + */ +public class TableReferencePrimaryImpl<TableReferenceType extends TableReferencePrimary> extends + TableReferenceImpl<TableReferencePrimary, TableReferenceType> + implements TableReferencePrimary +{ + + private final TableAlias _tableAlias; + + public TableReferencePrimaryImpl( SQLProcessorAggregator processor, + Class<? extends TableReferenceType> tableReferenceClass, TableAlias alias ) + { + super( processor, tableReferenceClass ); + this._tableAlias = alias; + } + + public TableAlias getTableAlias() + { + return this._tableAlias; + } + + @Override + protected boolean doesEqual( TableReferenceType another ) + { + return TypeableImpl.bothNullOrEquals( this._tableAlias, another.getTableAlias() ); + } +} http://git-wip-us.apache.org/repos/asf/polygene-java/blob/a36086b6/libraries/sql-generator/src/main/java/org/apache/polygene/library/sql/generator/implementation/grammar/query/TableValueConstructorImpl.java ---------------------------------------------------------------------- diff --git a/libraries/sql-generator/src/main/java/org/apache/polygene/library/sql/generator/implementation/grammar/query/TableValueConstructorImpl.java b/libraries/sql-generator/src/main/java/org/apache/polygene/library/sql/generator/implementation/grammar/query/TableValueConstructorImpl.java new file mode 100644 index 0000000..84dc5c9 --- /dev/null +++ b/libraries/sql-generator/src/main/java/org/apache/polygene/library/sql/generator/implementation/grammar/query/TableValueConstructorImpl.java @@ -0,0 +1,61 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * + */ +package org.apache.polygene.library.sql.generator.implementation.grammar.query; + +import java.util.Collections; +import java.util.List; +import java.util.Objects; +import org.apache.polygene.library.sql.generator.grammar.query.RowValueConstructor; +import org.apache.polygene.library.sql.generator.grammar.query.TableValueConstructor; +import org.apache.polygene.library.sql.generator.implementation.transformation.spi.SQLProcessorAggregator; + +/** + * @author Stanislav Muhametsin + */ +public class TableValueConstructorImpl extends QueryExpressionBodyImpl<TableValueConstructor> + implements TableValueConstructor +{ + + private final List<RowValueConstructor> _rows; + + public TableValueConstructorImpl( SQLProcessorAggregator processor, List<RowValueConstructor> rows ) + { + this( processor, TableValueConstructor.class, rows ); + } + + protected TableValueConstructorImpl( SQLProcessorAggregator processor, + Class<? extends TableValueConstructor> expressionClass, List<RowValueConstructor> rows ) + { + super( processor, expressionClass ); + Objects.requireNonNull( rows, "rows" ); + this._rows = Collections.unmodifiableList( rows ); + } + + public List<RowValueConstructor> getRows() + { + return this._rows; + } + + @Override + protected boolean doesEqual( TableValueConstructor other ) + { + return this._rows.equals( other.getRows() ); + } +} http://git-wip-us.apache.org/repos/asf/polygene-java/blob/a36086b6/libraries/sql-generator/src/main/java/org/apache/polygene/library/sql/generator/implementation/grammar/query/joins/CrossJoinedTableImpl.java ---------------------------------------------------------------------- diff --git a/libraries/sql-generator/src/main/java/org/apache/polygene/library/sql/generator/implementation/grammar/query/joins/CrossJoinedTableImpl.java b/libraries/sql-generator/src/main/java/org/apache/polygene/library/sql/generator/implementation/grammar/query/joins/CrossJoinedTableImpl.java new file mode 100644 index 0000000..a60d038 --- /dev/null +++ b/libraries/sql-generator/src/main/java/org/apache/polygene/library/sql/generator/implementation/grammar/query/joins/CrossJoinedTableImpl.java @@ -0,0 +1,55 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * + */ +package org.apache.polygene.library.sql.generator.implementation.grammar.query.joins; + +import org.apache.polygene.library.sql.generator.grammar.query.TableReference; +import org.apache.polygene.library.sql.generator.grammar.query.joins.CrossJoinedTable; +import org.apache.polygene.library.sql.generator.implementation.transformation.spi.SQLProcessorAggregator; + +/** + * @author Stanislav Muhametsin + */ +public class CrossJoinedTableImpl extends JoinedTableImpl<CrossJoinedTable> + implements CrossJoinedTable +{ + + public CrossJoinedTableImpl( SQLProcessorAggregator processor, TableReference left, TableReference right ) + { + this( processor, CrossJoinedTable.class, left, right ); + } + + protected CrossJoinedTableImpl( SQLProcessorAggregator processor, Class<? extends CrossJoinedTable> implClass, + TableReference left, TableReference right ) + { + super( processor, implClass, left, right ); + } + + @Override + protected boolean doesEqual( CrossJoinedTable another ) + { + boolean result = this.getRight().equals( another.getRight() ); + if( result ) + { + result = super.doesEqual( another ); + } + + return result; + } +} http://git-wip-us.apache.org/repos/asf/polygene-java/blob/a36086b6/libraries/sql-generator/src/main/java/org/apache/polygene/library/sql/generator/implementation/grammar/query/joins/JoinConditionImpl.java ---------------------------------------------------------------------- diff --git a/libraries/sql-generator/src/main/java/org/apache/polygene/library/sql/generator/implementation/grammar/query/joins/JoinConditionImpl.java b/libraries/sql-generator/src/main/java/org/apache/polygene/library/sql/generator/implementation/grammar/query/joins/JoinConditionImpl.java new file mode 100644 index 0000000..7859695 --- /dev/null +++ b/libraries/sql-generator/src/main/java/org/apache/polygene/library/sql/generator/implementation/grammar/query/joins/JoinConditionImpl.java @@ -0,0 +1,59 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * + */ +package org.apache.polygene.library.sql.generator.implementation.grammar.query.joins; + +import java.util.Objects; +import org.apache.polygene.library.sql.generator.grammar.booleans.BooleanExpression; +import org.apache.polygene.library.sql.generator.grammar.query.joins.JoinCondition; +import org.apache.polygene.library.sql.generator.implementation.transformation.spi.SQLProcessorAggregator; + +/** + * @author Stanislav Muhametsin + */ +public class JoinConditionImpl extends JoinSpecificationImpl<JoinCondition> + implements JoinCondition +{ + + private final BooleanExpression _searchCondition; + + public JoinConditionImpl( SQLProcessorAggregator processor, BooleanExpression searchCondition ) + { + this( processor, JoinCondition.class, searchCondition ); + } + + protected JoinConditionImpl( SQLProcessorAggregator processor, Class<? extends JoinCondition> implClass, + BooleanExpression searchCondition ) + { + super( processor, implClass ); + Objects.requireNonNull( searchCondition, "search condition" ); + this._searchCondition = searchCondition; + } + + public BooleanExpression getSearchConidition() + { + return this._searchCondition; + } + + @Override + protected boolean doesEqual( JoinCondition another ) + { + return this._searchCondition.equals( another.getSearchConidition() ); + } +} http://git-wip-us.apache.org/repos/asf/polygene-java/blob/a36086b6/libraries/sql-generator/src/main/java/org/apache/polygene/library/sql/generator/implementation/grammar/query/joins/JoinSpecificationImpl.java ---------------------------------------------------------------------- diff --git a/libraries/sql-generator/src/main/java/org/apache/polygene/library/sql/generator/implementation/grammar/query/joins/JoinSpecificationImpl.java b/libraries/sql-generator/src/main/java/org/apache/polygene/library/sql/generator/implementation/grammar/query/joins/JoinSpecificationImpl.java new file mode 100644 index 0000000..ef7c914 --- /dev/null +++ b/libraries/sql-generator/src/main/java/org/apache/polygene/library/sql/generator/implementation/grammar/query/joins/JoinSpecificationImpl.java @@ -0,0 +1,39 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * + */ +package org.apache.polygene.library.sql.generator.implementation.grammar.query.joins; + +import org.apache.polygene.library.sql.generator.grammar.query.joins.JoinSpecification; +import org.apache.polygene.library.sql.generator.implementation.grammar.common.SQLSyntaxElementBase; +import org.apache.polygene.library.sql.generator.implementation.transformation.spi.SQLProcessorAggregator; + +/** + * @author Stanislav Muhametsin + */ +public abstract class JoinSpecificationImpl<JoinSpecificationType extends JoinSpecification> extends + SQLSyntaxElementBase<JoinSpecification, JoinSpecificationType> + implements JoinSpecification +{ + + public JoinSpecificationImpl( SQLProcessorAggregator processor, + Class<? extends JoinSpecificationType> expressionClass ) + { + super( processor, expressionClass ); + } +} http://git-wip-us.apache.org/repos/asf/polygene-java/blob/a36086b6/libraries/sql-generator/src/main/java/org/apache/polygene/library/sql/generator/implementation/grammar/query/joins/JoinedTableImpl.java ---------------------------------------------------------------------- diff --git a/libraries/sql-generator/src/main/java/org/apache/polygene/library/sql/generator/implementation/grammar/query/joins/JoinedTableImpl.java b/libraries/sql-generator/src/main/java/org/apache/polygene/library/sql/generator/implementation/grammar/query/joins/JoinedTableImpl.java new file mode 100644 index 0000000..07a9b04 --- /dev/null +++ b/libraries/sql-generator/src/main/java/org/apache/polygene/library/sql/generator/implementation/grammar/query/joins/JoinedTableImpl.java @@ -0,0 +1,70 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * + */ +package org.apache.polygene.library.sql.generator.implementation.grammar.query.joins; + +import java.util.Objects; +import org.apache.polygene.library.sql.generator.Typeable; +import org.apache.polygene.library.sql.generator.grammar.query.TableReference; +import org.apache.polygene.library.sql.generator.grammar.query.joins.JoinedTable; +import org.apache.polygene.library.sql.generator.implementation.grammar.query.QueryExpressionBodyImpl; +import org.apache.polygene.library.sql.generator.implementation.transformation.spi.SQLProcessorAggregator; + +/** + * @author Stanislav Muhametsin + */ +public abstract class JoinedTableImpl<TableReferenceType extends JoinedTable> extends + QueryExpressionBodyImpl<TableReferenceType> + implements JoinedTable +{ + + private final TableReference _left; + private final TableReference _right; + + protected JoinedTableImpl( SQLProcessorAggregator processor, + Class<? extends TableReferenceType> tableReferenceClass, TableReference left, TableReference right ) + { + super( processor, tableReferenceClass ); + Objects.requireNonNull( left, "left" ); + Objects.requireNonNull( right, "right" ); + this._left = left; + this._right = right; + } + + public TableReference getLeft() + { + return this._left; + } + + public TableReference getRight() + { + return this._right; + } + + public Typeable<?> asTypeable() + { + return this; + } + + @Override + protected boolean doesEqual( TableReferenceType another ) + { + return this._left.equals( another.getLeft() ); + } +} http://git-wip-us.apache.org/repos/asf/polygene-java/blob/a36086b6/libraries/sql-generator/src/main/java/org/apache/polygene/library/sql/generator/implementation/grammar/query/joins/NamedColumnsJoinImpl.java ---------------------------------------------------------------------- diff --git a/libraries/sql-generator/src/main/java/org/apache/polygene/library/sql/generator/implementation/grammar/query/joins/NamedColumnsJoinImpl.java b/libraries/sql-generator/src/main/java/org/apache/polygene/library/sql/generator/implementation/grammar/query/joins/NamedColumnsJoinImpl.java new file mode 100644 index 0000000..7bc45d9 --- /dev/null +++ b/libraries/sql-generator/src/main/java/org/apache/polygene/library/sql/generator/implementation/grammar/query/joins/NamedColumnsJoinImpl.java @@ -0,0 +1,59 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * + */ +package org.apache.polygene.library.sql.generator.implementation.grammar.query.joins; + +import java.util.Objects; +import org.apache.polygene.library.sql.generator.grammar.common.ColumnNameList; +import org.apache.polygene.library.sql.generator.grammar.query.joins.NamedColumnsJoin; +import org.apache.polygene.library.sql.generator.implementation.transformation.spi.SQLProcessorAggregator; + +/** + * @author Stanislav Muhametsin + */ +public class NamedColumnsJoinImpl extends JoinSpecificationImpl<NamedColumnsJoin> + implements NamedColumnsJoin +{ + + private final ColumnNameList _columnNames; + + public NamedColumnsJoinImpl( SQLProcessorAggregator processor, ColumnNameList columnNames ) + { + this( processor, NamedColumnsJoin.class, columnNames ); + } + + protected NamedColumnsJoinImpl( SQLProcessorAggregator processor, Class<? extends NamedColumnsJoin> implClass, + ColumnNameList columnNames ) + { + super( processor, implClass ); + Objects.requireNonNull( columnNames, "column names" ); + this._columnNames = columnNames; + } + + public ColumnNameList getColumnNames() + { + return this._columnNames; + } + + @Override + protected boolean doesEqual( NamedColumnsJoin another ) + { + return this._columnNames.equals( another.getColumnNames() ); + } +} http://git-wip-us.apache.org/repos/asf/polygene-java/blob/a36086b6/libraries/sql-generator/src/main/java/org/apache/polygene/library/sql/generator/implementation/grammar/query/joins/NaturalJoinedTableImpl.java ---------------------------------------------------------------------- diff --git a/libraries/sql-generator/src/main/java/org/apache/polygene/library/sql/generator/implementation/grammar/query/joins/NaturalJoinedTableImpl.java b/libraries/sql-generator/src/main/java/org/apache/polygene/library/sql/generator/implementation/grammar/query/joins/NaturalJoinedTableImpl.java new file mode 100644 index 0000000..b1663f7 --- /dev/null +++ b/libraries/sql-generator/src/main/java/org/apache/polygene/library/sql/generator/implementation/grammar/query/joins/NaturalJoinedTableImpl.java @@ -0,0 +1,67 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * + */ +package org.apache.polygene.library.sql.generator.implementation.grammar.query.joins; + +import java.util.Objects; +import org.apache.polygene.library.sql.generator.grammar.query.TableReference; +import org.apache.polygene.library.sql.generator.grammar.query.joins.JoinType; +import org.apache.polygene.library.sql.generator.grammar.query.joins.NaturalJoinedTable; +import org.apache.polygene.library.sql.generator.implementation.transformation.spi.SQLProcessorAggregator; + +/** + * @author Stanislav Muhametsin + */ +public class NaturalJoinedTableImpl extends JoinedTableImpl<NaturalJoinedTable> + implements NaturalJoinedTable +{ + + private final JoinType _joinType; + + public NaturalJoinedTableImpl( SQLProcessorAggregator processor, TableReference left, TableReference right, + JoinType joinType ) + { + this( processor, NaturalJoinedTable.class, left, right, joinType ); + } + + protected NaturalJoinedTableImpl( SQLProcessorAggregator processor, Class<? extends NaturalJoinedTable> implClass, + TableReference left, TableReference right, JoinType joinType ) + { + super( processor, implClass, left, right ); + Objects.requireNonNull( joinType, "join type" ); + this._joinType = joinType; + } + + public JoinType getJoinType() + { + return this._joinType; + } + + @Override + protected boolean doesEqual( NaturalJoinedTable another ) + { + boolean result = this._joinType.equals( another.getJoinType() ) && this.getRight().equals( another.getRight() ); + if( result ) + { + result = super.doesEqual( another ); + } + + return result; + } +} http://git-wip-us.apache.org/repos/asf/polygene-java/blob/a36086b6/libraries/sql-generator/src/main/java/org/apache/polygene/library/sql/generator/implementation/grammar/query/joins/QualifiedJoinedTableImpl.java ---------------------------------------------------------------------- diff --git a/libraries/sql-generator/src/main/java/org/apache/polygene/library/sql/generator/implementation/grammar/query/joins/QualifiedJoinedTableImpl.java b/libraries/sql-generator/src/main/java/org/apache/polygene/library/sql/generator/implementation/grammar/query/joins/QualifiedJoinedTableImpl.java new file mode 100644 index 0000000..864c82b --- /dev/null +++ b/libraries/sql-generator/src/main/java/org/apache/polygene/library/sql/generator/implementation/grammar/query/joins/QualifiedJoinedTableImpl.java @@ -0,0 +1,78 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * + */ +package org.apache.polygene.library.sql.generator.implementation.grammar.query.joins; + +import java.util.Objects; +import org.apache.polygene.library.sql.generator.grammar.query.TableReference; +import org.apache.polygene.library.sql.generator.grammar.query.joins.JoinSpecification; +import org.apache.polygene.library.sql.generator.grammar.query.joins.JoinType; +import org.apache.polygene.library.sql.generator.grammar.query.joins.QualifiedJoinedTable; +import org.apache.polygene.library.sql.generator.implementation.transformation.spi.SQLProcessorAggregator; + +/** + * @author Stanislav Muhametsin + */ +public class QualifiedJoinedTableImpl extends JoinedTableImpl<QualifiedJoinedTable> + implements QualifiedJoinedTable +{ + + private final JoinType _joinType; + + private final JoinSpecification _joinSpec; + + public QualifiedJoinedTableImpl( SQLProcessorAggregator processor, TableReference left, TableReference right, + JoinType joinType, JoinSpecification joinSpec ) + { + this( processor, QualifiedJoinedTable.class, left, right, joinType, joinSpec ); + } + + protected QualifiedJoinedTableImpl( SQLProcessorAggregator processor, + Class<? extends QualifiedJoinedTable> implClass, TableReference left, TableReference right, JoinType joinType, + JoinSpecification joinSpec ) + { + super( processor, implClass, left, right ); + Objects.requireNonNull( joinType, "join type" ); + Objects.requireNonNull( joinSpec, "join specification" ); + this._joinType = joinType; + this._joinSpec = joinSpec; + } + + public JoinType getJoinType() + { + return this._joinType; + } + + public JoinSpecification getJoinSpecification() + { + return this._joinSpec; + } + + @Override + protected boolean doesEqual( QualifiedJoinedTable another ) + { + boolean result = this._joinType.equals( another.getJoinType() ) + && this._joinSpec.equals( another.getJoinSpecification() ) && this.getRight().equals( another.getRight() ); + if( result ) + { + result = super.doesEqual( another ); + } + return result; + } +} http://git-wip-us.apache.org/repos/asf/polygene-java/blob/a36086b6/libraries/sql-generator/src/main/java/org/apache/polygene/library/sql/generator/implementation/grammar/query/joins/UnionJoinedTableImpl.java ---------------------------------------------------------------------- diff --git a/libraries/sql-generator/src/main/java/org/apache/polygene/library/sql/generator/implementation/grammar/query/joins/UnionJoinedTableImpl.java b/libraries/sql-generator/src/main/java/org/apache/polygene/library/sql/generator/implementation/grammar/query/joins/UnionJoinedTableImpl.java new file mode 100644 index 0000000..9d7496b --- /dev/null +++ b/libraries/sql-generator/src/main/java/org/apache/polygene/library/sql/generator/implementation/grammar/query/joins/UnionJoinedTableImpl.java @@ -0,0 +1,55 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * + */ +package org.apache.polygene.library.sql.generator.implementation.grammar.query.joins; + +import org.apache.polygene.library.sql.generator.grammar.query.TableReference; +import org.apache.polygene.library.sql.generator.grammar.query.joins.UnionJoinedTable; +import org.apache.polygene.library.sql.generator.implementation.transformation.spi.SQLProcessorAggregator; + +/** + * @author Stanislav Muhametsin + */ +public class UnionJoinedTableImpl extends JoinedTableImpl<UnionJoinedTable> + implements UnionJoinedTable +{ + + public UnionJoinedTableImpl( SQLProcessorAggregator processor, TableReference left, TableReference right ) + { + this( processor, UnionJoinedTable.class, left, right ); + } + + protected UnionJoinedTableImpl( SQLProcessorAggregator processor, Class<? extends UnionJoinedTable> implClass, + TableReference left, TableReference right ) + { + super( processor, implClass, left, right ); + } + + @Override + protected boolean doesEqual( UnionJoinedTable another ) + { + boolean result = this.getRight().equals( another.getRight() ); + if( result ) + { + result = super.doesEqual( another ); + } + + return result; + } +} http://git-wip-us.apache.org/repos/asf/polygene-java/blob/a36086b6/libraries/sql-generator/src/main/java/org/apache/polygene/library/sql/generator/implementation/transformation/AbstractProcessor.java ---------------------------------------------------------------------- diff --git a/libraries/sql-generator/src/main/java/org/apache/polygene/library/sql/generator/implementation/transformation/AbstractProcessor.java b/libraries/sql-generator/src/main/java/org/apache/polygene/library/sql/generator/implementation/transformation/AbstractProcessor.java new file mode 100644 index 0000000..661014d --- /dev/null +++ b/libraries/sql-generator/src/main/java/org/apache/polygene/library/sql/generator/implementation/transformation/AbstractProcessor.java @@ -0,0 +1,51 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * + */ +package org.apache.polygene.library.sql.generator.implementation.transformation; + +import java.util.Objects; +import org.apache.polygene.library.sql.generator.Typeable; +import org.apache.polygene.library.sql.generator.implementation.transformation.spi.SQLProcessor; +import org.apache.polygene.library.sql.generator.implementation.transformation.spi.SQLProcessorAggregator; + +/** + * @author Stanislav Muhametsin + */ +public abstract class AbstractProcessor<ProcessableType extends Typeable<?>> + implements SQLProcessor +{ + + private final Class<? extends ProcessableType> _type; + + public AbstractProcessor( Class<? extends ProcessableType> realType ) + { + Objects.requireNonNull( realType, "Processable type" ); + this._type = realType; + } + + public void process( SQLProcessorAggregator aggregator, Typeable<?> object, StringBuilder builder ) + { + if( object != null ) + { + this.doProcess( aggregator, this._type.cast( object ), builder ); + } + } + + protected abstract void doProcess( SQLProcessorAggregator aggregator, ProcessableType object, StringBuilder builder ); +} http://git-wip-us.apache.org/repos/asf/polygene-java/blob/a36086b6/libraries/sql-generator/src/main/java/org/apache/polygene/library/sql/generator/implementation/transformation/BooleanExpressionProcessing.java ---------------------------------------------------------------------- diff --git a/libraries/sql-generator/src/main/java/org/apache/polygene/library/sql/generator/implementation/transformation/BooleanExpressionProcessing.java b/libraries/sql-generator/src/main/java/org/apache/polygene/library/sql/generator/implementation/transformation/BooleanExpressionProcessing.java new file mode 100644 index 0000000..8b42d9b --- /dev/null +++ b/libraries/sql-generator/src/main/java/org/apache/polygene/library/sql/generator/implementation/transformation/BooleanExpressionProcessing.java @@ -0,0 +1,289 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * + */ +package org.apache.polygene.library.sql.generator.implementation.transformation; + +import java.util.Iterator; +import java.util.Objects; +import org.apache.polygene.library.sql.generator.grammar.booleans.BinaryPredicate; +import org.apache.polygene.library.sql.generator.grammar.booleans.BooleanExpression; +import org.apache.polygene.library.sql.generator.grammar.booleans.BooleanTest; +import org.apache.polygene.library.sql.generator.grammar.booleans.BooleanTest.TestType; +import org.apache.polygene.library.sql.generator.grammar.booleans.Conjunction; +import org.apache.polygene.library.sql.generator.grammar.booleans.Disjunction; +import org.apache.polygene.library.sql.generator.grammar.booleans.MultiPredicate; +import org.apache.polygene.library.sql.generator.grammar.booleans.Negation; +import org.apache.polygene.library.sql.generator.grammar.booleans.UnaryPredicate; +import org.apache.polygene.library.sql.generator.grammar.common.NonBooleanExpression; +import org.apache.polygene.library.sql.generator.grammar.common.SQLConstants; +import org.apache.polygene.library.sql.generator.grammar.query.QueryExpression; +import org.apache.polygene.library.sql.generator.implementation.grammar.booleans.BooleanUtils; +import org.apache.polygene.library.sql.generator.implementation.transformation.spi.SQLProcessorAggregator; + +/** + * Currently not thread-safe. + * + * @author Stanislav Muhametsin + */ +public class BooleanExpressionProcessing +{ + public static class UnaryPredicateProcessor extends AbstractProcessor<UnaryPredicate> + { + public enum UnaryOperatorOrientation + { + BEFORE_EXPRESSION, // After expression + AFTER_EXPRESSION + // Before expression + } + + public static final UnaryOperatorOrientation DEFAULT_ORIENTATION = UnaryOperatorOrientation.AFTER_EXPRESSION; + + private final UnaryOperatorOrientation _orientation; + private final String _operator; + + public UnaryPredicateProcessor( String operator ) + { + this( DEFAULT_ORIENTATION, operator ); + } + + public UnaryPredicateProcessor( UnaryOperatorOrientation unaryOrientation, String unaryOperator ) + { + super( UnaryPredicate.class ); + Objects.requireNonNull( unaryOrientation, "unary operator orientation" ); + Objects.requireNonNull( unaryOperator, "unary operator" ); + this._orientation = unaryOrientation; + this._operator = unaryOperator; + } + + protected void doProcess( SQLProcessorAggregator processor, UnaryPredicate predicate, StringBuilder builder ) + { + UnaryOperatorOrientation orientation = this._orientation; + if( orientation == UnaryOperatorOrientation.BEFORE_EXPRESSION ) + { + builder.append( this._operator ).append( SQLConstants.TOKEN_SEPARATOR ); + } + + NonBooleanExpression exp = predicate.getValueExpression(); + Boolean isQuery = exp instanceof QueryExpression; + if( isQuery ) + { + builder.append( SQLConstants.OPEN_PARENTHESIS ); + } + processor.process( exp, builder ); + if( isQuery ) + { + builder.append( SQLConstants.CLOSE_PARENTHESIS ); + } + + if( orientation == UnaryOperatorOrientation.AFTER_EXPRESSION ) + { + builder.append( SQLConstants.TOKEN_SEPARATOR ).append( this._operator ); + } + } + } + + public static class BinaryPredicateProcessor extends AbstractProcessor<BinaryPredicate> + { + private final String _operator; + + public BinaryPredicateProcessor( String binaryOperator ) + { + super( BinaryPredicate.class ); + Objects.requireNonNull( binaryOperator, "binary operator" ); + + this._operator = binaryOperator; + } + + @Override + protected void doProcess( SQLProcessorAggregator processor, BinaryPredicate predicate, StringBuilder builder ) + { + processor.process( predicate.getLeft(), builder ); + builder.append( SQLConstants.TOKEN_SEPARATOR ).append( this._operator ) + .append( SQLConstants.TOKEN_SEPARATOR ); + processor.process( predicate.getRight(), builder ); + } + } + + public static class MultiPredicateProcessor extends AbstractProcessor<MultiPredicate> + { + private final String _operator; + private final String _separator; + private final Boolean _needParenthesis; + + public MultiPredicateProcessor( String multiOperator, String multiSeparator, Boolean needParenthesis ) + { + super( MultiPredicate.class ); + Objects.requireNonNull( multiOperator, "multi-operator" ); + Objects.requireNonNull( multiSeparator, "multi separator" ); + Objects.requireNonNull( needParenthesis, "need parenthesis" ); + + this._operator = multiOperator; + this._separator = multiSeparator; + this._needParenthesis = needParenthesis; + } + + @Override + protected void doProcess( SQLProcessorAggregator processor, MultiPredicate predicate, StringBuilder builder ) + { + processor.process( predicate.getLeft(), builder ); + builder.append( SQLConstants.TOKEN_SEPARATOR ).append( this._operator ) + .append( SQLConstants.TOKEN_SEPARATOR ); + if( this._needParenthesis ) + { + builder.append( SQLConstants.OPEN_PARENTHESIS ); + } + + Iterator<NonBooleanExpression> iter = predicate.getRights().iterator(); + while( iter.hasNext() ) + { + NonBooleanExpression next = iter.next(); + Boolean isQuery = next instanceof QueryExpression; + + if( isQuery ) + { + builder.append( SQLConstants.OPEN_PARENTHESIS ); + } + + processor.process( next, builder ); + + if( isQuery ) + { + builder.append( SQLConstants.CLOSE_PARENTHESIS ); + } + + if( iter.hasNext() ) + { + builder.append( this._separator ); + } + } + + if( this._needParenthesis ) + { + builder.append( SQLConstants.CLOSE_PARENTHESIS ); + } + } + } + + public static class NegationProcessor extends AbstractProcessor<Negation> + { + public NegationProcessor() + { + super( Negation.class ); + } + + @Override + protected void doProcess( SQLProcessorAggregator aggregator, Negation object, StringBuilder builder ) + { + BooleanExpression negated = object.getNegated(); + if( !BooleanUtils.isEmpty( negated ) ) + { + builder.append( SQLConstants.NOT ).append( SQLConstants.TOKEN_SEPARATOR ); + aggregator.process( negated, builder ); + } + } + } + + public static void processBinaryComposedObject( SQLProcessorAggregator aggregator, BooleanExpression left, + BooleanExpression right, StringBuilder builder, String operator ) + { + Boolean leftEmpty = BooleanUtils.isEmpty( left ); + Boolean rightEmpty = BooleanUtils.isEmpty( right ); + if( !leftEmpty || !rightEmpty ) + { + Boolean oneEmpty = leftEmpty || rightEmpty; + if( !oneEmpty ) + { + builder.append( SQLConstants.OPEN_PARENTHESIS ); + } + aggregator.process( left, builder ); + + if( !oneEmpty ) + { + if( !leftEmpty ) + { + builder.append( SQLConstants.TOKEN_SEPARATOR ); + } + builder.append( operator ); + if( !rightEmpty ) + { + builder.append( SQLConstants.TOKEN_SEPARATOR ); + } + } + + aggregator.process( right, builder ); + if( !oneEmpty ) + { + builder.append( SQLConstants.CLOSE_PARENTHESIS ); + } + } + } + + public static class ConjunctionProcessor extends AbstractProcessor<Conjunction> + { + + public ConjunctionProcessor() + { + super( Conjunction.class ); + } + + @Override + protected void doProcess( SQLProcessorAggregator aggregator, Conjunction object, StringBuilder builder ) + { + processBinaryComposedObject( aggregator, object.getLeft(), object.getRight(), builder, SQLConstants.AND ); + } + } + + public static class DisjunctionProcessor extends AbstractProcessor<Disjunction> + { + public DisjunctionProcessor() + { + super( Disjunction.class ); + } + + @Override + protected void doProcess( SQLProcessorAggregator aggregator, Disjunction object, StringBuilder builder ) + { + processBinaryComposedObject( aggregator, object.getLeft(), object.getRight(), builder, SQLConstants.OR ); + } + } + + public static class BooleanTestProcessor extends AbstractProcessor<BooleanTest> + { + public BooleanTestProcessor() + { + super( BooleanTest.class ); + } + + @Override + protected void doProcess( SQLProcessorAggregator aggregator, BooleanTest object, StringBuilder builder ) + { + BooleanExpression testable = object.getBooleanExpression(); + builder.append( SQLConstants.OPEN_PARENTHESIS ); + aggregator.process( testable, builder ); + builder.append( SQLConstants.CLOSE_PARENTHESIS ).append( SQLConstants.IS ) + .append( SQLConstants.TOKEN_SEPARATOR ); + if( object.getTestType() == TestType.IS_NOT ) + { + builder.append( SQLConstants.NOT ).append( SQLConstants.TOKEN_SEPARATOR ); + } + + builder.append( object.getTruthValue().toString() ); + } + } +} http://git-wip-us.apache.org/repos/asf/polygene-java/blob/a36086b6/libraries/sql-generator/src/main/java/org/apache/polygene/library/sql/generator/implementation/transformation/ColumnProcessing.java ---------------------------------------------------------------------- diff --git a/libraries/sql-generator/src/main/java/org/apache/polygene/library/sql/generator/implementation/transformation/ColumnProcessing.java b/libraries/sql-generator/src/main/java/org/apache/polygene/library/sql/generator/implementation/transformation/ColumnProcessing.java new file mode 100644 index 0000000..2c2dbf8 --- /dev/null +++ b/libraries/sql-generator/src/main/java/org/apache/polygene/library/sql/generator/implementation/transformation/ColumnProcessing.java @@ -0,0 +1,95 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * + */ +package org.apache.polygene.library.sql.generator.implementation.transformation; + +import java.util.Iterator; +import org.apache.polygene.library.sql.generator.grammar.common.ColumnNameList; +import org.apache.polygene.library.sql.generator.grammar.common.SQLConstants; +import org.apache.polygene.library.sql.generator.grammar.query.ColumnReferenceByExpression; +import org.apache.polygene.library.sql.generator.grammar.query.ColumnReferenceByName; +import org.apache.polygene.library.sql.generator.implementation.transformation.spi.SQLProcessorAggregator; + +/** + * @author Stanislav Muhametsin + */ +public class ColumnProcessing +{ + + public static class ColumnReferenceByNameProcessor extends AbstractProcessor<ColumnReferenceByName> + { + public ColumnReferenceByNameProcessor() + { + super( ColumnReferenceByName.class ); + } + + @Override + protected void doProcess( SQLProcessorAggregator processor, ColumnReferenceByName columnRef, + StringBuilder builder ) + { + String tableName = columnRef.getTableName(); + if( ProcessorUtils.notNullAndNotEmpty( tableName ) ) + { + builder.append( tableName ).append( SQLConstants.TABLE_COLUMN_SEPARATOR ); + } + + builder.append( columnRef.getColumnName() ); + } + } + + public static class ColumnReferenceByExpressionProcessor extends AbstractProcessor<ColumnReferenceByExpression> + { + + public ColumnReferenceByExpressionProcessor() + { + super( ColumnReferenceByExpression.class ); + } + + @Override + protected void doProcess( SQLProcessorAggregator processor, ColumnReferenceByExpression columnRef, + StringBuilder builder ) + { + processor.process( columnRef.getExpression(), builder ); + } + } + + public static class ColumnNamesProcessor extends AbstractProcessor<ColumnNameList> + { + public ColumnNamesProcessor() + { + super( ColumnNameList.class ); + } + + @Override + protected void doProcess( SQLProcessorAggregator processor, ColumnNameList object, StringBuilder builder ) + { + builder.append( SQLConstants.OPEN_PARENTHESIS ); + Iterator<String> iter = object.getColumnNames().iterator(); + while( iter.hasNext() ) + { + builder.append( iter.next() ); + if( iter.hasNext() ) + { + builder.append( SQLConstants.COMMA ).append( SQLConstants.TOKEN_SEPARATOR ); + } + } + builder.append( SQLConstants.CLOSE_PARENTHESIS ); + } + } +} http://git-wip-us.apache.org/repos/asf/polygene-java/blob/a36086b6/libraries/sql-generator/src/main/java/org/apache/polygene/library/sql/generator/implementation/transformation/ConstantProcessor.java ---------------------------------------------------------------------- diff --git a/libraries/sql-generator/src/main/java/org/apache/polygene/library/sql/generator/implementation/transformation/ConstantProcessor.java b/libraries/sql-generator/src/main/java/org/apache/polygene/library/sql/generator/implementation/transformation/ConstantProcessor.java new file mode 100644 index 0000000..a27c5f5 --- /dev/null +++ b/libraries/sql-generator/src/main/java/org/apache/polygene/library/sql/generator/implementation/transformation/ConstantProcessor.java @@ -0,0 +1,49 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * + */ +package org.apache.polygene.library.sql.generator.implementation.transformation; + +import org.apache.polygene.library.sql.generator.Typeable; +import org.apache.polygene.library.sql.generator.implementation.transformation.spi.SQLProcessor; +import org.apache.polygene.library.sql.generator.implementation.transformation.spi.SQLProcessorAggregator; + +/** + * @author Stanislav Muhametsin + */ +public class ConstantProcessor + implements SQLProcessor +{ + + private final String _constant; + + public ConstantProcessor( String constant ) + { + if( constant == null ) + { + constant = ""; + } + + this._constant = constant; + } + + public void process( SQLProcessorAggregator aggregator, Typeable<?> object, StringBuilder builder ) + { + builder.append( this._constant ); + } +} http://git-wip-us.apache.org/repos/asf/polygene-java/blob/a36086b6/libraries/sql-generator/src/main/java/org/apache/polygene/library/sql/generator/implementation/transformation/DataTypeProcessing.java ---------------------------------------------------------------------- diff --git a/libraries/sql-generator/src/main/java/org/apache/polygene/library/sql/generator/implementation/transformation/DataTypeProcessing.java b/libraries/sql-generator/src/main/java/org/apache/polygene/library/sql/generator/implementation/transformation/DataTypeProcessing.java new file mode 100644 index 0000000..abb1b2d --- /dev/null +++ b/libraries/sql-generator/src/main/java/org/apache/polygene/library/sql/generator/implementation/transformation/DataTypeProcessing.java @@ -0,0 +1,275 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * + */ +package org.apache.polygene.library.sql.generator.implementation.transformation; + +import java.util.HashMap; +import java.util.Map; +import org.apache.polygene.library.sql.generator.grammar.common.SQLConstants; +import org.apache.polygene.library.sql.generator.grammar.common.datatypes.Decimal; +import org.apache.polygene.library.sql.generator.grammar.common.datatypes.IntervalDataType; +import org.apache.polygene.library.sql.generator.grammar.common.datatypes.Numeric; +import org.apache.polygene.library.sql.generator.grammar.common.datatypes.SQLChar; +import org.apache.polygene.library.sql.generator.grammar.common.datatypes.SQLFloat; +import org.apache.polygene.library.sql.generator.grammar.common.datatypes.SQLInterval; +import org.apache.polygene.library.sql.generator.grammar.common.datatypes.SQLTime; +import org.apache.polygene.library.sql.generator.grammar.common.datatypes.SQLTimeStamp; +import org.apache.polygene.library.sql.generator.grammar.common.datatypes.UserDefinedType; +import org.apache.polygene.library.sql.generator.implementation.transformation.spi.SQLProcessorAggregator; + +/** + * @author Stanislav Muhametsin + */ +public class DataTypeProcessing +{ + + public static class UserDefinedDataTypeProcessor extends AbstractProcessor<UserDefinedType> + { + + public UserDefinedDataTypeProcessor() + { + super( UserDefinedType.class ); + } + + @Override + protected void doProcess( SQLProcessorAggregator aggregator, UserDefinedType object, StringBuilder builder ) + { + builder.append( object.getTextualRepresentation() ); + } + } + + public static class DecimalProcessor extends AbstractProcessor<Decimal> + { + + public DecimalProcessor() + { + super( Decimal.class ); + } + + @Override + protected void doProcess( SQLProcessorAggregator aggregator, Decimal object, StringBuilder builder ) + { + builder.append( "DECIMAL" ); + if( object.getPrecision() != null ) + { + builder.append( SQLConstants.OPEN_PARENTHESIS ).append( object.getPrecision() ); + if( object.getScale() != null ) + { + builder.append( object.getScale() ); + } + builder.append( SQLConstants.CLOSE_PARENTHESIS ); + } + } + } + + public static class NumericProcessor extends AbstractProcessor<Numeric> + { + + public NumericProcessor() + { + super( Numeric.class ); + } + + @Override + protected void doProcess( SQLProcessorAggregator aggregator, Numeric object, StringBuilder builder ) + { + builder.append( "NUMERIC" ); + if( object.getPrecision() != null ) + { + builder.append( SQLConstants.OPEN_PARENTHESIS ).append( object.getPrecision() ); + if( object.getScale() != null ) + { + builder.append( object.getScale() ); + } + builder.append( SQLConstants.CLOSE_PARENTHESIS ); + } + } + } + + public static class SQLCharProcessor extends AbstractProcessor<SQLChar> + { + public SQLCharProcessor() + { + super( SQLChar.class ); + } + + @Override + protected void doProcess( SQLProcessorAggregator aggregator, SQLChar object, StringBuilder builder ) + { + builder.append( "CHARACTER" ); + if( object.isVarying() ) + { + builder.append( SQLConstants.TOKEN_SEPARATOR ).append( "VARYING" ); + } + + if( object.getLength() != null ) + { + builder.append( SQLConstants.OPEN_PARENTHESIS ).append( object.getLength() ) + .append( SQLConstants.CLOSE_PARENTHESIS ); + } + } + } + + public static class SQLFloatProcessor extends AbstractProcessor<SQLFloat> + { + + public SQLFloatProcessor() + { + super( SQLFloat.class ); + } + + @Override + protected void doProcess( SQLProcessorAggregator aggregator, SQLFloat object, StringBuilder builder ) + { + builder.append( "FLOAT" ); + if( object.getPrecision() != null ) + { + builder.append( SQLConstants.OPEN_PARENTHESIS ).append( object.getPrecision() ) + .append( SQLConstants.CLOSE_PARENTHESIS ); + } + } + } + + public static class SQLIntervalProcessor extends AbstractProcessor<SQLInterval> + { + + private final static Map<IntervalDataType, String> _defaultIntervalDataTypes; + + static + { + Map<IntervalDataType, String> map = new HashMap<IntervalDataType, String>(); + map.put( IntervalDataType.YEAR, "YEAR" ); + map.put( IntervalDataType.MONTH, "MONTH" ); + map.put( IntervalDataType.DAY, "DAY" ); + map.put( IntervalDataType.HOUR, "HOUR" ); + map.put( IntervalDataType.MINUTE, "MINUTE" ); + map.put( IntervalDataType.SECOND, "SECOND" ); + + _defaultIntervalDataTypes = map; + } + + private final Map<IntervalDataType, String> _intervalDataTypes; + + public SQLIntervalProcessor() + { + this( _defaultIntervalDataTypes ); + } + + public SQLIntervalProcessor( Map<IntervalDataType, String> intervalDataTypes ) + { + super( SQLInterval.class ); + + this._intervalDataTypes = intervalDataTypes; + } + + @Override + protected void doProcess( SQLProcessorAggregator aggregator, SQLInterval object, StringBuilder builder ) + { + builder.append( "INTERVAL" ).append( SQLConstants.TOKEN_SEPARATOR ) + .append( this._intervalDataTypes.get( object.getStartField() ) ); + + if( object.getStartFieldPrecision() != null ) + { + builder.append( SQLConstants.OPEN_PARENTHESIS ).append( object.getStartFieldPrecision() ); + + if( object.getEndField() == null && object.getSecondFracs() != null ) + { + builder.append( SQLConstants.COMMA ).append( SQLConstants.TOKEN_SEPARATOR ) + .append( object.getSecondFracs() ); + } + + builder.append( SQLConstants.CLOSE_PARENTHESIS ); + } + + if( object.getEndField() != null ) + { + builder.append( SQLConstants.TOKEN_SEPARATOR ).append( "TO" ).append( SQLConstants.TOKEN_SEPARATOR ) + .append( this._intervalDataTypes.get( object.getEndField() ) ); + + if( object.getSecondFracs() != null ) + { + builder.append( SQLConstants.OPEN_PARENTHESIS ).append( object.getSecondFracs() ) + .append( SQLConstants.CLOSE_PARENTHESIS ); + } + } + } + } + + public static class SQLTimeProcessor extends AbstractProcessor<SQLTime> + { + + public SQLTimeProcessor() + { + super( SQLTime.class ); + } + + @Override + protected void doProcess( SQLProcessorAggregator aggregator, SQLTime object, StringBuilder builder ) + { + builder.append( "TIME" ); + if( object.getPrecision() != null ) + { + builder.append( SQLConstants.OPEN_PARENTHESIS ).append( object.getPrecision() ) + .append( SQLConstants.CLOSE_PARENTHESIS ); + } + + if( object.isWithTimeZone() != null ) + { + builder.append( SQLConstants.TOKEN_SEPARATOR ).append( "WITH" ); + if( !object.isWithTimeZone() ) + { + builder.append( "OUT" ); + } + + builder.append( SQLConstants.TOKEN_SEPARATOR ).append( "TIME ZONE" ); + } + } + } + + public static class SQLTimeStampProcessor extends AbstractProcessor<SQLTimeStamp> + { + + public SQLTimeStampProcessor() + { + super( SQLTimeStamp.class ); + } + + @Override + protected void doProcess( SQLProcessorAggregator aggregator, SQLTimeStamp object, StringBuilder builder ) + { + builder.append( "TIMESTAMP" ); + if( object.getPrecision() != null ) + { + builder.append( SQLConstants.OPEN_PARENTHESIS ).append( object.getPrecision() ) + .append( SQLConstants.CLOSE_PARENTHESIS ); + } + + if( object.isWithTimeZone() != null ) + { + builder.append( SQLConstants.TOKEN_SEPARATOR ).append( "WITH" ); + if( !object.isWithTimeZone() ) + { + builder.append( "OUT" ); + } + + builder.append( SQLConstants.TOKEN_SEPARATOR ).append( "TIME ZONE" ); + } + } + } +}
