HDBC-2.4.0.4: Haskell Database Connectivity

Safe Haskell | Safe-Inferred
Language | Haskell2010

Database.HDBC.Statement

Contents

- Re-exported from Database.HDBC.SqlValue; this re-exporting is deprecated

Synopsis

- data Statement = Statement {
  - execute :: [SqlValue] -> IO Integer
  - executeRaw :: IO ()
  - executeMany :: [[SqlValue]] -> IO ()
  - finish :: IO ()
  - fetchRow :: IO (Maybe [SqlValue])
  - getColumnNames :: IO [String]
  - originalQuery :: String
  - describeResult :: IO [(String, SqlColDesc)]
  }
- data SqlError = SqlError {
  - seState :: String
  - seNativeError :: Int
  - seErrorMsg :: String
  }
- nToSql :: Integral a => a -> SqlValue
- iToSql :: Int -> SqlValue
- posixToSql :: POSIXTime -> SqlValue
- fromSql :: Convertible SqlValue a => SqlValue -> a
- safeFromSql :: Convertible SqlValue a => SqlValue -> ConvertResult a
- toSql :: Convertible a SqlValue => a -> SqlValue
- data SqlValue
  - = SqlString String
  - | SqlByteString ByteString
  - | SqlWord32 Word32
  - | SqlWord64 Word64
  - | SqlInt32 Int32
  - | SqlInt64 Int64
  - | SqlInteger Integer
  - | SqlChar Char
  - | SqlBool Bool
  - | SqlDouble Double
  - | SqlRational Rational
  - | SqlLocalDate Day
  - | SqlLocalTimeOfDay TimeOfDay
  - | SqlZonedLocalTimeOfDay TimeOfDay TimeZone
  - | SqlLocalTime LocalTime
  - | SqlZonedTime ZonedTime
  - | SqlUTCTime UTCTime
  - | SqlDiffTime NominalDiffTime
  - | SqlPOSIXTime POSIXTime
  - | SqlEpochTime Integer
  - | SqlTimeDiff Integer
  - | SqlNull

# Documentation

data Statement Source #

Constructors

Statement |

Fields

- execute :: [SqlValue] -> IO Integer

  Execute the prepared statement, passing in the given positional parameters (that should take the place of the question marks in the call to `prepare`).

  For non-SELECT queries, the return value is the number of rows modified, if known. If no rows were modified, you get 0. If the value is unknown, you get -1. All current HDBC drivers support this function and should never return -1.

  For SELECT queries, you will always get 0.

  This function should automatically call finish() to finish the previous execution, if necessary.

- executeRaw :: IO ()

  Execute the statement as-is, without supplying any positional parameters. This is intended for statements for which the results aren't interesting or present (e.g., DDL or DML commands). If your query contains placeholders, this will certainly fail; use `execute` instead.

- executeMany :: [[SqlValue]] -> IO ()

  Execute the query with many rows. The return value is the return value from the final row as if you had called `execute` on it.

  Due to optimizations that are possible due to different databases and driver designs, this can often be significantly faster than using `execute` multiple times since queries need to be compiled only once.

  This is most useful for non-SELECT statements.

- finish :: IO ()

  Abort a query in progress -- usually not needed.

- fetchRow :: IO (Maybe [SqlValue])

  Fetches one row from the DB. Returns `Nothing` if there are no more rows. Will automatically call `finish` when the last row is read.

- getColumnNames :: IO [String]

  Returns a list of the column names in the result. For maximum portability, you should not assume that information is available until after an `execute` function has been run.

  Information is returned here directly as returned by the underlying database layer. Note that different databases have different rules about capitalization of return values and about representation of names of columns that are not simple columns. For this reason, it is suggested that you treat this information for display purposes only. Failing that, you should convert to lower (or upper) case, and use `AS` clauses for anything other than simple columns.

  A simple getColumnNames implementation could simply apply `map fst` to the return value of `describeResult`.

- originalQuery :: String

  The original query that this `Statement` was prepared with.

- describeResult :: IO [(String, SqlColDesc)]

  Obtain information about the columns in the result set. Must be run only after `execute`. The String in the result set is the column name.

  You should expect this to be returned in the same manner as a result from `fetchAllRows'`.

  All results should be converted to lowercase for you before you see them.

  Please see caveats under `getColumnNames` for information on the column name field here.

data SqlError Source #

The main HDBC exception object. As much information as possible is passed from the database through to the application through this object.

Errors generated in the Haskell layer will have seNativeError set to -1.

Constructors

SqlError |

Fields

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

#### Instances

Instances details

Exception SqlError Source # |
Instance details Defined in Database.HDBC.Statement Methods toException :: SqlError -> SomeException Source # fromException :: SomeException -> Maybe SqlError Source # displayException :: SqlError -> String Source # backtraceDesired :: SqlError -> Bool Source #
Read SqlError Source # |
Instance details Defined in Database.HDBC.Statement Methods readsPrec :: Int -> ReadS SqlError Source # readList :: ReadS [SqlError] Source # readPrec :: ReadPrec SqlError Source # readListPrec :: ReadPrec [SqlError] Source #
Show SqlError Source # |
Instance details Defined in Database.HDBC.Statement Methods showsPrec :: Int -> SqlError -> ShowS Source # show :: SqlError -> String Source # showList :: [SqlError] -> ShowS Source #
Eq SqlError Source # |
Instance details Defined in Database.HDBC.Statement Methods (==) :: SqlError -> SqlError -> Bool Source # (/=) :: SqlError -> SqlError -> Bool Source #

# Re-exported from Database.HDBC.SqlValue; this re-exporting is deprecated

nToSql :: Integral a => a -> SqlValue Source #

Converts any Integral type to a `SqlValue` by using toInteger.

iToSql :: Int -> SqlValue Source #

Convenience function for using numeric literals in your program.

posixToSql :: POSIXTime -> SqlValue Source #

Convenience function for converting `POSIXTime` to a `SqlValue`, because `toSql` cannot do the correct thing in this instance.

fromSql :: Convertible SqlValue a => SqlValue -> a Source #

Convert from an `SqlValue` to a Haskell value. Any problem is indicated by calling `error`. This function is simply a restricted-type wrapper around `convert`. See extended notes on `SqlValue`.

safeFromSql :: Convertible SqlValue a => SqlValue -> ConvertResult a Source #

Conversions to and from `SqlValue`s and standard Haskell types.

This function converts from an `SqlValue` to a Haskell value. Many people will use the simpler `fromSql` instead. This function is simply a restricted-type wrapper around `safeConvert`.

toSql :: Convertible a SqlValue => a -> SqlValue Source #

Convert a value to an `SqlValue`. This function is simply a restricted-type wrapper around `convert`. See extended notes on `SqlValue`.

data SqlValue Source #

`SqlValue` is the main type for expressing Haskell values to SQL databases.

INTRODUCTION TO SQLVALUE

This type is used to marshall Haskell data to and from database APIs. HDBC driver interfaces will do their best to use the most accurate and efficient way to send a particular value to the database server.

Values read back from the server are constructed with the most appropriate `SqlValue` constructor. `fromSql` or `safeFromSql` can then be used to convert them into whatever type is needed locally in Haskell.

Most people will use `toSql` and `fromSql` instead of manipulating `SqlValue`s directly.

EASY CONVERSIONS BETWEEN HASKELL TYPES

Conversions are powerful; for instance, you can call `fromSql` on a SqlInt32 and get a String or a Double out of it. This class attempts to Do The Right Thing whenever possible, and will raise an error when asked to do something incorrect. In particular, when converting to any type except a Maybe, `SqlNull` as the input will cause an error to be raised.

Conversions are implemented in terms of the Data.Convertible module, part of the convertible package. You can refer to its documentation, and import that module, if you wish to parse the Left result from `safeFromSql` yourself, or write your own conversion instances.

Here are some notes about conversion:

- Fractions of a second are not preserved on time values
- There is no `safeToSql` because `toSql` never fails.

See also `toSql`, `safeFromSql`, `fromSql`, `nToSql`, `iToSql`, `posixToSql`.

ERROR CONDITIONS

There may sometimes be an error during conversion. For instance, if you have a `SqlString` and are attempting to convert it to an Integer, but it doesn't parse as an Integer, you will get an error. This will be indicated as an exception if using `fromSql`, or a Left result if using `safeFromSql`.

SPECIAL NOTE ON POSIXTIME

Note that a `NominalDiffTime` or `POSIXTime` is converted to `SqlDiffTime` by `toSql`. HDBC cannot differentiate between `NominalDiffTime` and `POSIXTime` since they are the same underlying type. You must construct `SqlPOSIXTime` manually or via `posixToSql`, or use `SqlUTCTime`.

DETAILS ON SQL TYPES

HDBC database backends are expected to marshal date and time data back and forth using the appropriate representation for the underlying database engine. Databases such as PostgreSQL with builtin date and time types should see automatic conversion between these Haskell types to database types. Other databases will be presented with an integer or a string. Care should be taken to use the same type on the Haskell side as you use on the database side. For instance, if your database type lacks timezone information, you ought not to use ZonedTime, but instead LocalTime or UTCTime. Database type systems are not always as rich as Haskell. For instance, for data stored in a TIMESTAMP WITHOUT TIME ZONE column, HDBC may not be able to tell if it is intended as UTCTime or LocalTime data, and will happily convert it to both, upon your request. It is your responsibility to ensure that you treat timezone issues with due care.

This behavior also exists for other types. For instance, many databases do not have a Rational type, so they will just use the show function and store a Rational as a string.

The conversion between Haskell types and database types is complex, and generic code in HDBC or its backends cannot possibly accomodate every possible situation. In some cases, you may be best served by converting your Haskell type to a String, and passing that to the database.

UNICODE AND BYTESTRINGS

Beginning with HDBC v2.0, interactions with a database are presumed to occur in UTF-8.

To accomplish this, whenever a ByteString must be converted to or from a String, the ByteString is assumed to be in UTF-8 encoding, and will be decoded or encoded as appropriate. Database drivers will generally present text or string data they have received from the database as a SqlValue holding a ByteString, which `fromSql` will automatically convert to a String, and thus automatically decode UTF-8, when you need it. In the other direction, database drivers will generally convert a `SqlString` to a ByteString in UTF-8 encoding before passing it to the database engine.

If you are handling some sort of binary data that is not in UTF-8, you can of course work with the ByteString directly, which will bypass any conversion.

Due to lack of support by database engines, lazy ByteStrings are not passed to database drivers. When you use `toSql` on a lazy ByteString, it will be converted to a strict ByteString for storage. Similarly, `fromSql` will convert a strict ByteString to a lazy ByteString if you demand it.

EQUALITY OF SQLVALUE

Two SqlValues are considered to be equal if one of these hold. The first comparison that can be made is controlling; if none of these comparisons can be made, then they are not equal:

- Both are NULL
- Both represent the same type and the encapsulated values are considered equal by applying (==) to them
- The values of each, when converted to a string, are equal

STRING VERSIONS OF TIMES

Default string representations are given as comments below where such are non-obvious. These are used for `fromSql` when a `String` is desired. They are also defaults for representing data to SQL backends, though individual backends may override them when a different format is demanded by the underlying database. Date and time formats use ISO8601 date format, with HH:MM:SS added for time, and -HHMM added for timezone offsets.

DEPRECATED CONSTRUCTORS

`SqlEpochTime` and `SqlTimeDiff` are no longer created automatically by any `toSql` or `fromSql` functions or database backends. They may still be manually constructed, but are expected to be removed in a future version. Although these two constructures will be removed, support for marshalling to and from the old System.Time data will be maintained as long as System.Time is, simply using the newer data types for conversion.

Constructors

