diff options
author | William Carroll <wpcarro@gmail.com> | 2020-07-30T17·52+0100 |
---|---|---|
committer | William Carroll <wpcarro@gmail.com> | 2020-07-30T17·52+0100 |
commit | 6ecab8c3a625d58fa5c4c5daf62a6567b4fc7701 (patch) | |
tree | 0fe10c6750640706b055fa64e78e92f34dbde392 /src/Trips.hs | |
parent | dec8890190ff0b86f1a50044814701ef39b808e6 (diff) |
Prefer SELECT (a,b,c) to SELECT *
"SELECT *" in SQL may not guarantee the order in which a record's columns are returned. For example, in my FromRow instances for Account, I make successive call The following scenario silently and erroneously assigns: firstName, lastName = lastName, firstName ```sql CREATE TABLE People ( firstName TEXT NOT NULL, lastName TEXT NOT NULL, age INTEGER NOT NULL, PRIMARY KEY (firstName, lastName) ) ``` ```haskell data Person = Person { firstName :: String, lastName :: String, age :: Integer } fromRow = do firstName <- field lastName <- field age <- field pure Person{..} getPeople :: Connection -> IO [Person] getPeople conn = query conn "SELECT * FROM People" ``` This silently fails because both firstName and lastName are Strings, and so the FromRow Person instance type-checks, but you should expect to receive a list of names like "Wallace William" instead of "William Wallace". The following won't break the type-checker, but will result in a runtime parsing error: ```haskell -- all code from the previous example remains the same except for: fromRow = do age <- field firstName <- field lastName <- field ``` The "SELECT *" will return records like (firstName,lastName,age), but the FromRow instance for Person will attempt to parse firstName as Integer. So... what have we learned? Prefer "SELECT (firstName,lastName,age)" instead of "SELECT *".
Diffstat (limited to 'src/Trips.hs')
-rw-r--r-- | src/Trips.hs | 2 |
1 files changed, 1 insertions, 1 deletions
diff --git a/src/Trips.hs b/src/Trips.hs index 0d805cbe8603..a7880b503ddd 100644 --- a/src/Trips.hs +++ b/src/Trips.hs @@ -24,4 +24,4 @@ delete dbFile tripPK = -- | Return a list of all of the trips in `dbFile`. list :: FilePath -> IO [T.Trip] list dbFile = withConnection dbFile $ \conn -> - query_ conn "SELECT * FROM Trips" + query_ conn "SELECT (username,destination,startDate,endDate,comment) FROM Trips" |