Mercurial > hg > monetdb-java
annotate src/main/java/nl/cwi/monetdb/mcl/parser/HeaderLineParser.java @ 119:1ea2ee3b946c
Extend HeaderLineParser to also correctly parse result set header lines for table
and column names which contain special characters such as \", \\, comma-tab combination.
The comma-tab combination is the separator for names in the header line. This is now properly parsed.
Also extended the test program BugResultSetMetaData_Bug_6183.java with examples of such column names.
author | Martin van Dinther <martin.van.dinther@monetdbsolutions.com> |
---|---|
date | Thu, 23 Feb 2017 18:29:46 +0100 (2017-02-23) |
parents | a030c3e53cf5 |
children | fdf4c888d5b7 |
rev | line source |
---|---|
0
a5a898f6886c
Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
1 /* |
a5a898f6886c
Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
2 * This Source Code Form is subject to the terms of the Mozilla Public |
a5a898f6886c
Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
3 * License, v. 2.0. If a copy of the MPL was not distributed with this |
a5a898f6886c
Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. |
a5a898f6886c
Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
5 * |
90
b9b35ca2eec2
Updated copyright year to 2017 in *.java files and MonetDriver.java.in and build.xml and version.sh
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents:
0
diff
changeset
|
6 * Copyright 1997 - July 2008 CWI, August 2008 - 2017 MonetDB B.V. |
0
a5a898f6886c
Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
7 */ |
a5a898f6886c
Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
8 |
a5a898f6886c
Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
9 package nl.cwi.monetdb.mcl.parser; |
a5a898f6886c
Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
10 |
a5a898f6886c
Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
11 |
a5a898f6886c
Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
12 /** |
a5a898f6886c
Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
13 * The HeaderLineParser is a generic MCLParser that extracts values from |
a5a898f6886c
Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
14 * a metadata header in the MCL protocol either as string or integer |
a5a898f6886c
Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
15 * values. |
a5a898f6886c
Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
16 * |
a5a898f6886c
Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
17 * @author Fabian Groffen <Fabian.Groffen> |
a5a898f6886c
Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
18 */ |
a5a898f6886c
Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
19 public class HeaderLineParser extends MCLParser { |
a5a898f6886c
Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
20 private int type; |
a5a898f6886c
Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
21 |
a5a898f6886c
Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
22 public final static int NAME = 1; |
a5a898f6886c
Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
23 public final static int LENGTH = 2; |
a5a898f6886c
Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
24 public final static int TABLE = 3; |
a5a898f6886c
Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
25 public final static int TYPE = 4; |
a5a898f6886c
Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
26 |
a5a898f6886c
Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
27 /** |
a5a898f6886c
Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
28 * Constructs a HeaderLineParser which expects columncount columns. |
a5a898f6886c
Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
29 * |
a5a898f6886c
Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
30 * @param columncount the number of columns in the to be parsed string |
a5a898f6886c
Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
31 */ |
a5a898f6886c
Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
32 public HeaderLineParser(int columncount) { |
a5a898f6886c
Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
33 super(columncount); |
a5a898f6886c
Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
34 } |
a5a898f6886c
Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
35 |
a5a898f6886c
Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
36 /** |
a5a898f6886c
Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
37 * Parses the given String source as header line. If source cannot |
a5a898f6886c
Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
38 * be parsed, an MCLParseException is thrown. The columncount argument |
a5a898f6886c
Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
39 * given during construction is used for allocation of the backing |
a5a898f6886c
Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
40 * array. Parsing a header line with has more fields will therefore |
a5a898f6886c
Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
41 * result in a crash. While this seems illogical, the caller should |
a5a898f6886c
Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
42 * know this size, since the StartOfHeader contains this |
a5a898f6886c
Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
43 * information. |
a5a898f6886c
Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
44 * |
a5a898f6886c
Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
45 * @param source a String which should be parsed |
a5a898f6886c
Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
46 * @return the type of then parsed header line |
a5a898f6886c
Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
47 * @throws MCLParseException if an error occurs during parsing |
a5a898f6886c
Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
48 */ |
a5a898f6886c
Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
49 @Override |
a5a898f6886c
Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
50 public int parse(String source) throws MCLParseException { |
a5a898f6886c
Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
51 char[] chrLine = source.toCharArray(); |
a5a898f6886c
Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
52 int len = chrLine.length; |
a5a898f6886c
Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
53 int pos = 0; |
a5a898f6886c
Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
54 boolean foundChar = false; |
a5a898f6886c
Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
55 boolean nameFound = false; |
a5a898f6886c
Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
56 // find header name |
a5a898f6886c
Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
57 for (int i = len - 1; i >= 0; i--) { |
a5a898f6886c
Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
58 switch (chrLine[i]) { |
a5a898f6886c
Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
59 case ' ': |
a5a898f6886c
Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
60 case '\n': |
a5a898f6886c
Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
61 case '\t': |
a5a898f6886c
Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
62 case '\r': |
a5a898f6886c
Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
63 if (!foundChar) { |
a5a898f6886c
Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
64 len = i - 1; |
a5a898f6886c
Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
65 } else { |
a5a898f6886c
Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
66 pos = i + 1; |
a5a898f6886c
Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
67 } |
a5a898f6886c
Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
68 break; |
a5a898f6886c
Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
69 case '#': |
a5a898f6886c
Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
70 // found! |
a5a898f6886c
Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
71 nameFound = true; |
119
1ea2ee3b946c
Extend HeaderLineParser to also correctly parse result set header lines for table
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents:
117
diff
changeset
|
72 if (pos == 0) |
1ea2ee3b946c
Extend HeaderLineParser to also correctly parse result set header lines for table
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents:
117
diff
changeset
|
73 pos = i + 1; |
0
a5a898f6886c
Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
74 i = 0; // force the loop to terminate |
a5a898f6886c
Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
75 break; |
a5a898f6886c
Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
76 default: |
a5a898f6886c
Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
77 foundChar = true; |
a5a898f6886c
Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
78 pos = 0; |
a5a898f6886c
Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
79 break; |
a5a898f6886c
Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
80 } |
a5a898f6886c
Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
81 } |
a5a898f6886c
Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
82 if (!nameFound) |
a5a898f6886c
Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
83 throw new MCLParseException("invalid header, no header name found", pos); |
a5a898f6886c
Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
84 |
a5a898f6886c
Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
85 // depending on the name of the header, we continue |
a5a898f6886c
Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
86 switch (chrLine[pos]) { |
a5a898f6886c
Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
87 case 'n': |
119
1ea2ee3b946c
Extend HeaderLineParser to also correctly parse result set header lines for table
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents:
117
diff
changeset
|
88 if (len - pos == 4 && source.regionMatches(pos + 1, "name", 1, 3)) { |
0
a5a898f6886c
Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
89 getValues(chrLine, 2, pos - 3); |
a5a898f6886c
Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
90 type = NAME; |
a5a898f6886c
Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
91 } |
a5a898f6886c
Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
92 break; |
a5a898f6886c
Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
93 case 'l': |
119
1ea2ee3b946c
Extend HeaderLineParser to also correctly parse result set header lines for table
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents:
117
diff
changeset
|
94 if (len - pos == 6 && source.regionMatches(pos + 1, "length", 1, 5)) { |
0
a5a898f6886c
Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
95 getIntValues(chrLine, 2, pos - 3); |
a5a898f6886c
Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
96 type = LENGTH; |
a5a898f6886c
Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
97 } |
a5a898f6886c
Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
98 break; |
a5a898f6886c
Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
99 case 't': |
119
1ea2ee3b946c
Extend HeaderLineParser to also correctly parse result set header lines for table
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents:
117
diff
changeset
|
100 if (len - pos == 4 && source.regionMatches(pos + 1, "type", 1, 3)) { |
0
a5a898f6886c
Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
101 getValues(chrLine, 2, pos - 3); |
a5a898f6886c
Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
102 type = TYPE; |
119
1ea2ee3b946c
Extend HeaderLineParser to also correctly parse result set header lines for table
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents:
117
diff
changeset
|
103 } else |
1ea2ee3b946c
Extend HeaderLineParser to also correctly parse result set header lines for table
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents:
117
diff
changeset
|
104 if (len - pos == 10 && source.regionMatches(pos + 1, "table_name", 1, 9)) { |
0
a5a898f6886c
Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
105 getValues(chrLine, 2, pos - 3); |
a5a898f6886c
Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
106 type = TABLE; |
a5a898f6886c
Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
107 } |
a5a898f6886c
Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
108 break; |
a5a898f6886c
Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
109 default: |
a5a898f6886c
Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
110 throw new MCLParseException("unknown header: " + |
a5a898f6886c
Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
111 (new String(chrLine, pos, len - pos))); |
a5a898f6886c
Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
112 } |
a5a898f6886c
Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
113 |
a5a898f6886c
Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
114 // adjust colno |
a5a898f6886c
Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
115 reset(); |
a5a898f6886c
Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
116 |
a5a898f6886c
Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
117 return type; |
a5a898f6886c
Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
118 } |
a5a898f6886c
Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
119 |
a5a898f6886c
Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
120 /** |
a5a898f6886c
Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
121 * Returns an array of Strings containing the values between |
104
839ffec1d36d
Fix for bug 6183.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents:
90
diff
changeset
|
122 * ',\t' separators. |
119
1ea2ee3b946c
Extend HeaderLineParser to also correctly parse result set header lines for table
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents:
117
diff
changeset
|
123 * |
104
839ffec1d36d
Fix for bug 6183.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents:
90
diff
changeset
|
124 * As of Oct2014-SP1 release MAPI adds double quotes around names when |
119
1ea2ee3b946c
Extend HeaderLineParser to also correctly parse result set header lines for table
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents:
117
diff
changeset
|
125 * the name contains a comma or a tab or a space or a # or " or \ escape character. |
104
839ffec1d36d
Fix for bug 6183.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents:
90
diff
changeset
|
126 * See issue: https://www.monetdb.org/bugzilla/show_bug.cgi?id=3616 |
839ffec1d36d
Fix for bug 6183.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents:
90
diff
changeset
|
127 * If the parsed name string part has a " as first and last character, |
839ffec1d36d
Fix for bug 6183.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents:
90
diff
changeset
|
128 * we remove those added double quotes here. |
0
a5a898f6886c
Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
129 * |
a5a898f6886c
Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
130 * @param chrLine a character array holding the input data |
a5a898f6886c
Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
131 * @param start where the relevant data starts |
a5a898f6886c
Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
132 * @param stop where the relevant data stops |
a5a898f6886c
Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
133 */ |
a5a898f6886c
Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
134 final private void getValues(char[] chrLine, int start, int stop) { |
a5a898f6886c
Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
135 int elem = 0; |
119
1ea2ee3b946c
Extend HeaderLineParser to also correctly parse result set header lines for table
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents:
117
diff
changeset
|
136 boolean inString = false, escaped = false; |
0
a5a898f6886c
Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
137 |
119
1ea2ee3b946c
Extend HeaderLineParser to also correctly parse result set header lines for table
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents:
117
diff
changeset
|
138 for (int i = start; i < stop; i++) { |
1ea2ee3b946c
Extend HeaderLineParser to also correctly parse result set header lines for table
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents:
117
diff
changeset
|
139 switch(chrLine[i]) { |
1ea2ee3b946c
Extend HeaderLineParser to also correctly parse result set header lines for table
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents:
117
diff
changeset
|
140 case '\\': |
1ea2ee3b946c
Extend HeaderLineParser to also correctly parse result set header lines for table
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents:
117
diff
changeset
|
141 escaped = !escaped; |
1ea2ee3b946c
Extend HeaderLineParser to also correctly parse result set header lines for table
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents:
117
diff
changeset
|
142 break; |
1ea2ee3b946c
Extend HeaderLineParser to also correctly parse result set header lines for table
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents:
117
diff
changeset
|
143 case '"': |
1ea2ee3b946c
Extend HeaderLineParser to also correctly parse result set header lines for table
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents:
117
diff
changeset
|
144 /** |
1ea2ee3b946c
Extend HeaderLineParser to also correctly parse result set header lines for table
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents:
117
diff
changeset
|
145 * If all strings are wrapped between two quotes, a \" can |
1ea2ee3b946c
Extend HeaderLineParser to also correctly parse result set header lines for table
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents:
117
diff
changeset
|
146 * never exist outside a string. Thus if we believe that we |
1ea2ee3b946c
Extend HeaderLineParser to also correctly parse result set header lines for table
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents:
117
diff
changeset
|
147 * are not within a string, we can safely assume we're about |
1ea2ee3b946c
Extend HeaderLineParser to also correctly parse result set header lines for table
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents:
117
diff
changeset
|
148 * to enter a string if we find a quote. |
1ea2ee3b946c
Extend HeaderLineParser to also correctly parse result set header lines for table
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents:
117
diff
changeset
|
149 * If we are in a string we should stop being in a string if |
1ea2ee3b946c
Extend HeaderLineParser to also correctly parse result set header lines for table
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents:
117
diff
changeset
|
150 * we find a quote which is not prefixed by a \, for that |
1ea2ee3b946c
Extend HeaderLineParser to also correctly parse result set header lines for table
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents:
117
diff
changeset
|
151 * would be an escaped quote. However, a nasty situation can |
1ea2ee3b946c
Extend HeaderLineParser to also correctly parse result set header lines for table
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents:
117
diff
changeset
|
152 * occur where the string is like "test \\" as obvious, a |
1ea2ee3b946c
Extend HeaderLineParser to also correctly parse result set header lines for table
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents:
117
diff
changeset
|
153 * test for a \ in front of a " doesn't hold here for all |
1ea2ee3b946c
Extend HeaderLineParser to also correctly parse result set header lines for table
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents:
117
diff
changeset
|
154 * cases. Because "test \\\"" can exist as well, we need to |
1ea2ee3b946c
Extend HeaderLineParser to also correctly parse result set header lines for table
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents:
117
diff
changeset
|
155 * know if a quote is prefixed by an escaping slash or not. |
1ea2ee3b946c
Extend HeaderLineParser to also correctly parse result set header lines for table
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents:
117
diff
changeset
|
156 */ |
1ea2ee3b946c
Extend HeaderLineParser to also correctly parse result set header lines for table
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents:
117
diff
changeset
|
157 if (!inString) { |
1ea2ee3b946c
Extend HeaderLineParser to also correctly parse result set header lines for table
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents:
117
diff
changeset
|
158 inString = true; |
1ea2ee3b946c
Extend HeaderLineParser to also correctly parse result set header lines for table
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents:
117
diff
changeset
|
159 } else if (!escaped) { |
1ea2ee3b946c
Extend HeaderLineParser to also correctly parse result set header lines for table
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents:
117
diff
changeset
|
160 inString = false; |
1ea2ee3b946c
Extend HeaderLineParser to also correctly parse result set header lines for table
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents:
117
diff
changeset
|
161 } |
1ea2ee3b946c
Extend HeaderLineParser to also correctly parse result set header lines for table
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents:
117
diff
changeset
|
162 // reset escaped flag |
1ea2ee3b946c
Extend HeaderLineParser to also correctly parse result set header lines for table
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents:
117
diff
changeset
|
163 escaped = false; |
1ea2ee3b946c
Extend HeaderLineParser to also correctly parse result set header lines for table
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents:
117
diff
changeset
|
164 break; |
1ea2ee3b946c
Extend HeaderLineParser to also correctly parse result set header lines for table
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents:
117
diff
changeset
|
165 case ',': |
1ea2ee3b946c
Extend HeaderLineParser to also correctly parse result set header lines for table
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents:
117
diff
changeset
|
166 if (!inString && chrLine[i + 1] == '\t') { |
1ea2ee3b946c
Extend HeaderLineParser to also correctly parse result set header lines for table
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents:
117
diff
changeset
|
167 // we found the field separator |
1ea2ee3b946c
Extend HeaderLineParser to also correctly parse result set header lines for table
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents:
117
diff
changeset
|
168 if (chrLine[start] == '"') |
1ea2ee3b946c
Extend HeaderLineParser to also correctly parse result set header lines for table
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents:
117
diff
changeset
|
169 start++; // skip leading double quote |
1ea2ee3b946c
Extend HeaderLineParser to also correctly parse result set header lines for table
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents:
117
diff
changeset
|
170 if (elem < values.length) { |
1ea2ee3b946c
Extend HeaderLineParser to also correctly parse result set header lines for table
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents:
117
diff
changeset
|
171 values[elem++] = new String(chrLine, start, i - (chrLine[i - 1] == '"' ? 1 : 0) - start); |
1ea2ee3b946c
Extend HeaderLineParser to also correctly parse result set header lines for table
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents:
117
diff
changeset
|
172 } |
1ea2ee3b946c
Extend HeaderLineParser to also correctly parse result set header lines for table
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents:
117
diff
changeset
|
173 i++; |
1ea2ee3b946c
Extend HeaderLineParser to also correctly parse result set header lines for table
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents:
117
diff
changeset
|
174 start = i + 1; // reset start for the next name, skipping the field separator (a comma and tab) |
1ea2ee3b946c
Extend HeaderLineParser to also correctly parse result set header lines for table
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents:
117
diff
changeset
|
175 } |
1ea2ee3b946c
Extend HeaderLineParser to also correctly parse result set header lines for table
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents:
117
diff
changeset
|
176 // reset escaped flag |
1ea2ee3b946c
Extend HeaderLineParser to also correctly parse result set header lines for table
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents:
117
diff
changeset
|
177 escaped = false; |
1ea2ee3b946c
Extend HeaderLineParser to also correctly parse result set header lines for table
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents:
117
diff
changeset
|
178 break; |
1ea2ee3b946c
Extend HeaderLineParser to also correctly parse result set header lines for table
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents:
117
diff
changeset
|
179 default: |
1ea2ee3b946c
Extend HeaderLineParser to also correctly parse result set header lines for table
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents:
117
diff
changeset
|
180 escaped = false; |
1ea2ee3b946c
Extend HeaderLineParser to also correctly parse result set header lines for table
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents:
117
diff
changeset
|
181 break; |
0
a5a898f6886c
Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
182 } |
a5a898f6886c
Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
183 } |
104
839ffec1d36d
Fix for bug 6183.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents:
90
diff
changeset
|
184 // add the left over part (last column) |
839ffec1d36d
Fix for bug 6183.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents:
90
diff
changeset
|
185 if (chrLine[start] == '"') |
839ffec1d36d
Fix for bug 6183.
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents:
90
diff
changeset
|
186 start++; // skip leading double quote |
119
1ea2ee3b946c
Extend HeaderLineParser to also correctly parse result set header lines for table
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents:
117
diff
changeset
|
187 if (elem < values.length) |
1ea2ee3b946c
Extend HeaderLineParser to also correctly parse result set header lines for table
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents:
117
diff
changeset
|
188 values[elem] = new String(chrLine, start, stop - (chrLine[stop - 1] == '"' ? 1 : 0) - start); |
0
a5a898f6886c
Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
189 } |
a5a898f6886c
Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
190 |
a5a898f6886c
Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
191 /** |
a5a898f6886c
Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
192 * Returns an array of ints containing the values between |
a5a898f6886c
Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
193 * ',\t' separators. |
a5a898f6886c
Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
194 * |
116
04c535b05c52
Fixed negative number parsing on the StartOfHeaderParser
Pedro Ferreira <pedro.ferreira@monetdbsolutions.com>
parents:
104
diff
changeset
|
195 * Feb2017 note - This integer parser doesn't have to parse negative |
04c535b05c52
Fixed negative number parsing on the StartOfHeaderParser
Pedro Ferreira <pedro.ferreira@monetdbsolutions.com>
parents:
104
diff
changeset
|
196 * numbers, because it is only used to parse column lengths |
119
1ea2ee3b946c
Extend HeaderLineParser to also correctly parse result set header lines for table
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents:
117
diff
changeset
|
197 * which are always greater than 0. |
116
04c535b05c52
Fixed negative number parsing on the StartOfHeaderParser
Pedro Ferreira <pedro.ferreira@monetdbsolutions.com>
parents:
104
diff
changeset
|
198 * |
0
a5a898f6886c
Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
199 * @param chrLine a character array holding the input data |
a5a898f6886c
Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
200 * @param start where the relevant data starts |
a5a898f6886c
Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
201 * @param stop where the relevant data stops |
a5a898f6886c
Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
202 */ |
119
1ea2ee3b946c
Extend HeaderLineParser to also correctly parse result set header lines for table
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents:
117
diff
changeset
|
203 final private void getIntValues(char[] chrLine, int start, int stop) throws MCLParseException { |
0
a5a898f6886c
Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
204 int elem = 0; |
a5a898f6886c
Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
205 int tmp = 0; |
a5a898f6886c
Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
206 |
a5a898f6886c
Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
207 for (int i = start; i < stop; i++) { |
a5a898f6886c
Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
208 if (chrLine[i] == ',' && chrLine[i + 1] == '\t') { |
a5a898f6886c
Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
209 intValues[elem++] = tmp; |
a5a898f6886c
Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
210 tmp = 0; |
117
a030c3e53cf5
Skip over column delimiter, and don't accept just "-" as a valid number.
Sjoerd Mullender <sjoerd@acm.org>
parents:
116
diff
changeset
|
211 start = i++; |
0
a5a898f6886c
Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
212 } else { |
a5a898f6886c
Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
213 // note: don't use Character.isDigit() here, because |
a5a898f6886c
Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
214 // we only want ISO-LATIN-1 digits |
a5a898f6886c
Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
215 if (chrLine[i] >= '0' && chrLine[i] <= '9') { |
119
1ea2ee3b946c
Extend HeaderLineParser to also correctly parse result set header lines for table
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents:
117
diff
changeset
|
216 tmp *= 10; |
0
a5a898f6886c
Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
217 tmp += (int)chrLine[i] - (int)'0'; |
a5a898f6886c
Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
218 } else { |
a5a898f6886c
Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
219 throw new MCLParseException("expected a digit in " + new String(chrLine) + " at " + i); |
a5a898f6886c
Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
220 } |
a5a898f6886c
Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
221 } |
a5a898f6886c
Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
222 } |
119
1ea2ee3b946c
Extend HeaderLineParser to also correctly parse result set header lines for table
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents:
117
diff
changeset
|
223 // add the left over part (last column) |
1ea2ee3b946c
Extend HeaderLineParser to also correctly parse result set header lines for table
Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
parents:
117
diff
changeset
|
224 intValues[elem] = tmp; |
0
a5a898f6886c
Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
225 } |
a5a898f6886c
Copy of MonetDB java directory changeset e6e32756ad31.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
226 } |