All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.ptsmods.mysqlw.query.QueryCondition Maven / Gradle / Ivy

There is a newer version: 1.5.1
Show newest version
package com.ptsmods.mysqlw.query;

import com.ptsmods.mysqlw.Database;

/**
 * A condition a query must meet to affect or return rows.
 */
public abstract class QueryCondition {

    public static QueryCondition func(QueryFunction function) {
        return new QueryCondition() {
            @Override
            public String toString() {
                return function.toString();
            }
        };
    }

    public static QueryCondition equals(String key, Object value) {
        return new QueryCondition() {
            @Override
            public String toString() {
                return "`" + key + "` = " + Database.getAsString(value);
            }
        };
    }

    public static QueryCondition notEquals(String key, Object value) {
        return new QueryCondition() {
            @Override
            public String toString() {
                return "`" + key + "` <> " + Database.getAsString(value);
            }
        };
    }

    public static QueryCondition greater(String key, Object value) {
        return new QueryCondition() {
            @Override
            public String toString() {
                return "`" + key + "` > " + Database.getAsString(value);
            }
        };
    }

    public static QueryCondition greaterEqual(String key, Object value) {
        return new QueryCondition() {
            @Override
            public String toString() {
                return "`" + key + "` >= " + Database.getAsString(value);
            }
        };
    }

    public static QueryCondition less(String key, Object value) {
        return new QueryCondition() {
            @Override
            public String toString() {
                return "`" + key + "` < " + Database.getAsString(value);
            }
        };
    }

    public static QueryCondition lessEqual(String key, Object value) {
        return new QueryCondition() {
            @Override
            public String toString() {
                return "`" + key + "` <= " + Database.getAsString(value);
            }
        };
    }

    public static QueryCondition like(String key, String value) {
        return new QueryCondition() {
            @Override
            public String toString() {
                return "`" + key + "` LIKE " + Database.enquote(value);
            }
        };
    }

    public static QueryCondition match(String key, String value) {
        return new QueryCondition() {
            @Override
            public String toString() {
                return "`" + key + "` MATCH " + Database.enquote(value);
            }
        };
    }

    public static QueryCondition in(String key, Object[] objects) {
        return new QueryCondition() {
            @Override
            public String toString() {
                StringBuilder s = new StringBuilder("`").append(key).append("` IN (");
                for (Object o : objects)
                    s.append(Database.getAsString(o)).append(", ");
                return s.delete(s.length()-2, s.length()).append(")").toString();
            }
        };
    }

    public static QueryCondition notIn(String key, Object[] objects) {
        return new QueryCondition() {
            @Override
            public String toString() {
                StringBuilder s = new StringBuilder("`").append(key).append("` NOT IN (");
                for (Object o : objects)
                    s.append(Database.getAsString(o)).append(", ");
                return s.delete(s.length()-2, s.length()).append(")").toString();
            }
        };
    }

    QueryCondition() {}

    @Override
    public abstract String toString();

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy