<commit_before>package cn.socialclock.db;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import java.util.ArrayList;
import java.util.List;

import cn.socialclock.model.AlarmEvent;
import cn.socialclock.utils.DatetimeFormatter;
import cn.socialclock.utils.SocialClockLogger;

/**
 * @author mapler
 * Manage DB tables and queries.
 */
public class AlarmEventDbAdapter {

    // table name
    private static final String TABLE_NAME = "alarm_event";

    /* table columns */
    private static final String COLUMN_EVENT_ID = "event_id";
    private static final String COLUMN_USER_ID = "user_id";
    private static final String COLUMN_START_AT = "start_at";
    private static final String COLUMN_END_AT = "end_at";
    private static final String COLUMN_SNOOZE_TIMES = "snooze_times";
    private static final String COLUMN_SYNC_AT = "sync_at";
    private static final String COLUMN_DELETED_AT = "deleted_at";
    private static final String[] COLUMNS = {
            COLUMN_EVENT_ID,
            COLUMN_USER_ID,
            COLUMN_START_AT,
            COLUMN_END_AT,
            COLUMN_SNOOZE_TIMES,
            COLUMN_SYNC_AT,
            COLUMN_DELETED_AT,
    };

    // alarm_event table create sql
    public static final String CREATE_TABLE_QUERY = "" +
            "CREATE TABLE " + TABLE_NAME + "(" +
            COLUMN_EVENT_ID + " TEXT PRIMARY KEY," +
            COLUMN_USER_ID + " TEXT," +
            COLUMN_START_AT + " DATETIME DEFAULT CURRENT_TIMESTAMP," +
            COLUMN_END_AT + " DATETIME DEFAULT NULL," +
            COLUMN_SNOOZE_TIMES + " INTEGER DEFAULT 0," +
            COLUMN_SYNC_AT + " DATETIME DEFAULT NULL," +
            COLUMN_DELETED_AT + " DATETIME DEFAULT NULL" +
            ")";

    // alarm_event table drop sql
    public static final String DROP_TABLE_QUERY = "drop table if exists " + TABLE_NAME;

    // SQLiteDatabase
    private SQLiteDatabase db;

    /**
     * Constructor
     * @param db SQLiteDatabase
     */
    public AlarmEventDbAdapter(SQLiteDatabase db) {
        this.db = db;
    }

    /**
     * get all data
     * @return alarmEventList list of AlarmEvent
     */
    public List<AlarmEvent> findAll() {
        List<AlarmEvent> alarmEventList = new ArrayList<>();
        Cursor cursor = db.query(
                TABLE_NAME,
                COLUMNS,
                null,
                null,
                null,
                null,
                COLUMN_START_AT);

        while(cursor.moveToNext()) {
            AlarmEvent alarmEvent = createAlarmEvent(cursor);
            alarmEventList.add(alarmEvent);
        }

        return alarmEventList;
    }

    /**
     * select by event id
     * @param eventId String AlarmEvent eventId
     * @return AlarmEvent
     */
    public AlarmEvent findByEventId(String eventId) {
        String selection = COLUMN_EVENT_ID + "='" + eventId + "'";
        Cursor cursor = db.query(
                TABLE_NAME,
                COLUMNS,
                selection,
                null,
                null,
                null,
                null);

        cursor.moveToNext();
        return createAlarmEvent(cursor);
    }

