Database.Sqlite

persistent-sqlite-2.13.3.0: Backend for the persistent library using sqlite3.

Safe Haskell | None
Language | Haskell2010

Database.Sqlite

Contents

- Basic usage guide

Description

A port of the direct-sqlite package for dealing directly with `PersistValue`s.

Synopsis

- data Connection
- data Statement
- data Error
  - = ErrorOK
  - | ErrorError
  - | ErrorInternal
  - | ErrorPermission
  - | ErrorAbort
  - | ErrorBusy
  - | ErrorLocked
  - | ErrorNoMemory
  - | ErrorReadOnly
  - | ErrorInterrupt
  - | ErrorIO
  - | ErrorNotFound
  - | ErrorCorrupt
  - | ErrorFull
  - | ErrorCan'tOpen
  - | ErrorProtocol
  - | ErrorEmpty
  - | ErrorSchema
  - | ErrorTooBig
  - | ErrorConstraint
  - | ErrorMismatch
  - | ErrorMisuse
  - | ErrorNoLargeFileSupport
  - | ErrorAuthorization
  - | ErrorFormat
  - | ErrorRange
  - | ErrorNotAConnection
  - | ErrorRow
  - | ErrorDone
- data SqliteException = SqliteException {
  - seError :: !Error
  - seFunctionName :: !Text
  - seDetails :: !Text
  }
- data StepResult
  - = Row
  - | Done
- data Config = ConfigLogFn LogFunction
- data LogFunction
- data SqliteStatus = SqliteStatus {
  - sqliteStatusCurrent :: Maybe Int
  - sqliteStatusHighwater :: Maybe Int
  }
- data SqliteStatusVerb
  - = SqliteStatusMemoryUsed
  - | SqliteStatusPagecacheUsed
  - | SqliteStatusPagecacheOverflow
  - | SqliteStatusScratchUsed
  - | SqliteStatusScratchOverflow
  - | SqliteStatusMallocSize
  - | SqliteStatusPagecacheSize
  - | SqliteStatusScratchSize
  - | SqliteStatusMallocCount
- open :: Text -> IO Connection
- open' :: ByteString -> IO Connection
- close :: Connection -> IO ()
- prepare :: Connection -> Text -> IO Statement
- step :: Statement -> IO StepResult
- stepConn :: Connection -> Statement -> IO StepResult
- reset :: Connection -> Statement -> IO ()
- finalize :: Statement -> IO ()
- bindBlob :: Statement -> Int -> ByteString -> IO ()
- bindDouble :: Statement -> Int -> Double -> IO ()
- bindInt :: Statement -> Int -> Int -> IO ()
- bindInt64 :: Statement -> Int -> Int64 -> IO ()
- bindNull :: Statement -> Int -> IO ()
- bindText :: Statement -> Int -> Text -> IO ()
- bind :: Statement -> [PersistValue] -> IO ()
- column :: Statement -> Int -> IO PersistValue
- columns :: Statement -> IO [PersistValue]
- changes :: Connection -> IO Int64
- mkLogFunction :: (Int -> String -> IO ()) -> IO LogFunction
- freeLogFunction :: LogFunction -> IO ()
- config :: Config -> IO ()
- status :: SqliteStatusVerb -> Bool -> IO SqliteStatus
- softHeapLimit :: Int64 -> IO Int64
- enableExtendedResultCodes :: Connection -> IO ()
- disableExtendedResultCodes :: Connection -> IO ()

# Documentation

data Connection #

SQLite connection type, consist of an IORef tracking whether the connection has been closed and the raw SQLite C API pointer, wrapped in a 'Connection'' newtype.

Since: 2.10.2

data Statement #

Newtype wrapping SQLite C API pointer for a prepared statement.

Since: 2.10.2

data Error #

Constructors

ErrorOK |
ErrorError |
ErrorInternal |
ErrorPermission |
ErrorAbort |
ErrorBusy |
ErrorLocked |
ErrorNoMemory |
ErrorReadOnly |
ErrorInterrupt |
ErrorIO |
ErrorNotFound |
ErrorCorrupt |
ErrorFull |
ErrorCan'tOpen |
ErrorProtocol |
ErrorEmpty |
ErrorSchema |
ErrorTooBig |
ErrorConstraint |
ErrorMismatch |
ErrorMisuse |
ErrorNoLargeFileSupport |
ErrorAuthorization |
ErrorFormat |
ErrorRange |
ErrorNotAConnection |
ErrorRow |
ErrorDone |

#### Instances

