Skip to content

How to properly create a function #1072

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
relax-admin opened this issue Mar 14, 2020 · 2 comments
Closed

How to properly create a function #1072

relax-admin opened this issue Mar 14, 2020 · 2 comments

Comments

@relax-admin
Copy link

Issue description

docker-entrypoint-initdb.d of mysql docker already supports delimiter, but go-sql-driver still does not support this syntax, resulting in failure to create sql functions,If delimiter is not supported, how can I create a sql function correctly

Example code

  1. About docker-entrypoint-initdb.d test, please click the link to view
    https://github.com/relax-space/test-sql-driver-function.git

  2. For the test of go-sql-driver, please see the following code

package main

import (
	"database/sql"
	"fmt"

	_ "github.com/go-sql-driver/mysql"
)

func main() {
	db, err := sql.Open("mysql", "root:1234@tcp(127.0.0.1:3309)/fruit?charset=utf8&parseTime=True&loc=UTC")
	if err != nil {
		panic(err)
	}
	sql := `
CREATE FUNCTION hello1 (s CHAR(20))
RETURNS CHAR(50) DETERMINISTIC
RETURN CONCAT('Hello1, ',s,'!');
`
	rows, err := db.Exec(sql)
	if err != nil {
		panic(err)
	}
	fmt.Println("hello1 success", rows) //success

	sql = `
DROP FUNCTION IF EXISTS hello2;

DELIMITER $$
CREATE FUNCTION hello2(labelIds varchar(500)) 
RETURNS varchar(1000)
BEGIN
    RETURN CONCAT('Hello, ',s,'!');
END $$
DELIMITER ;
`
	rows, err = db.Exec(sql)
	if err != nil { //failure
		err = fmt.Errorf("hello2 failure,%v", err)
		panic(err)
	}
	fmt.Println(rows)
}

Error log

PS D:\source\gopath\src\test-sql-driver-function> go run .
hello1 success {0xc0000aa100 0xc00001e240}
panic: hello2 failure,Error 1064: You have an error in your SQL syntax; check the 
manual that corresponds to your MySQL server version for the right syntax to use near 'DELIMITER $$
CREATE FUNCTION hello2(labelIds varchar(500))
RETURNS varchar(1000' at line 3

goroutine 1 [running]:
main.main()
        D:/source/gopath/src/test-sql-driver-function/main.go:40 +0x2d1
exit status 2

Configuration

Driver version (or git SHA): 14bb9c0 (HEAD -> master) Add go.mod (#1003)

Go version: go version go1.13.8 windows/amd64

Server version: MySQL 5.7.22

Server OS: Windows 10

@methane
Copy link
Member

methane commented Mar 14, 2020

Try this https://github.com/go-sql-driver/mysql#multistatements

@relax-admin
Copy link
Author

relax-admin commented Mar 15, 2020

@methane
Thank you very much, this is exactly what I want

root:1234@tcp(127.0.0.1:3309)/fruit?charset=utf8&parseTime=True&loc=UTC&multiStatements=true

package main

import (
	"database/sql"
	"fmt"

	_ "github.com/go-sql-driver/mysql"
)

func main() {
	db, err := sql.Open("mysql", "root:1234@tcp(127.0.0.1:3309)/fruit?charset=utf8&parseTime=True&loc=UTC&multiStatements=true")
	if err != nil {
		panic(err)
	}
	sql := `
DROP FUNCTION IF EXISTS hello1;
CREATE FUNCTION hello1 (s CHAR(20))
RETURNS CHAR(50) DETERMINISTIC
RETURN CONCAT('Hello1, ',s,'!');
`
	rows, err := db.Exec(sql)
	if err != nil {
		panic(err)
	}
	fmt.Println("hello1 success", rows) //success

	sql = `
DROP FUNCTION IF EXISTS hello2;

CREATE FUNCTION hello2(labelIds varchar(500)) 
RETURNS varchar(1000)
BEGIN
    RETURN CONCAT('Hello, ',s,'!');
END
`
	rows, err = db.Exec(sql)
	if err != nil { //failure
		err = fmt.Errorf("hello2 failure,%v", err)
		panic(err)
	}
	fmt.Println("hello2 success", rows) //success
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants