hsql-1.8.2: Database access from Haskell.

Safe Haskell | None

Database.HSQL

Contents

- Connect/Disconnect
- Metadata
- Command Execution Functions
- Retrieving Statement values and types
- Transactions
- Utilities
- SQL Exceptions handling
- Extra types

Description

Synopsis

- data Connection
- disconnect :: Connection -> IO ()
- tables :: Connection -> IO [TableId]
- type ColDef = (ColId, SqlType, Nullability)
- describe :: Connection -> TableId -> IO [ColDef]
- type SQL = String
- execute :: Connection -> SQL -> IO ()
- data Statement
- query :: Connection -> SQL -> IO Statement
- closeStatement :: Statement -> IO ()
- fetch :: Statement -> IO Bool
- class SqlBind a where
  - fromSqlCStringLen :: ColDef -> CString -> Int -> IO a
- getFieldValue :: SqlBind a => Statement -> String -> IO a
- getFieldValueMB :: SqlBind a => Statement -> String -> IO (Maybe 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
- forEachRow :: (Statement -> s -> IO s) -> Statement -> s -> IO s
- forEachRow' :: (Statement -> IO ()) -> Statement -> IO ()
- collectRows :: (Statement -> IO a) -> Statement -> IO [a]
- data SqlError
  - = SqlError {
    - seState :: String
    - seNativeError :: Int
    - seErrorMsg :: String
    }
  - | SqlNoMoreData
  - | SqlInvalidHandle
  - | SqlStillExecuting
  - | SqlNeedMoreData
  - | 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 x => x -> Maybe SqlError
- data Point = Point {
  - pointX :: Double
  - pointY :: Double
  }
- data Line = Line {
  - lineBegin :: Point
  - lineEnd :: Point
  }
- data Path
  - = OpenPath {
    - pathPoints :: [Point]
    }
  - | ClosedPath {
    - pathPoints :: [Point]
    }
- data Box = Box {
  - boxX1 :: Double
  - boxY1 :: Double
  - boxX2 :: Double
  - boxY2 :: Double
  }
- data Circle = Circle {
  - circleCenter :: Point
  - circleRadius :: Double
  }
- data Polygon = Polygon {
  - polygonPoints :: [Point]
  }
- data INetAddr = INetAddr {
  - ip4Octet1 :: Int
  - ip4Octet2 :: Int
  - ip4Octet3 :: Int
  - ip4Octet4 :: Int
  - cidrMaskBits :: Int
  }
- data MacAddr = MacAddr {
  - macOctet1 :: Int
  - macOctet2 :: Int
  - macOctet3 :: Int
  - macOctet4 :: Int
  - macOctet5 :: Int
  - macOctet6 :: Int
  }
- data SqlType
  - = SqlInteger
  - | SqlBigInt
  - | SqlSmallInt
  - | SqlTinyInt
  - | SqlMedInt
  - | SqlDecimal {
    - typeSize :: Int
    - typeDecimals :: Int
    }
  - | SqlNumeric {
    - typeSize :: Int
    - typeDecimals :: Int
    }
  - | SqlReal
  - | SqlDouble
  - | SqlFloat
  - | SqlMoney
  - | SqlChar {
    - typeSize :: Int
    }
  - | SqlVarChar {
    - typeSize :: Int
    }
  - | SqlLongVarChar {
    - typeSize :: Int
    }
  - | SqlText
  - | SqlWChar {
    - typeSize :: Int
    }
  - | SqlWVarChar {
    - typeSize :: Int
    }
  - | SqlWLongVarChar {
    - typeSize :: Int
    }
  - | SqlDate
  - | SqlTime
  - | SqlTimeTZ
  - | SqlAbsTime
  - | SqlRelTime
  - | SqlTimeInterval
  - | SqlAbsTimeInterval
  - | SqlTimeStamp
  - | SqlDateTime
  - | SqlDateTimeTZ
  - | SqlYear
  - | SqlBit
  - | SqlENUM
  - | SqlPoint
  - | SqlLSeg
  - | SqlPath
  - | SqlBox
  - | SqlPolygon
  - | SqlLine
  - | SqlCircle
  - | SqlINetAddr
  - | SqlCIDRAddr
  - | SqlMacAddr
  - | SqlBinary {
    - typeSize :: Int
    }
  - | SqlVarBinary {
    - typeSize :: Int
    }
  - | SqlLongVarBinary {
    - typeSize :: Int
    }
  - | SqlSET
  - | SqlBLOB
  - | SqlUnknown {
    - typeCode :: Int
    }

# Connect/Disconnect

data Connection Source

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 ()Source

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.

# Metadata

tables Source

Arguments

:: Connection | Database connection
-> IO [TableId] | The names of all tables in the database.

List all tables in the database.

type ColDef = (ColId, SqlType, Nullability)Source

Description of the properties of a table column.

describe Source

Arguments

:: Connection | Database connection
-> TableId | Name of a database table
-> IO [ColDef] | The list of fields in the table

List all columns in a table along with their types and `nullable` flags

# Command Execution Functions

type SQL = String Source

An SQL Query.

Once a connection to a database has been successfully established, the functions described here are used to perform SQL queries and commands.

execute Source

Arguments

:: Connection | the database connection
-> SQL | the text of SQL command
-> IO () |

Submits a command to the database.

data Statement Source

The `Statement` type represents a result from the execution of given SQL query.

query Source

Arguments

:: Connection | the database connection
-> SQL | 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 ()Source

`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 Source

`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

class SqlBind a where Source

Equivalent to Show and Read adapted to SQL expressions.

Methods

fromSqlCStringLen Source

Arguments

:: ColDef |
-> CString | binary content of SQL expression
-> Int | size of binary content
-> IO a |

read from SQL expression in binary representation, by support of its `ColDef` and code size info. This allows for faster conversion for e.g. integral numeric types, etc.

Instances

SqlBind Bool |
SqlBind Double |
SqlBind Float |
SqlBind Int |
SqlBind Int64 |
SqlBind Integer |
SqlBind String |
SqlBind ClockTime |
SqlBind Circle |
SqlBind Polygon |
SqlBind Box |
SqlBind Path |
SqlBind Line |
SqlBind Point |
SqlBind MacAddr |
SqlBind INetAddr |
SqlBind a => SqlBind (Maybe a) |

getFieldValue Source

Arguments

:: SqlBind a |
=> Statement | result table data
-> String | field name
-> IO a | field value

Retrieves the value of field with the specified name.

getFieldValueMB :: SqlBind a => Statement -> String -> IO (Maybe a)Source

Deprecated: Use getFieldValue instead.

getFieldValue'Source

Arguments

:: 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)Source

