comparison src/c/urweb.c @ 1011:16f7cb0891b6

Initial support for char in SQL
author Adam Chlipala <adamc@hcoop.net>
date Thu, 22 Oct 2009 16:15:56 -0400
parents 5a0f6ec208ce
children e46227efcbba
comparison
equal deleted inserted replaced
1010:6b0f3853cc81 1011:16f7cb0891b6
1386 *p++ = 0; 1386 *p++ = 0;
1387 ctx->heap.front = p; 1387 ctx->heap.front = p;
1388 return result; 1388 return result;
1389 } 1389 }
1390 1390
1391 char *uw_Basis_attrifyChar(uw_context ctx, uw_Basis_char c) {
1392 char *result, *p;
1393 uw_check_heap(ctx, 7);
1394
1395 result = p = ctx->heap.front;
1396
1397 if (c == '"') {
1398 strcpy(p, "&quot;");
1399 p += 6;
1400 } else if (c == '&') {
1401 strcpy(p, "&amp;");
1402 p += 5;
1403 }
1404 else if (isprint(c))
1405 *p++ = c;
1406 else {
1407 int len2;
1408 sprintf(p, "&#%d;%n", c, &len2);
1409 p += len2;
1410 }
1411
1412 *p++ = 0;
1413 ctx->heap.front = p;
1414 return result;
1415 }
1416
1391 char *uw_Basis_attrifyCss_class(uw_context ctx, uw_Basis_css_class s) { 1417 char *uw_Basis_attrifyCss_class(uw_context ctx, uw_Basis_css_class s) {
1392 return s; 1418 return s;
1393 } 1419 }
1394 1420
1395 static void uw_Basis_attrifyInt_w_unsafe(uw_context ctx, uw_Basis_int n) { 1421 static void uw_Basis_attrifyInt_w_unsafe(uw_context ctx, uw_Basis_int n) {
1431 else { 1457 else {
1432 uw_write_unsafe(ctx, "&#"); 1458 uw_write_unsafe(ctx, "&#");
1433 uw_Basis_attrifyInt_w_unsafe(ctx, c); 1459 uw_Basis_attrifyInt_w_unsafe(ctx, c);
1434 uw_writec_unsafe(ctx, ';'); 1460 uw_writec_unsafe(ctx, ';');
1435 } 1461 }
1462 }
1463
1464 return uw_unit_v;
1465 }
1466
1467 uw_unit uw_Basis_attrifyChar_w(uw_context ctx, uw_Basis_char c) {
1468 uw_check(ctx, 6);
1469
1470 if (c == '"')
1471 uw_write_unsafe(ctx, "&quot;");
1472 else if (c == '&')
1473 uw_write_unsafe(ctx, "&amp;");
1474 else if (isprint(c))
1475 uw_writec_unsafe(ctx, c);
1476 else {
1477 uw_write_unsafe(ctx, "&#");
1478 uw_Basis_attrifyInt_w_unsafe(ctx, c);
1479 uw_writec_unsafe(ctx, ';');
1436 } 1480 }
1437 1481
1438 return uw_unit_v; 1482 return uw_unit_v;
1439 } 1483 }
1440 1484
1986 return uw_Basis_sqlifyFloat(ctx, *n); 2030 return uw_Basis_sqlifyFloat(ctx, *n);
1987 } 2031 }
1988 2032
1989 int uw_Estrings = 1; 2033 int uw_Estrings = 1;
1990 char *uw_sqlsuffixString = "::text"; 2034 char *uw_sqlsuffixString = "::text";
2035 char *uw_sqlsuffixChar = "::char";
1991 2036
1992 uw_Basis_string uw_Basis_sqlifyString(uw_context ctx, uw_Basis_string s) { 2037 uw_Basis_string uw_Basis_sqlifyString(uw_context ctx, uw_Basis_string s) {
1993 char *r, *s2; 2038 char *r, *s2;
1994 2039
1995 uw_check_heap(ctx, strlen(s) * 2 + 3 + uw_Estrings + strlen(uw_sqlsuffixString)); 2040 uw_check_heap(ctx, strlen(s) * 2 + 3 + uw_Estrings + strlen(uw_sqlsuffixString));
2030 } 2075 }
2031 2076
2032 *s2++ = '\''; 2077 *s2++ = '\'';
2033 strcpy(s2, uw_sqlsuffixString); 2078 strcpy(s2, uw_sqlsuffixString);
2034 ctx->heap.front = s2 + 1 + strlen(uw_sqlsuffixString); 2079 ctx->heap.front = s2 + 1 + strlen(uw_sqlsuffixString);
2080 return r;
2081 }
2082
2083 uw_Basis_string uw_Basis_sqlifyChar(uw_context ctx, uw_Basis_char c) {
2084 char *r, *s2;
2085
2086 uw_check_heap(ctx, 5 + uw_Estrings + strlen(uw_sqlsuffixChar));
2087
2088 r = s2 = ctx->heap.front;
2089 if (uw_Estrings)
2090 *s2++ = 'E';
2091 *s2++ = '\'';
2092
2093 switch (c) {
2094 case '\'':
2095 if (uw_Estrings)
2096 strcpy(s2, "\\'");
2097 else
2098 strcpy(s2, "''");
2099 s2 += 2;
2100 break;
2101 case '\\':
2102 if (uw_Estrings) {
2103 strcpy(s2, "\\\\");
2104 s2 += 2;
2105 } else
2106 *s2++ = '\\';
2107 break;
2108 default:
2109 if (isprint(c))
2110 *s2++ = c;
2111 else if (uw_Estrings) {
2112 sprintf(s2, "\\%03o", c);
2113 s2 += 4;
2114 }
2115 else
2116 uw_error(ctx, FATAL, "Non-printable character %u in char to SQLify", c);
2117 }
2118
2119 *s2++ = '\'';
2120 strcpy(s2, uw_sqlsuffixChar);
2121 ctx->heap.front = s2 + 1 + strlen(uw_sqlsuffixChar);
2035 return r; 2122 return r;
2036 } 2123 }
2037 2124
2038 char *uw_sqlsuffixBlob = "::bytea"; 2125 char *uw_sqlsuffixBlob = "::bytea";
2039 2126