Fun 语言的 O/R Mapping 和 OrmRPC ..

作者: 一了 <1liao3@funlang.org>
日期: 2019-08-08

O/R Mapping RPC

实施步骤

  1. 使用 SQL.exe 工具, 设置数据库连接串, 按 F8
  2. 使用 CodeAuto.exe 工具, 打开 SQL.exe 目录下的 sql.exe-default.xobject
  3. 在 CodeAuto.exe 中修改表/字段/关系名称, 完成 O/R Mapping 映射, 保存
  4. 在 SQL.exe 中 按 F9, 生成 sql.exe-default.json

OrmInit

在 json 根目录下建立 OrmInit 节点, 保存 O/R Mapping Schema 初始化信息

{
    "init-connectionString": "数据库连接串",
    "init-options": {
        //...
    },
    "OrmInit": {
        //...
    }
}

OrmSelectAutoId

{
    "init-connectionString": "数据库连接串",
    "init-options": {
        //...
    },
    "OrmInit": {
        //...
    },
    // 用于取回自增字段 autoId 的语句, 返回必须命名为 autoId
    "OrmSelectAutoId": "SELECT SCOPE_IDENTITY() as autoId"
}

OrmGet

{
    "method": "OrmGet",
    "args": {
        "ObjectName1": {
            // key-value list, may be empty (means get all)
        }, //...
        "ObjectNameX": {
            // key-value list, may be empty (means get all)
        }
    }
}

OrmSave

{
    "method": "OrmSave",
    "args": {
        "ObjectName1": [
            {
               // key-value list
            },
            // ...
        ], //...
        "ObjectNameX": [
            // Object list
        ]
    }
}

OrmUpdate

{
    "method": "OrmUpdate",
    "args": {
        "ObjectName1": {
            "Set": {
                // key-value list
            },
            "Where": {
                // key-value list
            }
        }, //...
        "ObjectNameX": {
            // Set and Where list
        }
    }
}

OrmDelete

{
    "method": "OrmDelete",
    "args": {
        "ObjectName1": {
            // key-value list
        }, //...
        "ObjectNameX": {
            // key-value list
        }
    }
}

有关查询条件

ORM 支持 NOT, AND, OR, IS [NOT] NULL, [NOT] LIKE, [NOT] BETWEEN AND, [NOT] IN, 比较运算符等, 如下:

[
  Order:       [
    CustomerID : 'VINET',
    $OR        : [
      CustomerID : ">= VI'NET",
      EmployeeID : 'NOT NULL',
      ShipAddress: 'not like a%',
      Freight    : 'not between 0 AND 1',
      OrderDate  : "between '1996-7-4' AND '2096-7-4'",
      $NOT       : [
        CustomerID : "<> VI'NET",
        Freight    : 'not in (0, 1)',
        $AND       : [
          Freight    : '<> 1 + 2',
          EmployeeID : 'NULL',
          CustomerID : "not in ('VINET')"
        ]
      ]
    ]
    , $Set_quantifier: 'DISTINCT TOP 10 '
    , $Order: [EmployeeID:1, Freight:-1]
  ],
  Customer: [CustomerID: 'VINET'],
  Region:   []
]
{
 "Order": {
    "CustomerID": "VINET",
    "$OR": {
        "CustomerID": ">= VI'NET",
        "EmployeeID": "NOT NULL",
        "ShipAddress": "not like a%",
        "Freight": "not between 0 AND 1",
        "OrderDate": "between '1996-7-4' AND '2096-7-4'",
        "$NOT": {
            "CustomerID": "<> VI'NET",
            "Freight": "not in (0, 1)",
            "$AND": {
                "Freight": "<> 1 + 2",
                "EmployeeID": "NULL",
                "CustomerID": "not in ('VINET')"
            }
        }
    },
    "$Set_quantifier": "DISTINCT TOP 10 ",
    "$Order": {
        "EmployeeID": 1,
        "Freight": -1
    }
 },
 "Customer": {
  "CustomerID": "VINET"
 },
 "Region": [
 ]
}
SELECT DISTINCT TOP 10 *
FROM "Orders"
WHERE
        "CustomerID" = 'VINET' AND (
                "CustomerID" >= 'VI''NET' OR
                "EmployeeID" IS NOT NULL OR
                "ShipAddress" NOT LIKE 'a%' OR
                "Freight" NOT BETWEEN 0 AND 1 OR
                "OrderDate" BETWEEN '1996-7-4' AND '2096-7-4' OR NOT (
                        "CustomerID" <> 'VI''NET' AND
                        "Freight" NOT IN (0, 1) AND
                                "Freight" <> 1 + 2 AND
                                "EmployeeID" IS NULL AND
                                "CustomerID" NOT IN ('VINET')
                )
        )
ORDER BY "EmployeeID", "Freight" DESC

SELECT *
FROM "Customers"
WHERE
        "CustomerID" = 'VINET'

SELECT "RegionDescription", "RegionID"
FROM "Region"