Skip to content

Commit

Permalink
add example for varray and update README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
sijms committed Jan 27, 2024
1 parent dc984bc commit 8096ceb
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -537,6 +537,7 @@ regular type supported
as input/output parameters
* [examples/regular_type_array](https://github.com/sijms/go-ora/blob/master/examples/regular_type_array/main.go) a complete example for all supported regullar type arrays
* [examples/null_udt](https://github.com/sijms/go-ora/blob/master/examples/null_udt/main.go) example represent null object as input and output
* [examples/varray](https://github.com/sijms/go-ora/blob/master/examples/varray/main.go) for varray types

* ### RefCursor
> as an output parameter
Expand Down
83 changes: 83 additions & 0 deletions examples/varray/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package main

import (
"database/sql"
"fmt"
_ "github.com/sijms/go-ora/v2"
go_ora "github.com/sijms/go-ora/v2"
"os"
)

func execCmd(db *sql.DB, stmts ...string) error {
for _, stmt := range stmts {
if _, err := db.Exec(stmt); err != nil {
if len(stmts) > 1 {
return fmt.Errorf("error: %v in execuation of stmt: %s", err, stmt)
} else {
return err
}
}
}
return nil
}

func createType(db *sql.DB) error {
return execCmd(db, `create type StringArray as VARRAY(10) of varchar2(20) not null`)
}

func dropType(db *sql.DB) error {
return execCmd(db, `drop type StringArray`)
}

func outputPar(db *sql.DB) error {
var output []string
_, err := db.Exec(`
DECLARE
l_array StringArray := StringArray();
BEGIN
for x in 1..10 loop
l_array.extend;
l_array(x) := 'string_' || x;
end loop;
:1 := l_array;
END;`, go_ora.Object{Name: "StringArray", Value: &output})
if err != nil {
return err
}
fmt.Println("output: ", output)
return nil
}
func main() {
db, err := sql.Open("oracle", os.Getenv("DSN"))
if err != nil {
fmt.Println("can't open db: ", err)
return
}
defer func() {
err = db.Close()
if err != nil {
fmt.Println("can't close db: ", err)
}
}()
err = createType(db)
if err != nil {
fmt.Println("can't create types: ", err)
return
}
defer func() {
err = dropType(db)
if err != nil {
fmt.Println("can't drop types: ", err)
}
}()
err = go_ora.RegisterType(db, "varchar2", "StringArray", nil)
if err != nil {
fmt.Println("can't register string array: ", err)
return
}
err = outputPar(db)
if err != nil {
fmt.Println("can't output pars: ", err)
return
}
}

0 comments on commit 8096ceb

Please sign in to comment.