ada_ado_de4b3c95/src/ado-parameters.adb

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
-----------------------------------------------------------------------
--  ADO Parameters -- Parameters for queries
--  Copyright (C) 2010, 2011, 2012, 2013, 2017, 2018, 2019, 2022 Stephane Carrez
--  Written by Stephane Carrez (Stephane.Carrez@gmail.com)
--
--  Licensed under the Apache License, Version 2.0 (the "License");
--  you may not use this file except in compliance with the License.
--  You may obtain a copy of the License at
--
--      http://www.apache.org/licenses/LICENSE-2.0
--
--  Unless required by applicable law or agreed to in writing, software
--  distributed under the License is distributed on an "AS IS" BASIS,
--  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
--  See the License for the specific language governing permissions and
--  limitations under the License.
-----------------------------------------------------------------------

with Util.Strings;
with Util.Log.Loggers;

with Ada.Calendar.Formatting;
package body ADO.Parameters is

   use Parameter_Vectors;

   Log : constant Util.Log.Loggers.Logger := Util.Log.Loggers.Create ("ADO.Parameters");

   --  --------------------
   --  Set the SQL dialect description object.
   --  --------------------
   procedure Set_Dialect (Params : in out Abstract_List;
                          D      : in ADO.Dialects.Dialect_Access) is
   begin
      Params.Dialect := D;
   end Set_Dialect;

   --  --------------------
   --  Set the cache expander to be used when expanding the SQL.
   --  --------------------
   procedure Set_Expander (Params   : in out Abstract_List;
                           Expander : in ADO.Parameters.Expander_Access) is
   begin
      Params.Expander := Expander;
   end Set_Expander;

   --  --------------------
   --  Get the SQL dialect description object.
   --  --------------------
   function Get_Dialect (From : in Abstract_List) return ADO.Dialects.Dialect_Access is
   begin
      return From.Dialect;
   end Get_Dialect;

   procedure Bind_Param (Params : in out Abstract_List;
                         Name   : in String;
                         Value  : in Boolean) is
      Param : constant Parameter := Parameter '(T         => T_BOOLEAN,
                                                Len       => Name'Length,
                                                Value_Len => 0,
                                                Name      => Name,
                                                Position  => 0,
                                                Bool      => Value);
   begin
      Abstract_List'Class (Params).Add_Parameter (Param);
   end Bind_Param;

   procedure Bind_Param (Params : in out Abstract_List;
                         Name   : in String;
                         Value  : in Integer) is
      Param : constant Parameter := Parameter '(T         => T_INTEGER,
                                                Len       => Name'Length,
                                                Value_Len => 0,
                                                Name      => Name,
                                                Position  => 0,
                                                Num       => Value);
   begin
      Abstract_List'Class (Params).Add_Parameter (Param);
   end Bind_Param;

   procedure Bind_Param (Params : in out Abstract_List;
                         Name   : in String;
                         Value  : in Long_Long_Integer) is
      Param : constant Parameter := Parameter '(T         => T_LONG_INTEGER,
                                                Len       => Name'Length,
                                                Value_Len => 0,
                                                Name      => Name,
                                                Position  => 0,
                                                Long_Num  => Value);
   begin
      Abstract_List'Class (Params).Add_Parameter (Param);
   end Bind_Param;

   procedure Bind_Param (Params : in out Abstract_List;
                         Name  : in String;
                         Value : in Identifier) is
      Param : constant Parameter := Parameter '(T         => T_LONG_INTEGER,
                                                Len       => Name'Length,
                                                Value_Len => 0,
                                                Name      => Name,
                                                Position  => 0,
                                                Long_Num  => Long_Long_Integer (Value));
   begin
      Abstract_List'Class (Params).Add_Parameter (Param);
   end Bind_Param;

   procedure Bind_Param (Params : in out Abstract_List;
                         Name   : in String;
                         Value  : in ADO.Entity_Type) is
      Param : constant Parameter := Parameter '(T         => T_LONG_INTEGER,
                                                Len       => Name'Length,
                                                Value_Len => 0,
                                                Name      => Name,
                                                Position  => 0,
                                                Long_Num  => Long_Long_Integer (Value));
   begin
      Abstract_List'Class (Params).Add_Parameter (Param);
   end Bind_Param;

   procedure Bind_Param (Params : in out Abstract_List;
                         Name  : in String;
                         Value : in String) is
   begin
      Abstract_List'Class (Params).
        Add_Parameter (Parameter '(T         => T_STRING,
                                   Len       => Name'Length,
                                   Value_Len => Value'Length,
                                   Name      => Name,
                                   Position  => 0,
                                   Str       => Value));
   end Bind_Param;

   procedure Bind_Param (Params : in out Abstract_List;
                         Name  : in String;
                         Value : in Token) is
   begin
      Abstract_List'Class (Params).
        Add_Parameter (Parameter '(T         => T_TOKEN,
                                   Len       => Name'Length,
                                   Value_Len => Value'Length,
                                   Name      => Name,
                                   Position  => 0,
                                   Str       => String (Value)));
   end Bind_Param;

   procedure Bind_Param (Params : in out Abstract_List;
                         Name  : in String;
                         Value : in ADO.Blob_Ref) is
   begin
      Abstract_List'Class (Params).
        Add_Parameter (Parameter '(T         => T_BLOB,
                                   Len       => Name'Length,
                                   Value_Len => 0,
                                   Name      => Name,
                                   Position  => 0,
                                   Data      => Value));
   end Bind_Param;

   procedure Bind_Param (Params : in out Abstract_List;
                         Name   : in String;
                         Value  : in ADO.Utils.Identifier_Vector) is
      S : constant String := ADO.Utils.To_Parameter_List (Value);
   begin
      Abstract_List'Class (Params).
        Add_Parameter (Parameter '(T         => T_LIST,
                                   Len       => Name'Length,
                                   Value_Len => S'Length,
                                   Name      => Name,
                                   Position  => 0,
                                   Str       => S));
   end Bind_Param;

   procedure Bind_Param (Params : in out Abstract_List;
                         Name  : in String;
                         Value : in Unbounded_String) is
      Val   : constant String := To_String (Value);
      Param : constant Parameter := Parameter '(T         => T_STRING,
                                                Len       => Name'Length,
                                                Value_Len => Val'Length,
                                                Name      => Name,
                                                Position  => 0,
                                                Str       => Val);
   begin
      Abstract_List'Class (Params).Add_Parameter (Param);
   end Bind_Param;

   procedure Bind_Param (Params : in out Abstract_List;
                         Name   : in String;
                         Value  : in Ada.Calendar.Time) is
      Param : constant Parameter := Parameter '(T         => T_DATE,
                                                Len       => Name'Length,
                                                Value_Len => 0,
                                                Name      => Name,
                                                Position  => 0,
                                                Time      => Value);
   begin
      Abstract_List'Class (Params).Add_Parameter (Param);
   end Bind_Param;

   procedure Bind_Param (Params   : in out Abstract_List;
                         Position : in Natural;
                         Value    : in Boolean) is
      P : Parameter := Parameter '(T         => T_BOOLEAN,
                                   Len       => 0,
                                   Value_Len => 0,
                                   Name      => "",
                                   Position  => Position,
                                   Bool      => Value);
   begin
      if Position = 0 then
         P.Position := Abstract_List'Class (Params).Length + 1;
      end if;
      Abstract_List'Class (Params).Add_Parameter (P);
   end Bind_Param;

   procedure Bind_Param (Params   : in out Abstract_List;
                         Position : in Natural;
                         Value    : in Long_Long_Integer) is
      P : Parameter := Parameter '(T         => T_LONG_INTEGER,
                                   Len       => 0,
                                   Value_Len => 0,
                                   Name      => "",
                                   Position  => Position,
                                   Long_Num  => Value);
   begin
      if Position = 0 then
         P.Position := Abstract_List'Class (Params).Length + 1;
      end if;
      Abstract_List'Class (Params).Add_Parameter (P);
   end Bind_Param;

   procedure Bind_Param (Params   : in out Abstract_List;
                         Position : in Natural;
                         Value    : in Long_Float) is
      P : Parameter := Parameter '(T         => T_LONG_FLOAT,
                                   Len       => 0,
                                   Value_Len => 0,
                                   Name      => "",
                                   Position  => Position,
                                   Float     => Value);
   begin
      if Position = 0 then
         P.Position := Abstract_List'Class (Params).Length + 1;
      end if;
      Abstract_List'Class (Params).Add_Parameter (P);
   end Bind_Param;

   procedure Bind_Param (Params   : in out Abstract_List;
                         Position : in Natural;
                         Value    : in Identifier) is
      P : Parameter := Parameter '(T         => T_LONG_INTEGER,
                                   Len       => 0,
                                   Value_Len => 0,
                                   Name      => "",
                                   Position  => Position,
                                   Long_Num  => Long_Long_Integer (Value));
   begin
      if Position = 0 then
         P.Position := Abstract_List'Class (Params).Length + 1;
      end if;
      Abstract_List'Class (Params).Add_Parameter (P);
   end Bind_Param;

   procedure Bind_Param (Params   : in out Abstract_List;
                         Position : in Natural;
                         Value    : in Integer) is
      P : Parameter := Parameter '(T         => T_INTEGER,
                                   Len       => 0,
                                   Value_Len => 0,
                                   Name      => "",
                                   Position  => Position,
                                   Num       => Value);
   begin
      if Position = 0 then
         P.Position := Abstract_List'Class (Params).Length + 1;
      end if;
      Abstract_List'Class (Params).Add_Parameter (P);
   end Bind_Param;

   procedure Bind_Param (Params   : in out Abstract_List;
                         Position : in Natural;
                         Value    : in Entity_Type) is
      P : Parameter := Parameter '(T         => T_INTEGER,
                                   Len       => 0,
                                   Value_Len => 0,
                                   Name      => "",
                                   Position  => Position,
                                   Num       => Integer (Value));
   begin
      if Position = 0 then
         P.Position := Abstract_List'Class (Params).Length + 1;
      end if;
      Abstract_List'Class (Params).Add_Parameter (P);
   end Bind_Param;

   procedure Bind_Param (Params   : in out Abstract_List;
                         Position : in Natural;
                         Value    : in String) is
      P : Parameter := Parameter '(T         => T_STRING,
                                   Len       => 0,
                                   Value_Len => Value'Length,
                                   Name      => "",
                                   Position  => Position,
                                   Str       => Value);
   begin
      if Position = 0 then
         P.Position := Abstract_List'Class (Params).Length + 1;
      end if;
      Abstract_List'Class (Params).Add_Parameter (P);
   end Bind_Param;

   procedure Bind_Param (Params   : in out Abstract_List;
                         Position : in Natural;
                         Value    : in Unbounded_String) is
      Val : constant String := To_String (Value);
      P : Parameter := Parameter '(T         => T_STRING,
                                   Len       => 0,
                                   Value_Len => Val'Length,
                                   Name      => "",
                                   Position  => Position,
                                   Str       => Val);
   begin
      if Position = 0 then
         P.Position := Abstract_List'Class (Params).Length + 1;
      end if;
      Abstract_List'Class (Params).Add_Parameter (P);
   end Bind_Param;

   procedure Bind_Param (Params    : in out Abstract_List;
                         Position : in Natural;
                         Value    : in Ada.Calendar.Time) is
      P : Parameter := Parameter '(T         => T_DATE,
                                   Len       => 0,
                                   Value_Len => 0,
                                   Name      => "",
                                   Position  => Position,
                                   Time      => Value);
   begin
      if Position = 0 then
         P.Position := Abstract_List'Class (Params).Length + 1;
      end if;
      Abstract_List'Class (Params).Add_Parameter (P);
   end Bind_Param;

   procedure Bind_Param (Params   : in out Abstract_List;
                         Position : in Natural;
                         Value    : in ADO.Blob_Ref) is
      P : Parameter := Parameter '(T         => T_BLOB,
                                   Len       => 0,
                                   Value_Len => 0,
                                   Name      => "",
                                   Position  => Position,
                                   Data      => Value);
   begin
      if Position = 0 then
         P.Position := Abstract_List'Class (Params).Length + 1;
      end if;
      Abstract_List'Class (Params).Add_Parameter (P);
   end Bind_Param;

   procedure Bind_Null_Param (Params   : in out Abstract_List;
                              Position : in Natural) is
      P : Parameter := Parameter '(T         => T_NULL,
                                   Len       => 0,
                                   Value_Len => 0,
                                   Name      => "",
                                   Position  => Position);
   begin
      if Position = 0 then
         P.Position := Abstract_List'Class (Params).Length + 1;
      end if;
      Abstract_List'Class (Params).Add_Parameter (P);
   end Bind_Null_Param;

   procedure Bind_Null_Param (Params   : in out Abstract_List;
                              Name     : in String) is
      P : constant Parameter := Parameter '(T         => T_NULL,
                                            Len       => Name'Length,
                                            Value_Len => 0,
                                            Name      => Name,
                                            Position  => 0);
   begin
      Abstract_List'Class (Params).Add_Parameter (P);
   end Bind_Null_Param;

   procedure Add_Param (Params : in out Abstract_List;
                        Value  : in Boolean) is
   begin
      Params.Bind_Param (Position => 0, Value => Value);
   end Add_Param;

   procedure Add_Param (Params : in out Abstract_List;
                        Value : in Long_Long_Integer) is
   begin
      Params.Bind_Param (Position => 0, Value => Value);
   end Add_Param;

   procedure Add_Param (Params : in out Abstract_List;
                        Value : in Identifier) is
   begin
      Params.Bind_Param (Position => 0, Value => Value);
   end Add_Param;

   procedure Add_Param (Params : in out Abstract_List;
                        Value : in Integer) is
   begin
      Params.Bind_Param (Position => 0, Value => Value);
   end Add_Param;

   procedure Add_Param (Params : in out Abstract_List;
                        Value : in String) is
   begin
      Params.Bind_Param (Position => 0, Value => Value);
   end Add_Param;

   procedure Add_Param (Params : in out Abstract_List;
                        Value : in Unbounded_String) is
   begin
      Params.Bind_Param (Position => 0, Value => Value);
   end Add_Param;

   procedure Add_Param (Params : in out Abstract_List;
                        Value : in Ada.Calendar.Time) is
   begin
      Params.Bind_Param (Position => 0, Value => Value);
   end Add_Param;

   --  ------------------------------
   --  Add a null parameter.
   --  ------------------------------
   procedure Add_Null_Param (Params : in out Abstract_List) is
   begin
      Params.Bind_Null_Param (Position => 0);
   end Add_Null_Param;

   --  ------------------------------
   --  Expand the SQL string with the query parameters.  The following parameters syntax
   --  are recognized and replaced:
   --  <ul>
   --     <li>? is replaced according to the current parameter index.  The index is incremented
   --         after each occurrence of ? character.
   --     <li>:nnn is replaced by the parameter at index <b>nnn</b>.
   --     <li>:name is replaced by the parameter with the name <b>name</b>
   --     <li>$group[var-name] is replaced by using the expander interface.
   --  </ul>
   --  Parameter strings are escaped.  When a parameter is not found, an empty string is used.
   --  Returns the expanded SQL string.
   --  ------------------------------
   function Expand (Params : in Abstract_List'Class;
                    SQL    : in String) return String is

      --  Format and append the date to the buffer.
      procedure Append_Date (Buffer : in out Unbounded_String;
                             Time   : in Ada.Calendar.Time);

      --  Replace the given parameter.
      procedure Replace_Parameter (Param : in Parameter);

      --  Find and replace the parameter identified by the name.
      procedure Replace_Parameter (Name : in String);

      --  Find and replace the parameter at the given index.
      procedure Replace_Parameter (Position : in Natural);

      --  Find and replace the variable by using the expander and looking up in the
      --  group identified by <tt>Group</tt> a value with the name <tt>Name</tt>.
      procedure Replace_Expander (Group : in String;
                                  Name  : in String);

      --  ------------------------------
      --  Format and append the date to the buffer.
      --  ------------------------------
      procedure Append_Date (Buffer : in out Unbounded_String;
                             Time   : in Ada.Calendar.Time) is
      begin
         Append (Buffer, "'");
         Append (Buffer, Ada.Calendar.Formatting.Image (Time, True));
         Append (Buffer, "'");
      end Append_Date;

      Max    : constant Natural := Params.Length;
      Buffer : Unbounded_String;

      --  ------------------------------
      --  Replace the given parameter.
      --  ------------------------------
      procedure Replace_Parameter (Param : in Parameter) is
      begin
         case Param.T is
            when T_LONG_INTEGER =>
               Append (Buffer, Util.Strings.Image (Param.Long_Num));

            when T_INTEGER =>
               Append (Buffer, Util.Strings.Image (Param.Num));

            when T_LONG_FLOAT =>
               Append (Buffer, Long_Float'Image (Param.Float));

            when T_BOOLEAN =>
               Params.Dialect.Escape_Sql (Buffer, Param.Bool);

            when T_DATE =>
               Append_Date (Buffer, Param.Time);

            when T_NULL =>
               Append (Buffer, "NULL");

            when T_TOKEN | T_LIST =>
               Append (Buffer, Param.Str);

            when T_BLOB =>
               Params.Dialect.Escape_Sql (Buffer, Param.Data);

            when others =>
               Params.Dialect.Escape_Sql (Buffer, Param.Str);
         end case;
      end Replace_Parameter;

      --  ------------------------------
      --  Find and replace the parameter at the given index.
      --  ------------------------------
      procedure Replace_Parameter (Position : in Natural) is
      begin
         if Position = 0 or else Position > Max then
            Log.Warn ("Invalid parameter '{0}' in query '{1}'",
                      Natural'Image (Position), SQL);
         else
            Params.Query_Element (Position => Position,
                                  Process  => Replace_Parameter'Access);
         end if;
      end Replace_Parameter;

      --  ------------------------------
      --  Find and replace the parameter identified by the name.
      --  ------------------------------
      procedure Replace_Parameter (Name : in String) is

         --  Check if the parameter matches and replace it.
         procedure Process (Param : in Parameter);

         Found : Boolean := False;

         --  ------------------------------
         --  Check if the parameter matches and replace it.
         --  ------------------------------
         procedure Process (Param : in Parameter) is
         begin
            if Param.Name = Name then
               Replace_Parameter (Param);
               Found := True;
            end if;
         end Process;

      begin
         --  We assume that in most queries, the number of parameters is relatively small
         --  (1, 2 or 3) and even complex queries should not have a lot of parameters.
         --  To avoid the cost and complexity of hash map, we use a simple search.
         for I in 1 .. Max loop
            Params.Query_Element (I, Process'Access);
            if Found then
               return;
            end if;
         end loop;

         Log.Warn ("Parameter '{0}' not found in query '{1}'", Name, SQL);
      end Replace_Parameter;

      --  ------------------------------
      --  Find and replace the variable by using the expander and looking up in the
      --  group identified by <tt>Group</tt> a value with the name <tt>Name</tt>.
      --  ------------------------------
      procedure Replace_Expander (Group : in String;
                                  Name  : in String) is
      begin
         if Params.Expander = null then
            Log.Warn ("There is no expander to evaluate ${0}[{1}]", Group, Name);
         else
            Replace_Parameter (Params.Expander.Expand (Group, Name));
         end if;

      exception
         when others =>
            Log.Warn ("No value ${0}[{1}] in the cache expander", Group, Name);

      end Replace_Expander;

      Num       : Natural := 0;
      Pos       : Natural;
      C         : Character;
      First_Pos : Natural;
   begin
      --  Build the SQL query by injecting the parameters.
      Pos := SQL'First;
      First_Pos := Pos;
      while Pos <= SQL'Last loop
         C := SQL (Pos);
         if C = '\' then
            if First_Pos <= Pos - 1 then
               Append (Buffer, SQL (First_Pos .. Pos - 1));
            end if;
            First_Pos := Pos + 1;
            exit when Pos + 1 > SQL'Last;
            Pos := Pos + 2;

         elsif C = ':' and then Pos + 1 <= SQL'Last then
            if First_Pos <= Pos - 1 then
               Append (Buffer, SQL (First_Pos .. Pos - 1));
            end if;
            Pos := Pos + 1;
            C := SQL (Pos);
            if C >= '0' and then C <= '9' then
               Num := 0;
               loop
                  Num := Num * 10 + Natural (Character'Pos (C) - Character'Pos ('0'));
                  Pos := Pos + 1;
                  exit when Pos > SQL'Last;
                  C := SQL (Pos);
                  exit when not (C >= '0' and then C <= '9');
               end loop;
               Replace_Parameter (Position => Num);
               First_Pos := Pos;

            else
               declare
                  Start_Pos : constant Natural := Pos;
               begin
                  --  Isolate the parameter name.
                  loop
                     exit when not (C >= 'a' and then C <= 'z')
                       and then not (C >= 'A' and then C <= 'Z')
                       and then not (C >= '0' and then C <= '9')
                       and then C /= '_';
                     Pos := Pos + 1;
                     exit when Pos > SQL'Last;
                     C := SQL (Pos);
                  end loop;

                  --  And replace it with its value.
                  Replace_Parameter (Name => SQL (Start_Pos .. Pos - 1));
                  First_Pos := Pos;
               end;
            end if;

         elsif C = '?' then
            if First_Pos <= Pos - 1 then
               Append (Buffer, SQL (First_Pos .. Pos - 1));
            end if;
            Num := Num + 1;
            Replace_Parameter (Position => Num);
            Pos := Pos + 1;
            First_Pos := Pos;

         elsif C = '$' then
            if First_Pos <= Pos - 1 then
               Append (Buffer, SQL (First_Pos .. Pos - 1));
            end if;

            --  We have a variable to lookup in a cache with the form: $group[var-name]
            Pos := Pos + 1;
            declare
               Group_Start : constant Natural := Pos;
               Group_End   : Natural;
            begin
               --  Isolate the parameter name.
               loop
                  C := SQL (Pos);
                  exit when C = '[';
                  Pos := Pos + 1;
                  exit when Pos > SQL'Last;
                  C := SQL (Pos);
               end loop;
               if C = '[' then
                  Group_End := Pos;
                  loop
                     C := SQL (Pos);
                     exit when C = ']';
                     Pos := Pos + 1;
                     exit when Pos > SQL'Last;
                     C := SQL (Pos);
                  end loop;
                  if C = ']' then
                     Replace_Expander (Group => SQL (Group_Start .. Group_End - 1),
                                       Name  => SQL (Group_End + 1 .. Pos - 1));
                     Pos := Pos + 1;
                  end if;
               end if;

               --  And replace it with its value.
               First_Pos := Pos;
            end;

         else
            Pos := Pos + 1;
         end if;
      end loop;
      if First_Pos <= SQL'Last then
         Append (Buffer, SQL (First_Pos .. SQL'Last));
      end if;
      return To_String (Buffer);
   end Expand;

   function Compare_On_Name (Left, Right : in Parameter) return Boolean is
   begin
      return Left.Name = Right.Name;
   end Compare_On_Name;

   --  ------------------------------
   --  Add the parameter in the list.
   --  ------------------------------
   overriding
   procedure Add_Parameter (Params : in out List;
                            Param  : in Parameter) is
      Pos : Parameter_Vectors.Extended_Index;
   begin
      if Param.Position = 0 then
         Pos := Params.Params.Find_Index (Param);
         if Pos /= Parameter_Vectors.No_Index then
            Params.Params.Replace_Element (Index    => Pos,
                                           New_Item => Param);
         else
            Params.Params.Append (Param);
         end if;
      elsif Param.Position = Natural (Length (Params.Params)) + 1 then
         Params.Params.Append (Param);
      else
         Params.Params.Replace_Element (Index    => Param.Position,
                                        New_Item => Param);
      end if;
   end Add_Parameter;

   --  ------------------------------
   --  Return the number of parameters in the list.
   --  ------------------------------
   overriding
   function Length (Params : in List) return Natural is
   begin
      return Natural (Length (Params.Params));
   end Length;

   --  ------------------------------
   --  Clear the list of parameters.
   --  ------------------------------
   overriding
   procedure Clear (Params : in out List) is
   begin
      Parameter_Vectors.Clear (Params.Params);
   end Clear;

   --  ------------------------------
   --  Set the parameters from another parameter list.
   --  ------------------------------
   overriding
   procedure Set_Parameters (Params : in out List;
                             From   : in Abstract_List'Class) is
      L : constant List'Class := List'Class (From);
   begin
      Params.Params := L.Params;
   end Set_Parameters;

   --  ------------------------------
   --  Return the parameter at the given position
   --  ------------------------------
   overriding
   function Element (Params   : in List;
                     Position : in Natural) return Parameter is
   begin
      return Element (Params.Params, Position);
   end Element;

   --  ------------------------------
   --  Execute the <b>Process</b> procedure with the given parameter as argument.
   --  ------------------------------
   overriding
   procedure Query_Element (Params   : in List;
                            Position : in Natural;
                            Process  : not null access procedure (Element : in Parameter)) is
   begin
      Query_Element (Params.Params, Position, Process);
   end Query_Element;

end ADO.Parameters;