IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 17775 for trunk/psLib


Ignore:
Timestamp:
May 22, 2008, 11:16:55 AM (18 years ago)
Author:
jhoblitt
Message:

add support for unique indexes

File:
1 edited

Legend:

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

    r16696 r17775  
    2323 * 4.1.2 or newer is required.
    2424 *
    25  * $Id: psDB.c,v 1.151 2008-02-28 02:13:03 jhoblitt Exp $
     25 * $Id: psDB.c,v 1.152 2008-05-22 21:16:55 jhoblitt Exp $
    2626 */
    2727
     
    16431643    // Reset iterator to head of list
    16441644    psListIteratorSet(cursor, 0);
     1645
    16451646    // look for INDEX values
    16461647    while ((item = psListGetAndIncrement(cursor))) {
     
    16921693        psTrace("psLib.db", 10, "regex $1 matched: %s", index);
    16931694        psStringAppend(&query, ", INDEX(%s)", index);
     1695        psFree(index);
     1696    }
     1697
     1698    // Reset iterator to head of list
     1699    psListIteratorSet(cursor, 0);
     1700    // look for UNIQUE INDEXES
     1701    while ((item = psListGetAndIncrement(cursor))) {
     1702        // don't compile a regex unless we have too
     1703        if (psStrcasestr(item->comment, "UNIQUE") == NULL) {
     1704            continue;
     1705        }
     1706
     1707        regex_t         myregex;
     1708        regmatch_t      mymatch[2];     // match + 1 sub strings
     1709        int             errbuf_size = 1024;
     1710        char            errbuf[errbuf_size];
     1711        char            *pattern = "UNIQUE[:space:]*[(]([^)]*)";
     1712
     1713        int status = regcomp(&myregex, pattern, REG_EXTENDED|REG_ICASE);
     1714        if (status != 0) {
     1715            regerror(status, &myregex, errbuf, errbuf_size);
     1716            psError(PS_ERR_UNKNOWN, true, "regcomp() failed: %s", errbuf);
     1717            psFree(query);
     1718            psFree(cursor);
     1719            return NULL;
     1720        }
     1721
     1722        psTrace("psLib.db", 10, "trying regex pattern: %s against string: %s", pattern, item->comment);
     1723        status = regexec(&myregex, item->comment, 2, mymatch, 0);
     1724        if (status != 0) {
     1725            regerror(status, &myregex, errbuf, errbuf_size);
     1726            psError(PS_ERR_UNKNOWN, true, "regexec() failed: %s", errbuf);
     1727            psFree(query);
     1728            psFree(cursor);
     1729            return NULL;
     1730        }
     1731
     1732        regfree(&myregex);
     1733
     1734        // sub string 1: unique(.*)
     1735        size_t matchStart = (size_t)mymatch[1].rm_so;
     1736        size_t matchEnd   = (size_t)mymatch[1].rm_eo;
     1737        size_t matchLength = matchEnd - matchStart;
     1738
     1739        if (matchStart == -1) {
     1740            psError(PS_ERR_UNKNOWN, true, "substring 1 failed to match");
     1741            psFree(query);
     1742            psFree(cursor);
     1743            return NULL;
     1744        }
     1745
     1746        psString index = psStringNCopy(item->comment + matchStart, matchLength);
     1747        psTrace("psLib.db", 10, "regex $1 matched: %s", index);
     1748        psStringAppend(&query, ", INDEX UNIQUE(%s)", index);
    16941749        psFree(index);
    16951750    }
Note: See TracChangeset for help on using the changeset viewer.