Skip to content

Instantly share code, notes, and snippets.

@robstradling
Last active February 16, 2024 11:16
Show Gist options
  • Save robstradling/363b54e979b879e0450cfa36e238cca6 to your computer and use it in GitHub Desktop.
Save robstradling/363b54e979b879e0450cfa36e238cca6 to your computer and use it in GitHub Desktop.
go-ora integer precision
module gist.github.com/robstradling/363b54e979b879e0450cfa36e238cca6
go 1.21.6
require github.com/sijms/go-ora/v2 v2.8.9
github.com/sijms/go-ora/v2 v2.8.9 h1:XIghcG8hjtYu+G9H235VEe5JXPLJtdzzj7pQm7JucVo=
github.com/sijms/go-ora/v2 v2.8.9/go.mod h1:EHxlY6x7y9HAsdfumurRfTd+v8NrEOTR3Xl4FWlH6xk=
package main
import (
"context"
"database/sql"
"flag"
"fmt"
"os"
go_ora "github.com/sijms/go-ora/v2"
)
func usage() {
fmt.Println()
fmt.Println("integer_precision")
fmt.Println(" a demonstration of go-ora's integer precision.")
fmt.Println()
fmt.Println("Usage:")
fmt.Println(` cant_assign_value -server server_url`)
flag.PrintDefaults()
fmt.Println()
fmt.Println("Example:")
fmt.Println(` integer_precision -server "oracle://user:pass@server/service_name"`)
fmt.Println()
}
func main() {
var (
server string
)
flag.StringVar(&server, "server", "", "Server's URL, oracle://user:pass@server/service_name")
flag.Parse()
connStr := os.ExpandEnv(server)
if connStr == "" {
fmt.Println("Missing -server option")
usage()
os.Exit(1)
}
fmt.Println("Connection string: ", connStr)
conn, err := go_ora.NewConnection(connStr)
if err != nil {
fmt.Println("Can't create connection: ", err)
return
} else if err = conn.Open(); err != nil {
fmt.Println("Can't open the driver: ", err)
return
}
defer func() {
err = conn.Close()
if err != nil {
fmt.Println("Can't close driver: ", err)
}
}()
err = conn.Ping(context.Background())
if err != nil {
fmt.Println("Can't ping connection: ", err)
return
}
fmt.Println("\n'SELECT -9223372036854775806 FROM DUAL' into a 'sql.NullInt64' variable")
var oneNullInt64 sql.NullInt64
err = conn.QueryRowContext(context.Background(), "SELECT -9223372036854775806 FROM DUAL", nil).Scan(&oneNullInt64)
if err != nil {
fmt.Println("Failed: ", err)
} else if oneNullInt64.Int64 != -9223372036854775806 {
fmt.Printf("Wrong value: %d != -9223372036854775806\n", oneNullInt64.Int64)
} else {
fmt.Println("OK: ", oneNullInt64)
}
fmt.Println("\n'SELECT -9223372036854775806 FROM DUAL' into an 'int64' variable")
var oneInt64 int64
err = conn.QueryRowContext(context.Background(), "SELECT -9223372036854775806 FROM DUAL", nil).Scan(&oneInt64)
if err != nil {
fmt.Println("Failed: ", err)
} else if oneInt64 != -9223372036854775806 {
fmt.Printf("Wrong value: %d != -9223372036854775806\n", oneInt64)
} else {
fmt.Println("OK: ", oneInt64)
}
}
Connection string: oracle://www:[email protected]:1522/saspdev.sectigo.gb?CLIENT+CHARSET=UTF8
'SELECT -9223372036854775806 FROM DUAL' into a 'sql.NullInt64' variable
Wrong value: -9223372036854775808 != -9223372036854775806
'SELECT -9223372036854775806 FROM DUAL' into an 'int64' variable
Wrong value: -9223372036854775808 != -9223372036854775806
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment