From: <kr_...@us...> - 2004-01-05 20:57:49
|
Update of /cvsroot/htoolkit/HSQL/MySQL In directory sc8-pr-cvs1:/tmp/cvs-serv10102/MySQL Modified Files: HSQL.hsc Log Message: Implementation for "tables" and "decribe" functions Index: HSQL.hsc =================================================================== RCS file: /cvsroot/htoolkit/HSQL/MySQL/HSQL.hsc,v retrieving revision 1.11 retrieving revision 1.12 diff -C2 -d -r1.11 -r1.12 *** HSQL.hsc 4 Jan 2004 13:14:58 -0000 1.11 --- HSQL.hsc 5 Jan 2004 20:57:45 -0000 1.12 *************** *** 21,24 **** --- 21,26 ---- , forEachRow' -- :: (Statement -> IO ()) -> Statement -> IO () , collectRows -- :: (Statement -> IO s) -> Statement -> IO [s] + , tables -- :: Connection -> IO [String] + , describe -- :: Connection -> String -> IO [(String, SqlType, Bool)] ) where *************** *** 57,60 **** --- 59,64 ---- foreign import ccall "mysql.h mysql_fetch_row" mysql_fetch_row :: MYSQL_RES -> IO MYSQL_ROW foreign import ccall "mysql.h mysql_fetch_lengths" mysql_fetch_lengths :: MYSQL_RES -> IO MYSQL_LENGTHS + foreign import ccall "mysql.h mysql_list_tables" mysql_list_tables :: MYSQL -> CString -> IO MYSQL_RES + foreign import ccall "mysql.h mysql_list_fields" mysql_list_fields :: MYSQL -> CString -> CString -> IO MYSQL_RES foreign import ccall "stdlib.h atoi" c_atoi :: CString -> IO Int *************** *** 175,185 **** when (res /= 0) (handleSqlError pMYSQL) ! -- | Executes the statement and returns a 'Statement' value which represents the result set ! query :: Connection -> String -> IO Statement ! query conn@(Connection pMYSQL) query = do ! res <- withCString query (mysql_query pMYSQL) ! when (res /= 0) (handleSqlError pMYSQL) currRow <- newIORef (nullPtr, nullPtr) - pRes <- mysql_use_result pMYSQL if (pRes == nullPtr) then do --- 179,186 ---- when (res /= 0) (handleSqlError pMYSQL) ! withStatement :: Connection -> (MYSQL -> IO MYSQL_RES) -> IO Statement ! withStatement conn@(Connection pMYSQL) f = do ! pRes <- f pMYSQL currRow <- newIORef (nullPtr, nullPtr) if (pRes == nullPtr) then do *************** *** 205,228 **** return ((name,sqlType,(flags .&. (#const NOT_NULL_FLAG)) == 0):defs) ! mkSqlType :: Int -> Int -> Int -> SqlType ! mkSqlType (#const FIELD_TYPE_STRING) size _ = SqlChar size ! mkSqlType (#const FIELD_TYPE_VAR_STRING) size _ = SqlVarChar size ! mkSqlType (#const FIELD_TYPE_DECIMAL) size prec = SqlNumeric size prec ! mkSqlType (#const FIELD_TYPE_SHORT) _ _ = SqlSmallInt ! mkSqlType (#const FIELD_TYPE_INT24) _ _ = SqlMedInt ! mkSqlType (#const FIELD_TYPE_LONG) _ _ = SqlInteger ! mkSqlType (#const FIELD_TYPE_FLOAT) _ _ = SqlReal ! mkSqlType (#const FIELD_TYPE_DOUBLE) _ _ = SqlDouble ! mkSqlType (#const FIELD_TYPE_TINY) _ _ = SqlTinyInt ! mkSqlType (#const FIELD_TYPE_LONGLONG) _ _ = SqlBigInt ! mkSqlType (#const FIELD_TYPE_DATE) _ _ = SqlDate ! mkSqlType (#const FIELD_TYPE_TIME) _ _ = SqlTime ! mkSqlType (#const FIELD_TYPE_TIMESTAMP) _ _ = SqlTimeStamp ! mkSqlType (#const FIELD_TYPE_DATETIME) _ _ = SqlDateTime ! mkSqlType (#const FIELD_TYPE_YEAR) _ _ = SqlYear ! mkSqlType (#const FIELD_TYPE_BLOB) _ _ = SqlBLOB ! mkSqlType (#const FIELD_TYPE_SET) _ _ = SqlSET ! mkSqlType (#const FIELD_TYPE_ENUM) _ _ = SqlENUM ! mkSqlType (#const FIELD_TYPE_NULL) _ _ = SqlUnknown -- | 'fetch' fetches the next rowset of data from the result set. --- 206,236 ---- return ((name,sqlType,(flags .&. (#const NOT_NULL_FLAG)) == 0):defs) ! mkSqlType :: Int -> Int -> Int -> SqlType ! mkSqlType (#const FIELD_TYPE_STRING) size _ = SqlChar size ! mkSqlType (#const FIELD_TYPE_VAR_STRING) size _ = SqlVarChar size ! mkSqlType (#const FIELD_TYPE_DECIMAL) size prec = SqlNumeric size prec ! mkSqlType (#const FIELD_TYPE_SHORT) _ _ = SqlSmallInt ! mkSqlType (#const FIELD_TYPE_INT24) _ _ = SqlMedInt ! mkSqlType (#const FIELD_TYPE_LONG) _ _ = SqlInteger ! mkSqlType (#const FIELD_TYPE_FLOAT) _ _ = SqlReal ! mkSqlType (#const FIELD_TYPE_DOUBLE) _ _ = SqlDouble ! mkSqlType (#const FIELD_TYPE_TINY) _ _ = SqlTinyInt ! mkSqlType (#const FIELD_TYPE_LONGLONG) _ _ = SqlBigInt ! mkSqlType (#const FIELD_TYPE_DATE) _ _ = SqlDate ! mkSqlType (#const FIELD_TYPE_TIME) _ _ = SqlTime ! mkSqlType (#const FIELD_TYPE_TIMESTAMP) _ _ = SqlTimeStamp ! mkSqlType (#const FIELD_TYPE_DATETIME) _ _ = SqlDateTime ! mkSqlType (#const FIELD_TYPE_YEAR) _ _ = SqlYear ! mkSqlType (#const FIELD_TYPE_BLOB) _ _ = SqlBLOB ! mkSqlType (#const FIELD_TYPE_SET) _ _ = SqlSET ! mkSqlType (#const FIELD_TYPE_ENUM) _ _ = SqlENUM ! mkSqlType (#const FIELD_TYPE_NULL) _ _ = SqlUnknown ! ! -- | Executes the statement and returns a 'Statement' value which represents the result set ! query :: Connection -> String -> IO Statement ! query conn@(Connection pMYSQL) query = withStatement conn $ \pMYSQL -> do ! res <- withCString query (mysql_query pMYSQL) ! when (res /= 0) (handleSqlError pMYSQL) ! mysql_use_result pMYSQL -- | 'fetch' fetches the next rowset of data from the result set. *************** *** 237,242 **** return (pRow /= nullPtr) ! -- | 'closeStatement' stops processing associated with a specific statement, closes any open cursors ! -- associated with the statement, discards pending results, and frees all resources associated with -- the statement. closeStatement :: Statement -> IO () --- 245,250 ---- return (pRow /= nullPtr) ! -- | 'closeStatement' stops processing associated with a specific statement, closes any open cursors ! -- associated with the statement, discards pending results, and frees all resources associated with -- the statement. closeStatement :: Statement -> IO () *************** *** 244,247 **** --- 252,285 ---- | pRes == nullPtr = return () | otherwise = mysql_free_result pRes + + ----------------------------------------------------------------------------------------- + -- getting table and column info + ----------------------------------------------------------------------------------------- + + -- | List all tables in the database. + tables :: Connection -- ^ Database connection + -> IO [String] -- ^ The names of all tables in the database. + tables conn = do + stmt <- withStatement conn list_tables + -- SQLTables returns: + -- Column name # Type + -- Tables_in_xx 0 VARCHAR + collectRows getTableName stmt + where + list_tables pMYSQL = mysql_list_tables pMYSQL nullPtr + + getTableName (Statement {currRow=currRow, fields=fieldDefs}) = do + (row, lengths) <- readIORef currRow + pValue <- peekElemOff row 0 + len <- fmap fromIntegral (peekElemOff lengths 0) + peekCStringLen (pValue, len) + + describe :: Connection -- ^ Database connection + -> String -- ^ Name of a database table + -> IO [(String, SqlType, Bool)] -- ^ @[(name, type, nullable)]@ + describe conn table = do + stmt <- withStatement conn list_fields + return (getFieldsTypes stmt) + where list_fields pMYSQL = withCString table (\table -> mysql_list_fields pMYSQL table nullPtr) ----------------------------------------------------------------------------------------- |