-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
oracle_ora_test.go
178 lines (164 loc) · 4.25 KB
/
oracle_ora_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package oracle
import (
"database/sql"
"database/sql/driver"
"encoding/json"
"errors"
"fmt"
"io"
"log"
"testing"
)
const (
procCreateExamplePagingQuery = `-- example procedure
create or replace PROCEDURE PRO_EXAMPLE_PAGING_QUERY (
BASIC_SQL IN VARCHAR2, -- 基础查询 SQL
ORDER_FIELD IN VARCHAR2, -- 排序字段
PAGE_NUM IN NUMBER, -- 当前页码
PAGE_SIZE IN NUMBER, -- 每页条数
TOTAL_NUM OUT NUMBER, -- 返回总条数
RES_CURSOR OUT SYS_REFCURSOR -- 返回结果集
)
AS
BEGIN
DECLARE
PAGING_SQL VARCHAR2(4000) := ''; -- 分页查询 SQL
TOTAL_SQL VARCHAR2(4000) := ''; -- 总条数查询 SQL
OFFSET NUMBER(10); -- 分页查询偏移量
BEGIN
-- 查询总条数
TOTAL_SQL := 'SELECT TO_NUMBER(COUNT(*)) FROM (' || BASIC_SQL || ') TB';
EXECUTE IMMEDIATE TOTAL_SQL INTO TOTAL_NUM;
-- 分页查询
OFFSET := (PAGE_NUM - 1) * PAGE_SIZE;
PAGING_SQL := 'SELECT * FROM (SELECT T.*, ROW_NUMBER() OVER (ORDER BY ' || ORDER_FIELD ||
') AS ROW_NUM FROM (' || BASIC_SQL || ') T) WHERE ROW_NUM BETWEEN ' ||
TO_CHAR(OFFSET+1) || ' AND ' || TO_CHAR(OFFSET+PAGE_SIZE);
OPEN RES_CURSOR FOR PAGING_SQL;
END;
END PRO_EXAMPLE_PAGING_QUERY;`
)
func ExampleRefCursor_Query() {
db, err := dbNamingCase, dbErrors[0]
if err != nil || db == nil {
log.Fatal(err)
}
if err = db.Exec(procCreateExamplePagingQuery).Error; err != nil {
log.Fatal(err)
}
var (
totalNum uint
resCursor RefCursor
values = []any{
"SELECT * FROM USER_TABLES",
"TABLE_NAME",
1,
10,
sql.Out{Dest: &totalNum},
sql.Out{Dest: &resCursor.RefCursor},
}
)
// 执行存储过程
if err = db.Exec(`
BEGIN
PRO_EXAMPLE_PAGING_QUERY(:BASIC_SQL, :ORDER_FIELD, :PAGE_NUM, :PAGE_SIZE, :TOTAL_NUM, :RES_CURSOR);
END;`, values...).Error; err != nil {
log.Fatal(err)
}
defer func(cursor *RefCursor) {
_ = cursor.Close()
}(&resCursor)
// 读取游标
var dataset *DataSet
if dataset, err = resCursor.Query(); err != nil {
log.Fatal(err)
}
defer func(dataset *DataSet) {
_ = dataset.Close()
}(dataset)
var dataRows []map[string]any
columns := dataset.Columns()
dest := make([]driver.Value, len(columns))
for {
if err = dataset.Next(dest); err != nil {
if errors.Is(err, io.EOF) {
err = nil
}
break
}
dataRow := make(map[string]any, len(columns))
for i, v := range dest {
dataRow[columns[i]] = v
}
dataRows = append(dataRows, dataRow)
}
if err != nil {
log.Fatal(err)
}
fmt.Println(len(dataRows) > 0)
//Output: true
}
func TestExecProcedure(t *testing.T) {
db, err := dbNamingCase, dbErrors[0]
if err != nil {
t.Fatal(err)
}
if db == nil {
t.Log("db is nil!")
return
}
if err = db.Exec(procCreateExamplePagingQuery).Error; err != nil {
t.Fatal(err)
}
var (
totalNum uint
resCursor RefCursor
values = []any{
"SELECT * FROM USER_TABLES", // sql.Named("BASIC_SQL", "SELECT * FROM USER_TABLES"),
"TABLE_NAME", // sql.Named("ORDER_FIELD", "TABLE_NAME"),
1, // sql.Named("PAGE_NUM", 1),
10, // sql.Named("PAGE_SIZE", 10),
sql.Out{Dest: &totalNum}, // sql.Named("TOTAL_NUM", sql.Out{Dest: &totalNum}),
sql.Out{Dest: &resCursor.RefCursor}, // sql.Named("RES_CURSOR", sql.Out{Dest: &resCursor.RefCursor}),
}
)
// 执行存储过程
if err = db.Exec(`
BEGIN
PRO_EXAMPLE_PAGING_QUERY(:BASIC_SQL, :ORDER_FIELD, :PAGE_NUM, :PAGE_SIZE, :TOTAL_NUM, :RES_CURSOR);
END;`, values...).Error; err != nil {
t.Fatal(err)
}
defer func(cursor *RefCursor) {
_ = cursor.Close()
}(&resCursor)
// 读取游标
var dataset *DataSet
if dataset, err = resCursor.Query(); err != nil {
t.Fatal(err)
}
defer func(dataset *DataSet) {
_ = dataset.Close()
}(dataset)
var dataRows []map[string]any
columns := dataset.Columns()
dest := make([]driver.Value, len(columns))
for {
if err = dataset.Next(dest); err != nil {
if errors.Is(err, io.EOF) {
err = nil
}
break
}
dataRow := make(map[string]any, len(columns))
for i, v := range dest {
dataRow[columns[i]] = v
}
dataRows = append(dataRows, dataRow)
}
if err != nil {
t.Fatal(err)
}
got, _ := json.Marshal(dataRows)
t.Logf("got total: %d, got size: %d, got data:\n%s", totalNum, len(dataRows), got)
}