packages feed

simplest-sqlite 0.0.0.4 → 0.0.0.5

raw patch · 2 files changed

+42/−42 lines, 2 files

Files

simplest-sqlite.cabal view
@@ -2,7 +2,7 @@ cabal-version: >= 1.8  name: simplest-sqlite-version: 0.0.0.4+version: 0.0.0.5 stability: Experimental author: YoshikuniJujo <PAF01143@nifty.ne.jp> maintainer: YoshikuniJujo <PAF01143@nifty.ne.jp>@@ -23,7 +23,7 @@ source-repository this     type: git     location: git://github.com/YoshikuniJujo/test_haskell-    tag: simplest-sqlite-0.0.0.4+    tag: simplest-sqlite-0.0.0.5  library     hs-source-dirs: src
src/Database/SmplstSQLite3.hs view
@@ -2,9 +2,9 @@  module Database.SmplstSQLite3 ( 	-- * Functions-	withSQLite3, withPrepared, step, SQLite3Data, column, SQLite3DataList,+	withSQLite, withPrepared, step, SQLiteData, column, SQLiteDataList, 	-- * Types-	SQLite3, SQLite3Stmt, ResultCode(..),+	SQLite, Stmt, ResultCode(..), 	) where  import Control.Applicative@@ -19,65 +19,65 @@  import Database.SmplstSQLite3.Constants -data SQLite3 = SQLite3 (Ptr SQLite3) deriving Show-data SQLite3Stmt = SQLite3Stmt (Ptr SQLite3Stmt) deriving Show+data SQLite = SQLite (Ptr SQLite) deriving Show+data Stmt = Stmt (Ptr Stmt) deriving Show  foreign import ccall unsafe "sqlite3.h sqlite3_open" c_sqlite3_open ::-	CString -> Ptr (Ptr SQLite3) -> IO CInt+	CString -> Ptr (Ptr SQLite) -> IO CInt foreign import ccall unsafe "sqlite3.h sqlite3_close" c_sqlite3_close ::-	Ptr SQLite3 -> IO CInt+	Ptr SQLite -> IO CInt foreign import ccall unsafe "sqlite3.h sqlite3_errmsg" c_sqlite3_errmsg ::-	Ptr SQLite3 -> IO CString+	Ptr SQLite -> IO CString -withSQLite3 :: String -> (SQLite3 -> IO a) -> IO a-withSQLite3 fp = bracket (sqlite3Open fp) sqlite3Close+withSQLite :: String -> (SQLite -> IO a) -> IO a+withSQLite fp = bracket (sqlite3Open fp) sqlite3Close -sqlite3Open :: String -> IO SQLite3+sqlite3Open :: String -> IO SQLite sqlite3Open fp = withCString fp $ \cfp -> alloca $ \ppDb -> do 	ret <- c_sqlite3_open cfp ppDb 	db <- peek ppDb-	let sq = SQLite3 db+	let sq = SQLite db 	when (ret /= sQLITE_OK) $ do 		em <- sqlite3Errmsg sq 		sqlite3Close sq 		ioError . userError $ "Cannot open database: " ++ em 	return sq -sqlite3Close :: SQLite3 -> IO ()-sqlite3Close sq@(SQLite3 db) = do+sqlite3Close :: SQLite -> IO ()+sqlite3Close sq@(SQLite db) = do 	ret <- c_sqlite3_close db 	when (ret /= sQLITE_OK) $ do 		em <- sqlite3Errmsg sq 		ioError . userError $ "Cannot close database: " ++ em -sqlite3Errmsg :: SQLite3 -> IO String-sqlite3Errmsg (SQLite3 db) = peekCString =<< c_sqlite3_errmsg db+sqlite3Errmsg :: SQLite -> IO String+sqlite3Errmsg (SQLite db) = peekCString =<< c_sqlite3_errmsg db  foreign import ccall unsafe "sqlite3.h sqlite3_prepare_v2" 	c_sqlite3_prepare_v2 ::-		Ptr SQLite3 -> CString -> CInt -> Ptr (Ptr SQLite3Stmt) ->+		Ptr SQLite -> CString -> CInt -> Ptr (Ptr Stmt) -> 		(Ptr CString) -> IO CInt foreign import ccall unsafe "sqlite3.h sqlite3_finalize" c_sqlite3_finalize ::-	Ptr SQLite3Stmt -> IO CInt+	Ptr Stmt -> IO CInt -withPrepared :: SQLite3 -> String -> (SQLite3Stmt -> IO a) -> IO (a, String)+withPrepared :: SQLite -> String -> (Stmt -> IO a) -> IO (a, String) withPrepared db sql act = 	bracket (sqlite3PrepareV2 db sql) (sqlite3Finalize . fst) $ 		\(sm, t) -> (, t) <$> act sm -sqlite3PrepareV2 :: SQLite3 -> String -> IO (SQLite3Stmt, String)-sqlite3PrepareV2 db@(SQLite3 pdb) sql =+sqlite3PrepareV2 :: SQLite -> String -> IO (Stmt, String)+sqlite3PrepareV2 db@(SQLite pdb) sql = 	withCString sql $ \csql -> alloca $ \psm -> alloca $ \pt -> do 		ret <- c_sqlite3_prepare_v2 pdb csql (- 1) psm pt-		sm <- SQLite3Stmt <$> peek psm+		sm <- Stmt <$> peek psm 		when (ret /= sQLITE_OK) $ do 			em <- sqlite3Errmsg db 			sqlite3Finalize sm 			ioError . userError $ "Cannot prepare: " ++ em 		(sm ,) <$> (peekCString =<< peek pt) -sqlite3Finalize :: SQLite3Stmt -> IO ()-sqlite3Finalize (SQLite3Stmt psm) = do+sqlite3Finalize :: Stmt -> IO ()+sqlite3Finalize (Stmt psm) = do 	ret <- c_sqlite3_finalize psm 	when (ret /= sQLITE_OK) $ 		ioError . userError $@@ -86,10 +86,10 @@ data ResultCode = SQLiteBusy | SQLiteRow | SQLiteDone deriving (Show, Eq)  foreign import ccall unsafe "sqlite3.h sqlite3_step" c_sqlite3_step ::-	Ptr SQLite3Stmt -> IO CInt+	Ptr Stmt -> IO CInt -step :: SQLite3Stmt -> IO ResultCode-step (SQLite3Stmt psm) = do+step :: Stmt -> IO ResultCode+step (Stmt psm) = do 	ret <- c_sqlite3_step psm 	case ret of 		_	| ret == sQLITE_BUSY -> return SQLiteBusy@@ -99,30 +99,30 @@ 			"Error while step: error code (" ++ show ret ++ ")"  foreign import ccall unsafe "sqlite3.h sqlite3_column_int"-	c_sqlite3_column_int :: Ptr SQLite3Stmt -> CInt -> IO CInt+	c_sqlite3_column_int :: Ptr Stmt -> CInt -> IO CInt foreign import ccall unsafe "sqlite3.h sqlite3_column_text"-	c_sqlite3_column_text :: Ptr SQLite3Stmt -> CInt -> IO CString+	c_sqlite3_column_text :: Ptr Stmt -> CInt -> IO CString -class SQLite3DataList a where-	columnList :: SQLite3Stmt -> Int -> IO [a]+class SQLiteDataList a where+	columnList :: Stmt -> Int -> IO [a] -class SQLite3Data a where-	column :: SQLite3Stmt -> Int -> IO a+class SQLiteData a where+	column :: Stmt -> Int -> IO a -instance SQLite3DataList a => SQLite3Data [a] where+instance SQLiteDataList a => SQLiteData [a] where 	column = columnList -instance SQLite3Data Int where-	column (SQLite3Stmt sm) i =+instance SQLiteData Int where+	column (Stmt sm) i = 		fromIntegral <$> c_sqlite3_column_int sm (fromIntegral i) -instance SQLite3DataList Char where-	columnList (SQLite3Stmt sm) i =+instance SQLiteDataList Char where+	columnList (Stmt sm) i = 		peekCString =<< c_sqlite3_column_text sm (fromIntegral i) -instance SQLite3Data BS.ByteString where-	column (SQLite3Stmt sm) i =+instance SQLiteData BS.ByteString where+	column (Stmt sm) i = 		BS.packCString =<< c_sqlite3_column_text sm (fromIntegral i) -instance SQLite3Data T.Text where+instance SQLiteData T.Text where 	column sm i = T.decodeUtf8 <$> column sm i