Database.HSQL

|  | Contents | Index
Database.HSQL |
Portability | portable
Stability | provisional
Maintainer | ka2_mail@yahoo.com
Contents
Connect/Disconnect Command Execution Functions Retrieving Statement values and types Transactions SQL Exceptions handling Utilities Metadata Extra types
Description
The module provides an abstract database interface
Synopsis
data Connection
disconnect :: Connection -> IO ()
execute :: Connection -> String -> IO ()
data Statement
query :: Connection -> String -> IO Statement
closeStatement :: Statement -> IO ()
fetch :: Statement -> IO Bool
type FieldDef = (String, SqlType, Bool)
data SqlType
= SqlChar Int
| SqlVarChar Int
| SqlLongVarChar Int
| SqlText
| SqlWChar Int
| SqlWVarChar Int
| SqlWLongVarChar Int
| SqlDecimal Int Int
| SqlNumeric Int Int
| SqlSmallInt
| SqlMedInt
| SqlInteger
| SqlReal
| SqlFloat
| SqlDouble
| SqlBit
| SqlTinyInt
| SqlBigInt
| SqlBinary Int
| SqlVarBinary Int
| SqlLongVarBinary Int
| SqlDate
| SqlTime
| SqlTimeTZ
| SqlAbsTime
| SqlRelTime
| SqlTimeInterval
| SqlAbsTimeInterval
| SqlTimeStamp
| SqlDateTime
| SqlDateTimeTZ
| SqlYear
| SqlSET
| SqlENUM
| SqlBLOB
| SqlMoney
| SqlINetAddr
| SqlCIDRAddr
| SqlMacAddr
| SqlPoint
| SqlLSeg
| SqlPath
| SqlBox
| SqlPolygon
| SqlLine
| SqlCircle
| SqlUnknown Int
class SqlBind a
toSqlValue :: SqlBind a => a -> String
getFieldValueMB :: SqlBind a => Statement -> String -> IO (Maybe a)
getFieldValue :: SqlBind a => Statement -> String -> IO a
getFieldValue' :: SqlBind a => Statement -> String -> a -> IO a
getFieldValueType :: Statement -> String -> (SqlType, Bool)
getFieldsTypes :: Statement -> [(String, SqlType, Bool)]
inTransaction :: Connection -> (Connection -> IO a) -> IO a
data SqlError
= SqlError {
seState :: String
seNativeError :: Int
seErrorMsg :: String
}
| SqlNoData
| SqlInvalidHandle
| SqlStillExecuting
| SqlNeedData
| SqlBadTypeCast {
seFieldName :: String
seFieldType :: SqlType
}
| SqlFetchNull {
seFieldName :: String
}
| SqlUnknownField {
seFieldName :: String
}
| SqlUnsupportedOperation
| SqlClosedHandle
catchSql :: IO a -> (SqlError -> IO a) -> IO a
handleSql :: (SqlError -> IO a) -> IO a -> IO a
sqlExceptions :: Exception -> Maybe SqlError
forEachRow :: (Statement -> s -> IO s) -> Statement -> s -> IO s
forEachRow' :: (Statement -> IO ()) -> Statement -> IO ()
collectRows :: (Statement -> IO a) -> Statement -> IO [a]
tables :: Connection -> IO [String]
describe :: Connection -> String -> IO [FieldDef]
data Point = Point Double Double
data Line = Line Point Point
data Path
= OpenPath [Point]
| ClosedPath [Point]
data Box = Box Double Double Double Double
data Circle = Circle Point Double
data Polygon = Polygon [Point]
Connect/Disconnect
data Connection
A `Connection` type represents a connection to a database, through which you can operate on the it. In order to create the connection you need to use the `connect` function from the module for your prefered backend.
disconnect :: Connection -> IO ()
Closes the connection. Performing `disconnect` on a connection that has already been closed has no effect. All other operations on a closed connection will fail.
Command Execution Functions
Once a connection to a database has been successfully established, the functions described here are used to perform SQL queries and commands.
execute
:: Connection | the database connection
-> String | the text of SQL command
-> IO () |
Submits a command to the database.
data Statement
The `Statement` type represents a result from the execution of given SQL query.
query
:: Connection | the database connection
-> String | the text of SQL query
-> IO Statement | the associated statement. Must be closed with the `closeStatement` function
Executes a query and returns a result set
closeStatement :: Statement -> IO ()
`closeStatement` stops processing associated with a specific statement, closes any open cursors associated with the statement, discards pending results, and frees all resources associated with the statement. Performing `closeStatement` on a statement that has already been closed has no effect. All other operations on a closed statement will fail.
fetch :: Statement -> IO Bool
`fetch` fetches the next rowset of data from the result set. The values from columns can be retrieved with `getFieldValue` function.
Retrieving Statement values and types
type FieldDef = (String, SqlType, Bool)
data SqlType
Constructors
SqlChar Int |
SqlVarChar Int |
SqlLongVarChar Int |
SqlText |
SqlWChar Int |
SqlWVarChar Int |
SqlWLongVarChar Int |
SqlDecimal Int Int |
SqlNumeric Int Int |
SqlSmallInt |
SqlMedInt |
SqlInteger |
SqlReal |
SqlFloat |
SqlDouble |
SqlBit |
SqlTinyInt |
SqlBigInt |
SqlBinary Int |
SqlVarBinary Int |
SqlLongVarBinary Int |
SqlDate |
SqlTime |
SqlTimeTZ |
SqlAbsTime |
SqlRelTime |
SqlTimeInterval |
SqlAbsTimeInterval |
SqlTimeStamp |
SqlDateTime |
SqlDateTimeTZ |
SqlYear |
SqlSET |
SqlENUM |
SqlBLOB |
SqlMoney |
SqlINetAddr |
SqlCIDRAddr |
SqlMacAddr |
SqlPoint |
SqlLSeg |
SqlPath |
SqlBox |
SqlPolygon |
SqlLine |
SqlCircle |
SqlUnknown Int | HSQL returns `SqlUnknown tp` for all columns for which it cannot determine the right type. The `tp` here is the internal type code returned from the backend library
Instances
Eq SqlType
Show SqlType
class SqlBind a
Instances
SqlBind Int
SqlBind Int64
SqlBind Integer
SqlBind String
SqlBind Bool
SqlBind Double
SqlBind ClockTime
SqlBind Point
SqlBind Line
SqlBind Path
SqlBind Box
SqlBind Polygon
SqlBind Circle
SqlBind INetAddr
SqlBind MacAddr
toSqlValue :: SqlBind a => a -> String
getFieldValueMB
:: SqlBind a |
=> Statement |
-> String | Field name
-> IO (Maybe a) | Field value or Nothing
Retrieves the value of field with the specified name. The returned value is Nothing if the field value is `null`.
getFieldValue
:: SqlBind a |
=> Statement |
-> String | Field name
-> IO a | Field value
Retrieves the value of field with the specified name. If the field value is `null` then the function will throw `SqlFetchNull` exception.
getFieldValue'
:: SqlBind a |
=> Statement |
-> String | Field name
-> a | Default field value
-> IO a | Field value
Retrieves the value of field with the specified name. If the field value is `null` then the function will return the default value.
getFieldValueType :: Statement -> String -> (SqlType, Bool)
Returns the type and the `nullable` flag for field with specified name
getFieldsTypes :: Statement -> [(String, SqlType, Bool)]
Returns the list of fields with their types and `nullable` flags
Transactions
inTransaction
:: Connection |
-> (Connection -> IO a) | an action
-> IO a | the returned value is the result returned from action
The `inTransaction` function executes the specified action in transaction mode. If the action completes successfully then the transaction will be commited. If the action completes with an exception then the transaction will be rolled back and the exception will be throw again.
SQL Exceptions handling
data SqlError
Constructors
SqlError |
seState :: String |
seNativeError :: Int |
seErrorMsg :: String |
SqlNoData |
SqlInvalidHandle |
SqlStillExecuting |
SqlNeedData |
SqlBadTypeCast |
seFieldName :: String |
seFieldType :: SqlType |
SqlFetchNull |
seFieldName :: String |
SqlUnknownField |
seFieldName :: String |
SqlUnsupportedOperation |
SqlClosedHandle |
Instances
Typeable SqlError
Show SqlError
catchSql :: IO a -> (SqlError -> IO a) -> IO a
handleSql :: (SqlError -> IO a) -> IO a -> IO a
sqlExceptions :: Exception -> Maybe SqlError
Utilities
forEachRow
:: (Statement -> s -> IO s) | an action
-> Statement | the statement
-> s | initial state
-> IO s | final state
The `forEachRow` function iterates through the result set in `Statement` and executes the given action for each row in the set. The function closes the `Statement` after the last row processing or if the given action raises an exception.
forEachRow' :: (Statement -> IO ()) -> Statement -> IO ()
The `forEachRow`' function is analogous to `forEachRow` but doesn't provide state. The function closes the `Statement` after the last row processing or if the given action raises an exception.
collectRows :: (Statement -> IO a) -> Statement -> IO [a]
The `collectRows` function iterates through the result set in `Statement` and executes the given action for each row in the set. The values returned from action are collected and returned as list. The function closes the `Statement` after the last row processing or if the given action raises an exception.
Metadata
tables
:: Connection | Database connection
-> IO [String] | The names of all tables in the database.
List all tables in the database.
describe
:: Connection | Database connection
-> String | Name of a database table
-> IO [FieldDef] | The list of fields in the table
List all columns in a table along with their types and `nullable` flags
Extra types
data Point
Constructors
Point Double Double |
Instances
SqlBind Point
Eq Point
Show Point
data Line
Constructors
Line Point Point |
Instances
SqlBind Line
Eq Line
Show Line
data Path
Constructors
OpenPath [Point] |
ClosedPath [Point] |
Instances
SqlBind Path
Eq Path
Show Path
data Box
Constructors
Box Double Double Double Double |
Instances
SqlBind Box
Eq Box
Show Box
data Circle
Constructors
Circle Point Double |
Instances
SqlBind Circle
Eq Circle
Show Circle
data Polygon
Constructors
Polygon [Point] |
Instances
SqlBind Polygon
Eq Polygon
Show Polygon
Produced by Haddock version 0.6