
DO $do$
BEGIN
    IF EXISTS (
        SELECT
        FROM
            pg_catalog.pg_roles
        WHERE
            rolname = 'tm_admin') 
    THEN
        RAISE NOTICE 'Role "tm_admin" already exists. Skipping.';
    ELSE
        CREATE ROLE tm_admin;
    END IF;
	
    IF EXISTS (
        SELECT
        FROM
            pg_catalog.pg_roles
        WHERE
            rolname = 'tm_user') 
    THEN
        RAISE NOTICE 'Role "tm_user" already exists. Skipping.';
    ELSE
        CREATE ROLE tm_user;
    END IF;
END
$do$;

CREATE SCHEMA IF NOT EXISTS public;
ALTER SCHEMA public OWNER TO tm_admin;
ALTER SCHEMA public OWNER TO tm_user;

-- Создание кодировок
create collation if not exists Cyrillic_General_CI_AS (
    provider = icu,
    locale = 'ru-RU-u-ks-level2-kr-latn-cyrl',
    deterministic = FALSE
);

create collation if not exists Cyrillic_General_CS_AS (
    provider = icu,
    locale = 'ru-RU-u-kr-latn-cyrl',
    deterministic = TRUE
);

DROP TABLE IF EXISTS public."GroupDesc" CASCADE;

CREATE TABLE IF NOT EXISTS public."GroupDesc"
(
	"GroupID" integer NOT NULL GENERATED ALWAYS AS IDENTITY ( CYCLE INCREMENT 1 START 1 MINVALUE 1 MAXVALUE 2147483647 CACHE 1 ),
    "GroupCode" character varying(64) COLLATE Cyrillic_General_CI_AS NOT NULL,
    "GroupName" character varying(255) COLLATE Cyrillic_General_CI_AS,
    disabled character(1) NOT NULL,
    "Mode" character varying(10) COLLATE Cyrillic_General_CS_AS,
    "DefWeight" double precision,
    "SplitOrders" integer,
    CONSTRAINT "GroupDesc_PK" PRIMARY KEY ("GroupID")
);

DROP TABLE IF EXISTS public."Clients";

CREATE TABLE IF NOT EXISTS public."Clients"
(
    "ClientCode" character varying(20) COLLATE Cyrillic_General_CS_AS NOT NULL,
    "GroupID" integer NOT NULL,
    "Num" integer,
    "Weight" double precision,
    "Comment" character varying(50) COLLATE Cyrillic_General_CI_AS,
    CONSTRAINT "Clients_FK00" FOREIGN KEY ("GroupID")
        REFERENCES public."GroupDesc" ("GroupID") MATCH SIMPLE
        ON UPDATE NO ACTION
        ON DELETE NO ACTION
);

DROP INDEX IF EXISTS public.clientsfullindex;

CREATE UNIQUE INDEX IF NOT EXISTS clientsfullindex
    ON public."Clients" USING btree
    ("GroupID" ASC NULLS LAST, "ClientCode" COLLATE Cyrillic_General_CS_AS ASC NULLS LAST);

DROP INDEX IF EXISTS public.groupdescclients;

CREATE INDEX IF NOT EXISTS groupdescclients
    ON public."Clients" USING btree
    ("GroupID" ASC NULLS LAST);

DROP TABLE IF EXISTS public."ExportOrder";

CREATE TABLE IF NOT EXISTS public."ExportOrder"
(
    "ID" integer NOT NULL,
    "OrderID" integer NOT NULL,
    "GroupOrderID" integer NOT NULL,
    "OrderType" integer NOT NULL,
    "TradeDate" integer NOT NULL,
    "SendDate" integer NOT NULL,
    "SendTime" integer NOT NULL,
    "OrderTime" integer NOT NULL,
    "Status" integer,
    "UserGroupID" integer,
    "UserGroupCode" character varying(64) COLLATE Cyrillic_General_CI_AS,
    "PoolID" integer,
    "BrokerPoolID" character varying(32) COLLATE Cyrillic_General_CI_AS,
    "ClientCode" character varying(12) COLLATE Cyrillic_General_CI_AS,
    "TrdAcc" character varying(12) COLLATE Cyrillic_General_CI_AS,
    "Price" double precision,
    "Qty" double precision,
    "Balance" double precision,
    "Buy" character(1) COLLATE Cyrillic_General_CI_AS,
    "ML" character(1) COLLATE Cyrillic_General_CI_AS,
    "ClassCode" character varying(12) COLLATE Cyrillic_General_CI_AS,
    "SecCode" character varying(12) COLLATE Cyrillic_General_CI_AS,
    "VisibleQty" double precision,
    "RepoValue" double precision,
    "Discount" double precision,
    "DiscountUpper" double precision,
    "DiscountLower" double precision,
    "RepoRate" double precision,
    "RepoTerm" integer,
    CONSTRAINT "PK_ExportOrder" PRIMARY KEY ("ID")
);

DROP INDEX IF EXISTS public."IX_ExportOrder_Date";

CREATE INDEX IF NOT EXISTS "IX_ExportOrder_Date"
    ON public."ExportOrder" USING btree
    ("TradeDate" ASC NULLS LAST);

DROP INDEX IF EXISTS public."IX_ExportOrder_GrOrderID";

CREATE INDEX IF NOT EXISTS "IX_ExportOrder_GrOrderID"
    ON public."ExportOrder" USING btree
    ("GroupOrderID" ASC NULLS LAST);

DROP INDEX IF EXISTS public."IX_ExportOrder_OrderID";

CREATE INDEX IF NOT EXISTS "IX_ExportOrder_OrderID"
    ON public."ExportOrder" USING btree
    ("OrderID" ASC NULLS LAST);

DROP INDEX IF EXISTS public."IX_ExportOrder_Time";

CREATE INDEX IF NOT EXISTS "IX_ExportOrder_Time"
    ON public."ExportOrder" USING btree
    ("SendTime" ASC NULLS LAST);

DROP INDEX IF EXISTS public."IX_ExportOrder_UserGrID";

CREATE INDEX IF NOT EXISTS "IX_ExportOrder_UserGrID"
    ON public."ExportOrder" USING btree
    ("UserGroupID" ASC NULLS LAST);

DROP INDEX IF EXISTS public."GroupCode";

CREATE INDEX IF NOT EXISTS "GroupCode"
    ON public."GroupDesc" USING btree
    ("GroupID" ASC NULLS LAST)
    INCLUDE("GroupID");

DROP TABLE IF EXISTS public."GroupNegOrders";

CREATE TABLE IF NOT EXISTS public."GroupNegOrders"
(
    "ID" integer NOT NULL,
    "GroupID" integer,
    "TradeDate" integer,
    "TradeTime" integer,
    "Buy" character(1) COLLATE Cyrillic_General_CI_AS,
    "SecCode" character varying(12) COLLATE Cyrillic_General_CI_AS,
    "ClassCode" character varying(12) COLLATE Cyrillic_General_CI_AS,
    "CPFirmID" character varying(12) COLLATE Cyrillic_General_CI_AS,
    "SettleCode" character varying(12) COLLATE Cyrillic_General_CI_AS,
    "MatchRef" character varying(10) COLLATE Cyrillic_General_CI_AS,
    "Price" double precision,
    "Vozm" double precision,
    "Qty" double precision,
    "RealQty" double precision,
    "KilledQty" double precision,
    "Status" character(1) COLLATE Cyrillic_General_CI_AS,
    "Result" character(1) COLLATE Cyrillic_General_CI_AS,
    "OrdersCount" integer,
    "RepoValue" double precision,
    "RealRepoValue" double precision,
    "Discount" double precision,
    "DiscountUpper" double precision,
    "DiscountLower" double precision,
    "RepoRate" double precision,
    "RepoTerm" integer,
    "OrderType" integer,
    "Flags" integer,
    "TMUserID" integer,
    "TMSessionID" character varying(40) COLLATE Cyrillic_General_CI_AS,
    "TMCounter" integer,
    CONSTRAINT "GroupNegOrders_PK" PRIMARY KEY ("ID")
);

DROP TABLE IF EXISTS public."GroupOrders" CASCADE;

CREATE TABLE IF NOT EXISTS public."GroupOrders"
(
    "ID" integer NOT NULL,
    "GroupID" integer NOT NULL,
    "TradeDate" integer NOT NULL,
    "TradeTime" integer NOT NULL,
    "Buy" character(1) COLLATE Cyrillic_General_CI_AS NOT NULL,
    "ML" character(1) COLLATE Cyrillic_General_CI_AS NOT NULL,
    "SecCode" character varying(12) COLLATE Cyrillic_General_CI_AS NOT NULL,
    "ClassCode" character varying(12) COLLATE Cyrillic_General_CI_AS NOT NULL,
    "Price" double precision NOT NULL,
    "Qty" double precision NOT NULL,
    "RealQty" double precision NOT NULL,
    "BalanceQty" double precision NOT NULL,
    "KilledQty" double precision NOT NULL,
    "Status" character(1) COLLATE Cyrillic_General_CI_AS NOT NULL,
    "Result" character(1) COLLATE Cyrillic_General_CI_AS NOT NULL,
    "OrdersCount" integer NOT NULL,
    "Flags" character varying(10) COLLATE Cyrillic_General_CI_AS,
    "PoolID" integer,
    "Value" double precision,
    "TMUserID" integer,
    "TMSessionID" character varying(40) COLLATE Cyrillic_General_CI_AS,
    "TMCounter" integer,
    "VisibleQty" double precision,
    CONSTRAINT "GroupOrders_PK" PRIMARY KEY ("ID"),
    CONSTRAINT "GroupOrders_FK00" FOREIGN KEY ("GroupID")
        REFERENCES public."GroupDesc" ("GroupID") MATCH SIMPLE
        ON UPDATE NO ACTION
        ON DELETE NO ACTION
);

DROP INDEX IF EXISTS public."GroupDescGroupOrders";

CREATE INDEX IF NOT EXISTS "GroupDescGroupOrders"
    ON public."GroupOrders" USING btree
    ("GroupID" ASC NULLS LAST)
    INCLUDE("GroupID");

DROP TABLE IF EXISTS public."GroupStopOrders";

CREATE TABLE IF NOT EXISTS public."GroupStopOrders"
(
    "ID" integer NOT NULL,
    "GroupID" integer NOT NULL,
    "StopStyle" integer,
    "TradeDate" integer NOT NULL,
    "TradeTime" integer NOT NULL,
    "Buy" character(1) COLLATE Cyrillic_General_CI_AS NOT NULL,
    "ML" character(1) COLLATE Cyrillic_General_CI_AS NOT NULL,
    "SecCode" character varying(12) COLLATE Cyrillic_General_CI_AS NOT NULL,
    "ClassCode" character varying(12) COLLATE Cyrillic_General_CI_AS NOT NULL,
    "ASecCode" character varying(12) COLLATE Cyrillic_General_CI_AS,
    "AClassCode" character varying(12) COLLATE Cyrillic_General_CI_AS,
    "StopCond" integer,
    "TransFlags" integer,
    "StopPrice" double precision NOT NULL,
    "Price" double precision NOT NULL,
    "LinkedPrice" double precision,
    "Offset" double precision,
    "Spread" double precision,
    "Qty" double precision NOT NULL,
    "RealQty" double precision NOT NULL,
    "BalanceQty" double precision NOT NULL,
    "KilledQty" double precision NOT NULL,
    "BaseOrderNum" double precision NOT NULL,
    "Status" character(1) COLLATE Cyrillic_General_CI_AS NOT NULL,
    "Result" character(1) COLLATE Cyrillic_General_CI_AS NOT NULL,
    "OrdersCount" integer NOT NULL,
    "LinkedGrOrder" integer,
    "CoOrderNum" double precision,
    "Expire" integer,
    "StopPrice2" double precision,
    "ActiveFromTime" integer,
    "ActiveToTime" integer,
    CONSTRAINT "GroupStopOrders_PK" PRIMARY KEY ("ID")
);

DROP TABLE IF EXISTS public."ImportLog";

CREATE TABLE IF NOT EXISTS public."ImportLog"
(
    "ID" integer NOT NULL,
    "ImportType" character varying(32) COLLATE Cyrillic_General_CI_AS,
    "TMUserID" integer,
    "TMSessionID" character varying(40) COLLATE Cyrillic_General_CI_AS,
    "TMCounter" integer,
    CONSTRAINT "PK_ImportLog" PRIMARY KEY ("ID")
);

DROP TABLE IF EXISTS public."LoadedPositions";

CREATE TABLE IF NOT EXISTS public."LoadedPositions"
(
    "PosID" integer NOT NULL,
    "PosType" integer NOT NULL,
    "FirmID" character varying(12) COLLATE Cyrillic_General_CI_AS,
    "ActiveCode" character varying(12) COLLATE Cyrillic_General_CI_AS,
    "ClientCode" character varying(12) COLLATE Cyrillic_General_CI_AS,
    "LimitKind" integer,
    "OpenBalance" numeric(19,6),
    CONSTRAINT "PK_LoadedPositions" PRIMARY KEY ("PosID")
);

DROP TABLE IF EXISTS public."LoadedSecurities" CASCADE;

CREATE TABLE IF NOT EXISTS public."LoadedSecurities"
(
    "SecID" integer NOT NULL,
    "SecCode" character varying(12) COLLATE Cyrillic_General_CI_AS,
    "SecName" character varying(16) COLLATE Cyrillic_General_CI_AS,
    "SecType" character varying(32) COLLATE Cyrillic_General_CI_AS,
    "FaceValue" numeric(19,4),
    "AccInt" numeric(19,4),
    "Price" numeric(19,6),
    "SecStyle" character varying(16) COLLATE Cyrillic_General_CI_AS,
    CONSTRAINT "PK_LoadedSecurities" PRIMARY KEY ("SecID")
);

DROP TABLE IF EXISTS public."LoadedSecuritiesClientPrices";

CREATE TABLE IF NOT EXISTS public."LoadedSecuritiesClientPrices"
(
    "ClientID" integer NOT NULL,
    "SecID" integer NOT NULL,
    "Code" character varying(12) COLLATE Cyrillic_General_CI_AS,
    "CodeType" integer,
    "Price" numeric(19,6),
    CONSTRAINT "PK_LoadedSecuritiesClientPrices" PRIMARY KEY ("ClientID"),
    CONSTRAINT "FK_LoadedSecuritiesClientPrices_LoadedSecurities" FOREIGN KEY ("SecID")
        REFERENCES public."LoadedSecurities" ("SecID") MATCH SIMPLE
        ON UPDATE NO ACTION
        ON DELETE NO ACTION
);

DROP INDEX IF EXISTS public."FK_LoadedSecuritiesClientPrices_LoadedSecurities";

CREATE INDEX IF NOT EXISTS "FK_LoadedSecuritiesClientPrices_LoadedSecurities"
    ON public."LoadedSecuritiesClientPrices" USING btree
    ("SecID" ASC NULLS LAST);

DROP TABLE IF EXISTS public."NegOrders";

CREATE TABLE IF NOT EXISTS public."NegOrders"
(
    "OrderId" integer NOT NULL,
    "GroupOrderID" integer,
    "TradeDate" integer,
    "TradeTime" integer,
    "Buy" character(1),
    "ClassCode" character varying(12) COLLATE Cyrillic_General_CI_AS,
    "SecCode" character varying(12) COLLATE Cyrillic_General_CI_AS,
    "ClientCode" character varying(12) COLLATE Cyrillic_General_CI_AS,
    "TrdAcc" character varying(12) COLLATE Cyrillic_General_CI_AS,
    "CPFirmId" character varying(12) COLLATE Cyrillic_General_CI_AS,
    "SettleCode" character varying(12) COLLATE Cyrillic_General_CI_AS,
    "MatchRef" character varying(10) COLLATE Cyrillic_General_CI_AS,
    "Price" double precision,
    "Vozm" double precision,
    "Qty" double precision,
    "OrderNum" double precision,
    "Status" character(1) COLLATE Cyrillic_General_CI_AS,
    "Result" character(1) COLLATE Cyrillic_General_CI_AS,
    "RepoValue" double precision,
    "Discount" double precision,
    "DiscountUpper" double precision,
    "DiscountLower" double precision,
    "RepoRate" double precision,
    "RepoTerm" double precision,
    "Flags" integer,
    "TMUserID" integer,
    "TMSessionID" character varying(40) COLLATE Cyrillic_General_CI_AS,
    "TMCounter" integer,
    CONSTRAINT "NegOrders_PK" PRIMARY KEY ("OrderId")
);

DROP TABLE IF EXISTS public."Orders";

CREATE TABLE IF NOT EXISTS public."Orders"
(
    "OrderID" integer NOT NULL,
    "GroupOrderID" integer NOT NULL,
    "Buy" character(1) NOT NULL,
    "ML" character(1) COLLATE Cyrillic_General_CI_AS NOT NULL,
    "TradeDate" integer NOT NULL,
    "TradeTime" integer NOT NULL,
    "SecCode" character varying(12) COLLATE Cyrillic_General_CI_AS NOT NULL,
    "ClassCode" character varying(12) COLLATE Cyrillic_General_CI_AS NOT NULL,
    "ClientCode" character varying(12) COLLATE Cyrillic_General_CS_AS NOT NULL,
    "TrdAcc" character varying(12) COLLATE Cyrillic_General_CI_AS,
    "Price" double precision NOT NULL,
    "Qty" double precision NOT NULL,
    "BalanceQty" double precision NOT NULL,
    "OrderNum" double precision NOT NULL,
    "Status" character(1) COLLATE Cyrillic_General_CI_AS,
    "Result" character(1) COLLATE Cyrillic_General_CI_AS NOT NULL,
    "OrderInPoolID" integer,
    "Value" double precision,
    "VisibleQty" double precision,
    "TMUserID" double precision,
    "TMSessionID" character varying(40) COLLATE Cyrillic_General_CI_AS,
    "TMCounter" integer,
    CONSTRAINT "Orders_PK" PRIMARY KEY ("OrderID"),
    CONSTRAINT "Orders_FK00" FOREIGN KEY ("GroupOrderID")
        REFERENCES public."GroupOrders" ("ID") MATCH SIMPLE
        ON UPDATE NO ACTION
        ON DELETE NO ACTION
);

DROP INDEX IF EXISTS public."Orders_FK00";

CREATE INDEX IF NOT EXISTS "Orders_FK00"
    ON public."Orders" USING btree
    ("GroupOrderID" ASC NULLS LAST);

DROP TABLE IF EXISTS public."PoolOrders" CASCADE;

CREATE TABLE IF NOT EXISTS public."PoolOrders"
(
    "PoolID" integer NOT NULL GENERATED ALWAYS AS IDENTITY ( INCREMENT 1 START 1 MINVALUE 1 MAXVALUE 2147483647 CACHE 1 ),
    "BrokerPoolID" character varying(32) COLLATE Cyrillic_General_CS_AS NOT NULL,
    "PoolName" character varying(128) COLLATE Cyrillic_General_CI_AS,
    "LoadDate" integer NOT NULL,
    "LoadTime" integer NOT NULL,
    "ExpireDate" integer NOT NULL,
    "Source" character varying(250) COLLATE Cyrillic_General_CI_AS,
    "Status" character varying(50) COLLATE Cyrillic_General_CI_AS,
    "KillTime" integer,
    "TotalQty" double precision,
    "SendQty" double precision,
    "ExecQty" double precision,
    "RejectQty" double precision,
    "KilledQty" double precision,
    "ActiveQty" double precision,
    CONSTRAINT "PK_PoolOrders" PRIMARY KEY ("PoolID")
);

DROP TABLE IF EXISTS public."OrdersInPool";

CREATE TABLE IF NOT EXISTS public."OrdersInPool"
(
    "OrderID" integer NOT NULL  GENERATED ALWAYS AS IDENTITY ( INCREMENT 1 START 1 MINVALUE 1 MAXVALUE 2147483647 CACHE 1 ),
    "PoolID" integer NOT NULL,
    "OrderType" character(1) COLLATE Cyrillic_General_CI_AS NOT NULL,
    "ClientCode" character varying(12) COLLATE Cyrillic_General_CS_AS NOT NULL,
    "ClassCode" character varying(12) COLLATE Cyrillic_General_CI_AS NOT NULL,
    "SecCode" character varying(12) COLLATE Cyrillic_General_CI_AS NOT NULL,
    "Price" double precision,
    "Qty" double precision NOT NULL,
    "Buy" character(1) NOT NULL,
    "ML" character(1) COLLATE Cyrillic_General_CI_AS NOT NULL,
    "SendQty" double precision NOT NULL,
    "ExecQty" double precision NOT NULL,
    "RejectQty" double precision NOT NULL,
    "KillQty" double precision NOT NULL,
    "ActiveQty" double precision,
    "Comment" character varying(12) COLLATE Cyrillic_General_CI_AS,
    CONSTRAINT "PK_OrdersInPool" PRIMARY KEY ("OrderID"),
    CONSTRAINT "FK_OrdersInPool_PoolOrders" FOREIGN KEY ("PoolID")
        REFERENCES public."PoolOrders" ("PoolID") MATCH SIMPLE
        ON UPDATE NO ACTION
        ON DELETE NO ACTION
);

DROP INDEX IF EXISTS public."FK_OrdersInPool_PoolOrders";

CREATE INDEX IF NOT EXISTS "FK_OrdersInPool_PoolOrders"
    ON public."OrdersInPool" USING btree
    ("PoolID" ASC NULLS LAST);

DROP TABLE IF EXISTS public."RatingAgencies";

CREATE TABLE IF NOT EXISTS public."RatingAgencies"
(
    "AgencyID" integer NOT NULL GENERATED ALWAYS AS IDENTITY ( INCREMENT 1 START 1 MINVALUE 1 MAXVALUE 2147483647 CACHE 1 ),
    "AgencyCode" character varying(20) COLLATE Cyrillic_General_CI_AS NOT NULL,
    "AgencyName" character varying(128) COLLATE Cyrillic_General_CI_AS,
    CONSTRAINT "PK_RatingAgencies" PRIMARY KEY ("AgencyID")
);

DROP TABLE IF EXISTS public."RatingScales";

CREATE TABLE IF NOT EXISTS public."RatingScales"
(
    "RatingID" integer NOT NULL  GENERATED ALWAYS AS IDENTITY ( INCREMENT 1 START 1 MINVALUE 1 MAXVALUE 2147483647 CACHE 1 ),
    "AgencyID" integer NOT NULL,
    "National" integer NOT NULL,
    "Rating" character varying(20) COLLATE Cyrillic_General_CI_AS NOT NULL,
    "Position" integer NOT NULL,
    CONSTRAINT "PK_RatingScales" PRIMARY KEY ("RatingID")
);

DROP TABLE IF EXISTS public."Restrictions";

CREATE TABLE IF NOT EXISTS public."Restrictions"
(
    "ID" integer NOT NULL,
    "RestType" character(1) COLLATE Cyrillic_General_CI_AS,
    "Client" character varying(20) COLLATE Cyrillic_General_CS_AS,
    "Mode" character varying(10) COLLATE Cyrillic_General_CI_AS,
    "Limit" double precision,
    "SecGroupID" integer,
    "CoSecGroupID" integer,
    disabled character(1) NOT NULL,
    "Value" numeric(19,4),
    "NoCheck" integer,
    "TMUserID" integer,
    "TMSessionID" character varying(40) COLLATE Cyrillic_General_CI_AS,
    "TMCounter" integer,
    "LimitKind" integer,
    "RestrCode" character varying(12),
    "RestrName" character varying(50),
    CONSTRAINT "PK_Restrictions" PRIMARY KEY ("ID")
);

DROP TABLE IF EXISTS public."SecGrMemb";

CREATE TABLE IF NOT EXISTS public."SecGrMemb"
(
    "GroupID" integer,
    "MembType" character(1) COLLATE Cyrillic_General_CI_AS,
    "MembCode" character varying(20) COLLATE Cyrillic_General_CI_AS,
    "MembClass" character varying(20) COLLATE Cyrillic_General_CI_AS,
    "MembID" integer
);

DROP TABLE IF EXISTS public."SecGroups";

CREATE TABLE IF NOT EXISTS public."SecGroups"
(
    "ID" integer NOT NULL,
    "SecGCode" character varying(64) COLLATE Cyrillic_General_CI_AS,
    "SecGName" character varying(255) COLLATE Cyrillic_General_CI_AS,
    disabled character(1) NOT NULL,
    "Filter" character varying(10) COLLATE Cyrillic_General_CI_AS,
    mode character varying(10) COLLATE Cyrillic_General_CI_AS,
    "RAgencies" integer,
    "ListLevel" integer,
    CONSTRAINT "PK_SecGroups" PRIMARY KEY ("ID")
);