Returns the type and the `nullable` flag for field with specified name

getFieldsTypes :: Statement -> [(String, SqlType, Bool)]Source

Returns the list of fields with their types and `nullable` flags

# Transactions

inTransaction Source

Arguments

:: Connection | Database 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. A transaction is to catch ANY exception, so `SomeException` is adequate.

# Utilities

forEachRow Source

Arguments

:: (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 ()Source

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]Source

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.

# SQL Exceptions handling

data SqlError Source

Constructors

SqlError | generic error condition, with further specification

Fields

seState :: String
seNativeError :: Int
seErrorMsg :: String

SqlNoMoreData | no more data was available
SqlInvalidHandle | requested handle is invalid
SqlStillExecuting | connection is blocked by running transaction
SqlNeedMoreData | more data is needed, e.g. additional connection specs
SqlBadTypeCast | requested field can't be converted to requested type

Fields

seFieldName :: String
seFieldType :: SqlType

SqlFetchNull | requested field returns NULL

Fields

seFieldName :: String

SqlUnknownField | requested field isn't known

Fields

seFieldName :: String

SqlUnsupportedOperation | requested operation isn't supported
SqlClosedHandle | referenced handle is already closed

Instances

Eq SqlError |
Ord SqlError |
Show SqlError |
Typeable SqlError |
Exception SqlError |

catchSql :: IO a -> (SqlError -> IO a) -> IO aSource

Deprecated: Use Control.Exception.catch instead.

Deprecated: Use `catch` instead.

handleSql :: (SqlError -> IO a) -> IO a -> IO aSource

Deprecated: Use Control.Exception.handle instead.

Deprecated: Use `handle` instead.

