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
Rowto create an instance of the specified type.The type specified must conform to the
Decodableprotocol. This method uses the column metadata provided byCursor.columnsto create a new instance of that type whose stored properties are set to the values of like-namedcolumns. (To make this column metadata available, setretrieveColumnMetadatatotruein callingStatement.execute(parameterValues:retrieveColumnMetadata:).)The supported property types are a superset of the types supported by
PostgresValue:Type of stored property Conversion performed BoolpostgresValue.bool()StringpostgresValue.string()DoublepostgresValue.double()FloatFloat(postgresValue.double())IntpostgresValue.int()Int8Int8(postgresValue.string())Int16Int16(postgresValue.string())Int32Int32(postgresValue.string())Int64Int64(postgresValue.string())UIntUInt(postgresValue.string())UInt8UInt8(postgresValue.string())UInt16UInt16(postgresValue.string())UInt32UInt32(postgresValue.string())UInt64UInt64(postgresValue.string())PostgresByteApostgresValue.byteA()PostgresTimestampWithTimeZonepostgresValue.timestampWithTimeZone()PostgresTimestamppostgresValue.timestamp()PostgresDatepostgresValue.date()PostgresTimepostgresValue.time()PostgresTimeWithTimeZonepostgresValue.timeWithTimeZone()Datesee below Foundation
Datestored 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, andPostgresTimeWithTimeZonewhenever 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.columnMetadataNotAvailableif column metadata is not available;DecodingErrorif the operation otherwise failsDeclaration
Swift
public func decodeByColumnName<T: Decodable>(_ type: T.Type, defaultTimeZone: TimeZone? = nil) throws -> TParameters
typethe type of instance to create
defaultTimeZonethe default time zone for certain conversions to Foundation
Date(see above); ifnilthen the UTC time zone is usedReturn Value
an instance of the specified type
-
Decodes this
Rowto create an instance of the specified type.The type specified must conform to the
Decodableprotocol. This method matchescolumnsto 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, aDecodabletype decodes its properties in declaration order. This default behavior can be overridden by providing implementations of theCodingKeysenum 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 BoolpostgresValue.bool()StringpostgresValue.string()DoublepostgresValue.double()FloatFloat(postgresValue.double())IntpostgresValue.int()Int8Int8(postgresValue.string())Int16Int16(postgresValue.string())Int32Int32(postgresValue.string())Int64Int64(postgresValue.string())UIntUInt(postgresValue.string())UInt8UInt8(postgresValue.string())UInt16UInt16(postgresValue.string())UInt32UInt32(postgresValue.string())UInt64UInt64(postgresValue.string())PostgresByteApostgresValue.byteA()PostgresTimestampWithTimeZonepostgresValue.timestampWithTimeZone()PostgresTimestamppostgresValue.timestamp()PostgresDatepostgresValue.date()PostgresTimepostgresValue.time()PostgresTimeWithTimeZonepostgresValue.timeWithTimeZone()Datesee below Foundation
Datestored 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, andPostgresTimeWithTimeZonewhenever 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
DecodingErrorif the operation failsDeclaration
Swift
public func decodeByColumnIndex<T: Decodable>(_ type: T.Type, defaultTimeZone: TimeZone? = nil) throws -> TParameters
typethe type of instance to create
defaultTimeZonethe default time zone for certain conversions to Foundation
Date(see above); ifnilthen 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 }