Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use CodeGen to parse operators to SQL
- Loading branch information
emilforslund
committed
May 5, 2015
1 parent
08c9518
commit 723fc70
Showing
6 changed files
with
169 additions
and
2 deletions.
There are no files selected for viewing
53 changes: 53 additions & 0 deletions
53
src/main/java/com/speedment/orm/core/manager/sql/generator/BinaryPredicateBuilderView.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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()) + | ||
"?" | ||
); | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
src/main/java/com/speedment/orm/core/manager/sql/generator/SQLGenerator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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()); | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
src/main/java/com/speedment/orm/core/manager/sql/generator/SQLTransformFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
src/main/java/com/speedment/orm/core/manager/sql/generator/UnaryPredicateBuilderView.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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()) | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters