-- Hoogle documentation, generated by Haddock -- See Hoogle, http://www.haskell.org/hoogle/ -- | Query SQL databases using Nested Relational Calculus embedded in Haskell. -- -- Narc is an embedded language for querying SQL databases, which permits -- using the nested relational model, a more flexible model than -- the traditional relational model of SQL databases. In spite of this -- richer data model, queries are transformed into SQL to run against -- standard databases. -- -- To use the language, express a query using the combinators like -- foreach, table, having, singleton -- and so on. Wrap this in a call to narcToSQLString to get a -- string that can be sent to a SQL database. -- -- To send a query directly to an HDBC connection, import the module -- Database.Narc.HDBC and use its run function, passing -- it an HDBC connection and a Narc query. The result is an IO action -- that returns a 2-D list of result values in HDBC's usual format. @package narc @version 0.1.2 module Database.Narc.Type type TyVar = Int data Type TBool :: Type TNum :: Type TString :: Type TUnit :: Type TList :: Type -> Type TArr :: Type -> Type -> Type TRecord :: [(String, Type)] -> Type TVar :: TyVar -> Type type QType = ([TyVar], Type) type TySubst = [(Int, Type)] type TyEnv = [(Var, QType)] emptyTySubst :: TySubst -- | ftvs: free type variables -- -- ftvsSubst: the free type variables of a type substitution--that is, -- the type variables free in the types in the range of the substitution. -- -- occurs x ty: does variable x appear in type ty? (Note there are no -- type-variable binders). applyTySubst :: TySubst -> Type -> Type -- | normalizeType: Renumber all the type variables in a normal way to -- allow comparing types. normalizeType :: Type -> State (Int, [(Int, Int)]) Type instanceOf :: Type -> Type -> Failure () unify :: Type -> Type -> Failure (TySubst) unifyAll :: [Type] -> Failure TySubst composeTySubst :: [TySubst] -> Failure TySubst disjoinSubst :: TySubst -> TySubst -> TySubst instance Eq Type instance Show Type instance Arbitrary Type module Database.Narc.SQL data Op Eq :: Op Less :: Op Plus :: Op Minus :: Op Times :: Op Divide :: Op data UnOp Min :: UnOp Max :: UnOp Count :: UnOp Sum :: UnOp Average :: UnOp -- | Query: the type of SQL queries (select R from Ts where B) (This -- is unpleasant; it should probably be organized into various syntactic -- classes.) data Query Select :: Query -> [(Field, Field, Type)] -> [Query] -> Query rslt :: Query -> Query tabs :: Query -> [(Field, Field, Type)] cond :: Query -> [Query] QNum :: Integer -> Query QBool :: Bool -> Query QNot :: Query -> Query QOp :: Query -> Op -> Query -> Query QField :: String -> String -> Query QRecord :: [(Field, Query)] -> Query QUnion :: Query -> Query -> Query QIf :: Query -> Query -> Query -> Query QExists :: Query -> Query -- | sizeQuery approximates the size of a query by calling giving -- up | its node count past a certain limit (currently limit = 100, -- below). sizeQueryExact :: Query -> Integer -- | sizeQuery approximates the size of a query by calling giving -- up | its node count past a certain limit (currently limit = 100, -- below). sizeQuery :: Query -> Integer -- | a groundQuery is a *real* SQL query--one without variables or appl'ns. groundQuery :: Query -> Bool -- | a groundQueryExpr is an atomic-type expression. groundQueryExpr :: Query -> Bool serialize :: Query -> String instance Eq Op instance Show Op instance Eq UnOp instance Show UnOp instance Eq Query instance Show Query module Database.Narc.HDBC -- | Run a Narc query directly against an HDBC connection. run :: IConnection conn => Term a -> conn -> IO [[SqlValue]] module Database.Narc.Test makeNormalizerTests :: ErrorGensym Test unitTests :: ErrorGensym Test runUnitTests :: IO Counts -- | Assertion that well-typed terms evaluate without throwing. prop_eval_safe :: Property prop_typedTermGen_tyCheck :: Property main :: IO () -- | Query SQL databases using Nested Relational Calculus embedded in -- Haskell. -- -- The primed functions in this module are in fact the syntactic forms of -- the embedded language. Use them as, for example: -- --
-- foreach (table "employees" []) $ \emp -> -- having (primApp "<" [cnst 20000, project emp "salary"]) $ -- singleton (record [(project emp "name")]) --module Database.Narc type NarcTerm = Gensym (Term ()) -- | Translate a Narc term to an SQL query. narcToSQL :: NarcTerm -> Query -- | Translate a Narc term to an SQL query string--perhaps the central | -- function of the interface. narcToSQLString :: NarcTerm -> String -- | A dummy value, or zero-width record. unit :: NarcTerm -- | A reference to a named database table; second argument is its schema -- type. table :: Tabname -> [(Field, Type)] -> NarcTerm -- | A polymorphic way of embedding constants into a term. class Constable a cnst :: Constable a => a -> NarcTerm -- | Apply some primitive function, such as (+) or avg, -- to a list of arguments. primApp :: String -> [NarcTerm] -> NarcTerm -- | Create a functional abstraction. abs :: (String -> NarcTerm) -> NarcTerm -- | Apply a functional term to an argument. app :: NarcTerm -> NarcTerm -> NarcTerm -- | A condition between two terms, as determined by the boolean value of -- the first term. ifthenelse :: NarcTerm -> NarcTerm -> NarcTerm -> NarcTerm -- | A singleton collection of one item. singleton :: NarcTerm -> NarcTerm -- | An empty collection. nil :: NarcTerm -- | The union of two collections union :: NarcTerm -> NarcTerm -> NarcTerm -- | Construct a record (name-value pairs) out of other terms; usually -- used, with base values for the record elements, as the final result of -- a query, corresponding to the select clause of a SQL query, -- but can also be used with nested results internally in a query. record :: [(String, NarcTerm)] -> NarcTerm -- | Project a field out of a record value. project :: NarcTerm -> String -> NarcTerm -- | For each item in the collection resulting from the first argument, -- give it to the function which is the second argument and -- evaluate--this corresponds to a loop, or two one part of a cross in -- traditional SQL queries. foreach :: NarcTerm -> (NarcTerm -> NarcTerm) -> NarcTerm -- | Filter the current iteration as per the condition in the first -- argument. Corresponds to a where clause in a SQL query. having :: NarcTerm -> NarcTerm -> NarcTerm data Type TBool :: Type TNum :: Type TString :: Type TUnit :: Type TList :: Type -> Type TArr :: Type -> Type -> Type TRecord :: [(String, Type)] -> Type TVar :: TyVar -> Type instance Constable Integer instance Constable Bool