Repository: proullon/ramsql (go)

Issue:
Cannot parse negative numbers
For example, this query:
```
select to_id, date from edges where from_id = -1 and edge_type = 3 order by id desc
```

leads to:
```
Cannot lex instruction. Syntax error near -1 and edge_type = 3 order by id desc
```

Pull request:
fix: (close #98) correctly parse negative number in queries

Patch:
diff --git a/engine/parser/lexer.go b/engine/parser/lexer.go
index a5529d2..6e82c03 100644
--- a/engine/parser/lexer.go
+++ b/engine/parser/lexer.go
@@ -367,7 +367,7 @@ func (l *lexer) MatchStringToken() bool {
 func (l *lexer) MatchFloatToken() bool {
 
 	i := l.pos
-	for i < l.instructionLen && (unicode.IsDigit(rune(l.instruction[i]))) {
+	for i < l.instructionLen && (unicode.IsDigit(rune(l.instruction[i])) || l.instruction[i] == '-') {
 		i++
 	}
 	if i == l.pos || i >= l.instructionLen {
@@ -413,7 +413,7 @@ func (l *lexer) MatchFloatToken() bool {
 func (l *lexer) MatchNumberToken() bool {
 
 	i := l.pos
-	for i < l.instructionLen && unicode.IsDigit(rune(l.instruction[i])) {
+	for i < l.instructionLen && (unicode.IsDigit(rune(l.instruction[i])) || l.instruction[i] == '-') {
 		i++
 	}
 


Test patch:
diff --git a/driver/driver_test.go b/driver/driver_test.go
index fb18f28..24f176b 100644
--- a/driver/driver_test.go
+++ b/driver/driver_test.go
@@ -1244,6 +1244,8 @@ func TestFloat(t *testing.T) {
 		`INSERT INTO user (name, surname, age) VALUES (Homer, Simpson, 40);`,
 		`INSERT INTO user (name, surname, age) VALUES (Marge, Simpson, 40);`,
 		`INSERT INTO user (name, surname, age) VALUES (Bruce, Wayne, 3333);`,
+		`INSERT INTO user (name, surname, age) VALUES (Bruce, Future, -3333);`,
+		`INSERT INTO user (name, surname, age) VALUES (Bruce, Demo, -1.0);`,
 	}
 
 	db, err := sql.Open("ramsql", "TestFloat")
@@ -1295,6 +1297,14 @@ func TestFloat(t *testing.T) {
 	if err != nil {
 		t.Fatalf("Cannot run UPDATE query with AND: %s\n", err)
 	}
+
+	db.QueryRow(`SELECT age FROM user WHERE surname = 'Demo'`).Scan(&age)
+	if err != nil {
+		t.Fatalf("Cannot load negative age: %s", err)
+	}
+	if age != -1.0 {
+		t.Fatalf("Expected age to be -1.0, got %f", age)
+	}
 }
 
 func TestDrop(t *testing.T) {
diff --git a/engine/parser/parser_test.go b/engine/parser/parser_test.go
index 2ea9158..d2a7d00 100644
--- a/engine/parser/parser_test.go
+++ b/engine/parser/parser_test.go
@@ -155,6 +155,11 @@ func TestInsertNumberWithBacktickQuote(t *testing.T) {
 	parse(query, 1, t)
 }
 
+func TestSelectNegativeNumber(t *testing.T) {
+	query := `select to_id, date from edges where from_id = -1 and edge_type = 3 order by id desc`
+	parse(query, 1, t)
+}
+
 func TestCreateTableWithKeywordName(t *testing.T) {
 	query := `CREATE TABLE test ("id" bigserial not null primary key, "name" text, "key" text)`
 	parse(query, 1, t)