Last active
January 16, 2020 01:00
-
-
Save lokcito/6f811dfec5168738a009fedea9006368 to your computer and use it in GitHub Desktop.
Android Sqlite
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package info.rayrojas.avispa.models; | |
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 java.util.ArrayList; | |
import info.rayrojas.avispa.conf.Settings; | |
import info.rayrojas.avispa.helpers.DatabaseHelper; | |
public class Channel { | |
private int id; | |
public String name; | |
public String is_active; | |
public static DatabaseHelper dbInstance; | |
/* LocalStorage */ | |
public int getId() { | |
return id; | |
} | |
public void setId(int id) { | |
this.id = id; | |
} | |
public String getName() { | |
return name; | |
} | |
public void setName(String name) { | |
this.name = name; | |
} | |
public String getIsActive() { | |
return is_active; | |
} | |
public void setIsActive(String is_active) { | |
this.is_active = is_active; | |
} | |
public DatabaseHelper getDbInstance(Context _context) { | |
if ( Channel.dbInstance == null ) { | |
Channel.dbInstance = new DatabaseHelper(_context); | |
return Channel.dbInstance; | |
} | |
return Channel.dbInstance; | |
} | |
public void getActive(Context _context) { | |
SQLiteDatabase db = this.getDbInstance(_context).getReadableDatabase(); | |
String[] fields = new String[] {DatabaseHelper.Columns._ID, | |
DatabaseHelper.Columns.COLUMN_NAME_NAME, | |
DatabaseHelper.Columns.COLUMN_NAME_ACTIVE}; | |
String[] args = new String[] {"1"}; | |
Cursor c = db.query(DatabaseHelper.CHANNEL_TABLE_NAME, fields, | |
"is_active = ?", args, null, null, null); | |
//Nos aseguramos de que existe al menos un registro | |
if (c.moveToFirst()) { | |
do { | |
this.setId(c.getInt(0)); | |
this.setName(c.getString(1)); | |
this.setIsActive(c.getString(2)); | |
} while(c.moveToNext()); | |
} | |
} | |
public boolean isNonDefined() { | |
return this.id == 0; | |
} | |
public void updateLocal(Context _context) { | |
SQLiteDatabase db = this.getDbInstance(_context).getWritableDatabase(); | |
ContentValues values = new ContentValues(); | |
values.put(DatabaseHelper.Columns.COLUMN_NAME_NAME, this.getName()); | |
values.put(DatabaseHelper.Columns.COLUMN_NAME_ACTIVE, this.getIsActive()); | |
String[] args = new String[] {this.getId() + ""}; | |
long newRowId = db.update(DatabaseHelper.CHANNEL_TABLE_NAME, values, "_id = ?", args); | |
} | |
public void getOne(Context _context) { | |
SQLiteDatabase db = this.getDbInstance(_context).getReadableDatabase(); | |
String[] fields = new String[] {DatabaseHelper.Columns._ID, | |
DatabaseHelper.Columns.COLUMN_NAME_NAME, | |
DatabaseHelper.Columns.COLUMN_NAME_ACTIVE}; | |
String[] args = new String[] {this.getId() + ""}; | |
Cursor c = db.query(DatabaseHelper.CHANNEL_TABLE_NAME, fields, | |
null, null, null, null, null); | |
//Nos aseguramos de que existe al menos un registro | |
if (c.moveToFirst()) { | |
do { | |
this.setId(c.getInt(0)); | |
this.setName(c.getString(1)); | |
this.setIsActive(c.getString(2)); | |
} while(c.moveToNext()); | |
} | |
} | |
public ArrayList<Channel> getAll(Context _context) { | |
ArrayList<Channel> rows = new ArrayList<>(); | |
SQLiteDatabase db = this.getDbInstance(_context).getReadableDatabase(); | |
String[] fields = new String[] {DatabaseHelper.Columns._ID, | |
DatabaseHelper.Columns.COLUMN_NAME_NAME, | |
DatabaseHelper.Columns.COLUMN_NAME_ACTIVE}; | |
String[] args = new String[] {this.getId() + ""}; | |
Cursor c = db.query(DatabaseHelper.CHANNEL_TABLE_NAME, fields, | |
null, null, null, null, | |
DatabaseHelper.Columns._ID+" DESC"); | |
//Nos aseguramos de que existe al menos un registro | |
if (c.moveToFirst()) { | |
do { | |
Channel n = new Channel(); | |
n.setId(c.getInt(0)); | |
n.setName(c.getString(1)); | |
n.setIsActive(c.getString(2)); | |
rows.add(n); | |
} while(c.moveToNext()); | |
} | |
return rows; | |
} | |
public void unsetLocal(Context _context) { | |
SQLiteDatabase db = this.getDbInstance(_context).getWritableDatabase(); | |
String[] args = new String[] {this.getId() + ""}; | |
db.delete(DatabaseHelper.CHANNEL_TABLE_NAME, "_id = ?", args); | |
} | |
public void setActiveJustOne(Context _context) { | |
SQLiteDatabase db = this.getDbInstance(_context).getWritableDatabase(); | |
ContentValues values = new ContentValues(); | |
values.put(DatabaseHelper.Columns.COLUMN_NAME_ACTIVE, "0"); | |
long newRowId = db.update(DatabaseHelper.CHANNEL_TABLE_NAME, values, "1 = 1", null); | |
ContentValues values_ = new ContentValues(); | |
values_.put(DatabaseHelper.Columns.COLUMN_NAME_ACTIVE, "1"); | |
String[] args = new String[] {this.getId() + ""}; | |
long newRowId_ = db.update(DatabaseHelper.CHANNEL_TABLE_NAME, values_, "_id = ?", args); | |
} | |
public void setLocal(Context _context) { | |
SQLiteDatabase db = this.getDbInstance(_context).getWritableDatabase(); | |
ContentValues values = new ContentValues(); | |
values.put(DatabaseHelper.Columns.COLUMN_NAME_NAME, this.getName()); | |
values.put(DatabaseHelper.Columns.COLUMN_NAME_ACTIVE, "0"); | |
String[] args = new String[] {this.getId() + ""}; | |
if (this.id == 0) { | |
this.id = this.getAll(_context).size() + 1; | |
} | |
long newRowId = db.insert(DatabaseHelper.CHANNEL_TABLE_NAME, null, values); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package info.rayrojas.avispa.helpers; | |
import android.content.Context; | |
import android.database.sqlite.SQLiteDatabase; | |
import android.database.sqlite.SQLiteOpenHelper; | |
import android.provider.BaseColumns; | |
import info.rayrojas.avispa.conf.Settings; | |
import info.rayrojas.avispa.models.Credential; | |
public class DatabaseHelper extends SQLiteOpenHelper { | |
public static class Columns implements BaseColumns { | |
public static final String COLUMN_NAME_TOKEN = "token"; | |
public static final String COLUMN_NAME_NAME = "name"; | |
public static final String COLUMN_NAME_TITLE = "title"; | |
public static final String COLUMN_NAME_MESSAGE = "message"; | |
public static final String COLUMN_NAME_EXTRA = "extra"; | |
public static final String COLUMN_NAME_ACTIVE = "is_active"; | |
public static final String COLUMN_NAME_CHANNEL = "channel"; | |
public static final String COLUMN_NAME_EVENT = "event"; | |
public static final String COLUMN_NAME_CLIENT = "client"; | |
} | |
public static final String CREDENTIAL_TABLE_NAME = "avispa_credentials"; | |
private static final String CREDENTIAL_SQL_CREATE_TABLE = | |
"CREATE TABLE " + CREDENTIAL_TABLE_NAME + " (" + | |
Columns._ID + " INTEGER PRIMARY KEY," + | |
Columns.COLUMN_NAME_CLIENT + " TEXT, " + | |
Columns.COLUMN_NAME_TOKEN + " TEXT, " + | |
Columns.COLUMN_NAME_ACTIVE + " TEXT)"; | |
private static final String CREDENTIAL_SQL_DELETE_ENTRIES = | |
"DROP TABLE IF EXISTS " + CREDENTIAL_TABLE_NAME; | |
public static final String EVENT_TABLE_NAME = "avispa_events"; | |
private static final String EVENT_SQL_CREATE_TABLE = | |
"CREATE TABLE " + EVENT_TABLE_NAME + " (" + | |
Columns._ID + " INTEGER PRIMARY KEY," + | |
Columns.COLUMN_NAME_NAME + " TEXT, " + | |
Columns.COLUMN_NAME_ACTIVE + " TEXT)"; | |
private static final String EVENT_SQL_DELETE_ENTRIES = | |
"DROP TABLE IF EXISTS " + EVENT_TABLE_NAME; | |
public static final String CHANNEL_TABLE_NAME = "avispa_channels"; | |
private static final String CHANNEL_SQL_CREATE_TABLE = | |
"CREATE TABLE " + CHANNEL_TABLE_NAME + " (" + | |
Columns._ID + " INTEGER PRIMARY KEY," + | |
Columns.COLUMN_NAME_NAME + " TEXT, " + | |
Columns.COLUMN_NAME_ACTIVE + " TEXT)"; | |
private static final String CHANNEL_SQL_DELETE_ENTRIES = | |
"DROP TABLE IF EXISTS " + CHANNEL_TABLE_NAME; | |
public static final String NOTIFY_TABLE_NAME = "avispa_notifications"; | |
private static final String NOTIFY_SQL_CREATE_TABLE = | |
"CREATE TABLE " + NOTIFY_TABLE_NAME + " (" + | |
Columns._ID + " INTEGER PRIMARY KEY," + | |
Columns.COLUMN_NAME_TITLE + " TEXT," + | |
Columns.COLUMN_NAME_MESSAGE + " TEXT," + | |
Columns.COLUMN_NAME_EXTRA + " TEXT," + | |
Columns.COLUMN_NAME_EVENT + " TEXT," + | |
Columns.COLUMN_NAME_CHANNEL + " TEXT," + | |
Columns.COLUMN_NAME_TOKEN + " TEXT)"; | |
private static final String NOTIFY_SQL_DELETE_ENTRIES = | |
"DROP TABLE IF EXISTS " + NOTIFY_TABLE_NAME; | |
public DatabaseHelper(Context context) { | |
super(context, Settings.DATABASE_NAME, null, Settings.DATABASE_VERSION); | |
} | |
@Override | |
public void onCreate(SQLiteDatabase db) { | |
db.execSQL(NOTIFY_SQL_CREATE_TABLE); | |
db.execSQL(CHANNEL_SQL_CREATE_TABLE); | |
db.execSQL(EVENT_SQL_CREATE_TABLE); | |
db.execSQL(CREDENTIAL_SQL_CREATE_TABLE); | |
} | |
@Override | |
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { | |
db.execSQL(CREDENTIAL_SQL_DELETE_ENTRIES); | |
db.execSQL(EVENT_SQL_DELETE_ENTRIES); | |
db.execSQL(CHANNEL_SQL_DELETE_ENTRIES); | |
db.execSQL(NOTIFY_SQL_DELETE_ENTRIES); | |
onCreate(db); | |
} | |
@Override | |
public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) { | |
onUpgrade(db, oldVersion, newVersion); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Settings { | |
public static String INFO = "avispa:info"; | |
public static String DEBUG = "avispa:debug"; | |
public static final String DATABASE_NAME = "avispa_sqlite.db"; | |
public static final int DATABASE_VERSION = 6; | |
public static String CLIENT_TOKEN = ""; | |
public static String CLIENT_CHANNEL_INFO = ""; | |
public static String CLIENT_EVENT_INFO = ""; | |
public static String CLIENT_PROVIDER = ""; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment