Skip to content

Commit 546b71d

Browse files
committed
fixes #390
1 parent d439643 commit 546b71d

File tree

4 files changed

+121
-41
lines changed

4 files changed

+121
-41
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ Also I would like to know about needed examples or documentation stuff.
4343

4444
## Extensions in the latest SNAPSHOT version 0.9.7
4545

46+
* Removed limitation of LongValue to accept only java.util.Long parsable values.
4647
* introduced NOT without parenthesis for column only conditions
4748
* introduced more complex expressions within CASE - statements
4849
* improved Postgresql JSON - support

src/main/java/net/sf/jsqlparser/expression/LongValue.java

Lines changed: 39 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -21,55 +21,54 @@
2121
*/
2222
package net.sf.jsqlparser.expression;
2323

24+
import java.math.BigInteger;
25+
2426
/**
25-
* Every number without a point or an exponential format is a LongValue
27+
* Every number without a point or an exponential format is a LongValue.
2628
*/
2729
public class LongValue implements Expression {
2830

29-
private long value;
30-
private String stringValue;
31+
private String stringValue;
32+
33+
public LongValue(final String value) {
34+
String val = value;
35+
if (val.charAt(0) == '+') {
36+
val = val.substring(1);
37+
}
38+
this.stringValue = val;
39+
}
3140

32-
public LongValue(final String value) {
33-
String val = value;
34-
if (val.charAt(0) == '+') {
35-
val = val.substring(1);
36-
}
37-
try {
38-
this.value = Long.parseLong(val);
39-
} catch (NumberFormatException e) {
40-
throw new NumberFormatException("Passed value does not contain a parsable long value");
41-
}
42-
this.stringValue = val;
43-
}
44-
45-
public LongValue(long value) {
46-
this.value=value;
47-
stringValue = String.valueOf(value);
48-
}
41+
public LongValue(long value) {
42+
stringValue = String.valueOf(value);
43+
}
4944

50-
@Override
51-
public void accept(ExpressionVisitor expressionVisitor) {
52-
expressionVisitor.visit(this);
53-
}
45+
@Override
46+
public void accept(ExpressionVisitor expressionVisitor) {
47+
expressionVisitor.visit(this);
48+
}
5449

55-
public long getValue() {
56-
return value;
57-
}
50+
public long getValue() {
51+
return Long.valueOf(stringValue);
52+
}
53+
54+
public BigInteger getBigIntegerValue() {
55+
return new BigInteger(stringValue);
56+
}
5857

59-
public void setValue(long d) {
60-
value = d;
61-
}
58+
public void setValue(long d) {
59+
stringValue = String.valueOf(d);
60+
}
6261

63-
public String getStringValue() {
64-
return stringValue;
65-
}
62+
public String getStringValue() {
63+
return stringValue;
64+
}
6665

67-
public void setStringValue(String string) {
68-
stringValue = string;
69-
}
66+
public void setStringValue(String string) {
67+
stringValue = string;
68+
}
7069

71-
@Override
72-
public String toString() {
73-
return getStringValue();
74-
}
70+
@Override
71+
public String toString() {
72+
return getStringValue();
73+
}
7574
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*
2+
* Copyright (C) 2017 JSQLParser.
3+
*
4+
* This library is free software; you can redistribute it and/or
5+
* modify it under the terms of the GNU Lesser General Public
6+
* License as published by the Free Software Foundation; either
7+
* version 2.1 of the License, or (at your option) any later version.
8+
*
9+
* This library 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 GNU
12+
* Lesser General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU Lesser General Public
15+
* License along with this library; if not, write to the Free Software
16+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
17+
* MA 02110-1301 USA
18+
*/
19+
package net.sf.jsqlparser.expression;
20+
21+
import java.math.BigInteger;
22+
import org.junit.After;
23+
import org.junit.AfterClass;
24+
import org.junit.Before;
25+
import org.junit.BeforeClass;
26+
import org.junit.Test;
27+
import static org.junit.Assert.*;
28+
29+
/**
30+
*
31+
* @author tw
32+
*/
33+
public class LongValueTest {
34+
35+
public LongValueTest() {
36+
}
37+
38+
@BeforeClass
39+
public static void setUpClass() {
40+
}
41+
42+
@AfterClass
43+
public static void tearDownClass() {
44+
}
45+
46+
@Before
47+
public void setUp() {
48+
}
49+
50+
@After
51+
public void tearDown() {
52+
}
53+
54+
@Test
55+
public void testSimpleNumber() {
56+
LongValue value = new LongValue("123");
57+
58+
assertEquals("123", value.getStringValue());
59+
assertEquals(123L, value.getValue());
60+
assertEquals(new BigInteger("123"), value.getBigIntegerValue());
61+
}
62+
63+
@Test
64+
public void testLargeNumber() {
65+
final String largeNumber = "20161114000000035001";
66+
LongValue value = new LongValue(largeNumber);
67+
68+
assertEquals(largeNumber, value.getStringValue());
69+
try {
70+
value.getValue();
71+
fail("should not work");
72+
} catch (Exception e) {
73+
74+
}
75+
assertEquals(new BigInteger(largeNumber), value.getBigIntegerValue());
76+
}
77+
}

src/test/java/net/sf/jsqlparser/test/select/SelectTest.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
import java.util.*;
1616

1717
import static net.sf.jsqlparser.test.TestUtils.*;
18-
import net.sf.jsqlparser.util.TablesNamesFinder;
1918

2019
public class SelectTest extends TestCase {
2120

@@ -2440,4 +2439,8 @@ public void testProblemInNotInProblemIssue379() throws JSQLParserException {
24402439
assertSqlCanBeParsedAndDeparsed("SELECT rank FROM DBObjects WHERE rank NOT IN (0, 1)");
24412440
assertSqlCanBeParsedAndDeparsed("SELECT rank FROM DBObjects WHERE rank IN (0, 1)");
24422441
}
2442+
2443+
public void testProblemLargeNumbersIssue390() throws JSQLParserException {
2444+
assertSqlCanBeParsedAndDeparsed("SELECT * FROM student WHERE student_no = 20161114000000035001");
2445+
}
24432446
}

0 commit comments

Comments
 (0)