DROP TABLE IF EXISTS public."SecMinQt";

CREATE TABLE IF NOT EXISTS public."SecMinQt"
(
    "ClassCode" character varying(12) COLLATE Cyrillic_General_CI_AS NOT NULL,
    "SecCode" character varying(12) COLLATE Cyrillic_General_CI_AS NOT NULL,
    "MinQty" integer NOT NULL
);

DROP TABLE IF EXISTS public."StopOrders";

CREATE TABLE IF NOT EXISTS public."StopOrders"
(
    "OrderID" integer NOT NULL,
    "GroupOrderID" integer NOT NULL,
    "StopStyle" integer,
    "Buy" character(1) NOT NULL,
    "ML" character(1) COLLATE Cyrillic_General_CI_AS NOT NULL,
    "TradeDate" integer NOT NULL,
    "TradeTime" integer NOT NULL,
    "SecCode" character varying(12) COLLATE Cyrillic_General_CI_AS NOT NULL,
    "ClassCode" character varying(12) COLLATE Cyrillic_General_CI_AS NOT NULL,
    "ASecCode" character varying(12) COLLATE Cyrillic_General_CI_AS,
    "AClassCode" character varying(12) COLLATE Cyrillic_General_CI_AS,
    "StopCond" integer,
    "TransFlags" integer,
    "ClientCode" character varying(12) COLLATE Cyrillic_General_CS_AS NOT NULL,
    "StopPrice" double precision NOT NULL,
    "Price" double precision NOT NULL,
    "LinkedPrice" double precision,
    "Offset" double precision,
    "Spread" double precision,
    "Qty" double precision,
    "Balance" double precision,
    "StopOrderNum" double precision NOT NULL,
    "LinkedOrderNum" double precision NOT NULL,
    "BaseOrderNum" double precision,
    "CoOrderNum" double precision,
    "Status" character(1) COLLATE Cyrillic_General_CI_AS,
    "Result" character(1) COLLATE Cyrillic_General_CI_AS NOT NULL,
    "Expire" integer,
    "StopPrice2" double precision,
    "ActiveFromTime" integer,
    "ActiveToTime" integer,
    "TrdAcc" character varying(12) COLLATE Cyrillic_General_CI_AS,
    CONSTRAINT "StopOrders_PK" PRIMARY KEY ("OrderID")
);

DROP TABLE IF EXISTS public."SystemCounters";

CREATE TABLE IF NOT EXISTS public."SystemCounters"
(
    "TableName" character varying(32) COLLATE Cyrillic_General_CI_AS NOT NULL,
    "Counter" integer NOT NULL,
    CONSTRAINT "PK_SystemCounters" PRIMARY KEY ("TableName")
);

DROP TABLE IF EXISTS public."SystemData";

CREATE TABLE IF NOT EXISTS public."SystemData"
(
    "KeyName" character varying(20) COLLATE Cyrillic_General_CI_AS,
    "KeyValue" character varying(50) COLLATE Cyrillic_General_CI_AS
);

DROP PROCEDURE IF EXISTS public."CreateNewRestriction"(integer, character varying, integer, character, double precision, integer, integer, integer, integer, integer, integer, integer, integer, character varying, character varying);

CREATE OR REPLACE PROCEDURE public."CreateNewRestriction"(
	IN onclient integer,
	IN clientcode character varying,
	IN restrtype integer,
	IN sign character,
	IN limit_ double precision,
	IN secgroupid integer,
	IN cosecgroupid integer,
	OUT id integer,
	IN modesummary integer DEFAULT 0,
	IN notuseaccint integer DEFAULT 0,
	IN modeemitent integer DEFAULT 0,
	IN modeanysecurity integer DEFAULT 0,
	IN modeanyemitent integer DEFAULT 0,
	IN limitkind integer DEFAULT 0,
	IN restrcode character varying DEFAULT NULL::character varying,
	IN restrname character varying DEFAULT NULL::character varying)
LANGUAGE 'plpgsql'
AS $BODY$
declare 
	RType char(1);
  	RLimit float;
  	RValue float;
  	RSign varchar(10);
  	TMSessionID varchar(40);
  	TMCounter int;
begin
  	--set NOCOUNT ON;

  	ID=-1;

  	if (RestrType<0) or (RestrType>13) then
    	return;
	end if;

  	if (RestrType in (0,1,2,3,7,8)) and (SecGroupID<2000000000) then
    	if not exists (select from public."SecGroups" where "ID"=SecGroupID) then
      		return;
		end if;
  	end if;

  	if (RestrType in (1,5,12)) and (CoSecGroupID<2000000000) then
    	if not exists (select from public."SecGroups" where "ID"=CoSecGroupID) then
      		return;
		end if;
	end if;

  	if ltrim(rtrim(@RestrCode))=='' then
    	RestrCode=NULL;
	end if;

  	if (RestrCode is NOT NULL) then
		if exists (select from public."Restrictions" where ("RestrCode" is NOT NULL) and ("RestrCode"=RestrCode)) then
	  		return;
		end if;
  	end if;

  	if (RestrType<>8) and (Sign is NULL) then
    	return;
	end if;

  	if (RestrType<>8) and (Limit_ is NULL) then
    	return;
	end if;

  	if RestrType==0 then
    	if OnClient==1 then 
			RType='C';
    	else
      		RType='G';
		end if;
    
		RSign = Sign;
    	RLimit = Limit_;
    	RValue = NULL;
    	CoSecGroupID = NULL;
	elsif RestrType==1 then
    	if OnClient==1 then 
      		RType='c';
    	else
      		RType='g';
		end if;
		
		RSign = Sign;
		RLimit = Limit_;
    	RValue = NULL;
	elsif RestrType==2 then
    	if OnClient==1 then 
      		RType='C';
    	else
			RType='G';
		end if;
		
		RSign = Sign+'A';
    	RLimit = NULL;
    	RValue = Limit_;
    	CoSecGroupID = NULL;
	elsif RestrType==3 then
    	if OnClient==1 then 
			RType='C';
    	else
      		RType='G';
		end if;
		
    	RSign = Sign+'V';
    	RLimit = Limit_;
    	RValue = NULL;
    	CoSecGroupID = NULL;
	elsif RestrType==4 then
    	if OnClient==1 then 
      		RType='C';
    	else
			RType='G';
		end if;
		
    	RSign = Sign+'E';
    	RLimit = Limit_;
    	RValue = NULL;
		
    	if ModeAnySecurity==0 then
      		SecGroupID = -1;
		end if;
		
		CoSecGroupID = -1;
	elsif RestrType==5 then
    	if OnClient==1 then 
			RType='C';
    	else
      		RType='G';
		end if;
		
    	RSign = Sign+'E';
    	RLimit = Limit_;
    	RValue = NULL;
    
		if ModeAnySecurity==0 then
      		SecGroupID = -1;
  		end if;
		
	elsif RestrType==6 then
    	if OnClient==1 then 
      		RType='C';
    	else
      		RType='G';
		end if;
		
    	RSign = Sign+'E';
    	RLimit = NULL;
    	RValue = Limit_;
		
    	if ModeAnySecurity==0 then
      		SecGroupID = -1;
    	end if;
		
		CoSecGroupID = -1;
	
	elsif RestrType==7 then
    	if OnClient==1 then 
			RType='C';
    	else
      		RType='G';
		end if;
    
		RSign = Sign+'C';
    	RLimit = -1;
    	RValue = Limit_;
    	CoSecGroupID = -1;

	elsif RestrType==8 then
    	if OnClient==1 then 
      		RType='C';
    	else
      		RType='G';
		end if;
		
    	RSign = '!';
    	RLimit = NULL;
    	RValue = NULL;
    	CoSecGroupID = NULL;
	elsif RestrType==9 then
    	if OnClient==1 then 
			RType='C';
    	else
      		RType='G';
		end if;
		
    	RSign = Sign+'DA';
		RLimit = NULL;
		RValue = Limit_;
    	SecGroupID = -1;
    	CoSecGroupID = -1;

	elsif RestrType==10 then
    	if OnClient==1 then 
      		RType='C';
    	else
      		RType='G';
		end if;
		
		RSign = Sign+'D';
		RLimit = Limit_;
    	RValue = NULL;
    	SecGroupID = -1;
    	CoSecGroupID = -1;

	elsif RestrType==11 then
    	if OnClient==1 then 
      		RType='C';
    	else
		  	RType='G';
		end if;
		
		RSign = Sign+'EJ';
		RLimit = Limit_;
		RValue = -1;
    
		if ModeAnyEmitent==0 then
      		SecGroupID = -1;
    	end if;
	
		CoSecGroupID = -1;
 
 	elsif RestrType==12 then
    	if OnClient==1 then 
      		RType='C';
    	else
      		RType='G';
		end if;
		
    	RSign = Sign+'EJ';
    	RLimit = Limit_;
    	RValue = -1;
    
		if ModeAnyEmitent==0 then
      		SecGroupID = -1;
  		end if;
		
 	elsif RestrType==13 then
    	if OnClient==1 then 
      		RType='C';
    	else
      		RType='G';
		end if;
		
    	RSign = Sign+'EJ';
		RLimit = -1;
    	RValue = Limit_;
		
    	if ModeAnyEmitent==0 then
      		SecGroupID = -1;
    	end if;
	
		CoSecGroupID = -1;
  	else 
		return;
	end if;
	
  	if OnClient==2 and ModeSummary==1 then
    	RSign = RSign+'S';
	end if;

  	if NotUseAccInt==1 then
    	RSign = RSign+'I';
	end if;

  	if RestrType==3 and ModeEmitent==1 then
    	RSign = RSign+'K';
	end if;

  	TMSessionID = 'stored proc ('+user_name()+')';

  	call public.tm_createrestriction(RType, ClientCode, RSign, RLimit,
                              SecGroupID, CoSecGroupID, RValue,
                              0, TMSessionID, ID, TMCounter,
                              LimitKind, RestrCode, RestrName);
--	set nocount off
end
$BODY$;

DROP PROCEDURE IF EXISTS public."CreateNewSecGroup"(character varying, character varying, integer, integer, integer, integer);

CREATE OR REPLACE PROCEDURE public."CreateNewSecGroup"(
	IN secgroupcode character varying,
	IN secgroupname character varying,
	IN modelogicaland integer,
	IN moderatings integer,
	IN ragencies integer,
	IN listlevel integer,
	OUT secgroupid integer)
LANGUAGE 'plpgsql'
AS $BODY$
declare
	RMode varchar;
begin
	
	SecGroupID = -1;
	
	if(ModeLogicalAnd==1 and ModeRatings==1) then
		return;
	end if;
	
	if ListLevel is null then
		ListLevel = 1;
	end if;
	
	if ((ListLevel < -1 or ListLevel > 3) or (ListLevel > -1 and ModeRatings == 1)) then
		return;
	end if;
	
	RMode = '';
	
	if ModeLogicalAnd == 1 then
		RMode = RMode + 'A';
	end if;
	
	if ModeRatings == 1 then
		RMode = RMode + 'R';
	end if;
	
	call public.tm_createsecgroup(SecGroupCode, SecGroupName, RMode, RAgencies, ListLevel, SecGroupID);
	
end
$BODY$;

DROP PROCEDURE IF EXISTS public."CreateNewSecGroupMember"(integer, character, character varying, character varying, integer);

CREATE OR REPLACE PROCEDURE public."CreateNewSecGroupMember"(
	IN secgroupid integer,
	IN membertype character,
	IN membercode character varying,
	IN memberclass character varying,
	IN memberid integer,
	OUT success integer)
LANGUAGE 'plpgsql'
AS $BODY$
declare
	GroupModeRatings integer;
	RMode varchar;
begin
	
	Success = 0;

  	if not exists (select from public."SecGroups" where "ID"=SecGroupID and "disabled"='n') then
    	return;
	end if;

  	select "Mode" into RMode from public."SecGroups" where "ID"=SecGroupID and "disabled"='n';

  	if position('R' in RMode) == 0 then
    	GroupModeRatings=0;
  	else
    	GroupModeRatings=1;
	end if;

  	if MemberType == 'R' then
    	if GroupModeRatings == 0 then
      		return;
		end if;
    	if not exists select from public."RatingAgencies" where "AgencyCode"=MemberCode then
      		return;
		end if;
    	
		if not exists select from public."RatingAgencies" ra, "RatingScales" rs
                   where ra."AgencyID"=rs."AgencyID" and ra."AgencyCode"=MemberCode and
                         rs."Rating"=MemberClass and rs."National"=MemberID then
      		return;
		end if;
  	else 
    	if GroupModeRatings==1 then
      		return;
		end if;
  	end if;
	
  	insert into public."SecGrMemb" ("GroupID", "MembType", "MembCode", "MembClass", "MembID")
  		values (SecGroupID, MemberType, MemberCode, MemberClass, MemberID);

  Success = 1;
	
end
$BODY$;

DROP PROCEDURE IF EXISTS public."DeleteRestriction"(integer);

CREATE OR REPLACE PROCEDURE public."DeleteRestriction"(
	IN id integer,
	OUT res integer)
LANGUAGE 'plpgsql'
AS $BODY$
begin
	
	if exist select from public."Restrictions" where "ID"=ID and "disabled"='n' then
		Res = 1;
		update public."Restrictions"
		set "disabled" = 'y'
		where "ID" = ID and "disabled" = 'n';
	else
		Res = 0;
	end if;
end
$BODY$;

DROP PROCEDURE IF EXISTS public."DeleteSecGroup"(integer);

CREATE OR REPLACE PROCEDURE public."DeleteSecGroup"(
	IN secgroupid integer,
	OUT success integer)
LANGUAGE 'plpgsql'
AS $BODY$
begin
	
	if exist select from public."SecGroups" where "ID"=SecGroupID and "disabled"='n' then
		Success = 1;
		update public."SecGroups"
		set "disabled" = 'y'
		where "ID" = SecGroupID and "disabled" = 'n';
	else
		Success = 0;
	end if;
end
$BODY$;

DROP PROCEDURE IF EXISTS public."DeleteSecGroupMember"(integer, character, character varying, character varying, integer);

CREATE OR REPLACE PROCEDURE public."DeleteSecGroupMember"(
	IN secgroupid integer,
	IN membertype character,
	IN membercode character varying,
	IN memberclass character varying,
	IN memberid integer,
	OUT success integer)
LANGUAGE 'plpgsql'
AS $BODY$
declare
	rowcnt bigint;
begin
	
	Success = 0;
	
	delete from public."SecGrMemb"
	where "GroupID"=SecGroupID and "MembType" = MembType and
		"MembCode"=MembCode and "MembClass"=MemberClass and
		"membID" = MemberID;
		
	GET DIAGNOSTICS rowcnt = ROW_COUNT;
	
	if rowcnt == 1 then
		Success = 1;
	end if;
end
$BODY$;

DROP PROCEDURE IF EXISTS public."EditRestriction"(integer, integer, character, double precision, integer, integer, integer, character varying, character varying);

CREATE OR REPLACE PROCEDURE public."EditRestriction"(
	IN id integer,
	IN restrtype integer,
	IN sign character,
	IN limit_ double precision,
	IN secgroupid integer,
	OUT res integer,
	IN nocheck integer DEFAULT NULL::integer,
	IN limitkind integer DEFAULT NULL::integer,
	IN restrcode character varying DEFAULT NULL::character varying,
	IN restrname character varying DEFAULT NULL::character varying)
LANGUAGE 'plpgsql'
AS $BODY$
declare 
	RLimit float;
  	RValue float;
  	RSign varchar(10);
  	RSecGroupID int;
  	RNoCheck int;
  	RLimitKind int;
  	RRestrCode varchar(12);
  	RRestrName varchar(50);
begin

  	Res = 0;

  	if (RestrType<0) or (RestrType>13) then
    	return;
	end if;

  	if not exists (select from public."Restrictions" where "ID"=ID and "disabled"='n') then
    	return;
	end if;

	select "Mode" into RSign from public."Restrictions" where "ID"=ID and "disabled"='n';
	select "Limit" into RLimit from public."Restrictions" where "ID"=ID and "disabled"='n';
	select "Value" into RValue from public."Restrictions" where "ID"=ID and "disabled"='n';
	select "SecGroupID" into RSecGroupID from public."Restrictions" where "ID"=ID and "disabled"='n';
	select "NoCheck" into RNoCheck from public."Restrictions" where "ID"=ID and "disabled"='n';
	select "LimitKind" into RLimitKind from public."Restrictions" where "ID"=ID and "disabled"='n';
	select "RestrCode" into RRestrCode from public."Restrictions" where "ID"=ID and "disabled"='n';
	select "RestrName" into RRestrName from public."Restrictions" where "ID"=ID and "disabled"='n';

  	if RestrType==0 then
    	if Limit_ is NOT NULL then
      		RLimit = Limit_;
		end if;
		
      	RValue = NULL;
	
	    if SecGroupID is NOT NULL then
    		RSecGroupID = SecGroupID;
		end if;
	
	elsif RestrType==1 then
    	if Limit_ is NOT NULL then
      		RLimit = Limit_;
      		RValue = NULL;
		end if;
	    
		if SecGroupID is NOT NULL then
			RSecGroupID = SecGroupID;
		end if;

	elsif RestrType==2 then
    	if Limit_ is NOT NULL then
      		RLimit = NULL;
      		RValue = Limit_;
		end if;
		
    	if SecGroupID is NOT NULL then
      		RSecGroupID = SecGroupID;
	 	end if;

	elsif RestrType==3 then
    	if Limit_ is NOT NULL then
      		RLimit = Limit_;
			RValue = NULL;
	  	end if;
	
		if SecGroupID is NOT NULL then
      		RSecGroupID = SecGroupID;
	  	end if;

	elsif RestrType==4 then
    	if Limit_ is NOT NULL then
      		RLimit = Limit_;
      		RValue = NULL;
    	end if;
		
    	if SecGroupID is NOT NULL then
      		RSecGroupID = SecGroupID;
		end if;
	elsif RestrType==5 then
    	if Limit_ is NOT NULL then
      		RLimit = Limit_;
      		RValue = NULL;
    	end if;
		
    	if SecGroupID is NOT NULL then
      		RSecGroupID = SecGroupID;
  		end if;
  elsif RestrType==6 then
    	if Limit_ is NOT NULL then
      		RLimit = NULL;
      		RValue = Limit_;
    	end if;
    	
		if SecGroupID is NOT NULL then
      		RSecGroupID = SecGroupID;
  		end if;
  elsif RestrType==7 then
    	if Limit_ is NOT NULL then
			RLimit = -1;
      		RValue = Limit_;
    	end if;
		
    	if SecGroupID is NOT NULL then
      		RSecGroupID = SecGroupID;
  		end if;
  elsif RestrType==8 then
    	if SecGroupID is NOT NULL then
      		RSecGroupID = SecGroupID;
  		end if;
  elsif RestrType==9 then
    	if Limit_ is NOT NULL then
      		RLimit = NULL;
      		RValue = Limit_;
    	end if;
		
    	RSecGroupID = -1;

  elsif RestrType==10 then
    	if Limit_ is NOT NULL then
      		RLimit = Limit_;
      		RValue = NULL;
    	end if;
		
    	RSecGroupID = -1;

  elsif RestrType==11 then
    	if Limit_ is NOT NULL then
      		RLimit = Limit_;
      		RValue = -1;
    	end if;
    
		if SecGroupID is NOT NULL then
      		RSecGroupID = SecGroupID;
  		end if;
  elsif RestrType==12 then
    	if Limit_ is NOT NULL then
      		RLimit = Limit_;
      		RValue = -1;
    	end if;
		
    	if SecGroupID is NOT NULL then
      		RSecGroupID = SecGroupID;
  		end if;
  elsif RestrType==13 then
    	if Limit_ is NOT NULL then
      		RLimit = -1;
      		RValue = Limit_;
    	end if;
    
		if SecGroupID is NOT NULL then
      		RSecGroupID = SecGroupID;
  		end if;
  else
    return;
  end if;
    
  if Sign is NOT NULL then
    RSign = replace(RSign, '<', '^');
    RSign = replace(RSign, '>', '^');
    RSign = replace(RSign, '!', '^');
    RSign = replace(RSign, '^', Sign);
  end if;

  if NoCheck is NOT NULL then
    	RNoCheck = NoCheck;
  end if;

  if LimitKind is NOT NULL then
    	RLimitKind = LimitKind;
  end if;

  if RestrName is NOT NULL then
  		RRestrName = RestrName;
  end if;

  if (RestrCode is NOT NULL) and 
     (ltrim(rtrim(RestrCode))<>'') and
     ((RRestrCode is NULL) or (ltrim(rtrim(RRestrCode))='')) then
		RRestrCode=RestrCode;
  end if;

  update public."Restrictions"
  set "Mode"=RSign, "Limit"=RLimit, "Value"=RValue, "SecGroupID"=RSecGroupID,
      "NoCheck"=RNoCheck, "LimitKind"=RLimitKind, "RestrCode"=RRestrCode,
	  "RestrName"=RRestrName
  where "ID"=ID and "disabled"='n';

  Res=1;
end
$BODY$;

DROP PROCEDURE IF EXISTS public."ModifySecGroup"(integer, character varying, integer, integer);

CREATE OR REPLACE PROCEDURE public."ModifySecGroup"(
	IN secgroupid integer,
	IN secgroupname character varying,
	IN modelogicaland integer,
	IN listlevel integer,
	OUT success integer)
LANGUAGE 'plpgsql'
AS $BODY$
declare
	RSecGroupName integer;
	RMode varchar;
	RListLevel integer;
begin
	
	Success = 0;

  	if not exists (select from public."SecGroups" where "ID"=SecGroupID and "disabled"='n') then
    	return;
	end if;
    
	select "SecGName", "Mode", "ListLevel"
	into RSecGroupName, RMode, RListLevel
	from public."SecGroups"
	where "ID" = SecGroupID and "disabled" = 'n';
	
	if SecGroupName is NOT NULL then
    	RSecGroupName = SecGroupName;
	end if;
    
  	if (ModeLogicalAnd is NOT NULL) then
    	RMode = replace(RMode, 'A', '');
    	if ModeLogicalAnd == 1 then
      		RMode = 'A' + RMode;
		end if;
  	end if;
  
  	if (ListLevel is NOT NULL) then
		RListLevel = ListLevel;
	end if;

  	update public."SecGroups"
  	set "SecGName"=RSecGroupName, "Mode"=RMode, "ListLevel"=RListLevel
  	where "ID"=SecGroupID and "disabled"='n';

  	Success=1;
	
end
$BODY$;

DROP PROCEDURE IF EXISTS public."TM_AddClientToGroup"(character varying, character varying, integer, character varying);

CREATE OR REPLACE PROCEDURE public."TM_AddClientToGroup"(
	IN groupcode character varying,
	IN clientcode character varying,
	IN weight integer,
	IN comment character varying,
	OUT groupid integer,
	OUT clientnum integer)
LANGUAGE 'plpgsql'
AS $BODY$
declare
	clientsGroupId integer;
begin
	select "GroupID"
	into GroupId
	from public."GroupDesc"
	where "GroupCode" = GroupCode;
	
	if GroupId is not null then
		
		select "GroupID"
		into clientsGroupId
		from public."Clients"
		where "ClientCode" = ClientCode and "GroupID" = GroupId;
		
		if clientsGroupId is null then
			select max("Num") + 1
			into ClientNum
			from public."Clients"
			where "GroupID" = GroupId;
			
			if ClientNum is null then 
				ClientNum = 1;
			end if;
			
			insert into public."Clients" ("ClientCode", "GroupID", "Num", "Weight", "Comment")
			values (ClientCode, GroupId, ClientNum, Weight, Comment)
			returning "GroupID" into GroupId;
			
		end if;
		
	end if;
end
$BODY$;

DROP PROCEDURE IF EXISTS public."TM_CreateClientGroup"(character varying, character varying, character varying, double precision, integer);

CREATE OR REPLACE PROCEDURE public."TM_CreateClientGroup"(
	IN groupcode character varying,
	IN groupname character varying,
	IN mode character varying,
	IN defweight double precision,
	IN splitorders integer,
	OUT groupid integer)
LANGUAGE 'plpgsql'
AS $BODY$
BEGIN
	insert into public."GroupDesc" ("GroupCode", "GroupName", "disabled", "Mode", "DefWeight", "SplitOrders")
		values (GroupCode, GroupName, 'n', Mode, DefWeight, SplitOrders) returning "GroupID" into GroupId;
END
$BODY$;

DROP PROCEDURE IF EXISTS public."TM_CreateClientGroupEx"(character varying, character varying, character varying, character varying, double precision, integer);

