package app.hongs.serv.common;

import app.hongs.HongsException;
import app.hongs.db.DB;
import app.hongs.db.Table;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.ByteArrayOutputStream;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * 简单数据存取模型
 * @author Hongs
 * @param <T>
 */
public class MRecord<T> implements IRecord<T> {

    private final Table table;

    public MRecord() throws HongsException {
        table = DB.getInstance("common").getTable("record");
    }

    /**
     * 获取数据
     * @param key
     * @return
     * @throws app.hongs.HongsException
     */
    @Override
    public T get(String key) throws HongsException {
        long now = System.currentTimeMillis() / 1000;

        try (
            ResultSet rs = table.db.query(
  "SELECT `data` FROM `" + table.tableName + "` WHERE id = ? AND (xtime > ? OR xtime == 0)"
            , 0,1, key, now).getReusltSet();
        ) {
            if (! rs.next()) {
                return null;
            }

            // 反序列化
            try (
                InputStream ins = rs.getBinaryStream(1);
                ObjectInputStream ois = new ObjectInputStream(ins);
            ) {
                return ( T ) ois.readObject(  );
            }
        }
        catch (SQLException ex) {
            throw new HongsException.Common(ex);
        }
        catch ( IOException ex) {
            throw new HongsException.Common(ex);
        }
        catch (ClassNotFoundException ex) {
            throw new HongsException.Common(ex);
        }
    }

    /**
     * 设置数据
     * @param key
     * @param val
     * @param exp
     */
    @Override
    public void set(String key, T val, long exp) throws HongsException {
        long now = System.currentTimeMillis() / 1000;

        // 序列化值
        byte[] arr;
        try (
            ByteArrayOutputStream bos = new ByteArrayOutputStream(   );
               ObjectOutputStream out = new    ObjectOutputStream(bos);
        ) {
            out.writeObject ( val );
            out.flush();
            arr = bos.toByteArray();
        }
        catch (IOException ex) {
            throw new HongsException.Common(ex);
        }

        table.db.open( );
        table.db.ready();

        try (
            PreparedStatement ps = table.db.prepareStatement(
                 "DELETE FROM `" + table.tableName + "` WHERE id = ?"
            );
        ) {
            ps.setString(1, key);
            ps.executeUpdate(  );
        }
        catch (SQLException ex ) {
            throw new HongsException.Common(ex);
        }

        try (
            PreparedStatement ps = table.db.prepareStatement(
                 "INSERT INTO `" + table.tableName + "` (id, data, xtime, ctime) VALUES (?, ?, ?, ?)"
            );
        ) {
            ps.setString(1, key);
            ps.setBytes (2, arr);
            ps.setLong  (3, exp);
            ps.setLong  (4, now);
            ps.executeUpdate(  );
        }
        catch (SQLException ex ) {
            throw new HongsException.Common(ex);
        }
    }

    /**
     * 设置过期
     * @param key
     * @param exp
     * @throws HongsException
     */
    @Override
    public void set(String key, long exp) throws HongsException {
        table.db.open( );
        table.db.ready();

        try (
            PreparedStatement ps = table.db.prepareStatement(
                      "UPDATE `" + table.tableName + "` SET xtime = ? WHERE id = ?"
            );
        ) {
            ps.setString(2, key);
            ps.setLong  (1, exp);
            ps.executeUpdate(  );
        }
        catch (SQLException ex ) {
            throw new HongsException.Common(ex);
        }
    }

    /**
     * 删除数据
     * @param key
     */
    @Override
    public void del(String key) throws HongsException {
        table.delete("id = ?", key);
    }

    /**
     * 清除数据
     * @param exp
     */
    @Override
    public void del( long  exp) throws HongsException {
        table.delete("xtime <= ? AND xtime != 0", exp);
    }

}

@@ -109,16 +109,47 @@
 .Table;%0A
+import app.hongs.db.link.Loop;%0A
 import j
@@ -894,20 +894,15 @@
     
-ResultSet rs
+Loop lp
  = t
@@ -915,19 +915,16 @@
 b.query(
-%0A  
 %22SELECT 
@@ -937,34 +937,74 @@
  FROM %60%22
- + table.tableName
+%0A                    + table.tableName%0A                   
  + %22%60 WH
@@ -1073,16 +1073,59 @@
 ey, now)
+;%0A        ) %7B%0A            ResultSet rs = lp
 .getReus
@@ -1133,28 +1133,16 @@
 tSet();%0A
-        ) %7B%0A
         
@@ -4035,208 +4035,930 @@
 le.d
-elete(%22id = ?%22, key);%0A    %7D%0A%0A    /**%0A     * %E6%B8%85%E9%99%A4%E6%95%B0%E6%8D%AE%0A     * @param exp%0A     */%0A    @Override%0A    public void del( long  exp) throws HongsException %7B%0A        table.delete(%22xtime %3C= ? AND xtime != 0%22, exp);
+b.open( );%0A        table.db.ready();%0A%0A        try (%0A            PreparedStatement ps = table.db.prepareStatement(%0A                 %22DELETE FROM %60%22 + table.tableName + %22%60 WHERE id = ?%22%0A            );%0A        ) %7B%0A            ps.setString(1, key);%0A            ps.executeUpdate(  );%0A        %7D%0A        catch (SQLException ex ) %7B%0A            throw new HongsException.Common(ex);%0A        %7D%0A    %7D%0A%0A    /**%0A     * %E6%B8%85%E9%99%A4%E6%95%B0%E6%8D%AE%0A     * @param exp%0A     */%0A    @Override%0A    public void del( long  exp) throws HongsException %7B%0A        table.db.open( );%0A        table.db.ready();%0A%0A        try (%0A            PreparedStatement ps = table.db.prepareStatement(%0A                 %22DELETE FROM %60%22 + table.tableName + %22%60 WHERE xtime %3C= ? AND xtime != ?%22%0A            );%0A        ) %7B%0A            ps.setLong  (1, exp);%0A            ps.executeUpdate(  );%0A        %7D%0A        catch (SQLException ex ) %7B%0A            throw new HongsException.Common(ex);%0A        %7D
 %0A   

修正 Statement 未关闭的问题