|
| 1 | +/* |
| 2 | + * Routines for handling of SET var TO statements |
| 3 | + * |
| 4 | + * $Id: variable.c,v 1.3 1997/04/17 13:50:30 scrappy Exp $ |
| 5 | + * |
| 6 | + * $Log: variable.c,v $ |
| 7 | + * Revision 1.3 1997/04/17 13:50:30 scrappy |
| 8 | + * From: "Martin J. Laubach" <mjl@CSlab.tuwien.ac.at> |
| 9 | + * Subject: [HACKERS] Patch: set date to euro/us postgres/iso/sql |
| 10 | + * |
| 11 | + * Here a patch that implements a SET date for use by the datetime |
| 12 | + * stuff. The syntax is |
| 13 | + * |
| 14 | + * SET date TO 'val[,val,...]' |
| 15 | + * |
| 16 | + * where val is us (us dates), euro (european dates), postgres, |
| 17 | + * iso or sql. |
| 18 | + * |
| 19 | + * Thomas is working on the integration in his datetime module. |
| 20 | + * I just needed to get the patch out before it went stale :) |
| 21 | + * |
| 22 | + * Revision 1.1 1997/04/10 16:52:07 mjl |
| 23 | + * Initial revision |
| 24 | + */ |
| 25 | +/*-----------------------------------------------------------------------*/ |
| 26 | + |
| 27 | +#include <string.h> |
1 | 28 | #include "postgres.h"
|
2 | 29 | #include "tcop/variable.h"
|
3 | 30 |
|
| 31 | +/*-----------------------------------------------------------------------*/ |
| 32 | +#if USE_EURODATES |
| 33 | +#define DATE_EURO TRUE |
| 34 | +#else |
| 35 | +#define DATE_EURO FALSE |
| 36 | +#endif |
| 37 | + |
| 38 | +/*-----------------------------------------------------------------------*/ |
| 39 | +struct PGVariables PGVariables = |
| 40 | + { |
| 41 | + { DATE_EURO, Date_Postgres } |
| 42 | + }; |
| 43 | + |
| 44 | +/*-----------------------------------------------------------------------*/ |
| 45 | +static const char *get_token(char *buf, int size, const char *str) |
| 46 | + { |
| 47 | + if(!*str) |
| 48 | + return NULL; |
| 49 | + |
| 50 | + /* skip white space */ |
| 51 | + while(*str && (*str == ' ' || *str == '\t')) |
| 52 | + str++; |
| 53 | + |
| 54 | + /* copy until we hit white space or comma or end of string */ |
| 55 | + while(*str && *str != ' ' && *str != '\t' && *str != ',' && size-- > 1) |
| 56 | + *buf++ = *str++; |
| 57 | + |
| 58 | + *buf = '\0'; |
| 59 | + |
| 60 | + /* skip white space and comma*/ |
| 61 | + while(*str && (*str == ' ' || *str == '\t' || *str == ',')) |
| 62 | + str++; |
| 63 | + |
| 64 | + return str; |
| 65 | + } |
| 66 | + |
| 67 | +/*-----------------------------------------------------------------------*/ |
| 68 | +static bool parse_null(const char *value) |
| 69 | + { |
| 70 | + return TRUE; |
| 71 | + } |
| 72 | + |
| 73 | +static bool parse_date(const char *value) |
| 74 | + { |
| 75 | + char tok[32]; |
| 76 | + int dcnt = 0, ecnt = 0; |
| 77 | + |
| 78 | + while(value = get_token(tok, sizeof(tok), value)) |
| 79 | + { |
| 80 | + /* Ugh. Somebody ought to write a table driven version -- mjl */ |
| 81 | + |
| 82 | + if(!strcasecmp(tok, "iso")) |
| 83 | + { |
| 84 | + PGVariables.date.format = Date_ISO; |
| 85 | + dcnt++; |
| 86 | + } |
| 87 | + else if(!strcasecmp(tok, "sql")) |
| 88 | + { |
| 89 | + PGVariables.date.format = Date_SQL; |
| 90 | + dcnt++; |
| 91 | + } |
| 92 | + else if(!strcasecmp(tok, "postgres")) |
| 93 | + { |
| 94 | + PGVariables.date.format = Date_Postgres; |
| 95 | + dcnt++; |
| 96 | + } |
| 97 | + else if(!strcasecmp(tok, "euro")) |
| 98 | + { |
| 99 | + PGVariables.date.euro = TRUE; |
| 100 | + ecnt++; |
| 101 | + } |
| 102 | + else if(!strcasecmp(tok, "us")) |
| 103 | + { |
| 104 | + PGVariables.date.euro = FALSE; |
| 105 | + ecnt++; |
| 106 | + } |
| 107 | + else |
| 108 | + { |
| 109 | + elog(WARN, "Bad value for date (%s)", tok); |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + if(dcnt > 1 || ecnt > 1) |
| 114 | + elog(NOTICE, "Conflicting settings for date"); |
| 115 | + |
| 116 | + return TRUE; |
| 117 | + } |
| 118 | + |
| 119 | +/*-----------------------------------------------------------------------*/ |
| 120 | +struct VariableParsers |
| 121 | + { |
| 122 | + const char *name; |
| 123 | + bool (*parser)(const char *); |
| 124 | + } VariableParsers[] = |
| 125 | + { |
| 126 | + { "date", parse_date }, |
| 127 | + { "timezone", parse_null }, |
| 128 | + { NULL } |
| 129 | + }; |
| 130 | + |
| 131 | +/*-----------------------------------------------------------------------*/ |
4 | 132 | bool SetPGVariable(const char *name, const char *value)
|
5 | 133 | {
|
6 |
| - elog(NOTICE, "Variable %s set to \"%s\"", name, value); |
| 134 | + struct VariableParsers *vp; |
| 135 | + |
| 136 | + for(vp = VariableParsers; vp->name; vp++) |
| 137 | + { |
| 138 | + if(!strcasecmp(vp->name, name)) |
| 139 | + return (vp->parser)(value); |
| 140 | + } |
| 141 | + |
| 142 | + elog(NOTICE, "No such variable %s", name); |
7 | 143 |
|
8 | 144 | return TRUE;
|
9 | 145 | }
|
10 | 146 |
|
| 147 | +/*-----------------------------------------------------------------------*/ |
11 | 148 | const char *GetPGVariable(const char *varName)
|
12 | 149 | {
|
13 | 150 | return NULL;
|
14 | 151 | }
|
| 152 | +/*-----------------------------------------------------------------------*/ |
0 commit comments