Skip to content

Commit 45782a4

Browse files
authored
Merge branch 'master' into pgroonga
2 parents c882f07 + dbb97b1 commit 45782a4

File tree

8 files changed

+255
-2
lines changed

8 files changed

+255
-2
lines changed

CN/modules/ROOT/nav.adoc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
**** xref:master/6.3.8.adoc[Force View]
3737
**** xref:master/6.3.9.adoc[大小写转换]
3838
**** xref:master/6.3.10.adoc[sys_guid 函数]
39+
**** xref:master/6.3.11.adoc[空字符串转null]
3940
*** 内置函数
4041
**** xref:master/6.4.1.adoc[sys_context]
4142
**** xref:master/6.4.2.adoc[userenv]
@@ -61,6 +62,7 @@
6162
*** xref:master/7.18.adoc[18、Force View]
6263
*** xref:master/7.19.adoc[19、嵌套子函数]
6364
*** xref:master/7.20.adoc[20、sys_guid 函数]
65+
*** xref:master/7.21.adoc[21、空字符串转null]
6466
** IvorySQL贡献指南
6567
*** xref:master/8.1.adoc[社区贡献指南]
6668
*** xref:master/8.2.adoc[asciidoc语法快速参考]
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
:sectnums:
2+
:sectnumlevels: 5
3+
4+
:imagesdir: ./_images
5+
6+
= 空字符串转NULL功能
7+
8+
== 目的
9+
10+
在Oracle数据库中,空字符串('')被视为NULL值,这是Oracle的一个重要特性。为了兼容Oracle的这一行为,IvorySQL提供了空字符串转NULL功能。当启用该功能后,SQL语句中的空字符串将自动转换为NULL值,从而保证Oracle应用程序在IvorySQL上的行为一致性。
11+
12+
== 实现说明
13+
14+
参数 `ivorysql.enable_emptystring_to_NULL` 对应的GUC变量为 `enable_emptystring_to_NULL`。
15+
16+
在文件 `ora_scan.l` 中可以看到对这个变量的使用:
17+
18+
[source,c]
19+
----
20+
case xe:
21+
yylval->str = litbufdup(yyscanner);
22+
if (strcmp(yylval->str, "") == 0 &&
23+
ORA_PARSER == compatible_db &&
24+
enable_emptystring_to_NULL)
25+
{
26+
return NULL_P;
27+
}
28+
return SCONST;
29+
----
30+
31+
其中 `xe` 代表被引号包围的字符串:
32+
33+
----
34+
<xe> extended quoted strings (support backslash escape sequences)
35+
----
36+
37+
上面代码的逻辑是,在词法分析时如果遇到空字符串,在空转NULL功能启用的情况下,返回 `NULL_P`,否则返回 `SCONST`。
38+
39+
在语法分析文件 `ora_gram.y` 里,对语句 `insert into abc values('');` 是这样解析的:
40+
41+
[source,c]
42+
----
43+
values_clause:
44+
VALUES '(' expr_list ')'
45+
{
46+
SelectStmt *n = makeNode(SelectStmt);
47+
48+
n->valuesLists = list_make1($3);
49+
$$ = (Node *) n;
50+
}
51+
52+
expr_list: a_expr
53+
{
54+
$$ = list_make1($1);
55+
}
56+
57+
a_expr: c_expr { $$ = $1; }
58+
59+
c_expr: AexprConst { $$ = $1; }
60+
61+
AexprConst: Iconst
62+
| Sconst
63+
{
64+
$$ = makeStringConst($1, @1);
65+
}
66+
| NULL_P
67+
{
68+
$$ = makeNullAConst(@1);
69+
}
70+
----
71+
72+
以上代码对词法分析时返回的 `NULL_P` 或者 `SCONST` 分别构建对应的节点进行处理。
73+

CN/modules/ROOT/pages/master/7.20.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ IvorySQL的sys_guid() 是一个强大的随机数产生函数,它产生并返
1212
== 使用示例
1313

