IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 12552


Ignore:
Timestamp:
Mar 22, 2007, 12:48:34 PM (19 years ago)
Author:
jhoblitt
Message:

add foreign key support

File:
1 edited

Legend:

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

    r12431 r12552  
    1212 *  @author Joshua Hoblitt
    1313 *
    14  *  @version $Revision: 1.140 $ $Name: not supported by cvs2svn $
    15  *  @date $Date: 2007-03-14 00:39:50 $
     14 *  @version $Revision: 1.141 $ $Name: not supported by cvs2svn $
     15 *  @date $Date: 2007-03-22 22:48:34 $
    1616 *
    1717 *  Copyright (C) 2005-2006  Joshua Hoblitt, University of Hawaii
     
    3333#include <math.h>
    3434#include <inttypes.h>
     35#include <sys/types.h>
     36#include <regex.h>
    3537#include <mysql.h>
    3638#include <mysql_com.h> // enum_field_types
     
    15831585    }
    15841586
     1587    // Reset iterator to head of list
     1588    psListIteratorSet(cursor, 0);
     1589    // look for foreign keys after all other key types
     1590    while ((item = psListGetAndIncrement(cursor))) {
     1591        // don't compile a regex unless we have too
     1592        if (strcasestr(item->comment, "FKEY") == NULL) {
     1593            continue;
     1594        }
     1595        // find foreign key and references
     1596        regex_t         myregex;
     1597        regmatch_t      mymatch[3];     // match + 2 sub strings
     1598        int             errbuf_size = 1024;
     1599        char            errbuf[errbuf_size];
     1600        char            *pattern = "FKEY(.*)[:space:]*REF(.*)";
     1601
     1602        int status = regcomp(&myregex, pattern, REG_EXTENDED|REG_ICASE);
     1603        if (status != 0) {
     1604            regerror(status, &myregex, errbuf, errbuf_size);
     1605            psError(PS_ERR_UNKNOWN, true, "regcomp() failed: %s", errbuf);
     1606            psFree(query);
     1607            psFree(cursor);
     1608            return NULL;
     1609        }
     1610       
     1611        psTrace("psLib.db", 10, "trying regex pattern: %s against string: %s", pattern, item->comment);
     1612        status = regexec(&myregex, item->comment, 3, mymatch, 0);
     1613        if (status != 0) {
     1614            regerror(status, &myregex, errbuf, errbuf_size);
     1615            psError(PS_ERR_UNKNOWN, true, "regexec() failed: %s", errbuf);
     1616            psFree(query);
     1617            psFree(cursor);
     1618            return NULL;
     1619        }
     1620       
     1621        regfree(&myregex);
     1622
     1623        // sub string 1: foreign(.*)
     1624        size_t matchStart = (size_t)mymatch[1].rm_so;
     1625        size_t matchEnd   = (size_t)mymatch[1].rm_eo;
     1626        size_t matchLength = matchEnd - matchStart;
     1627
     1628        if (matchStart == -1) {
     1629            psError(PS_ERR_UNKNOWN, true, "substring 1 failed to match");
     1630            psFree(query);
     1631            psFree(cursor);
     1632            return NULL;
     1633        }
     1634
     1635        psString fkey = psStringNCopy(item->comment + matchStart, matchLength);
     1636        psTrace("psLib.db", 10, "regex $1 matched: %s", fkey);
     1637        psStringAppend(&query, ", FOREIGN KEY %s", fkey);
     1638        psFree(fkey);
     1639
     1640        // sub string 2: references(.*)
     1641        matchStart = (size_t)mymatch[2].rm_so;
     1642        matchEnd   = (size_t)mymatch[2].rm_eo;
     1643        matchLength = matchEnd - matchStart;
     1644
     1645        if (matchStart == -1) {
     1646            psError(PS_ERR_UNKNOWN, true, "substring 2 failed to match");
     1647            psFree(query);
     1648            psFree(cursor);
     1649            return NULL;
     1650        }
     1651
     1652        psString refs = psStringNCopy(item->comment + matchStart, matchLength);
     1653        psTrace("psLib.db", 10, "regex $2 matched: %s", refs);
     1654        psStringAppend(&query, " REFERENCES %s", refs);
     1655        psFree(refs);
     1656    }
     1657
    15851658    psFree(cursor);
    15861659
Note: See TracChangeset for help on using the changeset viewer.