Skip to content

DATAJDBC-335 - Add DSL and renderer for Insert, Update, and Delete. #121

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>org.springframework.data</groupId>
<artifactId>spring-data-relational-parent</artifactId>
<version>1.1.0.BUILD-SNAPSHOT</version>
<version>2.2.0.DATAJDBC-335-SNAPSHOT</version>
<packaging>pom</packaging>

<name>Spring Data Relational Parent</name>
Expand Down
2 changes: 1 addition & 1 deletion spring-data-jdbc-distribution/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-relational-parent</artifactId>
<version>1.1.0.BUILD-SNAPSHOT</version>
<version>2.2.0.DATAJDBC-335-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
4 changes: 2 additions & 2 deletions spring-data-jdbc/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<modelVersion>4.0.0</modelVersion>

<artifactId>spring-data-jdbc</artifactId>
<version>1.1.0.BUILD-SNAPSHOT</version>
<version>2.2.0.DATAJDBC-335-SNAPSHOT</version>

<name>Spring Data JDBC</name>
<description>Spring Data module for JDBC repositories.</description>
Expand All @@ -14,7 +14,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-relational-parent</artifactId>
<version>1.1.0.BUILD-SNAPSHOT</version>
<version>2.2.0.DATAJDBC-335-SNAPSHOT</version>
</parent>

<properties>
Expand Down
4 changes: 2 additions & 2 deletions spring-data-relational/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
<modelVersion>4.0.0</modelVersion>

<artifactId>spring-data-relational</artifactId>
<version>1.1.0.BUILD-SNAPSHOT</version>
<version>2.2.0.DATAJDBC-335-SNAPSHOT</version>

<name>Spring Data Relational</name>
<description>Spring Data Relational support</description>

<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-relational-parent</artifactId>
<version>1.1.0.BUILD-SNAPSHOT</version>
<version>2.2.0.DATAJDBC-335-SNAPSHOT</version>
</parent>

<properties>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/*
* Copyright 2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.relational.core.sql;

import java.util.HashSet;
import java.util.Set;

import org.springframework.lang.Nullable;

/**
* Validator for statements to import columns.
*
* @author Mark Paluch
* @since 1.1
*/
abstract class AbstractImportValidator implements Visitor {

Set<Table> requiredByWhere = new HashSet<>();
Set<Table> from = new HashSet<>();
Visitable parent;

/*
* (non-Javadoc)
* @see org.springframework.data.relational.core.sql.Visitor#enter(org.springframework.data.relational.core.sql.Visitable)
*/
@Override
public void enter(Visitable segment) {

if (segment instanceof Table && parent instanceof From) {
from.add((Table) segment);
}

if (segment instanceof Where) {
segment.visit(new SubselectFilteringWhereVisitor());
}

if (segment instanceof Join || segment instanceof OrderByField || segment instanceof From
|| segment instanceof Select || segment instanceof Where || segment instanceof SimpleFunction) {
parent = segment;
}
}

/*
* (non-Javadoc)
* @see org.springframework.data.relational.core.sql.Visitor#leave(org.springframework.data.relational.core.sql.Visitable)
*/
@Override
public void leave(Visitable segment) {}

/**
* {@link Visitor} that skips sub-{@link Select} and collects columns within a {@link Where} clause.
*/
class SubselectFilteringWhereVisitor implements Visitor {

private @Nullable Select selectFilter;

/*
* (non-Javadoc)
* @see org.springframework.data.relational.core.sql.Visitor#enter(org.springframework.data.relational.core.sql.Visitable)
*/
@Override
public void enter(Visitable segment) {

if (selectFilter != null) {
return;
}

if (segment instanceof Select) {
this.selectFilter = (Select) segment;
return;
}

if (segment instanceof Table) {
requiredByWhere.add((Table) segment);
}
}

/*
* (non-Javadoc)
* @see org.springframework.data.relational.core.sql.Visitor#leave(org.springframework.data.relational.core.sql.Visitable)
*/
@Override
public void leave(Visitable segment) {

if (this.selectFilter == segment) {
this.selectFilter = null;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
* Copyright 2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.relational.core.sql;

import org.springframework.util.Assert;

/**
* Assign a {@link Expression} to a {@link Column}.
* <p/>
* Results in a rendered assignment: {@code <column> = <value>} (e.g. {@code col = 'foo'}.
*
* @author Mark Paluch
* @since 1.1
*/
public class AssignValue extends AbstractSegment implements Assignment {

private final Column column;
private final Expression value;

private AssignValue(Column column, Expression value) {
super(column, value);
this.column = column;
this.value = value;
}

/**
* Creates a {@link AssignValue value} assignment to a {@link Column} given an {@link Expression}.
*
* @param target target column, must not be {@literal null}.
* @param value assignment value, must not be {@literal null}.
* @return the {@link AssignValue}.
*/
public static AssignValue create(Column target, Expression value) {

Assert.notNull(target, "Target column must not be null!");
Assert.notNull(value, "Value must not be null!");

return new AssignValue(target, value);
}

/**
* @return the target {@link Column}.
*/
public Column getColumn() {
return column;
}

/**
* @return the value to assign.
*/
public Expression getValue() {
return value;
}

/*
* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@Override
public String toString() {

StringBuilder builder = new StringBuilder();
return builder.append(this.column).append(" = ").append(this.value).toString();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Copyright 2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.relational.core.sql;

/**
* Update assignment to a {@link Column}.
*
* @author Mark Paluch
*/
public interface Assignment extends Segment {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright 2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.relational.core.sql;

/**
* Factory for common {@link Assignment}s.
*
* @author Mark Paluch
* @since 1.1
* @see SQL
* @see Expressions
* @see Functions
*/
public abstract class Assignments {

/**
* Creates a {@link AssignValue value} assignment to a {@link Column} given an {@link Expression}.
*
* @param target target column, must not be {@literal null}.
* @param value assignment value, must not be {@literal null}.
* @return the {@link AssignValue}.
*/
public static AssignValue value(Column target, Expression value) {
return AssignValue.create(target, value);
}

// Utility constructor.
private Assignments() {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,20 @@ public Condition isNotNull() {
return isNull().not();
}

// -------------------------------------------------------------------------
// Methods for Assignment creation.
// -------------------------------------------------------------------------

/**
* Creates a value {@link AssignValue assignment}.
*
* @param value the value to assign.
* @return the {@link AssignValue} assignment.
*/
public AssignValue set(Expression value) {
return Assignments.value(this, value);
}

/*
* (non-Javadoc)
* @see org.springframework.data.relational.core.sql.Named#getName()
Expand Down
Loading