SHA256
Добавить линии связей и параметров пользователя
This commit is contained in:
@@ -122,6 +122,19 @@ public final class BlocksDAO {
|
||||
}
|
||||
public BlockEntry getByNumber(String bchName,int blockNumber)throws SQLException{try(Connection c=db.getConnection()){return getByNumber(c,bchName,blockNumber);} }
|
||||
|
||||
/** Последний принятый блок конкретной логической линии в текущем physical fork. */
|
||||
public BlockEntry getLastLineBlock(Connection c, String bchName, int msgType, int lineCode) throws SQLException {
|
||||
String sql = baseSelect()+" WHERE login=? AND fork_number=? AND msg_type=? AND line_code=? ORDER BY block_number DESC LIMIT 1";
|
||||
ChainRef r=chainRef(bchName);
|
||||
try(PreparedStatement ps=c.prepareStatement(sql)){
|
||||
ps.setString(1,r.login()); ps.setInt(2,r.fork()); ps.setInt(3,msgType); ps.setInt(4,lineCode);
|
||||
try(ResultSet rs=ps.executeQuery()){ return rs.next()?mapRow(rs):null; }
|
||||
}
|
||||
}
|
||||
public BlockEntry getLastLineBlock(String bchName, int msgType, int lineCode) throws SQLException {
|
||||
try(Connection c=db.getConnection()){ return getLastLineBlock(c,bchName,msgType,lineCode); }
|
||||
}
|
||||
|
||||
public List<BlockEntry> listRangeByNumber(String bchName,int from,int to)throws SQLException{try(Connection c=db.getConnection()){return listRangeByNumber(c,bchName,from,to);} }
|
||||
public List<BlockEntry> listRangeByNumber(Connection c,String bchName,int from,int to)throws SQLException{
|
||||
String sql=baseSelect()+" WHERE login=? AND fork_number=? AND block_number>=? AND block_number<=? ORDER BY block_number ASC";
|
||||
|
||||
@@ -523,7 +523,7 @@ CREATE TABLE IF NOT EXISTS blocks (
|
||||
block_bytes BYTEA NOT NULL,
|
||||
to_login TEXT,
|
||||
to_bch_name TEXT,
|
||||
to_fork_number INTEGER CHECK (to_fork_number IS NULL OR (to_fork_number >= 1 AND to_fork_number <= 999)),
|
||||
to_fork_number INTEGER CHECK (to_fork_number IS NULL OR (to_fork_number >= 0 AND to_fork_number <= 999)),
|
||||
to_block_number INTEGER CHECK (to_block_number IS NULL OR to_block_number >= 0),
|
||||
to_block_hash BYTEA,
|
||||
block_hash BYTEA NOT NULL,
|
||||
@@ -1110,6 +1110,7 @@ LANGUAGE plpgsql
|
||||
AS $$
|
||||
DECLARE
|
||||
prev_block RECORD;
|
||||
tail_block_number INTEGER;
|
||||
BEGIN
|
||||
IF NEW.line_code IS NULL
|
||||
AND NEW.prev_line_number IS NULL
|
||||
@@ -1149,26 +1150,35 @@ BEGIN
|
||||
RAISE EXCEPTION 'LINE_ERR_LINE_CODE_MISMATCH';
|
||||
END IF;
|
||||
|
||||
IF NEW.prev_line_number = NEW.line_code THEN
|
||||
SELECT block_number
|
||||
INTO tail_block_number
|
||||
FROM blocks
|
||||
WHERE bch_name = NEW.bch_name
|
||||
AND msg_type = NEW.msg_type
|
||||
AND line_code = NEW.line_code
|
||||
ORDER BY block_number DESC
|
||||
LIMIT 1;
|
||||
|
||||
IF tail_block_number IS NULL THEN
|
||||
IF NEW.prev_line_number <> NEW.line_code THEN
|
||||
RAISE EXCEPTION 'LINE_ERR_NOT_CURRENT_TAIL';
|
||||
END IF;
|
||||
IF NEW.this_line_number <> (CASE WHEN NEW.msg_type = 1 THEN 0 ELSE 1 END) THEN
|
||||
RAISE EXCEPTION 'LINE_ERR_FIRST_STEP_BAD_THIS';
|
||||
END IF;
|
||||
RETURN NEW;
|
||||
END IF;
|
||||
|
||||
IF NEW.prev_line_number <> tail_block_number THEN
|
||||
RAISE EXCEPTION 'LINE_ERR_NOT_CURRENT_TAIL';
|
||||
END IF;
|
||||
|
||||
IF prev_block.this_line_number IS NULL THEN
|
||||
RAISE EXCEPTION 'LINE_ERR_THIS_LINE_BAD_STEP';
|
||||
END IF;
|
||||
|
||||
IF NEW.msg_type = 1 THEN
|
||||
IF NEW.this_line_number <> prev_block.this_line_number
|
||||
AND NEW.this_line_number <> prev_block.this_line_number + 1 THEN
|
||||
RAISE EXCEPTION 'LINE_ERR_THIS_LINE_BAD_STEP';
|
||||
END IF;
|
||||
ELSE
|
||||
IF NEW.this_line_number <> prev_block.this_line_number + 1 THEN
|
||||
RAISE EXCEPTION 'LINE_ERR_THIS_LINE_BAD_STEP';
|
||||
END IF;
|
||||
IF NEW.this_line_number <> prev_block.this_line_number + 1 THEN
|
||||
RAISE EXCEPTION 'LINE_ERR_THIS_LINE_BAD_STEP';
|
||||
END IF;
|
||||
|
||||
RETURN NEW;
|
||||
@@ -2436,7 +2446,7 @@ $$;
|
||||
|
||||
-- Physical chain integrity uses numeric fork identity.
|
||||
CREATE OR REPLACE FUNCTION shine_blocks_line_integrity_bi() RETURNS TRIGGER LANGUAGE plpgsql AS $$
|
||||
DECLARE prev_block RECORD;
|
||||
DECLARE prev_block RECORD; tail_block_number INTEGER;
|
||||
BEGIN
|
||||
IF NEW.line_code IS NULL AND NEW.prev_line_number IS NULL AND NEW.prev_line_hash IS NULL AND NEW.this_line_number IS NULL THEN RETURN NEW; END IF;
|
||||
IF NEW.msg_type NOT IN (0,1,3,4) THEN RAISE EXCEPTION 'LINE_ERR_UNSUPPORTED_TYPE_WITH_LINE'; END IF;
|
||||
@@ -2445,15 +2455,17 @@ BEGIN
|
||||
IF NOT FOUND THEN RAISE EXCEPTION 'LINE_ERR_NO_PREV'; END IF;
|
||||
IF prev_block.block_hash IS DISTINCT FROM NEW.prev_line_hash THEN RAISE EXCEPTION 'LINE_ERR_PREV_HASH_MISMATCH'; END IF;
|
||||
IF NEW.prev_line_number<>NEW.line_code AND prev_block.line_code IS DISTINCT FROM NEW.line_code THEN RAISE EXCEPTION 'LINE_ERR_LINE_CODE_MISMATCH'; END IF;
|
||||
IF NEW.prev_line_number=NEW.line_code THEN
|
||||
IF NEW.this_line_number<>(CASE WHEN NEW.msg_type=1 THEN 0 ELSE 1 END) THEN RAISE EXCEPTION 'LINE_ERR_FIRST_STEP_BAD_THIS'; END IF; RETURN NEW;
|
||||
SELECT block_number INTO tail_block_number FROM blocks_store
|
||||
WHERE login=NEW.login AND fork_number=NEW.fork_number AND msg_type=NEW.msg_type AND line_code=NEW.line_code
|
||||
ORDER BY block_number DESC LIMIT 1;
|
||||
IF tail_block_number IS NULL THEN
|
||||
IF NEW.prev_line_number<>NEW.line_code THEN RAISE EXCEPTION 'LINE_ERR_NOT_CURRENT_TAIL'; END IF;
|
||||
IF NEW.this_line_number<>(CASE WHEN NEW.msg_type=1 THEN 0 ELSE 1 END) THEN RAISE EXCEPTION 'LINE_ERR_FIRST_STEP_BAD_THIS'; END IF;
|
||||
RETURN NEW;
|
||||
END IF;
|
||||
IF NEW.prev_line_number<>tail_block_number THEN RAISE EXCEPTION 'LINE_ERR_NOT_CURRENT_TAIL'; END IF;
|
||||
IF prev_block.this_line_number IS NULL THEN RAISE EXCEPTION 'LINE_ERR_THIS_LINE_BAD_STEP'; END IF;
|
||||
IF NEW.msg_type=1 THEN
|
||||
IF NEW.this_line_number<>prev_block.this_line_number AND NEW.this_line_number<>prev_block.this_line_number+1 THEN RAISE EXCEPTION 'LINE_ERR_THIS_LINE_BAD_STEP'; END IF;
|
||||
ELSE
|
||||
IF NEW.this_line_number<>prev_block.this_line_number+1 THEN RAISE EXCEPTION 'LINE_ERR_THIS_LINE_BAD_STEP'; END IF;
|
||||
END IF;
|
||||
IF NEW.this_line_number<>prev_block.this_line_number+1 THEN RAISE EXCEPTION 'LINE_ERR_THIS_LINE_BAD_STEP'; END IF;
|
||||
RETURN NEW;
|
||||
END $$;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user