Row
public struct Row : CustomStringConvertible
A Row
exposed by a Cursor
.
-
The values of the columns for this
Row
.Declaration
Swift
public var columns: [PostgresValue]
-
Decodes this
Row
to create an instance of the specified type.The type specified must conform to the
Decodable
protocol. This method uses the column metadata provided byCursor.columns
to create a new instance of that type whose stored properties are set to the values of like-namedcolumns
. (To make this column metadata available, setretrieveColumnMetadata
totrue
in callingStatement.execute(parameterValues:retrieveColumnMetadata:)
.)The supported property types are a superset of the types supported by
PostgresValue
:Type of stored property Conversion performed Bool
postgresValue.bool()
String
postgresValue.string()
Double
postgresValue.double()
Float
Float(postgresValue.double())
Int
postgresValue.int()
Int8
Int8(postgresValue.string())
Int16
Int16(postgresValue.string())
Int32
Int32(postgresValue.string())
Int64
Int64(postgresValue.string())
UInt
UInt(postgresValue.string())
UInt8
UInt8(postgresValue.string())
UInt16
UInt16(postgresValue.string())
UInt32
UInt32(postgresValue.string())
UInt64
UInt64(postgresValue.string())
PostgresByteA
postgresValue.byteA()
PostgresTimestampWithTimeZone
postgresValue.timestampWithTimeZone()
PostgresTimestamp
postgresValue.timestamp()
PostgresDate
postgresValue.date()
PostgresTime
postgresValue.time()
PostgresTimeWithTimeZone
postgresValue.timeWithTimeZone()
Date
see below Foundation
Date
stored properties are decoded as follows:postgresValue.timestampWithTimeZone().date
, if successful;- otherwise
postgresValue.timestamp().date(in: defaultTimeZone)
, if successful; - otherwise
postgresValue.date().date(in: defaultTimeZone)
, if successful; - otherwise
postgresValue.time().date(in: defaultTimeZone)
, if successful; - otherwise
postgresValue.timeWithTimeZone().date
, if successful
(Instead of
Date
, consider usingPostgresTimestampWithTimeZone
,PostgresTimestamp
,PostgresDate
,PostgresTime
, andPostgresTimeWithTimeZone
whenever possible.)Example:
struct Weather: Decodable { let date: PostgresDate let city: String let temp_lo: Int let temp_hi: Int let prcp: Double? } let connection: Connection = ... // Note that the columns must have the same names as the Weather // properties, but may be in a different order. let text = "SELECT city, temp_lo, temp_hi, prcp, date FROM weather;" let statement = try connection.prepareStatement(text: text) let cursor = try statement.execute(retrieveColumnMetadata: true) for row in cursor { let weather = try row.get().decodeByColumnName(Weather.self) ... }
Throws
PostgresError.columnMetadataNotAvailable
if column metadata is not available;DecodingError
if the operation otherwise failsDeclaration
Swift
public func decodeByColumnName<T: Decodable>(_ type: T.Type, defaultTimeZone: TimeZone? = nil) throws -> T
Parameters
type
the type of instance to create
defaultTimeZone
the default time zone for certain conversions to Foundation
Date
(see above); ifnil
then the UTC time zone is usedReturn Value
an instance of the specified type
-
Decodes this
Row
to create an instance of the specified type.The type specified must conform to the
Decodable
protocol. This method matchescolumns
to stored properties based on decoding order: the first property decoded is assigned the value ofcolumns[0]
, the second property is assigned the value ofcolumns[1]
, and so on. By default, aDecodable
type decodes its properties in declaration order. This default behavior can be overridden by providing implementations of theCodingKeys
enum and theinit(from:)
initializer. Refer to Apple’s developer documentation for further information.The supported property types are a superset of the types supported by
PostgresValue
:Type of stored property Conversion performed Bool
postgresValue.bool()
String
postgresValue.string()
Double
postgresValue.double()
Float
Float(postgresValue.double())
Int
postgresValue.int()
Int8
Int8(postgresValue.string())
Int16
Int16(postgresValue.string())
Int32
Int32(postgresValue.string())
Int64
Int64(postgresValue.string())
UInt
UInt(postgresValue.string())
UInt8
UInt8(postgresValue.string())
UInt16
UInt16(postgresValue.string())
UInt32
UInt32(postgresValue.string())
UInt64
UInt64(postgresValue.string())
PostgresByteA
postgresValue.byteA()
PostgresTimestampWithTimeZone
postgresValue.timestampWithTimeZone()
PostgresTimestamp
postgresValue.timestamp()
PostgresDate
postgresValue.date()
PostgresTime
postgresValue.time()
PostgresTimeWithTimeZone
postgresValue.timeWithTimeZone()
Date
see below Foundation
Date
stored properties are decoded as follows:postgresValue.timestampWithTimeZone().date
, if successful;- otherwise
postgresValue.timestamp().date(in: defaultTimeZone)
, if successful; - otherwise
postgresValue.date().date(in: defaultTimeZone)
, if successful; - otherwise
postgresValue.time().date(in: defaultTimeZone)
, if successful; - otherwise
postgresValue.timeWithTimeZone().date
, if successful
(Instead of
Date
, consider usingPostgresTimestampWithTimeZone
,PostgresTimestamp
,PostgresDate
,PostgresTime
, andPostgresTimeWithTimeZone
whenever possible.)Example:
struct Weather: Decodable { let city: String let lowestTemperature: Int let highestTemperature: Int let precipitation: Double? let date: PostgresDate } let connection: Connection = ... // Notice that the columns must be in the same order as the Weather // properties, but may have different names. let text = "SELECT city, temp_lo, temp_hi, prcp, date FROM weather;" let statement = try connection.prepareStatement(text: text) let cursor = try statement.execute() for row in cursor { let weather = try row.get().decodeByColumnIndex(Weather.self) ... }
Throws
DecodingError
if the operation failsDeclaration
Swift
public func decodeByColumnIndex<T: Decodable>(_ type: T.Type, defaultTimeZone: TimeZone? = nil) throws -> T
Parameters
type
the type of instance to create
defaultTimeZone
the default time zone for certain conversions to Foundation
Date
(see above); ifnil
then the UTC time zone is usedReturn Value
an instance of the specified type
-
A string representation of this
Row
.Declaration
Swift
public var description: String { get }