libepg.util.db.AboutDB.java Source code

Java tutorial

Introduction

Here is the source code for libepg.util.db.AboutDB.java

Source

/*
 * Copyright (C) 2017 normal
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package libepg.util.db;

import java.lang.invoke.MethodHandles;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import libepg.ts.packet.TsPacket;
import loggingsupport.loggerfactory.LoggerFactory;
import org.apache.commons.codec.binary.Hex;
import org.apache.commons.logging.Log;

/**
 *
 * @author normal
 */
public final class AboutDB {

    /**
     * false?????????????
     */
    public static final boolean CLASS_LOG_OUTPUT_MODE = true;

    private static final Log LOG;

    static {
        final Class<?> myClass = MethodHandles.lookup().lookupClass();
        LOG = new LoggerFactory(myClass, AboutDB.CLASS_LOG_OUTPUT_MODE).getLOG();
        if (CLASS_LOG_OUTPUT_MODE == false) {
            LOG.info("?");
        }
    }

    public AboutDB() {
    }

    /**
     * SqLitDB????(????)
     */
    public static final String URL = "jdbc:sqlite::memory:";

    /**
     * ??
     */
    public static final String TABLE_NAME = "temp_packet_table";

    /**
     * ?
     */
    public static final String NUMBER = "number";
    /**
     * ?
     */
    public static final String PID = "pid";
    /**
     * ?
     */
    public static final String CONTINUITY_CONTROL = "continuity_control";
    /**
     * ?
     */
    public static final String LACKFLAG = "lackflag";
    /**
     * ?
     */
    public static final String PACKET = "packet";
    /**
     * ??SQL
     */
    public static final String CRATE_TABLE = "create table " + TABLE_NAME + " " + "( " + NUMBER
            + "  integer PRIMARY KEY AUTOINCREMENT, " + PID + " integer NOT NULL, " + CONTINUITY_CONTROL
            + " integer NOT NULL, " + LACKFLAG + " integer NOT NULL, " + PACKET + " blob NOT NULL" + ")";

    /**
     * 
     */
    public static final String RESET_AUTOINCREMENT = "delete from sqlite_sequence where name=" + TABLE_NAME;

    /**
     * ??SQL<br>
     * Int Pid <br>
     * Int Continuity_counter<br>
     * Int lackflag<br>
     * Bytes packet<br>
     */
    public static final String INSERT_SQL = "insert into " + TABLE_NAME + " (" + PID + "," + CONTINUITY_CONTROL
            + "," + LACKFLAG + "," + PACKET + ")" + " values (?,?,?,?)";

    /**
     * SQL
     */
    public static final String DELETE_ALL_SQL = "delete from " + TABLE_NAME;

    /**
    * ?DB?????
    * @param src 
    * @param conn DB??
    * @throws java.sql.SQLException 
    * @see libepg.util.db.AboutDB#CRATE_TABLE
    * @see java.sql.Connection#createStatement() 
    */
    public static synchronized void convertToTable(List<TsPacket> src, Connection conn) throws SQLException {
        Statement stmt = conn.createStatement();
        //?
        stmt.executeUpdate(AboutDB.CRATE_TABLE);
        //?PID??????
        for (TsPacket tsp : src) {
            PreparedStatement insertStatement = conn.prepareStatement(INSERT_SQL);
            insertStatement.setInt(1, tsp.getPid());
            insertStatement.setInt(2, tsp.getContinuity_counter());
            insertStatement.setInt(3, 0);
            insertStatement.setBytes(4, tsp.getData());
            insertStatement.executeUpdate();
        }
    }

    /**
     * ???????? ?????
     *
     * @param conn ??DB??
     * @return ?
     * @throws java.sql.SQLException
     * @see libepg.util.db.AboutDB#CRATE_TABLE
     */
    public static synchronized List<TsPacket> convertToList(Connection conn) throws SQLException {
        final String DUMP = "SELECT " + PACKET + " FROM " + TABLE_NAME + " ORDER BY number ASC";
        ResultSet rs = conn.createStatement().executeQuery(DUMP);

        //?
        List<TsPacket> packets = new ArrayList<>();
        while (rs.next()) {
            TsPacket tsp = new TsPacket(rs.getBytes(PACKET));
            packets.add(tsp);
        }
        return packets;
    }

    /**
     * (???)
     *
     * @param conn ??DB??
     * @throws java.sql.SQLException
     */
    public static void debug_dump_table(Connection conn) throws SQLException {
        if (LOG.isDebugEnabled()) {
            final String DUMP = "SELECT * FROM " + TABLE_NAME + " ORDER BY number ASC";
            ResultSet rs = conn.createStatement().executeQuery(DUMP);
            while (rs.next()) {
                StringBuilder sb1 = new StringBuilder();
                sb1.append(NUMBER);
                sb1.append(" = ");
                sb1.append(rs.getInt(NUMBER));
                sb1.append("####");
                sb1.append(PID);
                sb1.append(" = ");
                sb1.append(rs.getInt(PID));
                sb1.append("####");
                sb1.append(CONTINUITY_CONTROL);
                sb1.append(" = ");
                sb1.append(rs.getInt(CONTINUITY_CONTROL));
                sb1.append("####");
                sb1.append(LACKFLAG);
                sb1.append(" = ");
                sb1.append(rs.getInt(LACKFLAG));
                sb1.append("####");
                sb1.append(PACKET);
                sb1.append(" = ");
                sb1.append(Hex.encodeHexString(rs.getBytes(PACKET)));
                LOG.debug(sb1.toString());
            }
        }
    }
}