Oracle Rest API多行

发布于 2025-01-18 01:30:43 字数 1356 浏览 5 评论 0原文

我必须创建一种GET类型的API,并且必须连接两个表。

例如,我有以下表:

表1:

  • customer_id:{001}
  • first_name:{f_name}
  • last_name:{l:name}

表2:

  • customer_id:{001} {001} {001}
  • 街道:
  • zip_code:{1234} {1234}
  • 城市:{xxxx} {xxxx}
  • country:{xx} {xx} {xx}

如果我连接两个表,我会得到以下结果:

{
        "customer_id": 001,
        "first_name": "f_name",
        "last_name": "l_name",
        "street": "A street",
        "zip_code": "1234",
        "city": "xxxx",
        "country": xx       
    }
{
        "customer_id": 001,
        "first_name": "f_name",
        "last_name": "l_name",
        "street": "B street",
        "zip_code": "1234",
        "city": "xxxx",
        "country": xx       
    }

这是beacuse table2 thable thable the table2 cubtist2 cubtistion_id:001 ”。

但是我希望这种结果:

{
    "customer_id": 001,
    "first_name": "f_name",
    "last_name": "l_name",
    "address": [
        {
            "street": "A steet",
            "zip_code": "1234",
            "city": "xxxx",
            "country": xx
        },
        {
            "street": "B street",
            "zip_code": "1234",
            "city": "xxxx",
            "country": xx
        }
    ]
}

似乎一个简单的查询在这里无法使用。是否有人理想我应该如何创建这种GET类型的API?

I have to create a GET type of API and I have to connect two tables.

For example, I have the following tables:

Table 1:

  • customer_id: {001}
  • first_name: {f_name}
  • last_name: {l:name}

Table 2:

  • customer_id: {001} {001}
  • street: {A street} {B street}
  • zip_code: {1234} {1234}
  • city: {xxxx} {xxxx}
  • country: {xx} {xx}

If I connect the two tables I get the following result:

{
        "customer_id": 001,
        "first_name": "f_name",
        "last_name": "l_name",
        "street": "A street",
        "zip_code": "1234",
        "city": "xxxx",
        "country": xx       
    }
{
        "customer_id": 001,
        "first_name": "f_name",
        "last_name": "l_name",
        "street": "B street",
        "zip_code": "1234",
        "city": "xxxx",
        "country": xx       
    }

This is beacuse the table2 has two rows with the customer_id:"001".

But I want this kind of result:

{
    "customer_id": 001,
    "first_name": "f_name",
    "last_name": "l_name",
    "address": [
        {
            "street": "A steet",
            "zip_code": "1234",
            "city": "xxxx",
            "country": xx
        },
        {
            "street": "B street",
            "zip_code": "1234",
            "city": "xxxx",
            "country": xx
        }
    ]
}

It seems like a simple query won't work here. Has anybody ideal how should I create this GET type of API?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

跨年 2025-01-25 01:30:43

我们将在查询中使用 CURSOR。

首先是表和数据:

CREATE TABLE CUST (
    CUSTOMER_ID INTEGER,
    FIRST_NAME  VARCHAR2(20),
    LAST_NAME   VARCHAR2(20)
);

ALTER TABLE CUST 
    ADD CONSTRAINT CUST_PK PRIMARY KEY ( 
    CUSTOMER_ID  
    );

CREATE TABLE ADDY (
    CUSTOMER_ID INTEGER,
    STREET      VARCHAR2(20),
    ZIPCODE     VARCHAR2(5),
    CITY        VARCHAR2(20),
    COUNTRY     VARCHAR2(20)
);

ALTER TABLE ADDY 
    ADD CONSTRAINT CUST_ID FOREIGN KEY 
    ( 
     CUSTOMER_ID
    ) 
    REFERENCES CUST 
    ( 
    CUSTOMER_ID 
    ) 
    ON DELETE CASCADE 
    NOT DEFERRABLE NOVALIDATE 
;

insert into CUST values (1, 'Jeff', 'Smith');
insert into addy values (1, 'Chowning Place', '00001', 'Blacksburg', 'USA');
insert into addy values (1, 'Inkberry Ct', '00002', 'Apex', 'USA');

