<commit_before>package com.simplenote.android.persistence;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.provider.BaseColumns;
import android.util.Log;

import com.simplenote.android.Constants;
import com.simplenote.android.model.Note;

/**
 * Handle database access
 * @author bryanjswift
 */
public class SimpleNoteDao {
	/* Sql Column names */
	public static final String KEY = "key";
	public static final String TITLE = "title";
	public static final String BODY = "body";
	public static final String MODIFY = "modify";
	public static final String DELETED = "deleted";
	public static final String SYNCED = "synced";
	private static final String[] columns = new String[] { BaseColumns._ID, KEY, TITLE, BODY, MODIFY, DELETED, SYNCED };
	/* Database information/names */
	private static final String DATABASE_NAME = "simplenotes_data.db";
	private static final String DATABASE_TABLE = "notes";
	private static final int DATABASE_VERSION = 2;
	/* Internal constants */
	private static final String LOGGING_TAG = Constants.TAG + "DAO";
	/* Internal fields */
	private final DatabaseHelper dbHelper;
	/**
	 * Construct Dao with Activity context
	 * @param context - where the Dao was created
	 */
	public SimpleNoteDao(Context context) {
		this.dbHelper = new DatabaseHelper(context);
	}
	/**
	 * Handle the creation and upgrading of the database
	 * @author bryanjswift
	 */
	private static class DatabaseHelper extends SQLiteOpenHelper {
		/**
		 * Database creation sql statement
		 */
		private static final String DATABASE_CREATE = (String.format(
				"create table %s (%s integer primary key autoincrement, " +
				"%s text not null, %s text not null, %s text not null, " +
				"%s boolean default 0, %s boolean default 1);",
				DATABASE_TABLE, BaseColumns._ID, KEY, BODY, MODIFY, DELETED, SYNCED));

		DatabaseHelper(Context context) {
			super(context, DATABASE_NAME, null, DATABASE_VERSION);
		}

		@Override
		public void onCreate(SQLiteDatabase db) {
			db.execSQL(DATABASE_CREATE);
		}

		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
			Log.w(LOGGING_TAG, "Upgrading database from version " + oldVersion + " to " + newVersion);
			switch (oldVersion) {
				case 1:
					final String tmpTable = "tmp_notes";
					final String oldCols = BaseColumns._ID + "," + KEY + "," + BODY + "," + MODIFY + "," + DELETED + ", NOT needs_sync";
					final String cols = BaseColumns._ID + "," + KEY + "," + BODY + "," + MODIFY + "," + DELETED + "," + SYNCED;
					db.execSQL(DATABASE_CREATE.replace("table " + DATABASE_TABLE, "table " + tmpTable));
					db.execSQL("INSERT INTO " + tmpTable + " (" + cols + ") SELECT " + oldCols + " FROM " + DATABASE_TABLE + ";");
					db.execSQL("DROP TABLE " + DATABASE_TABLE);
					db.execSQL("ALTER TABLE tmp_notes RENAME TO " + DATABASE_TABLE);
					break;
				default:
					Log.i(LOGGING_TAG, "No upgrade necessary.");
					break;
			}
		}
	}
	/**
	 * Class to wrap a Cursor so values can be queried by column name
	 * @author bryanjswift
	 */
	private static class CursorWrapper {
		private final Cursor cursor;
		CursorWrapper(Cursor cursor) {
			this.cursor = cursor;
		}
		private String getString(String column) {
			return cursor.getString(cursor.getColumnIndex(column));
		}
		private long getLong(String column) {
			return cursor.getLong(cursor.getColumnIndex(column));
		}
		private boolean getBoolean(String column) {
			return cursor.getInt(cursor.getColumnIndex(column)) == 1;
		}
	}
	/**
	 * Write a Note to the database
	 * @param note to store
	 * @return the Note if it was saved successfully, null otherwise
	 */
	synchronized public Note save(Note note) {
		SQLiteDatabase db = dbHelper.getWritableDatabase();
		Note result = null;
		/* Setup values */
		ContentValues values = new ContentValues();
		values.put(KEY, note.getKey());
		values.put(TITLE, note.getTitle());
		values.put(BODY, note.getBody());
		values.put(MODIFY, note.getDateModified());
		values.put(DELETED, note.isDeleted());
		values.put(SYNCED, note.isSynced());
		/* Perform query */
		try {
			db.beginTransaction();
			if (note.getId() < 0) { // If no id set then must be creating a new note
				Log.i(LOGGING_TAG, "Inserting new note into DB");
				long id = db.insert(DATABASE_TABLE, null, values);
				if (id > -1) {
					result = note.setId(id);
				}
			} else { // id exists, updating existing note
				Log.i(LOGGING_TAG, String.format("Updating note with id: %d", note.getId()));
				int rows = db.update(DATABASE_TABLE, values, BaseColumns._ID + "=" + note.getId(), null);
				if (rows == 1) {
					result = note;
				}
			}
			if (note != null) {
				db.setTransactionSuccessful();
			}
		} finally {
			db.endTransaction();
			db.close();
		}
		return result;
	}<commit_msg>Remove references to title column<commit_after>package com.simplenote.android.persistence;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.provider.BaseColumns;
