-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Make TINYINT(1) bool #1805
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
Merged
Merged
Make TINYINT(1) bool #1805
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
405def2
Add TINYINT(1) bool conversion
methane 187219b
Add tinyInt1IsBool option
methane 77556da
Test TINYINT(1) bool option
methane 6ccee6e
Enable TINYINT(1) bool handling by default
methane 0bbc51e
Update TINYINT(1) bool tests for default true
methane 4ba783d
fix test
methane 077fdea
rename tinyint1_test -> boolean_test
methane 86bb61d
fixup
methane File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,151 @@ | ||
| // Go MySQL Driver - A MySQL-Driver for Go's database/sql package | ||
| // | ||
| // Copyright 2026 The Go-MySQL-Driver Authors. All rights reserved. | ||
| // | ||
| // This Source Code Form is subject to the terms of the Mozilla Public | ||
| // License, v. 2.0. If a copy of the MPL was not distributed with this file, | ||
| // You can obtain one at http://mozilla.org/MPL/2.0/. | ||
|
|
||
| package mysql | ||
|
|
||
| import ( | ||
| "database/sql" | ||
| "reflect" | ||
| "strings" | ||
| "testing" | ||
| ) | ||
|
|
||
| func TestTinyInt1IsBoolConfig(t *testing.T) { | ||
| cfg := NewConfig() | ||
| if !cfg.tinyInt1IsBool { | ||
| t.Fatal("tinyInt1IsBool should be enabled by default") | ||
| } | ||
| if got := cfg.FormatDSN(); strings.Contains(got, "tinyInt1IsBool") { | ||
| t.Fatalf("FormatDSN() = %q; default option should be omitted", got) | ||
| } | ||
|
|
||
| if err := cfg.Apply(TinyInt1IsBool(false)); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| if cfg.tinyInt1IsBool { | ||
| t.Fatal("TinyInt1IsBool(false) did not disable the option") | ||
| } | ||
| if got := cfg.FormatDSN(); !strings.Contains(got, "tinyInt1IsBool=false") { | ||
| t.Fatalf("FormatDSN() = %q; want tinyInt1IsBool=false", got) | ||
| } | ||
|
|
||
| cfg, err := ParseDSN("/?tinyInt1IsBool=false") | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| if cfg.tinyInt1IsBool { | ||
| t.Fatal("ParseDSN did not disable tinyInt1IsBool") | ||
| } | ||
|
|
||
| if _, err := ParseDSN("/?tinyInt1IsBool=invalid"); err == nil { | ||
| t.Fatal("ParseDSN accepted invalid tinyInt1IsBool value") | ||
| } | ||
| } | ||
|
|
||
| func TestTinyInt1IsBool(t *testing.T) { | ||
| runTestsParallel(t, dsn, func(dbt *DBTest, tbl string) { | ||
| dbt.mustExec("CREATE TABLE " + tbl + " (" + | ||
| "id INT PRIMARY KEY, " + | ||
| "b TINYINT(1) NOT NULL, " + | ||
| "bn TINYINT(1), " + | ||
| "n TINYINT(2) NOT NULL, " + | ||
| "u TINYINT(1) UNSIGNED NOT NULL)") | ||
| dbt.mustExec("INSERT INTO " + tbl + " VALUES " + | ||
| "(1, 0, NULL, 2, 1), " + | ||
| "(2, 1, 0, 2, 1), " + | ||
| "(3, 2, -1, 2, 1), " + | ||
| "(4, 0, 0, 2, 1)") | ||
|
|
||
| rows := dbt.mustQuery("SELECT b, bn, n, u FROM " + tbl + " ORDER BY id") | ||
| defer rows.Close() | ||
|
|
||
| columnTypes, err := rows.ColumnTypes() | ||
| if err != nil { | ||
| dbt.Fatal(err) | ||
| } | ||
| wantDatabaseTypes := []string{"BOOLEAN", "BOOLEAN", "TINYINT", "UNSIGNED TINYINT"} | ||
| wantScanTypes := []reflect.Type{ | ||
| reflect.TypeFor[bool](), | ||
| reflect.TypeFor[sql.NullBool](), | ||
| scanTypeInt8, | ||
| scanTypeUint8, | ||
| } | ||
| for i, columnType := range columnTypes { | ||
| if got := columnType.DatabaseTypeName(); got != wantDatabaseTypes[i] { | ||
| dbt.Errorf("column %d DatabaseTypeName() = %q; want %q", i, got, wantDatabaseTypes[i]) | ||
| } | ||
| if got := columnType.ScanType(); got != wantScanTypes[i] { | ||
| dbt.Errorf("column %d ScanType() = %v; want %v", i, got, wantScanTypes[i]) | ||
| } | ||
| } | ||
|
|
||
| want := [][4]any{ | ||
| {false, nil, int64(2), int64(1)}, | ||
| {true, false, int64(2), int64(1)}, | ||
| {true, true, int64(2), int64(1)}, | ||
| {false, false, int64(2), int64(1)}, | ||
| } | ||
| row := 0 | ||
| for ; rows.Next(); row++ { | ||
| var got [4]any | ||
| if err := rows.Scan(&got[0], &got[1], &got[2], &got[3]); err != nil { | ||
| dbt.Fatal(err) | ||
| } | ||
| if row >= len(want) { | ||
| dbt.Errorf("unexpected row %d = %#v", row, got) | ||
| continue | ||
| } | ||
| if !reflect.DeepEqual(got, want[row]) { | ||
| dbt.Errorf("row %d = %#v; want %#v", row, got, want[row]) | ||
| } | ||
| } | ||
| if err := rows.Err(); err != nil { | ||
| dbt.Fatal(err) | ||
| } | ||
| if row != len(want) { | ||
| dbt.Errorf("got %d rows; want %d", row, len(want)) | ||
| } | ||
|
|
||
| stmt, err := dbt.db.Prepare("SELECT b, bn, n, u FROM " + tbl + " WHERE id = ?") | ||
| if err != nil { | ||
| dbt.Fatal(err) | ||
| } | ||
| defer stmt.Close() | ||
|
|
||
| for _, id := range []int{3, 4} { | ||
| var got [4]any | ||
| if err := stmt.QueryRow(id).Scan(&got[0], &got[1], &got[2], &got[3]); err != nil { | ||
| dbt.Fatal(err) | ||
| } | ||
| if !reflect.DeepEqual(got, want[id-1]) { | ||
| dbt.Errorf("prepared statement row %d = %#v; want %#v", id, got, want[id-1]) | ||
| } | ||
| } | ||
| }) | ||
| } | ||
|
|
||
| func TestTinyInt1IsBoolDisabled(t *testing.T) { | ||
| runTestsParallel(t, dsn+"&tinyInt1IsBool=false", func(dbt *DBTest, tbl string) { | ||
| dbt.mustExec("CREATE TABLE " + tbl + " (b TINYINT(1) NOT NULL)") | ||
| dbt.mustExec("INSERT INTO " + tbl + " VALUES (2)") | ||
|
|
||
| stmt, err := dbt.db.Prepare("SELECT b FROM " + tbl + " WHERE b = ?") | ||
| if err != nil { | ||
| dbt.Fatal(err) | ||
| } | ||
| defer stmt.Close() | ||
|
|
||
| var got any | ||
| if err := stmt.QueryRow(2).Scan(&got); err != nil { | ||
| dbt.Fatal(err) | ||
| } | ||
| if got != int64(2) { | ||
| dbt.Fatalf("Scan(&any) = %#v; want int64(2)", got) | ||
| } | ||
| }) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick win
Add a language identifier to the fenced code block.
markdownlintreports MD040 for the fence at Line 324. Use a language identifier such astextso the documentation lint passes.Proposed fix
📝 Committable suggestion
🧰 Tools
🪛 markdownlint-cli2 (0.23.2)
[warning] 324-324: Fenced code blocks should have a language specified
(MD040, fenced-code-language)
🤖 Prompt for AI Agents
Source: Linters/SAST tools