Instances details

Show Error # |
Instance details Defined in Database.Sqlite Methods showsPrec :: Int -> Error -> ShowS # show :: Error -> String # showList :: [Error] -> ShowS #
Eq Error # |
Instance details Defined in Database.Sqlite Methods (==) :: Error -> Error -> Bool # (/=) :: Error -> Error -> Bool #

data SqliteException #

A custom exception type to make it easier to catch exceptions.

Since: 2.1.3

Constructors

SqliteException |
Fields seError :: !Error seFunctionName :: !Text seDetails :: !Text

#### Instances

Instances details

Exception SqliteException # |
Instance details Defined in Database.Sqlite Methods toException :: SqliteException -> SomeException # fromException :: SomeException -> Maybe SqliteException # displayException :: SqliteException -> String # backtraceDesired :: SqliteException -> Bool #
Show SqliteException # |
Instance details Defined in Database.Sqlite Methods showsPrec :: Int -> SqliteException -> ShowS # show :: SqliteException -> String # showList :: [SqliteException] -> ShowS #

data StepResult #

Constructors

Row |
Done |

#### Instances

Instances details

Show StepResult # |
Instance details Defined in Database.Sqlite Methods showsPrec :: Int -> StepResult -> ShowS # show :: StepResult -> String # showList :: [StepResult] -> ShowS #
Eq StepResult # |
Instance details Defined in Database.Sqlite Methods (==) :: StepResult -> StepResult -> Bool # (/=) :: StepResult -> StepResult -> Bool #

data Config #

Configuration option for SQLite to be used together with the `config` function.

Since: 2.1.4

Constructors

ConfigLogFn LogFunction | A function to be used for logging

data LogFunction #

Since: 2.1.4

data SqliteStatus #

Return type of the `status` function

Since: 2.6.1

Constructors

SqliteStatus |
Fields sqliteStatusCurrent :: Maybe Int The current value of the parameter. Some parameters do not record current value. sqliteStatusHighwater :: Maybe Int The highest recorded value. Some parameters do not record the highest value.

#### Instances

Instances details

Show SqliteStatus # |
Instance details Defined in Database.Sqlite Methods showsPrec :: Int -> SqliteStatus -> ShowS # show :: SqliteStatus -> String # showList :: [SqliteStatus] -> ShowS #
Eq SqliteStatus # |
Instance details Defined in Database.Sqlite Methods (==) :: SqliteStatus -> SqliteStatus -> Bool # (/=) :: SqliteStatus -> SqliteStatus -> Bool #

data SqliteStatusVerb #

Run-time status parameter that can be returned by `status` function.

Since: 2.6.1

Constructors

SqliteStatusMemoryUsed | This parameter is the current amount of memory checked out using sqlite3_malloc(), either directly or indirectly. The figure includes calls made to sqlite3_malloc() by the application and internal memory usage by the SQLite library. Scratch memory controlled by SQLITE_CONFIG_SCRATCH and auxiliary page-cache memory controlled by SQLITE_CONFIG_PAGECACHE is not included in this parameter. The amount returned is the sum of the allocation sizes as reported by the xSize method in sqlite3_mem_methods.
SqliteStatusPagecacheUsed | This parameter returns the number of pages used out of the pagecache memory allocator that was configured using SQLITE_CONFIG_PAGECACHE. The value returned is in pages, not in bytes.
SqliteStatusPagecacheOverflow | This parameter returns the number of bytes of page cache allocation which could not be satisfied by the SQLITE_CONFIG_PAGECACHE buffer and where forced to overflow to sqlite3_malloc(). The returned value includes allocations that overflowed because they where too large (they were larger than the "sz" parameter to SQLITE_CONFIG_PAGECACHE) and allocations that overflowed because no space was left in the page cache.
SqliteStatusScratchUsed | This parameter returns the number of allocations used out of the scratch memory allocator configured using SQLITE_CONFIG_SCRATCH. The value returned is in allocations, not in bytes. Since a single thread may only have one scratch allocation outstanding at time, this parameter also reports the number of threads using scratch memory at the same time.
SqliteStatusScratchOverflow | This parameter returns the number of bytes of scratch memory allocation which could not be satisfied by the SQLITE_CONFIG_SCRATCH buffer and where forced to overflow to sqlite3_malloc(). The values returned include overflows because the requested allocation was too larger (that is, because the requested allocation was larger than the "sz" parameter to SQLITE_CONFIG_SCRATCH) and because no scratch buffer slots were available.
SqliteStatusMallocSize | This parameter records the largest memory allocation request handed to sqlite3_malloc() or sqlite3_realloc() (or their internal equivalents). Only the value returned in `sqliteStatusHighwater` field of `SqliteStatus` record is of interest. The value written into the `sqliteStatusCurrent` field is Nothing.
SqliteStatusPagecacheSize | This parameter records the largest memory allocation request handed to pagecache memory allocator. Only the value returned in the `sqliteStatusHighwater` field of `SqliteStatus` record is of interest. The value written into the `sqliteStatusCurrent` field is Nothing.
SqliteStatusScratchSize | This parameter records the largest memory allocation request handed to scratch memory allocator. Only the value returned in the `sqliteStatusHighwater` field of `SqliteStatus` record is of interest. The value written into the `sqliteStatusCurrent` field is Nothing.
SqliteStatusMallocCount | This parameter records the number of separate memory allocations currently checked out.