CREATE OR REPLACE PROCEDURE public."TM_CreateClientGroupEx"(
	IN groupcode character varying,
	IN groupname character varying,
	IN disabled character varying,
	IN mode character varying,
	IN defweight double precision,
	IN splitorders integer,
	OUT groupid integer)
LANGUAGE 'plpgsql'
AS $BODY$
BEGIN
	insert into public."GroupDesc" ("GroupCode", "GroupName", "disabled", "Mode", "DefWeight", "SplitOrders")
		values (GroupCode, GroupName, disabled, Mode, DefWeight, SplitOrders) returning "GroupID" into GroupId;
END
$BODY$;

DROP PROCEDURE IF EXISTS public."TM_CreateExportOrder"(integer, integer, integer, integer, integer, integer, integer, character, character, character varying, character varying, character varying, character varying, double precision, double precision, double precision, integer, integer, character varying, integer, character varying, double precision, double precision, double precision, double precision, double precision, double precision, integer);

CREATE OR REPLACE PROCEDURE public."TM_CreateExportOrder"(
	IN orderid integer,
	IN grouporderid integer,
	IN ordertype integer,
	IN tradedate integer,
	IN senddate integer,
	IN sendtime integer,
	IN ordertime integer,
	IN buy character,
	IN ml character,
	IN seccode character varying,
	IN classcode character varying,
	IN clientcode character varying,
	IN trdacc character varying,
	IN price double precision,
	IN qty double precision,
	IN balance double precision,
	IN status integer,
	IN usergroupid integer,
	IN usergroupcode character varying,
	IN poolid integer,
	IN brokerpoolid character varying,
	IN visibleqty double precision,
	OUT exportorderid integer,
	IN repovalue double precision DEFAULT 0,
	IN discount double precision DEFAULT 0,
	IN discountupper double precision DEFAULT 0,
	IN discountlower double precision DEFAULT 0,
	IN reporate double precision DEFAULT 0,
	IN repoterm integer DEFAULT 0)
LANGUAGE 'plpgsql'
AS $BODY$
begin
	select max("ID") + 1
	into ExportOrderID
	from public."ExportOrder";
	
	if ExportOrderID is null then
		ExportOrderID := 1;
	end if;
	
	insert into public."ExportOrder" ("ID", "OrderID", "GroupOrderID", "OrderType", "TradeDate", "SendDate", "SendTime", "OrderTime", "Status", "UserGroupID",
									  "UserGroupCode", "PoolID", "BrokerPoolID", "ClientCode", "TrdAcc", "Price", "Qty", "Balance", "VisibleQty", "Buy", "ML",
									  "ClassCode", "SecCode", "RepoValue", "Discount", "DiscountUpper", "DiscountLower", "RepoRate", "RepoTerm")
	values (ExportOrderID, OrderID, GroupOrderID, OrderType, TradeDate, SendDate, SendTime, OrderTime, Status, UserGroupID, UserGroupCode, PoolID, BrokerPoolID,
			ClientCode, TrdAcc, Price, Qty, Balance, VisibleQty, Buy, ML, ClassCode, SecCode, RepoValue, Discount, DiscountUpper, DiscountLower, RepoRate, RepoTerm);
end
$BODY$;

DROP PROCEDURE IF EXISTS public."TM_CreateGroupNegOrder"(integer, integer, integer, character, character varying, character varying, character varying, character varying, character varying, double precision, double precision, double precision, double precision, double precision, character, character, integer, double precision, double precision, double precision, double precision, double precision, double precision, integer, integer, integer, integer, character varying);

CREATE OR REPLACE PROCEDURE public."TM_CreateGroupNegOrder"(
	IN groupid integer,
	IN tradedate integer,
	IN tradetime integer,
	IN buy character,
	IN seccode character varying,
	IN classcode character varying,
	IN cpfirmid character varying,
	IN settlecode character varying,
	IN matchref character varying,
	IN price double precision,
	IN vozm double precision,
	IN qty double precision,
	IN realqty double precision,
	IN killedqty double precision,
	IN status character,
	IN result character,
	IN orderscount integer,
	OUT groupnegorderid integer,
	IN repovalue double precision,
	IN realrepovalue double precision,
	IN discount double precision,
	IN discountupper double precision,
	IN discountlower double precision,
	IN reporate double precision,
	IN repoterm integer,
	IN ordertype integer,
	IN flags integer,
	IN tmuserid integer,
	IN tmsessionid character varying,
	OUT tmcounter integer)
LANGUAGE 'plpgsql'
AS $BODY$
BEGIN
	select max("ID") + 1 from public."GroupNegOrders" into GroupNegOrderID;
	
	if(GroupNegOrderID is null) then
		GroupNegOrderID = 1;
	end if;
	
	call "TM_GetCounter"('GROUPNEGORDERS', TMCounter);
	
	insert into public."GroupNegOrders"("ID", "GroupID", "TradeDate", "TradeTime", "Buy", "SecCode", "ClassCode", "CPFirmID", "SettleCode", "MatchRef", "Price", "Vozm", "Qty", "RealQty", "KilledQty", "Status", "Result", "OrdersCount", "RepoValue", "RealRepoValue", "Discount", "DiscountUpper", "DiscountLower", "RepoRate", "RepoTerm", "OrderType", "Flags", "TMUserID", "TMSessionID", "TMCounter")
	values (GroupNegOrderID, GroupID, TradeDate, TradeTime, Buy, SecCode, ClassCode, CPFirmID, SettleCode, MatchRef, Price, Vozm, Qty, RealQty, KilledQty, Status, Result, OrdersCount, RepoValue, RealRepoValue, Discount, DiscountUpper, DiscountLower, RepoRate, RepoTerm, OrderType, Flags, TMUserID, TMSessionID, TMCounter);
END
$BODY$;

DROP PROCEDURE IF EXISTS public."TM_CreateGroupOrder"(integer, integer, integer, character, character, character varying, character varying, double precision, double precision, double precision, double precision, double precision, character, character, integer, character varying, integer, double precision, double precision, integer, character varying);

CREATE OR REPLACE PROCEDURE public."TM_CreateGroupOrder"(
	IN groupid integer,
	IN tradedate integer,
	IN tradetime integer,
	IN buy character,
	IN ml character,
	IN seccode character varying,
	IN classcode character varying,
	IN price double precision,
	IN qty double precision,
	IN realqty double precision,
	IN balanceqty double precision,
	IN killedqty double precision,
	IN status character,
	IN result character,
	IN ordercount integer,
	IN flags character varying,
	IN poolid integer,
	IN value double precision,
	IN visibleqty double precision,
	IN tmuserid integer,
	IN tmsessionid character varying,
	OUT grouporderid integer,
	OUT tmcounter integer)
LANGUAGE 'plpgsql'
AS $BODY$
begin
	select max("ID") + 1
	into GroupOrderID
	from public."GroupOrders";
	
	if GroupOrderID is null then
		GroupOrderID = 1;
	end if;
	
	call "TM_GetCounter"('GROUPORDERS', tmcounter);
	
	insert into public."GroupOrders" ("ID", "GroupID", "TradeDate", "TradeTime", "Buy", "ML", "SecCode", "ClassCode", "Price", "Qty", "RealQty", "BalanceQty",
									  "KilledQty", "Status", "Result", "OrdersCount", "Flags", "PoolID", "Value", "VisibleQty", "TMUserID", "TMSessionID", "TMCounter")
	values (GroupOrderID, GroupID, TradeDate, TradeTime, Buy, ML, SecCode, ClassCode, Price, Qty, RealQty, BalanceQty, KilledQty, Status, Result, OrderCount, Flags,
			PoolID, Value, VisibleQty, TMUserID, TMSessionID, TMCounter);
end
$BODY$;

DROP PROCEDURE IF EXISTS public."TM_CreateGroupStopOrder"(integer, integer, integer, integer, character, character, character varying, character varying, character varying, character varying, integer, integer, double precision, double precision, double precision, double precision, double precision, double precision, double precision, double precision, double precision, double precision, character, character, integer, integer, double precision, integer, double precision, integer, integer);

CREATE OR REPLACE PROCEDURE public."TM_CreateGroupStopOrder"(
	IN groupid integer,
	IN stopstyle integer,
	IN tradedate integer,
	IN tradetime integer,
	IN buy character,
	IN ml character,
	IN seccode character varying,
	IN classcode character varying,
	IN aseccode character varying,
	IN aclasscode character varying,
	IN stopcond integer,
	IN transflags integer,
	IN stopprice double precision,
	IN price double precision,
	IN linkedprice double precision,
	IN offset_ double precision,
	IN spread double precision,
	IN qty double precision,
	IN realqty double precision,
	IN balanceqty double precision,
	IN killedqty double precision,
	IN baseordernum double precision,
	IN status character,
	IN result character,
	IN orderscount integer,
	IN linkedgrorder integer,
	IN coordernum double precision,
	IN expire integer,
	IN stopprice2 double precision,
	IN activefromtime integer,
	IN activetotime integer,
	OUT groupstoporderid integer)
LANGUAGE 'plpgsql'
AS $BODY$
begin
	
	select max("ID") + 1
	into GroupStoporderID
	from public."GroupStopOrders";
	
	if(GroupStoporderID is null) then
		GroupStoporderID = 1;
	end if;
	
	insert into public."GroupStopOrders"("ID", "GroupID", "StopStyle", "TradeDate", "TradeTime", "Buy", "ML", "SecCode", "ClassCode", "ASecCode", "AClassCode", "StopCond", "TransFlags", "StopPrice", "Price", "LinkedPrice", "Offset", "Spread", "Qty", "RealQty", "BalanceQty", "KilledQty", "BaseOrderNum", "Status", "Result", "OrdersCount", "LinkedGrOrder", "CoOrderNum", "Expire", "StopPrice2", "ActiveFromTime", "ActiveToTime")
	values (GroupStopOrderID, GroupID, StopStyle, TradeDate, TradeTime, Buy, ML, SecCode, ClassCode, ASecCode, AClassCode,
            StopCond, TransFlags, StopPrice, Price, LinkedPrice, Offset_, Spread, Qty, RealQty, BalanceQty, KilledQty,
            BaseOrderNum, Status, Result, OrdersCount, LinkedGrOrder, CoOrderNum, Expire, StopPrice2, ActiveFromTime, ActiveToTime);
	
end
$BODY$;

DROP PROCEDURE IF EXISTS public."TM_CreateNegOrder"(integer, integer, integer, character, character varying, character varying, character varying, character varying, character varying, character varying, character varying, double precision, double precision, double precision, double precision, character, character, double precision, double precision, double precision, double precision, double precision, integer, integer, integer, character varying);

CREATE OR REPLACE PROCEDURE public."TM_CreateNegOrder"(
	IN grouporderid integer,
	IN tradedate integer,
	IN tradetime integer,
	IN buy character,
	IN seccode character varying,
	IN classcode character varying,
	IN clientcode character varying,
	IN trdacc character varying,
	IN cpfirmid character varying,
	IN settlecode character varying,
	IN matchref character varying,
	IN price double precision,
	IN vozm double precision,
	IN qty double precision,
	IN ordernum double precision,
	IN status character,
	IN result character,
	OUT negorderid integer,
	IN repovalue double precision,
	IN discount double precision,
	IN discountupper double precision,
	IN discountlower double precision,
	IN reporate double precision,
	IN repoterm integer,
	IN flags integer,
	IN tmuserid integer,
	IN tmsessionid character varying,
	OUT tmcounter integer)
LANGUAGE 'plpgsql'
AS $BODY$
begin
    select max("OrderId")+1
	into NegOrderID
	from public."NegOrders";
	
    if (NegOrderID is NULL) then
      NegOrderID=1;
	end if;
    call "TM_GetCounter"('NEGORDERS', TMCounter);
    insert into public."NegOrders"(
	"OrderId", "GroupOrderID", "TradeDate", "TradeTime", "Buy", "ClassCode", "SecCode", "ClientCode", "TrdAcc", "CPFirmId", "SettleCode", "MatchRef", "Price", "Vozm", "Qty", "OrderNum", "Status", "Result", "RepoValue", "Discount", "DiscountUpper", "DiscountLower", "RepoRate", "RepoTerm", "Flags", "TMUserID", "TMSessionID", "TMCounter")
	
    values (NegOrderID, GroupOrderID, TradeDate, TradeTime, Buy, SecCode, ClassCode, ClientCode, TrdAcc,
                                                       CPFirmID, SettleCode, MatchRef, Price, Vozm, Qty, OrderNum, Status, Result,
                                                       RepoValue, Discount, DiscountUpper, DiscountLower,
                                                       RepoRate, RepoTerm, Flags, TMUserID, TMSessionID, TMCounter);

end
$BODY$;

DROP PROCEDURE IF EXISTS public."TM_CreateOrder"(integer, integer, integer, character, character, character varying, character varying, character varying, character varying, double precision, double precision, double precision, double precision, character, character, integer, double precision, double precision, integer, character varying);

CREATE OR REPLACE PROCEDURE public."TM_CreateOrder"(
	IN grouporderid integer,
	IN tradedate integer,
	IN tradetime integer,
	IN buy character,
	IN ml character,
	IN seccode character varying,
	IN classcode character varying,
	IN clientcode character varying,
	IN trdacc character varying,
	IN price double precision,
	IN qty double precision,
	IN balanceqty double precision,
	IN ordernum double precision,
	IN status character,
	IN result character,
	IN orderinpoolid integer,
	IN value double precision,
	IN visibleqty double precision,
	IN tmuserid integer,
	IN tmsessionid character varying,
	OUT orderid integer,
	OUT tmcounter integer)
LANGUAGE 'plpgsql'
AS $BODY$
begin
	select max("OrderID") + 1
	into OrderID
	from public."Orders";
	
	if OrderID is null then
		OrderID = 1;
	end if;
	
	call public."TM_GetCounter"('ORDERS', tmcounter);
	
	insert into public."Orders" ("OrderID", "GroupOrderID", "Buy", "ML", "TradeDate", "TradeTime", "SecCode", "ClassCode", "ClientCode", "TrdAcc", "Price", "Qty",
								 "BalanceQty", "OrderNum", "Status", "Result", "OrderInPoolID", "Value", "VisibleQty", "TMUserID", "TMSessionID", "TMCounter")
	values (OrderID, GroupOrderID, Buy, ML, TradeDate, TradeTime, SecCode, ClassCode, ClientCode, TrdAcc, Price, Qty, BalanceQty, OrderNum, Status, Result, OrderInPoolID,
		   Value, VisibleQty, TMuserID, TMSessionID, TMCounter);
end
$BODY$;

DROP PROCEDURE IF EXISTS public."TM_CreateOrderInPool"(integer, character, character varying, character varying, character varying, double precision, double precision, character, character, double precision, double precision, double precision, double precision, double precision, character varying);

CREATE OR REPLACE PROCEDURE public."TM_CreateOrderInPool"(
	IN poolid integer,
	IN ordertype character,
	IN clientcode character varying,
	IN classcode character varying,
	IN seccode character varying,
	IN price double precision,
	IN qty double precision,
	IN buy character,
	IN ml character,
	IN sendqty double precision,
	IN execqty double precision,
	IN rejectqty double precision,
	IN killqty double precision,
	IN activeqty double precision,
	IN comment character varying,
	OUT orderid integer)
LANGUAGE 'plpgsql'
AS $BODY$
begin
    INSERT INTO public."OrdersInPool"(
	"PoolID", "OrderType", "ClientCode", "ClassCode", "SecCode", "Price", "Qty", "Buy", "ML", "SendQty", "ExecQty", "RejectQty", "KillQty", "ActiveQty", "Comment")
	
    values (PoolID, OrderType, ClientCode, ClassCode, SecCode, Price, Qty, Buy, ML, SendQty, ExecQty,
                                                        RejectQty, KillQty, ActiveQty, Comment) 
	returning "OrderID" into OrderID;
end
$BODY$;

DROP PROCEDURE IF EXISTS public."TM_CreatePool"(character varying, character varying, integer, integer, integer, character varying, character varying, integer, double precision, double precision, double precision, double precision, double precision, double precision);

CREATE OR REPLACE PROCEDURE public."TM_CreatePool"(
	IN brokerpoolid character varying,
	IN poolname character varying,
	IN loaddate integer,
	IN loadtime integer,
	IN expiredate integer,
	IN source character varying,
	IN status character varying,
	IN killtime integer,
	IN totalqty double precision,
	IN sendqty double precision,
	IN execqty double precision,
	IN rejectqty double precision,
	IN killedqty double precision,
	IN activeqty double precision,
	OUT poolid integer)
LANGUAGE 'plpgsql'
AS $BODY$
begin
    INSERT INTO public."PoolOrders"(
		"BrokerPoolID", "PoolName", "LoadDate", "LoadTime", "ExpireDate", "Source", "Status", "KillTime", "TotalQty", "SendQty", "ExecQty", "RejectQty", "KilledQty", "ActiveQty")
    values (BrokerPoolID, PoolName, LoadDate, LoadTime, ExpireDate, Source, Status, KillTime, TotalQty,
                                                   SendQty, ExecQty, RejectQty, KilledQty, ActiveQty)
	returning "PoolID" into PoolID;
end
$BODY$;

DROP PROCEDURE IF EXISTS public."TM_CreateRatingAgency"(character varying, character varying);

CREATE OR REPLACE PROCEDURE public."TM_CreateRatingAgency"(
	IN agencycode character varying,
	IN agencyname character varying,
	OUT agencyid integer)
LANGUAGE 'plpgsql'
AS $BODY$
begin
    insert into public."RatingAgencies" ("AgencyCode", "AgencyName")
    values (AgencyCode, AgencyName)
    returning "AgencyID" into AgencyID;
end
$BODY$;

DROP PROCEDURE IF EXISTS public."TM_CreateRatingScale"(integer, integer, character varying, integer);

CREATE OR REPLACE PROCEDURE public."TM_CreateRatingScale"(
	IN agencyid integer,
	IN national_ integer,
	IN rating character varying,
	IN position_ integer,
	OUT ratingid integer)
LANGUAGE 'plpgsql'
AS $BODY$
begin
    insert into public."RatingScales" ("AgencyID", "National", "Rating", "Position")
    values (AgencyID, National_, Rating, Position_)
    returning "RatingID" into RatingID;
end
$BODY$;

DROP PROCEDURE IF EXISTS public."TM_CreateRestriction"(character, character varying, character varying, double precision, integer, integer, numeric, integer, character varying, integer, character varying, character varying);

CREATE OR REPLACE PROCEDURE public."TM_CreateRestriction"(
	IN resttype character,
	IN client character varying,
	IN mode character varying,
	IN limit_ double precision,
	IN secgroupid integer,
	IN cosecgroupid integer,
	IN value numeric,
	IN tmuserid integer,
	IN tmsessionid character varying,
	OUT restrid integer,
	OUT tmcounter integer,
	IN limitkind integer DEFAULT 0,
	IN restrcode character varying DEFAULT NULL::character varying,
	IN restrname character varying DEFAULT NULL::character varying)
LANGUAGE 'plpgsql'
AS $BODY$
begin
    select max("ID")+1
	into RestrID 
	from public."Restrictions";
	
	if (RestrID is null) then
      RestrID=1;
	end if;
    call "TM_GetCounter"('RESTRICTIONS', TMCounter);
    insert into public."Restrictions" ("ID", "RestType", "Client", "Mode", "Limit",
	                                  "SecGroupID", "CoSecGroupID", "disabled",
									  "Value", "NoCheck", "LimitKind", "RestrCode", "RestrName",
									  "TMUserID", "TMSessionID", "TMCounter")
    values (RestrID, RestType, Client, Mode, Limit_,
	        SecGroupID, CoSecGroupID, 'n',
			Value, 0, LimitKind, RestrCode, RestrName,
			TMUserID, TMSessionID, TMCounter);
end
$BODY$;

DROP PROCEDURE IF EXISTS public."TM_CreateSecGroup"(character varying, character varying, character varying, character varying, integer, integer);

CREATE OR REPLACE PROCEDURE public."TM_CreateSecGroup"(
	IN secgcode character varying,
	IN secgname character varying,
	IN filter character varying,
	IN mode character varying,
	IN ragencies integer,
	IN listlevel integer,
	OUT groupid integer)
LANGUAGE 'plpgsql'
AS $BODY$
declare
	grid integer;
begin
	select max("ID") + 1
	into GroupID
	from public."SecGroups";
	
	if GroupID is null then
		GroupID = 1;
	end if;
	
	if ListLevel is null then
		ListLevel = 1;
	end if;
	
	insert into public."SecGroups" ("ID", "SecGCode", "SecGName", "disabled", "Filter", "ListLevel", "mode", "RAgencies")
	values (GroupID, SecGCode, SecGName, 'n', Filter, ListLevel, Mode, RAgencies);
end
$BODY$;

DROP PROCEDURE IF EXISTS public."TM_CreateSecGroupEx"(character varying, character varying, character varying, character varying, character varying, integer, integer);

CREATE OR REPLACE PROCEDURE public."TM_CreateSecGroupEx"(
	IN secgcode character varying,
	IN secgname character varying,
	IN disabled character varying,
	IN filter character varying,
	IN mode character varying,
	IN ragencies integer,
	IN listlevel integer,
	OUT groupid integer)
LANGUAGE 'plpgsql'
AS $BODY$
begin
	select max("ID") + 1
	into GroupID
	from public."SecGroups";
	
	if GroupID is null then
		GroupID = 1;
	end if;
	
	if ListLevel is null then
		ListLevel = 1;
	end if;
	
	insert into public."SecGroups" ("ID", "SecGCode", "SecGName", "disabled", "Filter", "ListLevel", "mode", "RAgencies")
	values (GroupID, SecGCode, SecGName, disabled, Filter, ListLevel, Mode, RAgencies);
end
$BODY$;

DROP PROCEDURE IF EXISTS public."TM_CreateStopOrder"(integer, integer, integer, integer, character, character, character varying, character varying, character varying, character varying, integer, integer, character varying, character varying, double precision, double precision, double precision, double precision, double precision, double precision, double precision, double precision, double precision, double precision, double precision, character, character, integer, double precision, integer, integer);

CREATE OR REPLACE PROCEDURE public."TM_CreateStopOrder"(
	IN grouporderid integer,
	IN stopstyle integer,
	IN tradedate integer,
	IN tradetime integer,
	IN buy character,
	IN ml character,
	IN seccode character varying,
	IN classcode character varying,
	IN aseccode character varying,
	IN aclasscode character varying,
	IN stopcond integer,
	IN transflags integer,
	IN clientcode character varying,
	IN trdacc character varying,
	IN stopprice double precision,
	IN price double precision,
	IN linkedprice double precision,
	IN _offset double precision,
	IN spread double precision,
	IN qty double precision,
	IN balance double precision,
	IN stopordernum double precision,
	IN linkedordernum double precision,
	IN baseordernum double precision,
	IN coordernum double precision,
	IN status character,
	IN result character,
	IN expire integer,
	IN stopprice2 double precision,
	IN activefromtime integer,
	IN activetotime integer,
	OUT stoporderid integer)
LANGUAGE 'plpgsql'
AS $BODY$
begin
	select max("OrderID") + 1
	into StopOrderID
	from public."StopOrders";
	
	if StopOrderID is null then
		StopOrderID = 1;
	end if;
    insert into public."StopOrders" ("OrderID", "GroupOrderID", "StopStyle", "TradeDate", "TradeTime", "Buy", "ML", "SecCode", "ClassCode", "ASecCode", "AClassCode",
        "StopCond", "TransFlags", "ClientCode", "TrdAcc", "StopPrice", "Price", "LinkedPrice", "Offset", "Spread", "Qty", "Balance",
        "StopOrderNum", "LinkedOrderNum", "BaseOrderNum", "CoOrderNum", "Status", "Result", "Expire", "StopPrice2", "ActiveFromTime", "ActiveToTime")
    values (StopOrderID, GroupOrderID, StopStyle, TradeDate, TradeTime, Buy, ML, SecCode, ClassCode, ASecCode, AClassCode,
    	StopCond, TransFlags, ClientCode, TrdAcc, StopPrice, Price, LinkedPrice, _Offset, Spread, Qty, Balance,
        StopOrderNum, LinkedOrderNum, BaseOrderNum, CoOrderNum, Status, Result, Expire, StopPrice2, ActiveFromTime, ActiveToTime);
end
$BODY$;

DROP PROCEDURE IF EXISTS public."TM_DeleteClientFromGroup"(character varying, character varying);

CREATE OR REPLACE PROCEDURE public."TM_DeleteClientFromGroup"(
	IN groupcode character varying,
	IN clientcode character varying,
	OUT success integer)
