Oracle ORDB 分层数据

发布于 2024-08-07 03:23:30 字数 3314 浏览 8 评论 0原文

我正在尝试使用 Oracle 对象关系功能创建一些分层数据。

我已经定义了一个“帖子”如下:

create type Post as object (title varchar(20), body varchar(2000), 
  author varchar(200), parent REF Post,
  MEMBER FUNCTION numReplies RETURN NUMBER,
  PRAGMA RESTRICT_REFERENCES(numReplies, WNDS)
);
/

create table Posts of Post;

现在我想编写函数 numReplies 来找出有多少帖子以“self”帖子作为父帖子:

create or replace type body Post AS

MEMBER FUNCTION numReplies RETURN number IS
i INT;
BEGIN 
  SELECT COUNT(*) INTO i FROM Posts p WHERE p.parent = SELF;
  RETURN i;
END;

END;
/

但是我收到编译错误:

Warning: Type Body created with compilation errors.

SQL> show errors;
Errors for TYPE BODY POST:

LINE/COL ERROR
-------- -----------------------------------------------------------------
6/3      PL/SQL: SQL Statement ignored
6/56     PL/SQL: ORA-00932: inconsistent datatypes: expected TSTER123.POST
         got REF TSTER123.POST

我尝试在中执行 REF(SELF) where 子句和我得到相同的错误消息。我也尝试过 REF(p.parent) (无论如何都没有意义),但收到错误:

LINE/COL ERROR
-------- -----------------------------------------------------------------
6/3      PL/SQL: SQL Statement ignored
6/49     PL/SQL: ORA-00904: "P"."PARENT": invalid identifier

我想使用 OR 功能(它是一个类项目),所以我不想诉诸添加 ID专栏发布并使用它。我怎样才能实现这个目标?

注意:以下查询有效,我只是无法让它在使用 SELF 的函数中工作。

SELECT title, (select count(*) from Posts p2 where p2.parent = REF(p))
FROM Posts p;

编辑:

好的,我已经编译它了,但是我没有得到我期望的数据。

这是我使用过的一些虚拟数据:

insert into Posts values ('foo', 'bar', 'tyler', null);
insert into Posts values ('hello world', 'bar', 'jatin', (select REF(p) FROM Posts p where p.title = 'foo'));
insert into Posts values ('bark', 'asd', 'tom', (select REF(p) FROM Posts p where p.title = 'hello world'));
insert into Posts values ('friendly', 'hgfags', 'tyler', (select REF(p) FROM Posts p where p.title = 'foo'));

这是编译的内容:

create or replace type body Post AS
MEMBER FUNCTION numReplies RETURN number IS
i INT;
BEGIN 
  SELECT COUNT(*) INTO i FROM Posts p WHERE DEREF(p.parent) = SELF;
  RETURN i;
END;
END;
/

但是,我没有得到正确的结果:

SQL> SELECT title, (select count(*) from Posts p2 where p2.parent = REF(p)) From Posts p;

TITLE                (SELECTCOUNT(*)FROMPOSTSP2WHEREP2.PARENT=REF(P))
-------------------- ------------------------------------------------
foo                                                                 2
hello world                                                         1
bark                                                                0
friendly                                                            0

SQL> select title, p.numReplies() from Posts p;

TITLE                P.NUMREPLIES()
-------------------- --------------
foo                               0
hello world                       1
bark                              0
friendly                          0

如果我使用这个:

create or replace type body Post AS
MEMBER FUNCTION numReplies RETURN number IS
i INT;
BEGIN 
  SELECT COUNT(*) INTO i FROM Posts p WHERE DEREF(p.parent).title = SELF.title;
  RETURN i;
END;
END;
/

我得到了预期的结果,但我要求每个帖子都有一个唯一的标题。

I am trying to create some hierarchical data using Oracles object relation features.

I have defined a "Post" as follows:

create type Post as object (title varchar(20), body varchar(2000), 
  author varchar(200), parent REF Post,
  MEMBER FUNCTION numReplies RETURN NUMBER,
  PRAGMA RESTRICT_REFERENCES(numReplies, WNDS)
);
/

create table Posts of Post;

Now I want to write the function numReplies to find out how many posts have the "self" post as the parent:

create or replace type body Post AS

MEMBER FUNCTION numReplies RETURN number IS
i INT;
BEGIN 
  SELECT COUNT(*) INTO i FROM Posts p WHERE p.parent = SELF;
  RETURN i;
END;

END;
/

But I get a compilation error:

Warning: Type Body created with compilation errors.

SQL> show errors;
Errors for TYPE BODY POST:

LINE/COL ERROR
-------- -----------------------------------------------------------------
6/3      PL/SQL: SQL Statement ignored
6/56     PL/SQL: ORA-00932: inconsistent datatypes: expected TSTER123.POST
         got REF TSTER123.POST

I have tried doing REF(SELF) in the where clause and I get the same error message. I have also tried REF(p.parent) (which doesn't make sense anyways) and I get the error:

LINE/COL ERROR
-------- -----------------------------------------------------------------
6/3      PL/SQL: SQL Statement ignored
6/49     PL/SQL: ORA-00904: "P"."PARENT": invalid identifier

I want to use the OR features (it's a class project) so I don't want to resort to adding an ID column to post and using that. How can I achieve this?

NOTE: the following query works, I just can't get it to work in a function using SELF.

SELECT title, (select count(*) from Posts p2 where p2.parent = REF(p))
FROM Posts p;

EDIT:

OK, I've gotten it to compile, but I'm not getting the data I expect.

Here is some dummy data I've used:

insert into Posts values ('foo', 'bar', 'tyler', null);
insert into Posts values ('hello world', 'bar', 'jatin', (select REF(p) FROM Posts p where p.title = 'foo'));
insert into Posts values ('bark', 'asd', 'tom', (select REF(p) FROM Posts p where p.title = 'hello world'));
insert into Posts values ('friendly', 'hgfags', 'tyler', (select REF(p) FROM Posts p where p.title = 'foo'));

And here is what compiles:

create or replace type body Post AS
MEMBER FUNCTION numReplies RETURN number IS
i INT;
BEGIN 
  SELECT COUNT(*) INTO i FROM Posts p WHERE DEREF(p.parent) = SELF;
  RETURN i;
END;
END;
/

But, I'm not getting the correct results:

SQL> SELECT title, (select count(*) from Posts p2 where p2.parent = REF(p)) From Posts p;

TITLE                (SELECTCOUNT(*)FROMPOSTSP2WHEREP2.PARENT=REF(P))
-------------------- ------------------------------------------------
foo                                                                 2
hello world                                                         1
bark                                                                0
friendly                                                            0

SQL> select title, p.numReplies() from Posts p;

TITLE                P.NUMREPLIES()
-------------------- --------------
foo                               0
hello world                       1
bark                              0
friendly                          0

If I use this:

create or replace type body Post AS
MEMBER FUNCTION numReplies RETURN number IS
i INT;
BEGIN 
  SELECT COUNT(*) INTO i FROM Posts p WHERE DEREF(p.parent).title = SELF.title;
  RETURN i;
END;
END;
/

I get the expected results, but I'm requiring each post to have a unique title.

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文