package com.speedment.runtime.connector.sqlite.internal;

import com.speedment.runtime.core.db.SqlPredicateFragment;
import com.speedment.runtime.core.internal.manager.sql.AbstractFieldPredicateView;
import com.speedment.runtime.field.predicate.FieldPredicate;

import static com.speedment.runtime.field.util.PredicateOperandUtil.getFirstOperandAsRaw;

/**
 * @author Emil Forslund
 * @since  3.1.10
 */
public class SqliteFieldPredicateView extends AbstractFieldPredicateView {

    @Override
    protected SqlPredicateFragment equalHelper(String cn, Object argument) {
        return of("(" + cn + " = ?)").add(argument);
    }

    @Override
    protected SqlPredicateFragment notEqualHelper(String cn, Object argument) {
        return of("(" + cn + " != ?)").add(argument);
    }

    @Override
    protected SqlPredicateFragment equalIgnoreCaseHelper(String cn, FieldPredicate<?> model, boolean negated) {
        return of("(" + cn + " COLLATE NOCASE " + (negated ? "!= ?)" : "= ?)"))
            .add(getFirstOperandAsRaw(model));
    }

    @Override
    protected SqlPredicateFragment startsWithHelper(String cn, FieldPredicate<?> model, boolean negated) {
        return of("(" + cn + " LIKE (? || \"%\") ESCAPE \"_\")", negated)
            .add(getFirstOperandAsRaw(model));
    }

    @Override
    protected SqlPredicateFragment startsWithIgnoreCaseHelper(String cn, FieldPredicate<?> model, boolean negated) {
        return of("(" + cn + " COLLATE NOCASE LIKE (? || \"%\") ESCAPE \"_\")", negated)
            .add(getFirstOperandAsRaw(model));
    }

    @Override
    protected SqlPredicateFragment endsWithHelper(String cn, FieldPredicate<?> model, boolean negated) {
        return of("(" + cn + " LIKE (\"%\" || ?) ESCAPE \"_\")", negated)
            .add(getFirstOperandAsRaw(model));
    }

    @Override
    protected SqlPredicateFragment endsWithIgnoreCaseHelper(String cn, FieldPredicate<?> model, boolean negated) {
        return of("(" + cn + " COLLATE NOCASE LIKE (\"%\" || ?) ESCAPE \"_\")", negated)
            .add(getFirstOperandAsRaw(model));
    }

    @Override
    protected SqlPredicateFragment containsHelper(String cn, FieldPredicate<?> model, boolean negated) {
        return of("(" + cn + " LIKE (\"%\" || ? || \"%\") ESCAPE \"_\")", negated)
            .add(getFirstOperandAsRaw(model));
    }

    @Override
    protected SqlPredicateFragment containsIgnoreCaseHelper(String cn, FieldPredicate<?> model, boolean negated) {
        return of("(" + cn + " COLLATE NOCASE LIKE (\"%\" || ? || \"%\") ESCAPE \"_\")", negated)
            .add(getFirstOperandAsRaw(model));
    }
}

@@ -1191,20 +1191,20 @@
  cn + %22 
-LIKE
+GLOB
  (? %7C%7C %5C
@@ -1204,33 +1204,20 @@
 (? %7C%7C %5C%22
-%25
+*
 %5C%22)
- ESCAPE %5C%22_%5C%22
 )%22, nega
@@ -1699,33 +1699,33 @@
  cn + %22 
-LIKE
+GLOB
  (%5C%22
-%25
+*
 %5C%22 %7C%7C ?)
  ESCAPE 
@@ -1712,37 +1712,24 @@
 (%5C%22*%5C%22 %7C%7C ?)
- ESCAPE %5C%22_%5C%22
 )%22, negated)
@@ -2205,25 +2205,25 @@
  cn + %22 
-LIKE
+GLOB
  (%5C%22
-%25
+*
 %5C%22 %7C%7C ? 
@@ -2227,33 +2227,20 @@
  ? %7C%7C %5C%22
-%25
+*
 %5C%22)
- ESCAPE %5C%22_%5C%22
 )%22, nega

Fix case-sensitive search operators