import android.util.Log;

import com.simplenote.android.Constants;
import com.simplenote.android.model.Note;

/**
 * Handle database access
 * @author bryanjswift
 */
public class SimpleNoteDao {
	/* Sql Column names */
	public static final String KEY = "key";
	public static final String BODY = "body";
	public static final String MODIFY = "modify";
	public static final String DELETED = "deleted";
	public static final String SYNCED = "synced";
	private static final String[] columns = new String[] { BaseColumns._ID, KEY, BODY, MODIFY, DELETED, SYNCED };
	/* Database information/names */
	private static final String DATABASE_NAME = "simplenotes_data.db";
	private static final String DATABASE_TABLE = "notes";
	private static final int DATABASE_VERSION = 2;
	/* Internal constants */
	private static final String LOGGING_TAG = Constants.TAG + "DAO";
	/* Internal fields */
	private final DatabaseHelper dbHelper;
	/**
	 * Construct Dao with Activity context
	 * @param context - where the Dao was created
	 */
	public SimpleNoteDao(Context context) {
		this.dbHelper = new DatabaseHelper(context);
	}
	/**
	 * Handle the creation and upgrading of the database
	 * @author bryanjswift
	 */
	private static class DatabaseHelper extends SQLiteOpenHelper {
		/**
		 * Database creation sql statement
		 */
		private static final String DATABASE_CREATE = (String.format(
				"create table %s (%s integer primary key autoincrement, " +
				"%s text not null, %s text not null, %s text not null, " +
				"%s boolean default 0, %s boolean default 1);",
				DATABASE_TABLE, BaseColumns._ID, KEY, BODY, MODIFY, DELETED, SYNCED));

		DatabaseHelper(Context context) {
			super(context, DATABASE_NAME, null, DATABASE_VERSION);
		}

		@Override
		public void onCreate(SQLiteDatabase db) {
			db.execSQL(DATABASE_CREATE);
		}

		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
			Log.w(LOGGING_TAG, "Upgrading database from version " + oldVersion + " to " + newVersion);
			switch (oldVersion) {
				case 1:
					final String tmpTable = "tmp_notes";
					final String oldCols = BaseColumns._ID + "," + KEY + "," + BODY + "," + MODIFY + "," + DELETED + ", NOT needs_sync";
					final String cols = BaseColumns._ID + "," + KEY + "," + BODY + "," + MODIFY + "," + DELETED + "," + SYNCED;
					db.execSQL(DATABASE_CREATE.replace("table " + DATABASE_TABLE, "table " + tmpTable));
					db.execSQL("INSERT INTO " + tmpTable + " (" + cols + ") SELECT " + oldCols + " FROM " + DATABASE_TABLE + ";");
					db.execSQL("DROP TABLE " + DATABASE_TABLE);
					db.execSQL("ALTER TABLE tmp_notes RENAME TO " + DATABASE_TABLE);
					break;
				default:
					Log.i(LOGGING_TAG, "No upgrade necessary.");
					break;
			}
		}
	}
	/**
	 * Class to wrap a Cursor so values can be queried by column name
	 * @author bryanjswift
	 */
	private static class CursorWrapper {
		private final Cursor cursor;
		CursorWrapper(Cursor cursor) {
			this.cursor = cursor;
		}
		private String getString(String column) {
			return cursor.getString(cursor.getColumnIndex(column));
		}
		private long getLong(String column) {
			return cursor.getLong(cursor.getColumnIndex(column));
		}
		private boolean getBoolean(String column) {
			return cursor.getInt(cursor.getColumnIndex(column)) == 1;
		}
	}
	/**
	 * Write a Note to the database
	 * @param note to store
	 * @return the Note if it was saved successfully, null otherwise
	 */
	synchronized public Note save(Note note) {
		SQLiteDatabase db = dbHelper.getWritableDatabase();
		Note result = null;
		/* Setup values */
		ContentValues values = new ContentValues();
		values.put(KEY, note.getKey());
		values.put(BODY, note.getBody());
		values.put(MODIFY, note.getDateModified());
		values.put(DELETED, note.isDeleted());
		values.put(SYNCED, note.isSynced());
		/* Perform query */
		try {
			db.beginTransaction();
			if (note.getId() < 0) { // If no id set then must be creating a new note
				Log.i(LOGGING_TAG, "Inserting new note into DB");
				long id = db.insert(DATABASE_TABLE, null, values);
				if (id > -1) {
					result = note.setId(id);
				}
			} else { // id exists, updating existing note
				Log.i(LOGGING_TAG, String.format("Updating note with id: %d", note.getId()));
				int rows = db.update(DATABASE_TABLE, values, BaseColumns._ID + "=" + note.getId(), null);
				if (rows == 1) {
					result = note;
				}
			}
			if (note != null) {
				db.setTransactionSuccessful();
			}
		} finally {
			db.endTransaction();
			db.close();
		}
		return result;
	}
{'fraction_non_alphanumeric': 0.12213349713349714, 'fraction_numerical': 0.001638001638001638, 'mean_word_length': 3.742233009708738, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '3629920', 'n_tokens_mistral': 3442, 'n_tokens_neox': 3026, 'n_words': 879}