-- This is written in Haskell. {-- HBase -- general-purpose libraries for Haskell Copyright (C) 2002 Ashley Yakeley This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA --} module Foreign.MySQL.RawField where { import Foreign.MySQL.FieldType; import HBase; import Foreign; {-- #define NOT_NULL_FLAG 1 /* Field can't be NULL */ #define PRI_KEY_FLAG 2 /* Field is part of a primary key */ #define UNIQUE_KEY_FLAG 4 /* Field is part of a unique key */ #define MULTIPLE_KEY_FLAG 8 /* Field is part of a key */ #define BLOB_FLAG 16 /* Field is a blob */ #define UNSIGNED_FLAG 32 /* Field is unsigned */ #define ZEROFILL_FLAG 64 /* Field is zerofill */ #define BINARY_FLAG 128 /* The following are only sent to new clients */ #define ENUM_FLAG 256 /* field is an enum */ #define AUTO_INCREMENT_FLAG 512 /* field is a autoincrement field */ #define TIMESTAMP_FLAG 1024 /* Field is a timestamp */ #define SET_FLAG 2048 /* field is a set */ #define NUM_FLAG 32768 /* Field is num (for clients) */ #define PART_KEY_FLAG 16384 /* Intern; Part of some key */ #define GROUP_FLAG 32768 /* Intern: Group field */ #define UNIQUE_FLAG 65536 /* Intern: Used by sql_yacc */ enum enum_field_types { FIELD_TYPE_DECIMAL, FIELD_TYPE_TINY, FIELD_TYPE_SHORT, FIELD_TYPE_LONG, FIELD_TYPE_FLOAT, FIELD_TYPE_DOUBLE, FIELD_TYPE_NULL, FIELD_TYPE_TIMESTAMP, FIELD_TYPE_LONGLONG,FIELD_TYPE_INT24, FIELD_TYPE_DATE, FIELD_TYPE_TIME, FIELD_TYPE_DATETIME, FIELD_TYPE_YEAR, FIELD_TYPE_NEWDATE, FIELD_TYPE_ENUM=247, FIELD_TYPE_SET=248, FIELD_TYPE_TINY_BLOB=249, FIELD_TYPE_MEDIUM_BLOB=250, FIELD_TYPE_LONG_BLOB=251, FIELD_TYPE_BLOB=252, FIELD_TYPE_VAR_STRING=253, FIELD_TYPE_STRING=254 }; typedef struct st_mysql_field { char *name; /* Name of column */ char *table; /* Table of column if column was a field */ char *def; /* Default value (set by mysql_list_fields) */ enum enum_field_types type; /* Type of field. Se mysql_com.h for types */ unsigned int length; /* Width of column */ unsigned int max_length; /* Max width of selected set */ unsigned int flags; /* Div flags */ unsigned int decimals; /* Number of decimals in field */ } MYSQL_FIELD; --} data RawField = MkRawField { fieldName :: CStringPtr, fieldTable :: CStringPtr, fieldDefault :: CStringPtr, fieldType :: MySQLFieldType, fieldNotNull :: Bool, fieldPrimaryKey :: Bool, fieldUniqueKey :: Bool, fieldMultipleKey :: Bool, fieldZeroFill :: Bool, fieldAutoIncrement :: Bool, fieldDecimals :: Word32 }; instance Storable RawField where { sizeOf _ = (psize*3) + (4*5) where { psize = sizeOf (undefined :: (Ptr ())); }; alignment _ = alignment (undefined :: (Ptr ())); peek ptr = do { (name ::CStringPtr) <- peekByteOff ptr 0; (table ::CStringPtr) <- peekByteOff ptr psize; (def ::CStringPtr) <- peekByteOff ptr (psize*2); (ftype ::Word32) <- peekByteOff ptr (psize*3); (len ::Word32) <- peekByteOff ptr ((psize*3) + 4); -- (mlen ::Word32) <- peekByteOff ptr ((psize*3) + (4*2)); (flags ::Word32) <- peekByteOff ptr ((psize*3) + (4*3)); (decimals::Word32) <- peekByteOff ptr ((psize*3) + (4*4)); return (MkRawField name table def (convertFieldType ftype flags len) (bitSingleGet 0 flags) -- NOT_NULL_FLAG 1 can't be NULL (bitSingleGet 1 flags) -- PRI_KEY_FLAG 2 is part of a primary key (bitSingleGet 2 flags) -- UNIQUE_KEY_FLAG 4 is part of a unique key (bitSingleGet 3 flags) -- MULTIPLE_KEY_FLAG 8 is part of a key (bitSingleGet 6 flags) -- ZEROFILL_FLAG 64 is zerofill (bitSingleGet 9 flags) -- AUTO_INCREMENT_FLAG 512 is a autoincrement field decimals) } where { psize = sizeOf (undefined :: (Ptr ())); blobType :: Word32 -> MySQLFieldType; blobType flags = if (bitSingleGet 7 flags) then BlobFieldType else TextFieldType; convertFieldType :: Word32 -> Word32 -> Word32 -> MySQLFieldType; convertFieldType 0 flags len = DecimalFieldType ; -- FIELD_TYPE_DECIMAL convertFieldType 1 flags len = IntFieldType (MkIntegerType (bitSingleGet 5 flags) 8) ; -- FIELD_TYPE_TINY convertFieldType 2 flags len = IntFieldType (MkIntegerType (bitSingleGet 5 flags) 16) ; -- FIELD_TYPE_SHORT convertFieldType 3 flags len = IntFieldType (MkIntegerType (bitSingleGet 5 flags) 32) ; -- FIELD_TYPE_LONG convertFieldType 4 flags len = Float32FieldType ; -- FIELD_TYPE_FLOAT convertFieldType 5 flags len = Float64FieldType ; -- FIELD_TYPE_DOUBLE convertFieldType 6 flags len = NullFieldType ; -- FIELD_TYPE_NULL convertFieldType 7 flags len = TimeStampFieldType; -- FIELD_TYPE_TIMESTAMP convertFieldType 8 flags len = IntFieldType (MkIntegerType (bitSingleGet 5 flags) 64) ; -- FIELD_TYPE_LONGLONG convertFieldType 9 flags len = IntFieldType (MkIntegerType (bitSingleGet 5 flags) 24) ; -- FIELD_TYPE_INT24 convertFieldType 10 flags len = DateFieldType ; -- FIELD_TYPE_DATE convertFieldType 11 flags len = TimeFieldType ; -- FIELD_TYPE_TIME convertFieldType 12 flags len = DateTimeFieldType ; -- FIELD_TYPE_DATETIME convertFieldType 13 flags len = YearFieldType ; -- FIELD_TYPE_YEAR convertFieldType 14 flags len = DateFieldType ; -- FIELD_TYPE_NEWDATE convertFieldType 247 flags len = EnumFieldType ; -- FIELD_TYPE_ENUM convertFieldType 248 flags len = SetFieldType ; -- FIELD_TYPE_SET convertFieldType 249 flags len = blobType flags ; -- FIELD_TYPE_TINY_BLOB convertFieldType 250 flags len = blobType flags ; -- FIELD_TYPE_MEDIUM_BLOB convertFieldType 251 flags len = blobType flags ; -- FIELD_TYPE_LONG_BLOB convertFieldType 252 flags len = blobType flags ; -- FIELD_TYPE_BLOB convertFieldType 253 flags len = FixedStringFieldType len ; -- FIELD_TYPE_VAR_STRING convertFieldType 254 flags len = FixedStringFieldType len ; -- FIELD_TYPE_STRING convertFieldType _ flags len = undefined ; }; poke _ _ = fail "RawField poke NYI"; }; }