Ignore:
Timestamp:
Feb 11, 2000, 7:35:55 PM (26 years ago)
Author:
bird
Message:

Odin32 DB.

File:
1 edited

Legend:

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

    r2742 r2759  
    1 /* $Id: db.cpp,v 1.4 2000-02-10 22:10:40 bird Exp $ */
     1/* $Id: db.cpp,v 1.5 2000-02-11 18:35:54 bird Exp $ */
    22/*
    33 * DB - contains all database routines.
     
    2929    pres2 = mysql_store_result(pmysql);\
    3030    if (rc < 0 || pres2 == NULL ||     \
    31         mysql_num_rows(pres2) <= 0)    \
     31        mysql_num_rows(pres2) == 0)    \
    3232    {                                  \
    3333        if (pszError[1] == '\xFE')     \
     
    151151}
    152152
     153/**
     154 * Gets the refid for the give dll name.
     155 * @returns   Dll refid. -1 on error.
     156 * @param     pszDllName  Dll name.
     157 */
     158signed short _System dbGetDll(const char *pszDllName)
     159{
     160    int         rc;
     161    char        szQuery[256];
     162    MYSQL_RES * pres;
     163
     164    sprintf(&szQuery[0], "SELECT refcode FROM dll WHERE name = '%s'\n", pszDllName);
     165    rc   = mysql_query(pmysql, &szQuery[0]);
     166    pres = mysql_store_result(pmysql);
     167
     168    if (rc >= 0 && pres != NULL && mysql_num_rows(pres) == 1)
     169        rc = (int)getvalue(0, mysql_fetch_row(pres));
     170    else
     171        rc = -1;
     172    mysql_free_result(pres);
     173    return (short)rc;
     174}
     175
     176
     177/**
     178 * Count the function in a given dll.
     179 * @returns  Number of functions. -1 on error.
     180 * @param    usDll  Dll refcode.
     181 */
     182signed long     _System dbCountFunctionInDll(signed long ulDll)
     183{
     184    signed long rc;
     185    char        szQuery[256];
     186    MYSQL_RES * pres;
     187
     188    if (ulDll >= 0)
     189    {
     190        sprintf(&szQuery[0], "SELECT count(refcode) FROM function WHERE dll = %d\n", ulDll);
     191        rc   = mysql_query(pmysql, &szQuery[0]);
     192        pres = mysql_store_result(pmysql);
     193
     194        if (rc >= 0 && pres != NULL && mysql_num_rows(pres) == 1)
     195            rc = (int)getvalue(0, mysql_fetch_row(pres));
     196        else
     197            rc = -1;
     198        mysql_free_result(pres);
     199    }
     200    else
     201        rc = -1;
     202    return rc;
     203}
     204
     205
    153206
    154207/**
     
    161214signed short _System dbCheckInsertDll(const char *pszDll)
    162215{
    163     int  rc;
    164     char szQuery[256];
    165     MYSQL_RES *pres;
     216    int         rc;
     217    char        szQuery[256];
     218    MYSQL_RES * pres;
    166219
    167220    /* try find match */
     
    230283 * @returns   Success indicator. TRUE / FALSE.
    231284 * @param     usDll           Dll refcode.
    232  * @param     pszFunction     Functionname.
     285 * @param     pszFunction     Function name.
     286 * @param     pszIntFunction  Internal function name.
    233287 * @param     ulOrdinal       Ordinal value.
    234288 * @param     fIgnoreOrdinal  Do not update ordinal value.
    235289 */
    236 BOOL _System dbInsertUpdateFunction(unsigned short usDll, const char *pszFunction,
     290BOOL _System dbInsertUpdateFunction(unsigned short usDll,
     291                                    const char *pszFunction, const char *pszIntFunction,
    237292                                    unsigned long ulOrdinal, BOOL fIgnoreOrdinal)
    238293{
     
    242297    MYSQL_RES *pres;
    243298
     299    /* when no internal name, the exported name is the internal name too! */
     300    if (pszIntFunction == NULL || *pszIntFunction == '\0')
     301        pszIntFunction = pszFunction;
     302
    244303    /* try find function */
    245     sprintf(&szQuery[0], "SELECT refcode FROM function WHERE dll = %d AND name = '%s'", usDll, pszFunction);
     304    sprintf(&szQuery[0], "SELECT refcode, intname FROM function WHERE dll = %d AND name = '%s'", usDll, pszFunction);
    246305    rc = mysql_query(pmysql, &szQuery[0]);
    247306    pres = mysql_store_result(pmysql);
    248     if (rc >= 0 && pres != NULL && mysql_num_rows(pres) > 0)
     307    if (rc >= 0 && pres != NULL && mysql_num_rows(pres) != 0)
    249308    {   /* update function (function is found) */
    250309        MYSQL_ROW parow;
     
    261320        mysql_free_result(pres);
    262321
    263         if (!fIgnoreOrdinal)
     322        if (strcmp(parow[1], pszIntFunction) != 0)
     323        {
     324            sprintf(&szQuery[0], "UPDATE function SET intname = '%s' WHERE refcode = %ld",
     325                    pszIntFunction, lFunction);
     326            rc = mysql_query(pmysql, &szQuery[0]);
     327        }
     328
     329        if (rc >= 0 && !fIgnoreOrdinal)
    264330        {
    265331            sprintf(&szQuery[0], "UPDATE function SET ordinal = %ld WHERE refcode = %ld",
     
    267333            rc = mysql_query(pmysql, &szQuery[0]);
    268334        }
     335
    269336    }
    270337    else
    271338    {   /* insert */
    272         sprintf(&szQuery[0], "INSERT INTO function(dll, name, ordinal) VALUES(%d, '%s', %ld)",
    273                 usDll, pszFunction, ulOrdinal);
     339        sprintf(&szQuery[0], "INSERT INTO function(dll, name, intname, ordinal) VALUES(%d, '%s', '%s', %ld)",
     340                usDll, pszFunction, pszIntFunction, ulOrdinal);
    274341        rc = mysql_query(pmysql, &szQuery[0]);
    275342    }
     
    295362
    296363/**
    297  * Find occurences of a function, given by name.
     364 * Find occurences of a function, given by internal name.
    298365 * @returns   success indicator, TRUE / FALSE.
    299366 * @param     pszFunctionName
     
    307374    char         szQuery[256];
    308375
    309     sprintf(&szQuery[0], "SELECT refcode, dll FROM function WHERE name = '%s'", pszFunctionName);
     376    sprintf(&szQuery[0], "SELECT refcode, dll FROM function WHERE intname = '%s'",
     377            pszFunctionName, pszFunctionName);
    310378
    311379    rc = mysql_query(pmysql, &szQuery[0]);
     
    416484    char *pszQuery = &szQuery[0];
    417485    long  lCurrentState;
    418     int   i,rc;
     486    int   i,k,rc;
    419487    unsigned long ulRc = 0;
    420488    BOOL          f = FALSE;
    421489
    422     lCurrentState = dbGetFunctionState(pFnDesc->lRefCode);
    423     if (lCurrentState == -1)
    424     {
    425         strcpy(pszError, dbGetLastErrorDesc());
    426         return 1;
    427     }
    428 
    429     /* update function table first */
    430     strcpy(pszQuery, "UPDATE function SET ");
    431     pszQuery += strlen(pszQuery);
    432     if (pFnDesc->lStatus != 99 || lCurrentState == 0)
    433     {
    434         sprintf(pszQuery, "state = %ld ", pFnDesc->lStatus);
    435         f = TRUE;
    436     }
    437     pszQuery += strlen(pszQuery);
    438 
    439     if (pFnDesc->pszReturnType != NULL)
    440     {
     490    for (k = 0; k < pFnDesc->cRefCodes; k++)
     491    {
     492        /* set updated flag */
     493        sprintf(pszQuery, "UPDATE function SET updated = updated + 1 WHERE refcode = %d",
     494                pFnDesc->alRefCode[k]);
     495        rc = mysql_query(pmysql, &szQuery[0]);
     496
     497        /* get current status */
     498        lCurrentState = dbGetFunctionState(pFnDesc->alRefCode[k]);
     499        if (lCurrentState == -1)
     500        {
     501            strcpy(pszError, dbGetLastErrorDesc());
     502            return 1;
     503        }
     504
     505        /* update function table first */
     506        strcpy(pszQuery, "UPDATE function SET ");
     507        pszQuery += strlen(pszQuery);
     508        if (pFnDesc->lStatus != 99 || lCurrentState == 0)
     509        {
     510            sprintf(pszQuery, "state = %ld ", pFnDesc->lStatus);
     511            f = TRUE;
     512        }
     513        pszQuery += strlen(pszQuery);
     514
     515        if (pFnDesc->pszReturnType != NULL)
     516        {
     517            if (f)
     518            {
     519                strcat(pszQuery, ", ");
     520                pszQuery += strlen(pszQuery);
     521            }
     522            sprintf(pszQuery, "return = '%s' ", pFnDesc->pszReturnType);
     523            pszQuery += strlen(pszQuery);
     524            f = TRUE;
     525        }
     526
    441527        if (f)
    442528        {
    443             strcat(pszQuery, ", ");
    444             pszQuery += strlen(pszQuery);
    445         }
    446         sprintf(pszQuery, "return = '%s' ", pFnDesc->pszReturnType);
    447         pszQuery += strlen(pszQuery);
    448         f = TRUE;
    449     }
    450 
    451     if (f)
    452     {
    453         sprintf(pszQuery, "WHERE refcode = %ld", pFnDesc->lRefCode);
     529            sprintf(pszQuery, "WHERE refcode = %ld", pFnDesc->alRefCode[k]);
     530            rc = mysql_query(pmysql, &szQuery[0]);
     531            if (rc < 0)
     532            {
     533                sprintf(pszError, "Updating functiontable failed with error: %s - (sql=%s) ",
     534                        dbGetLastErrorDesc(), &szQuery[0]);
     535                pszError += strlen(pszError) - 1;
     536                ulRc++;
     537            }
     538        }
     539
     540        /* parameters */
     541        pszQuery = &szQuery[0];
     542        sprintf(pszQuery, "DELETE FROM parameter WHERE function = %ld", pFnDesc->alRefCode[k]);
    454543        rc = mysql_query(pmysql, &szQuery[0]);
    455544        if (rc < 0)
    456545        {
    457             sprintf(pszError, "Updating functiontable failed with error: %s - (sql=%s) ",
     546            if (*pszError == ' ')
     547                strcpy(pszError++, "\n\t");
     548            sprintf(pszError, "Deleting old parameters failed with error: %s - (sql=%s) ",
    458549                    dbGetLastErrorDesc(), &szQuery[0]);
    459550            pszError += strlen(pszError) - 1;
    460551            ulRc++;
    461552        }
    462     }
    463 
    464     /* parameters */
    465     pszQuery = &szQuery[0];
    466     sprintf(pszQuery, "DELETE FROM parameter WHERE function = %ld", pFnDesc->lRefCode);
    467     rc = mysql_query(pmysql, &szQuery[0]);
    468     if (rc < 0)
    469     {
    470         if (*pszError == ' ')
    471             strcpy(pszError++, "\n\t");
    472         sprintf(pszError, "Deleting old parameters failed with error: %s - (sql=%s) ",
    473                 dbGetLastErrorDesc(), &szQuery[0]);
    474         pszError += strlen(pszError) - 1;
    475         ulRc++;
    476     }
    477 
    478     for (i = 0; i < pFnDesc->cParams; i++)
    479     {
    480         sprintf(pszQuery, "INSERT INTO parameter(function, sequencenbr, type, name) "
    481                 "VALUES (%ld, %d, '%s', '%s')",
    482                 pFnDesc->lRefCode, i,
    483                 pFnDesc->apszParamType[i] != NULL ? pFnDesc->apszParamType[i] : "",
    484                 pFnDesc->apszParamName[i] != NULL ? pFnDesc->apszParamName[i] : ""
    485                 );
    486         rc = mysql_query(pmysql, pszQuery);
     553
     554        for (i = 0; i < pFnDesc->cParams; i++)
     555        {
     556            sprintf(pszQuery, "INSERT INTO parameter(function, sequencenbr, type, name) "
     557                    "VALUES (%ld, %d, '%s', '%s')",
     558                    pFnDesc->alRefCode[k], i,
     559                    pFnDesc->apszParamType[i] != NULL ? pFnDesc->apszParamType[i] : "",
     560                    pFnDesc->apszParamName[i] != NULL ? pFnDesc->apszParamName[i] : ""
     561                    );
     562            rc = mysql_query(pmysql, pszQuery);
     563            if (rc < 0)
     564            {
     565                if (*pszError == ' ')
     566                    strcpy(pszError++, "\n\t");
     567                sprintf(pszError, "Inserting parameter %i failed with error: %s - (sql=%s) ",
     568                        i, dbGetLastErrorDesc(), &szQuery[0]);
     569                pszError += strlen(pszError) - 1;
     570                ulRc++;
     571            }
     572        }
     573
     574        /* authors */
     575        pszQuery = &szQuery[0];
     576        sprintf(pszQuery, "DELETE FROM fnauthor WHERE function = %ld", pFnDesc->alRefCode[k]);
     577        rc = mysql_query(pmysql, &szQuery[0]);
    487578        if (rc < 0)
    488579        {
    489580            if (*pszError == ' ')
    490581                strcpy(pszError++, "\n\t");
    491             sprintf(pszError, "Inserting parameter %i failed with error: %s - (sql=%s) ",
    492                     i, dbGetLastErrorDesc(), &szQuery[0]);
     582            sprintf(pszError, "Deleting old authors failed with error: %s - (sql=%s) ",
     583                    dbGetLastErrorDesc(), &szQuery[0]);
    493584            pszError += strlen(pszError) - 1;
    494585            ulRc++;
    495586        }
    496     }
    497 
    498     /* authors */
    499     pszQuery = &szQuery[0];
    500     sprintf(pszQuery, "DELETE FROM fnauthor WHERE function = %ld", pFnDesc->lRefCode);
    501     rc = mysql_query(pmysql, &szQuery[0]);
    502     if (rc < 0)
    503     {
    504         if (*pszError == ' ')
    505             strcpy(pszError++, "\n\t");
    506         sprintf(pszError, "Deleting old authors failed with error: %s - (sql=%s) ",
    507                 dbGetLastErrorDesc(), &szQuery[0]);
    508         pszError += strlen(pszError) - 1;
    509         ulRc++;
    510     }
    511 
    512     for (i = 0; i < pFnDesc->cAuthors; i++)
    513     {
    514         if (pFnDesc->alAuthorRefCode[i] == -1)
    515             continue;
    516         sprintf(pszQuery, "INSERT INTO fnauthor(author, function) "
    517                 "VALUES (%ld, %ld)",
    518                 pFnDesc->alAuthorRefCode[i], pFnDesc->lRefCode);
    519         rc = mysql_query(pmysql, pszQuery);
    520         if (rc < 0)
    521         {
    522             if (*pszError == ' ')
    523                 strcpy(pszError++, "\n\t");
    524             sprintf(pszError, "Inserting parameter %i failed with error: %s - (sql=%s) ",
    525                     i, dbGetLastErrorDesc(), &szQuery[0]);
    526             pszError += strlen(pszError) - 1;
    527             ulRc++;
    528         }
    529     }
    530 
     587
     588        for (i = 0; i < pFnDesc->cAuthors; i++)
     589        {
     590            if (pFnDesc->alAuthorRefCode[i] == -1)
     591                continue;
     592            sprintf(pszQuery, "INSERT INTO fnauthor(author, function) "
     593                    "VALUES (%ld, %ld)",
     594                    pFnDesc->alAuthorRefCode[i], pFnDesc->alRefCode[k]);
     595            rc = mysql_query(pmysql, pszQuery);
     596            if (rc < 0)
     597            {
     598                if (*pszError == ' ')
     599                    strcpy(pszError++, "\n\t");
     600                sprintf(pszError, "Inserting parameter %i failed with error: %s - (sql=%s) ",
     601                        i, dbGetLastErrorDesc(), &szQuery[0]);
     602                pszError += strlen(pszError) - 1;
     603                ulRc++;
     604            }
     605        }
     606    } /* for */
    531607    return ulRc;
    532608}
     
    908984    rc = mysql_query(pmysql, pszQuery);
    909985    pres = mysql_store_result(pmysql);
    910     if (rc < 0 || (pres != NULL && mysql_num_rows(pres) > 0))
     986    if (rc < 0 || (pres != NULL && mysql_num_rows(pres) != 0))
    911987    {   /* some kind of error has occurred */
    912988        if (pszError[1] == '\xFE')
     
    10991175        }
    11001176    }
     1177
     1178    return fRet;
     1179}
     1180
     1181
     1182/**
     1183 * Display all functions for, the given dll, that is not updated.
     1184 * @returns   TRUE / FALSE.
     1185 * @param     lDll         Dll reference number.
     1186 * @param     dbFetchCall  Callback function which will be called once for each
     1187 *                         field for all the functions not updated.
     1188 *                         pvUser is NULL, pszValue field value, pszFieldName the field name.
     1189 */
     1190BOOL _System dbGetNotUpdatedFunction(signed long lDll, DBCALLBACKFETCH dbFetchCallBack)
     1191{
     1192    BOOL        fRet = FALSE;
     1193    void       *pres;
     1194    char        szQuery[128];
     1195
     1196    /* not updated names */
     1197    sprintf(&szQuery[0], "SELECT name, intname FROM function WHERE dll = %d AND updated = 0",
     1198            lDll);
     1199    pres = dbExecuteQuery(szQuery);
     1200    if (pres != NULL)
     1201    {
     1202        BOOL f;
     1203        do
     1204        {
     1205            f = dbFetch(pres, dbFetchCallBack, NULL);
     1206        } while (f);
     1207        dbFreeResult(pres);
     1208        fRet = TRUE;
     1209    }
     1210
     1211    /* warn about updated > 1 too */
     1212    sprintf(&szQuery[0], "SELECT updated, name, intname FROM function WHERE dll = %d AND updated > 1",
     1213            lDll);
     1214    pres = dbExecuteQuery(szQuery);
     1215    if (pres != NULL)
     1216    {
     1217        BOOL f;
     1218        do
     1219        {
     1220            f = dbFetch(pres, dbFetchCallBack, NULL);
     1221        } while (f);
     1222        dbFreeResult(pres);
     1223        fRet = TRUE;
     1224    }
     1225
     1226    sprintf(&szQuery[0], "UPDATE function SET updated = 0",
     1227            lDll);
     1228    mysql_query(pmysql, &szQuery[0]);
    11011229
    11021230    return fRet;
Note: See TracChangeset for help on using the changeset viewer.