LANGUAGE 'plpgsql'
AS $BODY$
declare
	grid integer;
begin
	select "GroupID"
	into grid
	from public."GroupDesc"
	where "GroupCode" = GroupCode;
	
	if grid is not null then
		delete from public."Clients"
		where "GroupID" = grid and "ClientCode" = ClientCode;
		Success = 1;
	end if;
end
$BODY$;

DROP PROCEDURE IF EXISTS public."TM_DeleteGroup"(character varying);

CREATE OR REPLACE PROCEDURE public."TM_DeleteGroup"(
	IN groupcode character varying,
	OUT success integer)
LANGUAGE 'plpgsql'
AS $BODY$
declare GroupId integer;
begin
	Success = 0;
	
	select "GroupID"
	into GroupId
	from public."GroupDesc"
	where "GroupCode" = GroupCode;
	
	if GroupId is not null then
		Success = 1;
		update public."GroupDesc"
		set "disabled" = 'y'
		where "GroupID" = GroupId;
	end if;
end
$BODY$;

DROP PROCEDURE IF EXISTS public."TM_GetCounter"(character varying);

CREATE OR REPLACE PROCEDURE public."TM_GetCounter"(
	IN tablename character varying,
	OUT counter integer)
LANGUAGE 'plpgsql'
AS $BODY$
begin
	select "Counter"
	into Counter
	from public."SystemCounters"
	where "TableName" = TableName;
	
	if Counter is null then
		Counter = 1;
		insert into public."SystemCounters" ("TableName", "Counter")
		values (TableName, Counter);
	else
		Counter = Counter + 1;
		update public."SystemCounters"
		set "Counter" = Counter
		where "TableName" = TableName;
	end if;
	
end
$BODY$;

DROP PROCEDURE IF EXISTS public."TM_SetNewImportEvent"(character varying, integer, character varying);

CREATE OR REPLACE PROCEDURE public."TM_SetNewImportEvent"(
	IN importtype character varying,
	IN tmuserid integer,
	IN tmsessionid character varying,
	OUT tmcounter integer)
LANGUAGE 'plpgsql'
AS $BODY$
declare
	ImportID int;
begin
    select max("ID")+1 into ImportID from public."ImportLog";
    if ImportID is NULL then
    	ImportID=1;
	end if;
    call public."TM_GetCounter"('IMPORTLOG', TMCounter);
    insert into public."ImportLog" ("ID", "ImportType",
	                               "TMUserID", "TMSessionID", "TMCounter")
    values (ImportID, ImportType, TMUserID, TMSessionID, TMCounter);
end
$BODY$;

-- Устанавливаем права:


