InternStringNullable interns one or more optional string values and returns their IDs as sql.NullInt64. Returns sql.NullInt64{Valid: false} for any value pointer that is nil or points to an empty string. This is designed for use within transactions. Performance: For a single string, uses a fast-pat
(ctx context.Context, tx TxQuerier, values ...*string)
| 118 | // Performance: For a single string, uses a fast-path. For multiple strings, collects non-empty values |
| 119 | // and delegates to InternStrings for efficient batch processing. |
| 120 | func InternStringNullable(ctx context.Context, tx TxQuerier, values ...*string) ([]sql.NullInt64, error) { |
| 121 | if len(values) == 0 { |
| 122 | return []sql.NullInt64{}, nil |
| 123 | } |
| 124 | |
| 125 | // Fast path for single string |
| 126 | if len(values) == 1 { |
| 127 | if values[0] == nil || *values[0] == "" { |
| 128 | return []sql.NullInt64{{Valid: false}}, nil |
| 129 | } |
| 130 | |
| 131 | ids, err := InternStrings(ctx, tx, *values[0]) |
| 132 | if err != nil { |
| 133 | return nil, err |
| 134 | } |
| 135 | |
| 136 | return []sql.NullInt64{{Int64: ids[0], Valid: true}}, nil |
| 137 | } |
| 138 | |
| 139 | // Batch path: collect non-empty values and track their positions |
| 140 | results := make([]sql.NullInt64, len(values)) |
| 141 | var nonEmptyValues []string |
| 142 | var positions []int |
| 143 | |
| 144 | for i, v := range values { |
| 145 | if v == nil || *v == "" { |
| 146 | results[i] = sql.NullInt64{Valid: false} |
| 147 | continue |
| 148 | } |
| 149 | nonEmptyValues = append(nonEmptyValues, *v) |
| 150 | positions = append(positions, i) |
| 151 | } |
| 152 | |
| 153 | // If no non-empty values, return early |
| 154 | if len(nonEmptyValues) == 0 { |
| 155 | return results, nil |
| 156 | } |
| 157 | |
| 158 | // Intern all non-empty values (InternStrings handles deduplication internally) |
| 159 | ids, err := InternStrings(ctx, tx, nonEmptyValues...) |
| 160 | if err != nil { |
| 161 | return nil, err |
| 162 | } |
| 163 | |
| 164 | // Map IDs back to original positions |
| 165 | for i, pos := range positions { |
| 166 | results[pos] = sql.NullInt64{Int64: ids[i], Valid: true} |
| 167 | } |
| 168 | |
| 169 | return results, nil |
| 170 | } |
| 171 | |
| 172 | // InternEmptyString ensures the empty string exists in string_pool and returns its ID. |
| 173 | // This is needed for special cases like localhost bypass auth where an empty username |
no test coverage detected