static { try { Map<Object, Object> properties = getEntityManagerProperties(); Environment.verifyProperties(properties); ConfigurationHelper.resolvePlaceHolders(properties); ServiceRegistry reg = new ServiceRegistryBuilder().applySettings(properties).buildServiceRegistry(); DbOpenHelper openHelper = new DbOpenHelper(reg) { @Override public void onCreate() throws Exception { executeAllScript(0); } @Override public void onUpgrade(int oldVersion, int newVersion) throws Exception { // TODO Auto-generated method stub } }; openHelper.open(); emfInstance = Persistence.createEntityManagerFactory( "transactions-optional", getEntityManagerProperties()); } catch (Throwable t) { log.error("Error creating EMF", t); } }
@Override protected void setUp() throws Exception { RenamingDelegatingContext context = new RenamingDelegatingContext(getContext(), "test"); DbOpenHelper dbOpenHelper = new DbOpenHelper(context); db = dbOpenHelper.getWritableDatabase(); db.beginTransaction(); asteroidsDAO = new AsteroidsDAO(db); }
/** * Throws SprinklesNotInitializedException if you try to access the database before initializing * Sprinkles. * * @return the SQL Database used by Sprinkles. */ public static SQLiteDatabase getDatabase() { if (sInstance == null) { throw new SprinklesNotInitializedException(); } if (sDatabase == null) { DbOpenHelper dbOpenHelper = new DbOpenHelper( sInstance.mContext, sInstance.databaseName, sInstance.initialDatabaseVersion); sDatabase = dbOpenHelper.getWritableDatabase(); } return sDatabase; }
/** * @Title: updateRecordFlags @Description: check existence of those records, and update record * flags in database. * * @return boolean operation result * @throws */ @Deprecated public boolean updateRecordFlags() { SQLiteDatabase db = dbOpenHelper.getReadableDatabase(); String sql = "SELECT uid, date, record_flag FROM logs ORDER BY date DESC"; Cursor cursor = null; try { cursor = db.rawQuery(sql, null); while (cursor.moveToNext()) { String uid = cursor.getString(cursor.getColumnIndex("uid")); long date = Long.parseLong(cursor.getString(cursor.getColumnIndex("date"))); int recordFlag = cursor.getInt(cursor.getColumnIndex("record_flag")); boolean fileExists = Utils.isFileExists(uid, date); if (recordFlag == 0 && fileExists) { updateRecordFlag(uid, RecordFlagState.EXIST); updateRecordState(uid, RecordState.LOCAL); } else if (recordFlag == 1 && !fileExists) { updateRecordFlag(uid, RecordFlagState.NOT_EXIST); } } } catch (SQLException e) { e.printStackTrace(); return false; } finally { if (cursor != null && !cursor.isClosed()) { cursor.close(); } if (db != null && db.isOpen()) { db.close(); } } return true; }
/** * @Title: refreshRecords @Description: refresh records in database * * @return boolean operation result * @throws */ public boolean refreshRecords() { SQLiteDatabase db = dbOpenHelper.getWritableDatabase(); String sql = "SELECT uid, date FROM logs ORDER BY date"; Cursor cursor = null; try { cursor = db.rawQuery(sql, null); while (cursor.moveToNext()) { String uid = cursor.getString(cursor.getColumnIndex("uid")); long date = Long.parseLong(cursor.getString(cursor.getColumnIndex("date"))); if (!Utils.isFileExists(uid, date)) { // updateRecordState(uid, RecordState.DELETED); deleteLogByUid(uid); } } } catch (SQLException e) { e.printStackTrace(); return false; } finally { if (cursor != null && !cursor.isClosed()) { cursor.close(); } if (db != null && db.isOpen()) { db.close(); } } return true; }
/** * @Title: insertNewLog @Description: insert new log to database * * @param callLog MyCallLog instance * @param recordFlagState is record exists * @param recordState record storage state * @return boolean operation result * @throws */ public boolean insertNewLog( MyCallLog callLog, RecordFlagState recordFlagState, RecordState recordState) { SQLiteDatabase db = dbOpenHelper.getWritableDatabase(); String sql = "INSERT INTO logs (name, number, duration, date, type, uid, record_flag, log_state_flag, record_state_flag) VALUES(?, ?, ?, ?, ?, ?, ?, 0, ?)"; try { db.execSQL( sql, new Object[] { callLog.getName(), callLog.getNumber(), callLog.getDuration(), callLog.getDate(), callLog.getCallType().ordinal(), Check.getCRC32(callLog), recordFlagState.ordinal(), recordState.ordinal() }); } catch (SQLException e) { e.printStackTrace(); return false; } finally { if (db != null && db.isOpen()) { db.close(); } } return true; }
/** * @Title: getContacts @Description: get contacts * * @param page page (10 items per page) * @return ArrayList<MyContact> * @throws */ public ArrayList<MyContact> getContacts(int page) { SQLiteDatabase db = dbOpenHelper.getReadableDatabase(); String sql = "SELECT DISTINCT name, number FROM logs WHERE record_state_flag = " + RecordState.LOCAL.ordinal() + " ORDER BY date DESC limit " + (page * 10) + ", 10"; Cursor cursor = null; ArrayList<MyContact> contacts = new ArrayList<MyContact>(); try { cursor = db.rawQuery(sql, null); while (cursor.moveToNext()) { String name = cursor.getString(cursor.getColumnIndex("name")); String number = cursor.getString(cursor.getColumnIndex("number")); long date = getLatestContactDate(number); int count = getContactCount(number); contacts.add(new MyContact(name, number, date, count)); } return contacts; } catch (SQLException e) { e.printStackTrace(); return null; } finally { if (cursor != null && !cursor.isClosed()) { cursor.close(); } if (db != null && db.isOpen()) { db.close(); } } }
/** * @Title: getIndividualLogs @Description: get individual logs by page * * @param page * @return ArrayList<Log> * @throws */ public ArrayList<Log> getIndividualLogs(String number, int page) { SQLiteDatabase db = dbOpenHelper.getReadableDatabase(); String sql = "SELECT * FROM logs WHERE number like '%" + number + "%' AND record_state_flag = " + RecordState.LOCAL.ordinal() + " ORDER BY date DESC limit " + (page * 10) + ", 10"; // System.out.println(sql); Cursor cursor = null; ArrayList<Log> list = new ArrayList<Log>(); try { cursor = db.rawQuery(sql, null); while (cursor.moveToNext()) { int id = cursor.getInt(cursor.getColumnIndex("id")); String name = cursor.getString(cursor.getColumnIndex("name")); int duration = cursor.getInt(cursor.getColumnIndex("duration")); long date = Long.parseLong(cursor.getString(cursor.getColumnIndex("date"))); CallType callType = CallType.fromInt(cursor.getInt(cursor.getColumnIndex("type")) - 1); String uid = cursor.getString(cursor.getColumnIndex("uid")); RecordFlagState recordFlag = RecordFlagState.fromInt(cursor.getInt(cursor.getColumnIndex("record_flag"))); LogState logState = LogState.NOT_DELETED; RecordState recordState = RecordState.fromInt(cursor.getInt(cursor.getColumnIndex("record_state_flag"))); Timestamp addDate = Timestamp.valueOf(cursor.getString(cursor.getColumnIndex("update_date"))); list.add( new Log( id, name, number, duration, date, callType, uid, recordFlag, logState, recordState, addDate)); } return list; } catch (SQLException e) { e.printStackTrace(); return null; } finally { if (cursor != null && !cursor.isClosed()) { cursor.close(); } if (db != null && db.isOpen()) { db.close(); } } }
/** * 保存一个联系人 * * @param user */ public void saveContact(User user) { SQLiteDatabase db = dbHelper.getWritableDatabase(); ContentValues values = new ContentValues(); values.put(COLUMN_NAME_ID, user.getUsername()); if (user.getNick() != null) values.put(COLUMN_NAME_NICK, user.getNick()); if (user.getAvatar() != null) values.put(COLUMN_NAME_AVATAR, user.getAvatar()); if (db.isOpen()) { db.replace(TABLE_NAME, null, values); } }
public static T_WorkoutExercisesControl getInstance(Context context) { if (instance == null) { context = context.getApplicationContext(); ThreadFactory logThreadFactory = Executors.defaultThreadFactory(); Executor logExecutor = Executors.newSingleThreadExecutor(logThreadFactory); SQLiteOpenHelper openHelper = DbOpenHelper.getInstance(context, DBControl.DB_NAME, DBControl.DB_VERSION, logExecutor); instance = new T_WorkoutExercisesControl(context, logExecutor, openHelper); } return instance; }
/** * @Title: deleteLogByUid @Description: delete log by uid * * @param uid unique id of the log to be deleted * @return boolean operation result * @throws */ public boolean deleteLogByUid(String uid) { SQLiteDatabase db = dbOpenHelper.getWritableDatabase(); String sql = "DELETE FROM logs WHERE uid = \"" + uid + "\""; try { db.execSQL(sql); } catch (SQLException e) { e.printStackTrace(); return false; } finally { if (db != null && db.isOpen()) { db.close(); } } return true; }
/** * @Title: updateRecordState @Description: update record state * * @param uid the unique id of the record * @param state the state to set * @return boolean operation result * @throws */ public boolean updateRecordState(String uid, RecordState state) { SQLiteDatabase db = dbOpenHelper.getWritableDatabase(); String sql = "UPDATE logs SET record_state_flag = " + state.ordinal() + " WHERE uid = \"" + uid + "\""; try { db.execSQL(sql); } catch (SQLException e) { e.printStackTrace(); return false; } finally { if (db != null && db.isOpen()) { db.close(); } } return true; }
/** * 获取好友list * * @return */ public Map<String, User> getContactList() { SQLiteDatabase db = dbHelper.getReadableDatabase(); Map<String, User> users = new HashMap<String, User>(); if (db.isOpen()) { Cursor cursor = db.rawQuery("select * from " + TABLE_NAME /* + " desc" */, null); while (cursor.moveToNext()) { String username = cursor.getString(cursor.getColumnIndex(COLUMN_NAME_ID)); String nick = cursor.getString(cursor.getColumnIndex(COLUMN_NAME_NICK)); String avatar = cursor.getString(cursor.getColumnIndex(COLUMN_NAME_AVATAR)); User user = new User(); user.setUsername(username); user.setNick(nick); user.setAvatar(avatar); String headerName = null; if (!TextUtils.isEmpty(user.getNick())) { headerName = user.getNick(); } else { headerName = user.getUsername(); } if (username.equals(Constant.NEW_FRIENDS_USERNAME) || username.equals(Constant.GROUP_USERNAME)) { user.setHeader(""); } else if (Character.isDigit(headerName.charAt(0))) { user.setHeader("#"); } else { user.setHeader( HanziToPinyin.getInstance() .get(headerName.substring(0, 1)) .get(0) .target .substring(0, 1) .toUpperCase()); char header = user.getHeader().toLowerCase().charAt(0); if (header < 'a' || header > 'z') { user.setHeader("#"); } } users.put(username, user); } cursor.close(); } return users; }
/** * 保存message * * @param message * @return 返回这条messaged在db中的id */ public synchronized Integer saveMessage(InviteMessage message) { SQLiteDatabase db = dbHelper.getWritableDatabase(); int id = -1; if (db.isOpen()) { ContentValues values = new ContentValues(); values.put(COLUMN_NAME_FROM, message.getFrom()); values.put(COLUMN_NAME_GROUP_ID, message.getGroupId()); values.put(COLUMN_NAME_GROUP_Name, message.getGroupName()); values.put(COLUMN_NAME_REASON, message.getReason()); values.put(COLUMN_NAME_TIME, message.getTime()); values.put(COLUMN_NAME_STATUS, message.getStatus().ordinal()); db.insert(TABLE_NAME, null, values); Cursor cursor = db.rawQuery("select last_insert_rowid() from " + TABLE_NAME, null); if (cursor.moveToFirst()) { id = cursor.getInt(0); } } return id; }
/** * 获取messges * * @return */ public List<InviteMessage> getMessagesList() { SQLiteDatabase db = dbHelper.getReadableDatabase(); List<InviteMessage> msgs = new ArrayList<InviteMessage>(); if (db.isOpen()) { Cursor cursor = db.rawQuery("select * from " + TABLE_NAME + " desc", null); while (cursor.moveToNext()) { InviteMessage msg = new InviteMessage(); int id = cursor.getInt(cursor.getColumnIndex(COLUMN_NAME_ID)); String from = cursor.getString(cursor.getColumnIndex(COLUMN_NAME_FROM)); String groupid = cursor.getString(cursor.getColumnIndex(COLUMN_NAME_GROUP_ID)); String groupname = cursor.getString(cursor.getColumnIndex(COLUMN_NAME_GROUP_Name)); String reason = cursor.getString(cursor.getColumnIndex(COLUMN_NAME_REASON)); long time = cursor.getLong(cursor.getColumnIndex(COLUMN_NAME_TIME)); int isIniviteFromMe = cursor.getInt(cursor.getColumnIndex(COLUMN_NAME_ISINVITEFROMME)); int status = cursor.getInt(cursor.getColumnIndex(COLUMN_NAME_STATUS)); msg.setId(id); msg.setFrom(from); msg.setGroupId(groupid); msg.setGroupName(groupname); msg.setReason(reason); msg.setTime(time); if (status == InviteMesageStatus.BEINVITEED.ordinal()) msg.setStatus(InviteMesageStatus.BEINVITEED); else if (status == InviteMesageStatus.BEAGREED.ordinal()) msg.setStatus(InviteMesageStatus.BEAGREED); else if (status == InviteMesageStatus.BEREFUSED.ordinal()) msg.setStatus(InviteMesageStatus.BEREFUSED); else if (status == InviteMesageStatus.AGREED.ordinal()) msg.setStatus(InviteMesageStatus.AGREED); else if (status == InviteMesageStatus.REFUSED.ordinal()) msg.setStatus(InviteMesageStatus.REFUSED); else if (status == InviteMesageStatus.BEAPPLYED.ordinal()) { msg.setStatus(InviteMesageStatus.BEAPPLYED); } msgs.add(msg); } cursor.close(); } return msgs; }
/** * @Title: updateLogs @Description: update logs * * @param callLogs call logs to update * @return boolean the result of the operation * @throws */ @Deprecated public boolean updateLogs(List<MyCallLog> callLogs) { SQLiteDatabase db = dbOpenHelper.getWritableDatabase(); String sql = "SELECT uid, date FROM logs ORDER BY date"; Cursor cursor = null; try { cursor = db.rawQuery(sql, null); List<String> uids = new ArrayList<String>(); // update log_state while (cursor.moveToNext()) { String uid = cursor.getString(cursor.getColumnIndex("uid")); uids.add(uid); if (!Utils.containsUid(uid, callLogs)) { updateLogState(uid, LogState.ONLY_LOCAL); } } // insert new logs into table for (MyCallLog callLog : callLogs) { String tmp = Check.getCRC32(callLog); if (!uids.contains(tmp)) { insertNewLog(callLog); } } } catch (SQLException e) { e.printStackTrace(); return false; } finally { if (cursor != null && !cursor.isClosed()) { cursor.close(); } if (db != null && db.isOpen()) { db.close(); } } return true; }
/** * @Title: getLatestContactDate @Description: get latest contact date * * @param number contact number * @return long date * @throws */ public long getLatestContactDate(String number) { SQLiteDatabase db = dbOpenHelper.getReadableDatabase(); String sql = "SELECT date FROM logs WHERE number = '" + number + "' ORDER BY date DESC limit 1"; Cursor cursor = null; try { cursor = db.rawQuery(sql, null); if (cursor.moveToFirst()) { return Long.parseLong(cursor.getString(cursor.getColumnIndex("date"))); } } catch (SQLException e) { e.printStackTrace(); return 0; } finally { if (cursor != null && !cursor.isClosed()) { cursor.close(); } if (db != null && db.isOpen()) { db.close(); } } return 0; }
/** * @Title: getContactCount @Description: get count * * @param number number of the contact * @return int count * @throws */ public int getContactCount(String number) { SQLiteDatabase db = dbOpenHelper.getReadableDatabase(); String sql = ""; if (number.length() < 5) { sql = "SELECT count(*) as count FROM logs WHERE number = '" + number + "' AND record_state_flag = " + RecordState.LOCAL.ordinal(); } else { sql = "SELECT count(*) as count FROM logs WHERE number like '%" + number + "%' AND record_state_flag = " + RecordState.LOCAL.ordinal(); } Cursor cursor = null; // ArrayList<MyContact> contacts = new ArrayList<MyContact>(); try { cursor = db.rawQuery(sql, null); if (cursor.moveToFirst()) { return cursor.getInt(cursor.getColumnIndex("count")); } } catch (SQLException e) { e.printStackTrace(); return 0; } finally { if (cursor != null && !cursor.isClosed()) { cursor.close(); } if (db != null && db.isOpen()) { db.close(); } } return 0; }
public SqliteJobQueue(Configuration configuration, long sessionId, JobSerializer serializer) { this.sessionId = sessionId; jobStorage = new FileStorage(configuration.getAppContext(), "jobs_" + configuration.getId()); whereQueryCache = new WhereQueryCache(sessionId); dbOpenHelper = new DbOpenHelper( configuration.getAppContext(), configuration.isInTestMode() ? null : ("db_" + configuration.getId())); db = dbOpenHelper.getWritableDatabase(); sqlHelper = new SqlHelper( db, DbOpenHelper.JOB_HOLDER_TABLE_NAME, DbOpenHelper.ID_COLUMN.columnName, DbOpenHelper.COLUMN_COUNT, DbOpenHelper.JOB_TAGS_TABLE_NAME, DbOpenHelper.TAGS_COLUMN_COUNT, sessionId); this.jobSerializer = serializer; if (configuration.resetDelaysOnRestart()) { sqlHelper.resetDelayTimesTo(JobManager.NOT_DELAYED_JOB_DELAY); } cleanupFiles(); }
/** * 更新message * * @param msgId * @param values */ public void updateMessage(int msgId, ContentValues values) { SQLiteDatabase db = dbHelper.getWritableDatabase(); if (db.isOpen()) { db.update(TABLE_NAME, values, COLUMN_NAME_ID + " = ?", new String[] {String.valueOf(msgId)}); } }
public InviteMessgeDao(Context context) { dbHelper = DbOpenHelper.getInstance(context); }
public void deleteMessage(String from) { SQLiteDatabase db = dbHelper.getWritableDatabase(); if (db.isOpen()) { db.delete(TABLE_NAME, COLUMN_NAME_FROM + " = ?", new String[] {from}); } }
public SQLiteDatabase getReadableDatabase() { return mDatabaseOpenHelper.getReadableDatabase(); }
/** * 删除一个联系人 * * @param username */ public void deleteContact(String username) { SQLiteDatabase db = dbHelper.getWritableDatabase(); if (db.isOpen()) { db.delete(TABLE_NAME, COLUMN_NAME_ID + " = ?", new String[] {username}); } }
public UserDao(Context context) { dbHelper = DbOpenHelper.getInstance(context); }