-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Expand use of
QualifiedName
to types, composites, enums (#263)
Types in PostgreSQL can also be qualified with a schema. However, it's not sufficient to just change the type of `TypeInformation`'s `typeName` to `QualifiedName`, because a type isn't *just* a name. Postgres types can also be parameterised by modifiers (e.g., `numeric(7, 2)`) and array types of arbitrary depth (e.g., `int4[][]`). To accomodate this, a new type is introduced, `TypeName`. Like `QualifiedName`, it has an `IsString` instance, so the common case (`schema` set to `Nothing`, no modifiers, scalar type) will continue working as before.
- Loading branch information
1 parent
7ec674d
commit c06bd5f
Showing
13 changed files
with
143 additions
and
16 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,40 @@ | ||
<!-- | ||
A new scriv changelog fragment. | ||
Uncomment the section that is right (remove the HTML comment wrapper). | ||
--> | ||
|
||
<!-- | ||
### Removed | ||
- A bullet item for the Removed category. | ||
--> | ||
### Added | ||
|
||
- `TypeName` record, which gives a richer representation of the components of a PostgreSQL type name (name, schema, modifiers, scalar/array). | ||
|
||
### Changed | ||
|
||
- `TypeInformation`'s `typeName` parameter from `String` to `TypeName`. | ||
- `DBEnum`'s `enumTypeName` method from `String` to `QualifiedName`. | ||
- `DBComposite`'s `compositeTypeName` method from `String` to `QualifiedName`. | ||
|
||
<!-- | ||
### Deprecated | ||
- A bullet item for the Deprecated category. | ||
--> | ||
<!-- | ||
### Fixed | ||
- A bullet item for the Fixed category. | ||
--> | ||
<!-- | ||
### Security | ||
- A bullet item for the Security category. | ||
--> |
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
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
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
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 |
---|---|---|
|
@@ -41,7 +41,6 @@ import Text.PrettyPrint | |
, (<+>) | ||
, ($$) | ||
, comma | ||
, doubleQuotes | ||
, hcat | ||
, parens | ||
, punctuate | ||
|
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
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
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
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
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
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
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
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,58 @@ | ||
{-# language RecordWildCards #-} | ||
{-# language StrictData #-} | ||
|
||
module Rel8.Type.Name | ||
( TypeName (..) | ||
, showTypeName | ||
) | ||
where | ||
|
||
-- base | ||
import Data.Semigroup (mtimesDefault) | ||
import Data.String (IsString, fromString) | ||
import Prelude | ||
|
||
-- pretty | ||
import Text.PrettyPrint (Doc, comma, hcat, parens, punctuate, text) | ||
|
||
-- rel8 | ||
import Rel8.Schema.QualifiedName (QualifiedName, ppQualifiedName) | ||
|
||
|
||
-- | A PostgreSQL type consists of a 'QualifiedName' (name, schema), and | ||
-- optional 'modifiers' and 'arrayDepth'. 'modifiers' will usually be @[]@, | ||
-- but a type like @numeric(6, 2)@ will have @["6", "2"]@. 'arrayDepth' is | ||
-- always @0@ for non-array types. | ||
data TypeName = TypeName | ||
{ name :: QualifiedName | ||
-- ^ The name (and schema) of the type. | ||
, modifiers :: [String] | ||
-- ^ Any modifiers applied to the underlying type. | ||
, arrayDepth :: Word | ||
-- ^ If this is an array type, the depth of that array (@1@ for @[]@, @2@ | ||
-- for @[][]@, etc). | ||
} | ||
|
||
|
||
-- | Constructs 'TypeName's with 'schema' set to 'Nothing', 'modifiers' set | ||
-- to @[]@ and 'arrayDepth' set to @0@. | ||
instance IsString TypeName where | ||
fromString string = | ||
TypeName | ||
{ name = fromString string | ||
, modifiers = [] | ||
, arrayDepth = 0 | ||
} | ||
|
||
|
||
ppTypeName :: TypeName -> Doc | ||
ppTypeName TypeName {..} = | ||
ppQualifiedName name <> modifier <> mtimesDefault arrayDepth (text "[]") | ||
where | ||
modifier | ||
| null modifiers = mempty | ||
| otherwise = parens (hcat $ punctuate comma $ text <$> modifiers) | ||
|
||
|
||
showTypeName :: TypeName -> String | ||
showTypeName = show . ppTypeName |