Safe Haskell | Safe-Inferred |
---|---|
Language | Haskell2010 |
API for building Selda backends and adding support for more types in queries.
Synopsis
- class MonadIO m => MonadSelda m where
- type Backend m
- withConnection :: (SeldaConnection (Backend m) -> m a) -> m a
- transact :: m a -> m a
- data SeldaT b m a
- type SeldaM b = SeldaT b IO
- data SeldaError
- data StmtID
- data BackendID
- = SQLite
- | PostgreSQL
- | Other Text
- type QueryRunner a = Text -> [Param] -> IO a
- data SeldaBackend b = SeldaBackend {
- runStmt :: Text -> [Param] -> IO (Int, [[SqlValue]])
- runStmtWithPK :: Text -> [Param] -> IO Int64
- prepareStmt :: StmtID -> [SqlTypeRep] -> Text -> IO Dynamic
- runPrepared :: Dynamic -> [Param] -> IO (Int, [[SqlValue]])
- getTableInfo :: TableName -> IO TableInfo
- ppConfig :: PPConfig
- closeConnection :: SeldaConnection b -> IO ()
- backendId :: BackendID
- disableForeignKeys :: Bool -> IO ()
- data SeldaConnection b
- data SqlValue where
- data IndexMethod
- data Param where
- data ColAttr
- data AutoIncType
- data PPConfig = PPConfig {
- ppType :: SqlTypeRep -> Text
- ppTypeHook :: SqlTypeRep -> [ColAttr] -> (SqlTypeRep -> Text) -> Text
- ppTypePK :: SqlTypeRep -> Text
- ppPlaceholder :: Int -> Text
- ppColAttrs :: [ColAttr] -> Text
- ppColAttrsHook :: SqlTypeRep -> [ColAttr] -> ([ColAttr] -> Text) -> Text
- ppAutoIncInsert :: Text
- ppMaxInsertParams :: Maybe Int
- ppIndexMethodHook :: IndexMethod -> Text
- defPPConfig :: PPConfig
- data TableName
- data ColName
- data TableInfo = TableInfo {
- tableInfoName :: TableName
- tableColumnInfos :: [ColumnInfo]
- tableUniqueGroups :: [[ColName]]
- tablePrimaryKey :: [ColName]
- data ColumnInfo = ColumnInfo {
- colName :: ColName
- colType :: Either Text SqlTypeRep
- colIsAutoPrimary :: Bool
- colIsNullable :: Bool
- colHasIndex :: Bool
- colFKs :: [(TableName, ColName)]
- isAutoPrimary :: ColAttr -> Bool
- isPrimary :: ColAttr -> Bool
- isUnique :: ColAttr -> Bool
- tableInfo :: Table a -> TableInfo
- fromColInfo :: ColInfo -> ColumnInfo
- mkTableName :: Text -> TableName
- mkColName :: Text -> ColName
- fromTableName :: TableName -> Text
- fromColName :: ColName -> Text
- rawTableName :: TableName -> Text
- newConnection :: MonadIO m => SeldaBackend b -> Text -> m (SeldaConnection b)
- allStmts :: SeldaConnection b -> IO [(StmtID, Dynamic)]
- withBackend :: MonadSelda m => (SeldaBackend (Backend m) -> m a) -> m a
- runSeldaT :: (MonadIO m, MonadMask m) => SeldaT b m a -> SeldaConnection b -> m a
- seldaClose :: MonadIO m => SeldaConnection b -> m ()
- data UUID
- data UUID' a
- data ID a
- data RowID
- data SqlValue where
- data Lit a where
- LText :: !Text -> Lit Text
- LInt32 :: !Int32 -> Lit Int32
- LInt64 :: !Int64 -> Lit Int64
- LDouble :: !Double -> Lit Double
- LBool :: !Bool -> Lit Bool
- LDateTime :: !UTCTime -> Lit UTCTime
- LDate :: !Day -> Lit Day
- LTime :: !TimeOfDay -> Lit TimeOfDay
- LJust :: SqlType a => !(Lit a) -> Lit (Maybe a)
- LBlob :: !ByteString -> Lit ByteString
- LNull :: SqlType a => Lit (Maybe a)
- LCustom :: SqlTypeRep -> Lit a -> Lit b
- LUUID :: !UUID -> Lit UUID
- class (Typeable a, Bounded a, Enum a) => SqlEnum a where
- class Typeable a => SqlType a where
- mkLit :: a -> Lit a
- sqlType :: Proxy a -> SqlTypeRep
- fromSql :: SqlValue -> a
- defaultValue :: Lit a
- data SqlTypeRep
- sqlDateTimeFormat :: String
- sqlDateFormat :: String
- sqlTimeFormat :: String
- litType :: Lit a -> SqlTypeRep
- compLit :: Lit a -> Lit b -> Ordering
- invalidRowId :: RowID
- isInvalidRowId :: RowID -> Bool
- toRowId :: Int64 -> RowID
- fromRowId :: RowID -> Int64
- typedUuid :: UUID -> UUID' a
- toId :: Int64 -> ID a
- fromId :: ID a -> Int64
- invalidId :: ID a
- isInvalidId :: ID a -> Bool
Documentation
class MonadIO m => MonadSelda m where Source #
Some monad with Selda SQL capabilitites.
withConnection :: (SeldaConnection (Backend m) -> m a) -> m a Source #
Pass a Selda connection to the given computation and execute it.
After the computation finishes, withConnection
is free to do anything
it likes to the connection, including closing it or giving it to another
Selda computation.
Thus, the computation must take care never to return or otherwise
access the connection after returning.
transact :: m a -> m a Source #
Perform the given computation as a transaction.
Implementations must ensure that subsequent calls to withConnection
within the same transaction always passes the same connection
to its argument.
Monad transformer adding Selda SQL capabilities.
Instances
data SeldaError Source #
Thrown by any function in SeldaT
if an error occurs.
DbError String | Unable to open or connect to database. |
SqlError String | An error occurred while executing query. |
UnsafeError String | An error occurred due to improper use of an unsafe function. |
Instances
Exception SeldaError Source # | |
Defined in Database.Selda.Backend.Internal toException :: SeldaError -> SomeException # fromException :: SomeException -> Maybe SeldaError # displayException :: SeldaError -> String # | |
Show SeldaError Source # | |
Defined in Database.Selda.Backend.Internal showsPrec :: Int -> SeldaError -> ShowS # show :: SeldaError -> String # showList :: [SeldaError] -> ShowS # | |
Eq SeldaError Source # | |
Defined in Database.Selda.Backend.Internal (==) :: SeldaError -> SeldaError -> Bool # (/=) :: SeldaError -> SeldaError -> Bool # |
A prepared statement identifier. Guaranteed to be unique per application.
Uniquely identifies some particular backend.
When publishing a new backend, consider submitting a pull request with a
constructor for your backend instead of using the Other
constructor.
type QueryRunner a = Text -> [Param] -> IO a Source #
A function which executes a query and gives back a list of extensible tuples; one tuple per result row, and one tuple element per column.
data SeldaBackend b Source #
A collection of functions making up a Selda backend.
SeldaBackend | |
|
data SeldaConnection b Source #
Some value that is representable in SQL.
SqlInt32 :: !Int32 -> SqlValue | |
SqlInt64 :: !Int64 -> SqlValue | |
SqlFloat :: !Double -> SqlValue | |
SqlString :: !Text -> SqlValue | |
SqlBool :: !Bool -> SqlValue | |
SqlBlob :: !ByteString -> SqlValue | |
SqlUTCTime :: !UTCTime -> SqlValue | |
SqlTime :: !TimeOfDay -> SqlValue | |
SqlDate :: !Day -> SqlValue | |
SqlNull :: SqlValue |
data IndexMethod Source #
Method to use for indexing with indexedUsing
.
Index methods are ignored by the SQLite backend, as SQLite doesn't support
different index methods.
Instances
Show IndexMethod Source # | |
Defined in Database.Selda.Table.Type showsPrec :: Int -> IndexMethod -> ShowS # show :: IndexMethod -> String # showList :: [IndexMethod] -> ShowS # | |
Eq IndexMethod Source # | |
Defined in Database.Selda.Table.Type (==) :: IndexMethod -> IndexMethod -> Bool # (/=) :: IndexMethod -> IndexMethod -> Bool # | |
Ord IndexMethod Source # | |
Defined in Database.Selda.Table.Type compare :: IndexMethod -> IndexMethod -> Ordering # (<) :: IndexMethod -> IndexMethod -> Bool # (<=) :: IndexMethod -> IndexMethod -> Bool # (>) :: IndexMethod -> IndexMethod -> Bool # (>=) :: IndexMethod -> IndexMethod -> Bool # max :: IndexMethod -> IndexMethod -> IndexMethod # min :: IndexMethod -> IndexMethod -> IndexMethod # |
A parameter to a prepared SQL statement.
Column attributes such as nullability, auto increment, etc. When adding elements, make sure that they are added in the order required by SQL syntax, as this list is only sorted before being pretty-printed.
data AutoIncType Source #
Strongly or weakly auto-incrementing primary key?
Instances
Show AutoIncType Source # | |
Defined in Database.Selda.Table.Type showsPrec :: Int -> AutoIncType -> ShowS # show :: AutoIncType -> String # showList :: [AutoIncType] -> ShowS # | |
Eq AutoIncType Source # | |
Defined in Database.Selda.Table.Type (==) :: AutoIncType -> AutoIncType -> Bool # (/=) :: AutoIncType -> AutoIncType -> Bool # | |
Ord AutoIncType Source # | |
Defined in Database.Selda.Table.Type compare :: AutoIncType -> AutoIncType -> Ordering # (<) :: AutoIncType -> AutoIncType -> Bool # (<=) :: AutoIncType -> AutoIncType -> Bool # (>) :: AutoIncType -> AutoIncType -> Bool # (>=) :: AutoIncType -> AutoIncType -> Bool # max :: AutoIncType -> AutoIncType -> AutoIncType # min :: AutoIncType -> AutoIncType -> AutoIncType # |
Backend-specific configuration for the SQL pretty-printer.
PPConfig | |
|
defPPConfig :: PPConfig Source #
Default settings for pretty-printing. Geared towards SQLite.
The default definition of ppTypePK
is 'defType, so that you don’t have to do anything
special if you don’t use special types for primary keys.
Name of a database table.
Name of a database column.
Comprehensive information about a table.
TableInfo | |
|
data ColumnInfo Source #
Comprehensive information about a column.
ColumnInfo | |
|
Instances
Show ColumnInfo Source # | |
Defined in Database.Selda.Backend.Internal showsPrec :: Int -> ColumnInfo -> ShowS # show :: ColumnInfo -> String # showList :: [ColumnInfo] -> ShowS # | |
Eq ColumnInfo Source # | |
Defined in Database.Selda.Backend.Internal (==) :: ColumnInfo -> ColumnInfo -> Bool # (/=) :: ColumnInfo -> ColumnInfo -> Bool # |
isAutoPrimary :: ColAttr -> Bool Source #
tableInfo :: Table a -> TableInfo Source #
Get the column information for each column in the given table.
fromColInfo :: ColInfo -> ColumnInfo Source #
Convert a ColInfo
into a ColumnInfo
.
mkTableName :: Text -> TableName Source #
Create a column name.
fromTableName :: TableName -> Text Source #
Convert a table name into a string, with quotes.
fromColName :: ColName -> Text Source #
Convert a column name into a string, with quotes.
rawTableName :: TableName -> Text Source #
Convert a table name into a string, without quotes.
newConnection :: MonadIO m => SeldaBackend b -> Text -> m (SeldaConnection b) Source #
Create a new Selda connection for the given backend and database identifier string.
allStmts :: SeldaConnection b -> IO [(StmtID, Dynamic)] Source #
Get all statements and their corresponding identifiers for the current connection.
withBackend :: MonadSelda m => (SeldaBackend (Backend m) -> m a) -> m a Source #
Get the backend in use by the computation.
runSeldaT :: (MonadIO m, MonadMask m) => SeldaT b m a -> SeldaConnection b -> m a Source #
Run a Selda transformer. Backends should use this to implement their
withX
functions.
seldaClose :: MonadIO m => SeldaConnection b -> m () Source #
Close a reusable Selda connection.
Closing a connection while in use is undefined.
Passing a closed connection to runSeldaT
results in a SeldaError
being thrown. Closing a connection more than once is a no-op.
Type representing Universally Unique Identifiers (UUID) as specified in RFC 4122.
Instances
Data UUID | |
Defined in Data.UUID.Types.Internal gfoldl :: (forall d b. Data d => c (d -> b) -> d -> c b) -> (forall g. g -> c g) -> UUID -> c UUID # gunfold :: (forall b r. Data b => c (b -> r) -> c r) -> (forall r. r -> c r) -> Constr -> c UUID # dataTypeOf :: UUID -> DataType # dataCast1 :: Typeable t => (forall d. Data d => c (t d)) -> Maybe (c UUID) # dataCast2 :: Typeable t => (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c UUID) # gmapT :: (forall b. Data b => b -> b) -> UUID -> UUID # gmapQl :: (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> UUID -> r # gmapQr :: forall r r'. (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> UUID -> r # gmapQ :: (forall d. Data d => d -> u) -> UUID -> [u] # gmapQi :: Int -> (forall d. Data d => d -> u) -> UUID -> u # gmapM :: Monad m => (forall d. Data d => d -> m d) -> UUID -> m UUID # gmapMp :: MonadPlus m => (forall d. Data d => d -> m d) -> UUID -> m UUID # gmapMo :: MonadPlus m => (forall d. Data d => d -> m d) -> UUID -> m UUID # | |
Storable UUID | This |
Defined in Data.UUID.Types.Internal | |
Read UUID | |
Show UUID | Pretty prints a
|
Binary UUID | This |
NFData UUID | |
Defined in Data.UUID.Types.Internal | |
Eq UUID | |
Ord UUID | |
Hashable UUID | |
Defined in Data.UUID.Types.Internal | |
Random UUID | This |
Uniform UUID | |
Defined in Data.UUID.Types.Internal uniformM :: StatefulGen g m => g -> m UUID # | |
IsUUID UUID Source # | |
SqlType UUID Source # |
|
Lift UUID | |
An UUID identifying a database row.
A typed row identifier.
Generic tables should use this instead of RowID
.
Use untyped
to erase the type of a row identifier, and cast
from the
Database.Selda.Unsafe module if you for some reason need to add a type
to a row identifier.
A row identifier for some table. This is the type of auto-incrementing primary keys.
Some value that is representable in SQL.
SqlInt32 :: !Int32 -> SqlValue | |
SqlInt64 :: !Int64 -> SqlValue | |
SqlFloat :: !Double -> SqlValue | |
SqlString :: !Text -> SqlValue | |
SqlBool :: !Bool -> SqlValue | |
SqlBlob :: !ByteString -> SqlValue | |
SqlUTCTime :: !UTCTime -> SqlValue | |
SqlTime :: !TimeOfDay -> SqlValue | |
SqlDate :: !Day -> SqlValue | |
SqlNull :: SqlValue |
An SQL literal.
LText :: !Text -> Lit Text | |
LInt32 :: !Int32 -> Lit Int32 | |
LInt64 :: !Int64 -> Lit Int64 | |
LDouble :: !Double -> Lit Double | |
LBool :: !Bool -> Lit Bool | |
LDateTime :: !UTCTime -> Lit UTCTime | |
LDate :: !Day -> Lit Day | |
LTime :: !TimeOfDay -> Lit TimeOfDay | |
LJust :: SqlType a => !(Lit a) -> Lit (Maybe a) | |
LBlob :: !ByteString -> Lit ByteString | |
LNull :: SqlType a => Lit (Maybe a) | |
LCustom :: SqlTypeRep -> Lit a -> Lit b | |
LUUID :: !UUID -> Lit UUID |
class (Typeable a, Bounded a, Enum a) => SqlEnum a where Source #
Any type that's bounded, enumerable and has a text representation, and thus representable as a Selda enumerable.
While it would be more efficient to store enumerables as integers, this makes hand-rolled SQL touching the values inscrutable, and will break if the user a) derives Enum and b) changes the order of their constructors. Long-term, this should be implemented in PostgreSQL as a proper enum anyway, which mostly renders the performance argument moot.
class Typeable a => SqlType a where Source #
Any datatype representable in (Selda's subset of) SQL.
Nothing
Create a literal of this type.
sqlType :: Proxy a -> SqlTypeRep Source #
The SQL representation for this type.
fromSql :: SqlValue -> a Source #
Convert an SqlValue into this type.
defaultValue :: Lit a Source #
Default value when using def
at this type.
Instances
data SqlTypeRep Source #
Representation of an SQL type.
Instances
Show SqlTypeRep Source # | |
Defined in Database.Selda.SqlType showsPrec :: Int -> SqlTypeRep -> ShowS # show :: SqlTypeRep -> String # showList :: [SqlTypeRep] -> ShowS # | |
Eq SqlTypeRep Source # | |
Defined in Database.Selda.SqlType (==) :: SqlTypeRep -> SqlTypeRep -> Bool # (/=) :: SqlTypeRep -> SqlTypeRep -> Bool # | |
Ord SqlTypeRep Source # | |
Defined in Database.Selda.SqlType compare :: SqlTypeRep -> SqlTypeRep -> Ordering # (<) :: SqlTypeRep -> SqlTypeRep -> Bool # (<=) :: SqlTypeRep -> SqlTypeRep -> Bool # (>) :: SqlTypeRep -> SqlTypeRep -> Bool # (>=) :: SqlTypeRep -> SqlTypeRep -> Bool # max :: SqlTypeRep -> SqlTypeRep -> SqlTypeRep # min :: SqlTypeRep -> SqlTypeRep -> SqlTypeRep # |
sqlDateTimeFormat :: String Source #
Format string used to represent date and time when
representing timestamps as text.
If at all possible, use SqlUTCTime
instead.
sqlDateFormat :: String Source #
Format string used to represent date when
representing dates as text.
If at all possible, use SqlDate
instead.
sqlTimeFormat :: String Source #
Format string used to represent time of day when
representing time as text.
If at all possible, use SqlTime
instead.
litType :: Lit a -> SqlTypeRep Source #
The SQL type representation for the given literal.
invalidRowId :: RowID Source #
A row identifier which is guaranteed to not match any row in any table.
isInvalidRowId :: RowID -> Bool Source #
Is the given row identifier invalid? I.e. is it guaranteed to not match any row in any table?
toRowId :: Int64 -> RowID Source #
Create a row identifier from an integer. Use with caution, preferably only when reading user input.
typedUuid :: UUID -> UUID' a Source #
Convert an untyped UUID to a typed one. Use sparingly, preferably only during deserialization.
toId :: Int64 -> ID a Source #
Create a typed row identifier from an integer. Use with caution, preferably only when reading user input.
fromId :: ID a -> Int64 Source #
Create a typed row identifier from an integer. Use with caution, preferably only when reading user input.
A typed row identifier which is guaranteed to not match any row in any table.
isInvalidId :: ID a -> Bool Source #
Is the given typed row identifier invalid? I.e. is it guaranteed to not match any row in any table?