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.ScanFunc(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) } }
Output:
Index ¶
Examples ¶
Constants ¶
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.
func ScanFunc ¶ added in v1.6.1
ScanFunc returns a split function for a bufio.Scanner that returns each command as a token. In contrast of using the Scan function directly, the command separator can be specified. If comments is true, leading comments are added to each statement and discarded otherwise.
Types ¶
This section is empty.