ORA-01841 :(full) year must be between -4713 and +9999, and not be 0 when binding to dynamic sql with DBMS_SQL - plsql

When I run (snippets here):
t_cmd := 'begin :1 := other_package.some_function(:0,:1,:2); end;';
l_tmstmp timestamp_unconstrained := to_timestamp('1999-07-07 07:07:07.000000000',
'YYYY-MM-DD HH24:MI:SS.FF9');
DBMS_SQL.PARSE(curid, t_cmd, DBMS_SQL.NATIVE);
-- binding here other attributes
DBMS_SQL.BIND_VARIABLE(curid, ':2', l_tmstmp);
ret := DBMS_SQL.EXECUTE(curid);
I always recieve error, even when try to bind simple timestamp, timestamp from varchar2 (TO_TIMESTAMP('timestamp_in_proper_format'), 'YYYY-MM-DD HH24:MI:SS.FF9')) :
ORA-01841 :(full) year must be between -4713 and +9999, and not be 0
ORA-06512: przy linia 1
ORA-06512: przy "SYS.DBMS_SQL", line 1825
ORA-06512: przy "MY_PACKAGE", line where is :
ret := DBMS_SQL.EXECUTE(curid);
What is interesting problem don't occurs when I debug package.
I tried compile my package :
ALTER PACKAGE my_package COMPILE DEBUG PACKAGE;
but problem still occurs, besides in producation environment it will be compiled with optimization level so I can't do this in that way.
Additionaly I can not use EXECUTE IMMEDIATE because function name, types and number of parameters are known at runtime.
Any idea?

Thank you #OldProgrammer!
When I tried to run simple snippet as you suggested I realized something.
Problem was in this line:
t_cmd := 'begin :1 := other_package.some_function(:0,:1,:2); end;';
I didn't change binding name from this fragment: "begin :1" and now I have two bindings to the same value.
In execute immediate it was ok because there ORDER of bindings is significant, dbms_sql uses names of bindings.
Argument :1 from some_function(:0,:1,:2) is also timestamp.
some_function returns varchar2 so errors occured when it was trying to substitute result to first ":1".
I changed function call to:
t_cmd := 'begin :my_result := other_package.some_function(:0,:1,:2); end;';
And "ORA-01841 :(full) year must be between -4713 and +9999" is gone.
I did not focus on this fragment "begin :1 :=" because tried to get result from function like that:
ret := DBMS_SQL.EXECUTE(curid);
src_cur := DBMS_SQL.TO_REFCURSOR(curid);
FETCH src_cur INTO my_result;
CLOSE src_cur;
but this way getting result from function didn't work (some other errors occured)
Finally I changed above code to:
ret := DBMS_SQL.EXECUTE(curid);
DBMS_SQL.VARIABLE_VALUE(curid, 'my_result', my_result);
DBMS_SQL.CLOSE_CURSOR(curid);
And now everything runs fine.

Related

How do I enforce that a type hold only be a fixed set of non-contiguous values?

