Permalink
Browse files

Replace most uses of String with ByteString

  • Loading branch information...
1 parent 16b4aa1 commit fa155b10a5523dfaef4b93ea22c1895ffe65017c @lpsmith committed Feb 22, 2011
Showing with 92 additions and 88 deletions.
  1. +4 −3 Database/HDBC/ColTypes.hs
  2. +39 −39 Database/HDBC/SqlValue.hs
  3. +10 −9 Database/HDBC/Statement.hs
  4. +15 −14 Database/HDBC/Types.hs
  5. +24 −23 Database/HDBC/Utils.hs
@@ -38,6 +38,7 @@ module Database.HDBC.ColTypes (SqlColDesc(..),
where
import Data.Dynamic
+import Data.ByteString ( ByteString )
{- | The description of a column.
@@ -52,7 +53,7 @@ types, the total number of digits allowed. See the ODBC manual for more.
The colOctetLength field is defined for character and binary types, and
gives the number of bytes the column requires, regardless of encoding.
-}
-data SqlColDesc =
+data SqlColDesc =
SqlColDesc {
colType :: SqlTypeId -- ^ Type of data stored here
,colSize :: Maybe Int -- ^ The size of a column
@@ -68,7 +69,7 @@ sqlColDescTc = mkTyCon "Database.HDBC.SqlColDesc"
instance Typeable SqlColDesc where
typeOf _ = mkTyConApp sqlColDescTc []
-{- | The type identifier for a given column.
+{- | The type identifier for a given column.
This represents the type of data stored in the column in the underlying
SQL engine. It does not form the entire column type; see 'SqlColDesc' for
@@ -104,7 +105,7 @@ data SqlTypeId =
| SqlUTCTimeT -- ^ UTC time
| SqlIntervalT SqlInterval -- ^ A time or date difference
| SqlGUIDT -- ^ Global unique identifier
- | SqlUnknownT String -- ^ A type not represented here; implementation-specific information in the String
+ | SqlUnknownT ByteString -- ^ A type not represented here; implementation-specific information in the String
deriving (Eq, Show, Read)
View
@@ -24,7 +24,7 @@ import Data.Fixed
quickError :: (Typeable a, Convertible SqlValue a) => SqlValue -> ConvertResult a
quickError sv = convError "incompatible types" sv
-
+
{- | Convert a value to an 'SqlValue'. This function is simply
a restricted-type wrapper around 'convert'. See extended notes on 'SqlValue'. -}
toSql :: Convertible a SqlValue => a -> SqlValue
@@ -66,7 +66,7 @@ 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'
+constructor. 'fromSql' or 'safeFromSql'
can then be used to convert them into whatever type
is needed locally in Haskell.
@@ -120,7 +120,7 @@ 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,
+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.
@@ -185,7 +185,7 @@ expected to be removed in a future version. Although these two constructures wi
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.
-}
-data SqlValue = SqlString String
+data SqlValue = SqlString String
| SqlByteString B.ByteString
| SqlWord32 Word32
| SqlWord64 Word64
@@ -237,7 +237,7 @@ instance Eq SqlValue where
SqlNull == SqlNull = True
SqlNull == _ = False
_ == SqlNull = False
- a == b = ((safeFromSql a)::ConvertResult String) ==
+ a == b = ((safeFromSql a)::ConvertResult String) ==
((safeFromSql b)::ConvertResult String)
instance Convertible String SqlValue where
@@ -254,18 +254,18 @@ instance Convertible SqlValue String where
safeConvert (SqlBool x) = return . show $ x
safeConvert (SqlDouble x) = return . show $ x
safeConvert (SqlRational x) = return . show $ x
- safeConvert (SqlLocalDate x) =
+ safeConvert (SqlLocalDate x) =
return . formatTime defaultTimeLocale (iso8601DateFormat Nothing) $ x
- safeConvert (SqlLocalTimeOfDay x) =
+ safeConvert (SqlLocalTimeOfDay x) =
return . formatTime defaultTimeLocale "%T%Q" $ x
- safeConvert (SqlZonedLocalTimeOfDay tod tz) =
+ safeConvert (SqlZonedLocalTimeOfDay tod tz) =
return $ formatTime defaultTimeLocale "%T%Q " tod ++
formatTime defaultTimeLocale "%z" tz
- safeConvert (SqlLocalTime x) =
+ safeConvert (SqlLocalTime x) =
return . formatTime defaultTimeLocale (iso8601DateFormat (Just "%T%Q")) $ x
- safeConvert (SqlZonedTime x) =
+ safeConvert (SqlZonedTime x) =
return . formatTime defaultTimeLocale (iso8601DateFormat (Just "%T%Q %z")) $ x
- safeConvert (SqlUTCTime x) =
+ safeConvert (SqlUTCTime x) =
return . formatTime defaultTimeLocale (iso8601DateFormat (Just "%T%Q")) $ x
safeConvert (SqlDiffTime x) = return $ showFixed True fixedval
where fixedval :: Pico
@@ -296,7 +296,7 @@ instance Convertible SqlValue BSL.ByteString where
return (BSL.fromChunks [bs])
instance Convertible Int SqlValue where
- safeConvert x =
+ safeConvert x =
do i <- ((safeConvert x)::ConvertResult Int64)
return $ SqlInt64 i
instance Convertible SqlValue Int where
@@ -312,9 +312,9 @@ instance Convertible SqlValue Int where
safeConvert (SqlDouble x) = safeConvert x
safeConvert (SqlRational x) = safeConvert x
safeConvert y@(SqlLocalDate _) = viaInteger y fromIntegral
- safeConvert y@(SqlLocalTimeOfDay _) = viaInteger y fromIntegral
+ safeConvert y@(SqlLocalTimeOfDay _) = viaInteger y fromIntegral
safeConvert y@(SqlZonedLocalTimeOfDay _ _) = quickError y
- safeConvert y@(SqlLocalTime _) = viaInteger y fromIntegral
+ safeConvert y@(SqlLocalTime _) = viaInteger y fromIntegral
safeConvert y@(SqlZonedTime _) = viaInteger y fromIntegral
safeConvert (SqlUTCTime x) = safeConvert x
safeConvert (SqlDiffTime x) = safeConvert x
@@ -442,11 +442,11 @@ instance Convertible SqlValue Integer where
safeConvert (SqlDouble x) = safeConvert x
safeConvert (SqlRational x) = safeConvert x
safeConvert (SqlLocalDate x) = return . toModifiedJulianDay $ x
- safeConvert (SqlLocalTimeOfDay x) =
+ safeConvert (SqlLocalTimeOfDay x) =
return . fromIntegral . fromEnum . timeOfDayToTime $ x
safeConvert y@(SqlZonedLocalTimeOfDay _ _) = quickError y
safeConvert y@(SqlLocalTime _) = quickError y
- safeConvert (SqlZonedTime x) =
+ safeConvert (SqlZonedTime x) =
return . truncate . utcTimeToPOSIXSeconds . zonedTimeToUTC $ x
safeConvert (SqlUTCTime x) = safeConvert x
safeConvert (SqlDiffTime x) = safeConvert x
@@ -458,7 +458,7 @@ instance Convertible SqlValue Integer where
instance Convertible Bool SqlValue where
safeConvert = return . SqlBool
instance Convertible SqlValue Bool where
- safeConvert y@(SqlString x) =
+ safeConvert y@(SqlString x) =
case map toUpper x of
"TRUE" -> Right True
"T" -> Right True
@@ -497,7 +497,7 @@ instance Convertible Char SqlValue where
instance Convertible SqlValue Char where
safeConvert (SqlString [x]) = return x
safeConvert y@(SqlString _) = convError "String length /= 1" y
- safeConvert y@(SqlByteString x) =
+ safeConvert y@(SqlByteString x) =
case B.length x of
1 -> safeConvert . SqlString . BUTF8.toString $ x
_ -> convError "ByteString length /= 1" y
@@ -536,15 +536,15 @@ instance Convertible SqlValue Double where
safeConvert (SqlBool x) = return (if x then 1.0 else 0.0)
safeConvert (SqlDouble x) = return x
safeConvert (SqlRational x) = safeConvert x
- safeConvert y@(SqlLocalDate _) = ((safeConvert y)::ConvertResult Integer) >>=
+ safeConvert y@(SqlLocalDate _) = ((safeConvert y)::ConvertResult Integer) >>=
(return . fromIntegral)
- safeConvert (SqlLocalTimeOfDay x) =
+ safeConvert (SqlLocalTimeOfDay x) =
return . fromRational . toRational . timeOfDayToTime $ x
safeConvert y@(SqlZonedLocalTimeOfDay _ _) = quickError y
safeConvert y@(SqlLocalTime _) = quickError y
- safeConvert (SqlZonedTime x) =
+ safeConvert (SqlZonedTime x) =
safeConvert . SqlUTCTime . zonedTimeToUTC $ x
- safeConvert (SqlUTCTime x) =
+ safeConvert (SqlUTCTime x) =
return . fromRational . toRational . utcTimeToPOSIXSeconds $ x
safeConvert (SqlDiffTime x) = safeConvert x
safeConvert (SqlPOSIXTime x) = safeConvert x
@@ -563,11 +563,11 @@ instance Convertible SqlValue Rational where
safeConvert (SqlWord64 x) = safeConvert x
safeConvert (SqlInteger x) = safeConvert x
safeConvert (SqlChar x) = return . fromIntegral . fromEnum $ x
- safeConvert (SqlBool x) = return $ if x then fromIntegral (1::Int)
+ safeConvert (SqlBool x) = return $ if x then fromIntegral (1::Int)
else fromIntegral (0::Int)
safeConvert (SqlDouble x) = safeConvert x
safeConvert (SqlRational x) = return x
- safeConvert y@(SqlLocalDate _) = ((safeConvert y)::ConvertResult Integer) >>=
+ safeConvert y@(SqlLocalDate _) = ((safeConvert y)::ConvertResult Integer) >>=
(return . fromIntegral)
safeConvert (SqlLocalTimeOfDay x) = return . toRational . timeOfDayToTime $ x
safeConvert y@(SqlZonedLocalTimeOfDay _ _) = quickError y
@@ -605,19 +605,19 @@ instance Convertible Day SqlValue where
instance Convertible SqlValue Day where
safeConvert (SqlString x) = parseTime' (iso8601DateFormat Nothing) x
safeConvert (SqlByteString x) = safeConvert (SqlString (BUTF8.toString x))
- safeConvert (SqlInt32 x) =
+ safeConvert (SqlInt32 x) =
return $ ModifiedJulianDay {toModifiedJulianDay = fromIntegral x}
- safeConvert (SqlInt64 x) =
+ safeConvert (SqlInt64 x) =
return $ ModifiedJulianDay {toModifiedJulianDay = fromIntegral x}
- safeConvert (SqlWord32 x) =
+ safeConvert (SqlWord32 x) =
return $ ModifiedJulianDay {toModifiedJulianDay = fromIntegral x}
- safeConvert (SqlWord64 x) =
+ safeConvert (SqlWord64 x) =
return $ ModifiedJulianDay {toModifiedJulianDay = fromIntegral x}
- safeConvert (SqlInteger x) =
+ safeConvert (SqlInteger x) =
return $ ModifiedJulianDay {toModifiedJulianDay = x}
safeConvert y@(SqlChar _) = quickError y
safeConvert y@(SqlBool _) = quickError y
- safeConvert (SqlDouble x) =
+ safeConvert (SqlDouble x) =
return $ ModifiedJulianDay {toModifiedJulianDay = truncate x}
safeConvert (SqlRational x) = safeConvert . SqlDouble . fromRational $ x
safeConvert (SqlLocalDate x) = return x
@@ -644,7 +644,7 @@ instance Convertible SqlValue TimeOfDay where
safeConvert (SqlInteger x) = return . timeToTimeOfDay . fromInteger $ x
safeConvert y@(SqlChar _) = quickError y
safeConvert y@(SqlBool _) = quickError y
- safeConvert (SqlDouble x) =
+ safeConvert (SqlDouble x) =
return . timeToTimeOfDay . fromIntegral $ ((truncate x)::Integer)
safeConvert (SqlRational x) = safeConvert . SqlDouble . fromRational $ x
safeConvert y@(SqlLocalDate _) = quickError y
@@ -662,7 +662,7 @@ instance Convertible SqlValue TimeOfDay where
instance Convertible (TimeOfDay, TimeZone) SqlValue where
safeConvert (tod, tz) = return (SqlZonedLocalTimeOfDay tod tz)
instance Convertible SqlValue (TimeOfDay, TimeZone) where
- safeConvert (SqlString x) =
+ safeConvert (SqlString x) =
do tod <- parseTime' "%T%Q %z" x
tz <- case parseTime defaultTimeLocale "%T%Q %z" x of
Nothing -> convError "Couldn't extract timezone in" (SqlString x)
@@ -787,7 +787,7 @@ stringToPico s =
instance Convertible NominalDiffTime SqlValue where
safeConvert = return . SqlDiffTime
instance Convertible SqlValue NominalDiffTime where
- safeConvert (SqlString x) = stringToPico x >>=
+ safeConvert (SqlString x) = stringToPico x >>=
return . realToFrac
safeConvert (SqlByteString x) = (stringToPico (BUTF8.toString x)) >>=
return . realToFrac
@@ -800,9 +800,9 @@ instance Convertible SqlValue NominalDiffTime where
safeConvert y@(SqlBool _) = quickError y
safeConvert (SqlDouble x) = return . fromRational . toRational $ x
safeConvert (SqlRational x) = return . fromRational $ x
- safeConvert (SqlLocalDate x) = return . fromIntegral . (\y -> y * 60 * 60 * 24) .
+ safeConvert (SqlLocalDate x) = return . fromIntegral . (\y -> y * 60 * 60 * 24) .
toModifiedJulianDay $ x
- safeConvert (SqlLocalTimeOfDay x) =
+ safeConvert (SqlLocalTimeOfDay x) =
return . fromRational . toRational . timeOfDayToTime $ x
safeConvert y@(SqlZonedLocalTimeOfDay _ _) = quickError y
safeConvert y@(SqlLocalTime _) = quickError y
@@ -844,7 +844,7 @@ instance Convertible SqlValue ST.ClockTime where
instance Convertible ST.TimeDiff SqlValue where
safeConvert x = safeConvert x >>= return . SqlDiffTime
instance Convertible SqlValue ST.TimeDiff where
- safeConvert y@(SqlString _) =
+ safeConvert y@(SqlString _) =
do r <- safeConvert y
safeConvert (SqlDiffTime r)
safeConvert (SqlByteString x) = safeConvert . SqlString . BUTF8.toString $ x
@@ -911,7 +911,7 @@ instance (Convertible SqlValue a) => Convertible SqlValue (Maybe a) where
viaInteger' :: (Convertible SqlValue a, Bounded a, Show a, Convertible a Integer,
Typeable a) => SqlValue -> (Integer -> ConvertResult a) -> ConvertResult a
-viaInteger' sv func =
+viaInteger' sv func =
do i <- ((safeConvert sv)::ConvertResult Integer)
boundedConversion func i
@@ -926,7 +926,7 @@ secs2td x = safeConvert x
-- | Read a value from a string, and give an informative message
-- if it fails.
read' :: (Typeable a, Read a, Convertible SqlValue a) => String -> ConvertResult a
-read' s =
+read' s =
case reads s of
[(x,"")] -> Right x
_ -> convError "Cannot read source value as dest type" (SqlString s)
@@ -937,7 +937,7 @@ parseTime' _ inpstr =
convError "Hugs does not support time parsing" (SqlString inpstr)
#else
parseTime' :: (Typeable t, Convertible SqlValue t, ParseTime t) => String -> String -> ConvertResult t
-parseTime' fmtstr inpstr =
+parseTime' fmtstr inpstr =
case parseTime defaultTimeLocale fmtstr inpstr of
Nothing -> convError ("Cannot parse using default format string " ++ show fmtstr)
(SqlString inpstr)
View
@@ -13,6 +13,7 @@ import Data.Dynamic
import Database.HDBC.ColTypes
import Database.HDBC.SqlValue
import Control.Exception
+import Data.ByteString ( ByteString )
data Statement = Statement
{
@@ -39,8 +40,8 @@ data Statement = Statement
certainly fail; use 'execute' instead. -}
executeRaw :: IO (),
- {- | Execute the query with many rows.
- The return value is the return value from the final row
+ {- | 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
@@ -50,7 +51,7 @@ data Statement = Statement
This is most useful for non-SELECT statements. -}
executeMany :: [[SqlValue]] -> IO (),
-
+
{- | Abort a query in progress -- usually not needed. -}
finish :: IO (),
@@ -63,7 +64,7 @@ data Statement = Statement
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
@@ -77,12 +78,12 @@ data Statement = Statement
A simple getColumnNames implementation could simply
apply @map fst@ to the return value of 'describeResult'.
-}
- getColumnNames :: IO [String],
+ getColumnNames :: IO [ByteString],
{- | The original query that this 'Statement' was prepared
with. -}
- originalQuery :: String,
+ originalQuery :: ByteString,
{- | 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.
@@ -96,17 +97,17 @@ data Statement = Statement
Please see caveats under 'getColumnNames' for information
on the column name field here.
-}
- describeResult :: IO [(String, SqlColDesc)]
+ describeResult :: IO [(ByteString, SqlColDesc)]
}
{- | 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.
-}
-data SqlError = SqlError {seState :: String,
+data SqlError = SqlError {seState :: ByteString,
seNativeError :: Int,
- seErrorMsg :: String}
+ seErrorMsg :: ByteString}
deriving (Eq, Show, Read)
sqlErrorTc :: TyCon
Oops, something went wrong.

0 comments on commit fa155b1

Please sign in to comment.