<commit_before>package cat.ppicas.spades;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import cat.ppicas.spades.query.Query;

public abstract class Dao<T extends Entity> {

	public static interface EntityConsumer<T extends Entity> {
		public void accept(T entity, Cursor cursor);
	}

	protected SQLiteDatabase mDb;
	private Table mTable;
	private EntityMapper<T> mMapper;
	private List<Field> mRelatedFields = new ArrayList<Field>();

	public Dao(SQLiteDatabase db, Table table, EntityMapper<T> mapper) {
		mDb = db;
		mTable = table;
		mMapper = mapper;

		// Search for columns with a MappedField containing a Field of type
		// Related in order to add into mRelatedFields list.
		for (Column column : table.getColumns()) {
			if (column.mappedField != null) {
				Field field = column.mappedField.getField();
				if (field.getType() == Related.class) {
					mRelatedFields.add(field);
				}
			}
		}
		// Also add related Field from the Table RelatedInverse collection.
		for (RelatedInverse related : table.getRelatedInverses()) {
			mRelatedFields.add(related.getRelatedField());
		}
	}

	public long insert(T entity) {
		ContentValues values = new ContentValues();
		mMapper.putContentValues(entity, values);
		values.putNull(mTable.getColumnId().name);

		long newId = mDb.insertOrThrow(mTable.getName(), null, values);
		if (newId != -1) {
			entity.setEntityId(newId);
		}

		return newId;
	}

	public int update(T entity) {
		ContentValues values = new ContentValues();
		mMapper.putContentValues(entity, values);
		String colId = mTable.getColumnId().name;
		if (values.containsKey(colId)) {
			values.remove(colId);
		}

		return mDb.update(mTable.getName(), values, colId + "=" + entity.getEntityId(), null);
	}

	public boolean save(T entity) {
		if (entity.getEntityId() > 0) {
			return (update(entity) > 0);
		} else {
			return (insert(entity) != -1);
		}
	}

	public int delete(long id) {
		return mDb.delete(mTable.getName(), mTable.getColumnId().name + "=" + id, null);
	}

	public T get(long id) {
		Cursor cursor = mDb.query(mTable.getName(), new String[] { "*" },
				mTable.getColumnId().name + "=" + id, null, null, null, null, "1");
		try {
			return fetchFirst(cursor, mMapper.getDefaultMappings());
		} finally {
			cursor.close();
		}
	}

	public List<T> getAll() {
		Cursor cursor = mDb.query(mTable.getName(), new String[] { "*" }, null, null, null, null, null);
		try {
			return fetchAll(cursor, mMapper.getDefaultMappings());
		} finally {
			cursor.close();
		}
	}

	public T fetchFirst(Cursor cursor, int[][] mappings) {
		if (!cursor.moveToFirst() || mTable.index >= mappings.length
				|| mappings[mTable.index] == null) {
			return null;
		}

		T entity = mMapper.createFromCursor(cursor, mappings[mTable.index]);
		if (entity != null) {
			fetchRelatedFields(cursor, mappings, entity);
		}

		return entity;
	}

	public T fetchFirst(Query query) {
		Cursor cursor = query.execute(mDb);
		try {
			return fetchFirst(cursor, query.getMappings());
		} finally {
			cursor.close();
		}
	}

	public List<T> fetchAll(Cursor cursor, int[][] mappings) {
		return fetchAll(cursor, mappings, null);
	}