sqlExceptions Source

Arguments

:: Exception x |
=> x | the exception thinc to be cast
-> Maybe SqlError |

Casts, if possible, an `Exception` to an `SqlError`.

# Extra types

data Point Source

A 2D point.

Constructors

Point |

Fields

pointX :: Double
pointY :: Double

Instances

Eq Point |
Ord Point |
Read Point |
Show Point |
SqlBind Point |

data Line Source

A 2D straight line.

Constructors

Line |

Fields

lineBegin :: Point
lineEnd :: Point

Instances

Eq Line |
Read Line |
Show Line |
SqlBind Line |

data Path Source

A 2D path, either open, or closed (looping).

Constructors

OpenPath | An open path

Fields

pathPoints :: [Point]

ClosedPath | A looping path

Fields

pathPoints :: [Point]

Instances

Eq Path |
Read Path |
Show Path |
SqlBind Path |

data Box Source

A 2D rectangle.

Constructors

Box |

Fields

boxX1 :: Double
boxY1 :: Double
boxX2 :: Double
boxY2 :: Double

Instances

Eq Box |
Read Box |
Show Box |
SqlBind Box |

data Circle Source

A 2D circle

Constructors

Circle |

Fields

circleCenter :: Point
circleRadius :: Double

Instances

Eq Circle |
Read Circle |
Show Circle |
SqlBind Circle |

data Polygon Source

A 2D polygon (without holes).

Constructors

Polygon |

Fields

polygonPoints :: [Point]

Instances

Eq Polygon |
Read Polygon |
Show Polygon |
SqlBind Polygon |

data INetAddr Source

An IP4 address with netmask in CIDR notation.

Constructors

INetAddr |

Fields

ip4Octet1 :: Int
ip4Octet2 :: Int
ip4Octet3 :: Int
ip4Octet4 :: Int
cidrMaskBits :: Int

Instances

Eq INetAddr |
Ord INetAddr |
Read INetAddr |
Show INetAddr |
SqlBind INetAddr |

data MacAddr Source

A MAC network address.

Constructors

MacAddr |

Fields

macOctet1 :: Int
macOctet2 :: Int
macOctet3 :: Int
macOctet4 :: Int
macOctet5 :: Int
macOctet6 :: Int

Instances

Eq MacAddr |
Ord MacAddr |
Read MacAddr |
Show MacAddr |
SqlBind MacAddr |

data SqlType Source

Variety of common data types used in databases.

Constructors

SqlInteger |
SqlBigInt |
SqlSmallInt |
SqlTinyInt |
SqlMedInt |
SqlDecimal |

Fields

typeSize :: Int
typeDecimals :: Int

SqlNumeric |

Fields

typeSize :: Int
typeDecimals :: Int

SqlReal |
SqlDouble |
SqlFloat |
SqlMoney |
SqlChar |

Fields

typeSize :: Int

SqlVarChar |

Fields

typeSize :: Int

SqlLongVarChar |

Fields

typeSize :: Int

SqlText |
SqlWChar |

Fields

typeSize :: Int

SqlWVarChar |

Fields

typeSize :: Int

SqlWLongVarChar |

Fields

typeSize :: Int

SqlDate |
SqlTime |
SqlTimeTZ |
SqlAbsTime |
SqlRelTime |
SqlTimeInterval |
SqlAbsTimeInterval |
SqlTimeStamp |
SqlDateTime |
SqlDateTimeTZ |
SqlYear |
SqlBit |
SqlENUM |
SqlPoint |
SqlLSeg |
SqlPath |
SqlBox |
SqlPolygon |
SqlLine |
SqlCircle |
SqlINetAddr |
SqlCIDRAddr |
SqlMacAddr |
SqlBinary |

Fields

typeSize :: Int

SqlVarBinary |

Fields

typeSize :: Int

SqlLongVarBinary |

Fields

typeSize :: Int

SqlSET |
SqlBLOB |
SqlUnknown | HSQL returns `SqlUnknown` for all columns for which it cannot determine the right type. The `backendTypeCode` here is the internal type code returned from the backend library

Fields

typeCode :: Int

Instances

Eq SqlType |
Ord SqlType |
Read SqlType |
Show SqlType |