IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 9999 for trunk/psLib/src/db


Ignore:
Timestamp:
Nov 14, 2006, 5:49:43 PM (20 years ago)
Author:
jhoblitt
Message:

change psDBPackMySQLRow() & psDBGenerateCreateTableS() to support psS64s and convert if/elseif's to switch statements

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/psLib/src/db/psDB.c

    r9990 r9999  
    1212 *  @author Joshua Hoblitt
    1313 *
    14  *  @version $Revision: 1.111 $ $Name: not supported by cvs2svn $
    15  *  @date $Date: 2006-11-15 01:34:42 $
     14 *  @version $Revision: 1.112 $ $Name: not supported by cvs2svn $
     15 *  @date $Date: 2006-11-15 03:49:43 $
    1616 *
    1717 *  Copyright (C) 2005-2006  Joshua Hoblitt, University of Hawaii
     
    10831083        psFree(mType);
    10841084
    1085         // input data length is determined by the MYSQL_TYPE_* unless it's a string
    1086         if (item->type == PS_TYPE_S32) {
    1087             bind[i].length  = 0;
    1088             bind[i].buffer  = &item->data.S32;
    1089             bind[i].is_null = psDBIsPTypeNaN(item->type, &item->data.S32)
    1090                               ? (my_bool *)&isNull
    1091                               : NULL;
    1092         } else if (item->type == PS_TYPE_F32) {
    1093             bind[i].length  = 0;
    1094             bind[i].buffer  = &item->data.F32;
    1095             bind[i].is_null = psDBIsPTypeNaN(item->type, &item->data.F32)
    1096                               ? (my_bool *)&isNull
    1097                               : NULL;
    1098         } else if (item->type == PS_TYPE_F64) {
    1099             bind[i].length  = 0;
    1100             bind[i].buffer  = &item->data.F64;
    1101             bind[i].is_null = psDBIsPTypeNaN(item->type, &item->data.F64)
    1102                               ? (my_bool *)&isNull
    1103                               : NULL;
    1104         } else if (item->type == PS_TYPE_BOOL) {
    1105             // XXX: ASC HACK NOTE (2005/06/03): set extreme bytes to the
    1106             // boolean character value.  sizeof(psBool)==4 which triggers an
    1107             // endianess issue in the MySQL conversion (reading only 1 byte),
    1108             // on Macintosh hardware (and maybe others?)
    1109             unsigned int c  = (unsigned int)item->data.B;
    1110             item->data.S32  = (unsigned int)((c<<24) | c);
    1111             bind[i].length  = 0;
    1112             bind[i].buffer  = &item->data.B;
    1113             bind[i].is_null = psDBIsPTypeNaN(item->type, &item->data.B)
    1114                               ? (my_bool *)&isNull
    1115                               : NULL;
    1116         } else if (item->type == PS_DATA_STRING) {
    1117             // convert NaNs to NULL and set the buffer_length for strings
    1118 
    1119             if (item->data.str) {
    1120                 // will handle the case of "" as a NULL database value
    1121                 bind[i].buffer_length = (unsigned long)strlen(item->data.str);
    1122                 bind[i].length  = &bind[i].buffer_length;
    1123                 bind[i].buffer  = psStringCopy(item->data.V);
    1124                 bind[i].is_null = *item->data.str == '\0'
     1085        // input data length is determined by the MYSQL_TYPE_* unless it's a
     1086        // string
     1087        switch (item->type) {
     1088        case PS_TYPE_S32: {
     1089                bind[i].length  = 0;
     1090                bind[i].buffer  = &item->data.S32;
     1091                bind[i].is_null = psDBIsPTypeNaN(item->type, &item->data.S32)
    11251092                                  ? (my_bool *)&isNull
    11261093                                  : NULL;
    1127             } else {
    1128                 // handles the case of NULL as a NULL database value
    1129                 bind[i].buffer_length = 0;
    1130                 bind[i].length  = &bind[i].buffer_length;
    1131                 bind[i].buffer  = NULL;
    1132                 bind[i].is_null = (my_bool *)&isNull;
     1094                break;
    11331095            }
    1134         } else if (item->type == PS_DATA_TIME) {
    1135             // XXX we're abusing the comment field of the metadata item here as
    1136             // we need to have a buffer that exists outside this functions
    1137             // scope without leaking memory
    1138             // make a copy of the psTime so we don't modify user data when we
    1139             // try to do the conversion
    1140             if (item->data.str) {
    1141                 psTime *time = (psTime *)item->data.V;
    1142                 struct tm *tmTime = psTimeToTM(time);
    1143 
    1144                 // XXX it wouldn't hurt to make this conversion it's own
    1145                 // function
    1146                 // myTime is used as the 'buffer' so it doesn't have to be
    1147                 // free'd
    1148                 MYSQL_TIME *myTime = psAlloc(sizeof(MYSQL_TIME));
    1149                 myTime->year    = (unsigned int)tmTime->tm_year + 1900;
    1150                 myTime->month   = (unsigned int)tmTime->tm_mon + 1;
    1151                 myTime->day     = (unsigned int)tmTime->tm_mday;
    1152                 myTime->hour    = (unsigned int)tmTime->tm_hour;
    1153                 myTime->minute  = (unsigned int)tmTime->tm_min;
    1154                 myTime->second  = (unsigned int)tmTime->tm_sec;
    1155                 // assume for the time being that we don't have negative time
    1156                 // as ISO8601 doesn't support dates prior to 0
    1157                 myTime->neg     = (my_bool)false;
    1158                 // currently unused by mysql
    1159                 myTime->second_part  = (unsigned long)time->nsec;
    1160                 psFree(tmTime);
    1161 
    1162                 bind[i].buffer  = myTime;
    1163                 bind[i].buffer_length = sizeof(MYSQL_TIME);
    1164                 bind[i].length  = &bind[i].buffer_length;
    1165                 bind[i].is_null = NULL;
    1166             } else {
    1167                 // handles the case of NULL as a NULL database value
    1168                 bind[i].buffer_length = 0;
    1169                 bind[i].length  = &bind[i].buffer_length;
    1170                 bind[i].buffer  = NULL;
    1171                 bind[i].is_null = (my_bool *)&isNull;
     1096        case PS_TYPE_S64: {
     1097                bind[i].length  = 0;
     1098                bind[i].buffer  = &item->data.S64;
     1099                bind[i].is_null = psDBIsPTypeNaN(item->type, &item->data.S64)
     1100                                  ? (my_bool *)&isNull
     1101                                  : NULL;
     1102                break;
    11721103            }
    1173         } else {
    1174             psError(PS_ERR_BAD_PARAMETER_TYPE , true,
    1175                     "FIXME: Only type of PS_TYPE_S32 (PS_DATA_S32), "
    1176                     "PS_TYPE_F32 (PS_DATA_F32), PS_TYPE_F64 (PS_DATA_F64), "
    1177                     "PS_TYPE_BOOL (PS_DATA_BOOL), PS_DATA_STRING"
    1178                     "and PS_DATA_TIME are supported.");
    1179 
    1180             psFree(cursor);
    1181 
    1182             return false;
     1104        case PS_TYPE_F32: {
     1105                bind[i].length  = 0;
     1106                bind[i].buffer  = &item->data.F32;
     1107                bind[i].is_null = psDBIsPTypeNaN(item->type, &item->data.F32)
     1108                                  ? (my_bool *)&isNull
     1109                                  : NULL;
     1110                break;
     1111            }
     1112        case PS_TYPE_F64: {
     1113                bind[i].length  = 0;
     1114                bind[i].buffer  = &item->data.F64;
     1115                bind[i].is_null = psDBIsPTypeNaN(item->type, &item->data.F64)
     1116                                  ? (my_bool *)&isNull
     1117                                  : NULL;
     1118                break;
     1119            }
     1120        case PS_TYPE_BOOL: {
     1121                // XXX: ASC HACK NOTE (2005/06/03): set extreme bytes to the
     1122                // boolean character value.  sizeof(psBool)==4 which triggers
     1123                // an endianess issue in the MySQL conversion (reading only 1
     1124                // byte), on Macintosh hardware (and maybe others?)
     1125                unsigned int c  = (unsigned int)item->data.B;
     1126                item->data.S32  = (unsigned int)((c<<24) | c);
     1127                bind[i].length  = 0;
     1128                bind[i].buffer  = &item->data.B;
     1129                bind[i].is_null = psDBIsPTypeNaN(item->type, &item->data.B)
     1130                                  ? (my_bool *)&isNull
     1131                                  : NULL;
     1132                break;
     1133            }
     1134        case PS_DATA_STRING: {
     1135                // convert NaNs to NULL and set the buffer_length for strings
     1136
     1137                if (item->data.str) {
     1138                    // will handle the case of "" as a NULL database value
     1139                    bind[i].buffer_length = (unsigned long)strlen(item->data.str);
     1140                    bind[i].length  = &bind[i].buffer_length;
     1141                    bind[i].buffer  = psStringCopy(item->data.V);
     1142                    bind[i].is_null = *item->data.str == '\0'
     1143                                      ? (my_bool *)&isNull
     1144                                      : NULL;
     1145                } else {
     1146                    // handles the case of NULL as a NULL database value
     1147                    bind[i].buffer_length = 0;
     1148                    bind[i].length  = &bind[i].buffer_length;
     1149                    bind[i].buffer  = NULL;
     1150                    bind[i].is_null = (my_bool *)&isNull;
     1151                }
     1152                break;
     1153            }
     1154        case PS_DATA_TIME: {
     1155                // XXX we're abusing the comment field of the metadata item
     1156                // here as we need to have a buffer that exists outside this
     1157                // functions scope without leaking memory make a copy of the
     1158                // psTime so we don't modify user data when we try to do the
     1159                // conversion
     1160                if (item->data.str) {
     1161                    psTime *time = (psTime *)item->data.V;
     1162                    struct tm *tmTime = psTimeToTM(time);
     1163
     1164                    // XXX it wouldn't hurt to make this conversion it's own
     1165                    // function myTime is used as the 'buffer' so it doesn't
     1166                    // have to be free'd
     1167                    MYSQL_TIME *myTime = psAlloc(sizeof(MYSQL_TIME));
     1168                    myTime->year    = (unsigned int)tmTime->tm_year + 1900;
     1169                    myTime->month   = (unsigned int)tmTime->tm_mon + 1;
     1170                    myTime->day     = (unsigned int)tmTime->tm_mday;
     1171                    myTime->hour    = (unsigned int)tmTime->tm_hour;
     1172                    myTime->minute  = (unsigned int)tmTime->tm_min;
     1173                    myTime->second  = (unsigned int)tmTime->tm_sec;
     1174                    // assume for the time being that we don't have negative
     1175                    // time as ISO8601 doesn't support dates prior to 0
     1176                    myTime->neg     = (my_bool)false;
     1177                    // currently unused by mysql
     1178                    myTime->second_part  = (unsigned long)time->nsec;
     1179                    psFree(tmTime);
     1180
     1181                    bind[i].buffer  = myTime;
     1182                    bind[i].buffer_length = sizeof(MYSQL_TIME);
     1183                    bind[i].length  = &bind[i].buffer_length;
     1184                    bind[i].is_null = NULL;
     1185                } else {
     1186                    // handles the case of NULL as a NULL database value
     1187                    bind[i].buffer_length = 0;
     1188                    bind[i].length  = &bind[i].buffer_length;
     1189                    bind[i].buffer  = NULL;
     1190                    bind[i].is_null = (my_bool *)&isNull;
     1191                }
     1192                break;
     1193            }
     1194        default: {
     1195                psError(PS_ERR_BAD_PARAMETER_TYPE , true,
     1196                        "FIXME: Only type of "
     1197                        "PS_TYPE_S32 (PS_DATA_S32), "
     1198                        "PS_TYPE_S64 (PS_DATA_S64), "
     1199                        "PS_TYPE_F32 (PS_DATA_F32), "
     1200                        "PS_TYPE_F64 (PS_DATA_F64), "
     1201                        "PS_TYPE_BOOL (PS_DATA_BOOL), "
     1202                        "PS_DATA_STRING "
     1203                        "and PS_DATA_TIME are supported.");
     1204
     1205                psFree(cursor);
     1206
     1207                return false;
     1208                break;                  // unreachable
     1209            }
    11831210        }
    11841211    }
     
    12521279    // find column name and type
    12531280    while ((item = psListGetAndIncrement(cursor))) {
    1254         if ((item->type == PS_DATA_S32)
    1255                 || (item->type == PS_DATA_F32)
    1256                 || (item->type == PS_DATA_F64)
    1257                 || (item->type == PS_TYPE_S32)
    1258                 || (item->type == PS_TYPE_F32)
    1259                 || (item->type == PS_TYPE_F64)
    1260                 || (item->type == PS_TYPE_BOOL)
    1261                 || (item->type == PS_DATA_BOOL)
    1262                 || (item->type == PS_DATA_TIME)
    1263            ) {
    1264             // + column name + _ + column type
    1265             colType = psDBPTypeToSQL(item->type);
    1266             psStringAppend(&query, "%s %s", item->name, colType);
    1267             psFree(colType);
    1268         } else if (item->type == PS_DATA_STRING) {
    1269             // + column name + _ + varchar( + length + )
    1270             psStringAppend(&query, "%s VARCHAR(%s)", item->name, item->data.str);
    1271         } else {
    1272             psError(PS_ERR_BAD_PARAMETER_TYPE, true,
    1273                     "FIXME: Only type of PS_DATA_S32, PS_DATA_F32, PS_DATA_F64, PS_DATA_BOOL, "
    1274                     "and PS_DATA_STRING are supported, (not %d).", item->type);
    1275 
    1276             psFree(query);
    1277             psFree(cursor);
    1278 
    1279             return NULL;
    1280         }
     1281        switch (item->type) {
     1282        case PS_DATA_S32:
     1283        case PS_DATA_S64:
     1284        case PS_DATA_F32:
     1285        case PS_DATA_F64:
     1286        case PS_DATA_BOOL:
     1287        case PS_DATA_TIME: {
     1288                // + column name + _ + column type
     1289                colType = psDBPTypeToSQL(item->type);
     1290                psStringAppend(&query, "%s %s", item->name, colType);
     1291                psFree(colType);
     1292                break;
     1293            }
     1294        case PS_DATA_STRING: {
     1295                // + column name + _ + varchar( + length + )
     1296                psStringAppend(&query, "%s VARCHAR(%s)", item->name, item->data.str);
     1297                break;
     1298            }
     1299        default: {
     1300                psError(PS_ERR_BAD_PARAMETER_TYPE, true,
     1301                        "FIXME: Only type of PS_DATA_S32, PS_DATA_F32, PS_DATA_F64, PS_DATA_BOOL, "
     1302                        "and PS_DATA_STRING are supported, (not %d).", item->type);
     1303
     1304                psFree(query);
     1305                psFree(cursor);
     1306
     1307                return NULL;
     1308                break;                  // unreachable
     1309            }
     1310        }
     1311
    12811312        if (strstr(item->comment, "AUTO_INCREMENT")) {
    12821313            psStringAppend(&query, " %s", "AUTO_INCREMENT");
Note: See TracChangeset for help on using the changeset viewer.