| Safe Haskell | None |
|---|---|
| Language | Haskell2010 |
Hmssql.Catalog
Description
The database's schema, read from its catalog views: every user table
and view with its columns, their declared types (SqlType), the
primary key, the unique constraints and the foreign keys.
tables <- readTablesIn conn "dbo" forM_ tables $ \t -> forM_ (tableColumns t) $ \c -> putStrLn (T.unpack (objectName (tableName t) <> "." <> colName c <> ": " <> maybe (colTypeName c) sqlTypeName (colType c)))
Everything comes from sys.objects, sys.columns, sys.types and the
constraint views, in one request. A column's type is resolved through
an alias type (dbo.phone, based on varchar(20), is a
varchar(20); sysname is nvarchar(128)), and a CLR user-defined type
or a type newer than this library leaves colType Nothing with the
name in colTypeName.
What is not read: indexes, check constraints, triggers, the referential actions of foreign keys, identity seeds, procedures and functions.
Synopsis
- data ObjectName = ObjectName {
- objectSchema :: !Text
- objectName :: !Text
- objectNameSql :: ObjectName -> Text
- data Table = Table {
- tableName :: !ObjectName
- tableKind :: !TableKind
- tableColumns :: ![Column]
- tablePrimaryKey :: !(Maybe KeyConstraint)
- tableUniqueKeys :: ![KeyConstraint]
- tableForeignKeys :: ![ForeignKey]
- data TableKind
- data Column = Column {
- colName :: !Text
- colId :: !Int
- colTypeName :: !Text
- colType :: !(Maybe SqlType)
- colNullable :: !Bool
- colCollation :: !(Maybe Text)
- colIdentity :: !Bool
- colComputed :: !(Maybe Computed)
- colDefault :: !(Maybe Text)
- colGenerated :: !(Maybe GeneratedAlways)
- colHidden :: !Bool
- data Computed = Computed {}
- data GeneratedAlways
- data KeyConstraint = KeyConstraint {
- keyName :: !Text
- keyColumns :: ![Text]
- data ForeignKey = ForeignKey {
- fkName :: !Text
- fkColumns :: ![Text]
- fkReferencedTable :: !ObjectName
- fkReferencedColumns :: ![Text]
- readTables :: Connection -> IO [Table]
- readTablesIn :: Connection -> Text -> IO [Table]
- readTable :: Connection -> ObjectName -> IO (Maybe Table)
- lookupColumn :: Table -> Text -> Maybe Column
Tables and views
data ObjectName Source #
A schema-qualified name: dbo.customer.
Constructors
| ObjectName | |
Fields
| |
Instances
| Show ObjectName Source # | |
Defined in Hmssql.Catalog Methods showsPrec :: Int -> ObjectName -> ShowS # show :: ObjectName -> String # showList :: [ObjectName] -> ShowS # | |
| Eq ObjectName Source # | |
Defined in Hmssql.Catalog | |
| Ord ObjectName Source # | |
Defined in Hmssql.Catalog Methods compare :: ObjectName -> ObjectName -> Ordering # (<) :: ObjectName -> ObjectName -> Bool # (<=) :: ObjectName -> ObjectName -> Bool # (>) :: ObjectName -> ObjectName -> Bool # (>=) :: ObjectName -> ObjectName -> Bool # max :: ObjectName -> ObjectName -> ObjectName # min :: ObjectName -> ObjectName -> ObjectName # | |
objectNameSql :: ObjectName -> Text Source #
The name as SQL, bracket-quoted: [dbo].[customer].
A table or view with its columns and constraints.
Constructors
| Table | |
Fields
| |
Whether an object is a table or a view.
Instances
| Bounded TableKind Source # | |
| Enum TableKind Source # | |
Defined in Hmssql.Catalog Methods succ :: TableKind -> TableKind # pred :: TableKind -> TableKind # fromEnum :: TableKind -> Int # enumFrom :: TableKind -> [TableKind] # enumFromThen :: TableKind -> TableKind -> [TableKind] # enumFromTo :: TableKind -> TableKind -> [TableKind] # enumFromThenTo :: TableKind -> TableKind -> TableKind -> [TableKind] # | |
| Read TableKind Source # | |
| Show TableKind Source # | |
| Eq TableKind Source # | |
| Ord TableKind Source # | |
A column of a table or view.
Constructors
| Column | |
Fields
| |
A computed column's expression, and whether it is stored.
Constructors
| Computed | |
Fields
| |
data GeneratedAlways Source #
sys.columns.generated_always_type.
Constructors
| AsRowStart | |
| AsRowEnd | |
| AsTransactionIdStart | |
| AsTransactionIdEnd | |
| AsSequenceNumberStart | |
| AsSequenceNumberEnd | |
| AsOther !Int | A kind this library doesn't know, with the catalog's number. |
Instances
| Show GeneratedAlways Source # | |
Defined in Hmssql.Catalog Methods showsPrec :: Int -> GeneratedAlways -> ShowS # show :: GeneratedAlways -> String # showList :: [GeneratedAlways] -> ShowS # | |
| Eq GeneratedAlways Source # | |
Defined in Hmssql.Catalog Methods (==) :: GeneratedAlways -> GeneratedAlways -> Bool # (/=) :: GeneratedAlways -> GeneratedAlways -> Bool # | |
| Ord GeneratedAlways Source # | |
Defined in Hmssql.Catalog Methods compare :: GeneratedAlways -> GeneratedAlways -> Ordering # (<) :: GeneratedAlways -> GeneratedAlways -> Bool # (<=) :: GeneratedAlways -> GeneratedAlways -> Bool # (>) :: GeneratedAlways -> GeneratedAlways -> Bool # (>=) :: GeneratedAlways -> GeneratedAlways -> Bool # max :: GeneratedAlways -> GeneratedAlways -> GeneratedAlways # min :: GeneratedAlways -> GeneratedAlways -> GeneratedAlways # | |
data KeyConstraint Source #
A primary key or unique constraint.
Constructors
| KeyConstraint | |
Fields
| |
Instances
| Show KeyConstraint Source # | |
Defined in Hmssql.Catalog Methods showsPrec :: Int -> KeyConstraint -> ShowS # show :: KeyConstraint -> String # showList :: [KeyConstraint] -> ShowS # | |
| Eq KeyConstraint Source # | |
Defined in Hmssql.Catalog Methods (==) :: KeyConstraint -> KeyConstraint -> Bool # (/=) :: KeyConstraint -> KeyConstraint -> Bool # | |
data ForeignKey Source #
A foreign key constraint.
Constructors
| ForeignKey | |
Fields
| |
Instances
| Show ForeignKey Source # | |
Defined in Hmssql.Catalog Methods showsPrec :: Int -> ForeignKey -> ShowS # show :: ForeignKey -> String # showList :: [ForeignKey] -> ShowS # | |
| Eq ForeignKey Source # | |
Defined in Hmssql.Catalog | |
Reading the catalog
readTables :: Connection -> IO [Table] Source #
Every user table and view the login can see, in every schema, in name order. Objects marked as shipped by Microsoft are left out.
readTablesIn :: Connection -> Text -> IO [Table] Source #
The user tables and views of one schema.
readTable :: Connection -> ObjectName -> IO (Maybe Table) Source #
One table or view, if it exists.