github.com/voedger/voedger@v0.0.0-20240520144910-273e84102129/design/schemas/air.vsql (about)

     1  -- Copyright (c) 2020-present unTill Pro, Ltd.
     2  
     3  SCHEMA air;
     4  
     5  IMPORT SCHEMA "github.com/untillpro/airs-bp3/packages/untill"
     6  
     7  -- Principles:
     8  ---- 1. The following DDLs can only be declared in WORKSPACE:
     9  ----    QUERY, COMMAND, PROJECTOR, RATE, GRANT, USE TABLE
    10  ----        ??? Incompatible to APIv2, e.g.: POST /api/rest/owner/app/<wsid>/<query_qname>/
    11  ----              maybe:  /api/rest/owner/qname_app/qname_ws/<wsid>/<query_qname>/
    12  ---- 2. The following DDLs can only be declared out of WORKSPACE:
    13  ----    TEMPLATE
    14  
    15  
    16  WORKSPACE Restaurant (
    17  
    18      -------------------------------------------------------------------------------------
    19      -- Roles
    20      --
    21      ROLE UntillPaymentsUser;
    22      ROLE LocationManager;
    23      ROLE LocationUser;
    24  
    25      -------------------------------------------------------------------------------------
    26      -- Checks
    27      --
    28      FUNCTION MyTableValidator(sys.TableRow) RETURNS void ENGINE BUILTIN;
    29      FUNCTION MyFieldsValidator(fieldA text, fieldB text) RETURNS void ENGINE BUILTIN;
    30      FUNCTION ApproxEqual(param1 float, param2 float) RETURNS boolean ENGINE BUILTIN;
    31  
    32      ALTER TABLE untill.bill ADD CHECK (MyTableValidator(row));  -- ??? row/this/current ?
    33      ALTER TABLE untill.bill ADD CONSTRAINT MyBillCheck CHECK (MyFieldsValidator(name, pcname)); -- with name
    34      ALTER TABLE untill.bill ADD CHECK (text != pcname);
    35      ALTER TABLE untill.bill ALTER name SET CHECK (name != '');
    36      ALTER TABLE untill.bill ALTER working_day SET CHECK '^[0-9]{8}$';
    37      ALTER TABLE sometable ADD CHECK (!ApproxEqual(netto, brutto));
    38  
    39      -- see also: https://www.postgresql.org/docs/15/sql-altertable.html
    40  
    41      -- Was:
    42      -- CHECK ON TABLE untill.bill IS MyTableValidator;
    43      -- CHECK MyBillCheck ON TABLE untill.bill(name text, pcname text) IS MyFieldsValidator; -- name is optional
    44      -- CHECK ON TABLE untill.bill(name text, pcname text) IS (text != pcname);
    45      -- CHECK ON FIELD name OF TABLE untill.bill IS (name != '')
    46      -- CHECK ON FIELD working_day OF TABLE untill.bill IS '^[0-9]{8}$'
    47      -- CHECK NettoBruttoCheck ON TABLE sometable(netto float, brutto float) IS (!ApproxEqual(netto, brutto));
    48  
    49      -------------------------------------------------------------------------------------
    50      -- Projectors
    51      --
    52      FUNCTION FillUPProfile(sys.Event) RETURNS void ENGINE WASM;
    53  
    54      PROJECTOR ApplyUPProfile ON COMMAND IN (air.CreateUPProfile, air.UpdateUPProfile) AS FillUPProfile; -- name is optional
    55      PROJECTOR ON COMMAND air.CreateUPProfile AS SomeFunc;
    56      PROJECTOR ON COMMAND ARGUMENT untill.QNameOrders AS SomeFunc;
    57  
    58      -------------------------------------------------------------------------------------
    59      -- Commands
    60      --
    61      FUNCTION OrdersFunc(untill.orders) RETURNS void ENGINE BUILTIN;
    62      FUNCTION PbillFunc(untill.pbill) RETURNS PbillResult ENGINE BUILTIN;
    63  
    64      COMMAND Orders(untill.orders) AS PbillFunc;
    65      COMMAND Pbill(untill.pbill) AS PbillFunc;
    66  
    67      -------------------------------------------------------------------------------------
    68      -- Comments
    69      --
    70  
    71      -- Declare comments
    72      COMMENT BackofficeComment "This is a backoffice table";
    73  
    74      -- Apply comments
    75      -- See ALTER TABLE ... SET ( storage_parameter [= value], https://www.postgresql.org/docs/15/sql-altertable.html
    76      ALTER QUERY TransactionHistory SET Comment=BackofficeComment
    77      ALTER QUERY TransactionHistory SET Comment='Transaction History', Tags=[Pos, Table];
    78      ALTER TABLE ALL IN (Currency, Users) SET Comment=BackofficeComment
    79      ALTER TABLE ALL WITH TAG Backoffice SET Comment=BackofficeComment
    80  
    81      -- was:
    82      -- COMMENT BackofficeComment "This is a backoffice table";
    83      --
    84      -- COMMENT ON QUERY TransactionHistory IS 'Transaction History'; -- Do we allow inline values?
    85      -- COMMENT ON QUERY IN (TransactionHistory, ...) IS 'Transaction History';
    86      -- COMMENT ON ALL QUERIES WITH TAG Backoffice IS BackofficeComment;
    87  
    88      TYPE QueryResellerInfoResult (
    89          reseller_phone text,
    90          reseller_company text,
    91          reseller_email text,
    92          reseller_website text
    93      ) WITH Comment='Contains information about Reseller';
    94  
    95      -------------------------------------------------------------------------------------
    96      -- Rates
    97      --
    98  
    99      -- Declare rate
   100      RATE BackofficeFuncRate 100 PER MINUTE PER IP;
   101  
   102      -- Apply rate
   103      ALTER QUERY TransactionHistory SET Rate=BackofficeFuncRate;
   104      ALTER QUERY TransactionHistory SET Rate=101 PER MINUTE PER IP;
   105  
   106      --was:
   107      --RATE ON QUERY TransactionHistory IS BackofficeFuncLimit;
   108      --RATE ON QUERY TransactionHistory IS 101 PER MINUTE PER IP;  -- Do we allow inline values?
   109  
   110      -------------------------------------------------------------------------------------
   111      -- Tags
   112      --
   113  
   114      -- Declare tags
   115      TAG Backoffice;
   116      TAG Pos;
   117      TAG Collection;
   118  
   119      -- Apply tags
   120      ALTER TABLE bill SET Tags=[Pos]; -- not possible to declare new tag here
   121      ALTER COMMAND Orders ADD TAG Pos ADD TAG Backoffice; -- ??? adds tags
   122      ALTER QUERY QueryResellerInfo SET Tags=[Resseler, Backoffice];
   123      ALTER TABLE ALL WITH TAG Backoffice SET Tags=[sys.Collection]
   124  
   125      --was:
   126      --TAG ON TABLE bill IS Pos;
   127      --TAG ON COMMAND Orders IS Pos; -- inline values are not possible because no way to figure out if Pos is a new tag or reference
   128      --TAG ON QUERY QueryResellerInfo IS [Resseler, Backoffice];
   129  
   130      -- Collection is applied to all tables with tag "sys.Collection"
   131      -- TAG ON ALL TABLES WITH TAG Backoffice IS sys.Collection;
   132  
   133  
   134      -------------------------------------------------------------------------------------
   135      -- Sequences
   136      --
   137  
   138      SEQUENCE bill_numbers int START WITH 1;
   139      SEQUENCE bill_numbers int MINVALUE 1; -- same as previous
   140      SEQUENCE SomeDecrementSeqneuce int MAXVALUE 1000000 INCREMENT BY -1;
   141  
   142      -------------------------------------------------------------------------------------
   143      -- Types
   144      --
   145  
   146      TYPE TransactionHistoryParams (
   147          BillIDs text NOT NULL,
   148          EventTypes text NOT NULL,
   149      );
   150  
   151      TYPE TransactionHistoryResult (
   152          Offset offset NOT NULL,
   153          EventType int64 NOT NULL,
   154          Event text NOT NULL,
   155      );
   156  
   157      -------------------------------------------------------------------------------------
   158      -- Queries
   159      --
   160  
   161      FUNCTION MyFunc(reseller_id text) RETURNS QueryResellerInfoResult ENGINE WASM;
   162  
   163      QUERY QueryResellerInfo(reseller_id text) RETURNS QueryResellerInfoResult AS MyFunc
   164          WITH Rate=BackofficeFuncRate,
   165          Comment='Transaction History',
   166          Tags=[PosTag1, PosTag2];
   167  
   168      QUERY QueryResellerInfo AS MyFunc --same as previous, parameters maybe omitted
   169          WITH Rate=BackofficeFuncRate,
   170          Comment='Transaction History',
   171          Tags=[PosTag1, PosTag2];
   172  
   173      -------------------------------------------------------------------------------------
   174      -- Tables
   175      --
   176  
   177      -- Every workspace Restaurant has all tables from schema `untill`
   178      USE TABLE untill.*;
   179  
   180      -- ??? Do we need to USE something else besides TABLEs?
   181  
   182      TABLE air_table_plan OF CDOC (
   183          fstate int,
   184          name text,
   185          ml_name text,
   186          num int,
   187          width int,
   188          height int
   189      )
   190  
   191      -- see also: untill-tables.vsql
   192  
   193      -------------------------------------------------------------------------------------
   194      -- ACLs
   195      --
   196      GRANT ALL ON ALL TABLES WITH TAG untill.Backoffice TO LocationManager;
   197      GRANT INSERT,UPDATE ON ALL TABLES WITH TAG sys.ODoc TO LocationUser;
   198      GRANT SELECT ON TABLE untill.orders TO LocationUser;
   199      GRANT EXECUTE ON COMMAND PBill TO LocationUser;
   200      GRANT EXECUTE ON COMMAND Orders TO LocationUser;
   201      GRANT EXECUTE ON QUERY TransactionHistory TO LocationUser;
   202      GRANT EXECUTE ON ALL QUERIES WITH TAG PosTag TO LocationUser;
   203  
   204      -------------------------------------------------------------------------------------
   205      -- Singletones
   206      --
   207      TABLE Restaurant OF SINGLETONE (
   208          WorkStartTime text,
   209          DefaultCurrency int64,
   210          NextCourseTicketLayout int64,
   211          TransferTicketLayout int64,
   212          DisplayName text,
   213          Country text,
   214          City text,
   215          ZipCode text,
   216          Address text,
   217          PhoneNumber text,
   218          VATNumber text,
   219          ChamberOfCommerce text,
   220      )
   221  
   222      -------------------------------------------------------------------------------------
   223      -- Views
   224      --
   225  
   226  
   227      -- ??? AS or IS
   228      VIEW HourlySalesView(
   229          yyyymmdd,
   230          hour,
   231          total,
   232          count
   233      ) AS SELECT
   234          working_day,
   235          EXTRACT(hour from ord_datetime),
   236          (select sum(price * quantity) from order_item),
   237          (select sum(quantity) from order_item),
   238          from untill.orders
   239      WITH Comment=PosComment, PrimaryKey='(yyyymmdd, hour), asdas';
   240  
   241      VIEW HourlySalesView AS SELECT
   242          working_day,
   243          EXTRACT(hour from ord_datetime) as hour,
   244          (select open_datetime from bill where id=orders.id_bill),
   245          (select close_datetime from bill where id=orders.id_bill),
   246          (select sum(price * quantity) from order_item) as total, -- available for child tables
   247          (select sum(quantity) from order_item) as count,
   248          from untill.orders
   249      WITH Comment=PosComment, PrimaryKey='(yyyymmdd, hour), asdas';
   250  
   251      VIEW BillsCount AS SELECT count(*) from bill
   252      VIEW OrdersCount AS SELECT count(*) from orders
   253  
   254      -- same as previous, but with BILL joined
   255      VIEW HourlySalesView AS SELECT
   256          working_day,
   257          EXTRACT(hour from ord_datetime) as hour,
   258          open_datetime,
   259          close_datetime,
   260          (select sum(price * quantity) from order_item) as total, -- available for child tables
   261          (select sum(quantity) from order_item) as count,
   262          from untill.orders
   263          join bill on id_bill=bill.id
   264      WITH Comment=PosComment, PrimaryKey='(yyyymmdd, hour), asdas';
   265  
   266  --    VIEW HourlySalesView AS SELECT
   267          ---working_day,
   268          --EXTRACT(hour from ord_datetime) as hour, -- alias
   269          --SUM(price * quantity) as total,
   270          --SUM(quantity) as count
   271          --from untill.orders
   272              --join order_item on order_item.id_orders=orders.id
   273          --group by working_day, hour
   274      --WITH Comment=PosComment, PrimaryKey='(yyyymmdd, hour), asdas';
   275  
   276  
   277      VIEW XZReports(
   278          Year int32,
   279          Month int32,
   280          Day int32,
   281          Kind int32,
   282          Number int32,
   283          XZReportWDocID id
   284      ) AS RESULT OF UpdateXZReportsView
   285      WITH PrimaryKey='(Year), Month, Day, Kind, Number'
   286  
   287      -- see also air-views.vsql
   288  
   289  )
   290  
   291  -------------------------------------------------------------------------------------
   292  -- Child Workspaces
   293  --
   294  WORKSPACE Resellers {
   295  
   296      ROLE ResellersAdmin;
   297  
   298      -- Child workspace
   299      WORKSPACE Reseller {
   300          ROLE UntillPaymentsReseller;
   301          ROLE AirReseller;
   302          USE Table PaymentsProfile
   303      }
   304  }
   305  
   306  -------------------------------------------------------------------------------------
   307  -- WORKSPACE Templates
   308  --
   309  TEMPLATE demo OF WORKSPACE air.Restaurant SOURCE wsTemplate_demo;
   310  TEMPLATE resdemo OF WORKSPACE untill.Resellers SOURCE wsTemplate_demo_resellers;
   311  
   312