/**
  * 删除一条记录
  *
  * @param
  */
 public void deleteMessage(String key) {
   try {
     db.delete(db.getConnection(), MESSAGE_DB, RECEIVE_TIME + "=?", new String[] {key});
   } catch (Exception e) {
     e.printStackTrace();
   } finally {
     if (db.getConnection().isOpen()) {
       db.closeConnection(db.getConnection());
     }
   }
 }
 /**
  * 添加一条消息
  *
  * @param msg
  */
 public void addOneMessage(ReceiveMessage msg) {
   try {
     ContentValues cv = new ContentValues();
     cv.put(MESSAGE_TEXT, msg.message);
     cv.put(MESSAGE_STATE, msg.state);
     cv.put(RECEIVE_TIME, msg.time);
     db.save(db.getConnection(), MESSAGE_DB, cv);
   } catch (Exception e) {
     e.printStackTrace();
   } finally {
     if (db.getConnection().isOpen()) {
       db.closeConnection(db.getConnection());
     }
   }
 }
  static void shutdown() {
    if (baseDB != null) {
      baseDB.shutdown();
    }

    baseDB = null;
  }
 /**
  * 修改,更新一条记录
  *
  * @param groupid
  */
 public void updateMessage(ReceiveMessage msg) {
   try {
     ContentValues values = new ContentValues();
     values.put(MESSAGE_TEXT, msg.message);
     values.put(MESSAGE_STATE, msg.state);
     values.put(RECEIVE_TIME, msg.time);
     db.update(
         db.getConnection(), MESSAGE_DB, values, RECEIVE_TIME + "=?", new String[] {msg.time});
   } catch (Exception e) {
     e.printStackTrace();
   } finally {
     if (db.getConnection().isOpen()) {
       db.closeConnection(db.getConnection());
     }
   }
 }
  static boolean sessionExists(
      final String sessionName, final boolean bMustBeLive, final boolean bMustBeFinal) {
    if (!baseDB.sessionExists(sessionName)) {
      return false;
    }

    if (!bMustBeLive && !bMustBeFinal) {
      return true;
    }

    try {
      ReplaySessionInfo sessionInfo = getSessionInfo(sessionName);

      if (bMustBeLive && !sessionInfo.bIsLive) {
        return false;
      }

      if (bMustBeFinal && sessionInfo.bIsLive) {
        return false;
      }
    } catch (Exception e) {
      return false;
    }

    return true;
  }
 static List<ReplaySessionInfo> getRecentSessions(
     final String appName,
     final int version,
     final int changelist,
     final String userName,
     final int limit) {
   return baseDB.getRecentSessions(appName, version, changelist, userName, limit);
 }
 static List<ReplaySessionInfo> discoverSessions(
     final String appName,
     final int version,
     final int changelist,
     final String userString,
     final String metaString,
     final int limit) {
   return baseDB.discoverSessions(appName, version, changelist, userString, metaString, limit);
 }
 /** 批量添加 */
 public void saveMessageList(Vector<ReceiveMessage> list) {
   try {
     for (int i = 0; i < list.size(); i++) {
       ReceiveMessage msg = list.elementAt(i);
       ContentValues cv = new ContentValues();
       cv.put(MESSAGE_TEXT, msg.message);
       cv.put(MESSAGE_STATE, msg.state);
       cv.put(RECEIVE_TIME, msg.time);
       db.save(db.getConnection(), MESSAGE_DB, cv);
     }
   } catch (Exception e) {
     e.printStackTrace();
   } finally {
     if (db.getConnection().isOpen()) {
       db.closeConnection(db.getConnection());
     }
   }
 }
 static void writeSessionStream(
     final String sessionName,
     final String name,
     final String metaTime1,
     final String metaTime2,
     final InputStream inStream)
     throws ReplayException {
   baseDB.writeSessionStream(sessionName, name, metaTime1, metaTime2, inStream);
 }
 static void writeEventData(
     final String sessionName,
     final String eventGroup,
     final int eventTime1,
     final int eventTime2,
     final String eventMeta,
     byte[] eventData)
     throws ReplayException {
   baseDB.writeEventData(sessionName, eventGroup, eventTime1, eventTime2, eventMeta, eventData);
 }
 static String createViewer(final String sessionName, final String userName)
     throws ReplayException {
   try {
     final String viewerName = java.util.UUID.randomUUID().toString();
     baseDB.createViewer(sessionName, viewerName, userName);
     return viewerName;
   } catch (ReplayException e) {
     throw new ReplayException(
         "CreateViewer. Failed to create viewer. SessionPath: " + sessionName);
   }
 }
 static void refreshSession(
     final String sessionName,
     final boolean bIsLive,
     final boolean bIsIncomplete,
     final int NumChunks,
     final int DemoTimeInMS,
     final int SizeInBytes)
     throws ReplayException {
   baseDB.refreshSession(
       sessionName, bIsLive, bIsIncomplete, NumChunks, DemoTimeInMS, SizeInBytes);
 }
  /**
   * 获取所有分组列表
   *
   * @return
   */
  public Vector<ReceiveMessage> getMessageList() {
    messagelist.removeAllElements();
    try {
      String[] cols = new String[] {MESSAGE_TEXT, MESSAGE_STATE, RECEIVE_TIME};
      if (db.isTableExits(db.getConnection(), MESSAGE_DB)) {

      } else {
        initMessageTable();
      }
      System.out.println("table  name" + MESSAGE_DB);
      Cursor cursor = db.getConnection().query(MESSAGE_DB, cols, null, null, null, null, null);
      while (cursor.moveToNext()) {
        ReceiveMessage msg = new ReceiveMessage();
        msg.message = cursor.getString(cursor.getColumnIndex(MESSAGE_TEXT));
        msg.state = Integer.parseInt(cursor.getString(cursor.getColumnIndex(MESSAGE_STATE)));
        msg.time = cursor.getString(cursor.getColumnIndex(RECEIVE_TIME));
        messagelist.add(msg);
      }

      cursor.close();
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      if (db.getConnection().isOpen()) {
        db.closeConnection(db.getConnection());
      }
    }
    return this.messagelist;
  }
 /** 初始新建表 */
 public void initMessageTable() {
   String createSQL =
       "CREATE TABLE IF NOT EXISTS "
           + MESSAGE_DB
           + " ("
           + MESSAGE_STATE
           + " VARCHAR,"
           + MESSAGE_TEXT
           + " VARCHAR,"
           + RECEIVE_TIME
           + " VARCHAR"
           + ")";
   try {
     db.creatTable(db.getConnection(), createSQL);
   } catch (Exception e) {
     e.printStackTrace();
   } finally {
     if (db.getConnection().isOpen()) {
       db.closeConnection(db.getConnection());
     }
   }
 }
  @Override
  public void runSQL(String template) throws IOException, SQLException {
    if (template.startsWith(ALTER_COLUMN_NAME) || template.startsWith(ALTER_COLUMN_TYPE)) {

      String sql = buildSQL(template);

      String[] alterSqls = StringUtil.split(sql, CharPool.SEMICOLON);

      for (String alterSql : alterSqls) {
        if (!alterSql.startsWith("-- ")) {
          runSQL(alterSql);
        }
      }
    } else {
      super.runSQL(template);
    }
  }
  static String createSession(
      final String appName,
      final int version,
      final int changelist,
      final String friendlyName,
      final List<String> userNames,
      final String metaString)
      throws ReplayException {
    final UUID sessionId = java.util.UUID.randomUUID();
    final String sessionName = sessionId.toString();

    try {
      baseDB.createSession(
          appName, version, changelist, sessionName, friendlyName, userNames, metaString);
      return sessionName;
    } catch (ReplayException e) {
      throw new ReplayException(
          "CreateSession. Failed to create session directory. SessionPath: " + sessionName);
    }
  }
  public MessageTableManager(Context context, String time) {
    db = new BaseDB(context);
    time = time.replaceAll("-", "_"); // 数据库名不用有特殊符号和中文,因此先做替换操作
    MESSAGE_DB = MESSAGE_DB + "_" + time.substring(0, 10); // 将日期截取做为表名,如2011_06_10
    isNewInstall = true;
    try {
      if (db.isTableExits(db.getConnection(), MESSAGE_DB)) {
        Cursor cursor =
            db.getConnection()
                .query(
                    MESSAGE_DB,
                    new String[] {MESSAGE_STATE, MESSAGE_TEXT, RECEIVE_TIME},
                    null,
                    null,
                    null,
                    null,
                    null);
        if (cursor != null) {
          while (cursor.moveToNext()) {
            isNewInstall = false;
          }
        }
        cursor.close();
        db.closeConnection(db.getConnection());
      }
      if (isNewInstall) {

        initMessageTable();
      }
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      if (db.getConnection().isOpen()) {
        db.closeConnection(db.getConnection());
      }
    }
  }
 static BaseDB.StreamMetadata getStreamMetadata(final String sessionName, final String name)
     throws ReplayException {
   return baseDB.getStreamMetadata(sessionName, name);
 }
 static void longCleanup() {
   baseDB.longCleanup();
 }
 static void quickCleanup() {
   baseDB.quickCleanup();
 }
 static int printLog(final PrintWriter writer, final Level level, final String subFilter) {
   return baseDB.printLog(writer, level, subFilter);
 }
 static void log(final Level level, final String str) {
   baseDB.log(level, str);
 }
 static void refreshViewer(final String sessionName, final String viewerName)
     throws ReplayException {
   baseDB.refreshViewer(sessionName, viewerName);
 }
 static List<BaseDB.EventInfo> enumerateEvents(final String sessionName, final String eventGroup)
     throws ReplayException {
   return baseDB.enumerateEvents(sessionName, eventGroup);
 }
 static int getNumSessions(final String appName, final int version, final int changelist) {
   return baseDB.getNumSessions(appName, version, changelist);
 }
 static byte[] readEventData(final String eventId) throws ReplayException {
   return baseDB.readEventData(eventId);
 }
 static void deleteViewer(final String sessionName, final String viewerName)
     throws ReplayException {
   baseDB.deleteViewer(sessionName, viewerName);
 }
 static ReplaySessionInfo getSessionInfo(final String sessionName) throws ReplayException {
   return baseDB.getSessionInfo(sessionName);
 }
 static long readSessionStream(
     final String sessionName, final String name, final OutputStream outStream)
     throws ReplayException {
   return baseDB.readSessionStream(sessionName, name, outStream);
 }
 static boolean sessionStreamExists(final String sessionName, final String name) {
   return baseDB.sessionStreamExists(sessionName, name);
 }