Page 1 of 1

Automatic sequence

Posted: 21.11.2015, 08:31
by Edijus
Hello.
We have a problem: PostgreSQL + Zeos + Delphi XE2... Consider this:
DB part:

Code: Select all

CREATE DATABASE test
  WITH OWNER = postgres
       ENCODING = 'UTF8'
       TABLESPACE = pg_default
       LC_COLLATE = 'Lithuanian_Lithuania.1257'
       LC_CTYPE = 'Lithuanian_Lithuania.1257'
       CONNECTION LIMIT = -1;

CREATE TABLE sutartis
(
  st_id serial NOT NULL,
  st_vardas character varying(255) NOT NULL,
  CONSTRAINT sutartis_pkey PRIMARY KEY (st_id)
)
WITH (
  OIDS=FALSE
);
ALTER TABLE sutartis
  OWNER TO postgres;

CREATE OR REPLACE FUNCTION fnk_sutartis_edt(
    prm_id integer,
    prm_vardas character varying)
  RETURNS integer AS
$BODY$
BEGIN
  INSERT INTO sutartis(
    st_vardas)
  VALUES(
    prm_vardas
  );

  RETURN 0;
END;
$BODY$
  LANGUAGE plpgsql VOLATILE SECURITY DEFINER
  COST 100;
ALTER FUNCTION fnk_sutartis_edt(integer, character varying)
  OWNER TO postgres;
Delphi part:
TZQuery named qDuomenys:

Code: Select all

SELECT
  *
FROM
  sutartis;
Assign UpdateObject property to TZUpdateSQL (named uqDuomenys) with InsertSQL:

Code: Select all

SELECT
  *
FROM
  fnk_sutartis_edt(null, :prm_vardas);

Code: Select all

qDuomenys.Close;
qDuomenys.Open;
qDuomenys.Insert;
uqDuomenys.Params.ParamByName('prm_vardas').Value := DateTimeToStr(Now);
qDuomenys.Post;
Resulting data in DB will be: last sequence + 2 e.g. 1, 3, 5, 7, 9, etc.

I think it is a bug that ZeosLib doesn't look at AutoGenerateValue property setting.

Re: Automatic sequence

Posted: 24.11.2015, 18:02
by marsupilami
Hello Edijus,

this problem is described in this thread to some extent: http://zeoslib.sourceforge.net/viewtopi ... =20&t=3332
For now I can only suggest to use TZSequence and a bigint column or something similar. If you want to look at the code for yourself and find a proper solution, I will be happy to assist you in any way I can.
With best regards,

Jan