hpqtypes-extras 1.16.1.0 → 1.16.2.0
raw patch · 5 files changed
+61/−7 lines, 5 filesPVP: major bump suggested
API removals or changes: PVP suggests a major version bump
API changes (from Hackage documentation)
- Database.PostgreSQL.PQTypes.Deriving: type family EnumBase a;
+ Database.PostgreSQL.PQTypes.Deriving: type EnumBase a;
+ Database.PostgreSQL.PQTypes.Model.Table: [colCollation] :: TableColumn -> Maybe (RawSQL ())
- Database.PostgreSQL.PQTypes.Model.Table: TableColumn :: RawSQL () -> ColumnType -> Bool -> Maybe (RawSQL ()) -> TableColumn
+ Database.PostgreSQL.PQTypes.Model.Table: TableColumn :: RawSQL () -> ColumnType -> Maybe (RawSQL ()) -> Bool -> Maybe (RawSQL ()) -> TableColumn
Files
- CHANGELOG.md +3/−0
- hpqtypes-extras.cabal +1/−1
- src/Database/PostgreSQL/PQTypes/Checks.hs +9/−2
- src/Database/PostgreSQL/PQTypes/Model/Table.hs +7/−4
- test/Main.hs +41/−0
CHANGELOG.md view
@@ -1,3 +1,6 @@+# hpqtypes-extras-1.16.2.0 (2022-10-27)+* Add support for setting collation method for columns.+ # hpqtypes-extras-1.16.1.0 (2022-08-02) * Add support for `sqlResult` in `sqlDelete`. * Add a migration type for modifying columns.
hpqtypes-extras.cabal view
@@ -1,6 +1,6 @@ cabal-version: 2.2 name: hpqtypes-extras-version: 1.16.1.0+version: 1.16.2.0 synopsis: Extra utilities for hpqtypes library description: The following extras for hpqtypes library: .
src/Database/PostgreSQL/PQTypes/Checks.hs view
@@ -399,6 +399,12 @@ runQuery_ $ sqlSelect "pg_catalog.pg_attribute a" $ do sqlResult "a.attname::text" sqlResult "pg_catalog.format_type(a.atttypid, a.atttypmod)"+ sqlResult . parenthesize . toSQLCommand $+ sqlSelect "pg_catalog.pg_collation c, pg_catalog.pg_type t" $ do+ sqlResult "c.collname::text"+ -- `typcollation` specifies the default collation of the type (if+ -- any), and `attcollation` is the collation of the column.+ sqlWhere "c.oid = a.attcollation AND t.oid = a.atttypid AND a.attcollation <> t.typcollation" sqlResult "NOT a.attnotnull" sqlResult . parenthesize . toSQLCommand $ sqlSelect "pg_catalog.pg_attrdef d" $ do@@ -430,10 +436,11 @@ ] where fetchTableColumn- :: (String, ColumnType, Bool, Maybe String) -> TableColumn- fetchTableColumn (name, ctype, nullable, mdefault) = TableColumn {+ :: (String, ColumnType, Maybe Text, Bool, Maybe String) -> TableColumn+ fetchTableColumn (name, ctype, collation, nullable, mdefault) = TableColumn { colName = unsafeSQL name , colType = ctype+ , colCollation = flip rawSQL () <$> collation , colNullable = nullable , colDefault = unsafeSQL `liftM` mdefault }
src/Database/PostgreSQL/PQTypes/Model/Table.hs view
@@ -28,16 +28,18 @@ import Database.PostgreSQL.PQTypes.Model.Trigger data TableColumn = TableColumn {- colName :: RawSQL ()-, colType :: ColumnType-, colNullable :: Bool-, colDefault :: Maybe (RawSQL ())+ colName :: RawSQL ()+, colType :: ColumnType+, colCollation :: Maybe (RawSQL ())+, colNullable :: Bool+, colDefault :: Maybe (RawSQL ()) } deriving Show tblColumn :: TableColumn tblColumn = TableColumn { colName = error "tblColumn: column name must be specified" , colType = error "tblColumn: column type must be specified"+, colCollation = Nothing , colNullable = True , colDefault = Nothing }@@ -47,6 +49,7 @@ "ADD COLUMN" , colName , columnTypeToSQL colType+ , maybe "" (\c -> "COLLATE \"" <> c <> "\"") colCollation , if colNullable then "NULL" else "NOT NULL" , maybe "" ("DEFAULT" <+>) colDefault ]
test/Main.hs view
@@ -71,8 +71,10 @@ , colNullable = False , colDefault = Just "gen_random_uuid()" } , tblColumn { colName = "name", colType = TextT+ , colCollation = Just "en_US" , colNullable = False } , tblColumn { colName = "location", colType = TextT+ , colCollation = Just "C" , colNullable = False } ] , tblPrimaryKey = pkOnColumn "id"@@ -638,6 +640,45 @@ (robberyWitnessIds' :: [UUID]) <- fetchMany runIdentity liftIO $ assertEqual "INSERT into 'witnessed_robbery' table" 3 (length robberyWitnessIds')++ -- Create a new record to test order-by case sensitivity.+ runQuery_ . sqlInsert "bank" $ do+ sqlSet "name" ("byblos bank" :: T.Text)+ sqlSet "location" ("SYRIA" :: T.Text)++ -- Check that ordering results by the "location" column uses case-sensitive+ -- sorting (since the collation method for that column is "C").+ runQuery_ . sqlSelect "bank" $ do+ sqlResult "location"+ sqlOrderBy "location"++ details8 <- fetchMany runIdentity+ liftIO $ assertEqual "Using collation method \"C\" leads to case-sensitive ordering of results"+ [ "18 Bargatan, Stockholm, Sweden" :: String+ , "2/3 Quux Ave., Milton Keynes, UK"+ , "23 Baz Lane, Liverpool, UK"+ , "6600 Sunset Blvd., Los Angeles, CA, USA"+ , "SYRIA"+ , "Spain"+ ]+ details8++ -- Check that ordering results by the "name" column uses case-insensitive+ -- sorting (since the collation method for that column is "en_US").+ runQuery_ . sqlSelect "bank" $ do+ sqlResult "name"+ sqlOrderBy "name"++ details9 <- fetchMany runIdentity+ liftIO $ assertEqual "Using collation method \"en_US\" leads to case-insensitive ordering of results"+ [ "byblos bank" :: String+ , "Citi"+ , "Nordea"+ , "Santander"+ , "Swedbank"+ , "Wells Fargo"+ ]+ details9 do deletedRows <- runQuery . sqlDelete "witness" $ do