From af8971bcd826d366b0004d6789c3fb9fd2aa6cc5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Tue, 22 May 2018 22:14:18 +0200 Subject: [PATCH] Default implementation for TableRecord.databaseTableName --- GRDB/Record/TableRecord.swift | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/GRDB/Record/TableRecord.swift b/GRDB/Record/TableRecord.swift index 60c9478d4..1c36ce8fe 100644 --- a/GRDB/Record/TableRecord.swift +++ b/GRDB/Record/TableRecord.swift @@ -50,6 +50,40 @@ public protocol TableRecord { } extension TableRecord { + + /// The default name of the database table used to build requests. + /// + /// - Player -> "player" + /// - Place -> "place" + /// - PostalAddress -> "postalAddress" + /// - HTTPRequest -> "httpRequest" + /// - TOEFL -> "toefl" + internal static var defaultDatabaseTableName: String { + let typeName = "\(Self.self)".replacingOccurrences(of: "(.)\\b.*$", with: "$1", options: [.regularExpression]) + let initial = typeName.replacingOccurrences(of: "^([A-Z]+).*$", with: "$1", options: [.regularExpression]) + switch initial.count { + case typeName.count: + return initial.lowercased() + case 0: + return typeName + case 1: + return initial.lowercased() + typeName.dropFirst() + default: + return initial.dropLast().lowercased() + typeName.dropFirst(initial.count - 1) + } + } + + /// The default name of the database table used to build requests. + /// + /// - Player -> "player" + /// - Place -> "place" + /// - PostalAddress -> "postalAddress" + /// - HTTPRequest -> "httpRequest" + /// - TOEFL -> "toefl" + public static var databaseTableName: String { + return defaultDatabaseTableName + } + /// Default value: `[AllColumns()]`. public static var databaseSelection: [SQLSelectable] { return [AllColumns()]