Skip to content

How to properly create a function #1072

Closed
@relax-admin

Description

@relax-admin

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions