Skip to content

Commit 6696a19

Browse files
authored
Add length support for bit and varbit type (#743)
ColumnType is the interface to obtain length, precision, scale of a database column. The lib/pq implementation only support char, varchar, text, bytea. Bit and varbit support is missing. Signed-off-by: Huiliang Liu <[email protected]>
1 parent fb12405 commit 6696a19

File tree

3 files changed

+29
-4
lines changed

3 files changed

+29
-4
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ newer. Previously PostgreSQL 8.4 and newer were supported.
4747

4848
- Decode bpchar into a string ([#949]).
4949

50-
- Recognize float types in ColumnTypeScanType() ([#1166]).
50+
- Recognize bit/varbit ([#743]) and float types ([#1166]) in ColumnTypeScanType().
5151

5252
- Accept `PGGSSLIB` and `PGKRBSRVNAME` environment variables ([#1143]).
5353

@@ -57,6 +57,7 @@ newer. Previously PostgreSQL 8.4 and newer were supported.
5757

5858
[#595]: https://github.com/lib/pq/pull/595
5959
[#745]: https://github.com/lib/pq/pull/745
60+
[#743]: https://github.com/lib/pq/pull/743
6061
[#838]: https://github.com/lib/pq/pull/838
6162
[#949]: https://github.com/lib/pq/pull/949
6263
[#1125]: https://github.com/lib/pq/pull/1125

rows.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ func (fd fieldDesc) Type() reflect.Type {
3333
return reflect.TypeOf(float64(0))
3434
case oid.T_float4:
3535
return reflect.TypeOf(float32(0))
36-
case oid.T_varchar, oid.T_text:
36+
case oid.T_varchar, oid.T_text, oid.T_varbit, oid.T_bit:
3737
return reflect.TypeOf("")
3838
case oid.T_bool:
3939
return reflect.TypeOf(false)
@@ -56,6 +56,8 @@ func (fd fieldDesc) Length() (length int64, ok bool) {
5656
return math.MaxInt64, true
5757
case oid.T_varchar, oid.T_bpchar:
5858
return int64(fd.Mod - headerSize), true
59+
case oid.T_varbit, oid.T_bit:
60+
return int64(fd.Mod), true
5961
default:
6062
return 0, false
6163
}

rows_test.go

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ func TestDataTypeName(t *testing.T) {
1919
{oid.T_int2, "INT2"},
2020
{oid.T_varchar, "VARCHAR"},
2121
{oid.T_text, "TEXT"},
22+
{oid.T_bit, "BIT"},
23+
{oid.T_varbit, "VARBIT"},
2224
{oid.T_bool, "BOOL"},
2325
{oid.T_numeric, "NUMERIC"},
2426
{oid.T_date, "DATE"},
@@ -47,6 +49,8 @@ func TestDataType(t *testing.T) {
4749
{oid.T_int2, reflect.Int16},
4850
{oid.T_varchar, reflect.String},
4951
{oid.T_text, reflect.String},
52+
{oid.T_bit, reflect.String},
53+
{oid.T_varbit, reflect.String},
5054
{oid.T_bool, reflect.Bool},
5155
{oid.T_date, reflect.Struct},
5256
{oid.T_time, reflect.Struct},
@@ -76,6 +80,8 @@ func TestDataTypeLength(t *testing.T) {
7680
{oid.T_varchar, 65535, 9, 5, true},
7781
{oid.T_text, 65535, -1, math.MaxInt64, true},
7882
{oid.T_bytea, 65535, -1, math.MaxInt64, true},
83+
{oid.T_bit, 0, 10, 10, true},
84+
{oid.T_varbit, 0, 10, 10, true},
7985
}
8086

8187
for i, tt := range tts {
@@ -160,6 +166,20 @@ func TestRowsColumnTypes(t *testing.T) {
160166
DecimalSize: decimalSize{Precision: 0, Scale: 0, OK: false},
161167
ScanType: reflect.TypeOf(float64(0)),
162168
},
169+
{
170+
Name: "bit4",
171+
TypeName: "BIT",
172+
Length: length{Len: 4, OK: true},
173+
DecimalSize: decimalSize{Precision: 0, Scale: 0, OK: false},
174+
ScanType: reflect.TypeOf(""),
175+
},
176+
{
177+
Name: "varbit10",
178+
TypeName: "VARBIT",
179+
Length: length{Len: 10, OK: true},
180+
DecimalSize: decimalSize{Precision: 0, Scale: 0, OK: false},
181+
ScanType: reflect.TypeOf(""),
182+
},
163183
}
164184

165185
db := pqtest.MustDB(t)
@@ -169,7 +189,9 @@ func TestRowsColumnTypes(t *testing.T) {
169189
1 as a,
170190
text 'bar' as bar,
171191
1.28::numeric(9, 2) as dec,
172-
3.1415::float8 as f
192+
3.1415::float8 as f,
193+
'1111'::bit(4) as bit4,
194+
'1111'::varbit(10) as varbit10
173195
`)
174196
if err != nil {
175197
t.Fatal(err)
@@ -180,7 +202,7 @@ func TestRowsColumnTypes(t *testing.T) {
180202
if err != nil {
181203
t.Fatal(err)
182204
}
183-
if len(columns) != 4 {
205+
if len(columns) != 6 {
184206
t.Errorf("expected 4 columns found %d", len(columns))
185207
}
186208

0 commit comments

Comments
 (0)