packages feed

hmemdb 0.1.0.5 → 0.2.0.0

raw patch · 2 files changed

+59/−13 lines, 2 files

Files

hmemdb.cabal view
@@ -2,7 +2,7 @@ -- documentation, see http://haskell.org/cabal/users-guide/
 
 name:                hmemdb
-version:             0.1.0.5
+version:             0.2.0.0
 synopsis:            In-memory relational database
 description:         Library that provides a sort of relational database in memory (which could be saved to the disk, however). Very untested.
 license:             BSD3
@@ -16,18 +16,19 @@ 
 library
   exposed-modules:     Data.HMemDb
-  other-modules:       Data.HMemDb.Utils,
-                       Data.HMemDb.TableVars,
-                       Data.HMemDb.Tables,
-                       Data.HMemDb.Specs, 
-                       Data.HMemDb.References, 
-                       Data.HMemDb.RefConverter, 
-                       Data.HMemDb.RefContainer, 
-                       Data.HMemDb.Persistence,
-                       Data.HMemDb.KeyBackends,
-                       Data.HMemDb.ForeignKeys,
-                       Data.HMemDb.CreateTable, 
+  other-modules:       Data.HMemDb.Bin,
                        Data.HMemDb.Binary,
-                       Data.HMemDb.Bin
+                       Data.HMemDb.CreateTable, 
+                       Data.HMemDb.ForeignKeys,
+                       Data.HMemDb.KeyBackends,
+                       Data.HMemDb.Persistence,
+                       Data.HMemDb.RefContainer, 
+                       Data.HMemDb.RefConverter, 
+                       Data.HMemDb.References, 
+                       Data.HMemDb.Specs, 
+                       Data.HMemDb.Tables,
+                       Data.HMemDb.TableVarId,
+                       Data.HMemDb.TableVars,
+                       Data.HMemDb.Utils
   build-depends:       base ==4.6.*, TypeCompose ==0.9.*, transformers ==0.3.*, stm ==2.4.*, containers ==0.5.*, binary ==0.5.*
   hs-source-dirs:      src
+ src/Data/HMemDb/TableVarId.hs view
@@ -0,0 +1,45 @@+module Data.HMemDb.TableVarId (TableVarId, idToVar, tableIds, tvIdTarget, varToId) where+import Control.Compose (Id(Id), unId)+import Control.Concurrent.STM (readTVar)+import Control.Monad.Trans.Class (lift)+import Data.Function (on)+import qualified Data.Map as M (lookup)+import Data.HMemDb.Binary (MS)+import Data.HMemDb.ForeignKeys (ForeignKey(ForeignKey))+import Data.HMemDb.References (Ref(Ref, refContent, refIndex))+import Data.HMemDb.Tables (Table(Table), tabContent)+import Data.HMemDb.TableVars (TableVar, TableVarS(TableVar))+import Data.HMemDb.Utils (liftMaybe)+data TableVarId a =+    TableVarId+    {+      unTVId :: Integer,+      tvIdTarget :: Table a -- ^ 'Table' this id points to.+    }+-- ^ This type can be used for columns in a table.+-- It's sort of a 'ForeignKey' accompanied with a specific index.+instance Eq (TableVarId a) where (==) = (==) `on` unTVId+-- ^ 'TableVarId's pointing to different 'Table's may accidentally appear equal.+instance Ord (TableVarId a) where compare = compare `on` unTVId+varToId :: TableVar a -> TableVarId a+-- ^ This function gets an Id of the variable.+varToId (TableVar iref pt) =+    TableVarId {unTVId = refIndex (unId iref), tvIdTarget = Table pt}+idToVar :: TableVarId a -> MS (TableVar a)+-- ^ This function looks up the specific Id in the 'Table'.+idToVar tvId =+    case tvIdTarget tvId of+      Table pt ->+          do mp <- lift $ readTVar $ tabContent pt+             let i = unTVId tvId+             tv <- liftMaybe $ M.lookup i mp+             return $ TableVar (Id Ref {refContent = tv, refIndex = i}) pt+tableIds :: Table a -> ForeignKey Id (TableVarId a) a+-- ^ If you want to use 'TableVarId' as a column type in a table,+-- you'd need a 'ForeignKey'. This function provides one.+tableIds (Table pt) =+    ForeignKey pt $ \tvId -> do+      mp <- lift (readTVar $ tabContent pt)+      let i = unTVId tvId+      tv <- liftMaybe $ M.lookup i mp+      return $ Id Ref {refContent = tv, refIndex = i}