encodeColumnData encodes the given data using the given data type OID and returns the result as a string to be used in an INSERT or other DML query.
(mi *pgtype.Map, data interface{}, dataType uint32)
| 814 | // encodeColumnData encodes the given data using the given data type OID and returns the result as a string to be |
| 815 | // used in an INSERT or other DML query. |
| 816 | func encodeColumnData(mi *pgtype.Map, data interface{}, dataType uint32) (string, error) { |
| 817 | var value string |
| 818 | if dt, ok := mi.TypeForOID(dataType); ok { |
| 819 | e := dt.Codec.PlanEncode(mi, dataType, pgtype.TextFormatCode, data) |
| 820 | if e != nil { |
| 821 | encoded, err := e.Encode(data, nil) |
| 822 | if err != nil { |
| 823 | return "", err |
| 824 | } |
| 825 | value = string(encoded) |
| 826 | } else { |
| 827 | // no encoder for this type, use the string representation |
| 828 | value = fmt.Sprintf("%v", data) |
| 829 | } |
| 830 | } else { |
| 831 | value = fmt.Sprintf("%v", data) |
| 832 | } |
| 833 | |
| 834 | // Some types need additional quoting after encoding |
| 835 | switch data := data.(type) { |
| 836 | case string, time.Time, pgtype.Time, bool: |
| 837 | return fmt.Sprintf("'%s'", value), nil |
| 838 | case [16]byte: |
| 839 | // TODO: should we actually register an encoder for this type? |
| 840 | uid := uuid.UUID(data) |
| 841 | return fmt.Sprintf("'%s'", uid.String()), nil |
| 842 | default: |
| 843 | return value, nil |
| 844 | } |
| 845 | } |
no test coverage detected