然后我们可以构建 REST API。

我们的模板是 cust_address/:id,我们将对其执行 GET 操作。

GET 处理程序背后的 SQL:

select customer_id,
       first_name,
       last_name,
       CURSOR(
                select street,
                       zipcode,
                       city,
                       country
                from ADDY O
                where C.customer_id = O.customer_ID
       ) address
from CUST C
where customer_id = :id

将所有这些放在一起,我们的 TEMPLATE/HANDLER 组合在 ORDS 中定义如下(如 SQL Developer Web 中所示)

在此处输入图像描述

然后我们调用 API - 就在我们的浏览器:

在此处输入图像描述

您需要关闭集合查询的分页(设置为 0),或者需要将处理程序定义为集合项。

免责声明:我是 Oracle 员工,也是 Oracle REST 数据服务的产品经理。

We're going to use a CURSOR in the query.

First the tables and data:

CREATE TABLE CUST (
    CUSTOMER_ID INTEGER,
    FIRST_NAME  VARCHAR2(20),
    LAST_NAME   VARCHAR2(20)
);

ALTER TABLE CUST 
    ADD CONSTRAINT CUST_PK PRIMARY KEY ( 
    CUSTOMER_ID  
    );

CREATE TABLE ADDY (
    CUSTOMER_ID INTEGER,
    STREET      VARCHAR2(20),
    ZIPCODE     VARCHAR2(5),
    CITY        VARCHAR2(20),
    COUNTRY     VARCHAR2(20)
);

ALTER TABLE ADDY 
    ADD CONSTRAINT CUST_ID FOREIGN KEY 
    ( 
     CUSTOMER_ID
    ) 
    REFERENCES CUST 
    ( 
    CUSTOMER_ID 
    ) 
    ON DELETE CASCADE 
    NOT DEFERRABLE NOVALIDATE 
;

insert into CUST values (1, 'Jeff', 'Smith');
insert into addy values (1, 'Chowning Place', '00001', 'Blacksburg', 'USA');
insert into addy values (1, 'Inkberry Ct', '00002', 'Apex', 'USA');

Then we can build the REST API.

Our template is cust_address/:id, and we're going to do a GET on that.

The SQL behind the GET handler:

select customer_id,
       first_name,
       last_name,
       CURSOR(
                select street,
                       zipcode,
                       city,
                       country
                from ADDY O
                where C.customer_id = O.customer_ID
       ) address
from CUST C
where customer_id = :id

Putting that all together, our TEMPLATE/HANDLER combo is defined like this in ORDS (as shown in SQL Developer Web)

enter image description here

Then we call the API - just in our browser:

enter image description here

You'll need to either turn paging off for a Collection Query (set to 0), or you'll need to define your handler as a Collection Item.

Disclaimer: I'm an Oracle employee and the product manager for Oracle REST Data Services.

尸血腥色 2025-01-25 01:30:43

这可以使用 GET API 的简单 SELECT 语句来创建。您需要使用 CURSOR 命令在 JSON 对象中创建嵌套数组。下面是您需要的示例。

下面示例中的 :p_customer_id 是来自 API 的某种输入参数。

SELECT t1.customer_id,
       t1.first_name,
       t1.last_name,
       CURSOR (SELECT t2.street,
                      t2.zip_code,
                      t2.city,
                      t2.country
                 FROM table2 t2
                WHERE t2.customer_id = t1.customer_id)    AS address
  FROM table1 t1
 WHERE t1.customer_id = :p_customer_id;

This can be created using a simple SELECT statement for your GET API. You need to use the CURSOR command to create nested arrays within your JSON object. An example of what you need can be seen below.

:p_customer_id in the example below is some sort of input parameter from your API.

SELECT t1.customer_id,
       t1.first_name,
       t1.last_name,
       CURSOR (SELECT t2.street,
                      t2.zip_code,
                      t2.city,
                      t2.country
                 FROM table2 t2
                WHERE t2.customer_id = t1.customer_id)    AS address
  FROM table1 t1
 WHERE t1.customer_id = :p_customer_id;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文