	public List<T> fetchAll(Cursor cursor, int[][] mappings, EntityConsumer<T> consumer) {
		ArrayList<T> list = new ArrayList<T>();

		if (mTable.index < mappings.length && mappings[mTable.index] != null) {
			cursor.moveToPosition(-1);
			while (cursor.moveToNext()) {
				T entity = mMapper.createFromCursor(cursor, mappings[mTable.index]);
				if (entity != null) {
					fetchRelatedFields(cursor, mappings, entity);
					if (consumer != null) {
						consumer.accept(entity, cursor);<commit_msg>Fixed API inconsistency in EntityConsumer interface<commit_after>package cat.ppicas.spades;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import cat.ppicas.spades.query.Query;

public abstract class Dao<T extends Entity> {

	public static interface EntityConsumer<T extends Entity> {
		public void accept(Cursor cursor, int[][] mappings, T entity);
	}

	protected SQLiteDatabase mDb;
	private Table mTable;
	private EntityMapper<T> mMapper;
	private List<Field> mRelatedFields = new ArrayList<Field>();

	public Dao(SQLiteDatabase db, Table table, EntityMapper<T> mapper) {
		mDb = db;
		mTable = table;
		mMapper = mapper;

		// Search for columns with a MappedField containing a Field of type
		// Related in order to add into mRelatedFields list.
		for (Column column : table.getColumns()) {
			if (column.mappedField != null) {
				Field field = column.mappedField.getField();
				if (field.getType() == Related.class) {
					mRelatedFields.add(field);
				}
			}
		}
		// Also add related Field from the Table RelatedInverse collection.
		for (RelatedInverse related : table.getRelatedInverses()) {
			mRelatedFields.add(related.getRelatedField());
		}
	}

	public long insert(T entity) {
		ContentValues values = new ContentValues();
		mMapper.putContentValues(entity, values);
		values.putNull(mTable.getColumnId().name);

		long newId = mDb.insertOrThrow(mTable.getName(), null, values);
		if (newId != -1) {
			entity.setEntityId(newId);
		}

		return newId;
	}

	public int update(T entity) {
		ContentValues values = new ContentValues();
		mMapper.putContentValues(entity, values);
		String colId = mTable.getColumnId().name;
		if (values.containsKey(colId)) {
			values.remove(colId);
		}

		return mDb.update(mTable.getName(), values, colId + "=" + entity.getEntityId(), null);
	}

	public boolean save(T entity) {
		if (entity.getEntityId() > 0) {
			return (update(entity) > 0);
		} else {
			return (insert(entity) != -1);
		}
	}

	public int delete(long id) {
		return mDb.delete(mTable.getName(), mTable.getColumnId().name + "=" + id, null);
	}

	public T get(long id) {
		Cursor cursor = mDb.query(mTable.getName(), new String[] { "*" },
				mTable.getColumnId().name + "=" + id, null, null, null, null, "1");
		try {
			return fetchFirst(cursor, mMapper.getDefaultMappings());
		} finally {
			cursor.close();
		}
	}

	public List<T> getAll() {
		Cursor cursor = mDb.query(mTable.getName(), new String[] { "*" }, null, null, null, null, null);
		try {
			return fetchAll(cursor, mMapper.getDefaultMappings());
		} finally {
			cursor.close();
		}
	}

	public T fetchFirst(Cursor cursor, int[][] mappings) {
		if (!cursor.moveToFirst() || mTable.index >= mappings.length
				|| mappings[mTable.index] == null) {
			return null;
		}

		T entity = mMapper.createFromCursor(cursor, mappings[mTable.index]);
		if (entity != null) {
			fetchRelatedFields(cursor, mappings, entity);
		}

		return entity;
	}

	public T fetchFirst(Query query) {
		Cursor cursor = query.execute(mDb);
		try {
			return fetchFirst(cursor, query.getMappings());
		} finally {
			cursor.close();
		}
	}

	public List<T> fetchAll(Cursor cursor, int[][] mappings) {
		return fetchAll(cursor, mappings, null);
	}

	public List<T> fetchAll(Cursor cursor, int[][] mappings, EntityConsumer<T> consumer) {
		ArrayList<T> list = new ArrayList<T>();

		if (mTable.index < mappings.length && mappings[mTable.index] != null) {
			cursor.moveToPosition(-1);
			while (cursor.moveToNext()) {
				T entity = mMapper.createFromCursor(cursor, mappings[mTable.index]);
				if (entity != null) {
					fetchRelatedFields(cursor, mappings, entity);
					if (consumer != null) {
						consumer.accept(cursor, mappings, entity);
{'fraction_non_alphanumeric': 0.13896885632033498, 'fraction_numerical': 0.0015702695629416384, 'mean_word_length': 4.091938707528315, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 33, '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': '2958198', 'n_tokens_mistral': 2761, 'n_tokens_neox': 2430, 'n_words': 607}