Ignore:
Timestamp:
Feb 18, 2000, 1:42:08 PM (26 years ago)
Author:
bird
Message:

Read more of the function header into the database.
Stateupd is changed to do this and the database is expanded with new fields.
The sample is partly updated.

File:
1 edited

Legend:

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

    r2776 r2818  
    1 /* $Id: db.cpp,v 1.10 2000-02-14 17:18:31 bird Exp $ *
     1/* $Id: db.cpp,v 1.11 2000-02-18 12:42:06 bird Exp $ *
    22 *
    33 * DB - contains all database routines.
     
    8080static unsigned long CheckAuthorError(char * &pszError, const char *pszFieldName, const char *pszFieldValue, const char *pszQuery);
    8181static unsigned long logDbError(char * &pszError, const char *pszQuery);
     82static char *sqlstrcat(char *pszQuery, const char *pszBefore, const char *pszStr, const char *pszAfter = NULL);
     83
    8284#ifndef DLL
    8385    extern "C" void      dbHandler(int sig);
     
    297299    int  rc;
    298300    long lFunction = -1;
    299     char szQuery[256];
     301    char szQuery[512];
    300302    MYSQL_RES *pres;
    301303
     
    596598 *                       This doesn't have to be the name. Initials, alias and email
    597599 *                       is also searched.
    598  */
    599 signed long _System dbFindAuthor(const char *pszAuthor)
     600 * @param     pszEmail   Email address. Might be NULL!
     601 */
     602signed long _System dbFindAuthor(const char *pszAuthor, const char *pszEmail)
    600603{
    601604    signed long refcode = -1;
    602605    MYSQL_RES *pres;
    603     char szQuery[256];
    604 
     606    char szQuery[512];
     607
     608    /*
     609     * parameter validations
     610     */
     611    if (pszAuthor == NULL || strlen(pszAuthor) > 64)
     612        return -1;
     613    if (pszEmail != NULL && strlen(pszEmail) > 64)
     614    {
     615        fprintf(stderr, "email too long!");
     616        return -1;
     617    }
     618
     619    /*
     620     * Query
     621     */
    605622    sprintf(&szQuery[0],
    606623            "SELECT refcode FROM author "
     
    610627            "      email    = '%s'",
    611628            pszAuthor, pszAuthor, pszAuthor, pszAuthor);
     629
     630    if (pszEmail != NULL)
     631        sprintf(&szQuery[strlen(&szQuery[0])], " OR email = '%s'", pszEmail);
     632
    612633    if (mysql_query(pmysql, &szQuery[0]) >= 0)
    613634    {
     
    702723    MYSQL_RES *     pres;
    703724    MYSQL_ROW       row;
    704     char            szQuery[512];
    705     char *          pszQuery = &szQuery[0];
     725    char *          pszQuery2 = (char*)malloc(65500);
     726    char *          pszQuery = pszQuery2;
    706727    long            lCurrentState;
    707728    int             i,k,rc;
    708729    unsigned long   ulRc = 0;
    709730
     731    /* check if malloc have failed allocating memory for us. */
     732    if (pszQuery2 == NULL)
     733    {
     734        strcpy(pszError, "internal dbUpdateFunction error - malloc failed!\n");
     735        return 1;
     736    }
     737
     738
     739    /*
     740     * Loop thru all functions in the array of refocodes.
     741     */
    710742    for (k = 0; k < pFnDesc->cRefCodes; k++)
    711743    {
     
    717749        sprintf(pszQuery, "UPDATE function SET updated = updated + 1 WHERE refcode = %ld",
    718750                pFnDesc->alRefCode[k]);
    719         rc = mysql_queryu1(pmysql, &szQuery[0]);
     751        rc = mysql_queryu1(pmysql, pszQuery2);
    720752
    721753
     
    724756         */
    725757        lCurrentState = dbGetFunctionState(pFnDesc->alRefCode[k]);
    726         if (lCurrentState == -1)
     758        if (lCurrentState == -1 && dbGetLastErrorDesc() != NULL && strlen(dbGetLastErrorDesc()) != 0)
    727759        {
    728760            strcpy(pszError, dbGetLastErrorDesc());
     761            free(pszQuery2);
    729762            return 1;
    730763        }
     
    736769        strcpy(pszQuery, "UPDATE function SET ");
    737770        pszQuery += strlen(pszQuery);
    738         if (pFnDesc->lStatus != 99 || lCurrentState == 0)
    739         {
    740             sprintf(pszQuery, "state = %ld ", pFnDesc->lStatus);
     771
     772        /* Status */
     773        if (lCurrentState != pFnDesc->lStatus
     774            && pFnDesc->lStatus != 0
     775            && (lCurrentState == 0 || pFnDesc->lStatus != 99))
     776        {
     777            sprintf(pszQuery, "state = %ld", pFnDesc->lStatus);
    741778            f = TRUE;
    742779        }
    743780        pszQuery += strlen(pszQuery);
    744781
     782        /* return type */
    745783        if (pFnDesc->pszReturnType != NULL)
    746784        {
    747785            if (f)  strcat(pszQuery, ", ");
    748             sprintf(pszQuery + strlen(pszQuery),  "return = '%s' ", pFnDesc->pszReturnType);
    749             pszQuery += strlen(pszQuery);
     786            pszQuery = sqlstrcat(pszQuery, "return = ",  pFnDesc->pszReturnType);
    750787            f = TRUE;
    751788        }
    752789
     790        /* Description */
     791        if (pFnDesc->pszDescription != NULL)
     792        {
     793            if (f)  strcat(pszQuery, ", ");
     794            pszQuery = sqlstrcat(pszQuery, "description  = ", pFnDesc->pszDescription);
     795            f = TRUE;
     796        }
     797
     798        /* Remark */
     799        if (pFnDesc->pszRemark != NULL)
     800        {
     801            if (f)  strcat(pszQuery, ", ");
     802            pszQuery = sqlstrcat(pszQuery,  "remark = ", pFnDesc->pszRemark);
     803            f = TRUE;
     804        }
     805
     806        /* Description */
     807        if (pFnDesc->pszReturnDesc != NULL)
     808        {
     809            if (f)  strcat(pszQuery, ", ");
     810            pszQuery = sqlstrcat(pszQuery,  "returndesc = ", pFnDesc->pszReturnDesc);
     811            f = TRUE;
     812        }
     813
     814        /* Sketch */
     815        if (pFnDesc->pszSketch != NULL)
     816        {
     817            if (f)  strcat(pszQuery, ", ");
     818            pszQuery = sqlstrcat(pszQuery,  "sketch = ", pFnDesc->pszSketch);
     819            f = TRUE;
     820        }
     821
     822        /* Equiv */
     823        if (pFnDesc->pszEquiv != NULL)
     824        {
     825            if (f)  strcat(pszQuery, ", ");
     826            pszQuery = sqlstrcat(pszQuery,  "equiv = ", pFnDesc->pszEquiv);
     827            f = TRUE;
     828        }
     829
     830        /* Equiv */
     831        if (pFnDesc->pszTime != NULL)
     832        {
     833            if (f)  strcat(pszQuery, ", ");
     834            pszQuery = sqlstrcat(pszQuery,  "time = ", pFnDesc->pszTime);
     835            f = TRUE;
     836        }
     837
     838        /* Execute update query? */
    753839        if (f)
    754840        {
    755             sprintf(pszQuery + strlen(pszQuery), "WHERE refcode = %ld", pFnDesc->alRefCode[k]);
    756             rc = mysql_queryu2(pmysql, &szQuery[0]);
     841            sprintf(pszQuery + strlen(pszQuery), " WHERE refcode = %ld", pFnDesc->alRefCode[k]);
     842            rc = mysql_queryu2(pmysql, pszQuery2);
    757843            if (rc < 0)
    758844            {
    759845                sprintf(pszError, "Updating functiontable failed with error: %s - (sql=%s) ",
    760                         dbGetLastErrorDesc(), &szQuery[0]);
     846                        dbGetLastErrorDesc(), pszQuery2);
    761847                pszError += strlen(pszError) - 1;
    762848                ulRc++;
     
    765851
    766852
     853
    767854        /*
    768855         * Parameters
    769856         */
    770         pszQuery = &szQuery[0];
     857        pszQuery = pszQuery2;
    771858        sprintf(pszQuery, "SELECT count(*) FROM parameter WHERE function = %ld", pFnDesc->alRefCode[k]);
    772859        rc = mysql_queryu3(pmysql, pszQuery);
     
    782869                    for (i = 0; i < pFnDesc->cParams; i++)
    783870                    {
    784                         sprintf(pszQuery, "UPDATE parameter SET type = '%s', name = '%s' "
    785                                 "WHERE function = (%ld) AND sequencenbr = (%ld)",
     871                        sprintf(pszQuery, "UPDATE parameter SET type = '%s', name = '%s'",
    786872                                pFnDesc->apszParamType[i] != NULL ? pFnDesc->apszParamType[i] : "",
    787                                 pFnDesc->apszParamName[i] != NULL ? pFnDesc->apszParamName[i] : "",
    788                                 pFnDesc->alRefCode[k], i
    789                                 );
     873                                pFnDesc->apszParamName[i] != NULL ? pFnDesc->apszParamName[i] : "");
     874                        if (pFnDesc->apszParamDesc[i] != NULL)
     875                            sqlstrcat(pszQuery, ", description = ", pFnDesc->apszParamDesc[i]);
     876                        sprintf(pszQuery + strlen(pszQuery), " WHERE function = (%ld) AND sequencenbr = (%ld)",
     877                                pFnDesc->alRefCode[k], i);
    790878                        rc = mysql_queryu4(pmysql, pszQuery);
    791879                        if (rc < 0)
     
    820908                    for (i = 0; i < pFnDesc->cParams; i++)
    821909                    {
    822                         sprintf(pszQuery, "INSERT INTO parameter(function, sequencenbr, type, name) "
    823                                 "VALUES (%ld, %d, '%s', '%s')",
     910                        sprintf(pszQuery, "INSERT INTO parameter(function, sequencenbr, type, name, description) "
     911                                "VALUES (%ld, %d, '%s', '%s'",
    824912                                pFnDesc->alRefCode[k], i,
    825913                                pFnDesc->apszParamType[i] != NULL ? pFnDesc->apszParamType[i] : "",
    826914                                pFnDesc->apszParamName[i] != NULL ? pFnDesc->apszParamName[i] : ""
    827915                                );
    828                         rc = mysql_queryu6(pmysql, pszQuery);
     916                        if (pFnDesc->apszParamDesc[i] != NULL)
     917                            sqlstrcat(pszQuery, ", ", pFnDesc->apszParamDesc[i]);
     918                        else
     919                            strcat(pszQuery, ", NULL)");
     920
     921                        rc = mysql_queryu6(pmysql, pszQuery2);
    829922                        if (rc < 0)
    830923                        {
     
    896989
    897990    lDll = lDll;
     991    free(pszQuery2);
    898992    return ulRc;
    899993}
     
    15481642    mysql_free_result(pres);
    15491643    return (signed long)rc;
     1644}
     1645
     1646
     1647/**
     1648 *
     1649 * @returns   Pointer to end of the string.
     1650 * @param     pszQuery   Outputbuffer
     1651 * @param     pszBefore  Text before string, might be NULL.
     1652 * @param     pszStr     String (NOT NULL)
     1653 * @param     pszAfter   Text after, might be NULL.
     1654 * @status    completely implemented
     1655 * @author    knut st. osmundsen (knut.stange.osmundsen@pmsc.no)
     1656 */
     1657static char *sqlstrcat(char *pszQuery, const char *pszBefore, const char *pszStr, const char *pszAfter)
     1658{
     1659    register char ch;
     1660
     1661    pszQuery += strlen(pszQuery);
     1662
     1663    /*
     1664     * String before
     1665     */
     1666    if (pszBefore != NULL)
     1667    {
     1668        strcpy(pszQuery, pszBefore);
     1669        pszQuery += strlen(pszQuery);
     1670    }
     1671
     1672    /*
     1673     * THE String
     1674     */
     1675    *pszQuery++ = '\'';
     1676    while ((ch = *pszStr++) != '\0')
     1677    {
     1678        if (ch == '\'')
     1679            *pszQuery++ = '\\';
     1680        *pszQuery++ = ch;
     1681    }
     1682    *pszQuery++ = '\'';
     1683
     1684    /*
     1685     * String after
     1686     */
     1687    if (pszAfter != NULL)
     1688    {
     1689        strcpy(pszQuery, pszAfter);
     1690        pszQuery += strlen(pszQuery);
     1691    }
     1692    else
     1693        *pszQuery = '\0';
     1694
     1695
     1696    return pszQuery;
    15501697}
    15511698
Note: See TracChangeset for help on using the changeset viewer.