packages feed

sqlcli-odbc (empty) → 0.1.0.0

raw patch · 6 files changed

+167/−0 lines, 6 filesdep +basedep +sqlclisetup-changed

Dependencies added: base, sqlcli

Files

+ LICENSE view
@@ -0,0 +1,30 @@+Copyright Mihai Giurgeanu (c) 2017++All rights reserved.++Redistribution and use in source and binary forms, with or without+modification, are permitted provided that the following conditions are met:++    * Redistributions of source code must retain the above copyright+      notice, this list of conditions and the following disclaimer.++    * Redistributions in binary form must reproduce the above+      copyright notice, this list of conditions and the following+      disclaimer in the documentation and/or other materials provided+      with the distribution.++    * Neither the name of Mihai Giurgeanu nor the names of other+      contributors may be used to endorse or promote products derived+      from this software without specific prior written permission.++THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR+A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT+OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,+SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT+LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,+DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY+THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE+OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ README.md view
@@ -0,0 +1,5 @@+# sqlcli-odbc++This package adds to the sqlcli package ODBC specific definitions need it+if you intend to use sqlcli package to connect to ODBC.+
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ sqlcli-odbc.cabal view
@@ -0,0 +1,27 @@+name:                sqlcli-odbc+version:             0.1.0.0+synopsis:            Specific ODBC definitions to be used by SQL CLI clients.+description:         You should use this package if you intend to use sqlcli package+                     to connect to ODBC datasources.+homepage:            https://hub.darcs.com/mihaigiurgeanu/sqlcli-odbc+license:             BSD3+license-file:        LICENSE+author:              Mihai Giurgeanu+maintainer:          mihai.giurgeanu@gmail.com+copyright:           2017 Mihai Giurgeanu+category:            Web+build-type:          Simple+extra-source-files:  README.md+cabal-version:       >=1.10++library+  hs-source-dirs:      src+  exposed-modules:     SQL.ODBC,+                       SQL.CLI.ODBC+  build-depends:       base >= 4.7 && < 5,+                       sqlcli >= 0.1 && < 0.2+  default-language:    Haskell2010++source-repository head+  type:     darcs+  location: https://hub.darcs.com/mihaigiurgeanu/sqlcli-odbc
+ src/SQL/CLI/ODBC.hs view
@@ -0,0 +1,45 @@+module SQL.CLI.ODBC where
+
+import Prelude hiding (fail)
+
+import Control.Monad.IO.Class (MonadIO, liftIO)
+import Control.Monad.Fail (MonadFail, fail)
+
+import System.IO (hPutStrLn, stderr)
+
+import Foreign.Ptr (wordPtrToPtr)
+
+import SQL.ODBC (sql_attr_odbc_version, sql_ov_odbc3)
+import SQL.CLI (SQLHENV, sqlsetenvattr, sql_handle_env, sql_null_handle, sql_success, sql_success_with_info, sql_invalid_handle, sql_error)
+import SQL.CLI.Utils (SQLConfig(SQLConfig), allocHandle, displayDiagInfo)
+
+-- | holds information specific to ODBC implementation
+odbcImplementation :: SQLConfig
+odbcImplementation =  SQLConfig 1 2 3 4 5 6 7 8 9 10 11 12 13 15 16 17 18
+
+-- | helper function to allocate and setup an ODBC environment handle; it displays
+-- diagnostics on standard error and fails if the handle could not be allocated
+-- or setting the environment failed; it requires ODBC 3 implementation
+setupEnv :: (MonadIO m, MonadFail m) => m SQLHENV
+setupEnv = do
+  liftIO $ hPutStrLn stderr "alloc env handle"
+  henv <- allocHandle sql_handle_env sql_null_handle
+  liftIO $ hPutStrLn stderr "odbc specific - set odbc version env attr"
+  resultSetEnvOV <- liftIO $ sqlsetenvattr henv sql_attr_odbc_version (wordPtrToPtr $ fromIntegral sql_ov_odbc3) 0
+  case resultSetEnvOV of x | x == sql_success -> return henv
+                           | x == sql_success_with_info -> do
+                               liftIO $ do
+                                 hPutStrLn stderr "Set ODBC version generated warnings"
+                                 displayDiagInfo sql_handle_env henv
+                               return henv
+                           | x == sql_invalid_handle -> do
+                               liftIO $ hPutStrLn stderr "Set ODBC version failed because invalid handle was passed to SetEnvAttr function."
+                               fail "Set ODBC version failed"
+                           | x == sql_error -> do
+                               liftIO $ do
+                                 hPutStrLn stderr "Set ODBC version failed. Error diagnostics follow:"
+                                 displayDiagInfo sql_handle_env henv
+                               fail "Set ODBC version failed"
+                           | otherwise -> do
+                               liftIO $ hPutStrLn stderr $ "SetEnvAttr returned unexpected return code: " ++ (show x)
+                               fail "Set ODBC version failed"
+ src/SQL/ODBC.hs view
@@ -0,0 +1,58 @@+module SQL.ODBC where++import Foreign.C.Types++-- env attribute+sql_attr_odbc_version :: Num a => a+sql_attr_odbc_version = 200++sql_attr_connection_pooling :: Num a => a+sql_attr_connection_pooling = 201++sql_attr_cp_match :: Num a => a+sql_attr_cp_match = 202++-- unixODBC additions+sql_attr_unixodbc_syspath :: Num a => a+sql_attr_unixodbc_syspath = 65001++sql_attr_unixodbc_version :: Num a => a+sql_attr_unixodbc_version = 65002++sql_attr_unixodbc_envattr :: Num a => a+sql_attr_unixodbc_envattr = 65003+++-- values for SQL_ATTR_CONNECTION_POOLING+sql_cp_off :: CULong+sql_cp_off = 0++sql_cp_one_per_driver :: CULong+sql_cp_one_per_driver = 1++sql_cp_one_per_henv :: CULong+sql_cp_one_per_henv = 2++sql_cp_default :: CULong+sql_cp_default = sql_cp_off++-- values for SQL_ATTR_CP_MATCH+sql_cp_strict_match :: CULong+sql_cp_strict_match = 0++sql_cp_relaxed_match :: CULong+sql_cp_relaxed_match = 1++sql_cp_match_default :: CULong+sql_cp_match_default = sql_cp_strict_match++-- values for SQL_ATTR_ODBC_VERSION+sql_ov_odbc2 :: CULong+sql_ov_odbc2 = 2++sql_ov_odbc3 :: CULong+sql_ov_odbc3 = 3++-- From ODBC 3.8 onwards, we should use <major version> * 100 + <minor version>+sql_ov_odbc3_80 :: CULong+sql_ov_odbc3_80 = 380