Documentation
¶
Overview ¶
Package sqlscript provides functions to scan HDBSQL scripts with the help of bufio.Scanner. 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"
"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
}
connector, err := driver.NewDSNConnector(dsn)
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.SplitFunc(sqlscript.DefaultSeparator, true))
for scanner.Scan() {
if _, err := db.Exec(scanner.Text()); err != nil {
log.Fatal(err)
}
}
if err := scanner.Err(); err != nil {
log.Fatal(err)
}
}
Index ¶
Examples ¶
Constants ¶
View Source
const DefaultSeparator = ';'
DefaultSeparator is the default script statement separator.
Variables ¶
This section is empty.
Functions ¶
func Scan ¶
Scan is a split function for a bufio.Scanner that returns each statement as a token. It uses the default separator ';'. Comments are discarded - for adding leading comments to each statement or specify a different separator please use SplitFunc.
Types ¶
This section is empty.
Click to show internal directories.
Click to hide internal directories.