<commit_before>{- -*- mode: haskell; -*- 
   vim: set filetype=haskell:
Copyright (C) 2005-2007 <NAME> <<EMAIL>>

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2.1 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

-}

module Database.HDBC.Sqlite3.Utils where
import Foreign.C.String
import Foreign.ForeignPtr
import Foreign.Ptr
import Database.HDBC(throwSqlError)
import Database.HDBC.Types
import Database.HDBC.Sqlite3.Types
import Foreign.C.Types
import Control.Exception
import Foreign.Storable

#include "hdbc-sqlite3-helper.h"

checkError :: String -> Sqlite3 -> CInt -> IO ()
checkError _ _ 0 = return ()
checkError msg o res =
    withSqlite3 o
     (\p -> do rc <- sqlite3_errmsg p
               str <- peekCString rc
               throwSqlError $
                          SqlError {seState = "",
                                    seNativeError = fromIntegral res,
                                    seErrorMsg = msg ++ ": " ++ str}
     )

{- This is a little hairy.

We have a CSqlite3 object that is actually a finalizeonce wrapper around
the real object.  We use withSqlite3 to dereference the foreign pointer,
and then extract the pointer to the real object from the finalizeonce struct.

But, when we close the connection, we need the finalizeonce struct, so that's
done by withRawSqlite3.<commit_msg>Handle UTF8 errors<commit_after>{- -*- mode: haskell; -*- 
   vim: set filetype=haskell:
Copyright (C) 2005-2007 <NAME> <<EMAIL>>

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2.1 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

-}

module Database.HDBC.Sqlite3.Utils where
import Foreign.C.String
import Foreign.ForeignPtr
import Foreign.Ptr
import Database.HDBC(throwSqlError)
import Database.HDBC.Types
import Database.HDBC.Sqlite3.Types
import qualified Data.ByteString as B
import qualified Data.ByteString.UTF8 as BUTF8
import Foreign.C.Types
import Control.Exception
import Foreign.Storable

#include "hdbc-sqlite3-helper.h"

checkError :: String -> Sqlite3 -> CInt -> IO ()
checkError _ _ 0 = return ()
checkError msg o res =
    withSqlite3 o
     (\p -> do rc <- sqlite3_errmsg p
               bs <- B.packCString rc
               let str = BUTF8.toString bs
               throwSqlError $
                          SqlError {seState = "",
                                    seNativeError = fromIntegral res,
                                    seErrorMsg = msg ++ ": " ++ str}
     )

{- This is a little hairy.

We have a CSqlite3 object that is actually a finalizeonce wrapper around
the real object.  We use withSqlite3 to dereference the foreign pointer,
and then extract the pointer to the real object from the finalizeonce struct.

But, when we close the connection, we need the finalizeonce struct, so that's
done by withRawSqlite3.
{'fraction_non_alphanumeric': 0.06246925725528775, 'fraction_numerical': 0.016232169208066895, 'mean_word_length': 2.8151969981238274, 'pattern_counts': {'":': 2, '<': 13, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5876171', 'n_tokens_mistral': 1129, 'n_tokens_neox': 1064, 'n_words': 491}