Skip to content

Commit

Permalink
Add support for String
Browse files Browse the repository at this point in the history
  • Loading branch information
emilforslund committed May 5, 2015
1 parent bc885bb commit 9f53373
Show file tree
Hide file tree
Showing 14 changed files with 99 additions and 28 deletions.
Expand Up @@ -18,17 +18,18 @@


import com.speedment.codegen.base.Generator; import com.speedment.codegen.base.Generator;
import com.speedment.codegen.base.Transform; import com.speedment.codegen.base.Transform;
import com.speedment.orm.field.BinaryPredicateBuilder;
import com.speedment.orm.field.StandardBinaryOperator; import com.speedment.orm.field.StandardBinaryOperator;
import com.speedment.orm.field.reference.ReferenceBinaryPredicateBuilder;
import java.util.Optional; import java.util.Optional;


/** /**
* *
* @author Emil Forslund * @author Emil Forslund
* @param <T>
*/ */
public class BinaryPredicateBuilderView implements Transform<ReferenceBinaryPredicateBuilder, String> { public class BinaryPredicateBuilderView implements Transform<BinaryPredicateBuilder, String> {


private String render(StandardBinaryOperator op) { protected String render(StandardBinaryOperator op) {
switch (op) { switch (op) {
case EQUAL : return " == "; case EQUAL : return " == ";
case GREATER_OR_EQUAL : return " >= "; case GREATER_OR_EQUAL : return " >= ";
Expand All @@ -43,11 +44,13 @@ private String render(StandardBinaryOperator op) {
} }


@Override @Override
public Optional<String> transform(Generator gen, ReferenceBinaryPredicateBuilder model) { public Optional<String> transform(Generator gen, BinaryPredicateBuilder model) {
return Optional.of( if (model.getOperator() instanceof StandardBinaryOperator) {
model.getField().getColumn().getName() + return Optional.of(
render(model.getOperator()) + model.getField().getColumn().getName() +
"?" render((StandardBinaryOperator) model.getOperator()) +
); "?"
);
} else return Optional.empty();
} }
} }
Expand Up @@ -17,8 +17,9 @@
package com.speedment.orm.core.manager.sql.generator; package com.speedment.orm.core.manager.sql.generator;


import com.speedment.codegen.base.DefaultTransformFactory; import com.speedment.codegen.base.DefaultTransformFactory;
import com.speedment.orm.field.reference.ReferenceBinaryPredicateBuilder; import com.speedment.orm.field.BinaryPredicateBuilder;
import com.speedment.orm.field.reference.ReferenceUnaryPredicateBuilder; import com.speedment.orm.field.UnaryPredicateBuilder;
import com.speedment.orm.field.reference.string.StringBinaryPredicateBuilder;


/** /**
* *
Expand All @@ -28,7 +29,8 @@ public class SQLTransformFactory extends DefaultTransformFactory {


public SQLTransformFactory() { public SQLTransformFactory() {
super(SQLTransformFactory.class.getSimpleName()); super(SQLTransformFactory.class.getSimpleName());
install(ReferenceUnaryPredicateBuilder.class, UnaryPredicateBuilderView.class); install(UnaryPredicateBuilder.class, UnaryPredicateBuilderView.class);
install(ReferenceBinaryPredicateBuilder.class, BinaryPredicateBuilderView.class); install(BinaryPredicateBuilder.class, BinaryPredicateBuilderView.class);
install(StringBinaryPredicateBuilder.class, StringBinaryPredicateBuilderView.class);
} }
} }
@@ -0,0 +1,52 @@
/**
*
* Copyright (c) 2006-2015, Speedment, Inc. All Rights Reserved.
*
* 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 com.speedment.orm.core.manager.sql.generator;

import com.speedment.codegen.base.Generator;
import com.speedment.codegen.base.Transform;
import com.speedment.orm.field.StandardBinaryOperator;
import com.speedment.orm.field.StandardStringBinaryOperator;
import com.speedment.orm.field.reference.string.StringBinaryPredicateBuilder;
import java.util.Optional;

/**
*
* @author Emil Forslund
*/
public class StringBinaryPredicateBuilderView implements Transform<StringBinaryPredicateBuilder, String> {

protected String render(StandardStringBinaryOperator op) {
switch (op) {
// case CONTAINS : return "";
// case ENDS_WITH : return "";
// case EQUAL_IGNORE_CASE : return "";
// case NOT_EQUAL_IGNORE_CASE : return "";
// case STARTS_WITH : return "";
default : throw new UnsupportedOperationException(
"Unknown enum constant " + op.name() + "."
);
}
}

@Override
public Optional<String> transform(Generator gen, StringBinaryPredicateBuilder model) {
return Optional.of(
model.getField().getColumn().getName() +
render(model.getOperator())
);
}
}
Expand Up @@ -21,15 +21,15 @@
import com.speedment.orm.field.StandardUnaryOperator; import com.speedment.orm.field.StandardUnaryOperator;
import static com.speedment.orm.field.StandardUnaryOperator.IS_NOT_NULL; import static com.speedment.orm.field.StandardUnaryOperator.IS_NOT_NULL;
import static com.speedment.orm.field.StandardUnaryOperator.IS_NULL; import static com.speedment.orm.field.StandardUnaryOperator.IS_NULL;
import com.speedment.orm.field.reference.ReferenceUnaryPredicateBuilder; import com.speedment.orm.field.UnaryPredicateBuilder;


import java.util.Optional; import java.util.Optional;


/** /**
* *
* @author Emil Forslund * @author Emil Forslund
*/ */
public class UnaryPredicateBuilderView implements Transform<ReferenceUnaryPredicateBuilder, String> { public abstract class UnaryPredicateBuilderView implements Transform<UnaryPredicateBuilder, String> {


private String render(StandardUnaryOperator op) { private String render(StandardUnaryOperator op) {
switch (op) { switch (op) {
Expand All @@ -45,7 +45,7 @@ private String render(StandardUnaryOperator op) {
} }


@Override @Override
public Optional<String> transform(Generator gen, ReferenceUnaryPredicateBuilder model) { public Optional<String> transform(Generator gen, UnaryPredicateBuilder model) {
return Optional.of( return Optional.of(
model.getField().getColumn().getName() model.getField().getColumn().getName()
+ render(model.getOperator()) + render(model.getOperator())
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/com/speedment/orm/field/BinaryOperator.java
@@ -0,0 +1,9 @@
package com.speedment.orm.field;

/**
*
* @author Emil Forslund
*/
public interface BinaryOperator extends Operator {

}
Expand Up @@ -3,9 +3,12 @@
/** /**
* *
* @author pemi * @author pemi
* @param <V>
*/ */
public interface BinaryPredicateBuilder<V> extends PredicateBuilder { public interface BinaryPredicateBuilder<V> extends PredicateBuilder {


V getValueAsObject(); V getValueAsObject();


} @Override
public BinaryOperator getOperator();
}
Expand Up @@ -23,7 +23,7 @@
* *
* @author pemi * @author pemi
*/ */
public enum StandardBinaryOperator implements Operator { public enum StandardBinaryOperator implements BinaryOperator {


EQUAL(i -> i == 0), EQUAL(i -> i == 0),
NOT_EQUAL(i -> i != 0), NOT_EQUAL(i -> i != 0),
Expand Down
Expand Up @@ -23,7 +23,7 @@
* *
* @author pemi * @author pemi
*/ */
public enum StandardStringBinaryOperator implements Operator { public enum StandardStringBinaryOperator implements BinaryOperator {


STARTS_WITH(String::startsWith), STARTS_WITH(String::startsWith),
ENDS_WITH(String::endsWith), ENDS_WITH(String::endsWith),
Expand Down
Expand Up @@ -6,4 +6,7 @@
*/ */
public interface UnaryPredicateBuilder extends PredicateBuilder { public interface UnaryPredicateBuilder extends PredicateBuilder {


} @Override
public StandardUnaryOperator getOperator();

}
Expand Up @@ -18,7 +18,6 @@


import com.speedment.orm.field.BasePredicate; import com.speedment.orm.field.BasePredicate;
import com.speedment.orm.field.BinaryPredicateBuilder; import com.speedment.orm.field.BinaryPredicateBuilder;
import com.speedment.orm.field.Operator;
import com.speedment.orm.field.StandardBinaryOperator; import com.speedment.orm.field.StandardBinaryOperator;
import java.util.Objects; import java.util.Objects;
import java.util.function.Predicate; import java.util.function.Predicate;
Expand Down Expand Up @@ -59,7 +58,7 @@ public DoubleField getField() {
} }


@Override @Override
public Operator getOperator() { public StandardBinaryOperator getOperator() {
return binaryOperator; return binaryOperator;
} }


Expand Down
Expand Up @@ -52,7 +52,7 @@ public DoubleField getField() {
} }


@Override @Override
public Operator getOperator() { public StandardUnaryOperator getOperator() {
return unaryOperator; return unaryOperator;
} }


Expand Down
Expand Up @@ -59,7 +59,7 @@ public IntField getField() {
} }


@Override @Override
public Operator getOperator() { public StandardBinaryOperator getOperator() {
return binaryOperator; return binaryOperator;
} }


Expand Down
Expand Up @@ -59,7 +59,7 @@ public LongField getField() {
} }


@Override @Override
public Operator getOperator() { public StandardBinaryOperator getOperator() {
return binaryOperator; return binaryOperator;
} }


Expand Down
Expand Up @@ -19,7 +19,7 @@
import com.speedment.orm.field.reference.*; import com.speedment.orm.field.reference.*;
import com.speedment.orm.field.BasePredicate; import com.speedment.orm.field.BasePredicate;
import com.speedment.orm.field.BinaryPredicateBuilder; import com.speedment.orm.field.BinaryPredicateBuilder;
import com.speedment.orm.field.Operator; import com.speedment.orm.field.StandardBinaryOperator;
import com.speedment.orm.field.StandardStringBinaryOperator; import com.speedment.orm.field.StandardStringBinaryOperator;
import java.util.Objects; import java.util.Objects;


Expand Down Expand Up @@ -64,7 +64,7 @@ public String getValue() {
} }


@Override @Override
public Operator getOperator() { public StandardStringBinaryOperator getOperator() {
return binaryOperator; return binaryOperator;
} }


Expand Down

0 comments on commit 9f53373

Please sign in to comment.