Skip to content

Commit efbb92e

Browse files
authored
Merge pull request #248 from jongleb/fix-get-col-uint64
Fix parse uint64 with parens
2 parents b255e69 + c170fa2 commit efbb92e

File tree

2 files changed

+122
-19
lines changed

2 files changed

+122
-19
lines changed

lib/sql_parser.mly

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -387,10 +387,10 @@ column_def1: c=column_def { `Attr c }
387387
| FULLTEXT index_or_key? l=table_index { Dialect_feature.set_fulltext_index ($startofs, $endofs); `Index l }
388388
| SPATIAL index_or_key? l=table_index { `Index l }
389389

390-
key_part:
391-
n=IDENT delimited(LPAREN,INTEGER,RPAREN)? { n }
392-
| n=IDENT delimited(LPAREN,INTEGER,RPAREN)? ASC { n }
393-
| n=IDENT delimited(LPAREN,INTEGER,RPAREN)? DESC { n }
390+
int_arg: delimited(LPAREN,INTEGER,RPAREN) {}
391+
392+
key_part: n=IDENT int_arg? either(ASC,DESC)? { n }
393+
394394
index_options: list(IDENT)? { }
395395

396396
table_index: IDENT? l=sequence(key_part) index_options { l }
@@ -617,6 +617,16 @@ interval_unit: INTERVAL_UNIT
617617
| DAY_MICROSECOND | DAY_SECOND | DAY_MINUTE | DAY_HOUR
618618
| YEAR_MONTH { Value (strict Datetime) }
619619

620+
int1:
621+
| T_INTEGER { (Int, Int) }
622+
| T_BIG_INTEGER { (Int, UInt64) }
623+
624+
int_type:
625+
| kind=int1 int_arg? u=UNSIGNED? {
626+
let (signed, unsigned) = kind in
627+
Option.map_default (fun _ -> Dialect_feature.set_unsigned_types ($startofs, $endofs); unsigned) signed u
628+
}
629+
620630
expr_sql_type_flavor:
621631
| T_DECIMAL p=option(delimited(LPAREN, pair(INTEGER, option(preceded(COMMA, INTEGER))), RPAREN)) {
622632
match p with
@@ -631,18 +641,10 @@ expr_sql_type_flavor:
631641
| T_UUID { Blob }
632642
| T_JSON { Json }
633643

634-
sql_int_type_flavor:
635-
| T_INTEGER u=UNSIGNED? { Option.may (fun _ -> Dialect_feature.set_unsigned_types ($startofs, $endofs)) u; Int }
636-
| T_BIG_INTEGER u=UNSIGNED? {
637-
Option.may (fun _ -> Dialect_feature.set_unsigned_types ($startofs, $endofs)) u;
638-
match u with
639-
| Some _ -> UInt64
640-
| None -> Int
641-
}
642-
643-
sql_type_flavor: t=sql_int_type_flavor ZEROFILL? { t }
644-
| expr_sql_type_flavor { $1 }
645-
| ENUM ctors=sequence(TEXT) charset? collate? { make_enum_kind ctors }
644+
sql_type_flavor:
645+
| t=int_type ZEROFILL? { t }
646+
| expr_sql_type_flavor { $1 }
647+
| ENUM ctors=sequence(TEXT) charset? collate? { make_enum_kind ctors }
646648

647649
binary: T_BLOB | BINARY | BINARY VARYING { }
648650
text: T_TEXT | T_TEXT LPAREN INTEGER RPAREN | CHARACTER { }
@@ -681,9 +683,7 @@ strict_type:
681683
| T_TEXT { Text }
682684
| T_JSON { Json }
683685
| T_BLOB { Blob }
684-
| T_INTEGER { Int }
685-
| T_BIG_INTEGER { Int }
686-
| T_BIG_INTEGER UNSIGNED { UInt64 }
686+
| int_type { $1 }
687687
| T_FLOAT { Float }
688688
| T_BOOLEAN { Bool }
689689
| T_DATETIME { Datetime }

test/cram/test.t

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2462,3 +2462,106 @@ Test REPLACE function with INSERT REPLACE in same query:
24622462
T.execute db ("REPLACE INTO table_24_10_2025 (col_1, col_2) VALUES (REPLACE(?, ',', ' '), 'data')") set_params
24632463
24642464
end (* module Sqlgg *)
2465+
2466+
Test BIGINT(20) UNSIGNED with module annotation:
2467+
$ sqlgg -gen caml -no-header -dialect=mysql - <<'EOF' 2>&1
2468+
> CREATE TABLE test_table (
2469+
> -- [sqlgg] module=TestModule
2470+
> id BIGINT(20) UNSIGNED NOT NULL,
2471+
> counter INT(10) UNSIGNED NOT NULL,
2472+
> counter2 BIGINT(10) UNSIGNED NOT NULL DEFAULT 0,
2473+
> name TEXT
2474+
> );
2475+
> SELECT id, counter, counter2, name FROM test_table WHERE id = @id;
2476+
> INSERT INTO test_table (id, counter, counter2, name) VALUES (@id, @counter, @counter2, @name);
2477+
> UPDATE test_table SET counter = @counter, counter2 = @counter2, name = @name WHERE id = @id;
2478+
> EOF
2479+
module Sqlgg (T : Sqlgg_traits.M) = struct
2480+
2481+
module IO = Sqlgg_io.Blocking
2482+
2483+
let create_test_table db =
2484+
T.execute db ("CREATE TABLE test_table (\n\
2485+
id BIGINT(20) UNSIGNED NOT NULL,\n\
2486+
counter INT(10) UNSIGNED NOT NULL,\n\
2487+
counter2 BIGINT(10) UNSIGNED NOT NULL DEFAULT 0,\n\
2488+
name TEXT\n\
2489+
)") T.no_params
2490+
2491+
let select_1 db ~id callback =
2492+
let invoke_callback stmt =
2493+
callback
2494+
~id:(TestModule.get_column (T.get_column_uint64 stmt 0))
2495+
~counter:(T.get_column_Int stmt 1)
2496+
~counter2:(T.get_column_UInt64 stmt 2)
2497+
~name:(T.get_column_Text_nullable stmt 3)
2498+
in
2499+
let set_params stmt =
2500+
let p = T.start_params stmt (1) in
2501+
T.set_param_uint64 p (TestModule.set_param id);
2502+
T.finish_params p
2503+
in
2504+
T.select db ("SELECT id, counter, counter2, name FROM test_table WHERE id = ?") set_params invoke_callback
2505+
2506+
let insert_test_table_2 db ~id ~counter ~counter2 ~name =
2507+
let set_params stmt =
2508+
let p = T.start_params stmt (4) in
2509+
T.set_param_uint64 p (TestModule.set_param id);
2510+
T.set_param_Int p counter;
2511+
T.set_param_UInt64 p counter2;
2512+
begin match name with None -> T.set_param_null p | Some v -> T.set_param_Text p v end;
2513+
T.finish_params p
2514+
in
2515+
T.execute db ("INSERT INTO test_table (id, counter, counter2, name) VALUES (?, ?, ?, ?)") set_params
2516+
2517+
let update_test_table_3 db ~counter ~counter2 ~name ~id =
2518+
let set_params stmt =
2519+
let p = T.start_params stmt (4) in
2520+
T.set_param_Int p counter;
2521+
T.set_param_UInt64 p counter2;
2522+
begin match name with None -> T.set_param_null p | Some v -> T.set_param_Text p v end;
2523+
T.set_param_uint64 p (TestModule.set_param id);
2524+
T.finish_params p
2525+
in
2526+
T.execute db ("UPDATE test_table SET counter = ?, counter2 = ?, name = ? WHERE id = ?") set_params
2527+
2528+
module Fold = struct
2529+
let select_1 db ~id callback acc =
2530+
let invoke_callback stmt =
2531+
callback
2532+
~id:(TestModule.get_column (T.get_column_uint64 stmt 0))
2533+
~counter:(T.get_column_Int stmt 1)
2534+
~counter2:(T.get_column_UInt64 stmt 2)
2535+
~name:(T.get_column_Text_nullable stmt 3)
2536+
in
2537+
let set_params stmt =
2538+
let p = T.start_params stmt (1) in
2539+
T.set_param_uint64 p (TestModule.set_param id);
2540+
T.finish_params p
2541+
in
2542+
let r_acc = ref acc in
2543+
IO.(>>=) (T.select db ("SELECT id, counter, counter2, name FROM test_table WHERE id = ?") set_params (fun x -> r_acc := invoke_callback x !r_acc))
2544+
(fun () -> IO.return !r_acc)
2545+
2546+
end (* module Fold *)
2547+
2548+
module List = struct
2549+
let select_1 db ~id callback =
2550+
let invoke_callback stmt =
2551+
callback
2552+
~id:(TestModule.get_column (T.get_column_uint64 stmt 0))
2553+
~counter:(T.get_column_Int stmt 1)
2554+
~counter2:(T.get_column_UInt64 stmt 2)
2555+
~name:(T.get_column_Text_nullable stmt 3)
2556+
in
2557+
let set_params stmt =
2558+
let p = T.start_params stmt (1) in
2559+
T.set_param_uint64 p (TestModule.set_param id);
2560+
T.finish_params p
2561+
in
2562+
let r_acc = ref [] in
2563+
IO.(>>=) (T.select db ("SELECT id, counter, counter2, name FROM test_table WHERE id = ?") set_params (fun x -> r_acc := invoke_callback x :: !r_acc))
2564+
(fun () -> IO.return (List.rev !r_acc))
2565+
2566+
end (* module List *)
2567+
end (* module Sqlgg *)

0 commit comments

Comments
 (0)