Ignore:
Timestamp:
Jul 18, 2000, 9:16:01 AM (25 years ago)
Author:
bird
Message:

Implemented design notes and file info.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tools/database/db.cpp

    r2818 r3838  
    1 /* $Id: db.cpp,v 1.11 2000-02-18 12:42:06 bird Exp $ *
     1/* $Id: db.cpp,v 1.12 2000-07-18 07:15:59 bird Exp $ *
    22 *
    33 * DB - contains all database routines.
     
    351351
    352352
     353
     354/**
     355 * Inserts or updates (existing) file information.
     356 * @returns     Success indicator (TRUE / FALSE).
     357 * @param       usDll           Dll reference code.
     358 * @param       pszFilename     Filename.
     359 * @param       pszDescription  Pointer to file description.
     360 * @param       pszLastDateTime Date and time for last change (ISO).
     361 * @param       lLastAuthor     Author number. (-1 if not found.)
     362 * @param       pszRevision     Pointer to revision string.
     363 * @sketch
     364 * @remark
     365 */
     366BOOL            _System dbInsertUpdateFile(unsigned short usDll,
     367                                           const char *pszFilename,
     368                                           const char *pszDescription,
     369                                           const char *pszLastDateTime,
     370                                           signed long lLastAuthor,
     371                                           const char *pszRevision)
     372{
     373    int  rc;
     374    long lFile = -1;
     375    char szQuery[0x10000];
     376    MYSQL_RES *pres;
     377
     378    /* parameter assertions */
     379    assert(usDll != 0);
     380    assert(pszFilename != NULL);
     381    assert(*pszFilename != '\0');
     382
     383    /* try find file */
     384    sprintf(&szQuery[0], "SELECT refcode, name FROM file WHERE dll = %d AND name = '%s'", usDll, pszFilename);
     385    rc = mysql_query(pmysql, &szQuery[0]);
     386    pres = mysql_store_result(pmysql);
     387    if (rc >= 0 && pres != NULL && mysql_num_rows(pres) != 0)
     388    {   /* update file (file is found) */
     389        MYSQL_ROW parow;
     390        if (mysql_num_rows(pres) > 1)
     391        {
     392            fprintf(stderr, "internal database integrity error(%s): More files by the same name in the same dll. "
     393                    "usDll = %d, pszFilename = %s\n", __FUNCTION__, usDll, pszFilename);
     394            return FALSE;
     395        }
     396
     397        parow = mysql_fetch_row(pres);
     398        if (parow != NULL)
     399            lFile = getvalue(0, parow);
     400        mysql_free_result(pres);
     401
     402        if (strcmp(parow[1], pszFilename) != 0) /* case might have changed... */
     403        {
     404            sprintf(&szQuery[0], "UPDATE file SET name = '%s' WHERE refcode = %ld",
     405                    pszFilename, lFile);
     406            rc = mysql_query(pmysql, &szQuery[0]);
     407        }
     408
     409        if (rc >= 0)
     410        {
     411            if (pszDescription != NULL && pszDescription != '\0')
     412            {
     413                szQuery[0] = '\0';
     414                sqlstrcat(&szQuery[0], "UPDATE file SET description = ", pszDescription, NULL);
     415                sprintf(&szQuery[strlen(szQuery)], " WHERE refcode = %ld", lFile);
     416            }
     417            else
     418                sprintf(&szQuery[0], "UPDATE file SET description = NULL WHERE refcode = %ld",
     419                        lFile);
     420            rc = mysql_query(pmysql, &szQuery[0]);
     421        }
     422
     423        if (rc >= 0 && pszLastDateTime != NULL && *pszLastDateTime != '\0')
     424        {
     425            sprintf(&szQuery[0], "UPDATE file SET lastdatetime = '%s' WHERE refcode = %ld",
     426                    pszLastDateTime, lFile);
     427            rc = mysql_query(pmysql, &szQuery[0]);
     428        }
     429
     430        if (rc >= 0)
     431        {
     432            sprintf(&szQuery[0], "UPDATE file SET lastauthor = %ld WHERE refcode = %ld",
     433                    lLastAuthor, lFile);
     434            rc = mysql_query(pmysql, &szQuery[0]);
     435        }
     436
     437        if (rc >= 0 && pszRevision != NULL && *pszRevision != '\0')
     438        {
     439            sprintf(&szQuery[0], "UPDATE file SET revision = '%s' WHERE refcode = %ld",
     440                    pszRevision, lFile);
     441            rc = mysql_query(pmysql, &szQuery[0]);
     442        }
     443
     444    }
     445    else
     446    {   /* insert */
     447        sprintf(&szQuery[0], "INSERT INTO file(dll, name, description, lastauthor, lastdatetime, revision) VALUES(%d, '%s', %ld, ",
     448                usDll, pszFilename, lLastAuthor);
     449        if (pszDescription != NULL && *pszDescription != '\0')
     450            sqlstrcat(&szQuery[0], NULL, pszDescription);
     451        else
     452            strcat(&szQuery[0], "NULL");
     453
     454        if (pszLastDateTime != NULL && *pszLastDateTime != '\0')
     455            sqlstrcat(&szQuery[0], ", ", pszLastDateTime);
     456        else
     457            strcat(&szQuery[0], ", '1975-03-13 14:00:00'"); /* dummy */
     458
     459        if (pszRevision != NULL && *pszRevision != '\0')
     460            sqlstrcat(&szQuery[0], ", ", pszRevision, ")");
     461        else
     462            strcat(&szQuery[0], ", '')");
     463
     464        rc = mysql_query(pmysql, &szQuery[0]);
     465    }
     466
     467    return rc >= 0;
     468}
     469
     470
    353471/**
    354472 * Get a long value.
     
    399517 * Find occurences of a function, given by internal name.
    400518 * @returns   success indicator, TRUE / FALSE.
    401  * @param     pszFunctionName
    402  * @param     pFnFindBuf
    403  * @param     lDll
     519 * @param     pszFunctionName   Pointer to a function name string. (input)
     520 * @param     pFnFindBuf        Pointer to a find buffer. (output)
     521 * @param     lDll              Dll refcode (optional). If given the search is limited to
     522 *                              the given dll and aliasing functions is updated (slow!).
    404523 * @sketch    1) Get functions for this dll(if given).
    405524 *            2) Get functions which aliases the functions found in (1).
     
    420539     */
    421540    if (lDll < 0)
    422         sprintf(&szQuery[0], "SELECT refcode, dll, aliasfn, name FROM function WHERE intname = '%s'",
     541        sprintf(&szQuery[0], "SELECT refcode, dll, aliasfn, file, name FROM function WHERE intname = '%s'",
    423542                pszFunctionName);
    424543    else
    425         sprintf(&szQuery[0], "SELECT refcode, dll, aliasfn, name FROM function "
     544        sprintf(&szQuery[0], "SELECT refcode, dll, aliasfn, file, name FROM function "
    426545                "WHERE intname = '%s' AND dll = %ld",
    427546                pszFunctionName, lDll);
     
    441560                pFnFindBuf->alDllRefCode[pFnFindBuf->cFns] = atol(row[1]);
    442561                pFnFindBuf->alAliasFn[pFnFindBuf->cFns] = atol(row[2]);
    443                 strcpy(szFnName[pFnFindBuf->cFns], row[3]);
     562                pFnFindBuf->alFileRefCode[pFnFindBuf->cFns] = atol(row[3]);
     563                strcpy(szFnName[pFnFindBuf->cFns], row[4]);
    444564
    445565                /* next */
     
    457577                 */
    458578                cFnsThisDll = (int)pFnFindBuf->cFns;
    459                 strcpy(&szQuery[0], "SELECT refcode, dll, aliasfn, name FROM function WHERE aliasfn IN (");
     579                strcpy(&szQuery[0], "SELECT refcode, dll, aliasfn, file, name FROM function WHERE aliasfn IN (");
    460580                for (i = 0; i < cFnsThisDll; i++)
    461581                {
     
    476596                            pFnFindBuf->alDllRefCode[pFnFindBuf->cFns] = atol(row[1]);
    477597                            pFnFindBuf->alAliasFn[pFnFindBuf->cFns] = atol(row[2]);
    478                             strcpy(szFnName[pFnFindBuf->cFns], row[3]);
     598                            pFnFindBuf->alFileRefCode[pFnFindBuf->cFns] = atol(row[3]);
     599                            strcpy(szFnName[pFnFindBuf->cFns], row[4]);
    479600
    480601                            /* next */
     
    487608                         */
    488609                        cFnsAliasesAndThisDll = (int)pFnFindBuf->cFns;
    489                         sprintf(&szQuery[0], "SELECT refcode, dll, aliasfn FROM function "
     610                        sprintf(&szQuery[0], "SELECT refcode, dll, aliasfn, file FROM function "
    490611                                             "WHERE aliasfn = (-1) AND dll <> %ld AND (intname = '%s'",
    491612                                lDll, pszFunctionName);
     
    508629                                    else
    509630                                        pFnFindBuf->alAliasFn[pFnFindBuf->cFns] = ALIAS_NULL;
     631                                    pFnFindBuf->alFileRefCode[pFnFindBuf->cFns] = atol(row[3]);
    510632
    511633                                    /* next */
     
    518640                                 * 4) Get new aliases by name
    519641                                 */
    520                                 sprintf(&szQuery[0], "SELECT refcode, dll, aliasfn FROM function "
     642                                sprintf(&szQuery[0], "SELECT refcode, dll, aliasfn, file FROM function "
    521643                                                     "WHERE aliasfn = (-1) AND dll <> %ld AND (name = '%s'",
    522644                                        lDll, pszFunctionName);
     
    539661                                            else
    540662                                                pFnFindBuf->alAliasFn[pFnFindBuf->cFns] = ALIAS_NULL;
     663                                            pFnFindBuf->alFileRefCode[pFnFindBuf->cFns] = atol(row[3]);
    541664
    542665                                            /* next */
     
    565688                                             * 6) Update all functions from (3) and (4) to alias the first function from 1.
    566689                                             */
    567                                             sprintf(&szQuery[0], "UPDATE function SET aliasfn = (%ld) "
     690                                            sprintf(&szQuery[0], "UPDATE function SET aliasfn = (%ld), file = (%ld) "
    568691                                                                 "WHERE aliasfn = (-1) AND refcode IN (",
    569                                                     pFnFindBuf->alRefCode[0]);
     692                                                    pFnFindBuf->alRefCode[0], pFnFindBuf->alFileRefCode[0]);
    570693                                            for (i = cFnsAliasesAndThisDll; i < pFnFindBuf->cFns; i++)
    571694                                            {
     
    589712
    590713    return rc >= 0;
     714}
     715
     716
     717/**
     718 * Finds the refcode for a file (if it exists).
     719 * @returns     File 'refcode'.
     720 *              -1 on error or not found.
     721 * @param       lDll            Refcode of the dll which this file belongs to.
     722 * @param       pszFilename     The filename to search for.
     723 */
     724signed long     _System dbFindFile(signed long lDll, const char *pszFilename)
     725{
     726    char        szQuery[256];
     727    MYSQL_RES * pres;
     728    signed long lRefCode = -1;
     729
     730    assert(lDll >= 0);
     731    assert(pszFilename != NULL);
     732    assert(*pszFilename != '\0');
     733
     734    sprintf(&szQuery[0], "SELECT refcode FROM file WHERE dll = %ld AND name = '%s'",
     735            lDll, pszFilename);
     736    if (mysql_query(pmysql, &szQuery[0]) >= 0)
     737    {
     738        pres = mysql_store_result(pmysql);
     739        if (pres != NULL)
     740        {
     741            MYSQL_ROW parow = mysql_fetch_row(pres);
     742            if (parow != NULL)
     743                lRefCode = getvalue(0, parow);
     744            mysql_free_result(pres);
     745        }
     746    }
     747
     748    return lRefCode;
    591749}
    592750
     
    9951153
    9961154/**
     1155 * Removes all the existing design notes in the specified file.
     1156 * @returns     Success indicator.
     1157 * @param       lFile       File refcode of the file to remove all design notes for.
     1158 * @sketch
     1159 * @status
     1160 * @author    knut st. osmundsen (knut.stange.osmundsen@pmsc.no)
     1161 * @remark
     1162 */
     1163BOOL            _System dbRemoveDesignNotes(signed long lFile)
     1164{
     1165    char        szQuery[80];
     1166
     1167    assert(lFile >= 0);
     1168    sprintf(&szQuery[0], "DELETE FROM designnote WHERE file = %ld", lFile);
     1169    return mysql_query(pmysql, &szQuery[0]) >= 0;
     1170}
     1171
     1172
     1173/**
     1174 * Adds a design note.
     1175 * @returns     Success indicator.
     1176 * @param       lDll            Dll refcode.
     1177 * @param       lFile           File refcode.
     1178 * @param       pszTitle        Design note title.
     1179 * @param       pszText         Design note text.
     1180 * @param       lSeqNbr         Sequence number (in dll). If 0 the use next available number.
     1181 * @param       lSeqNbrFile     Sequence number in file.
     1182 */
     1183BOOL            _System dbAddDesignNote(signed long lDll,
     1184                                        signed long lFile,
     1185                                        const char *pszTitle,
     1186                                        const char *pszText,
     1187                                        signed long lSeqNbr,
     1188                                        signed long lSeqNbrFile)
     1189{
     1190    char        szQuery[0x10200];
     1191    MYSQL_RES * pres;
     1192
     1193
     1194    assert(lDll >= 0 && lFile >= 0);
     1195    assert(lSeqNbrFile >= 0);
     1196
     1197    /*
     1198     * If no lSqlNbr the make one.
     1199     */
     1200    if (lSeqNbr == 0)
     1201    {
     1202        sprintf(&szQuery[0], "SELECT MAX(lSeqNbr) + 1 FROM designnote WHERE dll = %ld'", lDll);
     1203        if (mysql_query(pmysql, &szQuery[0]) >= 0)
     1204        {
     1205            pres = mysql_store_result(pmysql);
     1206            if (pres != NULL)
     1207            {
     1208                MYSQL_ROW parow = mysql_fetch_row(pres);
     1209                if (parow != NULL)
     1210                    lSeqNbr = getvalue(0, parow);
     1211                else
     1212                    lSeqNbr = 1;
     1213                mysql_free_result(pres);
     1214            }
     1215            else
     1216                return FALSE;
     1217        }
     1218        else
     1219            return FALSE;
     1220    }
     1221
     1222    /*
     1223     * Create update query.
     1224     */
     1225    sprintf(&szQuery[0], "INSERT INTO designnote(dll, file, seqnbrfile, seqnbr, title, note) "
     1226                         "VALUES (%ld, %ld, %ld, %ld, ",
     1227            lDll, lFile, lSeqNbrFile, lSeqNbr);
     1228    if (pszTitle != NULL && *pszTitle != '\0')
     1229        sqlstrcat(&szQuery[0], NULL, pszTitle);
     1230    else
     1231        strcat(&szQuery[0], "NULL");
     1232    sqlstrcat(&szQuery[0], ", ", pszText == NULL ? "" : pszText, ")");
     1233
     1234    return mysql_query(pmysql, &szQuery[0]) >= 0;
     1235}
     1236
     1237
     1238
     1239/**
    9971240 * Updates the history tables.
    9981241 * @returns   Number of signals/errors.
     
    11091352
    11101353    /* foreign keys in function table */
    1111     strcpy(pszQuery, "SELECT refcode, dll, state, apigroup FROM function");
     1354    strcpy(pszQuery, "SELECT refcode, dll, state, apigroup, file FROM function");
    11121355    rc = mysql_query(pmysql, pszQuery);
    11131356    if (rc >= 0)
     
    11191362            {
    11201363                /* check dll */
    1121                 sprintf(pszQuery, "SELECT name FROM dll WHERE refcode = %s", row1[1]);
     1364                sprintf(pszQuery, "SELECT refcode FROM dll WHERE refcode = %s", row1[1]);
    11221365                rc = mysql_query(pmysql, pszQuery);
    11231366                CheckFKError("function/dll", "Foreign key 'dll' not found in the dll table");
    11241367
    11251368                /* check state */
    1126                 sprintf(pszQuery, "SELECT name FROM state WHERE refcode = %s", row1[2]);
     1369                sprintf(pszQuery, "SELECT refcode FROM state WHERE refcode = %s", row1[2]);
    11271370                rc = mysql_query(pmysql, pszQuery);
    11281371                CheckFKError("function/state", "Foreign key 'state' not found in the state table");
     
    11311374                if (row1[3] != NULL)
    11321375                {
    1133                     sprintf(pszQuery, "SELECT name FROM apigroup WHERE refcode = %s", row1[3]);
     1376                    sprintf(pszQuery, "SELECT refcode FROM apigroup WHERE refcode = %s", row1[3]);
    11341377                    rc = mysql_query(pmysql, pszQuery);
    11351378                    CheckFKError("function/state", "Foreign key 'state' not found in the state table");
    11361379                }
     1380
     1381                /* check file */
     1382                if (atoi(row1[4]) >= 0)
     1383                {
     1384                    sprintf(pszQuery, "SELECT refcode FROM file WHERE refcode = %s", row1[4]);
     1385                    rc = mysql_query(pmysql, pszQuery);
     1386                    CheckFKError("function/file", "Foreign key 'file' not found in the file table");
     1387                }
    11371388            }
    11381389            mysql_free_result(pres1);
     
    11421393        ulRc += logDbError(pszError, pszQuery);
    11431394
    1144     /* foreign keys in apigroup */
    1145     strcpy(pszQuery, "SELECT refcode, dll FROM apigroup");
     1395    /* foreign keys in file */
     1396    strcpy(pszQuery, "SELECT refcode, dll FROM file");
    11461397    rc = mysql_query(pmysql, pszQuery);
    11471398    if (rc >= 0)
     
    11561407                rc = mysql_query(pmysql, pszQuery);
    11571408                CheckFKError("apigroup/dll", "Foreign key 'dll' not found in the dll table");
     1409            }
     1410            mysql_free_result(pres1);
     1411        }
     1412    }
     1413    else
     1414        ulRc += logDbError(pszError, pszQuery);
     1415
     1416    /* foreign keys in apigroup */
     1417    strcpy(pszQuery, "SELECT refcode, dll FROM apigroup");
     1418    rc = mysql_query(pmysql, pszQuery);
     1419    if (rc >= 0)
     1420    {
     1421        pres1 = mysql_store_result(pmysql);
     1422        if (pres1 != NULL)
     1423        {
     1424            while ((row1 = mysql_fetch_row(pres1)) != NULL)
     1425            {
     1426                /* check dll */
     1427                sprintf(pszQuery, "SELECT refcode FROM dll WHERE refcode = %s", row1[1]);
     1428                rc = mysql_query(pmysql, pszQuery);
     1429                CheckFKError("file/dll", "Foreign key 'dll' not found in the dll table");
    11581430            }
    11591431            mysql_free_result(pres1);
     
    16461918
    16471919/**
    1648  *
     1920 * Appends a set of strings to a query. The main string (pszStr) is enclosed in "'"s.
    16491921 * @returns   Pointer to end of the string.
    16501922 * @param     pszQuery   Outputbuffer
     
    16771949    {
    16781950        if (ch == '\'')
     1951        {
    16791952            *pszQuery++ = '\\';
    1680         *pszQuery++ = ch;
     1953            *pszQuery++ = '\'';
     1954        }
     1955        else if (ch == '\n')
     1956        {
     1957            #if 0
     1958            *pszQuery++ = '\\';
     1959            *pszQuery++ = 'n';
     1960            #else
     1961            *pszQuery++ = '<';
     1962            *pszQuery++ = 'B';
     1963            *pszQuery++ = 'R';
     1964            *pszQuery++ = '>';
     1965            #endif
     1966        }
     1967        else if (ch == '\r')
     1968        {
     1969            #if 0
     1970            *pszQuery++ = '\\';
     1971            *pszQuery++ = 'r';
     1972            #else
     1973            *pszQuery++ = '<';
     1974            *pszQuery++ = 'B';
     1975            *pszQuery++ = 'R';
     1976            *pszQuery++ = '>';
     1977            #endif
     1978        }
     1979        else
     1980            *pszQuery++ = ch;
    16811981    }
    16821982    *pszQuery++ = '\'';
Note: See TracChangeset for help on using the changeset viewer.