Skip to content

Commit 40b2796

Browse files
committed
sqldb: add SQLStrValid helper
The SQL* helpers are meant to always set the `Valid` field of the sql.Null* type to true. Otherwise they cannot be used to set a valid, empty field. However, we dont want to break the behaviour of the existing SQLStr helper and so this commit adds a new helper with the desired functionality.
1 parent ea6cc81 commit 40b2796

File tree

3 files changed

+19
-4
lines changed

3 files changed

+19
-4
lines changed

graph/db/sql_migration.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1380,8 +1380,8 @@ func insertNodeSQLMig(ctx context.Context, db SQLQueries,
13801380

13811381
if node.HaveNodeAnnouncement {
13821382
params.LastUpdate = sqldb.SQLInt64(node.LastUpdate.Unix())
1383-
params.Color = sqldb.SQLStr(EncodeHexColor(node.Color))
1384-
params.Alias = sqldb.SQLStr(node.Alias)
1383+
params.Color = sqldb.SQLStrValid(EncodeHexColor(node.Color))
1384+
params.Alias = sqldb.SQLStrValid(node.Alias)
13851385
params.Signature = node.AuthSigBytes
13861386
}
13871387

graph/db/sql_store.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3391,8 +3391,8 @@ func upsertNode(ctx context.Context, db SQLQueries,
33913391

33923392
if node.HaveNodeAnnouncement {
33933393
params.LastUpdate = sqldb.SQLInt64(node.LastUpdate.Unix())
3394-
params.Color = sqldb.SQLStr(EncodeHexColor(node.Color))
3395-
params.Alias = sqldb.SQLStr(node.Alias)
3394+
params.Color = sqldb.SQLStrValid(EncodeHexColor(node.Color))
3395+
params.Alias = sqldb.SQLStrValid(node.Alias)
33963396
params.Signature = node.AuthSigBytes
33973397
}
33983398

sqldb/sqlutils.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ func SQLInt64[T constraints.Integer](num T) sql.NullInt64 {
4949

5050
// SQLStr turns a string into the NullString that sql/sqlc uses when a string
5151
// can be permitted to be NULL.
52+
//
53+
// NOTE: If the input string is empty, it returns a NullString with Valid set to
54+
// false. If this is not the desired behavior, consider using SQLStrValid
55+
// instead.
5256
func SQLStr(s string) sql.NullString {
5357
if s == "" {
5458
return sql.NullString{}
@@ -60,6 +64,17 @@ func SQLStr(s string) sql.NullString {
6064
}
6165
}
6266

67+
// SQLStrValid turns a string into the NullString that sql/sqlc uses when a
68+
// string can be permitted to be NULL.
69+
//
70+
// NOTE: Valid is always set to true, even if the input string is empty.
71+
func SQLStrValid(s string) sql.NullString {
72+
return sql.NullString{
73+
String: s,
74+
Valid: true,
75+
}
76+
}
77+
6378
// SQLTime turns a time.Time into the NullTime that sql/sqlc uses when a time
6479
// can be permitted to be NULL.
6580
func SQLTime(t time.Time) sql.NullTime {

0 commit comments

Comments
 (0)