hmssql
Safe HaskellNone
LanguageHaskell2010

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

Tables and views

data ObjectName Source #

A schema-qualified name: dbo.customer.

Constructors

ObjectName 

Fields

objectNameSql :: ObjectName -> Text Source #

The name as SQL, bracket-quoted: [dbo].[customer].

data Table Source #

A table or view with its columns and constraints.

Constructors

Table 

Fields

Instances

Instances details
Show Table Source # 
Instance details

Defined in Hmssql.Catalog

Methods

showsPrec :: Int -> Table -> ShowS #

show :: Table -> String #

showList :: [Table] -> ShowS #

Eq Table Source # 
Instance details

Defined in Hmssql.Catalog

Methods

(==) :: Table -> Table -> Bool #

(/=) :: Table -> Table -> Bool #

data TableKind Source #

Whether an object is a table or a view.

Constructors

UserTable 
View 

data Column Source #

A column of a table or view.

Constructors

Column 

Fields

Instances

Instances details
Show Column Source # 
Instance details

Defined in Hmssql.Catalog

Eq Column Source # 
Instance details

Defined in Hmssql.Catalog

Methods

(==) :: Column -> Column -> Bool #

(/=) :: Column -> Column -> Bool #

data Computed Source #

A computed column's expression, and whether it is stored.

Constructors

Computed 

Instances

Instances details
Show Computed Source # 
Instance details

Defined in Hmssql.Catalog

Eq Computed Source # 
Instance details

Defined in Hmssql.Catalog

data KeyConstraint Source #

A primary key or unique constraint.

Constructors

KeyConstraint 

Fields

Instances

Instances details
Show KeyConstraint Source # 
Instance details

Defined in Hmssql.Catalog

Eq KeyConstraint Source # 
Instance details

Defined in Hmssql.Catalog

data ForeignKey Source #

A foreign key constraint.

Constructors

ForeignKey 

Fields

Instances

Instances details
Show ForeignKey Source # 
Instance details

Defined in Hmssql.Catalog

Eq ForeignKey Source # 
Instance details

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.

lookupColumn :: Table -> Text -> Maybe Column Source #

The column of this name (exact match, as the catalog names it).