GRANT SELECT ON public."RatingAgencies" TO tm_admin;
GRANT INSERT ON public."RatingAgencies" TO tm_admin;
GRANT DELETE ON public."RatingAgencies" TO tm_admin;
GRANT UPDATE ON public."RatingAgencies" TO tm_admin;
GRANT REFERENCES ON public."RatingAgencies" TO tm_admin;
GRANT SELECT ON public."RatingAgencies" TO tm_user;
GRANT SELECT ("AgencyID") ON public."RatingAgencies" TO tm_admin;
GRANT UPDATE ("AgencyID") ON public."RatingAgencies" TO tm_admin;
GRANT REFERENCES  ("AgencyID") ON public."RatingAgencies" TO tm_admin;
GRANT SELECT ("AgencyID") ON public."RatingAgencies" TO tm_user;
GRANT SELECT ("AgencyCode") ON public."RatingAgencies" TO tm_admin;
GRANT UPDATE ("AgencyCode") ON public."RatingAgencies" TO tm_admin;
GRANT REFERENCES  ("AgencyCode") ON public."RatingAgencies" TO tm_admin;
GRANT SELECT ("AgencyCode") ON public."RatingAgencies" TO tm_user;
GRANT SELECT ("AgencyName") ON public."RatingAgencies" TO tm_admin;
GRANT UPDATE ("AgencyName") ON public."RatingAgencies" TO tm_admin;
GRANT REFERENCES ("AgencyName") ON public."RatingAgencies" TO tm_admin;
GRANT SELECT ("AgencyName") ON public."RatingAgencies" TO tm_user;
GRANT INSERT ON public."SecGrMemb" TO tm_admin;
GRANT DELETE ON public."SecGrMemb" TO tm_admin;
GRANT UPDATE ON public."SecGrMemb" TO tm_admin;
GRANT SELECT ON public."SecGrMemb" TO tm_admin;
GRANT REFERENCES ON public."SecGrMemb" TO tm_admin;
GRANT SELECT ON public."SecGrMemb" TO tm_user;
GRANT INSERT ON public."SecGrMemb" TO tm_user;
GRANT DELETE ON public."SecGrMemb" TO tm_user;
GRANT UPDATE ON public."SecGrMemb" TO tm_user;
GRANT SELECT ("GroupID") ON public."SecGrMemb" TO tm_admin;
GRANT UPDATE ("GroupID") ON public."SecGrMemb" TO tm_admin;
GRANT REFERENCES ("GroupID") ON public."SecGrMemb" TO tm_admin;
GRANT SELECT ("GroupID") ON public."SecGrMemb" TO tm_user;
GRANT UPDATE ("GroupID") ON public."SecGrMemb" TO tm_user;
GRANT SELECT ("MembType") ON public."SecGrMemb" TO tm_admin;
GRANT UPDATE ("MembType") ON public."SecGrMemb" TO tm_admin;
GRANT REFERENCES ("MembType") ON public."SecGrMemb" TO tm_admin;
GRANT SELECT ("MembType") ON public."SecGrMemb" TO tm_user;
GRANT UPDATE ("MembType") ON public."SecGrMemb" TO tm_user;
GRANT SELECT ("MembCode") ON public."SecGrMemb" TO tm_admin;
GRANT UPDATE ("MembCode") ON public."SecGrMemb" TO tm_admin;
GRANT REFERENCES ("MembCode") ON public."SecGrMemb" TO tm_admin;
GRANT SELECT ("MembCode") ON public."SecGrMemb" TO tm_user;
GRANT UPDATE ("MembCode") ON public."SecGrMemb" TO tm_user;
GRANT SELECT ("MembClass") ON public."SecGrMemb" TO tm_admin;
GRANT UPDATE ("MembClass") ON public."SecGrMemb" TO tm_admin;
GRANT REFERENCES ("MembClass") ON public."SecGrMemb" TO tm_admin;
GRANT SELECT ("MembClass") ON public."SecGrMemb" TO tm_user;
GRANT UPDATE ("MembClass") ON public."SecGrMemb" TO tm_user;
GRANT SELECT ("MembID") ON public."SecGrMemb" TO tm_admin;
GRANT UPDATE ("MembID") ON public."SecGrMemb" TO tm_admin;
GRANT REFERENCES ("MembID") ON public."SecGrMemb" TO tm_admin;
GRANT SELECT ("MembID") ON public."SecGrMemb" TO tm_user;
GRANT UPDATE ("MembID") ON public."SecGrMemb" TO tm_user;
GRANT SELECT ON public."RatingScales" TO tm_admin;
GRANT INSERT ON public."RatingScales" TO tm_admin;
GRANT DELETE ON public."RatingScales" TO tm_admin;
GRANT UPDATE ON public."RatingScales" TO tm_admin;
GRANT REFERENCES ON public."RatingScales" TO tm_admin;
GRANT SELECT ON public."RatingScales" TO tm_user;
GRANT SELECT ("RatingID") ON public."RatingScales" TO tm_admin;
GRANT UPDATE ("RatingID") ON public."RatingScales" TO tm_admin;
GRANT SELECT ("RatingID") ON public."RatingScales" TO tm_user;
GRANT SELECT ("AgencyID") ON public."RatingScales" TO tm_admin;
GRANT UPDATE ("AgencyID") ON public."RatingScales" TO tm_admin;
GRANT SELECT ("AgencyID") ON public."RatingScales" TO tm_user;
GRANT SELECT ("National") ON public."RatingScales" TO tm_admin;
GRANT UPDATE ("National") ON public."RatingScales" TO tm_admin;
GRANT SELECT ("National") ON public."RatingScales" TO tm_user;
GRANT SELECT ("Rating")   ON public."RatingScales" TO tm_admin;
GRANT UPDATE ("Rating")   ON public."RatingScales" TO tm_admin;
GRANT SELECT ("Rating")   ON public."RatingScales" TO tm_user;
GRANT SELECT ("Position") ON public."RatingScales" TO tm_admin;
GRANT UPDATE ("Position") ON public."RatingScales" TO tm_admin;
GRANT SELECT ("Position") ON public."RatingScales" TO tm_user;
GRANT REFERENCES ("RatingID") ON public."RatingScales" TO tm_admin;
GRANT REFERENCES ("AgencyID") ON public."RatingScales" TO tm_admin;
GRANT REFERENCES ("National") ON public."RatingScales" TO tm_admin;
GRANT REFERENCES ("Rating")   ON public."RatingScales" TO tm_admin;
GRANT REFERENCES ("Position") ON public."RatingScales" TO tm_admin;
GRANT SELECT ON public."SecMinQt" TO tm_admin;
GRANT INSERT ON public."SecMinQt" TO tm_admin;
GRANT DELETE ON public."SecMinQt" TO tm_admin;
GRANT UPDATE ON public."SecMinQt" TO tm_admin;
GRANT SELECT ON public."SecMinQt" TO tm_user;
GRANT INSERT ON public."SecMinQt" TO tm_user;
GRANT DELETE ON public."SecMinQt" TO tm_user;
GRANT UPDATE ON public."SecMinQt" TO tm_user;
GRANT SELECT ("ClassCode") ON public."SecMinQt" TO tm_admin;
GRANT UPDATE ("ClassCode") ON public."SecMinQt" TO tm_admin;
GRANT SELECT ("ClassCode") ON public."SecMinQt" TO tm_user;
GRANT UPDATE ("ClassCode") ON public."SecMinQt" TO tm_user;
GRANT SELECT ("SecCode") ON public."SecMinQt" TO tm_admin;
GRANT UPDATE ("SecCode") ON public."SecMinQt" TO tm_admin;
GRANT SELECT ("SecCode") ON public."SecMinQt" TO tm_user;
GRANT UPDATE ("SecCode") ON public."SecMinQt" TO tm_user;
GRANT SELECT ("MinQty") ON public."SecMinQt" TO tm_admin;
GRANT UPDATE ("MinQty") ON public."SecMinQt" TO tm_admin;
GRANT SELECT ("MinQty") ON public."SecMinQt" TO tm_user;
GRANT UPDATE ("MinQty") ON public."SecMinQt" TO tm_user;
GRANT REFERENCES ON public."SecMinQt" TO tm_admin;
GRANT REFERENCES ("ClassCode") ON public."SecMinQt" TO tm_admin;
GRANT REFERENCES ("SecCode") ON public."SecMinQt" TO tm_admin;
GRANT REFERENCES ("MinQty") ON public."SecMinQt" TO tm_admin;
GRANT SELECT ON public."SystemCounters" TO tm_admin;
GRANT INSERT ON public."SystemCounters" TO tm_admin;
GRANT DELETE ON public."SystemCounters" TO tm_admin;
GRANT UPDATE ON public."SystemCounters" TO tm_admin;
GRANT SELECT ON public."SystemCounters" TO tm_user;
GRANT INSERT ON public."SystemCounters" TO tm_user;
GRANT DELETE ON public."SystemCounters" TO tm_user;
GRANT UPDATE ON public."SystemCounters" TO tm_user;
GRANT SELECT ("TableName") ON public."SystemCounters" TO tm_admin;
GRANT UPDATE ("TableName") ON public."SystemCounters" TO tm_admin;
GRANT SELECT ("TableName") ON public."SystemCounters" TO tm_user;
GRANT UPDATE ("TableName") ON public."SystemCounters" TO tm_user;
GRANT SELECT ("Counter")   ON public."SystemCounters" TO tm_admin;
GRANT UPDATE ("Counter")   ON public."SystemCounters" TO tm_admin;
GRANT SELECT ("Counter")   ON public."SystemCounters" TO tm_user;
GRANT UPDATE ("Counter")   ON public."SystemCounters" TO tm_user;
GRANT REFERENCES ON public."SystemCounters" TO tm_admin;
GRANT REFERENCES ("TableName") ON public."SystemCounters" TO tm_admin;
GRANT REFERENCES ("Counter")   ON public."SystemCounters" TO tm_admin;
GRANT SELECT ON public."SystemData" TO tm_admin;
GRANT INSERT ON public."SystemData" TO tm_admin;
GRANT DELETE ON public."SystemData" TO tm_admin;
GRANT UPDATE ON public."SystemData" TO tm_admin;
GRANT SELECT ON public."SystemData" TO tm_user;
GRANT SELECT ("KeyName")  ON public."SystemData" TO tm_admin;
GRANT UPDATE ("KeyName")  ON public."SystemData" TO tm_admin;
GRANT SELECT ("KeyName")  ON public."SystemData" TO tm_user;
GRANT SELECT ("KeyValue") ON public."SystemData" TO tm_admin;
GRANT UPDATE ("KeyValue") ON public."SystemData" TO tm_admin;
GRANT SELECT ("KeyValue") ON public."SystemData" TO tm_user;
GRANT REFERENCES ON public."SystemData" TO tm_admin;
GRANT REFERENCES ("KeyName")  ON public."SystemData" TO tm_admin;
GRANT REFERENCES ("KeyValue") ON public."SystemData" TO tm_admin;
GRANT SELECT ON public."PoolOrders" TO tm_admin;
GRANT INSERT ON public."PoolOrders" TO tm_admin;
GRANT DELETE ON public."PoolOrders" TO tm_admin;
GRANT UPDATE ON public."PoolOrders" TO tm_admin;
GRANT SELECT ON public."PoolOrders" TO tm_user;
GRANT INSERT ON public."PoolOrders" TO tm_user;
GRANT DELETE ON public."PoolOrders" TO tm_user;
GRANT UPDATE ON public."PoolOrders" TO tm_user;
GRANT SELECT ("PoolID") ON public."PoolOrders" TO tm_admin;
GRANT UPDATE ("PoolID") ON public."PoolOrders" TO tm_admin;
GRANT SELECT ("PoolID") ON public."PoolOrders" TO tm_user;
GRANT UPDATE ("PoolID") ON public."PoolOrders" TO tm_user;
GRANT SELECT ("BrokerPoolID") ON public."PoolOrders" TO tm_admin;
GRANT UPDATE ("BrokerPoolID") ON public."PoolOrders" TO tm_admin;
GRANT SELECT ("BrokerPoolID") ON public."PoolOrders" TO tm_user;
GRANT UPDATE ("BrokerPoolID") ON public."PoolOrders" TO tm_user;
GRANT SELECT ("PoolName") ON public."PoolOrders" TO tm_admin;
GRANT UPDATE ("PoolName") ON public."PoolOrders" TO tm_admin;
GRANT SELECT ("PoolName") ON public."PoolOrders" TO tm_user;
GRANT UPDATE ("PoolName") ON public."PoolOrders" TO tm_user;
GRANT SELECT ("LoadDate") ON public."PoolOrders" TO tm_admin;
GRANT UPDATE ("LoadDate") ON public."PoolOrders" TO tm_admin;
GRANT SELECT ("LoadDate") ON public."PoolOrders" TO tm_user;
GRANT UPDATE ("LoadDate") ON public."PoolOrders" TO tm_user;
GRANT SELECT ("LoadTime") ON public."PoolOrders" TO tm_admin;
GRANT UPDATE ("LoadTime") ON public."PoolOrders" TO tm_admin;
GRANT SELECT ("LoadTime") ON public."PoolOrders" TO tm_user;
GRANT UPDATE ("LoadTime") ON public."PoolOrders" TO tm_user;
GRANT SELECT ("ExpireDate") ON public."PoolOrders" TO tm_admin;
GRANT UPDATE ("ExpireDate") ON public."PoolOrders" TO tm_admin;
GRANT SELECT ("ExpireDate") ON public."PoolOrders" TO tm_user;
GRANT UPDATE ("ExpireDate") ON public."PoolOrders" TO tm_user;
GRANT SELECT ("Source") ON public."PoolOrders" TO tm_admin;
GRANT UPDATE ("Source") ON public."PoolOrders" TO tm_admin;
GRANT SELECT ("Source") ON public."PoolOrders" TO tm_user;
GRANT UPDATE ("Source") ON public."PoolOrders" TO tm_user;
GRANT SELECT ("Status") ON public."PoolOrders" TO tm_admin;
GRANT UPDATE ("Status") ON public."PoolOrders" TO tm_admin;
GRANT SELECT ("Status") ON public."PoolOrders" TO tm_user;
GRANT UPDATE ("Status") ON public."PoolOrders" TO tm_user;
GRANT SELECT ("KillTime") ON public."PoolOrders" TO tm_admin;
GRANT UPDATE ("KillTime") ON public."PoolOrders" TO tm_admin;
GRANT SELECT ("KillTime") ON public."PoolOrders" TO tm_user;
GRANT UPDATE ("KillTime") ON public."PoolOrders" TO tm_user;
GRANT SELECT ("TotalQty") ON public."PoolOrders" TO tm_admin;
GRANT UPDATE ("TotalQty") ON public."PoolOrders" TO tm_admin;
GRANT SELECT ("TotalQty") ON public."PoolOrders" TO tm_user;
GRANT UPDATE ("TotalQty") ON public."PoolOrders" TO tm_user;
GRANT SELECT ("SendQty") ON public."PoolOrders" TO tm_admin;
GRANT UPDATE ("SendQty") ON public."PoolOrders" TO tm_admin;
GRANT SELECT ("SendQty") ON public."PoolOrders" TO tm_user;
GRANT UPDATE ("SendQty") ON public."PoolOrders" TO tm_user;
GRANT SELECT ("ExecQty") ON public."PoolOrders" TO tm_admin;
GRANT UPDATE ("ExecQty") ON public."PoolOrders" TO tm_admin;
GRANT SELECT ("ExecQty") ON public."PoolOrders" TO tm_user;
GRANT UPDATE ("ExecQty") ON public."PoolOrders" TO tm_user;
GRANT SELECT ("RejectQty") ON public."PoolOrders" TO tm_admin;
GRANT UPDATE ("RejectQty") ON public."PoolOrders" TO tm_admin;
GRANT SELECT ("RejectQty") ON public."PoolOrders" TO tm_user;
GRANT UPDATE ("RejectQty") ON public."PoolOrders" TO tm_user;
GRANT SELECT ("KilledQty") ON public."PoolOrders" TO tm_admin;
GRANT UPDATE ("KilledQty") ON public."PoolOrders" TO tm_admin;
GRANT SELECT ("KilledQty") ON public."PoolOrders" TO tm_user;
GRANT UPDATE ("KilledQty") ON public."PoolOrders" TO tm_user;
GRANT SELECT ("ActiveQty") ON public."PoolOrders" TO tm_admin;
GRANT UPDATE ("ActiveQty") ON public."PoolOrders" TO tm_admin;
GRANT SELECT ("ActiveQty") ON public."PoolOrders" TO tm_user;
GRANT UPDATE ("ActiveQty") ON public."PoolOrders" TO tm_user;
GRANT REFERENCES ON public."PoolOrders" TO tm_admin;
GRANT REFERENCES ("PoolID") ON public."PoolOrders" TO tm_admin;
GRANT REFERENCES ("BrokerPoolID") ON public."PoolOrders" TO tm_admin;
GRANT REFERENCES ("PoolName") ON public."PoolOrders" TO tm_admin;
GRANT REFERENCES ("LoadDate") ON public."PoolOrders" TO tm_admin;
GRANT REFERENCES ("LoadTime") ON public."PoolOrders" TO tm_admin;
GRANT REFERENCES ("ExpireDate") ON public."PoolOrders" TO tm_admin;
GRANT REFERENCES ("Source") ON public."PoolOrders" TO tm_admin;
GRANT REFERENCES ("Status") ON public."PoolOrders" TO tm_admin;
GRANT REFERENCES ("KillTime") ON public."PoolOrders" TO tm_admin;
GRANT REFERENCES ("TotalQty") ON public."PoolOrders" TO tm_admin;
GRANT REFERENCES ("SendQty") ON public."PoolOrders" TO tm_admin;
GRANT REFERENCES ("ExecQty") ON public."PoolOrders" TO tm_admin;
GRANT REFERENCES ("RejectQty") ON public."PoolOrders" TO tm_admin;
GRANT REFERENCES ("KilledQty") ON public."PoolOrders" TO tm_admin;
GRANT REFERENCES ("ActiveQty") ON public."PoolOrders" TO tm_admin;
GRANT SELECT ON public."Clients" TO tm_admin;
GRANT INSERT ON public."Clients" TO tm_admin;
GRANT DELETE ON public."Clients" TO tm_admin;
GRANT UPDATE ON public."Clients" TO tm_admin;
GRANT SELECT ON public."Clients" TO tm_user;
GRANT SELECT ("ClientCode") ON public."Clients" TO tm_admin;
GRANT UPDATE ("ClientCode") ON public."Clients" TO tm_admin;
GRANT SELECT ("ClientCode") ON public."Clients" TO tm_user;
GRANT SELECT ("GroupID") ON public."Clients" TO tm_admin;
GRANT UPDATE ("GroupID") ON public."Clients" TO tm_admin;
GRANT SELECT ("GroupID") ON public."Clients" TO tm_user;
GRANT SELECT ("Num") ON public."Clients" TO tm_admin;
GRANT UPDATE ("Num") ON public."Clients" TO tm_admin;
GRANT SELECT ("Num") ON public."Clients" TO tm_user;
GRANT SELECT ("Weight") ON public."Clients" TO tm_admin;
GRANT UPDATE ("Weight") ON public."Clients" TO tm_admin;
GRANT SELECT ("Weight") ON public."Clients" TO tm_user;
GRANT SELECT ("Comment") ON public."Clients" TO tm_user;
GRANT SELECT ("Comment") ON public."Clients" TO tm_admin;
GRANT UPDATE ("Comment") ON public."Clients" TO tm_admin;
GRANT REFERENCES ("GroupID") ON public."Clients" TO tm_admin;
GRANT REFERENCES ON public."Clients" TO tm_admin;
GRANT REFERENCES ("ClientCode") ON public."Clients" TO tm_admin;
GRANT REFERENCES ("Num") ON public."Clients" TO tm_admin;
GRANT REFERENCES ("Weight") ON public."Clients" TO tm_admin;
GRANT REFERENCES ("Comment") ON public."Clients" TO tm_admin;
GRANT SELECT ON public."Orders" TO tm_admin;
GRANT INSERT ON public."Orders" TO tm_admin;
GRANT DELETE ON public."Orders" TO tm_admin;
GRANT UPDATE ON public."Orders" TO tm_admin;
GRANT SELECT ON public."Orders" TO tm_user;
GRANT INSERT ON public."Orders" TO tm_user;
GRANT DELETE ON public."Orders" TO tm_user;
GRANT UPDATE ON public."Orders" TO tm_user;
GRANT SELECT ("OrderID") ON public."Orders" TO tm_admin;
GRANT UPDATE ("OrderID") ON public."Orders" TO tm_admin;
GRANT SELECT ("OrderID") ON public."Orders" TO tm_user;
GRANT UPDATE ("OrderID") ON public."Orders" TO tm_user;
GRANT SELECT ("GroupOrderID") ON public."Orders" TO tm_admin;
GRANT UPDATE ("GroupOrderID") ON public."Orders" TO tm_admin;
GRANT SELECT ("GroupOrderID") ON public."Orders" TO tm_user;
GRANT UPDATE ("GroupOrderID") ON public."Orders" TO tm_user;
GRANT SELECT ("Buy") ON public."Orders" TO tm_admin;
GRANT UPDATE ("Buy") ON public."Orders" TO tm_admin;
GRANT SELECT ("Buy") ON public."Orders" TO tm_user;
GRANT UPDATE ("Buy") ON public."Orders" TO tm_user;
GRANT SELECT ("ML") ON public."Orders" TO tm_admin;
GRANT UPDATE ("ML") ON public."Orders" TO tm_admin;
GRANT SELECT ("ML") ON public."Orders" TO tm_user;
GRANT UPDATE ("ML") ON public."Orders" TO tm_user;
GRANT SELECT ("TradeDate") ON public."Orders" TO tm_admin;
GRANT UPDATE ("TradeDate") ON public."Orders" TO tm_admin;
GRANT SELECT ("TradeDate") ON public."Orders" TO tm_user;
GRANT UPDATE ("TradeDate") ON public."Orders" TO tm_user;
GRANT SELECT ("TradeTime") ON public."Orders" TO tm_admin;
GRANT UPDATE ("TradeTime") ON public."Orders" TO tm_admin;
GRANT SELECT ("TradeTime") ON public."Orders" TO tm_user;
GRANT UPDATE ("TradeTime") ON public."Orders" TO tm_user;
GRANT SELECT ("SecCode") ON public."Orders" TO tm_admin;
GRANT UPDATE ("SecCode") ON public."Orders" TO tm_admin;
GRANT SELECT ("SecCode") ON public."Orders" TO tm_user;
GRANT UPDATE ("SecCode") ON public."Orders" TO tm_user;
GRANT SELECT ("ClassCode") ON public."Orders" TO tm_admin;
GRANT UPDATE ("ClassCode") ON public."Orders" TO tm_admin;
GRANT SELECT ("ClassCode") ON public."Orders" TO tm_user;
GRANT UPDATE ("ClassCode") ON public."Orders" TO tm_user;
GRANT SELECT ("ClientCode") ON public."Orders" TO tm_admin;
GRANT UPDATE ("ClientCode") ON public."Orders" TO tm_admin;
GRANT SELECT ("ClientCode") ON public."Orders" TO tm_user;
GRANT UPDATE ("ClientCode") ON public."Orders" TO tm_user;
GRANT SELECT ("TrdAcc") ON public."Orders" TO tm_admin;
GRANT UPDATE ("TrdAcc") ON public."Orders" TO tm_admin;
GRANT SELECT ("TrdAcc") ON public."Orders" TO tm_user;
GRANT UPDATE ("TrdAcc") ON public."Orders" TO tm_user;
GRANT SELECT ("Price") ON public."Orders" TO tm_admin;
GRANT UPDATE ("Price") ON public."Orders" TO tm_admin;
GRANT SELECT ("Price") ON public."Orders" TO tm_user;
GRANT UPDATE ("Price") ON public."Orders" TO tm_user;
GRANT SELECT ("Qty") ON public."Orders" TO tm_admin;
GRANT UPDATE ("Qty") ON public."Orders" TO tm_admin;
GRANT SELECT ("Qty") ON public."Orders" TO tm_user;
GRANT UPDATE ("Qty") ON public."Orders" TO tm_user;
GRANT SELECT ("BalanceQty") ON public."Orders" TO tm_admin;
GRANT UPDATE ("BalanceQty") ON public."Orders" TO tm_admin;
GRANT SELECT ("BalanceQty") ON public."Orders" TO tm_user;
GRANT UPDATE ("BalanceQty") ON public."Orders" TO tm_user;
GRANT SELECT ("OrderNum") ON public."Orders" TO tm_admin;
GRANT UPDATE ("OrderNum") ON public."Orders" TO tm_admin;
GRANT SELECT ("OrderNum") ON public."Orders" TO tm_user;
GRANT UPDATE ("OrderNum") ON public."Orders" TO tm_user;
GRANT SELECT ("Status") ON public."Orders" TO tm_admin;
GRANT UPDATE ("Status") ON public."Orders" TO tm_admin;
GRANT SELECT ("Status") ON public."Orders" TO tm_user;
GRANT UPDATE ("Status") ON public."Orders" TO tm_user;
GRANT SELECT ("Result") ON public."Orders" TO tm_admin;
GRANT UPDATE ("Result") ON public."Orders" TO tm_admin;
GRANT SELECT ("Result") ON public."Orders" TO tm_user;
GRANT UPDATE ("Result") ON public."Orders" TO tm_user;
GRANT SELECT ("OrderInPoolID") ON public."Orders" TO tm_admin;
GRANT UPDATE ("OrderInPoolID") ON public."Orders" TO tm_admin;
GRANT SELECT ("OrderInPoolID") ON public."Orders" TO tm_user;
GRANT UPDATE ("OrderInPoolID") ON public."Orders" TO tm_user;
GRANT SELECT ("Value") ON public."Orders" TO tm_admin;
GRANT UPDATE ("Value") ON public."Orders" TO tm_admin;
GRANT SELECT ("Value") ON public."Orders" TO tm_user;
GRANT UPDATE ("Value") ON public."Orders" TO tm_user;
GRANT SELECT ("VisibleQty") ON public."Orders" TO tm_admin;
GRANT UPDATE ("VisibleQty") ON public."Orders" TO tm_admin;
GRANT SELECT ("VisibleQty") ON public."Orders" TO tm_user;
GRANT UPDATE ("VisibleQty") ON public."Orders" TO tm_user;
GRANT SELECT ("TMUserID") ON public."Orders" TO tm_admin;
GRANT UPDATE ("TMUserID") ON public."Orders" TO tm_admin;
GRANT SELECT ("TMUserID") ON public."Orders" TO tm_user;
GRANT UPDATE ("TMUserID") ON public."Orders" TO tm_user;
GRANT SELECT ("TMSessionID") ON public."Orders" TO tm_admin;
GRANT UPDATE ("TMSessionID") ON public."Orders" TO tm_admin;
GRANT SELECT ("TMSessionID") ON public."Orders" TO tm_user;
GRANT UPDATE ("TMSessionID") ON public."Orders" TO tm_user;
GRANT SELECT ("TMCounter") ON public."Orders" TO tm_admin;
GRANT UPDATE ("TMCounter") ON public."Orders" TO tm_admin;
GRANT SELECT ("TMCounter") ON public."Orders" TO tm_user;
GRANT UPDATE ("TMCounter") ON public."Orders" TO tm_user;
GRANT REFERENCES ON public."Orders" TO tm_admin;
GRANT REFERENCES ("OrderID") ON public."Orders" TO tm_admin;
GRANT REFERENCES ("GroupOrderID") ON public."Orders" TO tm_admin;
GRANT REFERENCES ("Buy") ON public."Orders" TO tm_admin;
GRANT REFERENCES ("ML") ON public."Orders" TO tm_admin;
GRANT REFERENCES ("TradeDate") ON public."Orders" TO tm_admin;
GRANT REFERENCES ("TradeTime") ON public."Orders" TO tm_admin;
GRANT REFERENCES ("SecCode") ON public."Orders" TO tm_admin;
GRANT REFERENCES ("ClassCode") ON public."Orders" TO tm_admin;
GRANT REFERENCES ("ClientCode") ON public."Orders" TO tm_admin;
GRANT REFERENCES ("TrdAcc") ON public."Orders" TO tm_admin;
GRANT REFERENCES ("Price") ON public."Orders" TO tm_admin;
GRANT REFERENCES ("Qty") ON public."Orders" TO tm_admin;
GRANT REFERENCES ("BalanceQty") ON public."Orders" TO tm_admin;
GRANT REFERENCES ("OrderNum") ON public."Orders" TO tm_admin;
GRANT REFERENCES ("Status") ON public."Orders" TO tm_admin;
GRANT REFERENCES ("Result") ON public."Orders" TO tm_admin;
GRANT REFERENCES ("OrderInPoolID") ON public."Orders" TO tm_admin;
GRANT REFERENCES ("Value") ON public."Orders" TO tm_admin;
GRANT REFERENCES ("VisibleQty") ON public."Orders" TO tm_admin;
GRANT REFERENCES ("TMUserID") ON public."Orders" TO tm_admin;
GRANT REFERENCES ("TMSessionID") ON public."Orders" TO tm_admin;
GRANT REFERENCES ("TMCounter") ON public."Orders" TO tm_admin;
GRANT SELECT ON public."OrdersInPool" TO tm_admin;
GRANT INSERT ON public."OrdersInPool" TO tm_admin;
GRANT DELETE ON public."OrdersInPool" TO tm_admin;
GRANT UPDATE ON public."OrdersInPool" TO tm_admin;
GRANT SELECT ON public."OrdersInPool" TO tm_user;
GRANT INSERT ON public."OrdersInPool" TO tm_user;
GRANT DELETE ON public."OrdersInPool" TO tm_user;
GRANT UPDATE ON public."OrdersInPool" TO tm_user;
GRANT SELECT ("OrderID") ON public."OrdersInPool" TO tm_admin;
GRANT UPDATE ("OrderID") ON public."OrdersInPool" TO tm_admin;
GRANT SELECT ("OrderID") ON public."OrdersInPool" TO tm_user;
GRANT UPDATE ("OrderID") ON public."OrdersInPool" TO tm_user;
GRANT SELECT ("PoolID") ON public."OrdersInPool" TO tm_admin;
GRANT UPDATE ("PoolID") ON public."OrdersInPool" TO tm_admin;
GRANT SELECT ("PoolID") ON public."OrdersInPool" TO tm_user;
GRANT UPDATE ("PoolID") ON public."OrdersInPool" TO tm_user;
GRANT SELECT ("OrderType") ON public."OrdersInPool" TO tm_admin;
GRANT UPDATE ("OrderType") ON public."OrdersInPool" TO tm_admin;
GRANT SELECT ("OrderType") ON public."OrdersInPool" TO tm_user;
GRANT UPDATE ("OrderType") ON public."OrdersInPool" TO tm_user;
GRANT SELECT ("ClassCode") ON public."OrdersInPool" TO tm_admin;
GRANT UPDATE ("ClassCode") ON public."OrdersInPool" TO tm_admin;
GRANT SELECT ("ClassCode") ON public."OrdersInPool" TO tm_user;
GRANT UPDATE ("ClassCode") ON public."OrdersInPool" TO tm_user;
GRANT SELECT ("ClientCode") ON public."OrdersInPool" TO tm_admin;
GRANT UPDATE ("ClientCode") ON public."OrdersInPool" TO tm_admin;
GRANT SELECT ("ClientCode") ON public."OrdersInPool" TO tm_user;
GRANT UPDATE ("ClientCode") ON public."OrdersInPool" TO tm_user;
GRANT SELECT ("SecCode") ON public."OrdersInPool" TO tm_admin;
GRANT UPDATE ("SecCode") ON public."OrdersInPool" TO tm_admin;
GRANT SELECT ("SecCode") ON public."OrdersInPool" TO tm_user;
GRANT UPDATE ("SecCode") ON public."OrdersInPool" TO tm_user;
GRANT SELECT ("Price") ON public."OrdersInPool" TO tm_admin;
GRANT UPDATE ("Price") ON public."OrdersInPool" TO tm_admin;
GRANT SELECT ("Price") ON public."OrdersInPool" TO tm_user;
GRANT UPDATE ("Price") ON public."OrdersInPool" TO tm_user;
GRANT SELECT ("Qty") ON public."OrdersInPool" TO tm_admin;
GRANT UPDATE ("Qty") ON public."OrdersInPool" TO tm_admin;
GRANT SELECT ("Qty") ON public."OrdersInPool" TO tm_user;
GRANT UPDATE ("Qty") ON public."OrdersInPool" TO tm_user;
GRANT SELECT ("Buy") ON public."OrdersInPool" TO tm_admin;
GRANT UPDATE ("Buy") ON public."OrdersInPool" TO tm_admin;
GRANT SELECT ("Buy") ON public."OrdersInPool" TO tm_user;
GRANT UPDATE ("Buy") ON public."OrdersInPool" TO tm_user;
GRANT SELECT ("ML") ON public."OrdersInPool" TO tm_admin;
GRANT UPDATE ("ML") ON public."OrdersInPool" TO tm_admin;
GRANT SELECT ("ML") ON public."OrdersInPool" TO tm_user;
GRANT UPDATE ("ML") ON public."OrdersInPool" TO tm_user;
GRANT SELECT ("SendQty") ON public."OrdersInPool" TO tm_admin;
GRANT UPDATE ("SendQty") ON public."OrdersInPool" TO tm_admin;
GRANT SELECT ("SendQty") ON public."OrdersInPool" TO tm_user;
GRANT UPDATE ("SendQty") ON public."OrdersInPool" TO tm_user;
GRANT SELECT ("ExecQty") ON public."OrdersInPool" TO tm_admin;
GRANT UPDATE ("ExecQty") ON public."OrdersInPool" TO tm_admin;
GRANT SELECT ("ExecQty") ON public."OrdersInPool" TO tm_user;
GRANT UPDATE ("ExecQty") ON public."OrdersInPool" TO tm_user;
GRANT SELECT ("RejectQty") ON public."OrdersInPool" TO tm_admin;
GRANT UPDATE ("RejectQty") ON public."OrdersInPool" TO tm_admin;
GRANT SELECT ("RejectQty") ON public."OrdersInPool" TO tm_user;
GRANT UPDATE ("RejectQty") ON public."OrdersInPool" TO tm_user;
GRANT SELECT ("KillQty") ON public."OrdersInPool" TO tm_admin;
GRANT UPDATE ("KillQty") ON public."OrdersInPool" TO tm_admin;
GRANT SELECT ("KillQty") ON public."OrdersInPool" TO tm_user;
GRANT UPDATE ("KillQty") ON public."OrdersInPool" TO tm_user;
GRANT SELECT ("ActiveQty") ON public."OrdersInPool" TO tm_admin;
GRANT UPDATE ("ActiveQty") ON public."OrdersInPool" TO tm_admin;
GRANT SELECT ("ActiveQty") ON public."OrdersInPool" TO tm_user;
GRANT UPDATE ("ActiveQty") ON public."OrdersInPool" TO tm_user;
GRANT SELECT ("Comment") ON public."OrdersInPool" TO tm_admin;
GRANT UPDATE ("Comment") ON public."OrdersInPool" TO tm_admin;
GRANT SELECT ("Comment") ON public."OrdersInPool" TO tm_user;
GRANT UPDATE ("Comment") ON public."OrdersInPool" TO tm_user;
GRANT REFERENCES ON public."OrdersInPool" TO tm_admin;
GRANT REFERENCES ("OrderID") ON public."OrdersInPool" TO tm_admin;
GRANT REFERENCES ("PoolID") ON public."OrdersInPool" TO tm_admin;
GRANT REFERENCES ("OrderType") ON public."OrdersInPool" TO tm_admin;
GRANT REFERENCES ("ClientCode") ON public."OrdersInPool" TO tm_admin;
GRANT REFERENCES ("ClassCode") ON public."OrdersInPool" TO tm_admin;
GRANT REFERENCES ("SecCode") ON public."OrdersInPool" TO tm_admin;
GRANT REFERENCES ("Price") ON public."OrdersInPool" TO tm_admin;
GRANT REFERENCES ("Qty") ON public."OrdersInPool" TO tm_admin;
GRANT REFERENCES ("Buy") ON public."OrdersInPool" TO tm_admin;
GRANT REFERENCES ("ML") ON public."OrdersInPool" TO tm_admin;
GRANT REFERENCES ("SendQty") ON public."OrdersInPool" TO tm_admin;
GRANT REFERENCES ("ExecQty") ON public."OrdersInPool" TO tm_admin;
GRANT REFERENCES ("RejectQty") ON public."OrdersInPool" TO tm_admin;
GRANT REFERENCES ("KillQty") ON public."OrdersInPool" TO tm_admin;
GRANT REFERENCES ("ActiveQty") ON public."OrdersInPool" TO tm_admin;
GRANT REFERENCES ("Comment") ON public."OrdersInPool" TO tm_admin;
GRANT SELECT ON public."StopOrders" TO tm_admin;
GRANT INSERT ON public."StopOrders" TO tm_admin;
GRANT DELETE ON public."StopOrders" TO tm_admin;
GRANT UPDATE ON public."StopOrders" TO tm_admin;
GRANT SELECT ON public."StopOrders" TO tm_user;
GRANT INSERT ON public."StopOrders" TO tm_user;
GRANT DELETE ON public."StopOrders" TO tm_user;
GRANT UPDATE ON public."StopOrders" TO tm_user;
GRANT SELECT ("OrderID") ON public."StopOrders" TO tm_admin;
GRANT UPDATE ("OrderID") ON public."StopOrders" TO tm_admin;
GRANT SELECT ("OrderID") ON public."StopOrders" TO tm_user;
GRANT UPDATE ("OrderID") ON public."StopOrders" TO tm_user;
GRANT SELECT ("GroupOrderID") ON public."StopOrders" TO tm_admin;
GRANT UPDATE ("GroupOrderID") ON public."StopOrders" TO tm_admin;
GRANT SELECT ("GroupOrderID") ON public."StopOrders" TO tm_user;
GRANT UPDATE ("GroupOrderID") ON public."StopOrders" TO tm_user;
GRANT SELECT ("StopStyle") ON public."StopOrders" TO tm_admin;
GRANT UPDATE ("StopStyle") ON public."StopOrders" TO tm_admin;
GRANT SELECT ("StopStyle") ON public."StopOrders" TO tm_user;
GRANT UPDATE ("StopStyle") ON public."StopOrders" TO tm_user;
GRANT SELECT ("Buy") ON public."StopOrders" TO tm_admin;
GRANT UPDATE ("Buy") ON public."StopOrders" TO tm_admin;
GRANT SELECT ("Buy") ON public."StopOrders" TO tm_user;
GRANT UPDATE ("Buy") ON public."StopOrders" TO tm_user;
GRANT SELECT ("ML") ON public."StopOrders" TO tm_admin;
GRANT UPDATE ("ML") ON public."StopOrders" TO tm_admin;
GRANT SELECT ("ML") ON public."StopOrders" TO tm_user;
GRANT UPDATE ("ML") ON public."StopOrders" TO tm_user;
GRANT SELECT ("TradeDate") ON public."StopOrders" TO tm_admin;
GRANT UPDATE ("TradeDate") ON public."StopOrders" TO tm_admin;
GRANT SELECT ("TradeDate") ON public."StopOrders" TO tm_user;
GRANT UPDATE ("TradeDate") ON public."StopOrders" TO tm_user;
GRANT SELECT ("TradeTime") ON public."StopOrders" TO tm_admin;
GRANT UPDATE ("TradeTime") ON public."StopOrders" TO tm_admin;
GRANT SELECT ("TradeTime") ON public."StopOrders" TO tm_user;
GRANT UPDATE ("TradeTime") ON public."StopOrders" TO tm_user;
GRANT SELECT ("SecCode") ON public."StopOrders" TO tm_admin;
GRANT UPDATE ("SecCode") ON public."StopOrders" TO tm_admin;
GRANT SELECT ("SecCode") ON public."StopOrders" TO tm_user;
GRANT UPDATE ("SecCode") ON public."StopOrders" TO tm_user;
GRANT SELECT ("ClassCode") ON public."StopOrders" TO tm_admin;
GRANT UPDATE ("ClassCode") ON public."StopOrders" TO tm_admin;
GRANT SELECT ("ClassCode") ON public."StopOrders" TO tm_user;
GRANT UPDATE ("ClassCode") ON public."StopOrders" TO tm_user;
GRANT SELECT ("ASecCode") ON public."StopOrders" TO tm_admin;
GRANT UPDATE ("ASecCode") ON public."StopOrders" TO tm_admin;
GRANT SELECT ("ASecCode") ON public."StopOrders" TO tm_user;
GRANT UPDATE ("ASecCode") ON public."StopOrders" TO tm_user;
GRANT SELECT ("StopCond") ON public."StopOrders" TO tm_admin;
GRANT UPDATE ("StopCond") ON public."StopOrders" TO tm_admin;
GRANT SELECT ("StopCond") ON public."StopOrders" TO tm_user;
GRANT UPDATE ("StopCond") ON public."StopOrders" TO tm_user;
GRANT SELECT ("AClassCode") ON public."StopOrders" TO tm_admin;
GRANT UPDATE ("AClassCode") ON public."StopOrders" TO tm_admin;
GRANT SELECT ("AClassCode") ON public."StopOrders" TO tm_user;
GRANT UPDATE ("AClassCode") ON public."StopOrders" TO tm_user;
GRANT SELECT ("TransFlags") ON public."StopOrders" TO tm_admin;
GRANT UPDATE ("TransFlags") ON public."StopOrders" TO tm_admin;
GRANT SELECT ("TransFlags") ON public."StopOrders" TO tm_user;
GRANT UPDATE ("TransFlags") ON public."StopOrders" TO tm_user;
GRANT SELECT ("ClientCode") ON public."StopOrders" TO tm_admin;
GRANT UPDATE ("ClientCode") ON public."StopOrders" TO tm_admin;
GRANT SELECT ("ClientCode") ON public."StopOrders" TO tm_user;
GRANT UPDATE ("ClientCode") ON public."StopOrders" TO tm_user;
GRANT SELECT ("StopPrice") ON public."StopOrders" TO tm_admin;
GRANT UPDATE ("StopPrice") ON public."StopOrders" TO tm_admin;
GRANT SELECT ("StopPrice") ON public."StopOrders" TO tm_user;
GRANT UPDATE ("StopPrice") ON public."StopOrders" TO tm_user;
GRANT SELECT ("Price") ON public."StopOrders" TO tm_admin;
GRANT UPDATE ("Price") ON public."StopOrders" TO tm_admin;
GRANT SELECT ("Price") ON public."StopOrders" TO tm_user;
GRANT UPDATE ("Price") ON public."StopOrders" TO tm_user;
GRANT SELECT ("LinkedPrice") ON public."StopOrders" TO tm_admin;
GRANT UPDATE ("LinkedPrice") ON public."StopOrders" TO tm_admin;
GRANT SELECT ("LinkedPrice") ON public."StopOrders" TO tm_user;
GRANT UPDATE ("LinkedPrice") ON public."StopOrders" TO tm_user;
GRANT SELECT ("Offset") ON public."StopOrders" TO tm_admin;
GRANT UPDATE ("Offset") ON public."StopOrders" TO tm_admin;
GRANT SELECT ("Offset") ON public."StopOrders" TO tm_user;
GRANT UPDATE ("Offset") ON public."StopOrders" TO tm_user;
GRANT SELECT ("Spread") ON public."StopOrders" TO tm_admin;
GRANT UPDATE ("Spread") ON public."StopOrders" TO tm_admin;
GRANT SELECT ("Spread") ON public."StopOrders" TO tm_user;
GRANT UPDATE ("Spread") ON public."StopOrders" TO tm_user;
GRANT SELECT ("Qty") ON public."StopOrders" TO tm_admin;
GRANT UPDATE ("Qty") ON public."StopOrders" TO tm_admin;
GRANT SELECT ("Qty") ON public."StopOrders" TO tm_user;
GRANT UPDATE ("Qty") ON public."StopOrders" TO tm_user;
GRANT SELECT ("Balance") ON public."StopOrders" TO tm_admin;
GRANT UPDATE ("Balance") ON public."StopOrders" TO tm_admin;
GRANT SELECT ("Balance") ON public."StopOrders" TO tm_user;
GRANT UPDATE ("Balance") ON public."StopOrders" TO tm_user;
GRANT SELECT ("StopOrderNum") ON public."StopOrders" TO tm_admin;
GRANT UPDATE ("StopOrderNum") ON public."StopOrders" TO tm_admin;
GRANT SELECT ("StopOrderNum") ON public."StopOrders" TO tm_user;
GRANT UPDATE ("StopOrderNum") ON public."StopOrders" TO tm_user;
GRANT SELECT ("LinkedOrderNum") ON public."StopOrders" TO tm_admin;
GRANT UPDATE ("LinkedOrderNum") ON public."StopOrders" TO tm_admin;
GRANT SELECT ("LinkedOrderNum") ON public."StopOrders" TO tm_user;
GRANT UPDATE ("LinkedOrderNum") ON public."StopOrders" TO tm_user;
GRANT SELECT ("BaseOrderNum") ON public."StopOrders" TO tm_admin;
GRANT UPDATE ("BaseOrderNum") ON public."StopOrders" TO tm_admin;
GRANT SELECT ("BaseOrderNum") ON public."StopOrders" TO tm_user;
GRANT UPDATE ("BaseOrderNum") ON public."StopOrders" TO tm_user;
GRANT SELECT ("CoOrderNum") ON public."StopOrders" TO tm_admin;
GRANT UPDATE ("CoOrderNum") ON public."StopOrders" TO tm_admin;
GRANT SELECT ("CoOrderNum") ON public."StopOrders" TO tm_user;
GRANT UPDATE ("CoOrderNum") ON public."StopOrders" TO tm_user;
GRANT SELECT ("Status") ON public."StopOrders" TO tm_admin;
GRANT UPDATE ("Status") ON public."StopOrders" TO tm_admin;
GRANT SELECT ("Status") ON public."StopOrders" TO tm_user;
GRANT UPDATE ("Status") ON public."StopOrders" TO tm_user;
GRANT SELECT ("Result") ON public."StopOrders" TO tm_admin;
GRANT UPDATE ("Result") ON public."StopOrders" TO tm_admin;
GRANT SELECT ("Result") ON public."StopOrders" TO tm_user;
GRANT UPDATE ("Result") ON public."StopOrders" TO tm_user;
GRANT SELECT ("Expire") ON public."StopOrders" TO tm_admin;
GRANT UPDATE ("Expire") ON public."StopOrders" TO tm_admin;
GRANT SELECT ("Expire") ON public."StopOrders" TO tm_user;
GRANT UPDATE ("Expire") ON public."StopOrders" TO tm_user;
GRANT SELECT ("StopPrice2") ON public."StopOrders" TO tm_admin;
GRANT UPDATE ("StopPrice2") ON public."StopOrders" TO tm_admin;
GRANT SELECT ("StopPrice2") ON public."StopOrders" TO tm_user;
GRANT UPDATE ("StopPrice2") ON public."StopOrders" TO tm_user;
GRANT SELECT ("ActiveFromTime") ON public."StopOrders" TO tm_admin;
GRANT UPDATE ("ActiveFromTime") ON public."StopOrders" TO tm_admin;
GRANT SELECT ("ActiveFromTime") ON public."StopOrders" TO tm_user;
GRANT UPDATE ("ActiveFromTime") ON public."StopOrders" TO tm_user;
GRANT SELECT ("ActiveToTime") ON public."StopOrders" TO tm_admin;
GRANT UPDATE ("ActiveToTime") ON public."StopOrders" TO tm_admin;
GRANT SELECT ("ActiveToTime") ON public."StopOrders" TO tm_user;
GRANT UPDATE ("ActiveToTime") ON public."StopOrders" TO tm_user;
GRANT SELECT ("TrdAcc") ON public."StopOrders" TO tm_admin;
GRANT UPDATE ("TrdAcc") ON public."StopOrders" TO tm_admin;
GRANT SELECT ("TrdAcc") ON public."StopOrders" TO tm_user;
GRANT UPDATE ("TrdAcc") ON public."StopOrders" TO tm_user;
GRANT REFERENCES ON public."StopOrders" TO tm_admin;
GRANT REFERENCES ("OrderID") ON public."StopOrders" TO tm_admin;
GRANT REFERENCES ("GroupOrderID") ON public."StopOrders" TO tm_admin;
GRANT REFERENCES ("StopStyle") ON public."StopOrders" TO tm_admin;
GRANT REFERENCES ("Buy") ON public."StopOrders" TO tm_admin;
GRANT REFERENCES ("ML") ON public."StopOrders" TO tm_admin;
GRANT REFERENCES ("TradeDate") ON public."StopOrders" TO tm_admin;
GRANT REFERENCES ("TradeTime") ON public."StopOrders" TO tm_admin;
GRANT REFERENCES ("SecCode") ON public."StopOrders" TO tm_admin;
GRANT REFERENCES ("ClassCode") ON public."StopOrders" TO tm_admin;
GRANT REFERENCES ("ASecCode") ON public."StopOrders" TO tm_admin;
GRANT REFERENCES ("AClassCode") ON public."StopOrders" TO tm_admin;
GRANT REFERENCES ("StopCond") ON public."StopOrders" TO tm_admin;
GRANT REFERENCES ("TransFlags") ON public."StopOrders" TO tm_admin;
GRANT REFERENCES ("ClientCode") ON public."StopOrders" TO tm_admin;
GRANT REFERENCES ("StopPrice") ON public."StopOrders" TO tm_admin;
GRANT REFERENCES ("Price") ON public."StopOrders" TO tm_admin;
GRANT REFERENCES ("LinkedPrice") ON public."StopOrders" TO tm_admin;
GRANT REFERENCES ("Offset") ON public."StopOrders" TO tm_admin;
GRANT REFERENCES ("Spread") ON public."StopOrders" TO tm_admin;
GRANT REFERENCES ("Qty") ON public."StopOrders" TO tm_admin;
GRANT REFERENCES ("Balance") ON public."StopOrders" TO tm_admin;
GRANT REFERENCES ("StopOrderNum") ON public."StopOrders" TO tm_admin;
GRANT REFERENCES ("LinkedOrderNum") ON public."StopOrders" TO tm_admin;
GRANT REFERENCES ("BaseOrderNum") ON public."StopOrders" TO tm_admin;
GRANT REFERENCES ("CoOrderNum") ON public."StopOrders" TO tm_admin;
GRANT REFERENCES ("Status") ON public."StopOrders" TO tm_admin;
GRANT REFERENCES ("Result") ON public."StopOrders" TO tm_admin;
GRANT REFERENCES ("Expire") ON public."StopOrders" TO tm_admin;
GRANT REFERENCES ("StopPrice2") ON public."StopOrders" TO tm_admin;
GRANT REFERENCES ("ActiveFromTime") ON public."StopOrders" TO tm_admin;
GRANT REFERENCES ("ActiveToTime") ON public."StopOrders" TO tm_admin;
GRANT REFERENCES ("TrdAcc") ON public."StopOrders" TO tm_admin;
GRANT SELECT ON public."GroupOrders" TO tm_admin;
GRANT INSERT ON public."GroupOrders" TO tm_admin;
GRANT DELETE ON public."GroupOrders" TO tm_admin;
GRANT UPDATE ON public."GroupOrders" TO tm_admin;
GRANT SELECT ON public."GroupOrders" TO tm_user;
GRANT INSERT ON public."GroupOrders" TO tm_user;
GRANT DELETE ON public."GroupOrders" TO tm_user;
GRANT UPDATE ON public."GroupOrders" TO tm_user;
GRANT SELECT ("ID") ON public."GroupOrders" TO tm_admin;
GRANT UPDATE ("ID") ON public."GroupOrders" TO tm_admin;
GRANT SELECT ("ID") ON public."GroupOrders" TO tm_user;
GRANT UPDATE ("ID") ON public."GroupOrders" TO tm_user;
GRANT SELECT ("GroupID") ON public."GroupOrders" TO tm_admin;
GRANT UPDATE ("GroupID") ON public."GroupOrders" TO tm_admin;
GRANT SELECT ("GroupID") ON public."GroupOrders" TO tm_user;
GRANT UPDATE ("GroupID") ON public."GroupOrders" TO tm_user;
GRANT SELECT ("TradeDate") ON public."GroupOrders" TO tm_admin;
GRANT UPDATE ("TradeDate") ON public."GroupOrders" TO tm_admin;
GRANT SELECT ("TradeDate") ON public."GroupOrders" TO tm_user;
GRANT UPDATE ("TradeDate") ON public."GroupOrders" TO tm_user;
GRANT SELECT ("TradeTime") ON public."GroupOrders" TO tm_admin;
GRANT UPDATE ("TradeTime") ON public."GroupOrders" TO tm_admin;
GRANT SELECT ("TradeTime") ON public."GroupOrders" TO tm_user;
GRANT UPDATE ("TradeTime") ON public."GroupOrders" TO tm_user;
GRANT SELECT ("Buy") ON public."GroupOrders" TO tm_admin;
GRANT UPDATE ("Buy") ON public."GroupOrders" TO tm_admin;
GRANT SELECT ("Buy") ON public."GroupOrders" TO tm_user;
GRANT UPDATE ("Buy") ON public."GroupOrders" TO tm_user;
GRANT SELECT ("ML") ON public."GroupOrders" TO tm_admin;
GRANT UPDATE ("ML") ON public."GroupOrders" TO tm_admin;
GRANT SELECT ("ML") ON public."GroupOrders" TO tm_user;
GRANT UPDATE ("ML") ON public."GroupOrders" TO tm_user;
GRANT SELECT ("SecCode") ON public."GroupOrders" TO tm_admin;
GRANT UPDATE ("SecCode") ON public."GroupOrders" TO tm_admin;
GRANT SELECT ("SecCode") ON public."GroupOrders" TO tm_user;
GRANT UPDATE ("SecCode") ON public."GroupOrders" TO tm_user;
GRANT SELECT ("ClassCode") ON public."GroupOrders" TO tm_admin;
GRANT UPDATE ("ClassCode") ON public."GroupOrders" TO tm_admin;
GRANT SELECT ("ClassCode") ON public."GroupOrders" TO tm_user;
GRANT UPDATE ("ClassCode") ON public."GroupOrders" TO tm_user;
GRANT SELECT ("Price") ON public."GroupOrders" TO tm_admin;
GRANT UPDATE ("Price") ON public."GroupOrders" TO tm_admin;
GRANT SELECT ("Price") ON public."GroupOrders" TO tm_user;
GRANT UPDATE ("Price") ON public."GroupOrders" TO tm_user;
GRANT SELECT ("Qty") ON public."GroupOrders" TO tm_admin;
GRANT UPDATE ("Qty") ON public."GroupOrders" TO tm_admin;
GRANT SELECT ("Qty") ON public."GroupOrders" TO tm_user;
GRANT UPDATE ("Qty") ON public."GroupOrders" TO tm_user;
GRANT SELECT ("RealQty") ON public."GroupOrders" TO tm_admin;
GRANT UPDATE ("RealQty") ON public."GroupOrders" TO tm_admin;
GRANT SELECT ("RealQty") ON public."GroupOrders" TO tm_user;
GRANT UPDATE ("RealQty") ON public."GroupOrders" TO tm_user;
GRANT SELECT ("BalanceQty") ON public."GroupOrders" TO tm_admin;
GRANT UPDATE ("BalanceQty") ON public."GroupOrders" TO tm_admin;
GRANT SELECT ("BalanceQty") ON public."GroupOrders" TO tm_user;
GRANT UPDATE ("BalanceQty") ON public."GroupOrders" TO tm_user;
GRANT UPDATE ("KilledQty") ON public."GroupOrders" TO tm_admin;
GRANT SELECT ("KilledQty") ON public."GroupOrders" TO tm_admin;
GRANT SELECT ("KilledQty") ON public."GroupOrders" TO tm_user;
GRANT UPDATE ("KilledQty") ON public."GroupOrders" TO tm_user;
GRANT SELECT ("Status") ON public."GroupOrders" TO tm_admin;
GRANT UPDATE ("Status") ON public."GroupOrders" TO tm_admin;
GRANT SELECT ("Status") ON public."GroupOrders" TO tm_user;
GRANT UPDATE ("Status") ON public."GroupOrders" TO tm_user;
GRANT SELECT ("Result") ON public."GroupOrders" TO tm_admin;
GRANT UPDATE ("Result") ON public."GroupOrders" TO tm_admin;
GRANT SELECT ("Result") ON public."GroupOrders" TO tm_user;
GRANT UPDATE ("Result") ON public."GroupOrders" TO tm_user;
GRANT SELECT ("OrdersCount") ON public."GroupOrders" TO tm_admin;
GRANT UPDATE ("OrdersCount") ON public."GroupOrders" TO tm_admin;
GRANT SELECT ("OrdersCount") ON public."GroupOrders" TO tm_user;
GRANT UPDATE ("OrdersCount") ON public."GroupOrders" TO tm_user;
GRANT SELECT ("Flags") ON public."GroupOrders" TO tm_admin;
GRANT UPDATE ("Flags") ON public."GroupOrders" TO tm_admin;
GRANT SELECT ("Flags") ON public."GroupOrders" TO tm_user;
GRANT UPDATE ("Flags") ON public."GroupOrders" TO tm_user;
GRANT SELECT ("Value") ON public."GroupOrders" TO tm_admin;
GRANT UPDATE ("Value") ON public."GroupOrders" TO tm_admin;
GRANT SELECT ("Value") ON public."GroupOrders" TO tm_user;
GRANT UPDATE ("Value") ON public."GroupOrders" TO tm_user;
GRANT SELECT ("PoolID") ON public."GroupOrders" TO tm_admin;
GRANT UPDATE ("PoolID") ON public."GroupOrders" TO tm_admin;
GRANT SELECT ("PoolID") ON public."GroupOrders" TO tm_user;
GRANT UPDATE ("PoolID") ON public."GroupOrders" TO tm_user;
GRANT SELECT ("TMUserID") ON public."GroupOrders" TO tm_admin;
GRANT UPDATE ("TMUserID") ON public."GroupOrders" TO tm_admin;
GRANT SELECT ("TMUserID") ON public."GroupOrders" TO tm_user;
GRANT UPDATE ("TMUserID") ON public."GroupOrders" TO tm_user;
GRANT SELECT ("TMSessionID") ON public."GroupOrders" TO tm_admin;
GRANT UPDATE ("TMSessionID") ON public."GroupOrders" TO tm_admin;
GRANT SELECT ("TMSessionID") ON public."GroupOrders" TO tm_user;
GRANT UPDATE ("TMSessionID") ON public."GroupOrders" TO tm_user;
GRANT SELECT ("TMCounter") ON public."GroupOrders" TO tm_admin;
GRANT UPDATE ("TMCounter") ON public."GroupOrders" TO tm_admin;
GRANT SELECT ("TMCounter") ON public."GroupOrders" TO tm_user;
GRANT UPDATE ("TMCounter") ON public."GroupOrders" TO tm_user;
GRANT SELECT ("VisibleQty") ON public."GroupOrders" TO tm_admin;
GRANT UPDATE ("VisibleQty") ON public."GroupOrders" TO tm_admin;
GRANT SELECT ("VisibleQty") ON public."GroupOrders" TO tm_user;
GRANT UPDATE ("VisibleQty") ON public."GroupOrders" TO tm_user;
GRANT REFERENCES ON public."GroupOrders" TO tm_admin;
GRANT REFERENCES ("ID") ON public."GroupOrders" TO tm_admin;
GRANT REFERENCES ("GroupID") ON public."GroupOrders" TO tm_admin;
GRANT REFERENCES ("TradeDate") ON public."GroupOrders" TO tm_admin;
GRANT REFERENCES ("TradeTime") ON public."GroupOrders" TO tm_admin;
GRANT REFERENCES ("Buy") ON public."GroupOrders" TO tm_admin;
GRANT REFERENCES ("ML") ON public."GroupOrders" TO tm_admin;
GRANT REFERENCES ("SecCode") ON public."GroupOrders" TO tm_admin;
GRANT REFERENCES ("ClassCode") ON public."GroupOrders" TO tm_admin;
GRANT REFERENCES ("Price") ON public."GroupOrders" TO tm_admin;
GRANT REFERENCES ("Qty") ON public."GroupOrders" TO tm_admin;
GRANT REFERENCES ("RealQty") ON public."GroupOrders" TO tm_admin;
GRANT REFERENCES ("BalanceQty") ON public."GroupOrders" TO tm_admin;
GRANT REFERENCES ("KilledQty") ON public."GroupOrders" TO tm_admin;
GRANT REFERENCES ("Status") ON public."GroupOrders" TO tm_admin;
GRANT REFERENCES ("Result") ON public."GroupOrders" TO tm_admin;
GRANT REFERENCES ("OrdersCount") ON public."GroupOrders" TO tm_admin;
GRANT REFERENCES ("Flags") ON public."GroupOrders" TO tm_admin;
GRANT REFERENCES ("PoolID") ON public."GroupOrders" TO tm_admin;
GRANT REFERENCES ("Value") ON public."GroupOrders" TO tm_admin;
GRANT REFERENCES ("TMUserID") ON public."GroupOrders" TO tm_admin;
GRANT REFERENCES ("TMSessionID") ON public."GroupOrders" TO tm_admin;
GRANT REFERENCES ("TMCounter") ON public."GroupOrders" TO tm_admin;
GRANT REFERENCES ("VisibleQty") ON public."GroupOrders" TO tm_admin;
GRANT SELECT ON public."GroupStopOrders" TO tm_admin;
GRANT INSERT ON public."GroupStopOrders" TO tm_admin;
GRANT DELETE ON public."GroupStopOrders" TO tm_admin;
GRANT UPDATE ON public."GroupStopOrders" TO tm_admin;
GRANT SELECT ON public."GroupStopOrders" TO tm_user;
GRANT INSERT ON public."GroupStopOrders" TO tm_user;
GRANT DELETE ON public."GroupStopOrders" TO tm_user;
GRANT UPDATE ON public."GroupStopOrders" TO tm_user;
GRANT SELECT ("ID") ON public."GroupStopOrders" TO tm_admin;
GRANT UPDATE ("ID") ON public."GroupStopOrders" TO tm_admin;
GRANT SELECT ("ID") ON public."GroupStopOrders" TO tm_user;
GRANT UPDATE ("ID") ON public."GroupStopOrders" TO tm_user;
GRANT SELECT ("GroupID") ON public."GroupStopOrders" TO tm_admin;
GRANT UPDATE ("GroupID") ON public."GroupStopOrders" TO tm_admin;
GRANT SELECT ("GroupID") ON public."GroupStopOrders" TO tm_user;
GRANT UPDATE ("GroupID") ON public."GroupStopOrders" TO tm_user;
GRANT SELECT ("StopStyle") ON public."GroupStopOrders" TO tm_admin;
GRANT UPDATE ("StopStyle") ON public."GroupStopOrders" TO tm_admin;
GRANT SELECT ("StopStyle") ON public."GroupStopOrders" TO tm_user;
GRANT UPDATE ("StopStyle") ON public."GroupStopOrders" TO tm_user;
GRANT SELECT ("TradeDate") ON public."GroupStopOrders" TO tm_admin;
GRANT UPDATE ("TradeDate") ON public."GroupStopOrders" TO tm_admin;
GRANT SELECT ("TradeDate") ON public."GroupStopOrders" TO tm_user;
GRANT UPDATE ("TradeDate") ON public."GroupStopOrders" TO tm_user;
GRANT SELECT ("TradeTime") ON public."GroupStopOrders" TO tm_admin;
GRANT UPDATE ("TradeTime") ON public."GroupStopOrders" TO tm_admin;
GRANT SELECT ("TradeTime") ON public."GroupStopOrders" TO tm_user;
GRANT UPDATE ("TradeTime") ON public."GroupStopOrders" TO tm_user;
GRANT SELECT ("Buy") ON public."GroupStopOrders" TO tm_admin;
GRANT UPDATE ("Buy") ON public."GroupStopOrders" TO tm_admin;
GRANT SELECT ("Buy") ON public."GroupStopOrders" TO tm_user;
GRANT UPDATE ("Buy") ON public."GroupStopOrders" TO tm_user;
GRANT SELECT ("ML") ON public."GroupStopOrders" TO tm_admin;
GRANT UPDATE ("ML") ON public."GroupStopOrders" TO tm_admin;
GRANT SELECT ("ML") ON public."GroupStopOrders" TO tm_user;
GRANT UPDATE ("ML") ON public."GroupStopOrders" TO tm_user;
GRANT SELECT ("SecCode") ON public."GroupStopOrders" TO tm_admin;
GRANT UPDATE ("SecCode") ON public."GroupStopOrders" TO tm_admin;
GRANT SELECT ("SecCode") ON public."GroupStopOrders" TO tm_user;
GRANT UPDATE ("SecCode") ON public."GroupStopOrders" TO tm_user;
GRANT SELECT ("ClassCode") ON public."GroupStopOrders" TO tm_admin;
GRANT UPDATE ("ClassCode") ON public."GroupStopOrders" TO tm_admin;
GRANT SELECT ("ClassCode") ON public."GroupStopOrders" TO tm_user;
GRANT UPDATE ("ClassCode") ON public."GroupStopOrders" TO tm_user;
GRANT SELECT ("ASecCode") ON public."GroupStopOrders" TO tm_admin;
GRANT UPDATE ("ASecCode") ON public."GroupStopOrders" TO tm_admin;
GRANT SELECT ("ASecCode") ON public."GroupStopOrders" TO tm_user;
GRANT UPDATE ("ASecCode") ON public."GroupStopOrders" TO tm_user;
GRANT SELECT ("AClassCode") ON public."GroupStopOrders" TO tm_admin;
GRANT UPDATE ("AClassCode") ON public."GroupStopOrders" TO tm_admin;
GRANT SELECT ("AClassCode") ON public."GroupStopOrders" TO tm_user;
GRANT UPDATE ("AClassCode") ON public."GroupStopOrders" TO tm_user;
GRANT SELECT ("StopCond") ON public."GroupStopOrders" TO tm_admin;
GRANT UPDATE ("StopCond") ON public."GroupStopOrders" TO tm_admin;
GRANT SELECT ("StopCond") ON public."GroupStopOrders" TO tm_user;
GRANT UPDATE ("StopCond") ON public."GroupStopOrders" TO tm_user;
GRANT SELECT ("TransFlags") ON public."GroupStopOrders" TO tm_admin;
GRANT UPDATE ("TransFlags") ON public."GroupStopOrders" TO tm_admin;
GRANT SELECT ("TransFlags") ON public."GroupStopOrders" TO tm_user;
GRANT UPDATE ("TransFlags") ON public."GroupStopOrders" TO tm_user;
GRANT SELECT ("StopPrice") ON public."GroupStopOrders" TO tm_admin;
GRANT UPDATE ("StopPrice") ON public."GroupStopOrders" TO tm_admin;
GRANT SELECT ("StopPrice") ON public."GroupStopOrders" TO tm_user;
GRANT UPDATE ("StopPrice") ON public."GroupStopOrders" TO tm_user;
GRANT SELECT ("Price") ON public."GroupStopOrders" TO tm_admin;
GRANT UPDATE ("Price") ON public."GroupStopOrders" TO tm_admin;
GRANT SELECT ("Price") ON public."GroupStopOrders" TO tm_user;
GRANT UPDATE ("Price") ON public."GroupStopOrders" TO tm_user;
GRANT SELECT ("LinkedPrice") ON public."GroupStopOrders" TO tm_admin;
GRANT UPDATE ("LinkedPrice") ON public."GroupStopOrders" TO tm_admin;
GRANT SELECT ("LinkedPrice") ON public."GroupStopOrders" TO tm_user;
GRANT UPDATE ("LinkedPrice") ON public."GroupStopOrders" TO tm_user;
GRANT SELECT ("Offset") ON public."GroupStopOrders" TO tm_admin;
GRANT UPDATE ("Offset") ON public."GroupStopOrders" TO tm_admin;
GRANT SELECT ("Offset") ON public."GroupStopOrders" TO tm_user;
GRANT UPDATE ("Offset") ON public."GroupStopOrders" TO tm_user;
GRANT SELECT ("Spread") ON public."GroupStopOrders" TO tm_admin;
GRANT UPDATE ("Spread") ON public."GroupStopOrders" TO tm_admin;
GRANT SELECT ("Spread") ON public."GroupStopOrders" TO tm_user;
GRANT UPDATE ("Spread") ON public."GroupStopOrders" TO tm_user;
GRANT SELECT ("Qty") ON public."GroupStopOrders" TO tm_admin;
GRANT UPDATE ("Qty") ON public."GroupStopOrders" TO tm_admin;
GRANT SELECT ("Qty") ON public."GroupStopOrders" TO tm_user;
GRANT UPDATE ("Qty") ON public."GroupStopOrders" TO tm_user;
GRANT SELECT ("RealQty") ON public."GroupStopOrders" TO tm_admin;
GRANT UPDATE ("RealQty") ON public."GroupStopOrders" TO tm_admin;
GRANT SELECT ("RealQty") ON public."GroupStopOrders" TO tm_user;
GRANT UPDATE ("RealQty") ON public."GroupStopOrders" TO tm_user;
GRANT SELECT ("BalanceQty") ON public."GroupStopOrders" TO tm_admin;
GRANT UPDATE ("BalanceQty") ON public."GroupStopOrders" TO tm_admin;
GRANT SELECT ("BalanceQty") ON public."GroupStopOrders" TO tm_user;
GRANT UPDATE ("BalanceQty") ON public."GroupStopOrders" TO tm_user;
GRANT SELECT ("KilledQty") ON public."GroupStopOrders" TO tm_admin;
GRANT UPDATE ("KilledQty") ON public."GroupStopOrders" TO tm_admin;
GRANT SELECT ("KilledQty") ON public."GroupStopOrders" TO tm_user;
GRANT UPDATE ("KilledQty") ON public."GroupStopOrders" TO tm_user;
GRANT SELECT ("BaseOrderNum") ON public."GroupStopOrders" TO tm_admin;
GRANT UPDATE ("BaseOrderNum") ON public."GroupStopOrders" TO tm_admin;
GRANT SELECT ("BaseOrderNum") ON public."GroupStopOrders" TO tm_user;
GRANT UPDATE ("BaseOrderNum") ON public."GroupStopOrders" TO tm_user;
GRANT SELECT ("Status") ON public."GroupStopOrders" TO tm_admin;
GRANT UPDATE ("Status") ON public."GroupStopOrders" TO tm_admin;
GRANT SELECT ("Status") ON public."GroupStopOrders" TO tm_user;
GRANT UPDATE ("Status") ON public."GroupStopOrders" TO tm_user;
GRANT SELECT ("Result") ON public."GroupStopOrders" TO tm_admin;
GRANT UPDATE ("Result") ON public."GroupStopOrders" TO tm_admin;
GRANT SELECT ("Result") ON public."GroupStopOrders" TO tm_user;
GRANT UPDATE ("Result") ON public."GroupStopOrders" TO tm_user;
GRANT SELECT ("OrdersCount") ON public."GroupStopOrders" TO tm_admin;
GRANT UPDATE ("OrdersCount") ON public."GroupStopOrders" TO tm_admin;
GRANT SELECT ("OrdersCount") ON public."GroupStopOrders" TO tm_user;
GRANT UPDATE ("OrdersCount") ON public."GroupStopOrders" TO tm_user;
GRANT SELECT ("LinkedGrOrder") ON public."GroupStopOrders" TO tm_admin;
GRANT UPDATE ("LinkedGrOrder") ON public."GroupStopOrders" TO tm_admin;
GRANT SELECT ("LinkedGrOrder") ON public."GroupStopOrders" TO tm_user;
GRANT UPDATE ("LinkedGrOrder") ON public."GroupStopOrders" TO tm_user;
GRANT SELECT ("CoOrderNum") ON public."GroupStopOrders" TO tm_admin;
GRANT UPDATE ("CoOrderNum") ON public."GroupStopOrders" TO tm_admin;
GRANT SELECT ("CoOrderNum") ON public."GroupStopOrders" TO tm_user;
GRANT UPDATE ("CoOrderNum") ON public."GroupStopOrders" TO tm_user;
GRANT SELECT ("Expire") ON public."GroupStopOrders" TO tm_admin;
GRANT UPDATE ("Expire") ON public."GroupStopOrders" TO tm_admin;
GRANT SELECT ("Expire") ON public."GroupStopOrders" TO tm_user;
GRANT UPDATE ("Expire") ON public."GroupStopOrders" TO tm_user;
GRANT SELECT ("StopPrice2") ON public."GroupStopOrders" TO tm_admin;
GRANT UPDATE ("StopPrice2") ON public."GroupStopOrders" TO tm_admin;
GRANT SELECT ("StopPrice2") ON public."GroupStopOrders" TO tm_user;
GRANT UPDATE ("StopPrice2") ON public."GroupStopOrders" TO tm_user;
GRANT SELECT ("ActiveFromTime") ON public."GroupStopOrders" TO tm_admin;
GRANT UPDATE ("ActiveFromTime") ON public."GroupStopOrders" TO tm_admin;
GRANT SELECT ("ActiveFromTime") ON public."GroupStopOrders" TO tm_user;
GRANT UPDATE ("ActiveFromTime") ON public."GroupStopOrders" TO tm_user;
GRANT SELECT ("ActiveToTime") ON public."GroupStopOrders" TO tm_admin;
GRANT UPDATE ("ActiveToTime") ON public."GroupStopOrders" TO tm_admin;
GRANT SELECT ("ActiveToTime") ON public."GroupStopOrders" TO tm_user;
GRANT UPDATE ("ActiveToTime") ON public."GroupStopOrders" TO tm_user;
GRANT REFERENCES ON public."GroupStopOrders" TO tm_admin;
GRANT REFERENCES ("ID") ON public."GroupStopOrders" TO tm_admin;
GRANT REFERENCES ("GroupID") ON public."GroupStopOrders" TO tm_admin;
GRANT REFERENCES ("StopStyle") ON public."GroupStopOrders" TO tm_admin;
GRANT REFERENCES ("TradeDate") ON public."GroupStopOrders" TO tm_admin;
GRANT REFERENCES ("TradeTime") ON public."GroupStopOrders" TO tm_admin;
GRANT REFERENCES ("Buy") ON public."GroupStopOrders" TO tm_admin;
GRANT REFERENCES ("ML") ON public."GroupStopOrders" TO  tm_admin;
GRANT REFERENCES ("SecCode") ON public."GroupStopOrders" TO   tm_admin;
GRANT REFERENCES ("ClassCode") ON public."GroupStopOrders" TO tm_admin;
GRANT REFERENCES ("ASecCode") ON public."GroupStopOrders" TO tm_admin;
GRANT REFERENCES ("AClassCode") ON public."GroupStopOrders" TO tm_admin;
GRANT REFERENCES ("StopCond") ON public."GroupStopOrders" TO tm_admin;
GRANT REFERENCES ("TransFlags") ON public."GroupStopOrders" TO tm_admin;
GRANT REFERENCES ("StopPrice") ON public."GroupStopOrders" TO tm_admin;
GRANT REFERENCES ("Price") ON public."GroupStopOrders" TO tm_admin;
GRANT REFERENCES ("LinkedPrice") ON public."GroupStopOrders" TO tm_admin;
GRANT REFERENCES ("Offset") ON public."GroupStopOrders" TO tm_admin;
GRANT REFERENCES ("Spread") ON public."GroupStopOrders" TO tm_admin;
GRANT REFERENCES ("Qty") ON public."GroupStopOrders" TO tm_admin;
GRANT REFERENCES ("RealQty") ON public."GroupStopOrders" TO tm_admin;
GRANT REFERENCES ("BalanceQty") ON public."GroupStopOrders" TO tm_admin;
GRANT REFERENCES ("KilledQty") ON public."GroupStopOrders" TO tm_admin;
GRANT REFERENCES ("BaseOrderNum") ON public."GroupStopOrders" TO tm_admin;
GRANT REFERENCES ("Status") ON public."GroupStopOrders" TO tm_admin;
GRANT REFERENCES ("Result") ON public."GroupStopOrders" TO tm_admin;
GRANT REFERENCES ("OrdersCount") ON public."GroupStopOrders" TO tm_admin;
GRANT REFERENCES ("LinkedGrOrder") ON public."GroupStopOrders" TO tm_admin;
GRANT REFERENCES ("CoOrderNum") ON public."GroupStopOrders" TO tm_admin;
GRANT REFERENCES ("Expire") ON public."GroupStopOrders" TO tm_admin;
GRANT REFERENCES ("StopPrice2") ON public."GroupStopOrders" TO tm_admin;
GRANT REFERENCES ("ActiveFromTime") ON public."GroupStopOrders" TO tm_admin;
GRANT REFERENCES ("ActiveToTime") ON public."GroupStopOrders" TO tm_admin;
GRANT SELECT ON public."NegOrders" TO tm_admin;
GRANT INSERT ON public."NegOrders" TO tm_admin;
GRANT DELETE ON public."NegOrders" TO tm_admin;
GRANT UPDATE ON public."NegOrders" TO tm_admin;
GRANT SELECT ON public."NegOrders" TO tm_user;
GRANT INSERT ON public."NegOrders" TO tm_user;
GRANT DELETE ON public."NegOrders" TO tm_user;
GRANT UPDATE ON public."NegOrders" TO tm_user;
GRANT SELECT ("OrderId") ON public."NegOrders" TO tm_admin;
GRANT UPDATE ("OrderId") ON public."NegOrders" TO tm_admin;
GRANT SELECT ("OrderId") ON public."NegOrders" TO tm_user;
GRANT UPDATE ("OrderId") ON public."NegOrders" TO tm_user;
GRANT SELECT ("GroupOrderID") ON public."NegOrders" TO tm_admin;
GRANT UPDATE ("GroupOrderID") ON public."NegOrders" TO tm_admin;
GRANT SELECT ("GroupOrderID") ON public."NegOrders" TO tm_user;
GRANT UPDATE ("GroupOrderID") ON public."NegOrders" TO tm_user;
GRANT SELECT ("TradeDate") ON public."NegOrders" TO tm_admin;
GRANT UPDATE ("TradeDate") ON public."NegOrders" TO tm_admin;
GRANT SELECT ("TradeDate") ON public."NegOrders" TO tm_user;
GRANT UPDATE ("TradeDate") ON public."NegOrders" TO tm_user;
GRANT SELECT ("TradeTime") ON public."NegOrders" TO tm_admin;
GRANT UPDATE ("TradeTime") ON public."NegOrders" TO tm_admin;
GRANT SELECT ("TradeTime") ON public."NegOrders" TO tm_user;
GRANT UPDATE ("TradeTime") ON public."NegOrders" TO tm_user;
GRANT SELECT ("Buy") ON public."NegOrders" TO tm_admin;
GRANT UPDATE ("Buy") ON public."NegOrders" TO tm_admin;
GRANT UPDATE ("Buy") ON public."NegOrders" TO tm_user;
GRANT SELECT ("Buy") ON public."NegOrders" TO tm_user;
GRANT SELECT ("ClassCode") ON public."NegOrders" TO tm_admin;
GRANT UPDATE ("ClassCode") ON public."NegOrders" TO tm_admin;
GRANT SELECT ("ClassCode") ON public."NegOrders" TO tm_user;
GRANT UPDATE ("ClassCode") ON public."NegOrders" TO tm_user;
GRANT SELECT ("SecCode") ON public."NegOrders" TO tm_admin;
GRANT UPDATE ("SecCode") ON public."NegOrders" TO tm_admin;
GRANT SELECT ("SecCode") ON public."NegOrders" TO tm_user;
GRANT UPDATE ("SecCode") ON public."NegOrders" TO tm_user;
GRANT SELECT ("ClientCode") ON public."NegOrders" TO tm_admin;
GRANT UPDATE ("ClientCode") ON public."NegOrders" TO tm_admin;
GRANT SELECT ("ClientCode") ON public."NegOrders" TO tm_user;
GRANT UPDATE ("ClientCode") ON public."NegOrders" TO tm_user;
GRANT SELECT ("TrdAcc") ON public."NegOrders" TO tm_admin;
GRANT UPDATE ("TrdAcc") ON public."NegOrders" TO tm_admin;
GRANT SELECT ("TrdAcc") ON public."NegOrders" TO tm_user;
GRANT UPDATE ("TrdAcc") ON public."NegOrders" TO tm_user;
GRANT SELECT ("CPFirmId") ON public."NegOrders" TO tm_admin;
GRANT UPDATE ("CPFirmId") ON public."NegOrders" TO tm_admin;
GRANT SELECT ("CPFirmId") ON public."NegOrders" TO tm_user;
GRANT UPDATE ("CPFirmId") ON public."NegOrders" TO tm_user;
GRANT SELECT ("SettleCode") ON public."NegOrders" TO tm_admin;
GRANT UPDATE ("SettleCode") ON public."NegOrders" TO tm_admin;
GRANT SELECT ("SettleCode") ON public."NegOrders" TO tm_user;
GRANT UPDATE ("SettleCode") ON public."NegOrders" TO tm_user;
GRANT SELECT ("MatchRef") ON public."NegOrders" TO tm_admin;
GRANT UPDATE ("MatchRef") ON public."NegOrders" TO tm_admin;
GRANT SELECT ("MatchRef") ON public."NegOrders" TO tm_user;
GRANT UPDATE ("MatchRef") ON public."NegOrders" TO tm_user;
GRANT SELECT ("Price") ON public."NegOrders" TO tm_admin;
GRANT UPDATE ("Price") ON public."NegOrders" TO tm_admin;
GRANT SELECT ("Price") ON public."NegOrders" TO tm_user;
GRANT UPDATE ("Price") ON public."NegOrders" TO tm_user;
GRANT SELECT ("Vozm") ON public."NegOrders" TO tm_admin;
GRANT UPDATE ("Vozm") ON public."NegOrders" TO tm_admin;
GRANT SELECT ("Vozm") ON public."NegOrders" TO tm_user;
GRANT UPDATE ("Vozm") ON public."NegOrders" TO tm_user;
GRANT SELECT ("Qty") ON public."NegOrders" TO tm_admin;
GRANT UPDATE ("Qty") ON public."NegOrders" TO tm_admin;
GRANT SELECT ("Qty") ON public."NegOrders" TO tm_user;
GRANT UPDATE ("Qty") ON public."NegOrders" TO tm_user;
GRANT SELECT ("OrderNum") ON public."NegOrders" TO tm_admin;
GRANT UPDATE ("OrderNum") ON public."NegOrders" TO tm_admin;
GRANT SELECT ("OrderNum") ON public."NegOrders" TO tm_user;
GRANT UPDATE ("OrderNum") ON public."NegOrders" TO tm_user;
GRANT SELECT ("Status") ON public."NegOrders" TO tm_admin;
GRANT UPDATE ("Status") ON public."NegOrders" TO tm_admin;
GRANT SELECT ("Status") ON public."NegOrders" TO tm_user;
GRANT UPDATE ("Status") ON public."NegOrders" TO tm_user;
GRANT SELECT ("Result") ON public."NegOrders" TO tm_admin;
GRANT UPDATE ("Result") ON public."NegOrders" TO tm_admin;
GRANT SELECT ("Result") ON public."NegOrders" TO tm_user;
GRANT UPDATE ("Result") ON public."NegOrders" TO tm_user;
GRANT REFERENCES ON public."NegOrders" TO tm_admin;
GRANT REFERENCES ("OrderId") ON public."NegOrders" TO tm_admin;
GRANT REFERENCES ("GroupOrderID") ON public."NegOrders" TO tm_admin;
GRANT REFERENCES ("TradeDate") ON public."NegOrders" TO tm_admin;
GRANT REFERENCES ("TradeTime") ON public."NegOrders" TO tm_admin;
GRANT REFERENCES ("Buy") ON public."NegOrders" TO tm_admin;
GRANT REFERENCES ("ClassCode") ON public."NegOrders" TO tm_admin;
GRANT REFERENCES ("SecCode") ON public."NegOrders" TO tm_admin;
GRANT REFERENCES ("ClientCode") ON public."NegOrders" TO tm_admin;
GRANT REFERENCES ("TrdAcc") ON public."NegOrders" TO tm_admin;
GRANT REFERENCES ("CPFirmId") ON public."NegOrders" TO tm_admin;
GRANT REFERENCES ("SettleCode") ON public."NegOrders" TO tm_admin;
GRANT REFERENCES ("MatchRef") ON public."NegOrders" TO tm_admin;
GRANT REFERENCES ("Price") ON public."NegOrders" TO tm_admin;
GRANT REFERENCES ("Vozm") ON public."NegOrders" TO tm_admin;
GRANT REFERENCES ("Qty") ON public."NegOrders" TO tm_admin;
GRANT REFERENCES ("OrderNum") ON public."NegOrders" TO tm_admin;
GRANT REFERENCES ("Status") ON public."NegOrders" TO tm_admin;
GRANT REFERENCES ("Result") ON public."NegOrders" TO tm_admin;
GRANT SELECT ON public."GroupNegOrders" TO tm_admin;
GRANT INSERT ON public."GroupNegOrders" TO tm_admin;
GRANT DELETE ON public."GroupNegOrders" TO tm_admin;
GRANT UPDATE ON public."GroupNegOrders" TO tm_admin;
GRANT SELECT ON public."GroupNegOrders" TO tm_user;
GRANT INSERT ON public."GroupNegOrders" TO tm_user;
GRANT DELETE ON public."GroupNegOrders" TO tm_user;
GRANT UPDATE ON public."GroupNegOrders" TO tm_user;
GRANT SELECT ("ID") ON public."GroupNegOrders" TO tm_admin;
GRANT UPDATE ("ID") ON public."GroupNegOrders" TO tm_admin;
GRANT SELECT ("ID") ON public."GroupNegOrders" TO tm_user;
GRANT UPDATE ("ID") ON public."GroupNegOrders" TO tm_user;
GRANT SELECT ("GroupID") ON public."GroupNegOrders" TO tm_admin;
GRANT UPDATE ("GroupID") ON public."GroupNegOrders" TO tm_admin;
GRANT SELECT ("GroupID") ON public."GroupNegOrders" TO tm_user;
GRANT UPDATE ("GroupID") ON public."GroupNegOrders" TO tm_user;
GRANT SELECT ("TradeDate") ON public."GroupNegOrders" TO tm_admin;
GRANT UPDATE ("TradeDate") ON public."GroupNegOrders" TO tm_admin;
GRANT SELECT ("TradeDate") ON public."GroupNegOrders" TO tm_user;
GRANT UPDATE ("TradeDate") ON public."GroupNegOrders" TO tm_user;
GRANT SELECT ("TradeTime") ON public."GroupNegOrders" TO tm_admin;
GRANT UPDATE ("TradeTime") ON public."GroupNegOrders" TO tm_admin;
GRANT SELECT ("TradeTime") ON public."GroupNegOrders" TO tm_user;
GRANT UPDATE ("TradeTime") ON public."GroupNegOrders" TO tm_user;
GRANT SELECT ("Buy") ON public."GroupNegOrders" TO tm_admin;
GRANT UPDATE ("Buy") ON public."GroupNegOrders" TO tm_admin;
GRANT SELECT ("Buy") ON public."GroupNegOrders" TO tm_user;
GRANT UPDATE ("Buy") ON public."GroupNegOrders" TO tm_user;
GRANT SELECT ("SecCode") ON public."GroupNegOrders" TO tm_admin;
GRANT UPDATE ("SecCode") ON public."GroupNegOrders" TO tm_admin;
GRANT SELECT ("SecCode") ON public."GroupNegOrders" TO tm_user;
GRANT UPDATE ("SecCode") ON public."GroupNegOrders" TO tm_user;
GRANT SELECT ("ClassCode") ON public."GroupNegOrders" TO tm_admin;
GRANT UPDATE ("ClassCode") ON public."GroupNegOrders" TO tm_admin;
GRANT SELECT ("ClassCode") ON public."GroupNegOrders" TO tm_user;
GRANT UPDATE ("ClassCode") ON public."GroupNegOrders" TO tm_user;
GRANT SELECT ("CPFirmID") ON public."GroupNegOrders" TO tm_admin;
GRANT UPDATE ("CPFirmID") ON public."GroupNegOrders" TO tm_admin;
GRANT SELECT ("CPFirmID") ON public."GroupNegOrders" TO tm_user;
GRANT UPDATE ("CPFirmID") ON public."GroupNegOrders" TO tm_user;
GRANT SELECT ("SettleCode") ON public."GroupNegOrders" TO tm_admin;
GRANT UPDATE ("SettleCode") ON public."GroupNegOrders" TO tm_admin;
GRANT SELECT ("SettleCode") ON public."GroupNegOrders" TO tm_user;
GRANT UPDATE ("SettleCode") ON public."GroupNegOrders" TO tm_user;
GRANT SELECT ("MatchRef") ON public."GroupNegOrders" TO tm_admin;
GRANT UPDATE ("MatchRef") ON public."GroupNegOrders" TO tm_admin;
GRANT SELECT ("MatchRef") ON public."GroupNegOrders" TO tm_user;
GRANT UPDATE ("MatchRef") ON public."GroupNegOrders" TO tm_user;
GRANT SELECT ("Price") ON public."GroupNegOrders" TO tm_admin;
GRANT UPDATE ("Price") ON public."GroupNegOrders" TO tm_admin;
GRANT SELECT ("Price") ON public."GroupNegOrders" TO tm_user;
GRANT UPDATE ("Price") ON public."GroupNegOrders" TO tm_user;
GRANT SELECT ("Vozm") ON public."GroupNegOrders" TO tm_admin;
GRANT UPDATE ("Vozm") ON public."GroupNegOrders" TO tm_admin;
GRANT SELECT ("Vozm") ON public."GroupNegOrders" TO tm_user;
GRANT UPDATE ("Vozm") ON public."GroupNegOrders" TO tm_user;
GRANT SELECT ("Qty") ON public."GroupNegOrders" TO tm_admin;
GRANT UPDATE ("Qty") ON public."GroupNegOrders" TO tm_admin;
GRANT SELECT ("Qty") ON public."GroupNegOrders" TO tm_user;
GRANT UPDATE ("Qty") ON public."GroupNegOrders" TO tm_user;
GRANT SELECT ("RealQty") ON public."GroupNegOrders" TO tm_admin;
GRANT UPDATE ("RealQty") ON public."GroupNegOrders" TO tm_admin;
GRANT SELECT ("RealQty") ON public."GroupNegOrders" TO tm_user;
GRANT UPDATE ("RealQty") ON public."GroupNegOrders" TO tm_user;
GRANT SELECT ("KilledQty") ON public."GroupNegOrders" TO tm_admin;
GRANT UPDATE ("KilledQty") ON public."GroupNegOrders" TO tm_admin;
GRANT SELECT ("KilledQty") ON public."GroupNegOrders" TO tm_user;
GRANT UPDATE ("KilledQty") ON public."GroupNegOrders" TO tm_user;
GRANT SELECT ("Status") ON public."GroupNegOrders" TO tm_admin;
GRANT UPDATE ("Status") ON public."GroupNegOrders" TO tm_admin;
GRANT SELECT ("Status") ON public."GroupNegOrders" TO tm_user;
GRANT UPDATE ("Status") ON public."GroupNegOrders" TO tm_user;
GRANT SELECT ("Result") ON public."GroupNegOrders" TO tm_admin;
GRANT UPDATE ("Result") ON public."GroupNegOrders" TO tm_admin;
GRANT SELECT ("Result") ON public."GroupNegOrders" TO tm_user;
GRANT UPDATE ("Result") ON public."GroupNegOrders" TO tm_user;
GRANT SELECT ("OrdersCount") ON public."GroupNegOrders" TO tm_admin;
GRANT UPDATE ("OrdersCount") ON public."GroupNegOrders" TO tm_admin;
GRANT SELECT ("OrdersCount") ON public."GroupNegOrders" TO tm_user;
GRANT UPDATE ("OrdersCount") ON public."GroupNegOrders" TO tm_user;
GRANT REFERENCES ON public."GroupNegOrders" TO tm_admin;
GRANT REFERENCES ("ID") ON public."GroupNegOrders" TO tm_admin;
GRANT REFERENCES ("GroupID") ON public."GroupNegOrders" TO tm_admin;
GRANT REFERENCES ("TradeDate") ON public."GroupNegOrders" TO tm_admin;
GRANT REFERENCES ("TradeTime") ON public."GroupNegOrders" TO tm_admin;
GRANT REFERENCES ("Buy") ON public."GroupNegOrders" TO tm_admin;
GRANT REFERENCES ("SecCode") ON public."GroupNegOrders" TO tm_admin;
GRANT REFERENCES ("ClassCode") ON public."GroupNegOrders" TO tm_admin;
GRANT REFERENCES ("CPFirmID") ON public."GroupNegOrders" TO tm_admin;
GRANT REFERENCES ("SettleCode") ON public."GroupNegOrders" TO tm_admin;
GRANT REFERENCES ("MatchRef") ON public."GroupNegOrders" TO tm_admin;
GRANT REFERENCES ("Price") ON public."GroupNegOrders" TO tm_admin;
GRANT REFERENCES ("Vozm") ON public."GroupNegOrders" TO tm_admin;
GRANT REFERENCES ("Qty") ON public."GroupNegOrders" TO tm_admin;
GRANT REFERENCES ("RealQty") ON public."GroupNegOrders" TO tm_admin;
GRANT REFERENCES ("KilledQty") ON public."GroupNegOrders" TO tm_admin;
GRANT REFERENCES ("Status") ON public."GroupNegOrders" TO tm_admin;
GRANT REFERENCES ("Result") ON public."GroupNegOrders" TO tm_admin;
GRANT REFERENCES ("OrdersCount") ON public."GroupNegOrders" TO tm_admin;
GRANT SELECT ON public."ExportOrder" TO tm_admin;
GRANT INSERT ON public."ExportOrder" TO tm_admin;
GRANT DELETE ON public."ExportOrder" TO tm_admin;
GRANT UPDATE ON public."ExportOrder" TO tm_admin;
GRANT SELECT ON public."ExportOrder" TO tm_user;
GRANT INSERT ON public."ExportOrder" TO tm_user;
GRANT DELETE ON public."ExportOrder" TO tm_user;
GRANT UPDATE ON public."ExportOrder" TO tm_user;
GRANT SELECT ("ID") ON public."ExportOrder" TO tm_admin;
GRANT UPDATE ("ID") ON public."ExportOrder" TO tm_admin;
GRANT SELECT ("ID") ON public."ExportOrder" TO tm_user;
GRANT UPDATE ("ID") ON public."ExportOrder" TO tm_user;
GRANT SELECT ("OrderID") ON public."ExportOrder" TO tm_admin;
GRANT UPDATE ("OrderID") ON public."ExportOrder" TO tm_admin;
GRANT SELECT ("OrderID") ON public."ExportOrder" TO tm_user;
GRANT UPDATE ("OrderID") ON public."ExportOrder" TO tm_user;
GRANT SELECT ("GroupOrderID") ON public."ExportOrder" TO tm_admin;
GRANT UPDATE ("GroupOrderID") ON public."ExportOrder" TO tm_admin;
GRANT SELECT ("GroupOrderID") ON public."ExportOrder" TO tm_user;
GRANT UPDATE ("GroupOrderID") ON public."ExportOrder" TO tm_user;
GRANT SELECT ("OrderType") ON public."ExportOrder" TO tm_admin;
GRANT UPDATE ("OrderType") ON public."ExportOrder" TO tm_admin;
GRANT SELECT ("OrderType") ON public."ExportOrder" TO tm_user;
GRANT UPDATE ("OrderType") ON public."ExportOrder" TO tm_user;
GRANT SELECT ("TradeDate") ON public."ExportOrder" TO tm_admin;
GRANT UPDATE ("TradeDate") ON public."ExportOrder" TO tm_admin;
GRANT SELECT ("TradeDate") ON public."ExportOrder" TO tm_user;
GRANT UPDATE ("TradeDate") ON public."ExportOrder" TO tm_user;
GRANT SELECT ("SendDate") ON public."ExportOrder" TO tm_admin;
GRANT UPDATE ("SendDate") ON public."ExportOrder" TO tm_admin;
GRANT SELECT ("SendDate") ON public."ExportOrder" TO tm_user;
GRANT UPDATE ("SendDate") ON public."ExportOrder" TO tm_user;
GRANT SELECT ("SendTime") ON public."ExportOrder" TO tm_admin;
GRANT UPDATE ("SendTime") ON public."ExportOrder" TO tm_admin;
GRANT SELECT ("SendTime") ON public."ExportOrder" TO tm_user;
GRANT UPDATE ("SendTime") ON public."ExportOrder" TO tm_user;
GRANT SELECT ("OrderTime") ON public."ExportOrder" TO tm_admin;
GRANT UPDATE ("OrderTime") ON public."ExportOrder" TO tm_admin;
GRANT SELECT ("OrderTime") ON public."ExportOrder" TO tm_user;
GRANT UPDATE ("OrderTime") ON public."ExportOrder" TO tm_user;
GRANT SELECT ("Status") ON public."ExportOrder" TO tm_admin;
GRANT UPDATE ("Status") ON public."ExportOrder" TO tm_admin;
GRANT SELECT ("Status") ON public."ExportOrder" TO tm_user;
GRANT UPDATE ("Status") ON public."ExportOrder" TO tm_user;
GRANT SELECT ("UserGroupID") ON public."ExportOrder" TO tm_admin;
GRANT UPDATE ("UserGroupID") ON public."ExportOrder" TO tm_admin;
GRANT SELECT ("UserGroupID") ON public."ExportOrder" TO tm_user;
GRANT UPDATE ("UserGroupID") ON public."ExportOrder" TO tm_user;
GRANT SELECT ("UserGroupCode") ON public."ExportOrder" TO tm_admin;
GRANT UPDATE ("UserGroupCode") ON public."ExportOrder" TO tm_admin;
GRANT SELECT ("UserGroupCode") ON public."ExportOrder" TO tm_user;
GRANT UPDATE ("UserGroupCode") ON public."ExportOrder" TO tm_user;
GRANT SELECT ("PoolID") ON public."ExportOrder" TO tm_admin;
GRANT UPDATE ("PoolID") ON public."ExportOrder" TO tm_admin;
GRANT SELECT ("PoolID") ON public."ExportOrder" TO tm_user;
GRANT UPDATE ("PoolID") ON public."ExportOrder" TO tm_user;
GRANT SELECT ("BrokerPoolID") ON public."ExportOrder" TO tm_admin;
GRANT UPDATE ("BrokerPoolID") ON public."ExportOrder" TO tm_admin;
GRANT SELECT ("BrokerPoolID") ON public."ExportOrder" TO tm_user;
GRANT UPDATE ("BrokerPoolID") ON public."ExportOrder" TO tm_user;
GRANT SELECT ("ClientCode") ON public."ExportOrder" TO tm_admin;
GRANT UPDATE ("ClientCode") ON public."ExportOrder" TO tm_admin;
GRANT SELECT ("ClientCode") ON public."ExportOrder" TO tm_user;
GRANT UPDATE ("ClientCode") ON public."ExportOrder" TO tm_user;
GRANT SELECT ("TrdAcc") ON public."ExportOrder" TO tm_admin;
GRANT UPDATE ("TrdAcc") ON public."ExportOrder" TO tm_admin;
GRANT SELECT ("TrdAcc") ON public."ExportOrder" TO tm_user;
GRANT UPDATE ("TrdAcc") ON public."ExportOrder" TO tm_user;
GRANT SELECT ("Price") ON public."ExportOrder" TO tm_admin;
GRANT UPDATE ("Price") ON public."ExportOrder" TO tm_admin;
GRANT SELECT ("Price") ON public."ExportOrder" TO tm_user;
GRANT UPDATE ("Price") ON public."ExportOrder" TO tm_user;
GRANT SELECT ("Qty") ON public."ExportOrder" TO tm_admin;
GRANT UPDATE ("Qty") ON public."ExportOrder" TO tm_admin;
GRANT SELECT ("Qty") ON public."ExportOrder" TO tm_user;
GRANT UPDATE ("Qty") ON public."ExportOrder" TO tm_user;
GRANT SELECT ("Balance") ON public."ExportOrder" TO tm_admin;
GRANT UPDATE ("Balance") ON public."ExportOrder" TO tm_admin;
GRANT SELECT ("Balance") ON public."ExportOrder" TO tm_user;
GRANT UPDATE ("Balance") ON public."ExportOrder" TO tm_user;
GRANT SELECT ("Buy") ON public."ExportOrder" TO tm_admin;
GRANT UPDATE ("Buy") ON public."ExportOrder" TO tm_admin;
GRANT SELECT ("Buy") ON public."ExportOrder" TO tm_user;
GRANT UPDATE ("Buy") ON public."ExportOrder" TO tm_user;
GRANT SELECT ("ML") ON public."ExportOrder" TO tm_admin;
GRANT UPDATE ("ML") ON public."ExportOrder" TO tm_admin;
GRANT SELECT ("ML") ON public."ExportOrder" TO tm_user;
GRANT UPDATE ("ML") ON public."ExportOrder" TO tm_user;
GRANT SELECT ("ClassCode") ON public."ExportOrder" TO tm_admin;
GRANT UPDATE ("ClassCode") ON public."ExportOrder" TO tm_admin;
GRANT SELECT ("ClassCode") ON public."ExportOrder" TO tm_user;
GRANT UPDATE ("ClassCode") ON public."ExportOrder" TO tm_user;
GRANT SELECT ("SecCode") ON public."ExportOrder" TO tm_admin;
GRANT UPDATE ("SecCode") ON public."ExportOrder" TO tm_admin;
GRANT SELECT ("SecCode") ON public."ExportOrder" TO tm_user;
GRANT UPDATE ("SecCode") ON public."ExportOrder" TO tm_user;
GRANT SELECT ("VisibleQty") ON public."ExportOrder" TO tm_admin;
GRANT UPDATE ("VisibleQty") ON public."ExportOrder" TO tm_admin;
GRANT SELECT ("VisibleQty") ON public."ExportOrder" TO tm_user;
GRANT UPDATE ("VisibleQty") ON public."ExportOrder" TO tm_user;
GRANT SELECT ON public."GroupDesc" TO tm_admin;
GRANT INSERT ON public."GroupDesc" TO tm_admin;
GRANT DELETE ON public."GroupDesc" TO tm_admin;
GRANT UPDATE ON public."GroupDesc" TO tm_admin;
GRANT SELECT ON public."GroupDesc" TO tm_user;
GRANT SELECT ("GroupID") ON public."GroupDesc" TO tm_admin;
GRANT UPDATE ("GroupID") ON public."GroupDesc" TO tm_admin;
GRANT SELECT ("GroupID") ON public."GroupDesc" TO tm_user;
GRANT SELECT ("GroupCode") ON public."GroupDesc" TO tm_admin;
GRANT UPDATE ("GroupCode") ON public."GroupDesc" TO tm_admin;
GRANT SELECT ("GroupCode") ON public."GroupDesc" TO tm_user;
GRANT SELECT ("GroupName") ON public."GroupDesc" TO tm_admin;
GRANT UPDATE ("GroupName") ON public."GroupDesc" TO tm_admin;
GRANT SELECT ("GroupName") ON public."GroupDesc" TO tm_user;
GRANT SELECT ("disabled") ON public."GroupDesc" TO tm_admin;
GRANT UPDATE ("disabled") ON public."GroupDesc" TO tm_admin;
GRANT SELECT ("disabled") ON public."GroupDesc" TO tm_user;
GRANT SELECT ("Mode") ON public."GroupDesc" TO tm_admin;
GRANT UPDATE ("Mode") ON public."GroupDesc" TO tm_admin;
GRANT SELECT ("Mode") ON public."GroupDesc" TO tm_user;
GRANT SELECT ("DefWeight") ON public."GroupDesc" TO tm_admin;
GRANT UPDATE ("DefWeight") ON public."GroupDesc" TO tm_admin;
GRANT SELECT ("DefWeight") ON public."GroupDesc" TO tm_user;
GRANT SELECT ("SplitOrders") ON public."GroupDesc" TO tm_admin;
GRANT UPDATE ("SplitOrders") ON public."GroupDesc" TO tm_admin;
GRANT SELECT ("SplitOrders") ON public."GroupDesc" TO tm_user;
GRANT REFERENCES ON public."GroupDesc" TO tm_admin;
GRANT REFERENCES ("GroupID") ON public."GroupDesc" TO tm_admin;
GRANT REFERENCES ("GroupCode") ON public."GroupDesc" TO tm_admin;
GRANT REFERENCES ("GroupName") ON public."GroupDesc" TO tm_admin;
GRANT REFERENCES ("disabled") ON public."GroupDesc" TO tm_admin;
GRANT REFERENCES ("Mode") ON public."GroupDesc" TO tm_admin;
GRANT REFERENCES ("DefWeight") ON public."GroupDesc" TO tm_admin;
GRANT REFERENCES ("SplitOrders") ON public."GroupDesc" TO tm_admin;
GRANT SELECT ON public."Restrictions" TO tm_admin;
GRANT INSERT ON public."Restrictions" TO tm_admin;
GRANT DELETE ON public."Restrictions" TO tm_admin;
GRANT UPDATE ON public."Restrictions" TO tm_admin;
GRANT SELECT ON public."Restrictions" TO tm_user;
GRANT SELECT ("ID") ON public."Restrictions" TO tm_admin;
GRANT UPDATE ("ID") ON public."Restrictions" TO tm_admin;
GRANT SELECT ("ID") ON public."Restrictions" TO tm_user;
GRANT SELECT ("RestType") ON public."Restrictions" TO tm_admin;
GRANT UPDATE ("RestType") ON public."Restrictions" TO tm_admin;
GRANT SELECT ("RestType") ON public."Restrictions" TO tm_user;
GRANT SELECT ("Client") ON public."Restrictions" TO tm_admin;
GRANT UPDATE ("Client") ON public."Restrictions" TO tm_admin;
GRANT SELECT ("Client") ON public."Restrictions" TO tm_user;
GRANT SELECT ("Mode") ON public."Restrictions" TO tm_admin;
GRANT UPDATE ("Mode") ON public."Restrictions" TO tm_admin;
GRANT SELECT ("Mode") ON public."Restrictions" TO tm_user;
GRANT SELECT ("Limit") ON public."Restrictions" TO tm_admin;
GRANT UPDATE ("Limit") ON public."Restrictions" TO tm_admin;
GRANT SELECT ("Limit") ON public."Restrictions" TO tm_user;
GRANT SELECT ("SecGroupID") ON public."Restrictions" TO tm_admin;
GRANT UPDATE ("SecGroupID") ON public."Restrictions" TO tm_admin;
GRANT SELECT ("SecGroupID") ON public."Restrictions" TO tm_user;
GRANT SELECT ("CoSecGroupID") ON public."Restrictions" TO tm_admin;
GRANT UPDATE ("CoSecGroupID") ON public."Restrictions" TO tm_admin;
GRANT SELECT ("CoSecGroupID") ON public."Restrictions" TO tm_user;
GRANT SELECT ("disabled") ON public."Restrictions" TO tm_admin;
GRANT UPDATE ("disabled") ON public."Restrictions" TO tm_admin;
GRANT SELECT ("disabled") ON public."Restrictions" TO tm_user;
GRANT SELECT ("Value") ON public."Restrictions" TO tm_admin;
GRANT UPDATE ("Value") ON public."Restrictions" TO tm_admin;
GRANT SELECT ("Value") ON public."Restrictions" TO tm_user;
GRANT SELECT ("NoCheck") ON public."Restrictions" TO tm_admin;
GRANT UPDATE ("NoCheck") ON public."Restrictions" TO tm_admin;
GRANT SELECT ("NoCheck") ON public."Restrictions" TO tm_user;
GRANT SELECT ("TMUserID") ON public."Restrictions" TO tm_admin;
GRANT UPDATE ("TMUserID") ON public."Restrictions" TO tm_admin;
GRANT SELECT ("TMUserID") ON public."Restrictions" TO tm_user;
GRANT SELECT ("TMSessionID") ON public."Restrictions" TO tm_admin;
GRANT UPDATE ("TMSessionID") ON public."Restrictions" TO tm_admin;
GRANT SELECT ("TMSessionID") ON public."Restrictions" TO tm_user;
GRANT SELECT ("TMCounter") ON public."Restrictions" TO tm_admin;
GRANT UPDATE ("TMCounter") ON public."Restrictions" TO tm_admin;
GRANT SELECT ("TMCounter") ON public."Restrictions" TO tm_user;
GRANT REFERENCES ON public."Restrictions" TO tm_admin;
GRANT REFERENCES ("ID") ON public."Restrictions" TO tm_admin;
GRANT REFERENCES ("RestType") ON public."Restrictions" TO tm_admin;
GRANT REFERENCES ("Client") ON public."Restrictions" TO tm_admin;
GRANT REFERENCES ("Mode") ON public."Restrictions" TO tm_admin;
GRANT REFERENCES ("Limit") ON public."Restrictions" TO tm_admin;
GRANT REFERENCES ("SecGroupID") ON public."Restrictions" TO tm_admin;
GRANT REFERENCES ("CoSecGroupID") ON public."Restrictions" TO tm_admin;
GRANT REFERENCES ("disabled") ON public."Restrictions" TO tm_admin;
GRANT REFERENCES ("Value") ON public."Restrictions" TO tm_admin;
GRANT REFERENCES ("NoCheck") ON public."Restrictions" TO tm_admin;
GRANT REFERENCES ("TMUserID") ON public."Restrictions" TO tm_admin;
GRANT REFERENCES ("TMSessionID") ON public."Restrictions" TO tm_admin;
GRANT REFERENCES ("TMCounter") ON public."Restrictions" TO tm_admin;
GRANT SELECT ON public."SecGroups" TO tm_admin;
GRANT INSERT ON public."SecGroups" TO tm_admin;
GRANT DELETE ON public."SecGroups" TO tm_admin;
GRANT UPDATE ON public."SecGroups" TO tm_admin;
GRANT SELECT ON public."SecGroups" TO tm_user;
GRANT INSERT ON public."SecGroups" TO tm_user;
GRANT DELETE ON public."SecGroups" TO tm_user;
GRANT UPDATE ON public."SecGroups" TO tm_user;
GRANT SELECT ("ID") ON public."SecGroups" TO tm_admin;
GRANT UPDATE ("ID") ON public."SecGroups" TO tm_admin;
GRANT SELECT ("ID") ON public."SecGroups" TO tm_user;
GRANT UPDATE ("ID") ON public."SecGroups" TO tm_user;
GRANT SELECT ("SecGCode") ON public."SecGroups" TO tm_admin;
GRANT UPDATE ("SecGCode") ON public."SecGroups" TO tm_admin;
GRANT SELECT ("SecGCode") ON public."SecGroups" TO tm_user;
GRANT UPDATE ("SecGCode") ON public."SecGroups" TO tm_user;
GRANT SELECT ("SecGName") ON public."SecGroups" TO tm_admin;
GRANT UPDATE ("SecGName") ON public."SecGroups" TO tm_admin;
GRANT SELECT ("SecGName") ON public."SecGroups" TO tm_user;
GRANT UPDATE ("SecGName") ON public."SecGroups" TO tm_user;
GRANT SELECT ("disabled") ON public."SecGroups" TO tm_admin;
GRANT UPDATE ("disabled") ON public."SecGroups" TO tm_admin;
GRANT SELECT ("disabled") ON public."SecGroups" TO tm_user;
GRANT UPDATE ("disabled") ON public."SecGroups" TO tm_user;
GRANT SELECT ("Filter") ON public."SecGroups" TO tm_admin;
GRANT UPDATE ("Filter") ON public."SecGroups" TO tm_admin;
GRANT SELECT ("Filter") ON public."SecGroups" TO tm_user;
GRANT UPDATE ("Filter") ON public."SecGroups" TO tm_user;
GRANT SELECT ("mode") ON public."SecGroups" TO tm_admin;
GRANT UPDATE ("mode") ON public."SecGroups" TO tm_admin;
GRANT SELECT ("mode") ON public."SecGroups" TO tm_user;
GRANT UPDATE ("mode") ON public."SecGroups" TO tm_user;
GRANT SELECT ("RAgencies") ON public."SecGroups" TO tm_admin;
GRANT UPDATE ("RAgencies") ON public."SecGroups" TO tm_admin;
GRANT SELECT ("RAgencies") ON public."SecGroups" TO tm_user;
GRANT UPDATE ("RAgencies") ON public."SecGroups" TO tm_user;
GRANT REFERENCES ON public."SecGroups" TO tm_admin;
GRANT REFERENCES ("ID") ON public."SecGroups" TO tm_admin;
GRANT REFERENCES ("SecGCode") ON public."SecGroups" TO tm_admin;
GRANT REFERENCES ("SecGName") ON public."SecGroups" TO tm_admin;
GRANT REFERENCES ("disabled") ON public."SecGroups" TO tm_admin;
GRANT REFERENCES ("Filter") ON public."SecGroups" TO tm_admin;
GRANT REFERENCES ("mode") ON public."SecGroups" TO tm_admin;
GRANT REFERENCES ("RAgencies") ON public."SecGroups" TO tm_admin;

