Skip to content

Commit 6cb278d

Browse files
authored
Merge pull request #4 from vapor/array-support
array support
2 parents b6fa58b + ef5c819 commit 6cb278d

File tree

4 files changed

+54
-107
lines changed

4 files changed

+54
-107
lines changed
Lines changed: 0 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import Foundation
2-
31
/// A PostgreSQL column type and size.
42
public struct PostgreSQLColumn {
53
/// The columns data type.
@@ -23,67 +21,3 @@ public protocol PostgreSQLColumnStaticRepresentable {
2321
static var postgreSQLColumn: PostgreSQLColumn { get }
2422
}
2523

26-
/// MARK: Default Implementations
27-
28-
extension Data: PostgreSQLColumnStaticRepresentable {
29-
/// See `PostgreSQLColumnStaticRepresentable.postgreSQLColumn`
30-
public static var postgreSQLColumn: PostgreSQLColumn { return .init(type: .bytea) }
31-
}
32-
33-
extension UUID: PostgreSQLColumnStaticRepresentable {
34-
/// See `PostgreSQLColumnStaticRepresentable.postgreSQLColumn`
35-
public static var postgreSQLColumn: PostgreSQLColumn { return .init(type: .uuid) }
36-
}
37-
38-
extension Date: PostgreSQLColumnStaticRepresentable {
39-
/// See `PostgreSQLColumnStaticRepresentable.postgreSQLColumn`
40-
public static var postgreSQLColumn: PostgreSQLColumn { return .init(type: .timestamp) }
41-
}
42-
43-
extension FixedWidthInteger {
44-
/// See `PostgreSQLColumnStaticRepresentable.postgreSQLColumn`
45-
public static var postgreSQLColumn: PostgreSQLColumn {
46-
switch bitWidth {
47-
case 64: return .init(type: .int8)
48-
case 32: return .init(type: .int4)
49-
case 16: return .init(type: .int2)
50-
case 8: return .init(type: .char)
51-
default: fatalError("Unexpected \(Self.self) bit width: \(bitWidth)")
52-
}
53-
}
54-
}
55-
56-
extension Int: PostgreSQLColumnStaticRepresentable { }
57-
extension Int8: PostgreSQLColumnStaticRepresentable { }
58-
extension Int16: PostgreSQLColumnStaticRepresentable { }
59-
extension Int32: PostgreSQLColumnStaticRepresentable { }
60-
extension Int64: PostgreSQLColumnStaticRepresentable { }
61-
extension UInt: PostgreSQLColumnStaticRepresentable { }
62-
extension UInt8: PostgreSQLColumnStaticRepresentable { }
63-
extension UInt16: PostgreSQLColumnStaticRepresentable { }
64-
extension UInt32: PostgreSQLColumnStaticRepresentable { }
65-
extension UInt64: PostgreSQLColumnStaticRepresentable { }
66-
67-
extension BinaryFloatingPoint {
68-
/// See `PostgreSQLColumnStaticRepresentable.postgreSQLColumn`
69-
public static var postgreSQLColumn: PostgreSQLColumn {
70-
switch exponentBitCount + significandBitCount + 1 {
71-
case 64: return .init(type: .float8)
72-
case 32: return .init(type: .float4)
73-
default: fatalError("Unexpected \(Self.self) bit width: \(exponentBitCount + significandBitCount + 1)")
74-
}
75-
}
76-
}
77-
78-
extension Float: PostgreSQLColumnStaticRepresentable { }
79-
extension Double: PostgreSQLColumnStaticRepresentable { }
80-
81-
extension String: PostgreSQLColumnStaticRepresentable {
82-
/// See `PostgreSQLColumnStaticRepresentable.postgreSQLColumn`
83-
public static var postgreSQLColumn: PostgreSQLColumn { return .init(type: .text) }
84-
}
85-
86-
extension Bool: PostgreSQLColumnStaticRepresentable {
87-
/// See `PostgreSQLColumnStaticRepresentable.postgreSQLColumn`
88-
public static var postgreSQLColumn: PostgreSQLColumn { return .init(type: .bool) }
89-
}

Sources/FluentPostgreSQL/PostgreSQLDatabase+SchemaSupporting.swift

Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,30 +6,10 @@ extension PostgreSQLDatabase: SchemaSupporting {
66
/// See `SchemaSupporting.dataType`
77
public static func dataType(for field: SchemaField<PostgreSQLDatabase>) -> String {
88
var string: String
9-
switch field.type.type {
10-
case .bool: string = "BOOLEAN"
11-
case .bytea: string = "BYTEA"
12-
case .char: string = "CHAR"
13-
case .int8: string = "BIGINT"
14-
case .int2: string = "SMALLINT"
15-
case .int4, .oid, .regproc: string = "INTEGER"
16-
case .text, .name: string = "TEXT"
17-
case .point: string = "POINT"
18-
case .float4: string = "REAL"
19-
case .float8: string = "DOUBLE PRECISION"
20-
case ._aclitem: string = "_aclitem"
21-
case .bpchar: string = "BPCHAR"
22-
case .varchar: string = "VARCHAR"
23-
case .date: string = "DATE"
24-
case .time: string = "TIME"
25-
case .timestamp: string = "TIMESTAMP"
26-
case .numeric: string = "NUMERIC"
27-
case .void: string = "VOID"
28-
case .uuid: string = "UUID"
29-
case .jsonb: string = "JSONB"
30-
case .json: string = "JSON"
31-
case .pg_node_tree: string = "pg_node_tree"
32-
default: string = "VOID" // FIXME: better error?
9+
if let knownSQLName = field.type.type.knownSQLName {
10+
string = knownSQLName
11+
} else {
12+
string = "VOID"
3313
}
3414

3515
if field.type.size >= 0 {
Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,37 @@
1+
import Foundation
2+
13
/// A type that is compatible with PostgreSQL schema and data.
24
public protocol PostgreSQLType: PostgreSQLColumnStaticRepresentable, PostgreSQLDataCustomConvertible { }
35

6+
extension PostgreSQLColumnStaticRepresentable where Self: PostgreSQLDataCustomConvertible {
7+
/// The `PostgreSQLColumn` type that best represents this type.
8+
public static var postgreSQLColumn: PostgreSQLColumn { return .init(type: Self.postgreSQLDataType) }
9+
}
10+
411
/// A type that is supports being represented as JSONB in a PostgreSQL database.
512
public protocol PostgreSQLJSONType: PostgreSQLType, PostgreSQLJSONCustomConvertible { }
613

7-
extension PostgreSQLJSONType {
8-
/// The `PostgreSQLColumn` type that best represents this type.
9-
public static var postgreSQLColumn: PostgreSQLColumn { return .init(type: .jsonb) }
10-
}
14+
/// A type that is supports being represented as T[] in a PostgreSQL database.
15+
public protocol PostgreSQLArrayType: PostgreSQLType, PostgreSQLArrayCustomConvertible { }
16+
17+
/// MARK: Default Implementations
18+
19+
extension Data: PostgreSQLType { }
20+
extension UUID: PostgreSQLType { }
21+
extension Date: PostgreSQLType { }
22+
extension Int: PostgreSQLType { }
23+
extension Int8: PostgreSQLType { }
24+
extension Int16: PostgreSQLType { }
25+
extension Int32: PostgreSQLType { }
26+
extension Int64: PostgreSQLType { }
27+
extension UInt: PostgreSQLType { }
28+
extension UInt8: PostgreSQLType { }
29+
extension UInt16: PostgreSQLType { }
30+
extension UInt32: PostgreSQLType { }
31+
extension UInt64: PostgreSQLType { }
32+
extension Float: PostgreSQLType { }
33+
extension Double: PostgreSQLType { }
34+
extension String: PostgreSQLType { }
35+
extension Bool: PostgreSQLType { }
36+
extension Array: PostgreSQLArrayType { }
37+
extension Dictionary: PostgreSQLJSONType { }

Tests/FluentPostgreSQLTests/FluentPostgreSQLTests.swift

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -43,18 +43,22 @@ class FluentPostgreSQLTests: XCTestCase {
4343
}
4444

4545
func testNestedStruct() throws {
46-
let conn = try! database.makeConnection(using: .init(), on: eventLoop).await(on: eventLoop)
47-
// print(User.codingPath(forKey: \.favoriteColors))
48-
// print(User.properties())
49-
try! User.prepare(on: conn).await(on: eventLoop)
46+
let conn = try database.makeConnection(using: .init(), on: eventLoop).await(on: eventLoop)
47+
try User.prepare(on: conn).await(on: eventLoop)
5048
let user = User(id: nil, name: "Tanner", pet: Pet(name: "Zizek"))
51-
// user.favoriteColors = ["pink", "blue"]
52-
_ = try! user.save(on: conn).await(on: eventLoop)
53-
54-
let fetched = try! User.query(on: conn).first().await(on: eventLoop)
55-
56-
XCTAssertEqual(user.id, fetched?.id)
57-
try! User.revert(on: conn).await(on: eventLoop)
49+
user.favoriteColors = ["pink", "blue"]
50+
user.dict["hello"] = "world"
51+
_ = try user.save(on: conn).await(on: eventLoop)
52+
if let fetched = try User.query(on: conn).first().await(on: eventLoop) {
53+
XCTAssertEqual(user.id, fetched.id)
54+
XCTAssertNil(user.age)
55+
XCTAssertEqual(fetched.favoriteColors, ["pink", "blue"])
56+
XCTAssertEqual(fetched.dict["hello"], "world")
57+
} else {
58+
XCTFail()
59+
}
60+
61+
try User.revert(on: conn).await(on: eventLoop)
5862
conn.close()
5963
}
6064

@@ -78,11 +82,13 @@ final class User: PostgreSQLModel, Migration {
7882
var id: Int?
7983
var name: String
8084
var age: Int?
81-
// var favoriteColors: [String]
85+
var favoriteColors: [String]
8286
var pet: Pet
87+
var dict: [String: String]
8388

8489
init(id: Int? = nil, name: String, pet: Pet) {
85-
// self.favoriteColors = []
90+
self.favoriteColors = []
91+
self.dict = [:]
8692
self.id = id
8793
self.name = name
8894
self.pet = pet

0 commit comments

Comments
 (0)