I was expecting this program to raise an error when I feed it 3 as a valid Scale value, but no such luck:
with Ada.Text_IO; use Ada.Text_IO;
procedure predicate is
type Scale is new Integer
with Dynamic_Predicate => Scale in 1 | 2 | 4 | 8;
GivesWarning : Scale := 3; -- gives warning
begin
Put_Line ("Hello World");
loop
Put_Line ("Gimme a value");
declare
AnyValue : Integer := Integer'Value (Get_Line);
S : Scale := Scale (AnyValue); -- no check done!
begin
Put_Line ("okay, that works" & S'Image);
end;
end loop;
end predicate;
I found this related question, but there the requirement is to use an enum., and the solution is to define an array from enum -> value.
I want something that gives me at least a warning at compile time, and allows me to check at runtime as well, and that raises an error if I try to put an invalid value in. Then, if I can use SPARK to prove that no invalid values can occur, I could turn off said checks. I was under the impression that this was how Static_ / Dynamic_ predicates work, so the example above took me by surprise.
You need to enable assertions. Either compile with -gnata or set an appropriate Assertion_Policy
pragma Assertion_Policy(Dynamic_Predicate => Check);

How do I convert an unbounded string to an integer?

I am learning Ada (by trying https://adventofcode.com/2018/ problems). To start with, I am trying to develop a number of "utility" packages that will help with text processing etc.
I have successfully written a function that will read from stdin and return an array of Unbounded_Strings for each input line.
I am trying to modify that function to do the same, but instead convert each Unbounded_String to an Integer before insertion into the array.
Here is my package:
get_stdin.ads:
with Ada.Strings.Unbounded;
package get_stdin is
type IntegerArray is array (Natural range <>) of Integer;
function get_ints return IntegerArray;
end get_stdin;
get_stdin.adb:
with Ada.Text_IO;
with Ada.Text_IO.Unbounded_IO;
with Ada.Strings.Unbounded;
with Ada.Strings;
package body get_stdin is
function get_ints return IntegerArray is
Counter : Natural := 0;
Str : Ada.Strings.Unbounded.Unbounded_String;
Arr : IntegerArray(0..10000);
begin
while not Ada.Text_IO.End_Of_File loop
Str := Ada.Text_IO.Unbounded_IO.Get_Line;
Arr(Counter) := Integer'Value(Ada.Strings.Unbounded.To_String(Str));
Counter := Counter + 1;
end loop;
return Arr(0..Counter-1);
end get_ints;
end get_stdin;
I am calling using this package inside this procedure:
procedure d1 is
StdinArr : get_stdin.IntegerArray := get_stdin.get_ints;
begin
null; -- Array processing to follow
end;
This successfully compiles, and I then pipe in my input text file:
me#mypc /cygdrive/c/Users/me/aoc2018
$ cat d1.txt
-6
-1
-18
-10
...etc
me#mypc /cygdrive/c/Users/me/aoc2018
$ cat d1.txt | ./d1.exe
raised CONSTRAINT_ERROR : bad input for 'Value: "-6"
"-6" is the first value in the text file. My string-to-integer conversion code was essentially copied from this question.
I am not sure why a bad input error is raised.
It raises the same error if I replace -6 with a positive integer in the file
This is running under Cygwin on Windows 10.
Compiled/linked with gnatmake version 7.3.0
Note: I'm just getting started with Ada so there's probably lots of issues with my code in general.
What am I doing wrong and how can I fix this function/package to return my IntegerArray type correctly filled with Integers?
This was a line endings issue. I was running under cygwin on Windows 10. My text file has Windows-style line endings.
Using dos2unix:
$ cat d1.txt | dos2unix.exe | ./d1.exe
was sufficient to make it work correctly.
If anyone can explain precisely why, that would be interesting. I'm guessing that Get_Line only strips off the \n character, not the \r.

Ada linker not letting me call my procedure

Here is my code:
procedure String_To_Int(str: String) is
str_length : Integer := str'Size / 8;
ASCII_Values_Array: array (Integer range 1 .. str_length) of Integer;
begin
Text_Io.Put_Line(str & " has a length of " & natural'image(str_length));
for x in 1 .. str_length loop
ASCII_Values_Array(x) := Character'Pos(str(x));
Text_Io.Put_Line(natural'image(ASCII_Values_Array(x)));
end loop;
end String_To_Int;
and I am trying to call it with:
String_To_Int(str => "abcdefghijklmnopqrstuvwxyz");
but the compiler is telling me:
Saw '(', expected: , :
And I have no clue what is wrong about how I am calling my procedure. I have looked at many other examples of procedure calls and this looks exactly the same. Any help is appreciated!
There's something you aren't telling us.
./test_sti;
abcdefghijklmnopqrstuvwxyz has a length of 26
97
98
99
...
121
122
Now you don't say what you're actually doing, but here's what I did.
I called String_To_Int from another procedure, in a file test_sti.adb.
with String_To_Int;
procedure test_sti is
begin
String_To_Int(str => "abcdefghijklmnopqrstuvwxyz");
end;
Note that String_To_Int is a separate procedure, in its own file, so the with clause tells the compiler to look for it. I could have declared it locally, i.e. between "is" and "begin" and saved both the files below, but separation is probably better design.
Now anything "with"ed will have both a specification and a body - in this case, specification string_to_int.ads :
procedure String_To_Int(str: String);
and body string_to_int.adb :
with Ada.Text_IO;
use Ada;
procedure String_To_Int(str: String) is
str_length : Integer := str'Size / 8;
ASCII_Values_Array: array (Integer range 1 .. str_length) of Integer;
begin
Text_Io.Put_Line(str & " has a length of " & natural'image(str_length));
for x in 1 .. str_length loop
ASCII_Values_Array(x) := Character'Pos(str(x));
Text_Io.Put_Line(natural'image(ASCII_Values_Array(x)));
end loop;
end String_To_Int;
and to build the lot, simply
gnatmake test_sti.adb
and the compiler works out its own dependencies, no Makefile necessary.
It's actually a bit odd (but perfectly legal) to have a separate "compilation unit" like this for just one procedure. More normally it would either be declared locally, or it would be a part of a package - either a collection of utilities like Ada.Text_IO, or something like a class if you are familiar with Java or C++.
Incidentally, String_To_Int is a very odd procedure : instead of declaring its variables as
str_length : Integer := str'Size / 8;
ASCII_Values_Array: array (Integer range 1 .. str_length) of Integer;
it's cleaner to use the attributes more consistently:
str_length : constant natural := str'Length;
ASCII_Values_Array: array (str'range) of Integer;
and express the loop condition as
for x in str'range loop

Ada compiler chooses incorrect overloaded function

I am using avr-ada to compile my program. I want to right shift an unsigned_16 variable twice.
Interfaces.Shift_Right is overloaded and provides for both Unsigned_8 and Unisgned_16.
When I compile, I get the error 'expected type "Interfaces.Unsigned_8"', 'found type "Interfaces.Unsigned_16"'. I have tried to specify that the input is an Unsigned_16, but it won't work. How do I specify the correct function?
with AVR; use AVR;
with AVR.MCU;
with AVR.Timer0;
with AVR.ADC;
with Interfaces;
use Interfaces;
procedure Flash_Led is
adc_result_10 : Unsigned_16 := 0;
adc_result_8 : Unsigned_8 := 0;
begin
-- set OC0A pin as output, required for output toggling
MCU.DDRD_Bits := (others => DD_Output);
-- set all pins low
MCU.PortD := 16#00#;
-- clear register
MCU.TCCR0B := 16#00#;
-- initialize timer to Clear Timer on Compare, scale the input
-- clock and set a value to compare the timer against.
Timer0.Init_CTC (Timer0.Scale_By_1024, Overflow => 1);
-- Toggle the OC0(A)-Pin on compare match
Timer0.Set_Output_Compare_Mode_Toggle;
-- Initialize ADC
ADC.Init(ADC.Scale_By_64, Ref => ADC.Is_Vcc);
loop -- loop forever
adc_result_10 := ADC.Convert_10bit(Ch => 0);
adc_result_10 := Shift_Right(Unsigned_16'(adc_result_10), 2); --'
adc_result_8 := Unsigned_8(adc_result_10);
Timer0.Set_Overflow_At(adc_result_10);
end loop;
end Flash_Led;
The line:
Timer0.Set_Overflow_At(adc_result_10);
Should have been:
Timer0.Set_Overflow_At(adc_result_8);
I didn't look at the line number in the error message closely enough. Oops.
If you are unsure which function is being called (given that it is overloaded) you can always construct a unique name through renames...
function My16_Shift_Right (Item : in Unsigned_16; Amount : Natural) return Unsigned_16
renames Interfaces.Shift_Right;
and then test the way you are thinking about your program.

Creating objects and calling their methods based on keywords in Ada

This is a followup to my question here.
I got to a point in the program where I felt like I couldn't proceed any further with the current structure, so I did a lot of rewriting. The Statement type is no longer abstract, and each subtype of Statement creates its own instance of Statement's variables. I also removed the abstract execute function from the Statements package because the compiler didn't like that (each subtype will still have its own execute method regardless). The execute function has been changed to a procedure because the incoming Statement type must be modified. I moved statementFactory (formerly createStatement) to the Statement package.
Here is the error I'm getting:
statements-compoundstatements.adb:15:29: expected type "CompoundStatement" defined at statements-compoundstatements.ads:11
statements-compoundstatements.adb:15:29: found type "Statement'Class" defined at statements.ads:6
I'm a beginner at Ada, but my hunch is that because the execute procedure is in CompoundStatements (which is a "subclass" of Statements) it would never be able to see the execute method of another "subclass" of Statements. The only solution I can think of would be to dump all the execute procedures that call an execute procedure into the Statement package, but that seems undesirable. But that still doesn't explain why stmt.all is being used as a type Statement'Class instead of the type created in statementFactory.
Here is the new code:
package Statements is
type Statement is tagged private;
type Statement_Access is access all Statement'Class;
ParserException : Exception;
procedure createStatement(tokens : Vector; S : out Statement);
procedure statementFactory(S: in out Statement; stmt: out Statement_Access);
--.....A bunch of other procedures and functions.....
private
type Statement is tagged
record
tokens : Vector;
executedtokens : Vector;
end record;
end Statements;
procedure createStatement(tokens : Vector; S : out Statement) is
begin
S.tokens := tokens;
end createStatement;
procedure statementFactory(S: in out Statement; stmt: out Statement_Access) is
currenttoken : Unbounded_String;
C : CompoundStatement;
A : AssignmentStatement;
P : PrintStatement;
begin
currenttoken := getCurrentToken(S);
if currenttoken = "begin" then
createStatement(S.tokens, C);
stmt := new CompoundStatement;
stmt.all := Statement'Class(C);
elsif isVariable(To_String(currenttoken)) then
createStatement(S.tokens, A);
stmt := new AssignmentStatement;
stmt.all := Statement'Class(A);
elsif currenttoken = "print" then
createStatement(S.tokens, P);
stmt := new PrintStatement;
stmt.all := Statement'Class(P);
end statementFactory;
package body Statements.CompoundStatements is
procedure execute(skip: in Boolean; C: in out CompoundStatement; reset: out Integer) is
stmt: Statement_Access;
tokensexecuted: Integer;
currenttoken : Unbounded_String;
begin
match(C, "begin");
currenttoken := getCurrentToken(C);
while(currenttoken /= "end") loop
statementFactory(C, stmt);
execute(skip, stmt.all, tokensexecuted); //ERROR OCCURS HERE
You say “I also removed the abstract execute function from the Statements package because the compiler didn't like that”; but you really need it, because otherwise how is the compiler to know that when you call execute (skip, stmt.all, tokensexecuted) that any stmt.all will provide an execute for it to dispatch to?
Not only does the extended type inherit the attributes of its parent type (so each CompoundStatement etc already has tokens and executedtokens), it inherits the primitive operations of the parent; if the parent operation is abstract, the child must provide its own implementation, if not, the child can provide its own (overriding) implementation.
See the Ada 95 Rationale and Wikibooks for good discussions on this.

Resources