2
0

Removes name length convenience method

This commit is contained in:
Manni Wood
2016-10-01 13:46:48 -04:00
parent ca96431b5e
commit c8575984d8
2 changed files with 12 additions and 41 deletions
+12 -16
View File
@@ -66,12 +66,6 @@ const maxUint = ^uint(0)
const maxInt = int(maxUint >> 1) const maxInt = int(maxUint >> 1)
const minInt = -maxInt - 1 const minInt = -maxInt - 1
// NameDataLen is the same as PostgreSQL's NAMEDATALEN, defined in
// src/include/pg_config_manual.h. It is how many bytes long identifiers
// are allowed to be, including the trailing '\0' at the end of C strings.
// (Identifieres are table names, column names, function names, etc.)
const NameDataLen = 64
// DefaultTypeFormats maps type names to their default requested format (text // DefaultTypeFormats maps type names to their default requested format (text
// or binary). In theory the Scanner interface should be the one to determine // or binary). In theory the Scanner interface should be the one to determine
// the format of the returned values. However, the query has already been // the format of the returned values. However, the query has already been
@@ -267,19 +261,21 @@ func (n NullString) Encode(w *WriteBuf, oid Oid) error {
return encodeString(w, oid, n.String) return encodeString(w, oid, n.String)
} }
// The pgx.Name type is for PostgreSQL's special 63-byte // Name is a type used for PostgreSQL's special 63-byte
// name data type, used for identifiers like table names. // name data type, used for identifiers like table names.
// The pg_class.relname column is a good example of where the
// name data type is used.
//
// Note that the underlying Go data type of pgx.Name is string,
// so there is no way to enforce the 63-byte length. Inputting
// a longer name into PostgreSQL will result in silent truncation
// to 63 bytes.
//
// Also, if you have custom-compiled PostgreSQL and set
// NAMEDATALEN to a different value, obviously that number of
// bytes applies, rather than the default 63.
type Name string type Name string
// LengthOK is a convenience method that returns false if a name is longer
// than PostgreSQL will allow. PostgreSQL identifiers are allowed
// to be 63 bytes long (NAMEDATALEN in the PostgreSQL source code
// is defined as 64 bytes long, but the 64th char is the '\0' C
// string terminator.)
func (n Name) LengthOK() bool {
return len(string(n)) < NameDataLen
}
// NullName represents a pgx.Name that may be null. NullName implements the // NullName represents a pgx.Name that may be null. NullName implements the
// Scanner and Encoder interfaces so it may be used both as an argument to // Scanner and Encoder interfaces so it may be used both as an argument to
// Query[Row] and a destination for Scan for prepared and unprepared queries. // Query[Row] and a destination for Scan for prepared and unprepared queries.
-25
View File
@@ -551,28 +551,6 @@ func TestInetCidrTranscodeWithJustIP(t *testing.T) {
} }
} }
func TestNameLengthOK(t *testing.T) {
tests := []struct {
input pgx.Name
expected bool
}{
{"", true},
{"1234", true},
{"123456789012345678901234567890123456789012345678901234567890123", true},
{"1234567890123456789012345678901234567890123456789012345678901234", false},
}
var actual bool
for i, tt := range tests {
actual = tt.input.LengthOK()
if actual != tt.expected {
t.Errorf("%d. Expected %v, got %v (name -> %v)", i, tt.expected, actual, tt.input)
}
}
}
func TestNullX(t *testing.T) { func TestNullX(t *testing.T) {
t.Parallel() t.Parallel()
@@ -621,9 +599,6 @@ func TestNullX(t *testing.T) {
{"select $1::\"char\"", []interface{}{pgx.NullChar{Char: 255, Valid: true}}, []interface{}{&actual.c}, allTypes{c: pgx.NullChar{Char: 255, Valid: true}}}, {"select $1::\"char\"", []interface{}{pgx.NullChar{Char: 255, Valid: true}}, []interface{}{&actual.c}, allTypes{c: pgx.NullChar{Char: 255, Valid: true}}},
{"select $1::name", []interface{}{pgx.NullString{String: "foo", Valid: true}}, []interface{}{&actual.s}, allTypes{s: pgx.NullString{String: "foo", Valid: true}}}, {"select $1::name", []interface{}{pgx.NullString{String: "foo", Valid: true}}, []interface{}{&actual.s}, allTypes{s: pgx.NullString{String: "foo", Valid: true}}},
{"select $1::name", []interface{}{pgx.NullString{String: "foo", Valid: false}}, []interface{}{&actual.s}, allTypes{s: pgx.NullString{String: "", Valid: false}}}, {"select $1::name", []interface{}{pgx.NullString{String: "foo", Valid: false}}, []interface{}{&actual.s}, allTypes{s: pgx.NullString{String: "", Valid: false}}},
// bytes past NameDataLen-1 (63 bytes) get silently truncated by PostgreSQL
{"select $1::name", []interface{}{pgx.NullString{String: "1234567890123456789012345678901234567890123456789012345678901234", Valid: true}},
[]interface{}{&actual.s}, allTypes{s: pgx.NullString{String: "123456789012345678901234567890123456789012345678901234567890123", Valid: true}}},
{"select $1::cid", []interface{}{pgx.NullCid{Cid: 1, Valid: true}}, []interface{}{&actual.cid}, allTypes{cid: pgx.NullCid{Cid: 1, Valid: true}}}, {"select $1::cid", []interface{}{pgx.NullCid{Cid: 1, Valid: true}}, []interface{}{&actual.cid}, allTypes{cid: pgx.NullCid{Cid: 1, Valid: true}}},
{"select $1::cid", []interface{}{pgx.NullCid{Cid: 1, Valid: false}}, []interface{}{&actual.cid}, allTypes{cid: pgx.NullCid{Cid: 0, Valid: false}}}, {"select $1::cid", []interface{}{pgx.NullCid{Cid: 1, Valid: false}}, []interface{}{&actual.cid}, allTypes{cid: pgx.NullCid{Cid: 0, Valid: false}}},
{"select $1::cid", []interface{}{pgx.NullCid{Cid: 4294967295, Valid: true}}, []interface{}{&actual.cid}, allTypes{cid: pgx.NullCid{Cid: 4294967295, Valid: true}}}, {"select $1::cid", []interface{}{pgx.NullCid{Cid: 4294967295, Valid: true}}, []interface{}{&actual.cid}, allTypes{cid: pgx.NullCid{Cid: 4294967295, Valid: true}}},