DBBitwiseEqualsOperator.java

  1. /*
  2.  * Copyright 2014 Gregory Graham.
  3.  *
  4.  * Licensed under the Apache License, Version 2.0 (the "License");
  5.  * you may not use this file except in compliance with the License.
  6.  * You may obtain a copy of the License at
  7.  *
  8.  *      http://www.apache.org/licenses/LICENSE-2.0
  9.  *
  10.  * Unless required by applicable law or agreed to in writing, software
  11.  * distributed under the License is distributed on an "AS IS" BASIS,
  12.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13.  * See the License for the specific language governing permissions and
  14.  * limitations under the License.
  15.  */
  16. package nz.co.gregs.dbvolution.operators;

  17. import nz.co.gregs.dbvolution.databases.definitions.DBDefinition;
  18. import nz.co.gregs.dbvolution.expressions.BooleanExpression;
  19. import nz.co.gregs.dbvolution.results.BooleanResult;
  20. import nz.co.gregs.dbvolution.expressions.DBExpression;
  21. import nz.co.gregs.dbvolution.expressions.NumberExpression;
  22. import nz.co.gregs.dbvolution.results.NumberResult;

  23. /**
  24.  * Creates a bitwise comparison for boolean or number expressions
  25.  *
  26.  * <p style="color: #F90;">Support DBvolution at
  27.  * <a href="http://patreon.com/dbvolution" target=new>Patreon</a></p>
  28.  *
  29.  * @author gregorygraham
  30.  */
  31. public class DBBitwiseEqualsOperator extends DBEqualsOperator {

  32.     private static final long serialVersionUID = 1L;

  33.     /**
  34.      * Creates a bitwise comparison for boolean or number expressions
  35.      *
  36.      * @param equalTo the boolean expression
  37.      */
  38.     public DBBitwiseEqualsOperator(BooleanExpression equalTo) {
  39.         super(equalTo);
  40.     }

  41.     @Override
  42.     public BooleanExpression generateWhereExpression(DBDefinition db, DBExpression column) {
  43.         DBExpression genericExpression = column;
  44.         BooleanExpression op = BooleanExpression.trueExpression();
  45.         if (genericExpression instanceof BooleanExpression) {
  46.             BooleanExpression expr = (BooleanExpression) genericExpression;
  47.             final DBExpression firstValue = getFirstValue();
  48.             if (firstValue instanceof BooleanResult) {
  49.                 op = expr.is((BooleanResult) firstValue);
  50.             } else if (firstValue instanceof NumberResult) {
  51.                 op = expr.is(new NumberExpression((NumberResult) firstValue).is(1));
  52.             }
  53.         }
  54.         return this.invertOperator ? op.not() : op;
  55.     }
  56. }