comparison src/c/urweb.c @ 879:b2a175a0f2ef

Demo working with MySQL
author Adam Chlipala <adamc@hcoop.net>
date Thu, 16 Jul 2009 18:10:29 -0400
parents 3c7b48040dcf
children e6070333d8a8
comparison
equal deleted inserted replaced
878:a8952047e1d3 879:b2a175a0f2ef
1930 char *r = uw_malloc(ctx, len); 1930 char *r = uw_malloc(ctx, len);
1931 memcpy(r, p, len); 1931 memcpy(r, p, len);
1932 return r; 1932 return r;
1933 } 1933 }
1934 1934
1935 char *uw_sqlfmtInt = "%lld::int8%n";
1936
1935 char *uw_Basis_sqlifyInt(uw_context ctx, uw_Basis_int n) { 1937 char *uw_Basis_sqlifyInt(uw_context ctx, uw_Basis_int n) {
1936 int len; 1938 int len;
1937 char *r; 1939 char *r;
1938 1940
1939 uw_check_heap(ctx, INTS_MAX + 6); 1941 uw_check_heap(ctx, INTS_MAX + 6);
1940 r = ctx->heap.front; 1942 r = ctx->heap.front;
1941 sprintf(r, "%lld::int8%n", n, &len); 1943 sprintf(r, uw_sqlfmtInt, n, &len);
1942 ctx->heap.front += len+1; 1944 ctx->heap.front += len+1;
1943 return r; 1945 return r;
1944 } 1946 }
1945 1947
1946 char *uw_Basis_sqlifyIntN(uw_context ctx, uw_Basis_int *n) { 1948 char *uw_Basis_sqlifyIntN(uw_context ctx, uw_Basis_int *n) {
1948 return "NULL"; 1950 return "NULL";
1949 else 1951 else
1950 return uw_Basis_sqlifyInt(ctx, *n); 1952 return uw_Basis_sqlifyInt(ctx, *n);
1951 } 1953 }
1952 1954
1955 char *uw_sqlfmtFloat = "%g::float8%n";
1956
1953 char *uw_Basis_sqlifyFloat(uw_context ctx, uw_Basis_float n) { 1957 char *uw_Basis_sqlifyFloat(uw_context ctx, uw_Basis_float n) {
1954 int len; 1958 int len;
1955 char *r; 1959 char *r;
1956 1960
1957 uw_check_heap(ctx, FLOATS_MAX + 8); 1961 uw_check_heap(ctx, FLOATS_MAX + 8);
1958 r = ctx->heap.front; 1962 r = ctx->heap.front;
1959 sprintf(r, "%g::float8%n", n, &len); 1963 sprintf(r, uw_sqlfmtFloat, n, &len);
1960 ctx->heap.front += len+1; 1964 ctx->heap.front += len+1;
1961 return r; 1965 return r;
1962 } 1966 }
1963 1967
1964 char *uw_Basis_sqlifyFloatN(uw_context ctx, uw_Basis_float *n) { 1968 char *uw_Basis_sqlifyFloatN(uw_context ctx, uw_Basis_float *n) {
1966 return "NULL"; 1970 return "NULL";
1967 else 1971 else
1968 return uw_Basis_sqlifyFloat(ctx, *n); 1972 return uw_Basis_sqlifyFloat(ctx, *n);
1969 } 1973 }
1970 1974
1975 int uw_Estrings = 1;
1976 char *uw_sqlsuffixString = "::text";
1971 1977
1972 uw_Basis_string uw_Basis_sqlifyString(uw_context ctx, uw_Basis_string s) { 1978 uw_Basis_string uw_Basis_sqlifyString(uw_context ctx, uw_Basis_string s) {
1973 char *r, *s2; 1979 char *r, *s2;
1974 1980
1975 uw_check_heap(ctx, strlen(s) * 2 + 10); 1981 uw_check_heap(ctx, strlen(s) * 2 + 3 + uw_Estrings + strlen(uw_sqlsuffixString));
1976 1982
1977 r = s2 = ctx->heap.front; 1983 r = s2 = ctx->heap.front;
1978 *s2++ = 'E'; 1984 if (uw_Estrings)
1985 *s2++ = 'E';
1979 *s2++ = '\''; 1986 *s2++ = '\'';
1980 1987
1981 for (; *s; s++) { 1988 for (; *s; s++) {
1982 char c = *s; 1989 char c = *s;
1983 1990
1991 s2 += 2; 1998 s2 += 2;
1992 break; 1999 break;
1993 default: 2000 default:
1994 if (isprint(c)) 2001 if (isprint(c))
1995 *s2++ = c; 2002 *s2++ = c;
1996 else { 2003 else if (uw_Estrings) {
1997 sprintf(s2, "\\%03o", c); 2004 sprintf(s2, "\\%03o", c);
1998 s2 += 4; 2005 s2 += 4;
1999 } 2006 }
2000 } 2007 else
2001 } 2008 uw_error(ctx, FATAL, "Non-printable character %u in string to SQLify", c);
2002 2009 }
2003 strcpy(s2, "'::text"); 2010 }
2004 ctx->heap.front = s2 + 8; 2011
2005 return r; 2012 *s2++ = '\'';
2006 } 2013 strcpy(s2, uw_sqlsuffixString);
2014 ctx->heap.front = s2 + 1 + strlen(uw_sqlsuffixString);
2015 return r;
2016 }
2017
2018 char *uw_sqlsuffixBlob = "::bytea";
2007 2019
2008 uw_Basis_string uw_Basis_sqlifyBlob(uw_context ctx, uw_Basis_blob b) { 2020 uw_Basis_string uw_Basis_sqlifyBlob(uw_context ctx, uw_Basis_blob b) {
2009 char *r, *s2; 2021 char *r, *s2;
2010 size_t i; 2022 size_t i;
2011 2023
2012 uw_check_heap(ctx, b.size * 5 + 11); 2024 uw_check_heap(ctx, b.size * 5 + 3 + uw_Estrings + strlen(uw_sqlsuffixBlob));
2013 2025
2014 r = s2 = ctx->heap.front; 2026 r = s2 = ctx->heap.front;
2015 *s2++ = 'E'; 2027 if (uw_Estrings)
2028 *s2++ = 'E';
2016 *s2++ = '\''; 2029 *s2++ = '\'';
2017 2030
2018 for (i = 0; i < b.size; ++i) { 2031 for (i = 0; i < b.size; ++i) {
2019 char c = b.data[i]; 2032 char c = b.data[i];
2020 2033
2028 s2 += 4; 2041 s2 += 4;
2029 break; 2042 break;
2030 default: 2043 default:
2031 if (isprint(c)) 2044 if (isprint(c))
2032 *s2++ = c; 2045 *s2++ = c;
2033 else { 2046 else if (uw_Estrings) {
2034 sprintf(s2, "\\\\%03o", c); 2047 sprintf(s2, "\\\\%03o", c);
2035 s2 += 5; 2048 s2 += 5;
2036 } 2049 }
2037 } 2050 else
2038 } 2051 uw_error(ctx, FATAL, "Non-printable character %u in blob to SQLify", c);
2039 2052 }
2040 strcpy(s2, "'::bytea"); 2053 }
2041 ctx->heap.front = s2 + 9; 2054
2055 *s2++ = '\'';
2056 strcpy(s2, uw_sqlsuffixBlob);
2057 ctx->heap.front = s2 + 1 + strlen(uw_sqlsuffixBlob);
2042 return r; 2058 return r;
2043 } 2059 }
2044 2060
2045 char *uw_Basis_sqlifyChannel(uw_context ctx, uw_Basis_channel chn) { 2061 char *uw_Basis_sqlifyChannel(uw_context ctx, uw_Basis_channel chn) {
2046 int len; 2062 int len;
2047 char *r; 2063 char *r;
2048 unsigned long long combo = ((unsigned long long)chn.cli << 32) | chn.chn; 2064 unsigned long long combo = ((unsigned long long)chn.cli << 32) | chn.chn;
2049 2065
2050 uw_check_heap(ctx, INTS_MAX + 7); 2066 uw_check_heap(ctx, INTS_MAX + 7);
2051 r = ctx->heap.front; 2067 r = ctx->heap.front;
2052 sprintf(r, "%lld::int8%n", combo, &len); 2068 sprintf(r, uw_sqlfmtInt, combo, &len);
2053 ctx->heap.front += len+1; 2069 ctx->heap.front += len+1;
2054 return r; 2070 return r;
2055 } 2071 }
2056 2072
2057 char *uw_Basis_attrifyChannel(uw_context ctx, uw_Basis_channel chn) { 2073 char *uw_Basis_attrifyChannel(uw_context ctx, uw_Basis_channel chn) {
2064 sprintf(r, "%lld%n", combo, &len); 2080 sprintf(r, "%lld%n", combo, &len);
2065 ctx->heap.front += len+1; 2081 ctx->heap.front += len+1;
2066 return r; 2082 return r;
2067 } 2083 }
2068 2084
2085 char *uw_sqlfmtUint4 = "%u::int4%n";
2086
2069 char *uw_Basis_sqlifyClient(uw_context ctx, uw_Basis_client cli) { 2087 char *uw_Basis_sqlifyClient(uw_context ctx, uw_Basis_client cli) {
2070 int len; 2088 int len;
2071 char *r; 2089 char *r;
2072 2090
2073 uw_check_heap(ctx, INTS_MAX + 7); 2091 uw_check_heap(ctx, INTS_MAX + 7);
2074 r = ctx->heap.front; 2092 r = ctx->heap.front;
2075 sprintf(r, "%u::int4%n", cli, &len); 2093 sprintf(r, uw_sqlfmtUint4, cli, &len);
2076 ctx->heap.front += len+1; 2094 ctx->heap.front += len+1;
2077 return r; 2095 return r;
2078 } 2096 }
2079 2097
2080 char *uw_Basis_attrifyClient(uw_context ctx, uw_Basis_client cli) { 2098 char *uw_Basis_attrifyClient(uw_context ctx, uw_Basis_client cli) {