Skip to content

Commit

Permalink
Use CodeGen to parse operators to SQL
Browse files Browse the repository at this point in the history
  • Loading branch information
emilforslund committed May 5, 2015
1 parent 08c9518 commit 723fc70
Show file tree
Hide file tree
Showing 6 changed files with 169 additions and 2 deletions.
@@ -0,0 +1,53 @@
/**
*
* 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.reference.BinaryPredicateBuilder;
import java.util.Optional;

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

private String render(StandardBinaryOperator op) {
switch (op) {
case EQUAL : return " == ";
case GREATER_OR_EQUAL : return " >= ";
case GREATER_THAN : return " > ";
case LESS_OR_EQUAL : return " <= ";
case LESS_THAN : return " < ";
case NOT_EQUAL : return " <> ";
default : throw new UnsupportedOperationException(
"Unknown enum constant " + op.name() + "."
);
}
}

@Override
public Optional<String> transform(Generator gen, BinaryPredicateBuilder model) {
return Optional.of(
model.getField().getColumn().getName() +
render(model.getOperator()) +
"?"
);
}
}
@@ -0,0 +1,30 @@
/**
*
* 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.DefaultGenerator;

/**
*
* @author Emil Forslund
*/
public class SQLGenerator extends DefaultGenerator {

public SQLGenerator() {
super (new SQLTransformFactory());
}
}
@@ -0,0 +1,34 @@
/**
*
* 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.DefaultTransformFactory;
import com.speedment.orm.field.reference.BinaryPredicateBuilder;
import com.speedment.orm.field.reference.UnaryPredicateBuilder;

/**
*
* @author Emil Forslund
*/
public class SQLTransformFactory extends DefaultTransformFactory {

public SQLTransformFactory() {
super (SQLTransformFactory.class.getSimpleName());
install(UnaryPredicateBuilder.class, UnaryPredicateBuilderView.class);
install(BinaryPredicateBuilder.class, BinaryPredicateBuilderView.class);
}
}
@@ -0,0 +1,50 @@
/**
*
* 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.StandardUnaryOperator;
import static com.speedment.orm.field.StandardUnaryOperator.IS_NOT_NULL;
import static com.speedment.orm.field.StandardUnaryOperator.IS_NULL;
import com.speedment.orm.field.reference.UnaryPredicateBuilder;
import java.util.Optional;

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

private String render(StandardUnaryOperator op) {
switch (op) {
case IS_NOT_NULL : return " <> NULL";
case IS_NULL : return " == NULL";
default : throw new UnsupportedOperationException(
"Unknown enum constant " + op.name() + "."
);
}
}

@Override
public Optional<String> transform(Generator gen, UnaryPredicateBuilder model) {
return Optional.of(
model.getField().getColumn().getName() +
render(model.getOperator())
);
}
}
Expand Up @@ -75,7 +75,7 @@ public V getValue() {
}

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

Expand Down
Expand Up @@ -53,7 +53,7 @@ public ReferenceField getField() {
}

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

Expand Down

0 comments on commit 723fc70

Please sign in to comment.