sqlvalue-list (empty) → 0.2
raw patch · 5 files changed
+165/−0 lines, 5 filesdep +HDBCdep +basedep +convertiblesetup-changed
Dependencies added: HDBC, base, convertible, template-haskell
Files
- Database/HDBC/SqlValue/List.hs +49/−0
- Database/HDBC/SqlValue/List/Derive.hs +61/−0
- LICENSE +31/−0
- Setup.hs +2/−0
- sqlvalue-list.cabal +22/−0
+ Database/HDBC/SqlValue/List.hs view
@@ -0,0 +1,49 @@+{-# LANGUAGE FlexibleContexts, UndecidableInstances #-}++module Database.HDBC.SqlValue.List where++import Database.HDBC.SqlValue+import Data.Convertible++class SqlValueList a where+ toSqlList :: a -> [SqlValue]+ fromSqlList :: [SqlValue] -> a++instance (Convertible SqlValue a, Convertible a SqlValue) => SqlValueList [a]+ where toSqlList = map toSql+ fromSqlList = map fromSql ++instance ( Convertible SqlValue t1, Convertible t1 SqlValue+ , Convertible SqlValue t2, Convertible t2 SqlValue)+ => SqlValueList (t1, t2)+ where toSqlList (x1, x2) = [toSql x1, toSql x2]+ fromSqlList [x1, x2] = (fromSql x1, fromSql x2) ++instance ( Convertible SqlValue t1, Convertible t1 SqlValue+ , Convertible SqlValue t2, Convertible t2 SqlValue+ , Convertible SqlValue t3, Convertible t3 SqlValue)+ => SqlValueList (t1, t2, t3)+ where toSqlList (x1, x2, x3) = [toSql x1, toSql x2, toSql x3]+ fromSqlList [x1, x2, x3] = (fromSql x1, fromSql x2, fromSql x3) ++instance ( Convertible SqlValue t1, Convertible t1 SqlValue+ , Convertible SqlValue t2, Convertible t2 SqlValue+ , Convertible SqlValue t3, Convertible t3 SqlValue+ , Convertible SqlValue t4, Convertible t4 SqlValue)+ => SqlValueList (t1, t2, t3, t4)+ where toSqlList (x1, x2, x3, x4) = [toSql x1, toSql x2, toSql x3, toSql x4]+ fromSqlList [x1, x2, x3, x4] = (fromSql x1, fromSql x2+ ,fromSql x3, fromSql x4) ++instance ( Convertible SqlValue t1, Convertible t1 SqlValue+ , Convertible SqlValue t2, Convertible t2 SqlValue+ , Convertible SqlValue t3, Convertible t3 SqlValue+ , Convertible SqlValue t4, Convertible t4 SqlValue+ , Convertible SqlValue t5, Convertible t5 SqlValue)+ => SqlValueList (t1, t2, t3, t4, t5)+ where toSqlList (x1, x2, x3, x4, x5) = [toSql x1, toSql x2, toSql x3+ , toSql x4, toSql x5]+ fromSqlList [x1, x2, x3, x4, x5] = (fromSql x1, fromSql x2, fromSql x3+ ,fromSql x4, fromSql x5) ++
+ Database/HDBC/SqlValue/List/Derive.hs view
@@ -0,0 +1,61 @@+{-|++ /Example Use:/++@+data Record = R {who :: String, age :: Int} deriving (Show, Eq)+deriveSqlValueList ''Record+@+-}+module Database.HDBC.SqlValue.List.Derive (deriveSqlValueList)+where+import Language.Haskell.TH.Syntax++deriveSqlValueList :: Name -> Q [Dec]+deriveSqlValueList conTy = do+ (conD, argn) <- isPlain conTy+ deriveSqlValueList' conTy conD argn ++isPlain :: Name -> Q (Name, Int)+isPlain conTy = do+ info <- reify conTy+ case info of+ TyConI (DataD _ _ _ [con] _) -> return (convCon con)+ TyConI (NewtypeD _ _ _ con _) -> return (convCon con)+ _ -> error "deriveSqlValueList: Invalid type provided. The type must be a data with a single constructor or a newtype."+ where convCon (NormalC name sts ) = (name, length sts)+ convCon (RecC name vsts) = (name, length vsts)+++deriveSqlValueList' conTy conD argn = do+ decs <- sqlValueListDec (conTy, conD, argn)+ return [InstanceD [] ((ConT (mkName "SqlValueList")) `AppT` ConT conTy) decs]+++sqlValueListDec :: (Name, Name, Int)-> Q [Dec]+sqlValueListDec (conTy, conD, argn) = return $+ [ _fun (mkName "toSqlList")+ (x_conPat conD argn)+ (x_mapListExp (mkName "toSql") argn)+ , _fun (mkName "fromSqlList")+ (x_listPat argn)+ (x_mapConExp conD (mkName "fromSql") argn)+ ]++_fun :: Name -> Pat -> Exp -> Dec+_fun nm pat e = FunD nm [ Clause [pat] (NormalB e) []]++x_conPat :: Name -> Int -> Pat+x_conPat con n = ConP con (map VarP (x_args n))++x_listPat :: Int -> Pat+x_listPat n = ListP $ map VarP (x_args n)++x_mapListExp :: Name -> Int -> Exp+x_mapListExp f n = ListE $ map (\t -> VarE f `AppE` VarE t) (x_args n)++x_mapConExp ::Name -> Name -> Int -> Exp+x_mapConExp conD f n = foldl AppE (ConE conD) (map (\x -> VarE f `AppE` VarE x) (x_args n))++x_args :: Int -> [Name]+x_args n = [mkName ("x" ++ show i) | i <- [1..n]]
+ LICENSE view
@@ -0,0 +1,31 @@+Copyright (c) 2010, Yuriy Iskra+All rights reserved.++Redistribution and use in source and binary forms, with or without+modification, are permitted provided that the following conditions are+met:++ * Redistributions of source code must retain the above copyright+ notice, this list of conditions and the following disclaimer.++ * Redistributions in binary form must reproduce the above+ copyright notice, this list of conditions and the following+ disclaimer in the documentation and/or other materials provided+ with the distribution.++ * The names of its contributors may not be used to endorse or + promote products derived from this software without specific prior + written permission.++THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR+A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT+OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,+SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT+LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,+DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY+THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE+OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.+
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ sqlvalue-list.cabal view
@@ -0,0 +1,22 @@+Name: sqlvalue-list+Version: 0.2+Synopsis: Class and instances for conversion to list of SqlValue.+Category: Database+Description: Class and instances for conversion to list of SqlValue. +License: BSD3+License-file: LICENSE+Author: YuriyIskra+Maintainer: YuriyIskra <iskra.yw@gmail.com>+Stability: Experimental+Copyright: Copyright (c) 2011 Yuriy Iskra+Build-type: Simple+Cabal-version: >= 1.6++library+ Exposed-modules: Database.HDBC.SqlValue.List,+ Database.HDBC.SqlValue.List.Derive++ Build-Depends: base >= 4 && < 5,+ HDBC -any,+ convertible -any,+ template-haskell >= 2.4