| 1 | (* |
| 2 | * SQL database interfaces for Standard ML |
| 3 | * Copyright (C) 2003 Adam Chlipala |
| 4 | * |
| 5 | * This library is free software; you can redistribute it and/or |
| 6 | * modify it under the terms of the GNU Lesser General Public |
| 7 | * License as published by the Free Software Foundation; either |
| 8 | * version 2.1 of the License, or (at your option) any later version. |
| 9 | * |
| 10 | * This library is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | * Lesser General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU Lesser General Public |
| 16 | * License along with this library; if not, write to the Free Software |
| 17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 18 | *) |
| 19 | |
| 20 | (* Functor for creating a client from a minimal driver *) |
| 21 | |
| 22 | functor SqlClient(Driver : SQL_DRIVER) :> SQL_CLIENT |
| 23 | where type conn = Driver.conn = |
| 24 | struct |
| 25 | open Driver |
| 26 | |
| 27 | fun oneOrNoRows conn query = |
| 28 | fold conn (fn (row, NONE) => SOME row |
| 29 | | (_, SOME _) => raise Sql ("Expected one or zero rows; got multiple for:\n" ^ query)) NONE query |
| 30 | |
| 31 | fun oneRow conn query = |
| 32 | (case oneOrNoRows conn query of |
| 33 | NONE => raise Sql "Expected one row; got none" |
| 34 | | SOME row => row) |
| 35 | |
| 36 | fun app conn f q = fold conn (fn (row, ()) => f row) () q |
| 37 | fun map conn f q = List.rev (fold conn (fn (row, out) => (f row)::out) [] q) |
| 38 | |
| 39 | fun query conn q = List.rev (fold conn op:: [] q) |
| 40 | end |
| 41 | |