Skip to content

Commit

Permalink
Merge pull request #1 from vapor/gm
Browse files Browse the repository at this point in the history
sql 1.0.0 gm
  • Loading branch information
tanner0101 authored Apr 23, 2018
2 parents da47130 + a230d5e commit 1bb9427
Show file tree
Hide file tree
Showing 47 changed files with 1,607 additions and 53 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@
/.build
/Packages
/*.xcodeproj
Package.resolved

21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2018 Qutheory, LLC

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
22 changes: 4 additions & 18 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -1,28 +1,14 @@
// swift-tools-version:4.0
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

let package = Package(
name: "SQL",
products: [
// Products define the executables and libraries produced by a package, and make them visible to other packages.
.library(
name: "SQL",
targets: ["SQL"]),
],
dependencies: [
// Dependencies declare other packages that this package depends on.
// .package(url: /* package url */, from: "1.0.0"),
.library(name: "SQL", targets: ["SQL"]),
],
dependencies: [],
targets: [
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
// Targets can depend on other targets in this package, and on products in packages which this package depends on.
.target(
name: "SQL",
dependencies: []),
.testTarget(
name: "SQLTests",
dependencies: ["SQL"]),
.target(name: "SQL", dependencies: []),
.testTarget(name: "SQLTests", dependencies: ["SQL"]),
]
)
23 changes: 20 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
# SQL

