<commit_before>package org.kered.dko;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

import org.kered.dko.Condition.Binary2;
import org.kered.dko.Constants.CALENDAR;
import org.kered.dko.Constants.DB_TYPE;
import org.kered.dko.Table.__SimplePrimaryKey;

/**
 * A collection of SQL functions. &nbsp; Use these to call functions on fields
 * or values in the generated SQL. &nbsp; For example:
 *
 * <pre>   import static org.kered.dko.Function.*;
 *   SomeClass.ALL.where(SomeClass.BIRTH_DATE.gt(NOW())).count()</pre>
 *
 * <p>would generate:
 *
 * <pre>   select * from some_class where birth_date > NOW()</pre>
 *
 * @author <NAME>
 */
public abstract class SQLFunction<T> {

	/**
	 * Create a SQL function for IFNULL() (ISNULL() on SQL Server).
	 * @param f
	 * @param v
	 * @return
	 */
	public static <T> SQLFunction<Boolean> IFNULL(final Field<? extends T> f, final T v) {
		final Object[] oa = new Object[] {f, v};
		return new Custom<Boolean>(", ", "ifnull", "isnull", "ifnull", oa);
	}

	/**
	 * Create a SQL function for IFNULL() (ISNULL() on SQL Server).
	 * @param f
	 * @param v
	 * @return
	 */
	public static <T> SQLFunction<Boolean> IFNULL(final SQLFunction<? extends T> f, final T v) {
		final Object[] oa = new Object[] {f, v};
		return new Custom<Boolean>(", ", "ifnull", "isnull", "ifnull", oa);
	}

	/**
	 * @return the sql NOW() function (or GETDATE() on sql server)
	 */
	public static SQLFunction<java.sql.Date> NOW() {
		final Object[] oa = new Object[] {};
		return new Custom<java.sql.Date>(", ", "now", "getdate", "now", oa);
	}

	/**
	 * GETDATE and GETUTCDATE functions both return the current date and time. However,
	 * GETUTCDATE returns the current Universal Time Coordinate (UTC) time, whereas
	 * GETDATE returns the date and time on the computer where SQL Server is running.
	 * @return same as NOW()
	 */
	public static SQLFunction<java.sql.Date> GETDATE() {
		return NOW();
	}

	/**
	 * GETDATE and GETUTCDATE functions both return the current date and time. However,
	 * GETUTCDATE returns the current Universal Time Coordinate (UTC) time, whereas
	 * GETDATE returns the date and time on the computer where SQL Server is running.
	 * @return the sql GETUTCDATE() function
	 */
	public static SQLFunction<java.sql.Date> GETUTCDATE() {
		return new Custom<java.sql.Date>(", ", "GETUTCDATE", "GETUTCDATE", "GETUTCDATE");
	}

	/**
	 * COALESCE is a sql function that will return the first non-null parameter value.
	 * All objects other than functions or fields are passed verbatim to the
	 * PreparedStatement with setObject().
	 * @param fields
	 * @return
	 */
	public static <T> SQLFunction<T> COALESCE(final Object... fields) {
		return new Custom<T>("coalesce", fields);
	}

	/**
	 * DATEADD function is deterministic; it adds a certain period of time to the
	 * existing date and time value.
	 * @param f1
	 * @param count
	 * @param component
	 * @return
	 */
	public static <T> SQLFunction<java.sql.Date> DATEADD(final SQLFunction<? extends T> f1, final int count, final CALENDAR component) {
		return new SQLFunction<java.sql.Date>() {
			@Override
			void getSQL(final StringBuffer sb, final List<Object> bindings, final SqlContext context) {
				if (context.dbType == DB_TYPE.MYSQL) {
					sb.append("date_add(");
					f1.getSQL(sb, bindings, context);
					sb.append(", interval ? "+ component +")");
					bindings.add(count);
				} else if ((context.dbType == DB_TYPE.HSQL)) {
					sb.append("TIMESTAMPADD(SQL_TSI_" + component +", ?, ");
					bindings.add(count);
					f1.getSQL(sb, bindings, context);
					sb.append(")");
				} else {
					sb.append("dateadd(" + component +", ?, ");
					bindings.add(count);
					f1.getSQL(sb, bindings, context);
					sb.append(")");
				}
			}
		};

	}<commit_msg>fixed NPE<commit_after>package org.kered.dko;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

import org.kered.dko.Condition.Binary2;
import org.kered.dko.Constants.CALENDAR;
import org.kered.dko.Constants.DB_TYPE;
import org.kered.dko.Table.__SimplePrimaryKey;

/**
 * A collection of SQL functions. &nbsp; Use these to call functions on fields
 * or values in the generated SQL. &nbsp; For example:
 *
 * <pre>   import static org.kered.dko.Function.*;
 *   SomeClass.ALL.where(SomeClass.BIRTH_DATE.gt(NOW())).count()</pre>
 *
 * <p>would generate:
 *
 * <pre>   select * from some_class where birth_date > NOW()</pre>
 *
 * @author <NAME>
 */
public abstract class SQLFunction<T> {

