/*
 * Copyright (C) 2014 Michael Pardo
 *
 * 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 ollie.query;

import ollie.Model;
import ollie.Ollie;

public final class Delete extends QueryBase {
	private Delete() {
		super(null, null);
	}

	public static <T extends Model> From from(Class<T> table) {
		return new From<T>(new Delete(), table);
	}

	@Override
	public String getPartSql() {
		return "DELETE";
	}

	public static final class From<T extends Model> extends ExecutableQueryBase<T> {
		private From(Query parent, Class<T> table) {
			super(parent, table);
		}

		public Where<T> where(String where) {
			return where(where, (Object[]) null);
		}

		public Where<T> where(String where, Object... args) {
			return new Where<T>(this, mTable, where, args);
		}

		@Override
		public String getPartSql() {
			return "FROM " + Ollie.getTableName(mTable);
		}
	}

	public static final class Where<T extends Model> extends ExecutableQueryBase<T> {
		private String mWhere;
		private Object[] mWhereArgs;

		public Where(Query parent, Class<T> table, String where, Object[] args) {
			super(parent, table);
			mWhere = where;
			mWhereArgs = args;
		}

		public OrderBy<T> orderBy(String orderBy) {
			return new OrderBy<T>(this, mTable, orderBy);
		}

		public Limit<T> limit(String limits) {
			return new Limit<T>(this, mTable, limits);
		}

		@Override
		public String getPartSql() {
			return "WHERE " + mWhere;
		}

		@Override
		public String[] getPartArgs() {
			return toStringArray(mWhereArgs);
		}
	}

	public static final class OrderBy<T extends Model> extends QueryBase<T> {
		private String mOrderBy;

		private OrderBy(Query parent, Class<T> table, String orderBy) {
			super(parent, table);
			mOrderBy = orderBy;
		}

		public Limit<T> limit(String limits) {
			return new Limit<T>(this, mTable, limits);
		}

		@Override
		public String getPartSql() {
			return "ORDER BY " + mOrderBy;
		}

	}

	public static final class Limit<T extends Model> extends ExecutableQueryBase<T> {
		private String mLimit;

		private Limit(Query parent, Class<T> table, String limit) {
			super(parent, table);
			mLimit = limit;
		}

		public Offset<T> offset(String offset) {
			return new Offset<T>(this, mTable, offset);
		}

		@Override
		public String getPartSql() {
			return "LIMIT " + mLimit;
		}
	}

	public static final class Offset<T extends Model> extends ExecutableQueryBase<T> {
		private String mOffset;

		private Offset(Query parent, Class<T> table, String offset) {
			super(parent, table);
			mOffset = offset;
		}

		@Override
		protected String getPartSql() {
			return "OFFSET " + mOffset;
		}
	}
}

@@ -1671,1462 +1671,164 @@
 %0A%0A%09%09
-public OrderBy%3CT%3E orderBy(String orderBy) %7B%0A%09%09%09return new OrderBy%3CT%3E(this, mTable, orderBy);%0A%09%09%7D%0A%0A%09%09public Limit%3CT%3E limit(String limits) %7B%0A%09%09%09return new Limit%3CT%3E(this, mTable, limits);%0A%09%09%7D%0A%0A%09%09@Override%0A%09%09public String getPartSql() %7B%0A%09%09%09return %22WHERE %22 + mWhere;%0A%09%09%7D%0A%0A%09%09@Override%0A%09%09public String%5B%5D getPartArgs() %7B%0A%09%09%09return toStringArray(mWhereArgs);%0A%09%09%7D%0A%09%7D%0A%0A%09public static final class OrderBy%3CT extends Model%3E extends QueryBase%3CT%3E %7B%0A%09%09private String mOrderBy;%0A%0A%09%09private OrderBy(Query parent, Class%3CT%3E table, String orderBy) %7B%0A%09%09%09super(parent, table);%0A%09%09%09mOrderBy = orderBy;%0A%09%09%7D%0A%0A%09%09public Limit%3CT%3E limit(String limits) %7B%0A%09%09%09return new Limit%3CT%3E(this, mTable, limits);%0A%09%09%7D%0A%0A%09%09@Override%0A%09%09public String getPartSql() %7B%0A%09%09%09return %22ORDER BY %22 + mOrderBy;%0A%09%09%7D%0A%0A%09%7D%0A%0A%09public static final class Limit%3CT extends Model%3E extends ExecutableQueryBase%3CT%3E %7B%0A%09%09private String mLimit;%0A%0A%09%09private Limit(Query parent, Class%3CT%3E table, String limit) %7B%0A%09%09%09super(parent, table);%0A%09%09%09mLimit = limit;%0A%09%09%7D%0A%0A%09%09public Offset%3CT%3E offset(String offset) %7B%0A%09%09%09return new Offset%3CT%3E(this, mTable, offset);%0A%09%09%7D%0A%0A%09%09@Override%0A%09%09public String getPartSql() %7B%0A%09%09%09return %22LIMIT %22 + mLimit;%0A%09%09%7D%0A%09%7D%0A%0A%09public static final class Offset%3CT extends Model%3E extends ExecutableQueryBase%3CT%3E %7B%0A%09%09private String mOffset;%0A%0A%09%09private Offset(Query parent, Class%3CT%3E table, String offset) %7B%0A%09%09%09super(parent, table);%0A%09%09%09mOffset = offset;%0A%09%09%7D%0A%0A%09%09@Override%0A%09%09protected String getPartSql() %7B%0A%09%09%09return %22OFFSET %22 + mOffset
+@Override%0A%09%09public String getPartSql() %7B%0A%09%09%09return %22WHERE %22 + mWhere;%0A%09%09%7D%0A%0A%09%09@Override%0A%09%09public String%5B%5D getPartArgs() %7B%0A%09%09%09return toStringArray(mWhereArgs)
 ;%0A%09%09

Remove order by and limit. Apparently Android doesn't support these.