comparison src/c/urweb.c @ 1867:216a3a67ebe3

Tweak Sergey's patch to work with Postgres
author Adam Chlipala <adam@chlipala.net>
date Fri, 13 Sep 2013 10:24:10 -0400
parents 9fc086e3c95a
children 22b44fe822bf
comparison
equal deleted inserted replaced
1866:9fc086e3c95a 1867:216a3a67ebe3
2507 return r; 2507 return r;
2508 } 2508 }
2509 2509
2510 char *uw_sqlsuffixBlob = "::bytea"; 2510 char *uw_sqlsuffixBlob = "::bytea";
2511 2511
2512 uw_Basis_string uw_Basis_sqlifyBlob_old(uw_context ctx, uw_Basis_blob b) { 2512 uw_Basis_string uw_Basis_sqlifyBlob(uw_context ctx, uw_Basis_blob b) {
2513 char *r, *s2; 2513 char *r, *s2;
2514 size_t i; 2514 size_t i;
2515 2515
2516 uw_check_heap(ctx, b.size * 5 + 3 + uw_Estrings + strlen(uw_sqlsuffixBlob)); 2516 uw_check_heap(ctx, b.size * 5 + 4 + strlen(uw_sqlsuffixBlob));
2517 2517
2518 r = s2 = ctx->heap.front; 2518 r = s2 = ctx->heap.front;
2519 if (uw_Estrings) 2519 if (uw_Estrings)
2520 *s2++ = 'E'; 2520 *s2++ = 'E';
2521 else
2522 *s2++ = 'X';
2521 *s2++ = '\''; 2523 *s2++ = '\'';
2522 2524
2523 for (i = 0; i < b.size; ++i) { 2525 for (i = 0; i < b.size; ++i) {
2524 char c = b.data[i]; 2526 unsigned char c = b.data[i];
2525 2527
2526 switch (c) { 2528 if (uw_Estrings) {
2527 case '\'': 2529 switch (c) {
2528 if (uw_Estrings) 2530 case '\'':
2529 strcpy(s2, "\\'"); 2531 strcpy(s2, "\\'");
2530 else 2532 s2 += 2;
2531 strcpy(s2, "''"); 2533 break;
2532 s2 += 2; 2534 case '\\':
2533 break;
2534 case '\\':
2535 if (uw_Estrings) {
2536 strcpy(s2, "\\\\\\\\"); 2535 strcpy(s2, "\\\\\\\\");
2537 s2 += 4; 2536 s2 += 4;
2538 } else 2537 break;
2539 *s2++ = '\\'; 2538 default:
2540 break; 2539 if (isprint((int)c))
2541 default: 2540 *s2++ = c;
2542 if (isprint((int)c)) 2541 else {
2543 *s2++ = c; 2542 sprintf(s2, "\\\\%03o", c);
2544 else if (uw_Estrings) { 2543 s2 += 5;
2545 sprintf(s2, "\\\\%03o", c); 2544 }
2546 s2 += 5;
2547 } 2545 }
2548 else 2546 } else {
2549 uw_error(ctx, FATAL, "Non-printable character %u in blob to SQLify", c); 2547 sprintf(s2, "%02X", c);
2550 } 2548 s2 += 2;
2551 } 2549 }
2550 }
2552 2551
2553 *s2++ = '\''; 2552 *s2++ = '\'';
2554 strcpy(s2, uw_sqlsuffixBlob); 2553 strcpy(s2, uw_sqlsuffixBlob);
2555 ctx->heap.front = s2 + 1 + strlen(uw_sqlsuffixBlob); 2554 ctx->heap.front = s2 + 1 + strlen(uw_sqlsuffixBlob);
2556 return r;
2557 }
2558
2559 int uw_Xstrings = 1;
2560
2561 uw_Basis_string uw_Basis_sqlifyBlob(uw_context ctx, uw_Basis_blob b) {
2562 char *r, *s2;
2563 size_t i;
2564
2565 uw_check_heap(ctx, b.size * 2 + 3 + uw_Xstrings);
2566
2567 r = s2 = ctx->heap.front;
2568 *s2++ = 'X';
2569 *s2++ = '\'';
2570
2571 for (i = 0; i < b.size; ++i) {
2572 char c = b.data[i];
2573 sprintf(s2, "%02X", c);
2574 s2 += 2;
2575 }
2576
2577 *s2++ = '\'';
2578 ctx->heap.front = s2 + 1;
2579 return r; 2555 return r;
2580 } 2556 }
2581 2557
2582 char *uw_Basis_sqlifyChannel(uw_context ctx, uw_Basis_channel chn) { 2558 char *uw_Basis_sqlifyChannel(uw_context ctx, uw_Basis_channel chn) {
2583 int len; 2559 int len;