Added the postgres reader. Closes patch #6388
[pspp-builds.git] / src / language / data-io / get-data.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 2007, 2008 Free Software Foundation, Inc.
3
4    This program is free software: you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation, either version 3 of the License, or
7    (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program.  If not, see <http://www.gnu.org/licenses/>. */
16
17 #include <config.h>
18
19 #include <stdlib.h>
20
21 #include <data/gnumeric-reader.h>
22 #include <data/psql-reader.h>
23
24 #include <data/dictionary.h>
25 #include <data/format.h>
26 #include <data/procedure.h>
27 #include <language/command.h>
28 #include <language/data-io/data-parser.h>
29 #include <language/data-io/data-reader.h>
30 #include <language/data-io/file-handle.h>
31 #include <language/data-io/placement-parser.h>
32 #include <language/lexer/format-parser.h>
33 #include <language/lexer/lexer.h>
34 #include <libpspp/message.h>
35
36 #include "gettext.h"
37 #define _(msgid) gettext (msgid)
38 #define N_(msgid) (msgid)
39
40 static int parse_get_gnm (struct lexer *lexer, struct dataset *);
41 static int parse_get_txt (struct lexer *lexer, struct dataset *);
42 static int parse_get_psql (struct lexer *lexer, struct dataset *);
43
44 int
45 cmd_get_data (struct lexer *lexer, struct dataset *ds)
46 {
47   lex_force_match (lexer, '/');
48
49   if (!lex_force_match_id (lexer, "TYPE"))
50     return CMD_FAILURE;
51
52   lex_force_match (lexer, '=');
53
54   if (lex_match_id (lexer, "GNM"))
55     return parse_get_gnm (lexer, ds);
56   else if (lex_match_id (lexer, "TXT"))
57     return parse_get_txt (lexer, ds);
58   else if (lex_match_id (lexer, "PSQL"))
59     return parse_get_psql (lexer, ds);
60
61   msg (SE, _("Unsupported TYPE %s"), lex_tokid (lexer));
62   return CMD_FAILURE;
63 }
64
65 static int
66 parse_get_psql (struct lexer *lexer, struct dataset *ds)
67 {
68   struct psql_read_info psql;
69   psql.allow_clear = false;
70   psql.conninfo = NULL;
71   psql.str_width = -1;
72   ds_init_empty (&psql.sql);
73
74   lex_force_match (lexer, '/');
75
76   if (!lex_force_match_id (lexer, "CONNECT"))
77     goto error;
78
79   lex_force_match (lexer, '=');
80
81   if (!lex_force_string (lexer))
82     goto error;
83
84   psql.conninfo = strdup (ds_cstr (lex_tokstr (lexer)));
85
86   lex_get (lexer);
87
88   while (lex_match (lexer, '/') )
89     {
90       if ( lex_match_id (lexer, "ASSUMEDSTRWIDTH"))
91         {
92           lex_match (lexer, '=');
93           psql.str_width = lex_integer (lexer);
94           lex_get (lexer);
95         }
96       else if ( lex_match_id (lexer, "UNENCRYPTED"))
97         {
98           psql.allow_clear = true;
99         }
100       else if (lex_match_id (lexer, "SQL"))
101         {
102           lex_match (lexer, '=');
103           if ( ! lex_force_string (lexer) )
104             goto error;
105
106           ds_put_substring (&psql.sql,  lex_tokstr (lexer)->ss);
107           lex_get (lexer);
108         }
109      }
110   {
111     struct dictionary *dict = NULL;
112     struct casereader *reader = psql_open_reader (&psql, &dict);
113
114     if ( reader )
115       proc_set_active_file (ds, reader, dict);
116   }
117
118   ds_destroy (&psql.sql);
119   free (psql.conninfo);
120
121   return CMD_SUCCESS;
122
123  error:
124
125   ds_destroy (&psql.sql);
126   free (psql.conninfo);
127
128   return CMD_FAILURE;
129 }
130
131 static int
132 parse_get_gnm (struct lexer *lexer, struct dataset *ds)
133 {
134   struct gnumeric_read_info gri  = {NULL, NULL, NULL, 1, true, -1};
135
136   lex_force_match (lexer, '/');
137
138   if (!lex_force_match_id (lexer, "FILE"))
139     goto error;
140
141   lex_force_match (lexer, '=');
142
143   if (!lex_force_string (lexer))
144     goto error;
145
146   gri.file_name = strdup (ds_cstr (lex_tokstr (lexer)));
147
148   lex_get (lexer);
149
150   while (lex_match (lexer, '/') )
151     {
152       if ( lex_match_id (lexer, "ASSUMEDSTRWIDTH"))
153         {
154           lex_match (lexer, '=');
155           gri.asw = lex_integer (lexer);
156         }
157       else if (lex_match_id (lexer, "SHEET"))
158         {
159           lex_match (lexer, '=');
160           if (lex_match_id (lexer, "NAME"))
161             {
162               if ( ! lex_force_string (lexer) )
163                 goto error;
164
165               gri.sheet_name = strdup (ds_cstr (lex_tokstr (lexer)));
166               gri.sheet_index = -1;
167             }
168           else if (lex_match_id (lexer, "INDEX"))
169             {
170               gri.sheet_index = lex_integer (lexer);
171             }
172           else
173             goto error;
174         }
175       else if (lex_match_id (lexer, "CELLRANGE"))
176         {
177           lex_match (lexer, '=');
178
179           if (lex_match_id (lexer, "FULL"))
180             {
181               gri.cell_range = NULL;
182               lex_put_back (lexer, T_ID);
183             }
184           else if (lex_match_id (lexer, "RANGE"))
185             {
186               if ( ! lex_force_string (lexer) )
187                 goto error;
188
189               gri.cell_range = strdup (ds_cstr (lex_tokstr (lexer)));
190             }
191           else
192             goto error;
193         }
194       else if (lex_match_id (lexer, "READNAMES"))
195         {
196           lex_match (lexer, '=');
197
198           if ( lex_match_id (lexer, "ON"))
199             {
200               gri.read_names = true;
201             }
202           else if (lex_match_id (lexer, "OFF"))
203             {
204               gri.read_names = false;
205             }
206           else
207             goto error;
208           lex_put_back (lexer, T_ID);
209         }
210       else
211         {
212           printf ("Unknown data file type \"\%s\"\n", lex_tokid (lexer));
213           goto error;
214         }
215       lex_get (lexer);
216     }
217
218   {
219     struct dictionary *dict = NULL;
220     struct casereader *reader = gnumeric_open_reader (&gri, &dict);
221
222     if ( reader )
223       proc_set_active_file (ds, reader, dict);
224   }
225
226   free (gri.file_name);
227   free (gri.sheet_name);
228   free (gri.cell_range);
229   return CMD_SUCCESS;
230
231  error:
232
233   free (gri.file_name);
234   free (gri.sheet_name);
235   free (gri.cell_range);
236   return CMD_FAILURE;
237 }
238
239 static bool
240 set_type (struct data_parser *parser, const char *subcommand,
241           enum data_parser_type type, bool *has_type)
242 {
243   if (!*has_type)
244     {
245       data_parser_set_type (parser, type);
246       *has_type = true;
247     }
248   else if (type != data_parser_get_type (parser))
249     {
250       msg (SE, _("%s is allowed only with %s arrangement, but %s arrangement "
251                  "was stated or implied earlier in this command."),
252            subcommand,
253            type == DP_FIXED ? "FIXED" : "DELIMITED",
254            type == DP_FIXED ? "DELIMITED" : "FIXED");
255       return false;
256     }
257   return true;
258 }
259
260 static int
261 parse_get_txt (struct lexer *lexer, struct dataset *ds)
262 {
263   struct data_parser *parser = NULL;
264   struct dictionary *dict = NULL;
265   struct file_handle *fh = NULL;
266   struct dfm_reader *reader = NULL;
267
268   int record;
269   enum data_parser_type type;
270   bool has_type;
271
272   lex_force_match (lexer, '/');
273
274   if (!lex_force_match_id (lexer, "FILE"))
275     goto error;
276   lex_force_match (lexer, '=');
277   fh = fh_parse (lexer, FH_REF_FILE | FH_REF_INLINE);
278   if (fh == NULL)
279     goto error;
280
281   parser = data_parser_create ();
282   has_type = false;
283   data_parser_set_type (parser, DP_DELIMITED);
284   data_parser_set_span (parser, false);
285   data_parser_set_quotes (parser, ss_empty ());
286   data_parser_set_empty_line_has_field (parser, true);
287
288   for (;;)
289     {
290       if (!lex_force_match (lexer, '/'))
291         goto error;
292
293       if (lex_match_id (lexer, "ARRANGEMENT"))
294         {
295           bool ok;
296
297           lex_match (lexer, '=');
298           if (lex_match_id (lexer, "FIXED"))
299             ok = set_type (parser, "ARRANGEMENT=FIXED", DP_FIXED, &has_type);
300           else if (lex_match_id (lexer, "DELIMITED"))
301             ok = set_type (parser, "ARRANGEMENT=DELIMITED",
302                            DP_DELIMITED, &has_type);
303           else
304             {
305               lex_error (lexer, _("expecting FIXED or DELIMITED"));
306               goto error;
307             }
308           if (!ok)
309             goto error;
310         }
311       else if (lex_match_id (lexer, "FIRSTCASE"))
312         {
313           lex_match (lexer, '=');
314           if (!lex_force_int (lexer))
315             goto error;
316           if (lex_integer (lexer) < 1)
317             {
318               msg (SE, _("Value of FIRSTCASE must be 1 or greater."));
319               goto error;
320             }
321           data_parser_set_skip (parser, lex_integer (lexer) - 1);
322           lex_get (lexer);
323         }
324       else if (lex_match_id_n (lexer, "DELCASE", 4))
325         {
326           if (!set_type (parser, "DELCASE", DP_DELIMITED, &has_type))
327             goto error;
328           lex_match (lexer, '=');
329           if (lex_match_id (lexer, "LINE"))
330             data_parser_set_span (parser, false);
331           else if (lex_match_id (lexer, "VARIABLES"))
332             {
333               data_parser_set_span (parser, true);
334
335               /* VARIABLES takes an integer argument, but for no
336                  good reason.  We just ignore it. */
337               if (!lex_force_int (lexer))
338                 goto error;
339               lex_get (lexer);
340             }
341           else
342             {
343               lex_error (lexer, _("expecting LINE or VARIABLES"));
344               goto error;
345             }
346         }
347       else if (lex_match_id (lexer, "FIXCASE"))
348         {
349           if (!set_type (parser, "FIXCASE", DP_FIXED, &has_type))
350             goto error;
351           lex_match (lexer, '=');
352           if (!lex_force_int (lexer))
353             goto error;
354           if (lex_integer (lexer) < 1)
355             {
356               msg (SE, _("Value of FIXCASE must be at least 1."));
357               goto error;
358             }
359           data_parser_set_records (parser, lex_integer (lexer));
360           lex_get (lexer);
361         }
362       else if (lex_match_id (lexer, "IMPORTCASES"))
363         {
364           lex_match (lexer, '=');
365           if (lex_match (lexer, T_ALL))
366             {
367               data_parser_set_case_limit (parser, -1);
368               data_parser_set_case_percent (parser, 100);
369             }
370           else if (lex_match_id (lexer, "FIRST"))
371             {
372               if (!lex_force_int (lexer))
373                 goto error;
374               if (lex_integer (lexer) < 1)
375                 {
376                   msg (SE, _("Value of FIRST must be at least 1."));
377                   goto error;
378                 }
379               data_parser_set_case_limit (parser, lex_integer (lexer));
380               lex_get (lexer);
381             }
382           else if (lex_match_id (lexer, "PERCENT"))
383             {
384               if (!lex_force_int (lexer))
385                 goto error;
386               if (lex_integer (lexer) < 1 || lex_integer (lexer) > 100)
387                 {
388                   msg (SE, _("Value of PERCENT must be between 1 and 100."));
389                   goto error;
390                 }
391               data_parser_set_case_percent (parser, lex_integer (lexer));
392               lex_get (lexer);
393             }
394         }
395       else if (lex_match_id_n (lexer, "DELIMITERS", 4))
396         {
397           struct string hard_seps = DS_EMPTY_INITIALIZER;
398           const char *soft_seps = "";
399           struct substring s;
400           int c;
401
402           if (!set_type (parser, "DELIMITERS", DP_DELIMITED, &has_type))
403             goto error;
404           lex_match (lexer, '=');
405
406           if (!lex_force_string (lexer))
407             goto error;
408
409           s = ds_ss (lex_tokstr (lexer));
410           if (ss_match_string (&s, ss_cstr ("\\t")))
411             ds_put_cstr (&hard_seps, "\t");
412           if (ss_match_string (&s, ss_cstr ("\\\\")))
413             ds_put_cstr (&hard_seps, "\\");
414           while ((c = ss_get_char (&s)) != EOF)
415             if (c == ' ')
416               soft_seps = " ";
417             else
418               ds_put_char (&hard_seps, c);
419           data_parser_set_soft_delimiters (parser, ss_cstr (soft_seps));
420           data_parser_set_hard_delimiters (parser, ds_ss (&hard_seps));
421           ds_destroy (&hard_seps);
422
423           lex_get (lexer);
424         }
425       else if (lex_match_id (lexer, "QUALIFIER"))
426         {
427           if (!set_type (parser, "QUALIFIER", DP_DELIMITED, &has_type))
428             goto error;
429           lex_match (lexer, '=');
430
431           if (!lex_force_string (lexer))
432             goto error;
433
434           data_parser_set_quotes (parser, ds_ss (lex_tokstr (lexer)));
435           lex_get (lexer);
436         }
437       else if (lex_match_id (lexer, "VARIABLES"))
438         break;
439       else
440         {
441           lex_error (lexer, _("expecting VARIABLES"));
442           goto error;
443         }
444     }
445   lex_match (lexer, '=');
446
447   dict = dict_create ();
448   record = 1;
449   type = data_parser_get_type (parser);
450   do
451     {
452       char name[VAR_NAME_LEN + 1];
453       struct fmt_spec input, output;
454       int fc, lc;
455       struct variable *v;
456
457       while (type == DP_FIXED && lex_match (lexer, '/'))
458         {
459           if (!lex_force_int (lexer))
460             goto error;
461           if (lex_integer (lexer) < record)
462             {
463               msg (SE, _("The record number specified, %ld, is at or "
464                          "before the previous record, %d.  Data "
465                          "fields must be listed in order of "
466                          "increasing record number."),
467                    lex_integer (lexer), record);
468               goto error;
469             }
470           if (lex_integer (lexer) > data_parser_get_records (parser))
471             {
472               msg (SE, _("The record number specified, %ld, exceeds "
473                          "the number of records per case specified "
474                          "on FIXCASE, %d."),
475                    lex_integer (lexer), data_parser_get_records (parser));
476               goto error;
477             }
478           record = lex_integer (lexer);
479           lex_get (lexer);
480         }
481
482       if (!lex_force_id (lexer))
483         goto error;
484       strcpy (name, lex_tokid (lexer));
485       lex_get (lexer);
486
487       if (type == DP_DELIMITED)
488         {
489           if (!parse_format_specifier (lexer, &input)
490               || !fmt_check_input (&input))
491             goto error;
492         }
493       else
494         {
495           if (!parse_column_range (lexer, 0, &fc, &lc, NULL))
496             goto error;
497           if (!parse_format_specifier_name (lexer, &input.type))
498             goto error;
499           input.w = lc - fc + 1;
500           input.d = 0;
501           if (!fmt_check_input (&input))
502             goto error;
503         }
504       output = fmt_for_output_from_input (&input);
505
506       v = dict_create_var (dict, name, fmt_var_width (&input));
507       if (v == NULL)
508         {
509           msg (SE, _("%s is a duplicate variable name."), name);
510           goto error;
511         }
512       var_set_both_formats (v, &output);
513
514       if (type == DP_DELIMITED)
515         data_parser_add_delimited_field (parser, &input,
516                                          var_get_case_index (v),
517                                          name);
518       else
519         data_parser_add_fixed_field (parser, &input, var_get_case_index (v),
520                                      name, record, fc);
521     }
522   while (lex_token (lexer) != '.');
523
524   reader = dfm_open_reader (fh, lexer);
525   if (reader == NULL)
526     goto error;
527
528   data_parser_make_active_file (parser, ds, reader, dict);
529   fh_unref (fh);
530   return CMD_SUCCESS;
531
532  error:
533   data_parser_destroy (parser);
534   dict_destroy (dict);
535   fh_unref (fh);
536   return CMD_CASCADING_FAILURE;
537 }