go-mysql-org/go-mysql

add value string output

zhujintao opened this issue · 5 comments

rowdata.go

if isNull {
	data[i].Type = FieldValueTypeNull
} else {
	isUnsigned := f[i].Flag&UNSIGNED_FLAG != 0
	data[i].str = v  //add here

Hi, can you explain in which use case you meet the problem?

simple and fast combination of string SQL statements, no need to determine the type.

values := make([]string, 0, len(row))
for _, v := range row {
	if v.Type == mysql.FieldValueTypeNull {
		values = append(values, "NULL")
	} else {
		values = append(values, fmt.Sprintf("'%s'", v.AsString()))
	}
}
// INSERT INTO xx VALUES ('a','b','1','2.0',NULL)

I see that not all MySQL types implements AsString (for example, numbers uses FieldValue.value). So it does not totally solve the problem. And generally concatenating the SQL as string is a bad idea, it has security problems compared to letting SQL driver interpolate the arguments

can also use like String() Raw() methods to output the raw value v, FieldValue.str

can also use like String() Raw() methods to output the raw value v, FieldValue.str

LGTM. Do you have time to write a PR for it? 😄