-
-
Notifications
You must be signed in to change notification settings - Fork 381
Expand file tree
/
Copy pathMySQLGeneratedColumnClassification.swift
More file actions
49 lines (45 loc) · 2.07 KB
/
Copy pathMySQLGeneratedColumnClassification.swift
File metadata and controls
49 lines (45 loc) · 2.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
//
// MySQLGeneratedColumnClassification.swift
// MySQLDriverPlugin
//
import Foundation
import TableProPluginKit
/// MySQL and MariaDB report generated columns through the `Extra` column of
/// `SHOW FULL COLUMNS` and `INFORMATION_SCHEMA.COLUMNS.EXTRA`.
///
/// MySQL, and MariaDB from 10.2, report "STORED GENERATED" or "VIRTUAL
/// GENERATED" and may append further attributes: MySQL separates them with a
/// space ("STORED GENERATED INVISIBLE"), MariaDB with a comma ("STORED
/// GENERATED, INVISIBLE"), so the marker is matched as a substring. MariaDB
/// 10.1 and older instead report the whole value as bare "VIRTUAL" or
/// "PERSISTENT", and never combine it with another attribute.
///
/// "DEFAULT_GENERATED" is a MySQL 8 expression default, not a generated column,
/// and stays insertable.
internal func mysqlColumnIsGenerated(extra: String?) -> Bool {
guard let extra else { return false }
let upper = extra.uppercased()
if upper.contains("STORED GENERATED") || upper.contains("VIRTUAL GENERATED") {
return true
}
let trimmed = upper.trimmingCharacters(in: .whitespaces)
return trimmed == "VIRTUAL" || trimmed == "PERSISTENT"
}
/// AUTO_INCREMENT, from the same `Extra` value, because MySQL leaves `COLUMN_DEFAULT` null for such
/// a column exactly as PostgreSQL does for an identity column.
///
/// It is `byDefault` rather than `always`: MySQL accepts an explicit value and only allocates the
/// next one when the column is omitted or given NULL.
internal func mysqlIdentityKind(extra: String?) -> IdentityKind? {
guard let extra, extra.uppercased().contains("AUTO_INCREMENT") else { return nil }
return .byDefault
}
/// The kind, from the same `Extra` value. MariaDB 10.1 and older spell stored as "PERSISTENT".
internal func mysqlGenerationKind(extra: String?) -> GenerationKind? {
guard let extra, mysqlColumnIsGenerated(extra: extra) else { return nil }
let upper = extra.uppercased()
if upper.contains("STORED GENERATED") || upper.trimmingCharacters(in: .whitespaces) == "PERSISTENT" {
return .stored
}
return .virtual
}