PostgresValue

public struct PostgresValue : PostgresValueConvertible, Equatable, CustomStringConvertible

A value to be sent to or returned from the Postgres server.

PostgresClientKit extends standard Swift types to conform to the PostgresValueConvertible protocol, making it easy to convert instances of those types to PostgresValue. For example:

// Convert String to PostgresValue.
let greeting = "Hello, world!"
let greetingValue = greeting.postgresValue

// Convert Double to PostgresValue.
let pi = 3.14
let piValue = pi.postgresValue

// Convert an optional Int to PostgresValue.
let score: Int? = nil
let scoreValue = score.postgresValue

Use PostgresValue methods to convert PostgresValue instances back to standard Swift types. These methods throw errors if the conversion fails.

try greetingValue.string()      // "Hello, world!"
try greetingValue.int()         // throws PostgresError.valueConversionError

try piValue.double()            // 3.14
try piValue.string()            // "3.14"

try scoreValue.optionalInt()    // nil
try scoreValue.int()            // throws PostgresError.valueIsNil
  • Creates a PostgresValue from the raw value used in the Postgres network protocol.

    Declaration

    Swift

    public init(_ rawValue: String?)

    Parameters

    rawValue

    the raw value, or nil to represent a SQL NULL value

  • The raw value used in the Postgres network protocol, or nil to represent a SQL NULL value.

    Declaration

    Swift

    public let rawValue: String?
  • Whether this PostgresValue represents a SQL NULL value.

    Declaration

    Swift

    public var isNull: Bool { get }
  • Converts this PostgresValue to a String.

    Throws

    PostgresError if the conversion fails

    Declaration

    Swift

    func string() throws -> String

    Return Value

    the String

  • Converts this PostgresValue to an optional String.

    Throws

    PostgresError if the conversion fails

    Declaration

    Swift

    func optionalString() throws -> String?

    Return Value

    the optional String

  • Converts this PostgresValue to an Int.

    Throws

    PostgresError if the conversion fails

    Declaration

    Swift

    func int() throws -> Int

    Return Value

    the Int

  • Converts this PostgresValue to an optional Int.

    Throws

    PostgresError if the conversion fails

    Declaration

    Swift

    func optionalInt() throws -> Int?

    Return Value

    the optional Int

  • Converts this PostgresValue to a Double.

    Throws

    PostgresError if the conversion fails

    Declaration

    Swift

    func double() throws -> Double

    Return Value

    the Double

  • Converts this PostgresValue to an optional Double.

    Throws

    PostgresError if the conversion fails

    Declaration

    Swift

    func optionalDouble() throws -> Double?

    Return Value

    the optional Double

  • Converts this PostgresValue to a Decimal.

    Throws

    PostgresError if the conversion fails

    Declaration

    Swift

    func decimal() throws -> Decimal

    Return Value

    the Decimal

  • Converts this PostgresValue to an optional Decimal.

    Throws

    PostgresError if the conversion fails

    Declaration

    Swift

    func optionalDecimal() throws -> Decimal?

    Return Value

    the optional Decimal

  • Converts this PostgresValue to a Bool.

    Throws

    PostgresError if the conversion fails

    Declaration

    Swift

    func bool() throws -> Bool

    Return Value

    the Bool

  • Converts this PostgresValue to an optional Bool

    Throws

    PostgresError if the conversion fails

    Declaration

    Swift

    func optionalBool() throws -> Bool?

    Return Value

    the optional Bool

  • The PostgresValue itself.

    Declaration

    Swift

    var postgresValue: PostgresValue { get }
  • True if lhs.rawValue == rhs.rawValue.

    Declaration

    Swift

    static func == (lhs: PostgresValue, rhs: PostgresValue) -> Bool
  • A short string that describes this PostgresValue.

    Declaration

    Swift

    var description: String { get }