How to rename protected object entry - rename

I want to rename Synchronized_Queue_Interface Enqueue entry, but don’t understand how to do it right.
with Ada.Containers.Synchronized_Queue_Interfaces;
with Ada.Containers.Unbounded_Synchronized_Queues;
package Test is
use Ada.Containers;
package Boolean_Queue_Interfaces is new Synchronized_Queue_Interfaces
(Element_Type => Boolean);
package Boolean_Queues is new Unbounded_Synchronized_Queues
(Queue_Interfaces => Boolean_Queue_Interfaces);
Queue : Boolean_Queues.Queue;
procedure Enqueue (New_Item : Boolean) renames Queue.Enqueue; -- Illegal
end Test;

The form you need to use is this:
Protected Type Example is
Entry Queue( Item : Integer );
Private
Element : Integer := 0;
End Example;
Protected Body Example is
Entry Queue( Item : Integer ) when true is
Begin
Element:= Item;
End Queue;
End Example;
V : example;
Procedure Q( X : Integer )
renames V.Queue;
This is what you have, however it looks like the compiler is not able to tell the overloaded forms apart (Boolean_Queue_Interfaces.Queue.Enqueue vs Boolean_Queues.Queue.Enqueue). -- The best way to resolve this is to be explicit:
procedure Do_it( Item : Boolean )
renames Boolean_Queues.Queue(Queue).Enqueue;
You should probably file a bug report, certainly asking for better error-messaging.

Related

PostgreSQL last inserted ID not auto-increment typescript sequelize

User(
id :number ,
name : string pk)
How can I get the biggest latest id to insert a new one? The objective is to take the previous id and add a value but the id is not auto-increment. I was thinking about making a query to get the latest id but I hope there may be other optimized suggestion
thanks
You can do what you want. But just because you can do something does not mean you should. I repeat you should use a serial or identity. #Stefanov.sm is corrent in a MVCC environment this is a very bad idea; because it is a virtual guarantee you will get a duplicate at some point.
One way to get this is to write a function to do the insert. This function get the MAX(ID) + an increment. It also handles the duplicate error and retries the insert if needed.
create or replace
function establish_user (user_name_in text )
returns integer
language plpgsql
as $$
declare
k_id_inc constant integer := 1;
l_id integer;
l_inserted boolean = false;
begin
select coalesce(max(id) + k_id_inc)
into l_id
from users;
while not l_inserted
loop
begin
insert into users (id, name)
values (l_id, user_name_in);
l_inserted = true;
exception
when others then
if sqlstate != '23505'
or sqlerrm not like '%users_id_uk%'
then
raise;
else
l_id = l_id + 1;
end if;
end;
end loop;
return l_id;
end;
$$;
See Demo here.

How get property name from instance class

I need get property name as string from object class. It is possible in Delphi?
I need property transfer as argument of method and get property name as string. I don't want use property name as argument because the compiler does not catch error when name of property is changed in class.
type
TMyClass = class
private
fField: some_type;
public
property Field:some_type read fField;
end;
function GetPropertyName(arg: ??):string
begin
Result := arg.PropertyName; // here I need get property name form transfer type
end;
var
obj: TMyClass;
name: string;
begin
name := GetPropertyName(obj.Field);
end;
To clarify, as discussed in comments, I'm looking for a direct equivalent to the C# nameof function.
From the comments you make it clear that you are looking for a Delphi equivalent to the C# nameof function.
No such equivalent exists in Delphi, and the language does not have facilities for you to create it yourself. Instead you will need to name the method as a string literal in code.

Inno Setup function CheckItem syntax and usage

Following on from my question Inno Setup disable component selection when a specific component is selected, I think there may be a way to get this to work without the problem of checked states set in code being permanent (though use of the Checked property) by instead using:
function CheckItem(const Index: Integer; const AOperation: TCheckItemOperation): Boolean;
in TNewCheckListBox, however I am having trouble getting the syntax correct. I am trying:
CheckItem(CompIndexSync, coUncheck) := Checked[CompIndexClient];
where the CompIndexes are constants assigned to the indexes for the component values. I get an Identifier Expected error at compile. Can someone advise how to correctly use this function and what I am doing wrong?
The CheckItem member of the TNewCheckListBox class is a method of type function which updates the checked state by the AOperation operation and returns True if any changes were made to the state of the item at Index, or any of its children. Here is its prototype (source):
function TNewCheckListBox.CheckItem(const Index: Integer;
const AOperation: TCheckItemOperation): Boolean;
The problem was that you were trying to assign a value to the function result. That's not what you can do in Pascal language in general.
What you want to do with the item(s) is passed by the AOperation parameter. In pseudocode e.g.:
var
CheckList: TNewCheckListBox;
Operation: TCheckItemOperation;
begin
...
if ShouldCheck then
Operation := coCheck
else
Operation := coUncheck;
if CheckList.CheckItem(ItemIndex, Operation) then
MsgBox('An item has changed its state.', mbInformation, MB_OK);
end;

