Generate wrapper for oracle sequence?

Posts   
 
    
happyfirst
User
Posts: 215
Joined: 28-Nov-2008
# Posted on: 06-Jan-2009 21:25:39   

I see how I can add stored procedures to generate objects that I can then use from C#. But I don't see how to generate a wrapper for a sequencer. The sequences are not showing up in my schema. Is this possible? From code, I need to call a sequencer to get the next id.

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 07-Jan-2009 03:33:53   

The sequences are not showing up in my schema

Please provide more info to understand why sequences aren't showed.

From code, I need to call a sequencer to get the next id.

You can write a stored procedure that wraps the sequence call,

CREATE OR REPLACE FUNCTION NextID() RETURN NUMBER IS
BEGIN 
     SELECT sequence_test.nextval into n from dual;
     RETURN n;
END;

Then, you can map this function as a stored procedure at LLBLGen Designer and call it from your code.

David Elizondo | LLBLGen Support Team
happyfirst
User
Posts: 215
Joined: 28-Nov-2008
# Posted on: 07-Jan-2009 16:50:29   

Ver 2.6 - 10/6/08
Oracle 11g - .Net 3.5 - Self-Servicing

I can associate a table column key with a sequencer using the designer. That works and shows me all the sequences.

But in this case, I need to access the sequencer directly from C#, and didn't think I'd need to go through the headache of wrapping the sequences with stored procs. I assumed that I would just be able to add the sequence as some type of object and then I could access it just like a stored proc.

Walaa avatar
Walaa
Support Team
Posts: 14993
Joined: 21-Aug-2005
# Posted on: 07-Jan-2009 17:15:05   

I don't see why you need to access the sequence yourself as LLBLGen Pro should do that for you.

LLBLGen Pro will Query for the NEXTVAL of the serquence before inserting a new row, or if you have a trigger based sequences, the you should check the below qouted part of the docs.

Trigger based sequence values (Oracle/Firebird) Some Oracle schemas or Firebird databases use triggers to set the sequence value when a record is inserted. LLBLGen Pro's generated code for Oracle or Firebird uses a second query to first determine the next value of the sequence (using NEXTVAL on Oracle, using 1 instead of 0 in GEN_ID on Firebird), then pass that value to the insert query as a normal value. If that code is then used to insert a row in a table with a trigger for sequence values, the trigger will update the inserted value with a new value, making the value returned to the LLBLGen Pro runtime core not the value the trigger inserted into the sequenced field. To overcome this, the Oracle and Firebird Dynamic Query Engines (DQE) can be configured to execute a sequence retrieval query which checks for the current value (using CURRVAL on Oracle, using 0 instead of 1 in GEN_ID on Firebird) after the insert took place, instead of the sequence retrieval query which checks for the next value before the insert takes place.

To do that, add the following tag to the configuration's appSettings section:

Oracle Firebird <add key="OracleTriggerSequences" value="true" /> <add key="FirebirdTriggerSequences" value="true" />

A value of false or omitting the tag will cause normal behavior, a sequence call which asks for the next value before the insert.

Either way you should make sure the correct sequence is set for the correct field in the LLBLGen Pro Designer.

happyfirst
User
Posts: 215
Joined: 28-Nov-2008
# Posted on: 07-Jan-2009 17:22:34   

In this case, I was using the sequence value for something else, not a field in a table. It's the first param to a set of stored procudures I need to run.

happyfirst
User
Posts: 215
Joined: 28-Nov-2008
# Posted on: 07-Jan-2009 17:26:35   

Although you do bring up an side related issue I just ran into as I'm now getting into saving records. We have trigger based sequences and the ID was NOT coming back to the C# client. Our triggers set the ID if one is not supplied. Once I attached the sequencer to that column in your designer then it worked (but then our trigger wasn't really doing anything).

So setting <add key="OracleTriggerSequences" value="true" /> will allow me to keep using my triggers and the new ID will be returned to the C# client? That would be nice as then I woudn't need to associate all those sequences. And this is safe regardless of how many records are getting inserted at the time?

thanks!

happyfirst
User
Posts: 215
Joined: 28-Nov-2008
# Posted on: 07-Jan-2009 17:41:04   

I just added <add key="OracleTriggerSequences" value="true" /> to my web.config and removed the association to the sequencer in the designer and regenerated

but now my record after saving, comes back with ID=0

Walaa avatar
Walaa
Support Team
Posts: 14993
Joined: 21-Aug-2005
# Posted on: 07-Jan-2009 17:51:30   

No you got me wrong, I said:

Either way you should make sure the correct sequence is set for the correct field in the LLBLGen Pro Designer.

You should define the sequence in the Designer so LLBLgen Pro knows which sequence to retrive after the trigger effect. LLBLGen Pro then would execute a sequence retrieval query which checks for the current value (using CURRVAL on Oracle)

happyfirst
User
Posts: 215
Joined: 28-Nov-2008
# Posted on: 07-Jan-2009 17:58:18   

Ah, got it. So either way I need to manually associate. So then I don't really see the difference between before or after since our triggers first check to see if they need to act.

Walaa avatar
Walaa
Support Team
Posts: 14993
Joined: 21-Aug-2005
# Posted on: 07-Jan-2009 18:04:05   

If your triggers are smart enough not to do anything if a value was introduced, then you should do nothing at all except specifying the correct sequences to be used in the designer. And then the triggers might be considered as never existed before. simple_smile