SqlString String |
SqlByteString ByteString |
SqlWord32 Word32 |
SqlWord64 Word64 |
SqlInt32 Int32 |
SqlInt64 Int64 |
SqlInteger Integer |
SqlChar Char |
SqlBool Bool |
SqlDouble Double |
SqlRational Rational |
SqlLocalDate Day | Local YYYY-MM-DD (no timezone).
SqlLocalTimeOfDay TimeOfDay | Local HH:MM:SS (no timezone).
SqlZonedLocalTimeOfDay TimeOfDay TimeZone | Local HH:MM:SS -HHMM. Converts to and from (TimeOfDay, TimeZone).
SqlLocalTime LocalTime | Local YYYY-MM-DD HH:MM:SS (no timezone).
SqlZonedTime ZonedTime | Local YYYY-MM-DD HH:MM:SS -HHMM. Considered equal if both convert to the same UTC time.
SqlUTCTime UTCTime | UTC YYYY-MM-DD HH:MM:SS.
SqlDiffTime NominalDiffTime | Calendar diff between seconds. Rendered as Integer when converted to String, but greater precision may be preserved for other types or to underlying database.
SqlPOSIXTime POSIXTime | Time as seconds since midnight Jan 1 1970 UTC. Integer rendering as for `SqlDiffTime`.
SqlEpochTime Integer | DEPRECATED Representation of ClockTime or CalendarTime. Use SqlPOSIXTime instead.
SqlTimeDiff Integer | DEPRECATED Representation of TimeDiff. Use SqlDiffTime instead.
SqlNull | NULL in SQL or Nothing in Haskell.

#### Instances

Instances details

