Documentation
¶
Overview ¶
Package sqlscript provides functions to scan HDBSQL scripts with the help of bufio.Scanner. The statement separator (usually ';') is required between statements; it is not required after the final statement. This package is currently experimental and its public interface might be changed in an incompatible way at any time.
Example ¶
Example demonstrates the usage of go-hdb sqlscript scanning functions.
package main
import (
"bufio"
"context"
"database/sql"
"log"
"os"
"strings"
"github.com/SAP/go-hdb/driver"
"github.com/SAP/go-hdb/sqlscript"
)
func main() {
ddlScript := `
-- create local temporary table
CREATE LOCAL TEMPORARY TABLE #my_local_temp_table (
Column1 INTEGER,
Column2 VARCHAR(10)
);
--- insert some records
INSERT INTO #MY_LOCAL_TEMP_TABLE VALUES (1,'A');
INSERT INTO #MY_LOCAL_TEMP_TABLE VALUES (2,'B');
--- and drop the table
DROP TABLE #my_local_temp_table
`
const envDSN = "GOHDBDSN"
dsn := os.Getenv(envDSN)
// exit if DSN is missing.
if dsn == "" {
return
}
cfg, err := driver.ParseDSNConfig(dsn)
if err != nil {
log.Fatal(err)
}
connector, err := driver.NewConfigConnector(cfg)
if err != nil {
log.Fatal(err)
}
db := sql.OpenDB(connector)
defer db.Close()
scanner := bufio.NewScanner(strings.NewReader(ddlScript))
// Include comments as part of the SQL statements.
scanner.Split(sqlscript.ScanFunc(sqlscript.DefaultSeparator, true))
for scanner.Scan() {
if _, err := db.ExecContext(context.Background(), scanner.Text()); err != nil {
log.Fatal(err)
}
}
if err := scanner.Err(); err != nil {
log.Fatal(err)
}
}
Output:
Index ¶
Examples ¶
Constants ¶
View Source
const DefaultSeparator = ';'
DefaultSeparator is the default script statement separator.
Variables ¶
View Source
var ErrInvalidUTF8 = errors.New("invalid UTF-8 encoding")
ErrInvalidUTF8 is returned when the script contains a byte sequence that cannot be decoded as valid UTF-8.
Functions ¶
Types ¶
This section is empty.
Click to show internal directories.
Click to hide internal directories.