Skip to content

Commit 5736d09

Browse files
DengChanoushu1dengxingchen1
authored andcommitted
ColumnTypeLength method return the type length for varbit type.
1 parent e214c23 commit 5736d09

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
@@ -676,6 +676,8 @@ func (r *Rows) ColumnTypeLength(index int) (int64, bool) {
676676
return math.MaxInt64, true
677677
case pgtype.VarcharOID, pgtype.BPCharArrayOID:
678678
return int64(fd.TypeModifier - varHeaderSize), true
679+
case pgtype.VarbitOID:
680+
return int64(fd.TypeModifier), true
679681
default:
680682
return 0, false
681683
}

stdlib/sql_test.go

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

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

0 commit comments

Comments
 (0)