github.com/goplus/yap@v0.8.1/ydb/mysql/mysql.go (about) 1 /* 2 * Copyright (c) 2024 The GoPlus Authors (goplus.org). All rights reserved. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package mysql 18 19 import ( 20 "log" 21 "os" 22 23 "github.com/go-sql-driver/mysql" 24 "github.com/goplus/yap/ydb" 25 ) 26 27 // Register registers a user-defined testDataSource for `mysql` engine. 28 // eg. testDataSource = `root@/test?autodrop` 29 func Register(testDataSource string) { 30 ydb.Register(&ydb.Engine{ 31 Name: "mysql", 32 TestSource: testDataSource, 33 WrapErr: wrapErr, 34 }) 35 } 36 37 func init() { 38 ydb.Register(&ydb.Engine{ 39 Name: "mysql", 40 TestSource: testDataSource, 41 WrapErr: wrapErr, 42 }) 43 } 44 45 const ( 46 ER_DUP_ENTRY = 1062 47 ) 48 49 func wrapErr(prompt string, err error) error { 50 if prompt == "insert:" { 51 if e, ok := err.(*mysql.MySQLError); ok && e.Number == ER_DUP_ENTRY { 52 return ydb.ErrDuplicated 53 } 54 } 55 return err 56 } 57 58 func testDataSource() string { 59 dataSource := os.Getenv("YDB_MYSQL_TEST") 60 if dataSource == "" { 61 log.Panicln("env `YDB_MYSQL_TEST` not found, please set it before running") 62 } 63 return dataSource 64 }