Skip to content

BIT(N) columns don't include the N size in their types. #1672

@alarbada

Description

@alarbada

Setup:

CREATE TABLE bit_columns (
    bit1_col BIT(1),
    bit8_col BIT(8),
    bit16_col BIT(16),
    bit64_col BIT(64)
);

Example:

package main

import (
	"database/sql"
	"fmt"
	"log"

	_ "github.com/go-sql-driver/mysql"
)

func main() {
	db, err := sql.Open("mysql", "user:pass@tcp(127.0.0.1:3306)/dbname")
	if err != nil {
		log.Fatal(err)
	}
	defer db.Close()

	rows, err := db.Query("SELECT * FROM bit_columns LIMIT 1")
	if err != nil {
		log.Fatal(err)
	}
	defer rows.Close()

	colTypes, err := rows.ColumnTypes()
	if err != nil {
		log.Fatal(err)
	}

	for _, colType := range colTypes {
		fmt.Println(colType.Name())
		fmt.Println(colType.DecimalSize())
		fmt.Println(colType.DatabaseTypeName())
		fmt.Println(colType.Length())
	}
}

Output:

bit1_col
0 0 false
BIT
0 false // <- why 0?
bit8_col
0 0 false
BIT
0 false // <- why 0?
bit16_col
0 0 false
BIT
0 false // <- why 0?
bit64_col
0 0 false
BIT
0 false // <- why 0?

I would expect that the Length() method would return the mysql specified bit size, or at least have some way to get that piece of information from *sql.ColumnType.

I'm using github.com/go-sql-driver/mysql v1.8.1.

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