一、Halo数据库拥有多模解析引擎功能
Halo数据库具有独有的多模解析引擎技术,可以兼容PostgreSQL、Oracle、Sybase、MySQL、SQL Server、DB2 等主流数据库语法从而大幅减少迁移项目中的代码修改量。目前主要兼容Oracle语法,针对Oracle数据库的迁移至少可以减少95%以上的代码修改量,极大降低迁移成本,同时也大大降低了迁移风险。
Halo 使用可配置式解析引擎,可以灵活地在各个数据库引擎之间切换。
二、如何启用 Oracle 解析引擎
2.1 将参数文件 postgresql.conf 中参数database_compat_mode 设置为 'oracle'
database_compat_mode = 'oracle'
复制
2.2 重启数据库
pg_ctl restart
复制
2.3 针对需要启用Oracle 解析引擎的数据库创建Oracle扩展
create extension aux_oracle cascade;
复制
三、其他与Oracle引擎相关的参数
3.1 transform_null_equals
由于针对NULL的任何操作都只会返回NULL,所以要判断一个值是否为NULL一般只能用IS NULL来判断。
Halo提供了参数transform_null_equals来控制是否可以用“=”操作符来判断一个值是否为NULL。可以将参数transform_null_equals 设置为 on(默认为off)。
3.1.1 数据库中有一条空值记录,当参数transform_null_equals 状态等于off时,使用“=null”不能查询出空值记录
halo0root=# select * from test where id=null;
复制
id
复制
----
复制
(0 行记录)
复制
3.1.2 数据库中有一条空值记录,当参数transform_null_equals 状态等于on时,使用“=null”可以查询出空值记录
halo0root=# select * from test where id=null;
复制
id
复制
----
复制
(1 行记录)
复制
3.2 use_datetime_as_date
Oracle的date 类型带有日期和时间信息,这和Halo的默认设置不同。默认情况下,Halo的date类型只包含日期,datetime类型才包含日期和时间信息。如果想要实现和Oracle相同的date类型,可以将参数use_datetime_as_date设置为true (默认值是 off)。
use_datetime_as_date = true
复制
3.3 standard_parserengine_auxiliary
Oracle的date 类型带有日期和时间信息,这和Halo的默认设置不同。默认情况下,Halo的date类型只包含日期,datetime类型才包含日期和时间信息。如果想要实现和Oracle相同的date类型,可以将参数use_datetime_as_date设置为true (默认值是 off)。
use_datetime_as_date = true
复制
四、Halo数据库选择Oracle模式
Halo数据库 支持 Oracle 中绝大部分函数和语法、包、视图等等。这意味着在从 Oracle 过渡到 Halo的过程中,您不需要消耗大量的时间去学习新的内容,并且可以流畅的实现从 Oracle 到 Halo 的迁移。
4.1 兼容Oracle函数
Halo数据库支持兼容的函数:字符类型、数值类型、日期类型、时间戳类型、间隔类型、大对象类型、二进制类型、转换函数、聚集函数、时间函数等等。
以下我们进行一些简单的函数示例:
ADD_MONTHS函数在给定日期的基础上增加(如果第二个参数为负数则减少)指定的月数。在执行计算之前,月数参数的任何小数部分都会被截断。如果给定的日期包含时间部分,则将转到结果中。
halo0root=#SELECT ADD_MONTHS('13-JUN-07',4) FROM DUAL;
复制
add_months
复制
------------------------
复制
2013-10-07 00:00:00+08
复制
(1 行记录)
复制
BITAND函数执行按位和操作,并根据输入参数的数据类型返回一个值。
语法:BITAND(<expr1>, <expr2>)
返回类型:BITAND函数返回与输入参数的数据类型相同的值。
halo0root=#SELECT BITAND(10,11) FROM DUAL;
复制
bitand
复制
--------
复制
10
复制
(1 行记录)
复制
BTRIM 函数通过删除前导空格和尾随空格或删除与可选的指定字符串匹配的字 符来剪裁字符串。
语法:BTRIM(string [, matching_string ] )
halo0root=#select 'xyzaxyzbxyzcxyz' as untrim,btrim('xyzaxyzbxyzcxyz', 'xyz') as trim;
复制
untrim | trim
复制
-----------------+-----------
复制
xyzaxyzbxyzcxyz | axyzbxyzc
复制
(1 行记录)
复制
CONCAT函数将多个RAW值连接成单个RAW值。
这个函数返回一个RAW值。
halo0root=#select concat('aba','df') from dual;
复制
concat
复制
--------
复制
abadf
复制
(1 行记录)
复制
4.2 兼容Oracle语法
Halo数据库支持兼容的语法:外连接符(+)、序列、SYSDATE(查看日期)、NULL与空串、DECODE、ROWNUM、DBLINK、DUAL伪表、IN特别语法等等。
以下我们进行一些简单的语法示例:
外连接符(+):包括左外连接(左表不加限制),右外连接(右表不加限制),并且可以支持非常复杂的连接操作。
现建立ta; tb; tc; td; te; tf; tg; th; ti; tj 共10张表:
halo0root=# CREATE TABLE ta (id NUMBER, name VARCHAR2(30), location VARCHAR2(30));
复制
CREATE TABLE
复制
halo0root=# INSERT INTO ta VALUES (1, 'A', 'CN');
复制
INSERT 0 1
复制
halo0root=# INSERT INTO ta VALUES (2, 'B', 'CN');
复制
INSERT 0 1
复制
halo0root=# INSERT INTO ta VALUES (3, 'C', 'US');
复制
INSERT 0 1
复制
halo0root=# INSERT INTO ta VALUES (4, 'D', 'JP');
复制
INSERT 0 1
复制
halo0root=#
复制
halo0root=# CREATE TABLE tb (id NUMBER, aid NUMBER, class VARCHAR2(30), location VARCHAR2(30));
复制
CREATE TABLE
复制
halo0root=# INSERT INTO tb VALUES (1, 1, 'S1', 'CN');
复制
INSERT 0 1
复制
halo0root=# INSERT INTO tb VALUES (2, 2, 'S1', 'CN');
复制
INSERT 0 1
复制
halo0root=# INSERT INTO tb VALUES (3, 3, 'S2', 'US');
复制
INSERT 0 1
复制
halo0root=#
复制
halo0root=# CREATE TABLE tc (id NUMBER, name VARCHAR2(30), bid NUMBER, location VARCHAR2(30));
复制
CREATE TABLE
复制
halo0root=# INSERT INTO tc VALUES (1, 'T1', 1, 'CN');
复制
INSERT 0 1
复制
halo0root=# INSERT INTO tc VALUES (2, 'T1', 2, 'CN');
复制
INSERT 0 1
复制
halo0root=# INSERT INTO tc VALUES (3, 'T2', 3, 'US');
复制
INSERT 0 1
复制
halo0root=#
复制
halo0root=# CREATE TABLE td (id NUMBER, grade NUMBER, cid NUMBER);
复制
CREATE TABLE
复制
halo0root=# INSERT INTO td VALUES (1, 1, 1);
复制
INSERT 0 1
复制
halo0root=# INSERT INTO td VALUES (2, 1, 2);
复制
INSERT 0 1
复制
halo0root=# INSERT INTO td VALUES (3, 2, 3);
复制
INSERT 0 1
复制
halo0root=#
复制
halo0root=# CREATE TABLE te (id NUMBER, grade2 NUMBER, did NUMBER);
复制
CREATE TABLE
复制
halo0root=# INSERT INTO te VALUES (1, 1, 1);
复制
INSERT 0 1
复制
halo0root=# INSERT INTO te VALUES (2, 1, 2);
复制
INSERT 0 1
复制
halo0root=# INSERT INTO te VALUES (3, 2, 3);
复制
INSERT 0 1
复制
halo0root=#
复制
halo0root=# CREATE TABLE tf (id NUMBER, grade3 NUMBER, eid NUMBER);
复制
CREATE TABLE
复制
halo0root=# INSERT INTO tf VALUES (1, 1, 1);
复制
INSERT 0 1
复制
halo0root=# INSERT INTO tf VALUES (2, 1, 2);
复制
INSERT 0 1
复制
halo0root=# INSERT INTO tf VALUES (3, 2, 3);
复制
INSERT 0 1
复制
halo0root=#
复制
halo0root=# CREATE TABLE tg (id NUMBER, grade4 NUMBER, fid NUMBER);
复制
CREATE TABLE
复制
halo0root=# INSERT INTO tg VALUES (1, 1, 1);
复制
INSERT 0 1
复制
halo0root=# INSERT INTO tg VALUES (2, 1, 2);
复制
INSERT 0 1
复制
halo0root=# INSERT INTO tg VALUES (3, 2, 3);
复制
INSERT 0 1
复制
halo0root=#
复制
halo0root=# CREATE TABLE th (id NUMBER, grade5 NUMBER, gid NUMBER);
复制
CREATE TABLE
复制
halo0root=# INSERT INTO th VALUES (1, 1, 1);
复制
INSERT 0 1
复制
halo0root=# INSERT INTO th VALUES (2, 1, 2);
复制
INSERT 0 1
复制
halo0root=# INSERT INTO th VALUES (3, 2, 3);
复制
INSERT 0 1
复制
halo0root=#
复制
halo0root=# CREATE TABLE ti (id NUMBER, grade6 NUMBER, hid NUMBER);
复制
CREATE TABLE
复制
halo0root=# INSERT INTO ti VALUES (1, 1, 1);
复制
INSERT 0 1
复制
halo0root=# INSERT INTO ti VALUES (2, 1, 2);
复制
INSERT 0 1
复制
halo0root=# INSERT INTO ti VALUES (3, 2, 3);
复制
INSERT 0 1
复制
halo0root=#
复制
halo0root=# CREATE TABLE tj (id NUMBER, grade7 NUMBER, iid NUMBER);
复制
CREATE TABLE
复制
halo0root=# INSERT INTO tj VALUES (1, 1, 1);
复制
INSERT 0 1
复制
halo0root=# INSERT INTO tj VALUES (2, 1, 2);
复制
INSERT 0 1
复制
halo0root=# INSERT INTO tj VALUES (3, 2, 3);
复制
INSERT 0 1
复制
左连接:
alo0root=# SELECT a.name, a.location, b.class FROM ta a, tb b WHERE a.id = b.aid(+);
复制
name | location | class
复制
------+----------+-------
复制
A | CN | S1
复制
B | CN | S1
复制
C | US | S2
复制
D | JP |
复制
(4 行记录)
复制
右连接:
halo0root=# SELECT a.name, a.location, b.class FROM ta a, tb b WHERE a.id = b.aid( + );
复制
name | location | class
复制
------+----------+-------
复制
A | CN | S1
复制
B | CN | S1
复制
C | US | S2
复制
D | JP |
复制
(4 行记录)
复制
复杂的连接:
halo0root=# SELECT a.name, b.class, c.name, d.grade, e.grade2, f.grade3, g.grade4, h.grade5, i.grade6, j.grade7
复制
halo0root-# FROM ta a, tb b, tc c, td d, te e, tf f, tg g, th h, ti i, tj j
复制
halo0root-# WHERE b.location(+) = 'CN'
复制
halo0root-# AND f.id = g.id(+)
复制
halo0root-# AND a.id = e.id(+)
复制
halo0root-# AND c.id = e.id(+)
复制
halo0root-# AND c.id = d.id(+)
复制
halo0root-# AND a.id = b.id(+)
复制
halo0root-# AND d.id = f.id(+)
复制
halo0root-# AND a.id + e.id = b.id(+)
复制
halo0root-# AND e.id = g.id(+)
复制
halo0root-# AND g.id = h.id(+)
复制
halo0root-# AND a.id = h.id(+)
复制
halo0root-# AND i.hid(+) = 2
复制
halo0root-# AND d.id = i.id(+)
复制
halo0root-# AND h.id = j.id(+)
复制
halo0root-# AND i.id = j.id(+);
复制
name | class | name | grade | grade2 | grade3 | grade4 | grade5 | grade6 | grade7
复制
------+-------+------+-------+--------+--------+--------+--------+--------+--------
复制
A | | T1 | 1 | 1 | 1 | 1 | 1 | |
复制
B | | T1 | 1 | | 1 | | | |
复制
C | | T1 | 1 | | 1 | | | |
复制
D | | T1 | 1 | | 1 | | | |
复制
A | | T1 | 1 | | 1 | | | 1 |
复制
B | | T1 | 1 | 1 | 1 | 1 | 1 | 1 | 1
复制
C | | T1 | 1 | | 1 | | | 1 |
复制
D | | T1 | 1 | | 1 | | | 1 |
复制
A | | T2 | 2 | | 2 | | | |
复制
B | | T2 | 2 | | 2 | | | |
复制
C | | T2 | 2 | 2 | 2 | 2 | 2 | |
复制
D | | T2 | 2 | | 2 | | | |
复制
(12 行记录)
复制
序列(SEQUENCE)是序列号生成器,可以为表中的行自动生成序列号,产生一组等间隔的数值(类型为数字)。不占用磁盘空间,占用内存。其主要用途是生成表的主键值。另外,也支持ORDER关键字创建序列。
创建一个序列:
halo0root=#CREATE SEQUENCE a_seq;
复制
CREATESEQUENCE
复制
也支持ORDER关键字创建序列
复制
CREATE SEQUENCE a_seq2 ORDER;
复制
select a_seq.nextval,a.* from t_a a order by id;
复制
初始化序列:
halo0root=#SELECT a_seq.nextval FROM dual;
复制
nextval
复制
---------
复制
1
复制
(1 行记录)
复制
查询当前序列:
halo0root=#SELECT a_seq.currval FROM dual;
复制
currval
复制
---------
复制
1
复制
(1 行记录)
复制
MINUS在Oracle中也是用来做减法操作的,只不过它不是传统意义上对数字的减法,而是对查询结果集的减法。
halo0root=#select * from td;
复制
id
复制
----
复制
1
复制
2
复制
3
复制
(3 行记录)
复制
复制
halo0root=#select * from te;
复制
id
复制
----
复制
1
复制
2
复制
0
复制
(3 行记录)
复制
复制
halo0root=#select from td MINUS SELECT FROM te;
复制
id
复制
----
复制
3
复制
(1 行记录)
复制
<winagg_function>(distinct..) over(partition by)语法功能为求分组去重后目标的结果,支持常用的聚合函数,如count, sum, listagg等。
建立per表格
halo0root=# CREATE TABLE Per(Id int,Name varchar(255));
复制
CREATE TABLE
复制
halo0root=# INSERT INTO Per VALUES (1, 'ww');
复制
INSERT 0 1
复制
halo0root=# INSERT INTO Per VALUES (1, 'ee');
复制
INSERT 0 1
复制
halo0root=# INSERT INTO Per VALUES (1, 'ee');
复制
INSERT 0 1
复制
halo0root=# INSERT INTO Per VALUES (2, 'ee');
复制
INSERT 0 1
复制
halo0root=# INSERT INTO Per VALUES (2, 'dd');
复制
INSERT 0 1
复制
halo0root=# select * from Per;
复制
id | name
复制
----+------
复制
1 | ww
复制
1 | ee
复制
2 | ee
复制
2 | dd
复制
1 | ee
复制
(5 rows)
复制
count(distinct.. ) over(partition by)语法求分组去重后的结果:
halo0root=# select name, count(distinct name) over(partition by id) from Per;
复制
name | count
复制
------+-------
复制
ww | 2
复制
ee | 2
复制
ee | 2
复制
ee | 2
复制
dd | 2
复制
(5 rows)
复制
作为对比,无DISTINCT操作的结果
halo0root=# select name, count(name) over(partition by id) from Per;
复制
name | count
复制
------+-------
复制
ww | 3
复制
ee | 3
复制
ee | 3
复制
ee | 2
复制
dd | 2
复制
(5 rows)
复制
4.3 兼容Oracle系统包
Halo数据库支持兼容的系统包:
DBMS_ALERT、DBMS_ASSERT、DBMS_OUTPUT、DBMS_PIPE、DBMS_RANDOM、DBMS_UTILITY、UTL_FILE等等。
以下我们用DBMS_ASSERT包简单示例:
DBMS_ASSERT包提供了一个接口来验证输入值的属性。
halo0root=# \df dbms_assert.*
复制
dbms_assert.enquote_literal:用于字符串字面值添加了首引号和尾单引号。
halo0root=#select DBMS_ASSERT.ENQUOTE_LITERAL (ename) from emp;
复制
enquote_literal
复制
-----------------
复制
'ALLEN'
复制
'WARD'
复制
'JONES'
复制
'MARTIN'
复制
'BLAKE'
复制
(5 行记录)
复制
dbms_assert.qualified_sql_name:验证输入的字符串是否为合格的SQL名称。
halo0root=#select dbms_assert.qualified_sql_name ('wy') from dual;
复制
qualified_sql_name
复制
--------------------
复制
wy
复制
(1 行记录)
复制
4.4 兼容的Oracle视图
Halo数据库支持兼容的视图:DBA_SEGMENTS、PRODUCT_COMPONENT_VERSION、user_con_columns、USER_CONSTRAINTS、USER_IND_COLUMNS、USER_OBJECTS、USER_PROCEDURES、USER_SOURCE、USER_TAB_COLUMNS、USER_TABLES等等。
以下我们用DBA_SEGMENTS、PRODUCT_COMPONENT_VERSION进行示例:
DBA_SEGMENTS 视图描述的是数据库中所有段的存储和分配信息。
halo0root=# \dS dba_segments
复制
视图 "oracle.dba_segments"
复制
栏位 | 类型 | 校对规则 | 可空的 | 预设
复制
-----------------+-----------------------+----------+--------+------
复制
owner | name | | |
复制
segment_name | name | | |
复制
segment_type | character varying(18) | | |
复制
tablespace_name | name | | |
复制
header_file | oid | | |
复制
header_block | oid | | |
复制
bytes | bigint | | |
复制
blocks | integer | | |
复制
PRODUCT_COMPONENT_VERSION包含组件产品的版本和状态信息。
halo0root=# \dS product_component_version
复制
视图 "oracle.product_component_version"
复制
栏位 | 类型 | 校对规则 | 可空的 | 预设
复制
---------+------+----------+--------+------
复制
product |text | C | |
复制
version |text | C | |
复制
status | text | | |
复制
备注:
复制
Product 产品的名称
复制
Version 产品的版本号
复制
Status 产品状态包括兼容性高可用性
复制
以上只是展示部分兼容Oracle 功能,如您想要了解更多,请留言关注!谢谢
评论