	/**
	 * Create a SQL function for IFNULL() (ISNULL() on SQL Server).
	 * @param f
	 * @param v
	 * @return
	 */
	public static <T> SQLFunction<Boolean> IFNULL(final Field<? extends T> f, final T v) {
		final Object[] oa = new Object[] {f, v};
		return new Custom<Boolean>(", ", "ifnull", "isnull", "ifnull", oa);
	}

	/**
	 * Create a SQL function for IFNULL() (ISNULL() on SQL Server).
	 * @param f
	 * @param v
	 * @return
	 */
	public static <T> SQLFunction<Boolean> IFNULL(final SQLFunction<? extends T> f, final T v) {
		final Object[] oa = new Object[] {f, v};
		return new Custom<Boolean>(", ", "ifnull", "isnull", "ifnull", oa);
	}

	/**
	 * @return the sql NOW() function (or GETDATE() on sql server)
	 */
	public static SQLFunction<java.sql.Date> NOW() {
		final Object[] oa = new Object[] {};
		return new Custom<java.sql.Date>(", ", "now", "getdate", "now", oa);
	}

	/**
	 * GETDATE and GETUTCDATE functions both return the current date and time. However,
	 * GETUTCDATE returns the current Universal Time Coordinate (UTC) time, whereas
	 * GETDATE returns the date and time on the computer where SQL Server is running.
	 * @return same as NOW()
	 */
	public static SQLFunction<java.sql.Date> GETDATE() {
		return NOW();
	}

	/**
	 * GETDATE and GETUTCDATE functions both return the current date and time. However,
	 * GETUTCDATE returns the current Universal Time Coordinate (UTC) time, whereas
	 * GETDATE returns the date and time on the computer where SQL Server is running.
	 * @return the sql GETUTCDATE() function
	 */
	public static SQLFunction<java.sql.Date> GETUTCDATE() {
		return new Custom<java.sql.Date>(", ", "GETUTCDATE", "GETUTCDATE", "GETUTCDATE");
	}

	/**
	 * COALESCE is a sql function that will return the first non-null parameter value.
	 * All objects other than functions or fields are passed verbatim to the
	 * PreparedStatement with setObject().
	 * @param fields
	 * @return
	 */
	public static <T> SQLFunction<T> COALESCE(final Object... fields) {
		return new Custom<T>("coalesce", fields);
	}

	/**
	 * DATEADD function is deterministic; it adds a certain period of time to the
	 * existing date and time value.
	 * @param f1
	 * @param count
	 * @param component
	 * @return
	 */
	public static <T> SQLFunction<java.sql.Date> DATEADD(final SQLFunction<? extends T> f1, final int count, final CALENDAR component) {
		return new SQLFunction<java.sql.Date>() {
			@Override
			void getSQL(final StringBuffer sb, final List<Object> bindings, final SqlContext context) {
				final DB_TYPE dbType = context==null ? null : context.dbType;
				if (dbType == DB_TYPE.MYSQL) {
					sb.append("date_add(");
					f1.getSQL(sb, bindings, context);
					sb.append(", interval ? "+ component +")");
					bindings.add(count);
				} else if ((dbType == DB_TYPE.HSQL)) {
					sb.append("TIMESTAMPADD(SQL_TSI_" + component +", ?, ");
					bindings.add(count);
					f1.getSQL(sb, bindings, context);
					sb.append(")");
				} else {
					sb.append("dateadd(" + component +", ?, ");
					bindings.add(count);
					f1.getSQL(sb, bindings, context);
					sb.append(")");
				}
			}
		};

	}
{'fraction_non_alphanumeric': 0.14120249869859447, 'fraction_numerical': 0.0015616866215512754, 'mean_word_length': 3.6575757575757577, 'pattern_counts': {'":': 0, '<': 59, '<?xml version=': 0, '>': 61, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6594157', 'n_tokens_mistral': 2710, 'n_tokens_neox': 2425, 'n_words': 789}