IDEMPIERE-422 Complete Native Sequence feature
This commit is contained in:
parent
ae5622f7de
commit
7874f573e4
|
@ -24,11 +24,16 @@ BEGIN
|
||||||
END;
|
END;
|
||||||
*
|
*
|
||||||
************************************************************************/
|
************************************************************************/
|
||||||
AS
|
As
|
||||||
|
Isnativeseqon NVARCHAR2(1);
|
||||||
|
Tablename Nvarchar2(60);
|
||||||
|
sqlcmd VARCHAR2(200);
|
||||||
BEGIN
|
BEGIN
|
||||||
|
|
||||||
|
|
||||||
IF (p_System = 'Y') THEN
|
IF (p_System = 'Y') THEN
|
||||||
SELECT CurrentNextSys
|
SELECT CurrentNextSys
|
||||||
INTO o_NextID
|
INTO o_NextID
|
||||||
FROM AD_Sequence
|
FROM AD_Sequence
|
||||||
WHERE AD_Sequence_ID=p_AD_Sequence_ID
|
WHERE AD_Sequence_ID=p_AD_Sequence_ID
|
||||||
FOR UPDATE OF CurrentNextSys;
|
FOR UPDATE OF CurrentNextSys;
|
||||||
|
@ -36,16 +41,43 @@ BEGIN
|
||||||
UPDATE AD_Sequence
|
UPDATE AD_Sequence
|
||||||
SET CurrentNextSys = CurrentNextSys + IncrementNo
|
SET CurrentNextSys = CurrentNextSys + IncrementNo
|
||||||
WHERE AD_Sequence_ID=p_AD_Sequence_ID;
|
WHERE AD_Sequence_ID=p_AD_Sequence_ID;
|
||||||
ELSE
|
ELSE
|
||||||
SELECT CurrentNext
|
|
||||||
INTO o_NextID
|
BEGIN
|
||||||
FROM AD_Sequence
|
SELECT Value
|
||||||
WHERE AD_Sequence_ID=p_AD_Sequence_ID
|
Into Isnativeseqon
|
||||||
FOR UPDATE OF CurrentNext;
|
From Ad_Sysconfig
|
||||||
--
|
Where Name ='SYSTEM_NATIVE_SEQUENCE';
|
||||||
UPDATE AD_Sequence
|
EXCEPTION
|
||||||
SET CurrentNext = CurrentNext + IncrementNo
|
WHEN NO_DATA_FOUND THEN
|
||||||
WHERE AD_Sequence_ID=p_AD_Sequence_ID;
|
Isnativeseqon:= 'N';
|
||||||
|
END;
|
||||||
|
|
||||||
|
IF Isnativeseqon = 'Y' THEN
|
||||||
|
|
||||||
|
Select Name
|
||||||
|
INTO tablename
|
||||||
|
From Ad_Sequence
|
||||||
|
Where Ad_Sequence_Id=P_Ad_Sequence_Id
|
||||||
|
And Istableid = 'Y';
|
||||||
|
--
|
||||||
|
Sqlcmd := 'SELECT '||Tablename||'_SQ.Nextval FROM DUAL';
|
||||||
|
--
|
||||||
|
Execute Immediate Sqlcmd Into O_Nextid;
|
||||||
|
--
|
||||||
|
ELSE
|
||||||
|
|
||||||
|
SELECT CurrentNext
|
||||||
|
INTO o_NextID
|
||||||
|
FROM AD_Sequence
|
||||||
|
WHERE AD_Sequence_ID=p_AD_Sequence_ID
|
||||||
|
FOR UPDATE OF CurrentNext;
|
||||||
|
--
|
||||||
|
Update Ad_Sequence
|
||||||
|
Set Currentnext = Currentnext + Incrementno
|
||||||
|
Where Ad_Sequence_Id=P_Ad_Sequence_Id;
|
||||||
|
--
|
||||||
|
END IF;
|
||||||
END IF;
|
END IF;
|
||||||
--
|
--
|
||||||
EXCEPTION
|
EXCEPTION
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
CREATE OR REPLACE FUNCTION nextid(
|
CREATE OR REPLACE FUNCTION nextid(
|
||||||
p_AD_Sequence_ID IN INTEGER,
|
p_AD_Sequence_ID IN INTEGER,
|
||||||
p_System IN VARCHAR,
|
p_System IN VARCHAR,
|
||||||
o_NextID OUT INTEGER
|
o_NextID OUT INTEGER
|
||||||
|
@ -21,7 +21,9 @@ CREATE OR REPLACE FUNCTION nextid(
|
||||||
* select * from nextid((select ad_sequence_id from ad_sequence where name = 'Test')::Integer, 'Y'::Varchar);
|
* select * from nextid((select ad_sequence_id from ad_sequence where name = 'Test')::Integer, 'Y'::Varchar);
|
||||||
*
|
*
|
||||||
************************************************************************/
|
************************************************************************/
|
||||||
|
DECLARE
|
||||||
|
Isnativeseqon VARCHAR(1);
|
||||||
|
tablename VARCHAR(60);
|
||||||
BEGIN
|
BEGIN
|
||||||
IF (p_System = 'Y') THEN
|
IF (p_System = 'Y') THEN
|
||||||
RAISE NOTICE 'system';
|
RAISE NOTICE 'system';
|
||||||
|
@ -34,14 +36,35 @@ BEGIN
|
||||||
SET CurrentNextSys = CurrentNextSys + IncrementNo
|
SET CurrentNextSys = CurrentNextSys + IncrementNo
|
||||||
WHERE AD_Sequence_ID=p_AD_Sequence_ID;
|
WHERE AD_Sequence_ID=p_AD_Sequence_ID;
|
||||||
ELSE
|
ELSE
|
||||||
SELECT CurrentNext
|
|
||||||
|
BEGIN
|
||||||
|
SELECT Value
|
||||||
|
INTO Isnativeseqon
|
||||||
|
FROM AD_SYSCONFIG
|
||||||
|
WHERE Name ='SYSTEM_NATIVE_SEQUENCE';
|
||||||
|
EXCEPTION
|
||||||
|
WHEN NO_DATA_FOUND THEN
|
||||||
|
Isnativeseqon:= 'N';
|
||||||
|
END;
|
||||||
|
|
||||||
|
IF Isnativeseqon = 'Y' THEN
|
||||||
|
SELECT Name
|
||||||
|
INTO tablename
|
||||||
|
FROM Ad_Sequence
|
||||||
|
WHERE Ad_Sequence_Id=P_Ad_Sequence_Id;
|
||||||
|
--
|
||||||
|
EXECUTE 'SELECT nextval('''||tablename||'_sq'''||')' INTO o_NextID;
|
||||||
|
--
|
||||||
|
ELSE
|
||||||
|
SELECT CurrentNext
|
||||||
INTO o_NextID
|
INTO o_NextID
|
||||||
FROM AD_Sequence
|
FROM AD_Sequence
|
||||||
WHERE AD_Sequence_ID=p_AD_Sequence_ID;
|
WHERE AD_Sequence_ID=p_AD_Sequence_ID;
|
||||||
--
|
--
|
||||||
UPDATE AD_Sequence
|
UPDATE AD_Sequence
|
||||||
SET CurrentNext = CurrentNext + IncrementNo
|
SET CurrentNext = CurrentNext + IncrementNo
|
||||||
WHERE AD_Sequence_ID=p_AD_Sequence_ID;
|
WHERE AD_Sequence_ID=p_AD_Sequence_ID;
|
||||||
|
END IF;
|
||||||
END IF;
|
END IF;
|
||||||
--
|
--
|
||||||
EXCEPTION
|
EXCEPTION
|
||||||
|
|
|
@ -0,0 +1,89 @@
|
||||||
|
CREATE OR REPLACE PROCEDURE nextID
|
||||||
|
(
|
||||||
|
p_AD_Sequence_ID IN NUMBER,
|
||||||
|
p_System IN CHAR,
|
||||||
|
o_NextID OUT NUMBER
|
||||||
|
)
|
||||||
|
/*************************************************************************
|
||||||
|
* The contents of this file are subject to the Adempiere License. You may
|
||||||
|
* obtain a copy of the License at http://www.adempiere.org/license.html
|
||||||
|
* Software is on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either
|
||||||
|
* express or implied. See the License for details. Code: Adempiere ERP+CRM
|
||||||
|
* Copyright (C) 1999-2005 Jorg Janke, ComPiere, Inc. All Rights Reserved.
|
||||||
|
*************************************************************************
|
||||||
|
* $Id: nextID.sql,v 1.1 2006/04/21 17:51:58 jjanke Exp $
|
||||||
|
***
|
||||||
|
* Title: Get Next ID - no Commit
|
||||||
|
* Description:
|
||||||
|
* Test via
|
||||||
|
DECLARE
|
||||||
|
v_NextID NUMBER;
|
||||||
|
BEGIN
|
||||||
|
nextID(2, 'Y', v_NextID);
|
||||||
|
DBMS_OUTPUT.PUT_LINE(v_NextID);
|
||||||
|
END;
|
||||||
|
*
|
||||||
|
************************************************************************/
|
||||||
|
As
|
||||||
|
Isnativeseqon NVARCHAR2(1);
|
||||||
|
Tablename Nvarchar2(60);
|
||||||
|
sqlcmd VARCHAR2(200);
|
||||||
|
BEGIN
|
||||||
|
|
||||||
|
|
||||||
|
IF (p_System = 'Y') THEN
|
||||||
|
SELECT CurrentNextSys
|
||||||
|
INTO o_NextID
|
||||||
|
FROM AD_Sequence
|
||||||
|
WHERE AD_Sequence_ID=p_AD_Sequence_ID
|
||||||
|
FOR UPDATE OF CurrentNextSys;
|
||||||
|
--
|
||||||
|
UPDATE AD_Sequence
|
||||||
|
SET CurrentNextSys = CurrentNextSys + IncrementNo
|
||||||
|
WHERE AD_Sequence_ID=p_AD_Sequence_ID;
|
||||||
|
ELSE
|
||||||
|
|
||||||
|
BEGIN
|
||||||
|
SELECT Value
|
||||||
|
Into Isnativeseqon
|
||||||
|
From Ad_Sysconfig
|
||||||
|
Where Name ='SYSTEM_NATIVE_SEQUENCE';
|
||||||
|
EXCEPTION
|
||||||
|
WHEN NO_DATA_FOUND THEN
|
||||||
|
Isnativeseqon:= 'N';
|
||||||
|
END;
|
||||||
|
|
||||||
|
IF Isnativeseqon = 'Y' THEN
|
||||||
|
|
||||||
|
Select Name
|
||||||
|
INTO tablename
|
||||||
|
From Ad_Sequence
|
||||||
|
Where Ad_Sequence_Id=P_Ad_Sequence_Id
|
||||||
|
And Istableid = 'Y';
|
||||||
|
--
|
||||||
|
Sqlcmd := 'SELECT '||Tablename||'_SQ.Nextval FROM DUAL';
|
||||||
|
--
|
||||||
|
Execute Immediate Sqlcmd Into O_Nextid;
|
||||||
|
--
|
||||||
|
ELSE
|
||||||
|
|
||||||
|
SELECT CurrentNext
|
||||||
|
INTO o_NextID
|
||||||
|
FROM AD_Sequence
|
||||||
|
WHERE AD_Sequence_ID=p_AD_Sequence_ID
|
||||||
|
FOR UPDATE OF CurrentNext;
|
||||||
|
--
|
||||||
|
Update Ad_Sequence
|
||||||
|
Set Currentnext = Currentnext + Incrementno
|
||||||
|
Where Ad_Sequence_Id=P_Ad_Sequence_Id;
|
||||||
|
--
|
||||||
|
END IF;
|
||||||
|
END IF;
|
||||||
|
--
|
||||||
|
EXCEPTION
|
||||||
|
WHEN OTHERS THEN
|
||||||
|
DBMS_OUTPUT.PUT_LINE(SQLERRM);
|
||||||
|
END nextID;
|
||||||
|
/
|
||||||
|
SELECT register_migration_script('921_IDEMPIERE-422_NativeSequence.sql') FROM dual;
|
||||||
|
|
|
@ -0,0 +1,78 @@
|
||||||
|
CREATE OR REPLACE FUNCTION nextid(
|
||||||
|
p_AD_Sequence_ID IN INTEGER,
|
||||||
|
p_System IN VARCHAR,
|
||||||
|
o_NextID OUT INTEGER
|
||||||
|
)
|
||||||
|
RETURNS INTEGER AS $body$
|
||||||
|
/*************************************************************************
|
||||||
|
* The contents of this file are subject to the Compiere License. You may
|
||||||
|
* obtain a copy of the License at http://www.compiere.org/license.html
|
||||||
|
* Software is on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either
|
||||||
|
* express or implied. See the License for details. Code: Compiere ERP+CRM
|
||||||
|
* Copyright (C) 1999-2005 Jorg Janke, ComPiere, Inc. All Rights Reserved.
|
||||||
|
*
|
||||||
|
* converted to postgreSQL by Karsten Thiemann (Schaeffer AG),
|
||||||
|
* kthiemann@adempiere.org
|
||||||
|
*************************************************************************
|
||||||
|
***
|
||||||
|
* Title: Get Next ID - no Commit
|
||||||
|
* Description: Returns the next id of the sequence.
|
||||||
|
* Test:
|
||||||
|
* select * from nextid((select ad_sequence_id from ad_sequence where name = 'Test')::Integer, 'Y'::Varchar);
|
||||||
|
*
|
||||||
|
************************************************************************/
|
||||||
|
DECLARE
|
||||||
|
Isnativeseqon VARCHAR(1);
|
||||||
|
tablename VARCHAR(60);
|
||||||
|
BEGIN
|
||||||
|
IF (p_System = 'Y') THEN
|
||||||
|
RAISE NOTICE 'system';
|
||||||
|
SELECT CurrentNextSys
|
||||||
|
INTO o_NextID
|
||||||
|
FROM AD_Sequence
|
||||||
|
WHERE AD_Sequence_ID=p_AD_Sequence_ID;
|
||||||
|
--
|
||||||
|
UPDATE AD_Sequence
|
||||||
|
SET CurrentNextSys = CurrentNextSys + IncrementNo
|
||||||
|
WHERE AD_Sequence_ID=p_AD_Sequence_ID;
|
||||||
|
ELSE
|
||||||
|
|
||||||
|
BEGIN
|
||||||
|
SELECT Value
|
||||||
|
INTO Isnativeseqon
|
||||||
|
FROM AD_SYSCONFIG
|
||||||
|
WHERE Name ='SYSTEM_NATIVE_SEQUENCE';
|
||||||
|
EXCEPTION
|
||||||
|
WHEN NO_DATA_FOUND THEN
|
||||||
|
Isnativeseqon:= 'N';
|
||||||
|
END;
|
||||||
|
|
||||||
|
IF Isnativeseqon = 'Y' THEN
|
||||||
|
SELECT Name
|
||||||
|
INTO tablename
|
||||||
|
FROM Ad_Sequence
|
||||||
|
WHERE Ad_Sequence_Id=P_Ad_Sequence_Id;
|
||||||
|
--
|
||||||
|
EXECUTE 'SELECT nextval('''||tablename||'_sq'''||')' INTO o_NextID;
|
||||||
|
--
|
||||||
|
ELSE
|
||||||
|
SELECT CurrentNext
|
||||||
|
INTO o_NextID
|
||||||
|
FROM AD_Sequence
|
||||||
|
WHERE AD_Sequence_ID=p_AD_Sequence_ID;
|
||||||
|
--
|
||||||
|
UPDATE AD_Sequence
|
||||||
|
SET CurrentNext = CurrentNext + IncrementNo
|
||||||
|
WHERE AD_Sequence_ID=p_AD_Sequence_ID;
|
||||||
|
END IF;
|
||||||
|
END IF;
|
||||||
|
--
|
||||||
|
EXCEPTION
|
||||||
|
WHEN OTHERS THEN
|
||||||
|
RAISE NOTICE '%',SQLERRM;
|
||||||
|
END;
|
||||||
|
$body$ LANGUAGE plpgsql;
|
||||||
|
|
||||||
|
SELECT register_migration_script('921_IDEMPIERE-422_NativeSequence.sql') FROM dual
|
||||||
|
;
|
||||||
|
|
|
@ -7,7 +7,9 @@ DECLARE
|
||||||
currentnextsys NUMBER (10);
|
currentnextsys NUMBER (10);
|
||||||
currentnext NUMBER (10);
|
currentnext NUMBER (10);
|
||||||
currentseqsys NUMBER (10);
|
currentseqsys NUMBER (10);
|
||||||
currentseq NUMBER (10);
|
Currentseq Number (10);
|
||||||
|
Isnativeseqon Varchar2(1);
|
||||||
|
sqlcmd VARCHAR2(200);
|
||||||
BEGIN
|
BEGIN
|
||||||
DBMS_OUTPUT.PUT_LINE ('Start');
|
DBMS_OUTPUT.PUT_LINE ('Start');
|
||||||
|
|
||||||
|
@ -38,8 +40,18 @@ BEGIN
|
||||||
DBMS_OUTPUT.PUT_LINE ('Table not found');
|
DBMS_OUTPUT.PUT_LINE ('Table not found');
|
||||||
DBMS_OUTPUT.PUT_LINE (cmdsys);
|
DBMS_OUTPUT.PUT_LINE (cmdsys);
|
||||||
GOTO next_iteration;
|
GOTO next_iteration;
|
||||||
END;
|
End;
|
||||||
|
|
||||||
|
BEGIN
|
||||||
|
SELECT Value
|
||||||
|
Into Isnativeseqon
|
||||||
|
From Ad_Sysconfig
|
||||||
|
Where Name ='SYSTEM_NATIVE_SEQUENCE';
|
||||||
|
Exception
|
||||||
|
When NO_DATA_FOUND Then
|
||||||
|
Isnativeseqon :='N';
|
||||||
|
End;
|
||||||
|
|
||||||
IF currentnextsys IS NULL
|
IF currentnextsys IS NULL
|
||||||
THEN
|
THEN
|
||||||
currentnextsys := 0;
|
currentnextsys := 0;
|
||||||
|
@ -76,16 +88,26 @@ BEGIN
|
||||||
INTO currentnext
|
INTO currentnext
|
||||||
FROM DUAL;
|
FROM DUAL;
|
||||||
|
|
||||||
cmdseq :=
|
If Isnativeseqon ='Y' Then
|
||||||
'SELECT currentnext, currentnextsys FROM AD_Sequence '
|
Cmdseq :=
|
||||||
|
'SELECT '||R.Tablename||'_SQ.Nextval as currentnext,
|
||||||
|
currentnextsys
|
||||||
|
FROM AD_Sequence '
|
||||||
|| 'WHERE Name = '''
|
|| 'WHERE Name = '''
|
||||||
|| r.tablename
|
|| r.Tablename
|
||||||
|| ''' AND istableid = ''Y''';
|
|| ''' AND istableid = ''Y''';
|
||||||
|
ELSE
|
||||||
|
cmdseq :=
|
||||||
|
'SELECT currentnext, currentnextsys FROM AD_Sequence '
|
||||||
|
|| 'WHERE Name = '''
|
||||||
|
|| r.tablename
|
||||||
|
|| ''' AND istableid = ''Y''';
|
||||||
|
END IF;
|
||||||
|
|
||||||
EXECUTE IMMEDIATE cmdseq
|
EXECUTE IMMEDIATE cmdseq
|
||||||
INTO currentseq, currentseqsys;
|
INTO currentseq, currentseqsys;
|
||||||
|
|
||||||
IF currentnextsys <> currentseqsys OR currentnext <> currentseq
|
IF currentnextsys <> currentseqsys OR (currentnext <> currentseq AND Isnativeseqon ='N')
|
||||||
THEN
|
THEN
|
||||||
DBMS_OUTPUT.PUT_LINE ( r.tablename
|
DBMS_OUTPUT.PUT_LINE ( r.tablename
|
||||||
|| ' sys='
|
|| ' sys='
|
||||||
|
@ -108,8 +130,16 @@ BEGIN
|
||||||
DBMS_OUTPUT.PUT_LINE (cmdupd);
|
DBMS_OUTPUT.PUT_LINE (cmdupd);
|
||||||
|
|
||||||
EXECUTE IMMEDIATE cmdupd;
|
EXECUTE IMMEDIATE cmdupd;
|
||||||
END IF;
|
End If;
|
||||||
|
|
||||||
|
If Currentseq < Currentnext And Isnativeseqon ='Y' Then
|
||||||
|
Sqlcmd := 'SELECT '||R.Tablename||'_SQ.Nextval FROM DUAL';
|
||||||
|
DBMS_OUTPUT.PUT_LINE (Sqlcmd);
|
||||||
|
While Not Currentseq >= (currentnext-1) Loop
|
||||||
|
Execute Immediate Sqlcmd
|
||||||
|
Into currentseq;
|
||||||
|
End Loop;
|
||||||
|
END IF;
|
||||||
<<next_iteration>>
|
<<next_iteration>>
|
||||||
NULL;
|
NULL;
|
||||||
END LOOP;
|
END LOOP;
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
CREATE OR REPLACE FUNCTION update_sequences() RETURNS void as $func$
|
CREATE OR REPLACE FUNCTION update_sequences() RETURNS void as $func$
|
||||||
-- TODO: Currently not inserting new sequences
|
-- TODO: Currently not inserting new sequences
|
||||||
DECLARE
|
DECLARE
|
||||||
cmdsys VARCHAR (1000);
|
cmdsys VARCHAR (1000);
|
||||||
|
@ -11,10 +11,11 @@ DECLARE
|
||||||
currentseq NUMERIC (10);
|
currentseq NUMERIC (10);
|
||||||
ok BOOLEAN;
|
ok BOOLEAN;
|
||||||
r RECORD;
|
r RECORD;
|
||||||
|
isnativeseqon VARCHAR (1);
|
||||||
BEGIN
|
BEGIN
|
||||||
|
|
||||||
FOR r IN (SELECT tablename
|
FOR r IN (SELECT tablename
|
||||||
FROM AD_TABLE t
|
FROM AD_TABLE t
|
||||||
WHERE EXISTS (
|
WHERE EXISTS (
|
||||||
SELECT 1
|
SELECT 1
|
||||||
FROM AD_COLUMN c
|
FROM AD_COLUMN c
|
||||||
|
@ -41,6 +42,17 @@ BEGIN
|
||||||
END;
|
END;
|
||||||
|
|
||||||
IF ok THEN
|
IF ok THEN
|
||||||
|
|
||||||
|
BEGIN
|
||||||
|
SELECT Value
|
||||||
|
INTO isnativeseqon
|
||||||
|
FROM AD_SYSCONFIG
|
||||||
|
WHERE Name ='SYSTEM_NATIVE_SEQUENCE';
|
||||||
|
EXCEPTION
|
||||||
|
WHEN NO_DATA_FOUND THEN
|
||||||
|
isnativeseqon:= 'N';
|
||||||
|
END;
|
||||||
|
|
||||||
IF currentnextsys IS NULL
|
IF currentnextsys IS NULL
|
||||||
THEN
|
THEN
|
||||||
currentnextsys := 0;
|
currentnextsys := 0;
|
||||||
|
@ -72,15 +84,25 @@ BEGIN
|
||||||
ELSE coalesce (currentnext + 1, 1000000)
|
ELSE coalesce (currentnext + 1, 1000000)
|
||||||
END ;
|
END ;
|
||||||
|
|
||||||
cmdseq :=
|
IF isnativeseqon ='Y' THEN
|
||||||
'SELECT currentnext, currentnextsys FROM AD_Sequence '
|
cmdseq :=
|
||||||
|| 'WHERE Name = '''
|
'SELECT nextval('''||trim(r.tablename)||'_sq'''||') as currentnext,
|
||||||
|| r.tablename
|
currentnextsys
|
||||||
|| ''' AND istableid = ''Y''';
|
FROM AD_Sequence '
|
||||||
|
|| 'WHERE Name = '''
|
||||||
|
|| r.tablename
|
||||||
|
|| ''' AND istableid = ''Y''';
|
||||||
|
ELSE
|
||||||
|
cmdseq :=
|
||||||
|
'SELECT currentnext, currentnextsys FROM AD_Sequence '
|
||||||
|
|| 'WHERE Name = '''
|
||||||
|
|| r.tablename
|
||||||
|
|| ''' AND istableid = ''Y''';
|
||||||
|
END IF;
|
||||||
|
|
||||||
EXECUTE cmdseq INTO currentseq, currentseqsys;
|
EXECUTE cmdseq INTO currentseq, currentseqsys;
|
||||||
|
|
||||||
IF currentnextsys <> currentseqsys OR currentnext <> currentseq
|
IF currentnextsys <> currentseqsys OR (currentnext <> currentseq AND isnativeseqon ='N')
|
||||||
THEN
|
THEN
|
||||||
cmdupd :=
|
cmdupd :=
|
||||||
'update ad_sequence set currentnextsys = '
|
'update ad_sequence set currentnextsys = '
|
||||||
|
@ -93,8 +115,14 @@ BEGIN
|
||||||
|
|
||||||
EXECUTE cmdupd;
|
EXECUTE cmdupd;
|
||||||
END IF;
|
END IF;
|
||||||
|
IF currentseq < currentnext AND isnativeseqon ='Y' THEN
|
||||||
|
--RAISE NOTICE 'currentseq % ,currentnext %',currentseq,currentnext;
|
||||||
|
WHILE NOT currentseq >= (currentnext-1) LOOP
|
||||||
|
EXECUTE 'SELECT nextval('''||trim(r.tablename)||'_sq'''||')' INTO currentseq;
|
||||||
|
--RAISE NOTICE 'currentseq % ,currentnext %',currentseq,currentnext;
|
||||||
|
END LOOP;
|
||||||
|
END IF;
|
||||||
END IF;
|
END IF;
|
||||||
|
|
||||||
END LOOP;
|
END LOOP;
|
||||||
END;
|
END;
|
||||||
$func$ LANGUAGE plpgsql;
|
$func$ LANGUAGE plpgsql;
|
||||||
|
|
|
@ -202,42 +202,28 @@ public class SequenceCheck extends SvrProcess
|
||||||
+ "ORDER BY Name";
|
+ "ORDER BY Name";
|
||||||
int counter = 0;
|
int counter = 0;
|
||||||
PreparedStatement pstmt = null;
|
PreparedStatement pstmt = null;
|
||||||
|
ResultSet rs = null;
|
||||||
String trxName = null;
|
String trxName = null;
|
||||||
if (sp != null)
|
if (sp != null)
|
||||||
trxName = sp.get_TrxName();
|
trxName = sp.get_TrxName();
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
pstmt = DB.prepareStatement(sql, trxName);
|
pstmt = DB.prepareStatement(sql, trxName);
|
||||||
ResultSet rs = pstmt.executeQuery();
|
rs = pstmt.executeQuery();
|
||||||
while (rs.next())
|
while (rs.next())
|
||||||
{
|
{
|
||||||
MSequence seq = new MSequence (ctx, rs, trxName);
|
MSequence seq = new MSequence (ctx, rs, trxName);
|
||||||
int old = seq.getCurrentNext();
|
String tableValidation= seq.validateTableIDValue();
|
||||||
int oldSys = seq.getCurrentNextSys();
|
if (tableValidation!=null){
|
||||||
if (seq.validateTableIDValue())
|
if (sp != null)
|
||||||
{
|
sp.addLog(0, null, null, tableValidation);
|
||||||
if (seq.getCurrentNext() != old)
|
|
||||||
{
|
|
||||||
StringBuilder msg = new StringBuilder(seq.getName()).append(" ID ")
|
|
||||||
.append(old).append(" -> ").append(seq.getCurrentNext());
|
|
||||||
if (sp != null)
|
|
||||||
sp.addLog(0, null, null, msg.toString());
|
|
||||||
else
|
|
||||||
s_log.fine(msg.toString());
|
|
||||||
}
|
|
||||||
if (seq.getCurrentNextSys() != oldSys)
|
|
||||||
{
|
|
||||||
StringBuilder msg = new StringBuilder(seq.getName()).append(" Sys ")
|
|
||||||
.append(oldSys).append(" -> ").append(seq.getCurrentNextSys());
|
|
||||||
if (sp != null)
|
|
||||||
sp.addLog(0, null, null, msg.toString());
|
|
||||||
else
|
|
||||||
s_log.fine(msg.toString());
|
|
||||||
}
|
|
||||||
if (seq.save())
|
|
||||||
counter++;
|
|
||||||
else
|
else
|
||||||
s_log.severe("Not updated: " + seq);
|
s_log.fine(tableValidation);
|
||||||
|
|
||||||
|
if (seq.save())
|
||||||
|
counter++;
|
||||||
|
else
|
||||||
|
s_log.severe("Not updated: " + seq);
|
||||||
}
|
}
|
||||||
// else if (CLogMgt.isLevel(6))
|
// else if (CLogMgt.isLevel(6))
|
||||||
// log.fine("checkTableID - skipped " + tableName);
|
// log.fine("checkTableID - skipped " + tableName);
|
||||||
|
@ -249,16 +235,10 @@ public class SequenceCheck extends SvrProcess
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
{
|
{
|
||||||
s_log.log(Level.SEVERE, sql, e);
|
s_log.log(Level.SEVERE, sql, e);
|
||||||
}
|
}finally
|
||||||
try
|
|
||||||
{
|
{
|
||||||
if (pstmt != null)
|
DB.close(rs, pstmt);
|
||||||
pstmt.close();
|
rs = null; pstmt = null;
|
||||||
pstmt = null;
|
|
||||||
}
|
|
||||||
catch (Exception e)
|
|
||||||
{
|
|
||||||
pstmt = null;
|
|
||||||
}
|
}
|
||||||
s_log.fine("#" + counter);
|
s_log.fine("#" + counter);
|
||||||
} // checkTableID
|
} // checkTableID
|
||||||
|
|
|
@ -62,7 +62,8 @@ public class MSequence extends X_AD_Sequence
|
||||||
private static final int QUERY_TIME_OUT = 30;
|
private static final int QUERY_TIME_OUT = 30;
|
||||||
|
|
||||||
private static final String NoYearNorMonth = "-";
|
private static final String NoYearNorMonth = "-";
|
||||||
|
|
||||||
|
@Deprecated
|
||||||
public static int getNextID (int AD_Client_ID, String TableName)
|
public static int getNextID (int AD_Client_ID, String TableName)
|
||||||
{
|
{
|
||||||
return getNextID(AD_Client_ID, TableName, null);
|
return getNextID(AD_Client_ID, TableName, null);
|
||||||
|
@ -74,8 +75,13 @@ public class MSequence extends X_AD_Sequence
|
||||||
* @param AD_Client_ID client
|
* @param AD_Client_ID client
|
||||||
* @param TableName table name
|
* @param TableName table name
|
||||||
* @param trxName deprecated.
|
* @param trxName deprecated.
|
||||||
* @return next no or (-1=not found, -2=error)
|
* @return next no or (-1=not found, -2=error)
|
||||||
*/
|
*
|
||||||
|
* WARNING!! This method doesn't take into account the native sequence setting, it always read from table AD_Sequence
|
||||||
|
* must be used JUST for the method Enable Native Sequence
|
||||||
|
*
|
||||||
|
* @deprecated
|
||||||
|
*/
|
||||||
public static int getNextID (int AD_Client_ID, String TableName, String trxName)
|
public static int getNextID (int AD_Client_ID, String TableName, String trxName)
|
||||||
{
|
{
|
||||||
if (TableName == null || TableName.length() == 0)
|
if (TableName == null || TableName.length() == 0)
|
||||||
|
@ -693,7 +699,7 @@ public class MSequence extends X_AD_Sequence
|
||||||
seq.setDescription("Table " + TableName);
|
seq.setDescription("Table " + TableName);
|
||||||
seq.setIsTableID(tableID);
|
seq.setIsTableID(tableID);
|
||||||
seq.saveEx();
|
seq.saveEx();
|
||||||
next_id = seq.getCurrentNext();
|
next_id = INIT_NO;
|
||||||
}
|
}
|
||||||
if (! CConnection.get().getDatabase().createSequence(TableName+"_SQ", 1, 0 , 99999999, next_id, trxName))
|
if (! CConnection.get().getDatabase().createSequence(TableName+"_SQ", 1, 0 , 99999999, next_id, trxName))
|
||||||
return false;
|
return false;
|
||||||
|
@ -883,10 +889,10 @@ public class MSequence extends X_AD_Sequence
|
||||||
* Validate Table Sequence Values
|
* Validate Table Sequence Values
|
||||||
* @return true if updated
|
* @return true if updated
|
||||||
*/
|
*/
|
||||||
public boolean validateTableIDValue()
|
public String validateTableIDValue()
|
||||||
{
|
{
|
||||||
if (!isTableID())
|
if (!isTableID())
|
||||||
return false;
|
return null;
|
||||||
String tableName = getName();
|
String tableName = getName();
|
||||||
int AD_Column_ID = DB.getSQLValue(null, "SELECT MAX(c.AD_Column_ID) "
|
int AD_Column_ID = DB.getSQLValue(null, "SELECT MAX(c.AD_Column_ID) "
|
||||||
+ "FROM AD_Table t"
|
+ "FROM AD_Table t"
|
||||||
|
@ -894,13 +900,14 @@ public class MSequence extends X_AD_Sequence
|
||||||
+ "WHERE t.TableName='" + tableName + "'"
|
+ "WHERE t.TableName='" + tableName + "'"
|
||||||
+ " AND c.ColumnName='" + tableName + "_ID'");
|
+ " AND c.ColumnName='" + tableName + "_ID'");
|
||||||
if (AD_Column_ID <= 0)
|
if (AD_Column_ID <= 0)
|
||||||
return false;
|
return null;
|
||||||
//
|
//
|
||||||
MSystem system = MSystem.get(getCtx());
|
MSystem system = MSystem.get(getCtx());
|
||||||
int IDRangeEnd = 0;
|
int IDRangeEnd = 0;
|
||||||
if (system.getIDRangeEnd() != null)
|
if (system.getIDRangeEnd() != null)
|
||||||
IDRangeEnd = system.getIDRangeEnd().intValue();
|
IDRangeEnd = system.getIDRangeEnd().intValue();
|
||||||
boolean change = false;
|
|
||||||
|
String changeMsg = null;
|
||||||
String info = null;
|
String info = null;
|
||||||
|
|
||||||
// Current Next
|
// Current Next
|
||||||
|
@ -910,12 +917,14 @@ public class MSequence extends X_AD_Sequence
|
||||||
int maxTableID = DB.getSQLValue(null, sql);
|
int maxTableID = DB.getSQLValue(null, sql);
|
||||||
if (maxTableID < INIT_NO)
|
if (maxTableID < INIT_NO)
|
||||||
maxTableID = INIT_NO - 1;
|
maxTableID = INIT_NO - 1;
|
||||||
maxTableID++; // Next
|
maxTableID++; // Next
|
||||||
if (getCurrentNext() < maxTableID)
|
|
||||||
|
int currentNextValue = getCurrentNext();
|
||||||
|
if (currentNextValue < maxTableID)
|
||||||
{
|
{
|
||||||
setCurrentNext(maxTableID);
|
setCurrentNext(maxTableID);
|
||||||
info = "CurrentNext=" + maxTableID;
|
info = "CurrentNext=" + maxTableID;
|
||||||
change = true;
|
changeMsg = getName() + " ID " + currentNextValue + " -> " + maxTableID;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get Max System_ID used in Table
|
// Get Max System_ID used in Table
|
||||||
|
@ -924,22 +933,44 @@ public class MSequence extends X_AD_Sequence
|
||||||
int maxTableSysID = DB.getSQLValue(null, sql);
|
int maxTableSysID = DB.getSQLValue(null, sql);
|
||||||
if (maxTableSysID <= 0)
|
if (maxTableSysID <= 0)
|
||||||
maxTableSysID = INIT_SYS_NO - 1;
|
maxTableSysID = INIT_SYS_NO - 1;
|
||||||
maxTableSysID++; // Next
|
int currentNextSysValue = getCurrentNextSys();
|
||||||
if (getCurrentNextSys() < maxTableSysID)
|
if (currentNextSysValue < maxTableSysID){
|
||||||
{
|
|
||||||
setCurrentNextSys(maxTableSysID);
|
setCurrentNextSys(maxTableSysID);
|
||||||
if (info == null)
|
if (info == null)
|
||||||
info = "CurrentNextSys=" + maxTableSysID;
|
info = "CurrentNextSys=" + maxTableSysID;
|
||||||
else
|
else
|
||||||
info += " - CurrentNextSys=" + maxTableSysID;
|
info += " - CurrentNextSys=" + maxTableSysID;
|
||||||
change = true;
|
|
||||||
|
if (changeMsg == null)
|
||||||
|
changeMsg = getName() + " Sys " + currentNextSysValue + " -> " + maxTableSysID;
|
||||||
|
else
|
||||||
|
changeMsg += " - " +getName() + " Sys " + currentNextSysValue + " -> " + maxTableSysID;
|
||||||
}
|
}
|
||||||
if (info != null)
|
if (info != null)
|
||||||
log.fine(getName() + " - " + info);
|
log.fine(getName() + " - " + info);
|
||||||
return change;
|
|
||||||
|
return changeMsg;
|
||||||
} // validate
|
} // validate
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getCurrentNext() {
|
||||||
|
if (MSysConfig.getBooleanValue(MSysConfig.SYSTEM_NATIVE_SEQUENCE,false) && isTableID()){
|
||||||
|
return DB.getNextID (getAD_Client_ID(),getName(),get_TrxName());
|
||||||
|
}else {
|
||||||
|
return super.getCurrentNext();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setCurrentNext(int CurrentNext) {
|
||||||
|
if (MSysConfig.getBooleanValue(MSysConfig.SYSTEM_NATIVE_SEQUENCE,false) && isTableID()){
|
||||||
|
while (DB.getNextID(getAD_Client_ID(),getName(),get_TrxName()) < (CurrentNext-1)) {
|
||||||
|
// do nothing - the while is incrementing the sequence
|
||||||
|
}
|
||||||
|
}else {
|
||||||
|
super.setCurrentNext(CurrentNext);
|
||||||
|
}
|
||||||
|
}
|
||||||
/**************************************************************************
|
/**************************************************************************
|
||||||
* Test
|
* Test
|
||||||
* @param args ignored
|
* @param args ignored
|
||||||
|
|
Loading…
Reference in New Issue