Delphi - accesing non UI units from inside a thread

I have the following situation.
We develop in DelphiXE.
We are putting the majority of our functions in a DATAMODULE.
function1 (database, transaction, paramInteger) : float
for example
function1 take parameters database (TIBDATABASE), the transaction TIBTRANSACTIOn and aditiona parameter integer. and return a float
function GetLastPretAch(DIBase : TIBDatabase; Tran : TIBTransaction; const aID : Integer) : Double;
var workQuery : TIBQuery;
begin
try
workQuery := TIBQuery.Create(Application);
try
workQuery.Close;
workQuery.Database := DIBase;
workQuery.Transaction := Tran;
workQuery.SQL.Clear;
workQuery.SQL.Add('SELECT * FROM GETLASTPRETACH(-1, :AARTNR)');
workQuery.ParamByName('AARTNR').AsInteger := aID;
workQuery.Open;
Result := workQuery.FieldByName('LASTPRET').AsFloat;
except
on e : Exception do begin
raise EMagisterException.Create(TranslateIbError(e));
end;
end;
finally
FreeAndNil(workQuery);
end;
end;
Now I want to use this functions from a thread. is this thread safe?
inside execute procedure like
ID := GetLastPretAch(database, transaction, 1);
is or not thread safe?
The answer to your question is Yes, you can use that function from inside a worker thread's execute procedure. You might want to consider refining your SQL to only SELECT the field LASTPRET instead of SELECT *.
For an extended discussion on what "thread safe" means refer to this SO question
What does threadsafe mean?
Looks like you're using IBX Components which, the last time I looked were most definitely NOT thread-safe. If you switched to a data access layer that was thread-safe, you should be fine with that code. FYI UIB (Unified Interbase components) are thread-safe.

Delphi - TQuery .AsString to return 0 or 1 for Boolean field values

How can i get Delphi 7 to return a '0' or '1' when the FieldType of a TQuery descendant's Field is a ftBoolean? By default this returns 'TRUE' or 'FALSE', that is
Query1.Fields[0].AsString would return '0', not 'FALSE'
Use
(Query1.Fields[0] as TBooleanField).DisplayValues := 'TRUE;FALSE';
to set a string in the form of 'TRUE;FALSE' (or '1;0'). This allows you to define what values AsString will return.
If you added the field in design time, and/or you got yourself a boolean field component, you can use that too, without the typecast:
Query1YourBooleanField.DisplayValues := 'TRUE;FALSE';
By the way, it's not the query that returns '0', not is it the query that 'is' ftBoolean. These are the fields in the query that represent fields in the table or query result set.
Why not simple use Query1.Fields[0].AsInteger if you want a numerical representation for the boolean ? I think that should work ...
Modify STextFalse and STextTrue resourcestrings in 'dbconsts.pas'. You can put a modified version of the file to your project folder, or go about just like localizing your application.
If you want to modify the strings at run-time you can use the below (credit):
[...]
implementation
uses
dbconsts;
{$R *.dfm}
procedure SetResourceString(ResStringRec: pResStringRec; NewStr: string);
var
OldProtect: DWORD;
begin
if ResStringRec = nil then
Exit;
VirtualProtect(ResStringRec, SizeOf(ResStringRec^),
PAGE_EXECUTE_READWRITE, #OldProtect) ;
ResStringRec^.Identifier := Integer(NewStr) ;
VirtualProtect(ResStringRec, SizeOf(ResStringRec^), OldProtect, #OldProtect) ;
end;
const
TextFalse = '0';
TextTrue = '1';
procedure TForm1.FormCreate(Sender: TObject);
begin
SetResourceString(#STextFalse, TextFalse);
SetResourceString(#STextTrue, TextTrue);
[...]
Personally I use this trick in such situations:
const DigitBool: array[Boolean] of string = ['0', '1'];
//and than
Caption := DigitBool[Query1.Fields[0].Value];
Unfortunately it appears that the only way to do this is to check for every TField:
if (Query1.Fields[i] is TBooleanField) and then use one of the methods provided above.
There is no global TField.AsString hack as far as I'm aware.

Resources