All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.hadoopz.MyDroidLib.orm.MySqliteTemplate Maven / Gradle / Ivy

There is a newer version: 1.0.62
Show newest version
/*
 * Copyright 2019 jw362j.
 *
 * 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 com.hadoopz.MyDroidLib.orm;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteStatement;
import com.hadoopz.MyDroidLib.orm.core.CompiledStatementSetter;
import com.hadoopz.MyDroidLib.orm.core.RowMapper;
import com.hadoopz.MyDroidLib.orm.core.SqliteTemplate;
import com.hadoopz.MyDroidLib.orm.core.TheDataBaseHelper;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author jw362j
 */
public class MySqliteTemplate implements SqliteTemplate {

    private Context mCntext;
    private TheDataBaseHelper dbHelper;

    public MySqliteTemplate(Context context, TheDataBaseHelper sqlitedatabase) {
        this.dbHelper = sqlitedatabase;
        this.mCntext = context;
    }

    public long[] batchInsert(String sql, CompiledStatementSetter statementSetter) {
        long record_ids[] = new long[statementSetter.getBatchSize()];
        SQLiteDatabase database = dbHelper.getWritableDatabase();
        SQLiteStatement stat = database.compileStatement(sql);
        database.beginTransaction();
        for (int i = 0; i < statementSetter.getBatchSize(); i++) {
            statementSetter.setValue(stat, i);
            record_ids[i] = stat.executeInsert();
        }
        database.setTransactionSuccessful();
        database.endTransaction();
        database.close();
        return record_ids;
    }

    public long[] batchUpdate(String sql, CompiledStatementSetter statementSetter) {
        long record_ids[] = new long[statementSetter.getBatchSize()];
        SQLiteDatabase database = dbHelper.getWritableDatabase();
        SQLiteStatement stat = database.compileStatement(sql);
        database.beginTransaction();
        for (int i = 0; i < statementSetter.getBatchSize(); i++) {
            statementSetter.setValue(stat, i);
            record_ids[i] = stat.executeUpdateDelete();
        }
        database.setTransactionSuccessful();
        database.endTransaction();
        database.close();
        return record_ids;
    }

    public List query(String sql_query, String[] queryParams, int[] queryParamsTypes, RowMapper rowMapper) {
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        Cursor cursor = db.rawQuery(sql_query, queryParams);
        List results = new ArrayList();
        int rowNum = 0;
        while (cursor.moveToNext()) {
            Object tmp_ = rowMapper.mapRow(cursor, rowNum++);
            results.add(tmp_);
        }
        cursor.close();
        db.close();
        return results;
    }

    

}