(ctx context.Context, db *sql.DB, repeatableRead bool)
| 1022 | } |
| 1023 | |
| 1024 | func createConnWithConsistency(ctx context.Context, db *sql.DB, repeatableRead bool) (*sql.Conn, error) { |
| 1025 | conn, err := db.Conn(ctx) |
| 1026 | if err != nil { |
| 1027 | return nil, errors.Trace(err) |
| 1028 | } |
| 1029 | var query string |
| 1030 | if repeatableRead { |
| 1031 | query = "SET SESSION TRANSACTION ISOLATION LEVEL REPEATABLE READ" |
| 1032 | _, err = conn.ExecContext(ctx, query) |
| 1033 | if err != nil { |
| 1034 | return nil, errors.Annotatef(err, "sql: %s", query) |
| 1035 | } |
| 1036 | } |
| 1037 | query = "START TRANSACTION /*!40108 WITH CONSISTENT SNAPSHOT */" |
| 1038 | _, err = conn.ExecContext(ctx, query) |
| 1039 | if err != nil { |
| 1040 | // Some MySQL Compatible databases like Vitess and MemSQL/SingleStore |
| 1041 | // are newer than 4.1.8 (the version comment) but don't actually support |
| 1042 | // `WITH CONSISTENT SNAPSHOT`. So retry without that if the statement fails. |
| 1043 | query = "START TRANSACTION" |
| 1044 | _, err = conn.ExecContext(ctx, query) |
| 1045 | if err != nil { |
| 1046 | return nil, errors.Annotatef(err, "sql: %s", query) |
| 1047 | } |
| 1048 | } |
| 1049 | return conn, nil |
| 1050 | } |
| 1051 | |
| 1052 | // buildSelectField returns the selecting fields' string(joined by comma(`,`)), |
| 1053 | // and the number of writable fields. |
no test coverage detected