From ad17b3065c4d3831fd7a7b17d3cb1e793ff7a20c Mon Sep 17 00:00:00 2001 From: Benjamin Culkin Date: Mon, 27 May 2024 11:41:47 -0400 Subject: Initial import --- .../sqlelements/AbstractAliasedSQLElement.java | 64 ++++++++ .../org/israfil/sqlelements/AbstractCommand.java | 56 +++++++ .../java/org/israfil/sqlelements/AbstractJoin.java | 57 +++++++ .../sqlelements/AbstractParameterizedCommand.java | 80 ++++++++++ .../org/israfil/sqlelements/AbstractStatement.java | 68 ++++++++ .../org/israfil/sqlelements/AbstractTable.java | 48 ++++++ .../org/israfil/sqlelements/AliasedSQLElement.java | 42 +++++ .../main/java/org/israfil/sqlelements/Column.java | 45 ++++++ .../main/java/org/israfil/sqlelements/Command.java | 42 +++++ .../main/java/org/israfil/sqlelements/Delete.java | 79 +++++++++ .../java/org/israfil/sqlelements/InnerJoin.java | 73 +++++++++ .../main/java/org/israfil/sqlelements/Join.java | 45 ++++++ .../org/israfil/sqlelements/LeftOuterJoin.java | 56 +++++++ .../org/israfil/sqlelements/LiteralColumn.java | 71 +++++++++ .../israfil/sqlelements/ParameterizedCommand.java | 44 +++++ .../main/java/org/israfil/sqlelements/Query.java | 55 +++++++ .../java/org/israfil/sqlelements/SQLElement.java | 40 +++++ .../java/org/israfil/sqlelements/SQLParameter.java | 59 +++++++ .../main/java/org/israfil/sqlelements/Select.java | 177 +++++++++++++++++++++ .../java/org/israfil/sqlelements/SelectTable.java | 82 ++++++++++ .../java/org/israfil/sqlelements/SimpleColumn.java | 101 ++++++++++++ .../java/org/israfil/sqlelements/SimpleTable.java | 76 +++++++++ .../java/org/israfil/sqlelements/Statement.java | 51 ++++++ .../main/java/org/israfil/sqlelements/Table.java | 41 +++++ .../main/java/org/israfil/sqlelements/Update.java | 81 ++++++++++ .../org/israfil/sqlelements/constraints/And.java | 51 ++++++ .../constraints/ArbitraryStringConstraint.java | 57 +++++++ .../sqlelements/constraints/BetweenRange.java | 86 ++++++++++ .../sqlelements/constraints/BinaryConstraint.java | 62 ++++++++ .../sqlelements/constraints/ColumnConstraint.java | 65 ++++++++ .../sqlelements/constraints/ComplexConstraint.java | 43 +++++ .../sqlelements/constraints/Constraint.java | 42 +++++ .../sqlelements/constraints/ConstraintUtils.java | 48 ++++++ .../israfil/sqlelements/constraints/Equals.java | 45 ++++++ .../sqlelements/constraints/JoinConstraint.java | 56 +++++++ .../sqlelements/constraints/NaryConstraint.java | 93 +++++++++++ .../org/israfil/sqlelements/constraints/Not.java | 53 ++++++ .../israfil/sqlelements/constraints/NotEquals.java | 46 ++++++ .../org/israfil/sqlelements/constraints/Or.java | 51 ++++++ .../sqlelements/constraints/SimpleConstraint.java | 42 +++++ .../sqlelements/render/CommandRenderer.java | 44 +++++ .../israfil/sqlelements/render/DatabaseType.java | 119 ++++++++++++++ .../israfil/sqlelements/render/DateRenderer.java | 42 +++++ .../render/DelegatedSQLRenderEngine.java | 73 +++++++++ .../sqlelements/render/GenericSQLRenderEngine.java | 94 +++++++++++ .../sqlelements/render/MockSQLRenderEngine.java | 60 +++++++ .../sqlelements/render/MySqlSQLRenderEngine.java | 40 +++++ .../sqlelements/render/OracleSQLRenderEngine.java | 97 +++++++++++ .../israfil/sqlelements/render/QueryRenderer.java | 44 +++++ .../sqlelements/render/SQLRenderContext.java | 97 +++++++++++ 50 files changed, 3183 insertions(+) create mode 100644 sqlelements/src/main/java/org/israfil/sqlelements/AbstractAliasedSQLElement.java create mode 100644 sqlelements/src/main/java/org/israfil/sqlelements/AbstractCommand.java create mode 100644 sqlelements/src/main/java/org/israfil/sqlelements/AbstractJoin.java create mode 100644 sqlelements/src/main/java/org/israfil/sqlelements/AbstractParameterizedCommand.java create mode 100644 sqlelements/src/main/java/org/israfil/sqlelements/AbstractStatement.java create mode 100644 sqlelements/src/main/java/org/israfil/sqlelements/AbstractTable.java create mode 100644 sqlelements/src/main/java/org/israfil/sqlelements/AliasedSQLElement.java create mode 100644 sqlelements/src/main/java/org/israfil/sqlelements/Column.java create mode 100644 sqlelements/src/main/java/org/israfil/sqlelements/Command.java create mode 100644 sqlelements/src/main/java/org/israfil/sqlelements/Delete.java create mode 100644 sqlelements/src/main/java/org/israfil/sqlelements/InnerJoin.java create mode 100644 sqlelements/src/main/java/org/israfil/sqlelements/Join.java create mode 100644 sqlelements/src/main/java/org/israfil/sqlelements/LeftOuterJoin.java create mode 100644 sqlelements/src/main/java/org/israfil/sqlelements/LiteralColumn.java create mode 100644 sqlelements/src/main/java/org/israfil/sqlelements/ParameterizedCommand.java create mode 100644 sqlelements/src/main/java/org/israfil/sqlelements/Query.java create mode 100644 sqlelements/src/main/java/org/israfil/sqlelements/SQLElement.java create mode 100644 sqlelements/src/main/java/org/israfil/sqlelements/SQLParameter.java create mode 100644 sqlelements/src/main/java/org/israfil/sqlelements/Select.java create mode 100644 sqlelements/src/main/java/org/israfil/sqlelements/SelectTable.java create mode 100644 sqlelements/src/main/java/org/israfil/sqlelements/SimpleColumn.java create mode 100644 sqlelements/src/main/java/org/israfil/sqlelements/SimpleTable.java create mode 100644 sqlelements/src/main/java/org/israfil/sqlelements/Statement.java create mode 100644 sqlelements/src/main/java/org/israfil/sqlelements/Table.java create mode 100644 sqlelements/src/main/java/org/israfil/sqlelements/Update.java create mode 100644 sqlelements/src/main/java/org/israfil/sqlelements/constraints/And.java create mode 100644 sqlelements/src/main/java/org/israfil/sqlelements/constraints/ArbitraryStringConstraint.java create mode 100644 sqlelements/src/main/java/org/israfil/sqlelements/constraints/BetweenRange.java create mode 100644 sqlelements/src/main/java/org/israfil/sqlelements/constraints/BinaryConstraint.java create mode 100644 sqlelements/src/main/java/org/israfil/sqlelements/constraints/ColumnConstraint.java create mode 100644 sqlelements/src/main/java/org/israfil/sqlelements/constraints/ComplexConstraint.java create mode 100644 sqlelements/src/main/java/org/israfil/sqlelements/constraints/Constraint.java create mode 100644 sqlelements/src/main/java/org/israfil/sqlelements/constraints/ConstraintUtils.java create mode 100644 sqlelements/src/main/java/org/israfil/sqlelements/constraints/Equals.java create mode 100644 sqlelements/src/main/java/org/israfil/sqlelements/constraints/JoinConstraint.java create mode 100644 sqlelements/src/main/java/org/israfil/sqlelements/constraints/NaryConstraint.java create mode 100644 sqlelements/src/main/java/org/israfil/sqlelements/constraints/Not.java create mode 100644 sqlelements/src/main/java/org/israfil/sqlelements/constraints/NotEquals.java create mode 100644 sqlelements/src/main/java/org/israfil/sqlelements/constraints/Or.java create mode 100644 sqlelements/src/main/java/org/israfil/sqlelements/constraints/SimpleConstraint.java create mode 100644 sqlelements/src/main/java/org/israfil/sqlelements/render/CommandRenderer.java create mode 100644 sqlelements/src/main/java/org/israfil/sqlelements/render/DatabaseType.java create mode 100644 sqlelements/src/main/java/org/israfil/sqlelements/render/DateRenderer.java create mode 100644 sqlelements/src/main/java/org/israfil/sqlelements/render/DelegatedSQLRenderEngine.java create mode 100644 sqlelements/src/main/java/org/israfil/sqlelements/render/GenericSQLRenderEngine.java create mode 100644 sqlelements/src/main/java/org/israfil/sqlelements/render/MockSQLRenderEngine.java create mode 100644 sqlelements/src/main/java/org/israfil/sqlelements/render/MySqlSQLRenderEngine.java create mode 100644 sqlelements/src/main/java/org/israfil/sqlelements/render/OracleSQLRenderEngine.java create mode 100644 sqlelements/src/main/java/org/israfil/sqlelements/render/QueryRenderer.java create mode 100644 sqlelements/src/main/java/org/israfil/sqlelements/render/SQLRenderContext.java (limited to 'sqlelements/src/main/java') diff --git a/sqlelements/src/main/java/org/israfil/sqlelements/AbstractAliasedSQLElement.java b/sqlelements/src/main/java/org/israfil/sqlelements/AbstractAliasedSQLElement.java new file mode 100644 index 0000000..e534187 --- /dev/null +++ b/sqlelements/src/main/java/org/israfil/sqlelements/AbstractAliasedSQLElement.java @@ -0,0 +1,64 @@ +/* + * SQLElements object-oriented SQL parsing and generation library + * + * Copyright (c) 2003, 2004, 2005 Israfil Consulting Services Corporation + * Copyright (c) 2003, 2004, 2005 Christian Edward Gruber + * All Rights Reserved + * + * This library 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 2.1 of the License, or + * (at your option) any later version. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * 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. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library; if not, please visit http://www.gnu.org/licenses/lgpl.html + * + * $Id: AbstractAliasedSQLElement.java 16 2005-12-09 04:03:58Z cgruber $ + */ +package org.israfil.sqlelements; + +/** + * + * @author Christian Edward Gruber + * @author Latest: $Author: cgruber $ + * @version $Revision: 16 $ + */ +public abstract class AbstractAliasedSQLElement implements AliasedSQLElement { + protected String alias; + + public String getAlias() { + return alias; + } + + public void setAlias(String value) { + alias = value; + } + + public boolean hasAlias() { + return (alias != null && !alias.equals("")); + } + + public AbstractAliasedSQLElement() { + } + + public AbstractAliasedSQLElement(String alias) { + this.alias = alias; + } + + public void finalize() throws Throwable { + alias = null; + } + +} + diff --git a/sqlelements/src/main/java/org/israfil/sqlelements/AbstractCommand.java b/sqlelements/src/main/java/org/israfil/sqlelements/AbstractCommand.java new file mode 100644 index 0000000..2f8b412 --- /dev/null +++ b/sqlelements/src/main/java/org/israfil/sqlelements/AbstractCommand.java @@ -0,0 +1,56 @@ +/* + * SQLElements object-oriented SQL parsing and generation library + * + * Copyright (c) 2003, 2004, 2005 Israfil Consulting Services Corporation + * Copyright (c) 2003, 2004, 2005 Christian Edward Gruber + * All Rights Reserved + * + * This library 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 2.1 of the License, or + * (at your option) any later version. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * 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. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library; if not, please visit http://www.gnu.org/licenses/lgpl.html + * + * $Id: AbstractCommand.java 25 2005-12-15 23:36:26Z cgruber $ + */ +package org.israfil.sqlelements; + +import java.util.List; + +import org.israfil.sqlelements.constraints.Constraint; + +/** + * + * @author Christian Edward Gruber + * @author Latest: $Author: cgruber $ + * @version $Revision: 25 $ + */ +public abstract class AbstractCommand extends AbstractStatement implements Command { + + protected Table table = null; + public Table getTable() { return table; } + + public List getColumns() { return null; } + + protected AbstractCommand() { + super(); + } + + public AbstractCommand (Constraint constraint) { + super(constraint); + } + +} diff --git a/sqlelements/src/main/java/org/israfil/sqlelements/AbstractJoin.java b/sqlelements/src/main/java/org/israfil/sqlelements/AbstractJoin.java new file mode 100644 index 0000000..ffc0db8 --- /dev/null +++ b/sqlelements/src/main/java/org/israfil/sqlelements/AbstractJoin.java @@ -0,0 +1,57 @@ +/* + * SQLElements object-oriented SQL parsing and generation library + * + * Copyright (c) 2003, 2004, 2005 Israfil Consulting Services Corporation + * Copyright (c) 2003, 2004, 2005 Christian Edward Gruber + * All Rights Reserved + * + * This library 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 2.1 of the License, or + * (at your option) any later version. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * 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. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library; if not, please visit http://www.gnu.org/licenses/lgpl.html + * + * $Id: AbstractJoin.java 22 2005-12-11 15:08:56Z cgruber $ + */ +package org.israfil.sqlelements; + +import org.israfil.sqlelements.render.SQLRenderContext; + + +/** + * Represents a single column from a single (physical or virtual) table. + * + * @author Christian Edward Gruber + * @author Latest: $Author: cgruber $ + * @version $Revision: 22 $ + */ +public abstract class AbstractJoin implements Join { + protected Column left; + protected Column right; + + public Column getLeftColumn() { return this.left; } + public Column getRightColumn() { return this.right; } + + public AbstractJoin(Column left, Column right) + { + if (left.getTable() == null || right.getTable() == null) + throw new IllegalArgumentException("Cannot assign a Column with no Table to a Join"); + this.left = left; + this.right = right; + } + + public abstract String render(SQLRenderContext context); +} \ No newline at end of file diff --git a/sqlelements/src/main/java/org/israfil/sqlelements/AbstractParameterizedCommand.java b/sqlelements/src/main/java/org/israfil/sqlelements/AbstractParameterizedCommand.java new file mode 100644 index 0000000..6ba7d60 --- /dev/null +++ b/sqlelements/src/main/java/org/israfil/sqlelements/AbstractParameterizedCommand.java @@ -0,0 +1,80 @@ +/* + * SQLElements object-oriented SQL parsing and generation library + * + * Copyright (c) 2003, 2004, 2005 Israfil Consulting Services Corporation + * Copyright (c) 2003, 2004, 2005 Christian Edward Gruber + * All Rights Reserved + * + * This library 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 2.1 of the License, or + * (at your option) any later version. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * 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. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library; if not, please visit http://www.gnu.org/licenses/lgpl.html + * + * $Id: AbstractParameterizedCommand.java 27 2006-01-14 12:50:08Z cgruber $ + */ +package org.israfil.sqlelements; + +import java.util.Set; + +import net.israfil.foundation.collections.ArraySet; + +import org.israfil.sqlelements.constraints.Constraint; + +/** + * + * @author Christian Edward Gruber + * @author Latest: $Author: cgruber $ + * @version $Revision: 27 $ + */ +public abstract class AbstractParameterizedCommand extends AbstractCommand implements ParameterizedCommand { + + protected Set parameters = new ArraySet(); + public Set getParameters() { return parameters; } + + protected AbstractParameterizedCommand() { + super(); + } + + public AbstractParameterizedCommand (Constraint constraint, SQLParameter ... parameters) { + super(constraint); + setParameters(parameters); + } + + public void setParameters(SQLParameter ... parameters) + { + for (SQLParameter p : parameters) setParameter(p); + } + + public void setParameter(SQLParameter parameter) + { + if (parameter.column.getTable() == null) throw new IllegalArgumentException("Must supply column in parameters that contain a table."); + if (table == null) table = parameter.column.getTable(); + if (!table.equals(parameter.column.getTable())) throw new IllegalArgumentException("Must supply columns only from the same table."); + for (SQLParameter current : parameters) + if (current.column == parameter.column) parameters.remove(current); + parameters.add(parameter); + } + + public void setParameter(Column c,Object v) { + setParameter(new SQLParameter(c,v)); + } + + public Object getParameterValue(Column c) { + for (SQLParameter p : parameters) if (p.column == c) return p.value; + return null; + } +} diff --git a/sqlelements/src/main/java/org/israfil/sqlelements/AbstractStatement.java b/sqlelements/src/main/java/org/israfil/sqlelements/AbstractStatement.java new file mode 100644 index 0000000..6f8890b --- /dev/null +++ b/sqlelements/src/main/java/org/israfil/sqlelements/AbstractStatement.java @@ -0,0 +1,68 @@ +/* + * SQLElements object-oriented SQL parsing and generation library + * + * Copyright (c) 2003, 2004, 2005 Israfil Consulting Services Corporation + * Copyright (c) 2003, 2004, 2005 Christian Edward Gruber + * All Rights Reserved + * + * This library 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 2.1 of the License, or + * (at your option) any later version. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * 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. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library; if not, please visit http://www.gnu.org/licenses/lgpl.html + * + * $Id: AbstractStatement.java 25 2005-12-15 23:36:26Z cgruber $ + */ +package org.israfil.sqlelements; + +import org.israfil.sqlelements.constraints.Constraint; +import org.israfil.sqlelements.render.SQLRenderContext; + +/** + * Abstract Superclass containing some shared infrastructure for queries and + * commands. + * + * @author Christian Edward Gruber + * @author Latest: $Author: cgruber $ + * @version $Revision: 25 $ + */ +public abstract class AbstractStatement implements Statement { + + protected Constraint constraint; + + public boolean hasConstraint() { + return (constraint != null); + } + + public Constraint getConstraint() { + return this.constraint; + } + + public void setConstraint(Constraint value) { + this.constraint = value; + } + + protected AbstractStatement() { + } + + protected AbstractStatement(Constraint constraint) { + this.constraint = constraint; + } + + public abstract String render(SQLRenderContext context); + + public abstract Object clone() throws CloneNotSupportedException; +} diff --git a/sqlelements/src/main/java/org/israfil/sqlelements/AbstractTable.java b/sqlelements/src/main/java/org/israfil/sqlelements/AbstractTable.java new file mode 100644 index 0000000..a3b8f07 --- /dev/null +++ b/sqlelements/src/main/java/org/israfil/sqlelements/AbstractTable.java @@ -0,0 +1,48 @@ +/* + * SQLElements object-oriented SQL parsing and generation library + * + * Copyright (c) 2003, 2004, 2005 Israfil Consulting Services Corporation + * Copyright (c) 2003, 2004, 2005 Christian Edward Gruber + * All Rights Reserved + * + * This library 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 2.1 of the License, or + * (at your option) any later version. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * 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. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library; if not, please visit http://www.gnu.org/licenses/lgpl.html + * + * $Id: AbstractTable.java 16 2005-12-09 04:03:58Z cgruber $ + */ +package org.israfil.sqlelements; + +/** + * Abstract Superclass containing some shared infrastructure Table objects. + * + * @author Christian Edward Gruber + * @author Latest: $Author: cgruber $ + * @version $Revision: 16 $ + */ +public abstract class AbstractTable extends AbstractAliasedSQLElement implements Table { + + public AbstractTable(String alias) { + super(alias); + } + + public void setAlias(String value) { + this.alias = value; + } + +} diff --git a/sqlelements/src/main/java/org/israfil/sqlelements/AliasedSQLElement.java b/sqlelements/src/main/java/org/israfil/sqlelements/AliasedSQLElement.java new file mode 100644 index 0000000..f0828dd --- /dev/null +++ b/sqlelements/src/main/java/org/israfil/sqlelements/AliasedSQLElement.java @@ -0,0 +1,42 @@ +/* + * SQLElements object-oriented SQL parsing and generation library + * + * Copyright (c) 2003, 2004, 2005 Israfil Consulting Services Corporation + * Copyright (c) 2003, 2004, 2005 Christian Edward Gruber + * All Rights Reserved + * + * This library 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 2.1 of the License, or + * (at your option) any later version. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * 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. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library; if not, please visit http://www.gnu.org/licenses/lgpl.html + * + * $Id: AliasedSQLElement.java 16 2005-12-09 04:03:58Z cgruber $ + */ +package org.israfil.sqlelements; + +/** + * + * + * @author Christian Edward Gruber + * @author Latest: $Author: cgruber $ + * @version $Revision: 16 $ + */ +public interface AliasedSQLElement extends SQLElement { + public String getAlias(); + + public boolean hasAlias(); +} diff --git a/sqlelements/src/main/java/org/israfil/sqlelements/Column.java b/sqlelements/src/main/java/org/israfil/sqlelements/Column.java new file mode 100644 index 0000000..7f63664 --- /dev/null +++ b/sqlelements/src/main/java/org/israfil/sqlelements/Column.java @@ -0,0 +1,45 @@ +/* + * SQLElements object-oriented SQL parsing and generation library + * + * Copyright (c) 2003, 2004, 2005 Israfil Consulting Services Corporation + * Copyright (c) 2003, 2004, 2005 Christian Edward Gruber + * All Rights Reserved + * + * This library 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 2.1 of the License, or + * (at your option) any later version. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * 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. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library; if not, please visit http://www.gnu.org/licenses/lgpl.html + * + * $Id: Column.java 27 2006-01-14 12:50:08Z cgruber $ + */ +package org.israfil.sqlelements; + + +/** + * Represents a single column from a single (physical or virtual) table. + * + * @author Christian Edward Gruber + * @author Latest: $Author: cgruber $ + * @version $Revision: 27 $ + */ +public interface Column extends AliasedSQLElement +{ + public String getName(); + + public Table getTable(); + +} diff --git a/sqlelements/src/main/java/org/israfil/sqlelements/Command.java b/sqlelements/src/main/java/org/israfil/sqlelements/Command.java new file mode 100644 index 0000000..0ed6e69 --- /dev/null +++ b/sqlelements/src/main/java/org/israfil/sqlelements/Command.java @@ -0,0 +1,42 @@ +/* + * SQLElements object-oriented SQL parsing and generation library + * + * Copyright (c) 2003, 2004, 2005 Israfil Consulting Services Corporation + * Copyright (c) 2003, 2004, 2005 Christian Edward Gruber + * All Rights Reserved + * + * This library 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 2.1 of the License, or + * (at your option) any later version. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * 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. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library; if not, please visit http://www.gnu.org/licenses/lgpl.html + * + * $Id: Command.java 25 2005-12-15 23:36:26Z cgruber $ + */ +package org.israfil.sqlelements; + + +/** + * + * @author Christian Edward Gruber + * @author Latest: $Author: cgruber $ + * @version $Revision: 25 $ + */ +public interface Command extends Statement { + + public Table getTable(); + +} diff --git a/sqlelements/src/main/java/org/israfil/sqlelements/Delete.java b/sqlelements/src/main/java/org/israfil/sqlelements/Delete.java new file mode 100644 index 0000000..234dfce --- /dev/null +++ b/sqlelements/src/main/java/org/israfil/sqlelements/Delete.java @@ -0,0 +1,79 @@ +/* + * SQLElements object-oriented SQL parsing and generation library + * + * Copyright (c) 2003, 2004, 2005 Israfil Consulting Services Corporation + * Copyright (c) 2003, 2004, 2005 Christian Edward Gruber + * All Rights Reserved + * + * This library 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 2.1 of the License, or + * (at your option) any later version. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * 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. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library; if not, please visit http://www.gnu.org/licenses/lgpl.html + * + * $Id: Delete.java 25 2005-12-15 23:36:26Z cgruber $ + */ +package org.israfil.sqlelements; + +import org.israfil.sqlelements.constraints.Constraint; +import org.israfil.sqlelements.render.SQLRenderContext; + +/** + * + * @author Christian Edward Gruber + * @author Latest: $Author: cgruber $ + * @version $Revision: 25 $ + */ +public class Delete extends AbstractStatement implements Command +{ + protected Table table; + public Table getTable() { return table; } + + //public CommandType CommandType { get { return System.Data.CommandType.Text; } } + + public Delete() { + super(); + } + + public Delete (Table table, Constraint constraint) + { + super(constraint); + this.table = table; + } + + public Object clone() throws CloneNotSupportedException { + Delete delete = new Delete(); + delete.table = this.table; + delete.constraint = this.constraint; + return delete; + } + + public String render(SQLRenderContext context) + { + if (table == null) throw new IllegalStateException("Table cannot be null."); + String sql = "delete from "; + sql += table.render(context); + if (this.hasConstraint()) { + sql += " "; + sql += context.getAlias(table); + } + if (this.hasConstraint()) { + sql += " where "; + sql += this.constraint.render(context); + } + return sql; + } +} \ No newline at end of file diff --git a/sqlelements/src/main/java/org/israfil/sqlelements/InnerJoin.java b/sqlelements/src/main/java/org/israfil/sqlelements/InnerJoin.java new file mode 100644 index 0000000..9d122fe --- /dev/null +++ b/sqlelements/src/main/java/org/israfil/sqlelements/InnerJoin.java @@ -0,0 +1,73 @@ +/* + * SQLElements object-oriented SQL parsing and generation library + * + * Copyright (c) 2003, 2004, 2005 Israfil Consulting Services Corporation + * Copyright (c) 2003, 2004, 2005 Christian Edward Gruber + * All Rights Reserved + * + * This library 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 2.1 of the License, or + * (at your option) any later version. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * 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. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library; if not, please visit http://www.gnu.org/licenses/lgpl.html + * + * $Id: InnerJoin.java 22 2005-12-11 15:08:56Z cgruber $ + */ +package org.israfil.sqlelements; + +import org.israfil.sqlelements.render.SQLRenderContext; + +/** + * A Constraint wrapper around a Join object + * + * @author Christian Edward Gruber + * @author Latest: $Author: cgruber $ + * @version $Revision: 22 $ + */ +public class InnerJoin extends AbstractJoin { + + + /** + * This constructs an inner join based on equality of two + * columns. Note: each column must contain a Table reference + */ + public InnerJoin(Column left, Column right) { + super(left,right); + } + + /** + * This constructor creates an InnerJoin based on a column of + * the same name that occurs in two tables - specifically where + * they are equal. This is a convenience constructor which + * creates a Column object for the column name in each table. + * + * @param columnName + * @param left + * @param right + */ + public InnerJoin(String columnName,Table left,Table right) { + this(new SimpleColumn(left,columnName),new SimpleColumn(right,columnName)); + } + + public String render(SQLRenderContext context) { + + String sql = context.getAlias(left.getTable()) + "." + left.render(context); + sql += " = "; + sql += context.getAlias(right.getTable()) + "." + right.render(context); + return sql; + } + +} \ No newline at end of file diff --git a/sqlelements/src/main/java/org/israfil/sqlelements/Join.java b/sqlelements/src/main/java/org/israfil/sqlelements/Join.java new file mode 100644 index 0000000..14d0722 --- /dev/null +++ b/sqlelements/src/main/java/org/israfil/sqlelements/Join.java @@ -0,0 +1,45 @@ +/* + * SQLElements object-oriented SQL parsing and generation library + * + * Copyright (c) 2003, 2004, 2005 Israfil Consulting Services Corporation + * Copyright (c) 2003, 2004, 2005 Christian Edward Gruber + * All Rights Reserved + * + * This library 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 2.1 of the License, or + * (at your option) any later version. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * 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. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library; if not, please visit http://www.gnu.org/licenses/lgpl.html + * + * $Id: Join.java 22 2005-12-11 15:08:56Z cgruber $ + */ +package org.israfil.sqlelements; + +import org.israfil.sqlelements.render.SQLRenderContext; + +/** + * Represents a single column from a single (physical or virtual) table. + * + * @author Christian Edward Gruber + * @author Latest: $Author: cgruber $ + * @version $Revision: 22 $ + */ +public interface Join /*extends ComplexConstraint*/ +{ + public Column getLeftColumn(); + public Column getRightColumn(); + public String render(SQLRenderContext context); +} diff --git a/sqlelements/src/main/java/org/israfil/sqlelements/LeftOuterJoin.java b/sqlelements/src/main/java/org/israfil/sqlelements/LeftOuterJoin.java new file mode 100644 index 0000000..9765e59 --- /dev/null +++ b/sqlelements/src/main/java/org/israfil/sqlelements/LeftOuterJoin.java @@ -0,0 +1,56 @@ +/* + * SQLElements object-oriented SQL parsing and generation library + * + * Copyright (c) 2003, 2004, 2005 Israfil Consulting Services Corporation + * Copyright (c) 2003, 2004, 2005 Christian Edward Gruber + * All Rights Reserved + * + * This library 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 2.1 of the License, or + * (at your option) any later version. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * 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. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library; if not, please visit http://www.gnu.org/licenses/lgpl.html + * + * $Id: LeftOuterJoin.java 22 2005-12-11 15:08:56Z cgruber $ + */ +package org.israfil.sqlelements; + +import org.israfil.sqlelements.render.SQLRenderContext; + +/** + * Represents a single column from a single (physical or virtual) table. + * + * @author Christian Edward Gruber + * @author Latest: $Author: cgruber $ + * @version $Revision: 22 $ + */ +public class LeftOuterJoin extends AbstractJoin +{ + public LeftOuterJoin(Column left, Column right) { + super(left,right); + } + public LeftOuterJoin(String columnName,Table left,Table right) { + this(new SimpleColumn(left,columnName),new SimpleColumn(right,columnName)); + } + public String render(SQLRenderContext context) + { + String sql = context.getAlias(left.getTable()) + "." + left.render(context); + sql += " = "; + sql += context.getAlias(right.getTable()) + "." + right.render(context); + sql += " (+)"; + return sql; + } +} \ No newline at end of file diff --git a/sqlelements/src/main/java/org/israfil/sqlelements/LiteralColumn.java b/sqlelements/src/main/java/org/israfil/sqlelements/LiteralColumn.java new file mode 100644 index 0000000..5deac63 --- /dev/null +++ b/sqlelements/src/main/java/org/israfil/sqlelements/LiteralColumn.java @@ -0,0 +1,71 @@ +/* + * SQLElements object-oriented SQL parsing and generation library + * + * Copyright (c) 2003, 2004, 2005 Israfil Consulting Services Corporation + * Copyright (c) 2003, 2004, 2005 Christian Edward Gruber + * All Rights Reserved + * + * This library 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 2.1 of the License, or + * (at your option) any later version. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * 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. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library; if not, please visit http://www.gnu.org/licenses/lgpl.html + * + * $Id: LiteralColumn.java 25 2005-12-15 23:36:26Z cgruber $ + */ +package org.israfil.sqlelements; + +import java.util.Date; + +import org.israfil.sqlelements.render.DatabaseType; +import org.israfil.sqlelements.render.SQLRenderContext; + + +/** + * A simple implementation of a Column which takes a name and a table. The table + * can be any Table object. + * + * @author Christian Edward Gruber + * @author Latest: $Author: cgruber $ + * @version $Revision: 25 $ + */ +public class LiteralColumn extends AbstractAliasedSQLElement implements Column { + + public Table getTable() { return null; } + + protected Object literal; + public String getName() { return render(new SQLRenderContext(DatabaseType.Generic)); } + public Object getLiteral() { return literal; } + + public LiteralColumn(Object literal) { + super(); + this.literal = literal; + } + + public LiteralColumn(Object literal, String alias) + { + super(alias); + this.literal = literal; + } + + + public String render(SQLRenderContext context) { + if (literal == null) return "null"; + if (literal instanceof String || literal instanceof Enum) return context.getStringDelimiter() + literal + context.getStringDelimiter(); + if (literal instanceof Date) return context.format((Date)literal); + return literal.toString(); + } +} diff --git a/sqlelements/src/main/java/org/israfil/sqlelements/ParameterizedCommand.java b/sqlelements/src/main/java/org/israfil/sqlelements/ParameterizedCommand.java new file mode 100644 index 0000000..c173cd5 --- /dev/null +++ b/sqlelements/src/main/java/org/israfil/sqlelements/ParameterizedCommand.java @@ -0,0 +1,44 @@ +/* + * SQLElements object-oriented SQL parsing and generation library + * + * Copyright (c) 2003, 2004, 2005 Israfil Consulting Services Corporation + * Copyright (c) 2003, 2004, 2005 Christian Edward Gruber + * All Rights Reserved + * + * This library 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 2.1 of the License, or + * (at your option) any later version. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * 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. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library; if not, please visit http://www.gnu.org/licenses/lgpl.html + * + * $Id: ParameterizedCommand.java 25 2005-12-15 23:36:26Z cgruber $ + */ +package org.israfil.sqlelements; + +import java.util.Set; + +/** + * + * @author Christian Edward Gruber + * @author Latest: $Author: cgruber $ + * @version $Revision: 25 $ + */ +public interface ParameterizedCommand extends Command { + Set getParameters(); + void setParameters(SQLParameter ... parameters); + void setParameter(SQLParameter parameter); + Object getParameterValue(Column c); +} diff --git a/sqlelements/src/main/java/org/israfil/sqlelements/Query.java b/sqlelements/src/main/java/org/israfil/sqlelements/Query.java new file mode 100644 index 0000000..fff3411 --- /dev/null +++ b/sqlelements/src/main/java/org/israfil/sqlelements/Query.java @@ -0,0 +1,55 @@ +/* + * SQLElements object-oriented SQL parsing and generation library + * + * Copyright (c) 2003, 2004, 2005 Israfil Consulting Services Corporation + * Copyright (c) 2003, 2004, 2005 Christian Edward Gruber + * All Rights Reserved + * + * This library 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 2.1 of the License, or + * (at your option) any later version. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * 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. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library; if not, please visit http://www.gnu.org/licenses/lgpl.html + * + * $Id: Query.java 25 2005-12-15 23:36:26Z cgruber $ + */ +package org.israfil.sqlelements; + +import java.util.List; +import java.util.Set; + +import org.israfil.sqlelements.constraints.Constraint; + +/** + * + * + * @author Christian Edward Gruber + * @author Latest: $Author: cgruber $ + * @version $Revision: 25 $ + */ +public interface Query extends Statement { + + public List getColumns(); + + public Set getTables(); + + public boolean hasJoins(); + + public Set getJoinConstraints(); + + public Set getJoins(); + +} diff --git a/sqlelements/src/main/java/org/israfil/sqlelements/SQLElement.java b/sqlelements/src/main/java/org/israfil/sqlelements/SQLElement.java new file mode 100644 index 0000000..3d320c7 --- /dev/null +++ b/sqlelements/src/main/java/org/israfil/sqlelements/SQLElement.java @@ -0,0 +1,40 @@ +/* + * SQLElements object-oriented SQL parsing and generation library + * + * Copyright (c) 2003, 2004, 2005 Israfil Consulting Services Corporation + * Copyright (c) 2003, 2004, 2005 Christian Edward Gruber + * All Rights Reserved + * + * This library 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 2.1 of the License, or + * (at your option) any later version. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * 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. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library; if not, please visit http://www.gnu.org/licenses/lgpl.html + * + * $Id: SQLElement.java 16 2005-12-09 04:03:58Z cgruber $ + */ +package org.israfil.sqlelements; + +import org.israfil.sqlelements.render.SQLRenderContext; + +/** + * @author Christian Edward Gruber + * @author Latest: $Author: cgruber $ + * @version $Revision: 16 $ + */ +public interface SQLElement extends Cloneable { + public String render(SQLRenderContext context); +} diff --git a/sqlelements/src/main/java/org/israfil/sqlelements/SQLParameter.java b/sqlelements/src/main/java/org/israfil/sqlelements/SQLParameter.java new file mode 100644 index 0000000..4bd6614 --- /dev/null +++ b/sqlelements/src/main/java/org/israfil/sqlelements/SQLParameter.java @@ -0,0 +1,59 @@ +/* + * SQLElements object-oriented SQL parsing and generation library + * + * Copyright (c) 2003, 2004, 2005 Israfil Consulting Services Corporation + * Copyright (c) 2003, 2004, 2005 Christian Edward Gruber + * All Rights Reserved + * + * This library 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 2.1 of the License, or + * (at your option) any later version. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * 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. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library; if not, please visit http://www.gnu.org/licenses/lgpl.html + * + * $Id: SQLParameter.java 25 2005-12-15 23:36:26Z cgruber $ + */ +package org.israfil.sqlelements; + + +/** + * + * @author Christian Edward Gruber + * @author Latest: $Author: cgruber $ + * @version $Revision: 25 $ + */ +public class SQLParameter { + //public final static SQLParameter NULL = new SQLParameter(); + public final Column column; + public Object value; + + public SQLParameter(Column column) { + if (column == null) throw new IllegalArgumentException("SQLParameter column cannot be null."); + this.column = column; + this.value = null; + } + + public SQLParameter(Column column, Object val) { + if (column == null) throw new IllegalArgumentException("SQLParameter column cannot be null."); + this.column = column; + this.value = val; + } + + public String toString() { + return "SQLParameter[" + column + "," + value + "]"; + } + +} diff --git a/sqlelements/src/main/java/org/israfil/sqlelements/Select.java b/sqlelements/src/main/java/org/israfil/sqlelements/Select.java new file mode 100644 index 0000000..90a4946 --- /dev/null +++ b/sqlelements/src/main/java/org/israfil/sqlelements/Select.java @@ -0,0 +1,177 @@ +/* + * SQLElements object-oriented SQL parsing and generation library + * + * Copyright (c) 2003, 2004, 2005 Israfil Consulting Services Corporation + * Copyright (c) 2003, 2004, 2005 Christian Edward Gruber + * All Rights Reserved + * + * This library 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 2.1 of the License, or + * (at your option) any later version. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * 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. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library; if not, please visit http://www.gnu.org/licenses/lgpl.html + * + * $Id: Select.java 27 2006-01-14 12:50:08Z cgruber $ + */ +package org.israfil.sqlelements; + +import java.util.ArrayList; +import java.util.List; +import java.util.Set; + +import net.israfil.foundation.collections.ArraySet; +import net.israfil.foundation.collections.ArrayUtils; + +import org.israfil.sqlelements.constraints.Constraint; +import org.israfil.sqlelements.constraints.JoinConstraint; +import org.israfil.sqlelements.render.SQLRenderContext; + +/** + * + * + * @author Christian Edward Gruber + * @author Latest: $Author: cgruber $ + * @version $Revision: 27 $ + */ +public class Select extends AbstractStatement implements Query { + + + protected Set joins; + public boolean hasJoins() { return (joins != null && joins.size() > 0); } + public Set getJoins() { return this.joins; } + + /* + * Return the available joins as a set of constraints. + * + * @return getJoinsAsConstraints a Set containing current joins. + */ + public Set getJoinConstraints() { + + //Implementation notes: + // + //For some crazy reason, you can't cast the following: + //
+		//class C {}
+		//class D extends C {}
+		//public void example() {
+		//    Set d = new HashSet();
+		//    Set c = d; 
+		//}
+		//
+ // + //The above code will fail on a conversion error, even + //though semantically, it can be guarranteed that Set isa Set. + //The problem is likely that if you allowed Set to be assigned to + //Set, and then one attempted to add an object of type E, where + //E extends C, it would fail, violating Set's contract. + // + //So we just re-wrap the joins in a Set + + Set jc = new ArraySet(); + for (Join j : joins) { + jc.add(new JoinConstraint(j)); + } + return jc; + } + + protected Select() { + super(); + } + + public Select (Column column, Join ... joins) { + this(new Column[] { column },joins); + } + + public Select (Column[] columns, Join ... joins) { + this(columns,null,joins); + } + + public Select (Column column, Constraint constraint, Join ... joins) { + this(new Column[] { column },constraint,joins); + } + + public Select (Column[] columns, Constraint constraint, Join ... joins) { + super(constraint); + this.addColumns(columns); + this.addJoins(joins); + } + + protected List columns; + public List getColumns() { return this.columns; } + + public Set
getTables() { + Set
tables = new ArraySet
(); + for (Column c : this.columns) { + Table table = c.getTable(); + if (table != null) tables.add(table); + } + for (Join j : this.joins) { + if (j.getLeftColumn().getTable() != null) tables.add(j.getLeftColumn().getTable()); + if (j.getRightColumn().getTable() != null) tables.add(j.getRightColumn().getTable()); + } + return tables; + } + + public void addJoins(Join ... newJoins) { + if (this.joins == null) this.joins = new ArraySet(); + for (Join j : newJoins) { + this.joins.add(j); + } + } + + public void addJoins(Set joins) { + if (this.joins == null) this.joins = new ArraySet(); + for (Join j : joins) { + this.joins.add(j); + } + } + + public Object clone() throws CloneNotSupportedException { + Select newSelect = new Select(); + newSelect.addColumns(this.columns); + newSelect.addJoins(this.joins); + newSelect.constraint = this.constraint; + return newSelect; + } + + + + public void addColumns(Column ... cols) { + if (this.columns == null) this.columns = new ArrayList(); + for (Column c : cols) { + this.columns.add(c); + } + } + public void addColumns(List cols) { + if (this.columns == null) this.columns = new ArrayList(); + for (Column c : cols) { + this.columns.add(c); + } + } + + protected void clearColumns() { + columns = new ArrayList(); + } + + public String render(SQLRenderContext context) { + return context.renderQuery(this); + } + + public static Column[] aggregate(Column[] ... columns) { + return ArrayUtils.aggregate(new Column[0],columns); + } +} + diff --git a/sqlelements/src/main/java/org/israfil/sqlelements/SelectTable.java b/sqlelements/src/main/java/org/israfil/sqlelements/SelectTable.java new file mode 100644 index 0000000..7adcaf3 --- /dev/null +++ b/sqlelements/src/main/java/org/israfil/sqlelements/SelectTable.java @@ -0,0 +1,82 @@ +/* + * SQLElements object-oriented SQL parsing and generation library + * + * Copyright (c) 2003, 2004, 2005 Israfil Consulting Services Corporation + * Copyright (c) 2003, 2004, 2005 Christian Edward Gruber + * All Rights Reserved + * + * This library 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 2.1 of the License, or + * (at your option) any later version. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * 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. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library; if not, please visit http://www.gnu.org/licenses/lgpl.html + * + * $Id: SelectTable.java 17 2005-12-10 20:06:44Z cgruber $ + */ +package org.israfil.sqlelements; + +import org.israfil.sqlelements.render.SQLRenderContext; + +/** + * A Table implementation that wraps a sub-select. Used to implement sql such as + * "select * from at_job_wk_registry where job_id = (select max(job_id) from at_job)". + * the inner select would be represented via a SelectTable object. + * + * @author Christian Edward Gruber + * @author Latest: $Author: cgruber $ + * @version $Revision: 17 $ + */ + +public class SelectTable extends AbstractTable { + /// The inner query. + public final Query query; + + /// Constructor + public SelectTable(Query query) { + this(query,null); + } + + /// Constructor + public SelectTable(Query query, String alias) { + super(alias); + this.query=query; + } + + /** + * Pretty printed string representation of the object + */ + @Override + public String toString() + { + return "SelectTable[" + query.toString() + "]"; + } + + /** Implementation of Object.clone() */ + @Override + public Object clone() throws CloneNotSupportedException { + SelectTable st = new SelectTable((Query)this.query.clone()); + if (this.hasAlias()) st.setAlias(this.getAlias()); + return st; + } + + /** @see SQLElement.render(SQLRenderContext) */ + public String render(SQLRenderContext context) + { + if (alias == null && "".equalsIgnoreCase(alias)) return "( " + query.render(context) + ")"; + else return "( " + query.render(context) + ") " + alias; + } + +} \ No newline at end of file diff --git a/sqlelements/src/main/java/org/israfil/sqlelements/SimpleColumn.java b/sqlelements/src/main/java/org/israfil/sqlelements/SimpleColumn.java new file mode 100644 index 0000000..d5919aa --- /dev/null +++ b/sqlelements/src/main/java/org/israfil/sqlelements/SimpleColumn.java @@ -0,0 +1,101 @@ +/* + * SQLElements object-oriented SQL parsing and generation library + * + * Copyright (c) 2003, 2004, 2005 Israfil Consulting Services Corporation + * Copyright (c) 2003, 2004, 2005 Christian Edward Gruber + * All Rights Reserved + * + * This library 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 2.1 of the License, or + * (at your option) any later version. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * 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. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library; if not, please visit http://www.gnu.org/licenses/lgpl.html + * + * $Id: SimpleColumn.java 25 2005-12-15 23:36:26Z cgruber $ + */ +package org.israfil.sqlelements; + +import org.israfil.sqlelements.render.SQLRenderContext; + +/** + * A simple implementation of a Column which takes a name and a table. The table + * can be any Table object. + * + * @author Christian Edward Gruber + * @author Latest: $Author: cgruber $ + * @version $Revision: 25 $ + */ +public class SimpleColumn extends AbstractAliasedSQLElement implements Column +{ + + protected Table table; + public Table getTable() { return table; } + + protected String name; + public String getName() { return name; } + + /** + * Constructs a Column implementation instance + * + * @param table A Table object of which this Column is a field. + * @param colName The name of this column + */ + public SimpleColumn(Table table, String colName) { + this(table,colName,null); + } + + /** + * Constructs a Column implementation instance + * + * @param table A Table object of which this Column is a field. + * @param colName The name of this column + * @param alias An alternate designation for this column - usually shorter + */ + public SimpleColumn(Table table, String colName, String alias) + { + super(alias); + this.table = table; + this.name = colName; + } + + public String render(SQLRenderContext context) + { + return this.name; + } + + /** + * Convenience method to create batches of SimpleColumns from an array + * of names. Note: this cannot be used to create SimpleColumns with + * aliases. + * + * @param table A table, within whom the columns will be created. + * @param names An array of column names + * @return an array of SimpleColumns + */ + public static SimpleColumn[] createColumns(Table table, String ... names) + { + SimpleColumn[] result = new SimpleColumn[names.length]; + for (int i = 0; i < names.length; i++) + result[i] = new SimpleColumn(table,names[i]); + return result; + } + + public String toString() + { + return "SimpleColumn[" + table + "," + name + "]"; + } + +} diff --git a/sqlelements/src/main/java/org/israfil/sqlelements/SimpleTable.java b/sqlelements/src/main/java/org/israfil/sqlelements/SimpleTable.java new file mode 100644 index 0000000..722a3d0 --- /dev/null +++ b/sqlelements/src/main/java/org/israfil/sqlelements/SimpleTable.java @@ -0,0 +1,76 @@ +/* + * SQLElements object-oriented SQL parsing and generation library + * + * Copyright (c) 2003, 2004, 2005 Israfil Consulting Services Corporation + * Copyright (c) 2003, 2004, 2005 Christian Edward Gruber + * All Rights Reserved + * + * This library 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 2.1 of the License, or + * (at your option) any later version. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * 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. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library; if not, please visit http://www.gnu.org/licenses/lgpl.html + * + * $Id: SimpleTable.java 16 2005-12-09 04:03:58Z cgruber $ + */ +package org.israfil.sqlelements; + +import org.israfil.sqlelements.render.SQLRenderContext; + +/** + * An object representation of a simple table from which columns can be + * selected. + * + * + * @author Christian Edward Gruber + * @author Latest: $Author: cgruber $ + * @version $Revision: 16 $ + */ +public class SimpleTable extends AbstractTable { + + protected String name; + + public SimpleTable(String name) { + this(name, null); + } + + public SimpleTable(String name, String alias) { + super(alias); + this.name = name; + } + + public String getName() { + return name; + } + + public String toString() { + if (this.hasAlias()) + return "SimpleTable[" + name + " " + alias +"]"; + else return "SimpleTable[" + name + "]"; + } + + public Object clone() { + SimpleTable st = new SimpleTable(this.name); + if (this.hasAlias()) + st.alias = this.alias; + return st; + } + + public String render(SQLRenderContext context) { + return name; + } + +} diff --git a/sqlelements/src/main/java/org/israfil/sqlelements/Statement.java b/sqlelements/src/main/java/org/israfil/sqlelements/Statement.java new file mode 100644 index 0000000..eddfcac --- /dev/null +++ b/sqlelements/src/main/java/org/israfil/sqlelements/Statement.java @@ -0,0 +1,51 @@ +/* + * SQLElements object-oriented SQL parsing and generation library + * + * Copyright (c) 2003, 2004, 2005 Israfil Consulting Services Corporation + * Copyright (c) 2003, 2004, 2005 Christian Edward Gruber + * All Rights Reserved + * + * This library 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 2.1 of the License, or + * (at your option) any later version. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * 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. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library; if not, please visit http://www.gnu.org/licenses/lgpl.html + * + * $Id: Statement.java 17 2005-12-10 20:06:44Z cgruber $ + */ +package org.israfil.sqlelements; + +import org.israfil.sqlelements.constraints.Constraint; + +/** + * A SQLElement containing the elements of a SQL query or command + * + * @author Christian Edward Gruber + * @author Latest: $Author: cgruber $ + * @version $Revision: 17 $ + */ + +public interface Statement extends SQLElement, Cloneable { + + public boolean hasConstraint(); + + public Constraint getConstraint(); + + public void setConstraint(Constraint c); + + public Object clone() throws CloneNotSupportedException; + +} diff --git a/sqlelements/src/main/java/org/israfil/sqlelements/Table.java b/sqlelements/src/main/java/org/israfil/sqlelements/Table.java new file mode 100644 index 0000000..a4f721c --- /dev/null +++ b/sqlelements/src/main/java/org/israfil/sqlelements/Table.java @@ -0,0 +1,41 @@ +/* + * SQLElements object-oriented SQL parsing and generation library + * + * Copyright (c) 2003, 2004, 2005 Israfil Consulting Services Corporation + * Copyright (c) 2003, 2004, 2005 Christian Edward Gruber + * All Rights Reserved + * + * This library 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 2.1 of the License, or + * (at your option) any later version. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * 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. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library; if not, please visit http://www.gnu.org/licenses/lgpl.html + * + * $Id: Table.java 16 2005-12-09 04:03:58Z cgruber $ + */ +package org.israfil.sqlelements; + +/** + * + * @author Christian Edward Gruber + * + * @author Christian Edward Gruber + * @author Latest: $Author: cgruber $ + * @version $Revision: 16 $ + */ +public interface Table extends AliasedSQLElement, Cloneable { + public void setAlias(String alias); +} diff --git a/sqlelements/src/main/java/org/israfil/sqlelements/Update.java b/sqlelements/src/main/java/org/israfil/sqlelements/Update.java new file mode 100644 index 0000000..21fa01b --- /dev/null +++ b/sqlelements/src/main/java/org/israfil/sqlelements/Update.java @@ -0,0 +1,81 @@ +/* + * SQLElements object-oriented SQL parsing and generation library + * + * Copyright (c) 2003, 2004, 2005 Israfil Consulting Services Corporation + * Copyright (c) 2003, 2004, 2005 Christian Edward Gruber + * All Rights Reserved + * + * This library 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 2.1 of the License, or + * (at your option) any later version. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * 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. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library; if not, please visit http://www.gnu.org/licenses/lgpl.html + * + * $Id: Update.java 27 2006-01-14 12:50:08Z cgruber $ + */ +package org.israfil.sqlelements; + +import net.israfil.foundation.collections.ArraySet; + +import org.israfil.sqlelements.constraints.Constraint; +import org.israfil.sqlelements.render.SQLRenderContext; + +/** + * + * @author Christian Edward Gruber + * @author Latest: $Author: cgruber $ + * @version $Revision: 27 $ + */ +public class Update extends AbstractParameterizedCommand { + public Update() { + super(); + } + public Update(SQLParameter ... parameters) { + this(null,parameters); + } + public Update (Constraint constraint, SQLParameter ... parameters) { + super(constraint,parameters); + } + + public Object clone() throws CloneNotSupportedException { + Update update = new Update(); + update.constraint = this.constraint; + update.parameters = new ArraySet(this.parameters); + return update; + } + + public String render(SQLRenderContext context) + { + if (table == null) throw new IllegalStateException("Table cannot be null."); + String sql = "update "; + sql += table.render(context); + if (this.hasConstraint()) { + sql += " "; + sql += context.getAlias(table); + } + for (SQLParameter pm : parameters) { + sql += " set "; + sql += pm.column.render(context); + sql += " = "; + sql += new LiteralColumn(pm.value).render(context); + } + if (this.hasConstraint()) { + sql += " where "; + sql += this.constraint.render(context); + } + return sql; + } +} diff --git a/sqlelements/src/main/java/org/israfil/sqlelements/constraints/And.java b/sqlelements/src/main/java/org/israfil/sqlelements/constraints/And.java new file mode 100644 index 0000000..14d82e1 --- /dev/null +++ b/sqlelements/src/main/java/org/israfil/sqlelements/constraints/And.java @@ -0,0 +1,51 @@ +/* + * SQLElements object-oriented SQL parsing and generation library + * + * Copyright (c) 2003, 2004, 2005 Israfil Consulting Services Corporation + * Copyright (c) 2003, 2004, 2005 Christian Edward Gruber + * All Rights Reserved + * + * This library 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 2.1 of the License, or + * (at your option) any later version. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * 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. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library; if not, please visit http://www.gnu.org/licenses/lgpl.html + * + * $Id: And.java 18 2005-12-10 20:08:16Z cgruber $ + */ +package org.israfil.sqlelements.constraints; + +import java.util.Set; + + +/** + * Represents an AND operator in a SQL WHERE clause. + * + * @author Christian Edward Gruber + * @author Latest: $Author: cgruber $ + * @version $Revision: 18 $ + */ +public class And extends NaryConstraint { + + public And(Constraint ... constraints) { + super("and",constraints); + } + + public And(Set constraints) { + super("and",constraints); + } + +} diff --git a/sqlelements/src/main/java/org/israfil/sqlelements/constraints/ArbitraryStringConstraint.java b/sqlelements/src/main/java/org/israfil/sqlelements/constraints/ArbitraryStringConstraint.java new file mode 100644 index 0000000..cb3ddfc --- /dev/null +++ b/sqlelements/src/main/java/org/israfil/sqlelements/constraints/ArbitraryStringConstraint.java @@ -0,0 +1,57 @@ +/* + * SQLElements object-oriented SQL parsing and generation library + * + * Copyright (c) 2003, 2004, 2005 Israfil Consulting Services Corporation + * Copyright (c) 2003, 2004, 2005 Christian Edward Gruber + * All Rights Reserved + * + * This library 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 2.1 of the License, or + * (at your option) any later version. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * 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. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library; if not, please visit http://www.gnu.org/licenses/lgpl.html + * + * $Id: ArbitraryStringConstraint.java 16 2005-12-09 04:03:58Z cgruber $ + */ +package org.israfil.sqlelements.constraints; + +import org.israfil.sqlelements.render.SQLRenderContext; + + +/** + * Represents an AND operator in a SQL WHERE clause. + * + * @author Christian Edward Gruber + * @author Latest: $Author: cgruber $ + * @version $Revision: 16 $ + */ +/// +/// Constraints that contain things other than constraints (such +/// as columns or literals) and which need no enclosing parentheses +/// if rendered within a complex +/// +public class ArbitraryStringConstraint implements SimpleConstraint +{ + public final String text; + public ArbitraryStringConstraint(String constraintText) + { + this.text = constraintText; + } + public String render(SQLRenderContext context) + { + return text; + } +} diff --git a/sqlelements/src/main/java/org/israfil/sqlelements/constraints/BetweenRange.java b/sqlelements/src/main/java/org/israfil/sqlelements/constraints/BetweenRange.java new file mode 100644 index 0000000..51918b5 --- /dev/null +++ b/sqlelements/src/main/java/org/israfil/sqlelements/constraints/BetweenRange.java @@ -0,0 +1,86 @@ +/* + * SQLElements object-oriented SQL parsing and generation library + * + * Copyright (c) 2003, 2004, 2005 Israfil Consulting Services Corporation + * Copyright (c) 2003, 2004, 2005 Christian Edward Gruber + * All Rights Reserved + * + * This library 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 2.1 of the License, or + * (at your option) any later version. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * 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. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library; if not, please visit http://www.gnu.org/licenses/lgpl.html + * + * $Id: BetweenRange.java 25 2005-12-15 23:36:26Z cgruber $ + */ +package org.israfil.sqlelements.constraints; + +import org.israfil.sqlelements.Column; +import org.israfil.sqlelements.render.SQLRenderContext; + + +/** + * A range comparison constraint. + * + * @author Christian Edward Gruber + * @author Latest: $Author: cgruber $ + * @version $Revision: 25 $ + */ +public class BetweenRange implements ComplexConstraint { + private Column operand; + private Column begin; + private Column end; + + public BetweenRange(Column operand, Column beginRange, Column endRange) { + this.operand = operand; + this.begin = beginRange; + this.end = endRange; + } + + /** + * Renders the BetweenRange. + * TODO: Migrate this into SQLRenderContext, as some SQL variants support BETWEEN syntax + */ + public String render(SQLRenderContext context) + { + + String sql = ""; + if (operand.getTable() != null) sql += context.getAlias(operand.getTable()) + "."; + sql += operand.render(context); + sql += " >= "; + if (begin.getTable() != null) sql += context.getAlias(begin.getTable()) + "."; + sql += begin.render(context); + sql += " and "; + if (operand.getTable() != null) sql += context.getAlias(operand.getTable()) + "."; + sql += operand.render(context); + sql += " <= "; + if (end.getTable() != null) sql += context.getAlias(end.getTable()) + "."; + sql += end.render(context); + return sql; + /* + String sql = ""; + if (operand.getTable() != null) sql += context.getAlias(operand.getTable()) + "."; + sql += operand.render(context); + sql += " between "; + if (begin.getTable() != null) sql += context.getAlias(begin.getTable()) + "."; + sql += begin.render(context); + sql += " and "; + if (end.getTable() != null) sql += context.getAlias(end.getTable()) + "."; + sql += end.render(context); + return sql; + */ + } +} diff --git a/sqlelements/src/main/java/org/israfil/sqlelements/constraints/BinaryConstraint.java b/sqlelements/src/main/java/org/israfil/sqlelements/constraints/BinaryConstraint.java new file mode 100644 index 0000000..c3b990c --- /dev/null +++ b/sqlelements/src/main/java/org/israfil/sqlelements/constraints/BinaryConstraint.java @@ -0,0 +1,62 @@ +/* + * SQLElements object-oriented SQL parsing and generation library + * + * Copyright (c) 2003, 2004, 2005 Israfil Consulting Services Corporation + * Copyright (c) 2003, 2004, 2005 Christian Edward Gruber + * All Rights Reserved + * + * This library 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 2.1 of the License, or + * (at your option) any later version. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * 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. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library; if not, please visit http://www.gnu.org/licenses/lgpl.html + * + * $Id: BinaryConstraint.java 16 2005-12-09 04:03:58Z cgruber $ + */ +package org.israfil.sqlelements.constraints; + +import org.israfil.sqlelements.render.SQLRenderContext; + +/** + * A friendly parent for binary column constraints. + * + * @author Christian Edward Gruber + * @author Latest: $Author: cgruber $ + * @version $Revision: 16 $ + */ +public abstract class BinaryConstraint implements ComplexConstraint +{ + protected Constraint leftOperand; + protected Constraint rightOperand; + protected String operatorText; + + protected BinaryConstraint(Constraint leftOperand,Constraint rightOperand, String operatorText) + { + this.leftOperand = leftOperand; + this.rightOperand = rightOperand; + this.operatorText = operatorText; + } + public String render(SQLRenderContext context) + { + String sql = ConstraintUtils.renderInParentheses(leftOperand,context); + sql += " "; + sql += operatorText; + sql += " "; + sql += ConstraintUtils.renderInParentheses(rightOperand,context); + return sql; + } +} + diff --git a/sqlelements/src/main/java/org/israfil/sqlelements/constraints/ColumnConstraint.java b/sqlelements/src/main/java/org/israfil/sqlelements/constraints/ColumnConstraint.java new file mode 100644 index 0000000..1feae0b --- /dev/null +++ b/sqlelements/src/main/java/org/israfil/sqlelements/constraints/ColumnConstraint.java @@ -0,0 +1,65 @@ +/* + * SQLElements object-oriented SQL parsing and generation library + * + * Copyright (c) 2003, 2004, 2005 Israfil Consulting Services Corporation + * Copyright (c) 2003, 2004, 2005 Christian Edward Gruber + * All Rights Reserved + * + * This library 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 2.1 of the License, or + * (at your option) any later version. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * 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. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library; if not, please visit http://www.gnu.org/licenses/lgpl.html + * + * $Id: ColumnConstraint.java 21 2005-12-10 21:40:45Z cgruber $ + */ +package org.israfil.sqlelements.constraints; + +import org.israfil.sqlelements.Column; +import org.israfil.sqlelements.render.SQLRenderContext; + + +/** + * A constraint wrapper for a column. It's used internally by + * the library to ease implementation of column-oriented + * constraints such as "Equals", etc. + * + * @see BinaryConstraint + * @see Equals + * + * @author Christian Edward Gruber + * @author Latest: $Author: cgruber $ + * @version $Revision: 21 $ + */ +class ColumnConstraint implements SimpleConstraint +{ + Column column; + public Column getColumn() { return column; } + + public ColumnConstraint(Column column) + { + this.column = column; + } + + public String render(SQLRenderContext context) + { + if (column.getTable() != null) + { + return context.getAlias(column.getTable()) + "." + column.render(context); + } + else return column.render(context); + } +} \ No newline at end of file diff --git a/sqlelements/src/main/java/org/israfil/sqlelements/constraints/ComplexConstraint.java b/sqlelements/src/main/java/org/israfil/sqlelements/constraints/ComplexConstraint.java new file mode 100644 index 0000000..91960e1 --- /dev/null +++ b/sqlelements/src/main/java/org/israfil/sqlelements/constraints/ComplexConstraint.java @@ -0,0 +1,43 @@ +/* + * SQLElements object-oriented SQL parsing and generation library + * + * Copyright (c) 2003, 2004, 2005 Israfil Consulting Services Corporation + * Copyright (c) 2003, 2004, 2005 Christian Edward Gruber + * All Rights Reserved + * + * This library 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 2.1 of the License, or + * (at your option) any later version. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * 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. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library; if not, please visit http://www.gnu.org/licenses/lgpl.html + * + * $Id: ComplexConstraint.java 16 2005-12-09 04:03:58Z cgruber $ + */ +package org.israfil.sqlelements.constraints; + + +/** + * Represents a constraint that would need to be enclosed by parentheses. + * + * @author Christian Edward Gruber + * @author Latest: $Author: cgruber $ + * @version $Revision: 16 $ + */ +public interface ComplexConstraint extends Constraint +{ + +} + diff --git a/sqlelements/src/main/java/org/israfil/sqlelements/constraints/Constraint.java b/sqlelements/src/main/java/org/israfil/sqlelements/constraints/Constraint.java new file mode 100644 index 0000000..3de2fed --- /dev/null +++ b/sqlelements/src/main/java/org/israfil/sqlelements/constraints/Constraint.java @@ -0,0 +1,42 @@ +/* + * SQLElements object-oriented SQL parsing and generation library + * + * Copyright (c) 2003, 2004, 2005 Israfil Consulting Services Corporation + * Copyright (c) 2003, 2004, 2005 Christian Edward Gruber + * All Rights Reserved + * + * This library 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 2.1 of the License, or + * (at your option) any later version. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * 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. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library; if not, please visit http://www.gnu.org/licenses/lgpl.html + * + * $Id: Constraint.java 16 2005-12-09 04:03:58Z cgruber $ + */ +package org.israfil.sqlelements.constraints; + +import org.israfil.sqlelements.SQLElement; + +/** + * Represents a constraint specification for a request for data. + * + * @author Christian Edward Gruber + * @author Latest: $Author: cgruber $ + * @version $Revision: 16 $ + */ +public interface Constraint extends SQLElement { +} + diff --git a/sqlelements/src/main/java/org/israfil/sqlelements/constraints/ConstraintUtils.java b/sqlelements/src/main/java/org/israfil/sqlelements/constraints/ConstraintUtils.java new file mode 100644 index 0000000..9828461 --- /dev/null +++ b/sqlelements/src/main/java/org/israfil/sqlelements/constraints/ConstraintUtils.java @@ -0,0 +1,48 @@ +/* + * SQLElements object-oriented SQL parsing and generation library + * + * Copyright (c) 2003, 2004, 2005 Israfil Consulting Services Corporation + * Copyright (c) 2003, 2004, 2005 Christian Edward Gruber + * All Rights Reserved + * + * This library 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 2.1 of the License, or + * (at your option) any later version. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * 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. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library; if not, please visit http://www.gnu.org/licenses/lgpl.html + * + * $Id: ConstraintUtils.java 16 2005-12-09 04:03:58Z cgruber $ + */ +package org.israfil.sqlelements.constraints; + +import org.israfil.sqlelements.render.SQLRenderContext; + +/** + * Represents a constraint specification for a request for data. + * + * @author Christian Edward Gruber + * @author Latest: $Author: cgruber $ + * @version $Revision: 16 $ + */ +public class ConstraintUtils +{ + public static String renderInParentheses(Constraint c,SQLRenderContext context) + { + if (c instanceof ComplexConstraint) return "(" + c.render(context) + ")"; + else return c.render(context); + } +} + diff --git a/sqlelements/src/main/java/org/israfil/sqlelements/constraints/Equals.java b/sqlelements/src/main/java/org/israfil/sqlelements/constraints/Equals.java new file mode 100644 index 0000000..3877231 --- /dev/null +++ b/sqlelements/src/main/java/org/israfil/sqlelements/constraints/Equals.java @@ -0,0 +1,45 @@ +/* + * SQLElements object-oriented SQL parsing and generation library + * + * Copyright (c) 2003, 2004, 2005 Israfil Consulting Services Corporation + * Copyright (c) 2003, 2004, 2005 Christian Edward Gruber + * All Rights Reserved + * + * This library 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 2.1 of the License, or + * (at your option) any later version. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * 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. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library; if not, please visit http://www.gnu.org/licenses/lgpl.html + * + * $Id: Equals.java 21 2005-12-10 21:40:45Z cgruber $ + */ +package org.israfil.sqlelements.constraints; + +import org.israfil.sqlelements.Column; + + +/** + * A friendly Column Equality constraint. + * + * @author Christian Edward Gruber + * @author Latest: $Author: cgruber $ + * @version $Revision: 21 $ + */ +public class Equals extends BinaryConstraint { + public Equals(Column leftOperand,Column rightOperand) { + super(new ColumnConstraint(leftOperand),new ColumnConstraint(rightOperand), "=") ; + } +} diff --git a/sqlelements/src/main/java/org/israfil/sqlelements/constraints/JoinConstraint.java b/sqlelements/src/main/java/org/israfil/sqlelements/constraints/JoinConstraint.java new file mode 100644 index 0000000..84d07bc --- /dev/null +++ b/sqlelements/src/main/java/org/israfil/sqlelements/constraints/JoinConstraint.java @@ -0,0 +1,56 @@ +/* + * SQLElements object-oriented SQL parsing and generation library + * + * Copyright (c) 2003, 2004, 2005 Israfil Consulting Services Corporation + * Copyright (c) 2003, 2004, 2005 Christian Edward Gruber + * All Rights Reserved + * + * This library 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 2.1 of the License, or + * (at your option) any later version. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * 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. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library; if not, please visit http://www.gnu.org/licenses/lgpl.html + * + * $Id: JoinConstraint.java 22 2005-12-11 15:08:56Z cgruber $ + */ +package org.israfil.sqlelements.constraints; + +import org.israfil.sqlelements.Join; +import org.israfil.sqlelements.render.SQLRenderContext; + +/** + * Represents a single column from a single (physical or virtual) table. + * + * @author Christian Edward Gruber + * @author Latest: $Author: cgruber $ + * @version $Revision: 22 $ + */ +public class JoinConstraint implements ComplexConstraint +{ + private Join j; + + public JoinConstraint(Join j) { + this.j = j; + } + + /** + * Renders based on the contained Join + */ + public String render(SQLRenderContext context) { + return j.render(context); + } + +} diff --git a/sqlelements/src/main/java/org/israfil/sqlelements/constraints/NaryConstraint.java b/sqlelements/src/main/java/org/israfil/sqlelements/constraints/NaryConstraint.java new file mode 100644 index 0000000..0d0058d --- /dev/null +++ b/sqlelements/src/main/java/org/israfil/sqlelements/constraints/NaryConstraint.java @@ -0,0 +1,93 @@ +/* + * SQLElements object-oriented SQL parsing and generation library + * + * Copyright (c) 2003, 2004, 2005 Israfil Consulting Services Corporation + * Copyright (c) 2003, 2004, 2005 Christian Edward Gruber + * All Rights Reserved + * + * This library 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 2.1 of the License, or + * (at your option) any later version. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * 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. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library; if not, please visit http://www.gnu.org/licenses/lgpl.html + * + * $Id: NaryConstraint.java 27 2006-01-14 12:50:08Z cgruber $ + */ +package org.israfil.sqlelements.constraints; + +import java.util.Arrays; +import java.util.Set; + +import net.israfil.foundation.collections.ArraySet; + +import org.israfil.sqlelements.render.SQLRenderContext; + +/** + * N-ary Constraint + * A friendly parent for constraints that can have an infinite + * number of operands, such as and, or, etc. + * i.e.: "(a=b) and (b=c) and (c=d)" is a valid SQL construction, but + * can be represented (logically) by: and(condition1,condition2,condition3) + * + * @author Christian Edward Gruber + * @author Latest: $Author: cgruber $ + * @version $Revision: 27 $ + */ +public abstract class NaryConstraint implements ComplexConstraint +{ + protected Constraint[] operands; + protected String operatorText; + + protected NaryConstraint(String operatorText, Constraint ... operands) { + this(operatorText,prepArray(operands)); + } + + protected static Set prepArray(Constraint[] array) { + Set s = new ArraySet(); + if (array != null) s.addAll(Arrays.asList(array)); + return s; + } + protected NaryConstraint(String operatorText, Set operands) { + if (operands == null) operands = new ArraySet(0); + this.operatorText = operatorText; + Set constraintSet = new ArraySet(); + for (Constraint c : operands) { + if (c == null) continue; + if (this.getClass().equals(c.getClass())) { + for(Constraint sub : ((NaryConstraint)c).operands){ + constraintSet.add(sub); + } + } + else constraintSet.add(c); + } + this.operands = constraintSet.toArray(new Constraint[constraintSet.size()]); + } + + public String render(SQLRenderContext context) { + boolean first = true; + String sql = ""; + for (Constraint c : this.operands) { + if (!first) { + sql += " "; + sql += operatorText; + sql += " "; + } + else first = false; + sql += ConstraintUtils.renderInParentheses(c,context); + } + return sql; + } +} diff --git a/sqlelements/src/main/java/org/israfil/sqlelements/constraints/Not.java b/sqlelements/src/main/java/org/israfil/sqlelements/constraints/Not.java new file mode 100644 index 0000000..9b266c8 --- /dev/null +++ b/sqlelements/src/main/java/org/israfil/sqlelements/constraints/Not.java @@ -0,0 +1,53 @@ +/* + * SQLElements object-oriented SQL parsing and generation library + * + * Copyright (c) 2003, 2004, 2005 Israfil Consulting Services Corporation + * Copyright (c) 2003, 2004, 2005 Christian Edward Gruber + * All Rights Reserved + * + * This library 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 2.1 of the License, or + * (at your option) any later version. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * 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. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library; if not, please visit http://www.gnu.org/licenses/lgpl.html + * + * $Id: Not.java 17 2005-12-10 20:06:44Z cgruber $ + */ +package org.israfil.sqlelements.constraints; + +import org.israfil.sqlelements.render.SQLRenderContext; + + +/** + * Represents an AND operator in a SQL WHERE clause. + * + * @author Christian Edward Gruber + * @author Latest: $Author: cgruber $ + * @version $Revision: 17 $ + */ +public class Not implements ComplexConstraint +{ + protected Constraint operand; + + public Not(Constraint operand) { + this.operand=operand; + } + + public String render(SQLRenderContext context) + { + return "not (" + operand.render(context) + ")"; + } +} diff --git a/sqlelements/src/main/java/org/israfil/sqlelements/constraints/NotEquals.java b/sqlelements/src/main/java/org/israfil/sqlelements/constraints/NotEquals.java new file mode 100644 index 0000000..07c9836 --- /dev/null +++ b/sqlelements/src/main/java/org/israfil/sqlelements/constraints/NotEquals.java @@ -0,0 +1,46 @@ +/* + * SQLElements object-oriented SQL parsing and generation library + * + * Copyright (c) 2003, 2004, 2005 Israfil Consulting Services Corporation + * Copyright (c) 2003, 2004, 2005 Christian Edward Gruber + * All Rights Reserved + * + * This library 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 2.1 of the License, or + * (at your option) any later version. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * 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. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library; if not, please visit http://www.gnu.org/licenses/lgpl.html + * + * $Id: NotEquals.java 22 2005-12-11 15:08:56Z cgruber $ + */ +package org.israfil.sqlelements.constraints; + +import org.israfil.sqlelements.Column; + + +/** + * A friendly Column Inequality constraint. + * + * @author Christian Edward Gruber + * @author Latest: $Author: cgruber $ + * @version $Revision: 22 $ + */ +public class NotEquals extends BinaryConstraint { + public NotEquals(Column leftOperand,Column rightOperand) { + super(new ColumnConstraint(leftOperand),new ColumnConstraint(rightOperand), "!=") ; + } +} + diff --git a/sqlelements/src/main/java/org/israfil/sqlelements/constraints/Or.java b/sqlelements/src/main/java/org/israfil/sqlelements/constraints/Or.java new file mode 100644 index 0000000..25761b0 --- /dev/null +++ b/sqlelements/src/main/java/org/israfil/sqlelements/constraints/Or.java @@ -0,0 +1,51 @@ +/* + * SQLElements object-oriented SQL parsing and generation library + * + * Copyright (c) 2003, 2004, 2005 Israfil Consulting Services Corporation + * Copyright (c) 2003, 2004, 2005 Christian Edward Gruber + * All Rights Reserved + * + * This library 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 2.1 of the License, or + * (at your option) any later version. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * 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. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library; if not, please visit http://www.gnu.org/licenses/lgpl.html + * + * $Id: Or.java 17 2005-12-10 20:06:44Z cgruber $ + */ +package org.israfil.sqlelements.constraints; + +import java.util.Set; + + +/** + * Represents an AND operator in a SQL WHERE clause. + * + * @author Christian Edward Gruber + * @author Latest: $Author: cgruber $ + * @version $Revision: 17 $ + */ +public class Or extends NaryConstraint { + + public Or(Constraint ... constraints) { + super("or",constraints); + } + + public Or(Set constraints) { + super("or",constraints); + } + +} diff --git a/sqlelements/src/main/java/org/israfil/sqlelements/constraints/SimpleConstraint.java b/sqlelements/src/main/java/org/israfil/sqlelements/constraints/SimpleConstraint.java new file mode 100644 index 0000000..89dc9ff --- /dev/null +++ b/sqlelements/src/main/java/org/israfil/sqlelements/constraints/SimpleConstraint.java @@ -0,0 +1,42 @@ +/* + * SQLElements object-oriented SQL parsing and generation library + * + * Copyright (c) 2003, 2004, 2005 Israfil Consulting Services Corporation + * Copyright (c) 2003, 2004, 2005 Christian Edward Gruber + * All Rights Reserved + * + * This library 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 2.1 of the License, or + * (at your option) any later version. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * 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. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library; if not, please visit http://www.gnu.org/licenses/lgpl.html + * + * $Id: SimpleConstraint.java 16 2005-12-09 04:03:58Z cgruber $ + */ +package org.israfil.sqlelements.constraints; + + +/** + * Represents a constraint that would not need to be enclosed by parentheses. + * + * @author Christian Edward Gruber + * @author Latest: $Author: cgruber $ + * @version $Revision: 16 $ + */ +public interface SimpleConstraint extends Constraint +{ + +} diff --git a/sqlelements/src/main/java/org/israfil/sqlelements/render/CommandRenderer.java b/sqlelements/src/main/java/org/israfil/sqlelements/render/CommandRenderer.java new file mode 100644 index 0000000..b4ae7ea --- /dev/null +++ b/sqlelements/src/main/java/org/israfil/sqlelements/render/CommandRenderer.java @@ -0,0 +1,44 @@ +/* + * SQLElements object-oriented SQL parsing and generation library + * + * Copyright (c) 2003, 2004, 2005 Israfil Consulting Services Corporation + * Copyright (c) 2003, 2004, 2005 Christian Edward Gruber + * All Rights Reserved + * + * This library 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 2.1 of the License, or + * (at your option) any later version. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * 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. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library; if not, please visit http://www.gnu.org/licenses/lgpl.html + * + * $Id: CommandRenderer.java 16 2005-12-09 04:03:58Z cgruber $ + */ +package org.israfil.sqlelements.render; + +import org.israfil.sqlelements.Command; + +/** + * A thin interface to render sql commands. + * + * @author Christian Edward Gruber + * @author Latest: $Author: cgruber $ + * @version $Revision: 16 $ + */ +public interface CommandRenderer { + public String render(SQLRenderContext context, Command dt); +} + + diff --git a/sqlelements/src/main/java/org/israfil/sqlelements/render/DatabaseType.java b/sqlelements/src/main/java/org/israfil/sqlelements/render/DatabaseType.java new file mode 100644 index 0000000..5f672d7 --- /dev/null +++ b/sqlelements/src/main/java/org/israfil/sqlelements/render/DatabaseType.java @@ -0,0 +1,119 @@ +/* + * SQLElements object-oriented SQL parsing and generation library + * + * Copyright (c) 2003, 2004, 2005 Israfil Consulting Services Corporation + * Copyright (c) 2003, 2004, 2005 Christian Edward Gruber + * All Rights Reserved + * + * This library 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 2.1 of the License, or + * (at your option) any later version. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * 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. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library; if not, please visit http://www.gnu.org/licenses/lgpl.html + * + * $Id: DatabaseType.java 16 2005-12-09 04:03:58Z cgruber $ + */ +package org.israfil.sqlelements.render; + + +/** + * An enumerated type, but since eclipse doesn't quite support enums yet, we're spoofing + * it until a later Cheetah release. + * + * @author Christian Edward Gruber + * @author Latest: $Author: cgruber $ + * @version $Revision: 16 $ + */ +public enum DatabaseType { + + Mock ( + -1, + "Mock", + new MockSQLRenderEngine(), + new MockSQLRenderEngine(), + new MockSQLRenderEngine(), + "'" + ), + Generic ( + 0, + "Oracle", + new GenericSQLRenderEngine(), + new GenericSQLRenderEngine(), + new GenericSQLRenderEngine(), + "'" + ), + Oracle ( + 1, + "Oracle", + new OracleSQLRenderEngine(), + new OracleSQLRenderEngine(), + new OracleSQLRenderEngine(), + "'" + ), + SQLServer ( + 2, + "Oracle", + new GenericSQLRenderEngine(), + new GenericSQLRenderEngine(), + new GenericSQLRenderEngine(), + "'" + ), + Sybase ( + 3, + "Oracle", + new GenericSQLRenderEngine(), + new GenericSQLRenderEngine(), + new GenericSQLRenderEngine(), + "'" + ), + MySql ( + 4, + "Oracle", + new GenericSQLRenderEngine(), + new GenericSQLRenderEngine(), + new GenericSQLRenderEngine(), + "'" + ), + Delegated ( + 5, + "Delegated", + new DelegatedSQLRenderEngine(), + new DelegatedSQLRenderEngine(), + new DelegatedSQLRenderEngine(), + "'" + ); + + + private DatabaseType(int code, String name, QueryRenderer qr, CommandRenderer cr, DateRenderer dr, String sdelim) { + this.code = code; + this.name = name; + this.qr = qr; + this.cr = cr; + this.dr = dr; + this.sdelim = sdelim; + } + + public final int code; + public final String name; + public final CommandRenderer cr; + public final QueryRenderer qr; + public final DateRenderer dr; + + /** A delimiter for literal strings in sql */ + public final String sdelim; + + +} diff --git a/sqlelements/src/main/java/org/israfil/sqlelements/render/DateRenderer.java b/sqlelements/src/main/java/org/israfil/sqlelements/render/DateRenderer.java new file mode 100644 index 0000000..571a3a4 --- /dev/null +++ b/sqlelements/src/main/java/org/israfil/sqlelements/render/DateRenderer.java @@ -0,0 +1,42 @@ +/* + * SQLElements object-oriented SQL parsing and generation library + * + * Copyright (c) 2003, 2004, 2005 Israfil Consulting Services Corporation + * Copyright (c) 2003, 2004, 2005 Christian Edward Gruber + * All Rights Reserved + * + * This library 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 2.1 of the License, or + * (at your option) any later version. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * 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. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library; if not, please visit http://www.gnu.org/licenses/lgpl.html + * + * $Id: DateRenderer.java 16 2005-12-09 04:03:58Z cgruber $ + */ +package org.israfil.sqlelements.render; + +import java.util.Date; + +/** + * A thin interface to render dates in sql. + * + * @author Christian Edward Gruber + * @author Latest: $Author: cgruber $ + * @version $Revision: 16 $ + */ +public interface DateRenderer { + public String format(SQLRenderContext context, Date dt); +} diff --git a/sqlelements/src/main/java/org/israfil/sqlelements/render/DelegatedSQLRenderEngine.java b/sqlelements/src/main/java/org/israfil/sqlelements/render/DelegatedSQLRenderEngine.java new file mode 100644 index 0000000..4c222b9 --- /dev/null +++ b/sqlelements/src/main/java/org/israfil/sqlelements/render/DelegatedSQLRenderEngine.java @@ -0,0 +1,73 @@ +/* + * SQLElements object-oriented SQL parsing and generation library + * + * Copyright (c) 2003, 2004, 2005 Israfil Consulting Services Corporation + * Copyright (c) 2003, 2004, 2005 Christian Edward Gruber + * All Rights Reserved + * + * This library 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 2.1 of the License, or + * (at your option) any later version. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * 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. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library; if not, please visit http://www.gnu.org/licenses/lgpl.html + * + * $Id: DelegatedSQLRenderEngine.java 16 2005-12-09 04:03:58Z cgruber $ + */ +package org.israfil.sqlelements.render; + +import java.util.Date; + +import org.israfil.sqlelements.Command; +import org.israfil.sqlelements.Query; + +/** + * A SQL rendering engine that provides for a delegate, and passes through + * all rendering requests to that delegate. It's for extensibility. + * + * TODO: Evaluate the elimination of the DatabaseType enum (at least as + * TODO: a vendor construct) and eliminate the need for this indirection. + * + * @author Christian Edward Gruber + * @author Latest: $Author: cgruber $ + * @version $Revision: 16 $ + */ +public class DelegatedSQLRenderEngine implements QueryRenderer, CommandRenderer, DateRenderer { + + protected QueryRenderer qr; + protected CommandRenderer cr; + protected DateRenderer dr; + + public void setDelegates(QueryRenderer qr, CommandRenderer cr, DateRenderer dr) { + this.qr=qr; + this.cr=cr; + this.dr=dr; + } + + public String render(SQLRenderContext context,Query q) { + if (qr == null) throw new NullPointerException("Query Renderer cannot be null."); + return qr.render(context,q); + } + + public String render(SQLRenderContext context,Command cmd) { + if (cr == null) throw new NullPointerException("Command Renderer cannot be null."); + return cr.render(context,cmd); + } + + public String format(SQLRenderContext context, Date dt) { + if (dr == null) throw new NullPointerException("Date Formatter cannot be null."); + return dr.format(context,dt); + } +} diff --git a/sqlelements/src/main/java/org/israfil/sqlelements/render/GenericSQLRenderEngine.java b/sqlelements/src/main/java/org/israfil/sqlelements/render/GenericSQLRenderEngine.java new file mode 100644 index 0000000..c9f9cfb --- /dev/null +++ b/sqlelements/src/main/java/org/israfil/sqlelements/render/GenericSQLRenderEngine.java @@ -0,0 +1,94 @@ +/* + * SQLElements object-oriented SQL parsing and generation library + * + * Copyright (c) 2003, 2004, 2005 Israfil Consulting Services Corporation + * Copyright (c) 2003, 2004, 2005 Christian Edward Gruber + * All Rights Reserved + * + * This library 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 2.1 of the License, or + * (at your option) any later version. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * 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. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library; if not, please visit http://www.gnu.org/licenses/lgpl.html + * + * $Id: GenericSQLRenderEngine.java 25 2005-12-15 23:36:26Z cgruber $ + */ +package org.israfil.sqlelements.render; + +import java.util.Date; +import java.util.Set; + +import org.israfil.sqlelements.Column; +import org.israfil.sqlelements.Command; +import org.israfil.sqlelements.Query; +import org.israfil.sqlelements.Table; +import org.israfil.sqlelements.constraints.And; +import org.israfil.sqlelements.constraints.Constraint; + +/** + * A concrete SQL rendering engine. + * + * @author Christian Edward Gruber + * @author Latest: $Author: cgruber $ + * @version $Revision: 25 $ + */ +public class GenericSQLRenderEngine implements QueryRenderer, CommandRenderer, DateRenderer { + + public String render(SQLRenderContext context,Query q) { + Set
tables = q.getTables(); + + String sql = "select "; + + boolean firstColumn = true; + for (Column column : q.getColumns()) { + if (!firstColumn) sql+= ", "; + else firstColumn = false; + if (column.getTable() != null) sql += context.getAlias(column.getTable()) + "."; + sql += column.render(context); + if (column.hasAlias()) sql += " as \"" + column.getAlias() + "\""; + } + + sql += " from "; + + boolean firstTable = true; + for (Table table : tables) + { + if (!firstTable) sql+= ", "; + else firstTable = false; + sql += table.render(context); + sql += " " + context.getAlias(table); + } + Constraint c = q.getConstraint(); + if (q.hasJoins()) { + if (c == null) c = new And(q.getJoinConstraints()); + else c = new And((new And(q.getJoinConstraints())),q.getConstraint()); + } + if (c != null) { + sql += " where "; + sql += c.render(context); + } + return sql; + } + + public String render(SQLRenderContext context,Command cmd) { + return null; + } + + public String format(SQLRenderContext context, Date dt) { + return dt.toString(); + } + +} diff --git a/sqlelements/src/main/java/org/israfil/sqlelements/render/MockSQLRenderEngine.java b/sqlelements/src/main/java/org/israfil/sqlelements/render/MockSQLRenderEngine.java new file mode 100644 index 0000000..a697c57 --- /dev/null +++ b/sqlelements/src/main/java/org/israfil/sqlelements/render/MockSQLRenderEngine.java @@ -0,0 +1,60 @@ +/* + * SQLElements object-oriented SQL parsing and generation library + * + * Copyright (c) 2003, 2004, 2005 Israfil Consulting Services Corporation + * Copyright (c) 2003, 2004, 2005 Christian Edward Gruber + * All Rights Reserved + * + * This library 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 2.1 of the License, or + * (at your option) any later version. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * 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. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library; if not, please visit http://www.gnu.org/licenses/lgpl.html + * + * $Id: MockSQLRenderEngine.java 16 2005-12-09 04:03:58Z cgruber $ + */ +package org.israfil.sqlelements.render; + +import java.util.Date; + +import org.israfil.sqlelements.Command; +import org.israfil.sqlelements.Query; + +/** + * A concrete SQL rendering engine. + * + * @author Christian Edward Gruber + * @author Latest: $Author: cgruber $ + * @version $Revision: 16 $ + */ +public class MockSQLRenderEngine implements QueryRenderer, CommandRenderer, DateRenderer { + + public String render(SQLRenderContext context, Query dt) { + // TODO Auto-generated method stub + return null; + } + + public String render(SQLRenderContext context, Command dt) { + // TODO Auto-generated method stub + return null; + } + + public String format(SQLRenderContext context, Date dt) { + // TODO Auto-generated method stub + return null; + } + +} diff --git a/sqlelements/src/main/java/org/israfil/sqlelements/render/MySqlSQLRenderEngine.java b/sqlelements/src/main/java/org/israfil/sqlelements/render/MySqlSQLRenderEngine.java new file mode 100644 index 0000000..0233094 --- /dev/null +++ b/sqlelements/src/main/java/org/israfil/sqlelements/render/MySqlSQLRenderEngine.java @@ -0,0 +1,40 @@ +/* + * SQLElements object-oriented SQL parsing and generation library + * + * Copyright (c) 2003, 2004, 2005 Israfil Consulting Services Corporation + * Copyright (c) 2003, 2004, 2005 Christian Edward Gruber + * All Rights Reserved + * + * This library 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 2.1 of the License, or + * (at your option) any later version. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * 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. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library; if not, please visit http://www.gnu.org/licenses/lgpl.html + * + * $Id: MySqlSQLRenderEngine.java 16 2005-12-09 04:03:58Z cgruber $ + */ +package org.israfil.sqlelements.render; + +/** + * A concrete SQL rendering engine. + * + * @author Christian Edward Gruber + * @author Latest: $Author: cgruber $ + * @version $Revision: 16 $ + */ +public class MySqlSQLRenderEngine extends GenericSQLRenderEngine { + +} diff --git a/sqlelements/src/main/java/org/israfil/sqlelements/render/OracleSQLRenderEngine.java b/sqlelements/src/main/java/org/israfil/sqlelements/render/OracleSQLRenderEngine.java new file mode 100644 index 0000000..92084e6 --- /dev/null +++ b/sqlelements/src/main/java/org/israfil/sqlelements/render/OracleSQLRenderEngine.java @@ -0,0 +1,97 @@ +/* + * SQLElements object-oriented SQL parsing and generation library + * + * Copyright (c) 2003, 2004, 2005 Israfil Consulting Services Corporation + * Copyright (c) 2003, 2004, 2005 Christian Edward Gruber + * All Rights Reserved + * + * This library 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 2.1 of the License, or + * (at your option) any later version. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * 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. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library; if not, please visit http://www.gnu.org/licenses/lgpl.html + * + * $Id: OracleSQLRenderEngine.java 22 2005-12-11 15:08:56Z cgruber $ + */ +package org.israfil.sqlelements.render; + +import java.util.Date; +import java.util.Set; + +import org.israfil.sqlelements.Column; +import org.israfil.sqlelements.Command; +import org.israfil.sqlelements.Query; +import org.israfil.sqlelements.Table; +import org.israfil.sqlelements.constraints.And; +import org.israfil.sqlelements.constraints.Constraint; + +/** + * A concrete SQL rendering engine. + * + * @author Christian Edward Gruber + * @author Latest: $Author: cgruber $ + * @version $Revision: 22 $ + */ +public class OracleSQLRenderEngine implements QueryRenderer, CommandRenderer, DateRenderer { + + public String render(SQLRenderContext context,Query q) { + Set
tables = q.getTables(); + + String sql = "select "; + + boolean firstColumn = true; + for (Column column : q.getColumns()) { + if (!firstColumn) sql+= ", "; + else firstColumn = false; + if (column.getTable() != null) sql += context.getAlias(column.getTable()) + "."; + sql += column.render(context); + if (column.hasAlias()) sql += " as \"" + column.getAlias() + "\""; + } + + sql += " from "; + + boolean firstTable = true; + for (Table table : tables) + { + if (!firstTable) sql+= ", "; + else firstTable = false; + sql += table.render(context); + sql += " " + context.getAlias(table); + } + if (q.hasJoins()) { + if (q.getJoins() != null && q.getJoins().size() > 0) { + if (q.hasConstraint()) { + q.setConstraint(new And((Constraint)(new And(q.getJoinConstraints())),q.getConstraint())); + } else q.setConstraint(new And(q.getJoinConstraints())); + } + } + if (q.hasConstraint()) { + sql += " where "; + sql += q.getConstraint().render(context); + } + return sql; + } + + public String render(SQLRenderContext context,Command cmd) { + return null; + } + + public String format(SQLRenderContext context, Date dt) { + return dt.toString(); + } +} + + diff --git a/sqlelements/src/main/java/org/israfil/sqlelements/render/QueryRenderer.java b/sqlelements/src/main/java/org/israfil/sqlelements/render/QueryRenderer.java new file mode 100644 index 0000000..3d014fe --- /dev/null +++ b/sqlelements/src/main/java/org/israfil/sqlelements/render/QueryRenderer.java @@ -0,0 +1,44 @@ +/* + * SQLElements object-oriented SQL parsing and generation library + * + * Copyright (c) 2003, 2004, 2005 Israfil Consulting Services Corporation + * Copyright (c) 2003, 2004, 2005 Christian Edward Gruber + * All Rights Reserved + * + * This library 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 2.1 of the License, or + * (at your option) any later version. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * 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. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library; if not, please visit http://www.gnu.org/licenses/lgpl.html + * + * $Id: QueryRenderer.java 16 2005-12-09 04:03:58Z cgruber $ + */ +package org.israfil.sqlelements.render; + +import org.israfil.sqlelements.Query; + + +/** + * A concrete SQL rendering engine. + * + * @author Christian Edward Gruber + * @author Latest: $Author: cgruber $ + * @version $Revision: 16 $ + */ +public interface QueryRenderer { + public String render(SQLRenderContext context, Query dt); +} + diff --git a/sqlelements/src/main/java/org/israfil/sqlelements/render/SQLRenderContext.java b/sqlelements/src/main/java/org/israfil/sqlelements/render/SQLRenderContext.java new file mode 100644 index 0000000..6d70949 --- /dev/null +++ b/sqlelements/src/main/java/org/israfil/sqlelements/render/SQLRenderContext.java @@ -0,0 +1,97 @@ +/* + * SQLElements object-oriented SQL parsing and generation library + * + * Copyright (c) 2003, 2004, 2005 Israfil Consulting Services Corporation + * Copyright (c) 2003, 2004, 2005 Christian Edward Gruber + * All Rights Reserved + * + * This library 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 2.1 of the License, or + * (at your option) any later version. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * 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. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library; if not, please visit http://www.gnu.org/licenses/lgpl.html + * + * $Id: SQLRenderContext.java 16 2005-12-09 04:03:58Z cgruber $ + */ +package org.israfil.sqlelements.render; + +import java.util.Date; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; + +import org.israfil.sqlelements.Command; +import org.israfil.sqlelements.Query; +import org.israfil.sqlelements.Table; + + +/** + * A local context for a render that includes database-specific rendering help, + * as well as alias re-writing for non-aliased tables, etc. + * + * @author Christian Edward Gruber + * @author Latest: $Author: cgruber $ + * @version $Revision: 16 $ + */ +public class SQLRenderContext { + + protected DatabaseType type; + /* + * protected CommandRenderer commandRenderer; + */ + protected Map aliases = new HashMap(); + + public SQLRenderContext(DatabaseType type) { + this.type = type; + } + + public void finalize() throws Throwable { + this.type = null; + } + + public String getAlias(Table table) { + if (aliases.get(table) != null) + return aliases.get(table); // succeed fast. + + Set usedAliases = new HashSet(); + for (String alias : aliases.values()) + usedAliases.add(alias); + + if (table.hasAlias()) { + aliases.put(table,table.getAlias()); + return table.getAlias(); + } else { + int tnumber = 0; + while (usedAliases.contains("t" + tnumber)) tnumber++; + aliases.put(table,"t" + tnumber); + return aliases.get(table); + } + } + + public String format(Date dt) { + return type.dr.format(this,dt); + } + public String renderQuery(Query q) { + return type.qr.render(this,q); + } + public String renderCommand(Command c) { + return type.cr.render(this,c); + } + public String getStringDelimiter() { + return type.sdelim; + } +} -- cgit v1.2.3