1d8785d66ed85fd0bff3ed301a7cd5a7692265c4 braney Wed Apr 8 13:05:43 2015 -0700 allow for different names for the sessionDb and userDb tables diff --git src/hg/lib/cart.c src/hg/lib/cart.c index 7fece59..ad32e02 100644 --- src/hg/lib/cart.c +++ src/hg/lib/cart.c @@ -73,38 +73,38 @@ void cartTrace(struct cart *cart, char *when, struct sqlConnection *conn) /* Write some properties of the cart to stderr for debugging. */ { if (cfgOption("cart.trace") == NULL) return; struct cartDb *u = cart->userInfo, *s = cart->sessionInfo; char *pix = hashFindVal(cart->hash, "pix"); char *textSize = hashFindVal(cart->hash, "textSize"); char *trackControls = hashFindVal(cart->hash, "trackControlsOnMain"); int uLen, sLen; if (conn != NULL) { /* Since the content string is chopped, query for the actual length. */ struct dyString *query = dyStringNew(1024); - sqlDyStringPrintf(query, "select length(contents) from userDb" - " where id = %d", u->id); + sqlDyStringPrintf(query, "select length(contents) from %s" + " where id = %d", userDbTable(), u->id); if (cartDbUseSessionKey()) sqlDyStringPrintf(query, " and sessionKey='%s'", u->sessionKey); uLen = sqlQuickNum(conn, query->string); dyStringClear(query); - sqlDyStringPrintf(query, "select length(contents) from sessionDb" - " where id = %d", s->id); + sqlDyStringPrintf(query, "select length(contents) from %s" + " where id = %d", sessionDbTable(),s->id); if (cartDbUseSessionKey()) sqlDyStringPrintf(query, " and sessionKey='%s'", s->sessionKey); sLen = sqlQuickNum(conn, query->string); dyStringFree(&query); } else { uLen = strlen(u->contents); sLen = strlen(s->contents); } if (pix == NULL) pix = "-"; if (textSize == NULL) textSize = "-"; if (trackControls == NULL) @@ -119,40 +119,40 @@ cartDbSecureId(userIdKey, sizeof userIdKey, u); if (cart->userId && !sameString(userIdKey, cart->userId)) fprintf(stderr, "ASH: bad userId %s --> %d_%s! pid=%ld\n", cart->userId, u->id, u->sessionKey, (long)getpid()); char sessionIdKey[256]; cartDbSecureId(sessionIdKey, sizeof sessionIdKey, s); if (cart->sessionId && !sameString(sessionIdKey, cart->sessionId)) fprintf(stderr, "ASH: bad sessionId %s --> %d_%s! pid=%ld\n", cart->sessionId, s->id, s->sessionKey, (long)getpid()); } boolean cartTablesOk(struct sqlConnection *conn) /* Return TRUE if cart tables are accessible (otherwise, the connection * doesn't do us any good). */ { -if (!sqlTableExists(conn, "userDb")) +if (!sqlTableExists(conn, userDbTable())) { - fprintf(stderr, "ASH: cartTablesOk failed on %s.userDb! pid=%ld\n", - sqlGetDatabase(conn), (long)getpid()); + fprintf(stderr, "ASH: cartTablesOk failed on %s.%s! pid=%ld\n", + sqlGetDatabase(conn), userDbTable(), (long)getpid()); return FALSE; } -if (!sqlTableExists(conn, "sessionDb")) +if (!sqlTableExists(conn, sessionDbTable())) { - fprintf(stderr, "ASH: cartTablesOk failed on %s.sessionDb! pid=%ld\n", - sqlGetDatabase(conn), (long)getpid()); + fprintf(stderr, "ASH: cartTablesOk failed on %s.%s! pid=%ld\n", + sqlGetDatabase(conn), sessionDbTable(), (long)getpid()); return FALSE; } return TRUE; } static void cartParseOverHash(struct cart *cart, char *contents) /* Parse cgi-style contents into a hash table. This will *not* * replace existing members of hash that have same name, so we can * support multi-select form inputs (same var name can have multiple * values which will be in separate hashEl's). */ { struct hash *hash = cart->hash; char *namePt, *dataPt, *nextNamePt; namePt = contents; while (namePt != NULL && namePt[0] != 0) @@ -657,32 +657,32 @@ char **exclude, struct hash *oldVars) /* Load up cart from user & session id's. Exclude is a null-terminated list of * strings to not include */ { cgiApoptosisSetup(); struct cart *cart; struct sqlConnection *conn = cartDefaultConnector(); char *ex; boolean userIdFound = FALSE, sessionIdFound = FALSE; AllocVar(cart); cart->hash = newHash(12); cart->exclude = newHash(7); cart->userId = userId; cart->sessionId = sessionId; -cart->userInfo = loadDb(conn, "userDb", userId, &userIdFound); -cart->sessionInfo = loadDb(conn, "sessionDb", sessionId, &sessionIdFound); +cart->userInfo = loadDb(conn, userDbTable(), userId, &userIdFound); +cart->sessionInfo = loadDb(conn, sessionDbTable(), sessionId, &sessionIdFound); if (sessionIdFound) cartParseOverHash(cart, cart->sessionInfo->contents); else if (userIdFound) cartParseOverHash(cart, cart->userInfo->contents); char when[1024]; safef(when, sizeof(when), "open %s %s", userId, sessionId); cartTrace(cart, when, conn); loadCgiOverHash(cart, oldVars); // I think this is the place to justify old and new values cartJustify(cart, oldVars); #ifndef GBROWSE /* If some CGI other than hgSession been passed hgSession loading instructions, @@ -790,39 +790,39 @@ } static void saveState(struct cart *cart) /* Save out state to permanent storage in both user and session db. */ { struct sqlConnection *conn = cartDefaultConnector(); struct dyString *encoded = newDyString(4096); /* Make up encoded string holding all variables. */ cartEncodeState(cart, encoded); /* Make up update statement unless it looks like a robot with * a great bunch of variables. */ if (encoded->stringSize < 16*1024 || cart->userInfo->useCount > 0) { - updateOne(conn, "userDb", cart->userInfo, encoded->string, encoded->stringSize); - updateOne(conn, "sessionDb", cart->sessionInfo, encoded->string, encoded->stringSize); + updateOne(conn, userDbTable(), cart->userInfo, encoded->string, encoded->stringSize); + updateOne(conn, sessionDbTable(), cart->sessionInfo, encoded->string, encoded->stringSize); } else { fprintf(stderr, "Cart stuffing bot? Not writing %d bytes to cart on first use of %d from IP=%s\n", encoded->stringSize, cart->userInfo->id, cgiRemoteAddr()); /* Do increment the useCount so that cookie-users don't get stuck here: */ - updateOne(conn, "userDb", cart->userInfo, "", 0); + updateOne(conn, userDbTable(), cart->userInfo, "", 0); } /* Cleanup */ cartDefaultDisconnector(&conn); dyStringFree(&encoded); } void cartCheckout(struct cart **pCart) /* Free up cart and save it to database. */ { struct cart *cart = *pCart; if (cart != NULL) { saveState(cart); struct sqlConnection *conn = cartDefaultConnector(); @@ -1442,32 +1442,32 @@ sessionKey = ""; sqlDyStringPrintf(query, " and sessionKey='%s'", sessionKey); } sqlUpdate(conn, query->string); dyStringFree(&query); } void cartResetInDb(char *cookieName) /* Clear cart in database. */ { char *hguid = getCookieId(cookieName); char *hgsid = getSessionId(); struct sqlConnection *conn = cartDefaultConnector(); -clearDbContents(conn, "userDb", hguid); -clearDbContents(conn, "sessionDb", hgsid); +clearDbContents(conn, userDbTable(), hguid); +clearDbContents(conn, sessionDbTable(), hgsid); cartDefaultDisconnector(&conn); } void cartWriteCookie(struct cart *cart, char *cookieName) /* Write out HTTP Set-Cookie statement for cart. */ { char *domain = cfgVal("central.domain"); if (sameWord("HTTPHOST", domain)) { // IE9 does not accept portnames in cookie domains char *hostWithPort = hHttpHost(); struct netParsedUrl npu; netParseUrl(hostWithPort, &npu); if (strchr(npu.host, '.') != NULL) // Domains without a . don't seem to be kept domain = cloneString(npu.host);