1414
```
15-
highgo=# select sys_guid() from dual;
15+
ivorysql=# select sys_guid() from dual;
1616
sys_guid
1717
------------------------------------
1818
\x3ed9426c8a093442a38bea09a74f44a1
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
:sectnums:
2+
:sectnumlevels: 5
3+
4+
:imagesdir: ./_images
5+
6+
= 兼容Oracle 空字符串转NULL
7+
8+
== 目的
9+
10+
- 在 IvorySQL 的 Oracle 兼容模式下,支持将空字符串转换成NULL进行存储,提供与Oracle数据库一致的行为。
11+
12+
== 功能说明
13+
14+
- Oracle兼容模式下支持将空字符串转换成NULL进行存储。
15+
- 通过参数 `ivorysql.enable_emptystring_to_null` 控制该特性,默认值为 `on`。
16+
- 当该参数开启时,插入空字符串会自动转换为NULL值存储。
17+
- 可以通过 `IS NULL` 条件查询到转换后的NULL值。
18+
19+
== 测试用例
20+
21+
[literal]
22+
----
23+
-- 创建测试表
24+
ivorysql=# create table abc (id int);
25+
CREATE TABLE
26+
27+
-- 查看空字符串转NULL参数状态
28+
ivorysql=# show ivorysql.enable_emptystring_to_null;
29+
ivorysql.enable_emptystring_to_NULL
30+
-------------------------------------
31+
on
32+
(1 row)
33+
34+
-- 插入空字符串
35+
ivorysql=# insert into abc values('');
36+
INSERT 0 1
37+
38+
-- 查询表数据,显示为NULL
39+
ivorysql=# select * from abc;
40+
id
41+
\----
42+
43+
(1 row)
44+
45+
-- 使用IS NULL条件查询
46+
ivorysql=# select * from abc where id is null;
47+
id
48+
\----
49+
50+
(1 row)
51+
----

EN/modules/ROOT/nav.adoc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
*** xref:master/6.3.8.adoc[Force View]
3636
*** xref:master/6.3.9.adoc[Case Conversion]
3737
*** xref:master/6.3.10.adoc[sys_guid Function]
38+
*** xref:master/6.3.11.adoc[Empty String to NULL]
3839
** Built-in Functions
3940
*** xref:master/6.4.1.adoc[sys_context]
4041
*** xref:master/6.4.2.adoc[userenv]
@@ -60,6 +61,7 @@
6061
** xref:master/7.18.adoc[18、Force View]
6162
** xref:master/7.19.adoc[19、Nested Subfunctions]
6263
** xref:master/7.20.adoc[20、sys_guid Function]
64+
** xref:master/7.21.adoc[21、Empty String to NULL]
6365
* xref:master/8.adoc[Community contribution]
6466
* xref:master/9.adoc[Tool Reference]
6567
* xref:master/10.adoc[FAQ]
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
:sectnums:
2+
:sectnumlevels: 5
3+
4+
:imagesdir: ./_images
5+
6+
= Empty String to NULL Conversion
7+
8+
== Purpose
9+
10+
In Oracle databases, empty strings ('') are treated as NULL values, which is an important Oracle feature. To maintain compatibility with this Oracle behavior, IvorySQL provides the empty string to NULL conversion functionality. When this feature is enabled, empty strings in SQL statements are automatically converted to NULL values, ensuring behavioral consistency for Oracle applications running on IvorySQL.
11+
12+
== Implementation
13+
14+
The parameter `ivorysql.enable_emptystring_to_NULL` corresponds to the GUC variable `enable_emptystring_to_NULL`.
15+
16+
In the file `ora_scan.l`, you can see how this variable is used:
17+
18+
[source,c]
19+
----
20+
case xe:
21+
yylval->str = litbufdup(yyscanner);
22+
if (strcmp(yylval->str, "") == 0 &&
23+
ORA_PARSER == compatible_db &&
24+
enable_emptystring_to_NULL)
25+
{
26+
return NULL_P;
27+
}
28+
return SCONST;
29+
----
30+
31+
Where `xe` represents strings enclosed in quotes:
32+
33+
----
34+
<xe> extended quoted strings (support backslash escape sequences)
35+
----
36+
37+
The logic of the above code is that during lexical analysis, if an empty string is encountered and the empty-to-NULL conversion feature is enabled, it returns `NULL_P`; otherwise, it returns `SCONST`.
38+
39+
In the grammar analysis file `ora_gram.y`, the statement `insert into abc values('');` is parsed as follows:
40+
41+
[source,c]
42+
----
43+
values_clause:
44+
VALUES '(' expr_list ')'
45+
{
46+
SelectStmt *n = makeNode(SelectStmt);
47+
48+
n->valuesLists = list_make1($3);
49+
$$ = (Node *) n;
50+
}
51+
52+
expr_list: a_expr
53+
{
54+
$$ = list_make1($1);
55+
}
56+
57+
a_expr: c_expr { $$ = $1; }
58+
59+
c_expr: AexprConst { $$ = $1; }
60+
61+
AexprConst: Iconst
62+
| Sconst
63+
{
64+
$$ = makeStringConst($1, @1);
65+
}
66+
| NULL_P
67+
{
68+
$$ = makeNullAConst(@1);
69+
}
70+
----
71+
72+
The above code constructs corresponding nodes to handle `NULL_P` or `SCONST` returned from the lexical analysis.
73+

EN/modules/ROOT/pages/master/7.20.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ IvorySQL's sys_guid() is a powerful random number generation function that gener
1212
== Usage example
1313

1414
```
15-
highgo=# select sys_guid() from dual;
15+
ivorysql=# select sys_guid() from dual;
1616
sys_guid
1717
------------------------------------
1818
\x3ed9426c8a093442a38bea09a74f44a1
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
:sectnums:
2+
:sectnumlevels: 5
3+
4+
:imagesdir: ./_images
5+
6+
= Oracle Compatible Empty String to NULL
7+
8+
== Purpose
9+
10+
- In IvorySQL's Oracle compatibility mode, support converting empty strings to NULL for storage, providing behavior consistent with Oracle Database.
11+
12+
== Feature Description
13+
14+
- Oracle compatibility mode supports converting empty strings to NULL for storage.
15+
- The feature is controlled by parameter `ivorysql.enable_emptystring_to_null`, with default value `on`.
16+
- When this parameter is enabled, inserting an empty string will automatically convert it to NULL value for storage.
17+
- NULL values after conversion can be queried using the `IS NULL` condition.
18+
19+
== Test Cases
20+
21+
[literal]
22+
----
23+
-- Create test table
24+
ivorysql=# create table abc (id int);
25+
CREATE TABLE
26+
27+
-- Check empty string to NULL parameter status
28+
ivorysql=# show ivorysql.enable_emptystring_to_null;
29+
ivorysql.enable_emptystring_to_NULL
30+
-------------------------------------
31+
on
32+
(1 row)
33+
34+
-- Insert empty string
35+
ivorysql=# insert into abc values('');
36+
INSERT 0 1
37+
38+
-- Query table data, displays as NULL
39+
ivorysql=# select * from abc;
40+
id
41+
\----
42+
43+
(1 row)
44+
45+
-- Query using IS NULL condition
46+
ivorysql=# select * from abc where id is null;
47+
id
48+
\----
49+
50+
(1 row)
51+
----
52+

0 commit comments

Comments
 (0)