-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
223 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
/** | ||
Copyright IBM Corporation 2016, 2017, 2018 | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
import Foundation | ||
|
||
struct ForeignKey: Hashable { | ||
var keyColumns: [Column] | ||
var refColumns: [Column] | ||
var keyNames: [String] | ||
var refNames: [String] | ||
|
||
public init?(keys: [Column], refs: [Column]){ | ||
if !ForeignKey.validKey(keys, refs) { | ||
return nil | ||
} | ||
keyColumns = keys | ||
refColumns = refs | ||
keyNames = keyColumns.map { "\($0._table._name).\($0.name)"} | ||
refNames = refColumns.map { "\($0._table._name).\($0.name)"} | ||
} | ||
|
||
static func validKey(_ keys: [Column], _ refs: [Column]) -> Bool { | ||
if keys.count == 0 || refs.count == 0 || keys.count != refs.count { | ||
return false | ||
} | ||
else if !columnsBelongToSameTable(columns: keys) { | ||
return false | ||
} | ||
else if !columnsBelongToSameTable(columns: refs) { | ||
return false | ||
} | ||
return true | ||
} | ||
|
||
static func columnsBelongToSameTable(columns: [Column]) -> Bool { | ||
let tableName = columns[0]._table._name | ||
for column in columns { | ||
if column.table._name != tableName { | ||
return false | ||
} | ||
} | ||
return true | ||
} | ||
|
||
public var hashValue: Int { | ||
var hashvalue = "foreignKey".hashValue | ||
for key in keyNames { | ||
hashvalue = hashvalue ^ key.hashValue | ||
} | ||
for ref in refNames { | ||
hashvalue = hashvalue ^ ref.hashValue | ||
} | ||
return hashvalue | ||
} | ||
|
||
static public func == (lhs: ForeignKey, rhs: ForeignKey) -> Bool { | ||
// Foreign keys cannot span databases and we do not currently support temporary tables therefore checking key name and ref name sets match is sufficient to establish equality | ||
if !(Set(lhs.keyNames) == Set(rhs.keyNames)) { | ||
return false | ||
} | ||
if !(Set(lhs.refNames) == Set(rhs.refNames)) { | ||
return false | ||
} | ||
return true | ||
} | ||
|
||
func describe(queryBuilder: QueryBuilder) -> String { | ||
var append = ", FOREIGN KEY (" | ||
append += keyColumns.map { Utils.packName($0.name, queryBuilder: queryBuilder) }.joined(separator: ", ") | ||
append += ") REFERENCES " | ||
append += Utils.packName(refColumns[0].table._name, queryBuilder: queryBuilder) | ||
append += "(" | ||
append += refColumns.map { Utils.packName($0.name, queryBuilder: queryBuilder) }.joined(separator: ", ") | ||
append += ")" | ||
return append | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.