    /**
     * insert alarm event
     * @param alarmEvent AlarmEvent object
     */
    public long insert(AlarmEvent alarmEvent) {
        ContentValues values = new ContentValues();
        values.put(COLUMN_EVENT_ID, alarmEvent.getEventId());<commit_msg>Fix DbAdapter, Add Does not exist check and cursor close<commit_after>package cn.socialclock.db;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import java.util.ArrayList;
import java.util.List;

import cn.socialclock.model.AlarmEvent;
import cn.socialclock.utils.DatetimeFormatter;
import cn.socialclock.utils.SocialClockLogger;

/**
 * @author mapler
 * Manage DB tables and queries.
 */
public class AlarmEventDbAdapter {

    // table name
    private static final String TABLE_NAME = "alarm_event";

    /* table columns */
    private static final String COLUMN_EVENT_ID = "event_id";
    private static final String COLUMN_USER_ID = "user_id";
    private static final String COLUMN_START_AT = "start_at";
    private static final String COLUMN_END_AT = "end_at";
    private static final String COLUMN_SNOOZE_TIMES = "snooze_times";
    private static final String COLUMN_SYNC_AT = "sync_at";
    private static final String COLUMN_DELETED_AT = "deleted_at";
    private static final String[] COLUMNS = {
            COLUMN_EVENT_ID,
            COLUMN_USER_ID,
            COLUMN_START_AT,
            COLUMN_END_AT,
            COLUMN_SNOOZE_TIMES,
            COLUMN_SYNC_AT,
            COLUMN_DELETED_AT,
    };

    // alarm_event table create sql
    public static final String CREATE_TABLE_QUERY = "" +
            "CREATE TABLE " + TABLE_NAME + "(" +
            COLUMN_EVENT_ID + " TEXT PRIMARY KEY," +
            COLUMN_USER_ID + " TEXT," +
            COLUMN_START_AT + " DATETIME DEFAULT CURRENT_TIMESTAMP," +
            COLUMN_END_AT + " DATETIME DEFAULT NULL," +
            COLUMN_SNOOZE_TIMES + " INTEGER DEFAULT 0," +
            COLUMN_SYNC_AT + " DATETIME DEFAULT NULL," +
            COLUMN_DELETED_AT + " DATETIME DEFAULT NULL" +
            ")";

    // alarm_event table drop sql
    public static final String DROP_TABLE_QUERY = "drop table if exists " + TABLE_NAME;

    // SQLiteDatabase
    private SQLiteDatabase db;

    /**
     * Constructor
     * @param db SQLiteDatabase
     */
    public AlarmEventDbAdapter(SQLiteDatabase db) {
        this.db = db;
    }

    /**
     * get all data
     * @return alarmEventList list of AlarmEvent
     */
    public List<AlarmEvent> findAll() {
        List<AlarmEvent> alarmEventList = new ArrayList<>();
        Cursor cursor = db.query(
                TABLE_NAME,
                COLUMNS,
                null,
                null,
                null,
                null,
                COLUMN_START_AT);

        while(cursor.moveToNext()) {
            AlarmEvent alarmEvent = createAlarmEvent(cursor);
            alarmEventList.add(alarmEvent);
        }
        cursor.close();

        return alarmEventList;
    }

    /**
     * select by event id
     * @param eventId String AlarmEvent eventId
     * @return AlarmEvent or null (if does not exist)
     */
    public AlarmEvent findByEventId(String eventId) {
        String selection = COLUMN_EVENT_ID + "='" + eventId + "'";
        Cursor cursor = db.query(
                TABLE_NAME,
                COLUMNS,
                selection,
                null,
                null,
                null,
                null);
        AlarmEvent alarmEvent;
        /* check if exist */
        if (cursor.getCount() > 0) {
            cursor.moveToNext();
            alarmEvent = createAlarmEvent(cursor);
        }
        else {
            alarmEvent = null;
        }
        cursor.close();
        return alarmEvent;
    }

    /**
     * insert alarm event
     * @param alarmEvent AlarmEvent object
     */
    public long insert(AlarmEvent alarmEvent) {
        ContentValues values = new ContentValues();
        values.put(COLUMN_EVENT_ID, alarmEvent.getEventId());
{'fraction_non_alphanumeric': 0.08308198519330957, 'fraction_numerical': 0.0004112969564025226, 'mean_word_length': 2.0780590717299576, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 10, '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': '2274091', 'n_tokens_mistral': 2148, 'n_tokens_neox': 2122, 'n_words': 505}