Skip to content

Commit 2cf78dd

Browse files
authored
Merge pull request #2448 from DengChan/column_type_lenth_varbit
ColumnTypeLength method return the type length for varbit type.
2 parents 2d1c4ef + 5736d09 commit 2cf78dd

File tree

2 files changed

+66
-0
lines changed

2 files changed

+66
-0
lines changed

stdlib/sql.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -664,6 +664,8 @@ func (r *Rows) ColumnTypeLength(index int) (int64, bool) {
664664
return math.MaxInt64, true
665665
case pgtype.VarcharOID, pgtype.BPCharArrayOID:
666666
return int64(fd.TypeModifier - varHeaderSize), true
667+
case pgtype.VarbitOID:
668+
return int64(fd.TypeModifier), true
667669
default:
668670
return 0, false
669671
}

stdlib/sql_test.go

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1403,3 +1403,67 @@ func TestOptionShouldPing_HookCalledOnReuse(t *testing.T) {
14031403

14041404
require.True(t, hookCalled, "hook should be called on reuse")
14051405
}
1406+
1407+
func TestRowsColumnTypeLength(t *testing.T) {
1408+
testWithAllQueryExecModes(t, func(t *testing.T, db *sql.DB) {
1409+
columnTypeLengthTests := []struct {
1410+
Len int64
1411+
OK bool
1412+
}{
1413+
{
1414+
math.MaxInt64,
1415+
true,
1416+
},
1417+
{
1418+
math.MaxInt64,
1419+
true,
1420+
},
1421+
{
1422+
255,
1423+
true,
1424+
},
1425+
{
1426+
10,
1427+
true,
1428+
},
1429+
{
1430+
50,
1431+
true,
1432+
},
1433+
{
1434+
0,
1435+
false,
1436+
},
1437+
}
1438+
1439+
_, err := db.Exec(`CREATE TEMPORARY TABLE temp_column_type_length (
1440+
text_column TEXT,
1441+
bytea_column BYTEA,
1442+
varchar_column VARCHAR(255),
1443+
bpcharA_column BPCHAR(10)[],
1444+
varbit_column VARBIT(50),
1445+
int_column INT
1446+
);`)
1447+
require.NoError(t, err)
1448+
1449+
rows, err := db.Query("SELECT * FROM temp_column_type_length")
1450+
require.NoError(t, err)
1451+
1452+
columns, err := rows.ColumnTypes()
1453+
require.NoError(t, err)
1454+
assert.Len(t, columns, 6)
1455+
1456+
for i, tt := range columnTypeLengthTests {
1457+
c := columns[i]
1458+
1459+
l, ok := c.Length()
1460+
if l != tt.Len {
1461+
t.Errorf("(%d) got: %d, want: %d", i, l, tt.Len)
1462+
}
1463+
if ok != tt.OK {
1464+
t.Errorf("(%d) got: %t, want: %t", i, ok, tt.OK)
1465+
}
1466+
1467+
}
1468+
})
1469+
}

0 commit comments

Comments
 (0)