# Basic usage guide

Note that the example code shown here is a low level interface usage. Let's create a small demo sqlite3 database which we will use in our program:

    $ sqlite3 ~/test.db
    sqlite> create table t1(a,b);
    sqlite> insert into t1(a,b) values (1,1);
    sqlite> insert into t1(a,b) values (2,2);
    sqlite> select * from t1;
    1|1
    2|2

Now let's write code using the functions in this module to fetch the rows from the table:

    {-#LANGUAGE OverloadedStrings#-}

    import Database.Sqlite
    import Data.Text

    main :: IO ()
    main = do
      conn <- open "/home/sibi/test.db"
      smt <- prepare conn "select * from t1;"
      row1 <- step smt >> columns smt
      row2 <- step smt >> columns smt
      print (row1, row2)
      finalize smt
      close conn

On executing the above code:

    $ ./demo-program
    $ ([PersistInt64 1,PersistInt64 1],[PersistInt64 2,PersistInt64 2])

open :: Text -> IO Connection #

open' :: ByteString -> IO Connection #

Like `open`, but accepts a `ByteString` instead of a `Text`.

Since: 2.13.3.0

close :: Connection -> IO () #

prepare :: Connection -> Text -> IO Statement #

step :: Statement -> IO StepResult #

Execute a database statement. It's recommended to use `stepConn` instead, because it gives better error messages.

stepConn :: Connection -> Statement -> IO StepResult #

Execute a database statement. This function uses the `Connection` passed to it to give better error messages than `step`.

Since: 2.6.4

reset :: Connection -> Statement -> IO () #

finalize :: Statement -> IO () #

bindBlob :: Statement -> Int -> ByteString -> IO () #

bindDouble :: Statement -> Int -> Double -> IO () #

bindInt :: Statement -> Int -> Int -> IO () #

bindInt64 :: Statement -> Int -> Int64 -> IO () #

bindNull :: Statement -> Int -> IO () #

bindText :: Statement -> Int -> Text -> IO () #

bind :: Statement -> [PersistValue] -> IO () #

column :: Statement -> Int -> IO PersistValue #

columns :: Statement -> IO [PersistValue] #

changes :: Connection -> IO Int64 #

mkLogFunction :: (Int -> String -> IO ()) -> IO LogFunction #

Wraps a given function to a `LogFunction` to be further used with `ConfigLogFn`. First argument of given function will take error code, second - log message. Returned value should be released with `freeLogFunction` when no longer required.

freeLogFunction :: LogFunction -> IO () #

Releases a native FunPtr for the `LogFunction`.

Since: 2.1.4

config :: Config -> IO () #

Sets SQLite global configuration parameter. See SQLite documentation for the sqlite3_config function. In short, this must be called prior to any other SQLite function if you want the call to succeed.

Since: 2.1.4

status :: SqliteStatusVerb -> Bool -> IO SqliteStatus #

Retrieves runtime status information about the performance of SQLite, and optionally resets various highwater marks. The first argument is a status parameter to measure, the second is reset flag. If reset flag is True then the highest recorded value is reset after being returned from this function.

Since: 2.6.1

softHeapLimit :: Int64 -> IO Int64 #

Sets and/or queries the soft limit on the amount of heap memory that may be allocated by SQLite. If the argument is zero then the soft heap limit is disabled. If the argument is negative then no change is made to the soft heap limit. Hence, the current size of the soft heap limit can be determined by invoking this function with a negative argument.

Since: 2.6.1

enableExtendedResultCodes :: Connection -> IO () #

disableExtendedResultCodes :: Connection -> IO () #