Show SqlValue Source # |
Instance details Defined in Database.HDBC.SqlValue Methods showsPrec :: Int -> SqlValue -> ShowS Source # show :: SqlValue -> String Source # showList :: [SqlValue] -> ShowS Source #
Eq SqlValue Source # |
Instance details Defined in Database.HDBC.SqlValue Methods (==) :: SqlValue -> SqlValue -> Bool Source # (/=) :: SqlValue -> SqlValue -> Bool Source #
Convertible SqlValue SqlValue Source # |
Instance details Defined in Database.HDBC.SqlValue Methods safeConvert :: SqlValue -> ConvertResult SqlValue Source #
Convertible SqlValue ByteString Source # |
Instance details Defined in Database.HDBC.SqlValue Methods safeConvert :: SqlValue -> ConvertResult ByteString Source #
Convertible SqlValue ByteString Source # |
Instance details Defined in Database.HDBC.SqlValue Methods safeConvert :: SqlValue -> ConvertResult ByteString Source #
Convertible SqlValue Int32 Source # |
Instance details Defined in Database.HDBC.SqlValue Methods safeConvert :: SqlValue -> ConvertResult Int32 Source #
Convertible SqlValue Int64 Source # |
Instance details Defined in Database.HDBC.SqlValue Methods safeConvert :: SqlValue -> ConvertResult Int64 Source #
Convertible SqlValue Rational Source # |
Instance details Defined in Database.HDBC.SqlValue Methods safeConvert :: SqlValue -> ConvertResult Rational Source #
Convertible SqlValue Word32 Source # |
Instance details Defined in Database.HDBC.SqlValue Methods safeConvert :: SqlValue -> ConvertResult Word32 Source #
Convertible SqlValue Word64 Source # |
Instance details Defined in Database.HDBC.SqlValue Methods safeConvert :: SqlValue -> ConvertResult Word64 Source #
Convertible SqlValue CalendarTime Source # |
Instance details Defined in Database.HDBC.SqlValue Methods safeConvert :: SqlValue -> ConvertResult CalendarTime Source #
Convertible SqlValue ClockTime Source # |
Instance details Defined in Database.HDBC.SqlValue Methods safeConvert :: SqlValue -> ConvertResult ClockTime Source #
Convertible SqlValue TimeDiff Source # |
Instance details Defined in Database.HDBC.SqlValue Methods safeConvert :: SqlValue -> ConvertResult TimeDiff Source #
Convertible SqlValue Text Source # |
Instance details Defined in Database.HDBC.SqlValue Methods safeConvert :: SqlValue -> ConvertResult Text Source #
Convertible SqlValue Text Source # |
Instance details Defined in Database.HDBC.SqlValue Methods safeConvert :: SqlValue -> ConvertResult Text Source #
Convertible SqlValue Day Source # |
Instance details Defined in Database.HDBC.SqlValue Methods safeConvert :: SqlValue -> ConvertResult Day Source #
Convertible SqlValue DiffTime Source # |
Instance details Defined in Database.HDBC.SqlValue Methods safeConvert :: SqlValue -> ConvertResult DiffTime Source #
Convertible SqlValue NominalDiffTime Source # |
Instance details Defined in Database.HDBC.SqlValue Methods safeConvert :: SqlValue -> ConvertResult NominalDiffTime Source #
Convertible SqlValue UTCTime Source # |
Instance details Defined in Database.HDBC.SqlValue Methods safeConvert :: SqlValue -> ConvertResult UTCTime Source #
Convertible SqlValue LocalTime Source # |
Instance details Defined in Database.HDBC.SqlValue Methods safeConvert :: SqlValue -> ConvertResult LocalTime Source #
Convertible SqlValue TimeOfDay Source # |
Instance details Defined in Database.HDBC.SqlValue Methods safeConvert :: SqlValue -> ConvertResult TimeOfDay Source #
Convertible SqlValue ZonedTime Source # |
Instance details Defined in Database.HDBC.SqlValue Methods safeConvert :: SqlValue -> ConvertResult ZonedTime Source #
Convertible SqlValue Integer Source # |
Instance details Defined in Database.HDBC.SqlValue Methods safeConvert :: SqlValue -> ConvertResult Integer Source #
Convertible SqlValue String Source # |
Instance details Defined in Database.HDBC.SqlValue Methods safeConvert :: SqlValue -> ConvertResult String Source #
Convertible SqlValue Bool Source # |
Instance details Defined in Database.HDBC.SqlValue Methods safeConvert :: SqlValue -> ConvertResult Bool Source #
Convertible SqlValue Char Source # |
Instance details Defined in Database.HDBC.SqlValue Methods safeConvert :: SqlValue -> ConvertResult Char Source #
Convertible SqlValue Double Source # |
Instance details Defined in Database.HDBC.SqlValue Methods safeConvert :: SqlValue -> ConvertResult Double Source #
Convertible SqlValue Int Source # |
Instance details Defined in Database.HDBC.SqlValue Methods safeConvert :: SqlValue -> ConvertResult Int Source #
Convertible ByteString SqlValue Source # |
Instance details Defined in Database.HDBC.SqlValue Methods safeConvert :: ByteString -> ConvertResult SqlValue Source #
Convertible ByteString SqlValue Source # |
Instance details Defined in Database.HDBC.SqlValue Methods safeConvert :: ByteString -> ConvertResult SqlValue Source #
Convertible Int32 SqlValue Source # |
Instance details Defined in Database.HDBC.SqlValue Methods safeConvert :: Int32 -> ConvertResult SqlValue Source #
Convertible Int64 SqlValue Source # |
Instance details Defined in Database.HDBC.SqlValue Methods safeConvert :: Int64 -> ConvertResult SqlValue Source #
Convertible Rational SqlValue Source # |
Instance details Defined in Database.HDBC.SqlValue Methods safeConvert :: Rational -> ConvertResult SqlValue Source #
Convertible Word32 SqlValue Source # |
Instance details Defined in Database.HDBC.SqlValue Methods safeConvert :: Word32 -> ConvertResult SqlValue Source #
Convertible Word64 SqlValue Source # |
Instance details Defined in Database.HDBC.SqlValue Methods safeConvert :: Word64 -> ConvertResult SqlValue Source #
Convertible CalendarTime SqlValue Source # |
Instance details Defined in Database.HDBC.SqlValue Methods safeConvert :: CalendarTime -> ConvertResult SqlValue Source #
Convertible ClockTime SqlValue Source # |
Instance details Defined in Database.HDBC.SqlValue Methods safeConvert :: ClockTime -> ConvertResult SqlValue Source #
Convertible TimeDiff SqlValue Source # |
Instance details Defined in Database.HDBC.SqlValue Methods safeConvert :: TimeDiff -> ConvertResult SqlValue Source #
Convertible Text SqlValue Source # |
Instance details Defined in Database.HDBC.SqlValue Methods safeConvert :: Text -> ConvertResult SqlValue Source #
Convertible Text SqlValue Source # |
Instance details Defined in Database.HDBC.SqlValue Methods safeConvert :: Text -> ConvertResult SqlValue Source #
Convertible Day SqlValue Source # |
Instance details Defined in Database.HDBC.SqlValue Methods safeConvert :: Day -> ConvertResult SqlValue Source #
Convertible DiffTime SqlValue Source # |
Instance details Defined in Database.HDBC.SqlValue Methods safeConvert :: DiffTime -> ConvertResult SqlValue Source #
Convertible NominalDiffTime SqlValue Source # |
Instance details Defined in Database.HDBC.SqlValue Methods safeConvert :: NominalDiffTime -> ConvertResult SqlValue Source #
Convertible UTCTime SqlValue Source # |
Instance details Defined in Database.HDBC.SqlValue Methods safeConvert :: UTCTime -> ConvertResult SqlValue Source #
Convertible LocalTime SqlValue Source # |
Instance details Defined in Database.HDBC.SqlValue Methods safeConvert :: LocalTime -> ConvertResult SqlValue Source #
Convertible TimeOfDay SqlValue Source # |
Instance details Defined in Database.HDBC.SqlValue Methods safeConvert :: TimeOfDay -> ConvertResult SqlValue Source #
Convertible ZonedTime SqlValue Source # |
Instance details Defined in Database.HDBC.SqlValue Methods safeConvert :: ZonedTime -> ConvertResult SqlValue Source #
Convertible Integer SqlValue Source # |
Instance details Defined in Database.HDBC.SqlValue Methods safeConvert :: Integer -> ConvertResult SqlValue Source #
Convertible String SqlValue Source # |
Instance details Defined in Database.HDBC.SqlValue Methods safeConvert :: String -> ConvertResult SqlValue Source #
Convertible Bool SqlValue Source # |
Instance details Defined in Database.HDBC.SqlValue Methods safeConvert :: Bool -> ConvertResult SqlValue Source #
Convertible Char SqlValue Source # |
Instance details Defined in Database.HDBC.SqlValue Methods safeConvert :: Char -> ConvertResult SqlValue Source #
Convertible Double SqlValue Source # |
Instance details Defined in Database.HDBC.SqlValue Methods safeConvert :: Double -> ConvertResult SqlValue Source #
Convertible Int SqlValue Source # |
Instance details Defined in Database.HDBC.SqlValue Methods safeConvert :: Int -> ConvertResult SqlValue Source #
Convertible SqlValue a => Convertible SqlValue (Maybe a) Source # |
Instance details Defined in Database.HDBC.SqlValue Methods safeConvert :: SqlValue -> ConvertResult (Maybe a) Source #
Convertible SqlValue (TimeOfDay, TimeZone) Source # |
Instance details Defined in Database.HDBC.SqlValue Methods safeConvert :: SqlValue -> ConvertResult (TimeOfDay, TimeZone) Source #
Convertible a SqlValue => Convertible (Maybe a) SqlValue Source # |
Instance details Defined in Database.HDBC.SqlValue Methods safeConvert :: Maybe a -> ConvertResult SqlValue Source #
Convertible (TimeOfDay, TimeZone) SqlValue Source # |
Instance details Defined in Database.HDBC.SqlValue Methods safeConvert :: (TimeOfDay, TimeZone) -> ConvertResult SqlValue Source #