GRANT EXECUTE ON PROCEDURE public."TM_CreateOrderInPool" TO tm_admin;
GRANT EXECUTE ON PROCEDURE public."TM_CreateOrderInPool" TO tm_user;
GRANT EXECUTE ON PROCEDURE public."TM_SetNewImportEvent" TO tm_admin;
GRANT EXECUTE ON PROCEDURE public."TM_SetNewImportEvent" TO tm_user;
GRANT EXECUTE ON PROCEDURE public."TM_CreateRestriction" TO tm_admin;
GRANT EXECUTE ON PROCEDURE public."TM_CreateRestriction" TO tm_user;
GRANT EXECUTE ON PROCEDURE public."CreateNewRestriction" TO tm_admin;
GRANT EXECUTE ON PROCEDURE public."CreateNewRestriction" TO tm_user;
GRANT EXECUTE ON PROCEDURE public."TM_CreateSecGroup" TO tm_admin;
GRANT EXECUTE ON PROCEDURE public."TM_CreateSecGroup" TO tm_user;
GRANT EXECUTE ON PROCEDURE public."ModifySecGroup" TO tm_admin;
GRANT EXECUTE ON PROCEDURE public."ModifySecGroup" TO tm_user;
GRANT EXECUTE ON PROCEDURE public."DeleteSecGroup" TO tm_admin;
GRANT EXECUTE ON PROCEDURE public."DeleteSecGroup" TO tm_user;
GRANT EXECUTE ON PROCEDURE public."TM_CreateGroupOrder" TO tm_admin;
GRANT EXECUTE ON PROCEDURE public."TM_CreateGroupOrder" TO tm_user;
GRANT EXECUTE ON PROCEDURE public."TM_CreateOrder" TO tm_admin;
GRANT EXECUTE ON PROCEDURE public."TM_CreateOrder" TO tm_user;
GRANT EXECUTE ON PROCEDURE public."EditRestriction" TO tm_admin;
GRANT EXECUTE ON PROCEDURE public."EditRestriction" TO tm_user;
GRANT EXECUTE ON PROCEDURE public."TM_CreateClientGroup" TO tm_admin;
GRANT EXECUTE ON PROCEDURE public."TM_CreateClientGroup" TO tm_user;
GRANT EXECUTE ON PROCEDURE public."TM_CreateExportOrder" TO tm_admin;
GRANT EXECUTE ON PROCEDURE public."TM_CreateExportOrder" TO tm_user;
GRANT EXECUTE ON PROCEDURE public."TM_CreateGroupNegOrder" TO tm_admin;
GRANT EXECUTE ON PROCEDURE public."TM_CreateGroupNegOrder" TO tm_user;
GRANT EXECUTE ON PROCEDURE public."TM_CreateGroupStopOrder" TO tm_admin;
GRANT EXECUTE ON PROCEDURE public."TM_CreateGroupStopOrder" TO tm_user;
GRANT EXECUTE ON PROCEDURE public."TM_CreateNegOrder" TO tm_admin;
GRANT EXECUTE ON PROCEDURE public."TM_CreateNegOrder" TO tm_user;
GRANT EXECUTE ON PROCEDURE public."CreateNewSecGroup" TO tm_admin;
GRANT EXECUTE ON PROCEDURE public."CreateNewSecGroup" TO tm_user;
GRANT EXECUTE ON PROCEDURE public."TM_CreateOrderInPool" TO tm_admin;
GRANT EXECUTE ON PROCEDURE public."TM_CreateOrderInPool" TO tm_user;
GRANT EXECUTE ON PROCEDURE public."TM_SetNewImportEvent" TO tm_admin;
GRANT EXECUTE ON PROCEDURE public."TM_SetNewImportEvent" TO tm_user;
GRANT EXECUTE ON PROCEDURE public."TM_CreateRatingAgency" TO tm_admin;
GRANT EXECUTE ON PROCEDURE public."TM_CreateRatingAgency" TO tm_user;
GRANT EXECUTE ON PROCEDURE public."CreateNewSecGroupMember" TO tm_admin;
GRANT EXECUTE ON PROCEDURE public."CreateNewSecGroupMember" TO tm_user;
GRANT EXECUTE ON PROCEDURE public."DeleteSecGroupMember" TO tm_admin;
GRANT EXECUTE ON PROCEDURE public."DeleteSecGroupMember" TO tm_user;
GRANT EXECUTE ON PROCEDURE public."TM_CreateRatingScale" TO tm_admin;
GRANT EXECUTE ON PROCEDURE public."TM_CreateRatingScale" TO tm_user;
GRANT EXECUTE ON PROCEDURE public."TM_GetCounter" TO tm_admin;
GRANT EXECUTE ON PROCEDURE public."TM_GetCounter" TO tm_user;
GRANT EXECUTE ON PROCEDURE public."TM_CreatePool" TO tm_admin;
GRANT EXECUTE ON PROCEDURE public."TM_CreatePool" TO tm_user;
GRANT EXECUTE ON PROCEDURE public."DeleteRestriction" TO tm_admin;
GRANT EXECUTE ON PROCEDURE public."DeleteRestriction" TO tm_user;
GRANT EXECUTE ON PROCEDURE public."TM_CreateStopOrder" TO tm_admin;
GRANT EXECUTE ON PROCEDURE public."TM_CreateStopOrder" TO tm_user;
GRANT EXECUTE ON PROCEDURE public."TM_AddClientToGroup" TO tm_admin;
GRANT EXECUTE ON PROCEDURE public."TM_AddClientToGroup" TO tm_user;
GRANT EXECUTE ON PROCEDURE public."TM_DeleteClientFromGroup" TO tm_admin;
GRANT EXECUTE ON PROCEDURE public."TM_DeleteClientFromGroup" TO tm_user;
GRANT EXECUTE ON PROCEDURE public."TM_DeleteGroup" TO tm_admin;
GRANT EXECUTE ON PROCEDURE public."TM_DeleteGroup" TO tm_user;


-- Устанавливаем 29 версию БД

DO
$$
BEGIN
	
	INSERT INTO public."SystemData" ("KeyName", "KeyValue")
	VALUES ('dbver', '29');

	INSERT INTO public."SystemData" ("KeyName", "KeyValue")
	VALUES ('UseStoredProc', 'y');

	INSERT INTO public."SystemData" ("KeyName", "KeyValue")
	VALUES ('dbgen_029', CURRENT_DATE);

	RAISE NOTICE 'DB has been created as version 29';
END
$$;