github.com/voedger/voedger@v0.0.0-20240520144910-273e84102129/design/schemas/progress/air3-after-meeting-20032023.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  ---- 2. The following DDLs can only be declared out of WORKSPACE:
    11  ----    TEMPLATE
    12  
    13  -- Conclusion:
    14  --  - PROCEDURE - remove
    15  --  - AS - remove
    16  --  - WITH Key=Value
    17  --  - Inline declaration not possible
    18  --  - Tag - value not possible
    19  --  - WITH Tags=[]
    20  
    21  WORKSPACE Restaurant (
    22  
    23      -------------------------------------------------------------------------------------
    24      -- Roles
    25      --
    26      ROLE UntillPaymentsUser;
    27      ROLE LocationManager;
    28      ROLE LocationUser;
    29  
    30      -------------------------------------------------------------------------------------
    31      -- Checks
    32      --
    33      FUNCTION MyTableValidator(sys.TableRow) RETURNS void ENGINE BUILTIN;
    34      FUNCTION MyFieldsValidator(fieldA text, fieldB text) RETURNS void ENGINE BUILTIN;
    35      FUNCTION ApproxEqual(param1 float, param2 float) RETURNS boolean ENGINE BUILTIN;
    36  
    37      CHECK ON TABLE untill.bill IS MyTableValidator;
    38  
    39      CHECK MyBillCheck ON TABLE untill.bill(name text, pcname text) IS MyFieldsValidator; -- name is optional
    40      CHECK ON TABLE untill.bill(name text, pcname text) IS (text != pcname);
    41      CHECK ON FIELD name OF TABLE untill.bill IS (name != '')
    42      CHECK ON FIELD working_day OF TABLE untill.bill IS '^[0-9]{8}$'
    43      CHECK NettoBruttoCheck ON TABLE sometable(netto float, brutto float) IS (!ApproxEqual(netto, brutto));
    44      -------------------------------------------------------------------------------------
    45      -- Projectors
    46      --
    47      FUNCTION FillUPProfile(sys.Event) RETURNS void ENGINE WASM;
    48  
    49      PROJECTOR ApplyUPProfile ON COMMAND IN (air.CreateUPProfile, air.UpdateUPProfile) IS FillUPProfile; -- name is optional
    50      PROJECTOR ON COMMAND air.CreateUPProfile IS SomeFunc;
    51      PROJECTOR ON COMMAND ARGUMENT untill.QNameOrders IS SomeFunc;
    52  
    53      -------------------------------------------------------------------------------------
    54      -- Commands
    55      --
    56      FUNCTION OrdersFunc(untill.orders) RETURNS void ENGINE BUILTIN;
    57      FUNCTION PbillFunc(untill.pbill) RETURNS PbillResult ENGINE BUILTIN;
    58  
    59      COMMAND Orders(untill.orders) IS PbillFunc;
    60      COMMAND Pbill(untill.pbill) IS PbillFunc;
    61  
    62      -------------------------------------------------------------------------------------
    63      -- Comments
    64      --
    65  
    66      -- Declare comments
    67      COMMENT BackofficeComment "This is a backoffice table";
    68  
    69      -- Apply comments
    70      COMMENT ON QUERY TransactionHistory IS 'Transaction History'; -- Do we allow inline values?
    71      COMMENT ON QUERY IN (TransactionHistory, ...) IS 'Transaction History';
    72      COMMENT ON ALL QUERIES WITH TAG Backoffice IS BackofficeComment;
    73  
    74      TYPE QueryResellerInfoResult (
    75          reseller_phone text,
    76          reseller_company text,
    77          reseller_email text,
    78          reseller_website text
    79      ) WITH Comment='Contains information about Reseller';
    80  
    81      -------------------------------------------------------------------------------------
    82      -- Rates and Limits
    83      --
    84  
    85      -- Declare rate
    86      RATE BackofficeFuncLimit 100 PER MINUTE PER IP;
    87  
    88      -- Apply rate
    89      RATE ON QUERY TransactionHistory IS BackofficeFuncLimit;
    90      RATE ON QUERY TransactionHistory IS 101 PER MINUTE PER IP;  -- Do we allow inline values?
    91  
    92      -------------------------------------------------------------------------------------
    93      -- Tags
    94      --
    95  
    96      -- Declare tags
    97      TAG Backoffice;
    98      TAG Pos;
    99      TAG Collection;
   100  
   101      -- Apply tags
   102      TAG ON TABLE bill IS Pos;
   103      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
   104      TAG ON QUERY QueryResellerInfo IS [Resseler, Backoffice];
   105  
   106      -- Collection is applied to all tables with tag "sys.Collection"
   107      TAG ON ALL TABLES WITH TAG Backoffice IS sys.Collection;
   108  
   109      -------------------------------------------------------------------------------------
   110      -- Sequences
   111      --
   112  
   113      SEQUENCE bill_numbers int START WITH 1;
   114      SEQUENCE bill_numbers int MINVALUE 1; -- same as previous
   115      SEQUENCE SomeDecrementSeqneuce int MAXVALUE 1000000 INCREMENT BY -1;
   116  
   117      -------------------------------------------------------------------------------------
   118      -- Types
   119      --
   120  
   121      TYPE TransactionHistoryParams (
   122          BillIDs text NOT NULL,
   123          EventTypes text NOT NULL,
   124      );
   125  
   126      TYPE TransactionHistoryResult (
   127          Offset offset NOT NULL,
   128          EventType int64 NOT NULL,
   129          Event text NOT NULL,
   130      );
   131  
   132      -------------------------------------------------------------------------------------
   133      -- Queries
   134      --
   135  
   136      FUNCTION MyFunc(reseller_id text) RETURNS QueryResellerInfoResult ENGINE WASM;
   137  
   138      QUERY QueryResellerInfo(reseller_id text) RETURNS QueryResellerInfoResult IS MyFunc
   139          WITH Rate=BackofficeFuncRate
   140          AND Comment='Transaction History'
   141          AND Tags=[PosTag1, PosTag2];
   142  
   143      -------------------------------------------------------------------------------------
   144      -- Tables
   145      --
   146  
   147      -- Every workspace Restaurant has all tables from schema `untill`
   148      USE TABLE untill.*;
   149  
   150      -- ??? Do we need to USE something else besides TABLEs?
   151  
   152      TABLE air_table_plan OF CDOC (
   153          fstate int,
   154          name text,
   155          ml_name text,
   156          num int,
   157          width int,
   158          height int
   159      )
   160  
   161      -- see also: untill-tables.vsql
   162  
   163      -------------------------------------------------------------------------------------
   164      -- ACLs
   165      --
   166      GRANT ALL ON ALL TABLES WITH TAG untill.Backoffice TO LocationManager;
   167      GRANT INSERT,UPDATE ON ALL TABLES WITH TAG sys.ODoc TO LocationUser;
   168      GRANT SELECT ON TABLE untill.orders TO LocationUser;
   169      GRANT EXECUTE ON COMMAND PBill TO LocationUser;
   170      GRANT EXECUTE ON COMMAND Orders TO LocationUser;
   171      GRANT EXECUTE ON QUERY TransactionHistory TO LocationUser;
   172      GRANT EXECUTE ON ALL QUERIES WITH TAG PosTag TO LocationUser;
   173  
   174      -------------------------------------------------------------------------------------
   175      -- Singletones
   176      --
   177      TABLE Restaurant OF SINGLETONE (
   178          WorkStartTime text,
   179          DefaultCurrency int64,
   180          NextCourseTicketLayout int64,
   181          TransferTicketLayout int64,
   182          DisplayName text,
   183          Country text,
   184          City text,
   185          ZipCode text,
   186          Address text,
   187          PhoneNumber text,
   188          VATNumber text,
   189          ChamberOfCommerce text,
   190      )
   191  
   192      -------------------------------------------------------------------------------------
   193      -- Views
   194      --
   195  
   196  
   197      -- ??? AS or IS
   198      VIEW HourlySalesView(
   199          yyyymmdd text,
   200          hour int,
   201          total int,
   202          count int,
   203          primary key((yyyymmdd, hour))
   204      ) AS SELECT
   205          working_day as yyyymmdd,
   206          EXTRACT(hour from ord_datetime) as hour,
   207          SUM(price * quantity) as total,
   208          SUM(quantity) as count
   209          from untill.orders
   210              join order_item on order_item.id_orders=orders.id
   211          group by working_day, hour
   212      WITH Comment IS PosComment;
   213  
   214      VIEW XZReports(
   215          Year int32,
   216          Month int32,
   217          Day int32,
   218          Kind int32,
   219          Number int32,
   220          XZReportWDocID id,
   221          PRIMARY KEY((Year), Month, Day, Kind, Number)
   222      ) AS RESULT OF UpdateXZReportsView
   223  
   224      -- see also air-views.vsql
   225  
   226  )
   227  
   228  -------------------------------------------------------------------------------------
   229  -- Child Workspaces
   230  --
   231  WORKSPACE Resellers {
   232  
   233      ROLE ResellersAdmin;
   234  
   235      -- Child workspace
   236      WORKSPACE Reseller {
   237          ROLE UntillPaymentsReseller;
   238          ROLE AirReseller;
   239          USE Table PaymentsProfile
   240      }
   241  }
   242  
   243  -------------------------------------------------------------------------------------
   244  -- WORKSPACE Templates
   245  --
   246  TEMPLATE demo OF WORKSPACE air.Restaurant WITH SOURCE wsTemplate_demo;
   247  TEMPLATE resdemo OF WORKSPACE untill.Resellers WITH SOURCE wsTemplate_demo_resellers;
   248  
   249