A description of this package.
<p align="center">
<img src="https://user-images.githubusercontent.com/1342803/39145428-9f6e8328-4701-11e8-9955-663bbbe3e9b4.png" height="64" alt="SQL">
<br>
<br>
<a href="http://docs.vapor.codes/3.0/sql/getting-started/">
<img src="http://img.shields.io/badge/read_the-docs-2196f3.svg" alt="Documentation">
</a>
<a href="http://vapor.team">
<img src="http://vapor.team/badge.svg" alt="Slack Team">
</a>
<a href="LICENSE">
<img src="http://img.shields.io/badge/license-MIT-brightgreen.svg" alt="MIT License">
</a>
<a href="https://circleci.com/gh/vapor/sql">
<img src="https://circleci.com/gh/vapor/sql.svg?style=shield" alt="Continuous Integration">
</a>
<a href="https://swift.org">
<img src="http://img.shields.io/badge/swift-4.1-brightgreen.svg" alt="Swift 4.1">
</a>
</p>
19 changes: 19 additions & 0 deletions Sources/SQL/Data/DataColumn.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/// A SQL data column with optional table name.
public struct DataColumn: ExpressibleByStringLiteral {
/// The table name for this column. If `nil`, it will be omitted.
public var table: String?

/// This column's name.
public var name: String

/// Creates a new SQL `DataColumn`.
public init(table: String? = nil, name: String) {
self.table = table
self.name = name
}

/// See `ExpressibleByStringLiteral`.
public init(stringLiteral value: String) {
self.init(name: value)
}
}
14 changes: 14 additions & 0 deletions Sources/SQL/Data/DataComputedColumn.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/// A computed SQL column.
public struct DataComputedColumn {
/// The SQL function to call.
public var function: String

/// The SQL data column parameters to the function. Can be none.
public var columns: [DataColumn]

/// Creates a new SQL `DataComputedColumn`.
public init(function: String, columns: [DataColumn] = []) {
self.function = function
self.columns = columns
}
}
8 changes: 8 additions & 0 deletions Sources/SQL/Data/DataGroupBy.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/// Available methods for `GROUP BY`, either column or custom.
public enum DataGroupBy {
/// Group by a particular column.
case column(DataColumn)

/// Group by a computed column.
case computed(DataComputedColumn)
}
18 changes: 18 additions & 0 deletions Sources/SQL/Data/DataJoin.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/// Represents a SQL join.
public struct DataJoin {
/// `INNER`, `OUTER`, etc.
public let method: DataJoinMethod

/// The left-hand side of the join. References the local column.
public let local: DataColumn

/// The right-hand side of the join. References the column being joined.
public let foreign: DataColumn

/// Creates a new SQL `DataJoin`.
public init(method: DataJoinMethod, local: DataColumn, foreign: DataColumn) {
self.method = method
self.local = local
self.foreign = foreign
}
}
7 changes: 7 additions & 0 deletions Sources/SQL/Data/DataJoinMethod.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/// Supported SQL `DataJoin` methods.
public enum DataJoinMethod {
/// INNER
case inner
/// OUTER
case outer
}
14 changes: 14 additions & 0 deletions Sources/SQL/Data/DataOrderBy.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/// A SQL `ORDER BY` that determines the order of results.
public struct DataOrderBy {
/// The columns to order.
public var columns: [DataColumn]

/// The direction to order the results.
public var direction: DataOrderByDirection

/// Creates a new SQL `DataOrderBy`
public init(columns: [DataColumn], direction: DataOrderByDirection) {
self.columns = columns
self.direction = direction
}
}
8 changes: 8 additions & 0 deletions Sources/SQL/Data/DataOrderByDirection.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/// Available order by directions for a `DataOrderBy`.
public enum DataOrderByDirection {
/// DESC
case ascending

/// ASC
case descending
}
18 changes: 18 additions & 0 deletions Sources/SQL/Data/DataPredicate.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/// A SQL search predicate (a.k.a, filter). Listed after `WHERE` in a SQL statement.
public struct DataPredicate {
/// The left-hand side of the predicate. References a column being fetched.
public var column: DataColumn

/// The method of comparison to use. Usually `=`, `<`, etc.
public var comparison: DataPredicateComparison

/// The value to compare to. Can be another column, static value, or more SQL.
public var value: DataPredicateValue

/// Creates a SQL `DataPredicate`.
public init(column: DataColumn, comparison: DataPredicateComparison, value: DataPredicateValue = .placeholder) {
self.column = column
self.comparison = comparison
self.value = value
}
}
33 changes: 33 additions & 0 deletions Sources/SQL/Data/DataPredicateComparison.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/// All suported SQL `DataPredicate` comparisons.
public enum DataPredicateComparison: Equatable {
/// =
case equal
/// !=, <>
case notEqual
/// <
case lessThan
/// >
case greaterThan
/// <=
case lessThanOrEqual
/// >=
case greaterThanOrEqual
/// IN
case `in`
/// NOT IN
case notIn
/// BETWEEN
case between
/// LIKE
case like
/// NOT LIKE
case notLike
/// IS NULL
case isNull
/// IS NOT NULL
case isNotNull
/// No comparison type
case none
/// Raw SQL string
case sql(String)
}
14 changes: 14 additions & 0 deletions Sources/SQL/Data/DataPredicateGroup.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/// A group of SQL `DataPredicate`s joined by a relation (`AND`, `OR`, etc).
public struct DataPredicateGroup {
/// The relation these predicates are joined by, usually `AND`.
public var relation: DataPredicateGroupRelation

/// One or more sub-predicates.
public var predicates: [DataPredicateItem]

/// Creates a new `DataPredicateGroup`
public init(relation: DataPredicateGroupRelation, predicates: [DataPredicateItem]) {
self.relation = relation
self.predicates = predicates
}
}
9 changes: 9 additions & 0 deletions Sources/SQL/Data/DataPredicateGroupRelation.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/// Supported data predicate relations.
public enum DataPredicateGroupRelation {
/// AND
case and
/// OR
case or
/// custom
case custom(String)
}
8 changes: 8 additions & 0 deletions Sources/SQL/Data/DataPredicateItem.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/// Either a single SQL `DataPredicate` or a group (AND/OR) of them.
public enum DataPredicateItem {
/// A collection of `DataPredicate` items joined by AND or OR.
case group(DataPredicateGroup)

/// A single `DataPredicate`.
case predicate(DataPredicate)
}
24 changes: 24 additions & 0 deletions Sources/SQL/Data/DataPredicateValue.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/// All supported values for a SQL `DataPredicate`.
public enum DataPredicateValue {
/// No value.
case none

/// One or more placeholders.
case placeholders(count: Int)

/// A single placeholder.
public static let placeholder: DataPredicateValue = .placeholders(count: 1)

/// Compare to another column in the database.
case column(DataColumn)

/// Compare to a computed column.
case computed(DataComputedColumn)

/// Serializes a complete sub-query as this predicate's value.
case subquery(DataQuery)

/// Custom string that will be interpolated into the SQL query.
/// - warning: Be careful about SQL injection when using this.
case custom(sql: String)
}
18 changes: 18 additions & 0 deletions Sources/SQL/DataDefinition/DataDefinitionColumn.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/// A single column in a DDL statement.
public struct DataDefinitionColumn {
/// The column's name.
public var name: String

/// The column's data type.
public var dataType: String

/// A collection of attributes to apply to this column.
public var attributes: [String]

/// Creates a new `DataDefinitionColumn`.
public init(name: String, dataType: String, attributes: [String] = []) {
self.name = name
self.dataType = dataType
self.attributes = attributes
}
}
32 changes: 32 additions & 0 deletions Sources/SQL/DataDefinition/DataDefinitionForeignKey.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/// A single foreign key, referencing two columns.
public struct DataDefinitionForeignKey {
/// This foreign key's unique name.
public var name: String

/// The local column being referenced.
public var local: DataColumn

/// The foreign column being referenced.
public var foreign: DataColumn

/// An optional `DataDefinitionForeignKeyAction` to apply on updates.
public var onUpdate: DataDefinitionForeignKeyAction?

/// An optional `DataDefinitionForeignKeyAction` to apply on delete.
public var onDelete: DataDefinitionForeignKeyAction?

/// Creates a new `DataDefinitionForeignKey`.
public init(
name: String,
local: DataColumn,
foreign: DataColumn,
onUpdate: DataDefinitionForeignKeyAction? = nil,
onDelete: DataDefinitionForeignKeyAction? = nil
) {
self.name = name
self.local = local
self.foreign = foreign
self.onUpdate = onUpdate
self.onDelete = onDelete
}
}
18 changes: 18 additions & 0 deletions Sources/SQL/DataDefinition/DataDefinitionForeignKeyAction.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/// Foreign key actions to apply when data related via a foreign key is updated or deleted.
public enum DataDefinitionForeignKeyAction {
/// Do nothing.
case noAction

/// Restrict the operation, this is the default.
case restrict

/// Set the relation to null.
case setNull

/// Set the relation to default values.
case setDefault

/// Cascade the operation. For example, if an entity is deleted
/// then also delete the related entity.
case cascade
}
Loading

0 comments on commit 1bb9427

Please sign in to comment.