on dev help sign in
ALTER statements
commit8a455b309b0d88b2cd708382e37929bf03824840 authorEitan Chatav <eitan.chatav@gmail.com> dateWed 26 Jul 2017, 08:16 (9 years ago) parenta1b731e
2 files changed+127−11
squeel-postgresql/src/Squeel/PostgreSQL/Schema.hs+15 squeel-postgresql/src/Squeel/PostgreSQL/Statement.hs+112−11
squeel-postgresql/src/Squeel/PostgreSQL/Schema.hs+15
@@ -28,6 +28,9 @@ module Squeel.PostgreSQL.Schema
2828 , NullifyColumn
2929 , NullifyColumns
3030 , NullifyTable
31+ , Drop
32+ , Alter
33+ , Rename
3134 , ColumnType (..)
3235 , module GHC.OverloadedLabels
3336 , module GHC.TypeLits
@@ -140,3 +143,15 @@ type family NullifyColumns columns where
140143
141144type family NullifyTable table where
142145 NullifyTable (table ::: columns) = table ::: NullifyColumns columns
146+
147+type family Drop alias xs where
148+ Drop alias ((alias ::: x) ': xs) = xs
149+ Drop alias (x ': xs) = x ': Drop alias xs
150+
151+type family Alter alias xs x where
152+ Alter alias ((alias ::: x0) ': xs) x1 = (alias ::: x1) ': xs
153+ Alter alias (x0 ': xs) x1 = x0 ': Alter alias xs x1
154+
155+type family Rename alias0 alias1 xs where
156+ Rename alias0 alias1 ((alias0 ::: x0) ': xs) = (alias1 ::: x0) ': xs
157+ Rename alias0 alias1 (x ': xs) = x ': Rename alias0 alias1 xs
squeel-postgresql/src/Squeel/PostgreSQL/Statement.hs+112−11
@@ -736,17 +736,118 @@ createTable (Alias table) columns = UnsafeStatement $ mconcat
736736DROP statements
737737-----------------------------------------}
738738
739−class KnownSymbol table => DropTable table schema0 schema1
740− | table schema0 -> schema1 where
741− dropTable :: Alias table -> Statement '[] '[] schema0 schema1
742− dropTable (Alias table) = UnsafeStatement $
743− "DROP TABLE " <> fromString (symbolVal' table) <> ";"
744−instance {-# OVERLAPPING #-}
745− (KnownSymbol table, table ~ table', schema ~ schema')
746− => DropTable table ((table' ::: columns) ': schema) schema'
747−instance {-# OVERLAPPABLE #-}
748− DropTable table schema0 schema1
749− => DropTable table (table' ': schema0) (table' ': schema1)
739+dropTable
740+ :: KnownSymbol table
741+ => Alias table
742+ -> Statement '[] '[] schema (Drop table schema)
743+dropTable (Alias table) = UnsafeStatement $
744+ "DROP TABLE " <> fromString (symbolVal' table) <> ";"
745+
746+{-----------------------------------------
747+ALTER statements
748+-----------------------------------------}
749+
750+alterTable
751+ :: HasTable table schema columns0
752+ => Alias table
753+ -> AlterTable columns0 columns1
754+ -> Statement '[] '[] schema (Alter table schema columns1)
755+alterTable (Alias table) alteration = UnsafeStatement $
756+ "ALTER TABLE "
757+ <> fromString (symbolVal' table)
758+ <> " "
759+ <> renderAlterTable alteration
760+ <> ";"
761+
762+alterTableRename
763+ :: (KnownSymbol table0, KnownSymbol table1)
764+ => Alias table0
765+ -> Alias table1
766+ -> Statement '[] '[] schema (Rename table0 table1 schema)
767+alterTableRename (Alias table0) (Alias table1) = UnsafeStatement $
768+ "ALTER TABLE "
769+ <> fromString (symbolVal' table0)
770+ <> " RENAME TO "
771+ <> fromString (symbolVal' table1)
772+ <> ";"
773+
774+data AlterTable
775+ (columns0 :: [(Symbol, ColumnType)])
776+ (columns1 :: [(Symbol, ColumnType)]) =
777+ UnsafeAlterTable {renderAlterTable :: ByteString}
778+
779+addColumnDefault
780+ :: KnownSymbol column
781+ => Alias column
782+ -> TypeExpression ('Optional ty)
783+ -> AlterTable columns ((column ::: 'Optional ty) ': columns)
784+addColumnDefault (Alias column) ty = UnsafeAlterTable $
785+ "ADD COLUMN "
786+ <> fromString (symbolVal' column)
787+ <> " "
788+ <> renderTypeExpression ty
789+
790+addColumnNull
791+ :: KnownSymbol column
792+ => Alias column
793+ -> TypeExpression ('Required ('Null ty))
794+ -> AlterTable columns ((column ::: 'Required ('Null ty)) ': columns)
795+addColumnNull (Alias column) ty = UnsafeAlterTable $
796+ "ADD COLUMN "
797+ <> fromString (symbolVal' column)
798+ <> " "
799+ <> renderTypeExpression ty
800+
801+dropColumn
802+ :: KnownSymbol column
803+ => Alias column
804+ -> AlterTable columns (Drop column columns)
805+dropColumn (Alias column) = UnsafeAlterTable $
806+ "DROP COLUMN " <> fromString (symbolVal' column)
807+
808+renameColumn
809+ :: (KnownSymbol column0, KnownSymbol column1)
810+ => Alias column0
811+ -> Alias column1
812+ -> AlterTable columns (Rename column0 column1 columns)
813+renameColumn (Alias column0) (Alias column1) = UnsafeAlterTable $
814+ "RENAME COLUMN " <> fromString (symbolVal' column0)
815+ <> " TO " <> fromString (symbolVal' column1)
816+
817+alterColumn
818+ :: (KnownSymbol column, HasColumn column columns ty0)
819+ => Alias column
820+ -> AlterColumn ty0 ty1
821+ -> AlterTable columns (Alter column columns ty1)
822+alterColumn (Alias column) alteration = UnsafeAlterTable $
823+ "ALTER COLUMN " <> fromString (symbolVal' column)
824+ <> " " <> renderAlterColumn alteration
825+
826+data AlterColumn (ty0 :: ColumnType) (ty1 :: ColumnType) =
827+ UnsafeAlterColumn {renderAlterColumn :: ByteString}
828+
829+setDefault
830+ :: Expression '[] '[] ('Required ty)
831+ -> AlterColumn ('Required ty) ('Optional ty)
832+setDefault expression = UnsafeAlterColumn $
833+ "SET DEFAULT " <> renderExpression expression
834+
835+dropDefault :: AlterColumn ('Optional ty) ('Required ty)
836+dropDefault = UnsafeAlterColumn $ "DROP DEFAULT"
837+
838+setNotNull
839+ :: AlterColumn (optionality ('Null ty)) (optionality ('NotNull ty))
840+setNotNull = UnsafeAlterColumn $ "SET NOT NULL"
841+
842+dropNotNull
843+ :: AlterColumn (optionality ('NotNull ty)) (optionality ('Null ty))
844+dropNotNull = UnsafeAlterColumn $ "DROP NOT NULL"
845+
846+alterType
847+ :: PGCast ty0 ty1
848+ => TypeExpression (optionality (nullity ty1))
849+ -> AlterColumn (optionality (nullity ty0)) (optionality (nullity ty1))
850+alterType ty = UnsafeAlterColumn $ "TYPE " <> renderTypeExpression ty
750851
751852{-----------------------------------------
752853INSERT statements