|
| 1 | +// Copyright 2026 GoSQLX Authors |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package parser_test |
| 16 | + |
| 17 | +import ( |
| 18 | + "testing" |
| 19 | + |
| 20 | + "github.com/ajitpratap0/GoSQLX/pkg/gosqlx" |
| 21 | + "github.com/ajitpratap0/GoSQLX/pkg/sql/ast" |
| 22 | + "github.com/ajitpratap0/GoSQLX/pkg/sql/keywords" |
| 23 | +) |
| 24 | + |
| 25 | +// TestConnectBy_Oracle_Basic tests basic Oracle CONNECT BY syntax. |
| 26 | +func TestConnectBy_Oracle_Basic(t *testing.T) { |
| 27 | + sql := `SELECT employee_id, manager_id, name |
| 28 | + FROM employees |
| 29 | + START WITH manager_id IS NULL |
| 30 | + CONNECT BY PRIOR employee_id = manager_id` |
| 31 | + tree, err := gosqlx.ParseWithDialect(sql, keywords.DialectOracle) |
| 32 | + if err != nil { |
| 33 | + t.Fatalf("Parse() error: %v", err) |
| 34 | + } |
| 35 | + sel, ok := tree.Statements[0].(*ast.SelectStatement) |
| 36 | + if !ok { |
| 37 | + t.Fatalf("expected SelectStatement, got %T", tree.Statements[0]) |
| 38 | + } |
| 39 | + if sel.ConnectBy == nil { |
| 40 | + t.Error("expected ConnectBy clause to be populated") |
| 41 | + } |
| 42 | + if sel.StartWith == nil { |
| 43 | + t.Error("expected StartWith expression to be populated") |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +// TestConnectBy_Oracle_NoCycle tests Oracle CONNECT BY with NOCYCLE modifier. |
| 48 | +func TestConnectBy_Oracle_NoCycle(t *testing.T) { |
| 49 | + sql := `SELECT id, parent_id FROM categories |
| 50 | + START WITH parent_id IS NULL |
| 51 | + CONNECT BY NOCYCLE PRIOR id = parent_id` |
| 52 | + tree, err := gosqlx.ParseWithDialect(sql, keywords.DialectOracle) |
| 53 | + if err != nil { |
| 54 | + t.Fatalf("Parse() error: %v", err) |
| 55 | + } |
| 56 | + sel, ok := tree.Statements[0].(*ast.SelectStatement) |
| 57 | + if !ok { |
| 58 | + t.Fatalf("expected SelectStatement, got %T", tree.Statements[0]) |
| 59 | + } |
| 60 | + if sel.ConnectBy == nil { |
| 61 | + t.Fatal("expected ConnectBy") |
| 62 | + } |
| 63 | + if !sel.ConnectBy.NoCycle { |
| 64 | + t.Error("expected NoCycle = true") |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +// TestConnectBy_Oracle_ConnectByOnly tests CONNECT BY without START WITH. |
| 69 | +func TestConnectBy_Oracle_ConnectByOnly(t *testing.T) { |
| 70 | + sql := `SELECT id, parent_id FROM categories |
| 71 | + CONNECT BY PRIOR id = parent_id` |
| 72 | + tree, err := gosqlx.ParseWithDialect(sql, keywords.DialectOracle) |
| 73 | + if err != nil { |
| 74 | + t.Fatalf("Parse() error: %v", err) |
| 75 | + } |
| 76 | + sel, ok := tree.Statements[0].(*ast.SelectStatement) |
| 77 | + if !ok { |
| 78 | + t.Fatalf("expected SelectStatement, got %T", tree.Statements[0]) |
| 79 | + } |
| 80 | + if sel.ConnectBy == nil { |
| 81 | + t.Error("expected ConnectBy clause") |
| 82 | + } |
| 83 | + if sel.StartWith != nil { |
| 84 | + t.Error("expected StartWith to be nil when not specified") |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +// TestConnectBy_MariaDB_StillWorks ensures existing MariaDB CONNECT BY parsing remains intact. |
| 89 | +func TestConnectBy_MariaDB_StillWorks(t *testing.T) { |
| 90 | + sql := `SELECT id, parent_id FROM categories |
| 91 | + START WITH parent_id IS NULL |
| 92 | + CONNECT BY PRIOR id = parent_id` |
| 93 | + tree, err := gosqlx.ParseWithDialect(sql, keywords.DialectMariaDB) |
| 94 | + if err != nil { |
| 95 | + t.Fatalf("Parse() error: %v", err) |
| 96 | + } |
| 97 | + sel, ok := tree.Statements[0].(*ast.SelectStatement) |
| 98 | + if !ok { |
| 99 | + t.Fatalf("expected SelectStatement, got %T", tree.Statements[0]) |
| 100 | + } |
| 101 | + if sel.ConnectBy == nil { |
| 102 | + t.Error("expected ConnectBy clause") |
| 103 | + } |
| 104 | + if sel.StartWith == nil { |
| 105 | + t.Error("expected StartWith expression") |
| 106 | + } |
| 107 | +} |
0 commit comments