SmartPersistencelayer 2.0 (4) --- Criteria Operations

xiaoxiao2021-03-06  45

SmartPersistencelayer 2.0 criteria operations

concept

In the previous article, the operation of entity Entity is described, it is obvious that only the actual operation is far less than enough. Such as: We often get record sets from the database and bind to the database to the DataGrid, which will make batch of UPDATE and DELETE according to the conditions. To this end, in SPL introduced the concept of criteria (standard), then design in SPL design Three criteria:

RetrieVecriteria (Get Standard)

This standard is based on query conditions, sorting methods to obtain data that meets the conditions, and can be returned in a DataTable, entity set. This standard will generate "SELECT * from the TB Where ... Order ..." statement execution

UpdateCriteria (Update Standard)

This standard is a bulk update based on the conditions, and eventually generates "Update TB Set ... where .."

Deletecriteria (Delete)

This standard is a mass-deleted deletion according to the condition, and eventually generates "Delete from TB Where ..." execution.

These standards are for a single entity, which is for a table, of course, these operations have been sufficient for Update and Delete, and we often query multi-table for SELECT, so on this joint query Will explain in the next "Advanced Query".

RetrieVecriteria Get Standard

This RETRIEVECRITERIA is a query for a single entity, which can define query conditions, sorting the way and return it. Here I will take an example: We check the second grade surname Liu's student number P DententityRia rc = new retrievecriteria (); And sequel

Retrievecriteria Rc = New RetrieVecriteria (TypeEnTentity); // Installation

Condition c = rc.getnewcondition (); // instantiate a condition

C.Addequalto (studententity .__ grade, 2); // second grade conditions

C.addmatchprefix (studententity .__ name, 'Liu'); // Match 'Liu'

C.ORDERBY (studEntentity .__ no); // According to the students

DataTable DT = rc.ASDataTable (); // Returns in a DataTable mode

Step analysis:

1. Instantiate a Retrievecriteria to indicate which entity class, use "TypeOf" "

2. New a condition (condition), you can use this method New out of multiple Condition, multiple conditions are the relationship between "OR"

3. Give the condition specified, such as addMatchprefix, addMatchprefix, about Condition will be specifically introduced

4. Use the Orderby () method to specify the sort mode, default is ascending, if you want to use sequencing: OrderBy (studentent, .__ no, false)

5. Finally, returning to DataTable in the ASDATABLE, which can make it easy to perform DataGrid data binding

Many ways to return

In the example, DataTable is used to return, which is also the most commonly used in the SPL, and there are other ways: EntityContainer entity set

This means that the return result is reflected in the collection of entities, and the operation of the entity set is very simple:

EntityContainer EC = rc.asentityContainer ();

For (int i = 0; i

{

StudENTentity Student = (studententity) EC [i];

....// After getting the entity, you can do your own operation.

}

ObjectCursor cursor

The SPL also provides a way of cursors return, which is similar to the entity set, which is a compatible reservation for the original version:

ObjectCursor OC = rc.asobjectCursor ();

IF (Oc.hasObject ())

{

StudENTENTITY STUDENT = (studententity) oc.nextObject ();

.... // After getting the entity, you can do your own operation.

}

After returning with the cursor, it must be judged by HASObject () before the operation is performed, so as not to throw an abnormality.

EntityObject single entity

SPL adds the return mode of the EntityObject single substantio in 2.0, everyone feels strange, Retrievecriteria is back to multi-recorded, what is this single entity. We sometimes encounter a situation, through the "Hou Selected Primary Key", the so-called "Hou Selected Primary Key" is non-primary, but it can be determined to be unique, such as the student number, but in the data sheet Number is not a primary key, you can't get it with Entity automatic RETRIEVE (), and you can use Retrievecriteria to get it, so you can confirm that you are unique, you can use a single body return:

EntityObject EO = rc.asentity ();

IF (eO! = null)

{

StudENTENTITY Student = (EntityObject) EO;

.... // After getting the entity, you can do your own operation.

}

In fact, this way is to take the first information and automatically convert to the EntityObject method, so if it is a plurality of records, use this to take the first entity.

TOP function

In SPL2.0, TOP function is added, we only need to use:

Retrievecriteria rc = new retrievecriteria (TypeEOf (Studentent));

rc.top = 20;

This will return only the top 20 conformity.

This feature only supports SQL, Access, because this is the statement using "SELECT TOP N ...", and Oracle's TOP function is more troublesome, especially after sorting, the first N returns, due to time and test environment, there is no Support for Oracle.

Multi-note

This is also a new function of SPL2.0, as long as you specify the data source name, there are two ways:

Retrievecriteria Rc = New RetrieVecriteria (TypeEOf (Studentent), "DB

2"

);

or:

Retrievecriteira RC = New RetrieVecriteria (TypeEnTentity):

Rc.DatabaseName = "DB

2"

;

This allows you to operate the specified data source, not the default data source.

other

CLEAR () method can clear all Condition

SQLSTRING attributes can return the execution standard SQL statement

Also supports memory storage, which is transparent to the developer, as long as the entity declares in the XML file as IssaveTMemory = TrueUpdatecriteria update standard

The update criteria is for a single entity according to the conditions according to the conditions. I will take an example: upgrade all the second grade students to the third grade.

Updatecriteria uc = new updatecriteria (typeof (studentent)); // instantiate

Condition c = uc.getnewcondition (); // instantiate a condition

C.addequalto (studententity .__ grade, 2); // Specify a second grade student

c. addttributeforupdate (studententity .__ grade, 3); // Update to third grade

Try

{

INT RE = uc.perform (); // Execute, return to the number of affected

IF (re> 0) // If the database is affected

Response.write ("Update" RE.TOSTRING () "Sum Record");

ELSE // Unaffected Database

Response.write ("No Update Record");

} CARTH (PLEXCEPTION EXP) // Capture Exception

{

if (exp.errorType == ErrorTypes.restricterror) // If it is a cascaded update constraint exception

Response.write ("Encounted Grade Update Constrained Abnormal"); // Friendly Tips

Else

Throw Exp; // Throw an exception

}

Step analysis:

1. Al instantly specify a UPDATECRITERIA, which entity type is

2. Create a Condition, you can also get the relationship between "or" between multiple conditions, condition

3. Use the AddAttributeForUpdate (field name, value) to specify that you want to update a field to a value, you can add multiple, due to time problems, the current SPL2.0 does not support "" update "

4. Call Perform () execution, return the number of affected bars, if there is no AddAttributeForUpdate, that is, no setup to update, then return 0

5. Will throw a level of unusual abnormalities in the entertainment, you can capture the friendly tips

Multi-note

Update standards also support multi-books, similar to RetrieVecriteria:

Updatecriteria uc = new updatecriteria (TypeEOf (Studentent), "DB

2"

);

or:

Updatecriteria uc = new updatecriteria (TypeEntent);

uc.DatabaseName = "dB

2"

;

This allows you to update the data in the specified data source.

The SQLString property can return the execution SQL statement to debug statements.

Support memory storage, that is, if the entity is stored, then after executing Update, the memory is automatically updated, transparent to the developer

Support transaction processing

Deletecriteria Delete Standard

The deletion criteria is based on the condition of the data sheet, which is worth reminding, this standard use is very cautious, using improper deletion of data. For example: To delete the fourth grade student

Deletecriteria DC = New DeleteCriteria (TypeEntent);

Condition C = dc.getnewcondition ();

C.ADDEQUALTO (studEntentity .__ grade, 4);

Try

{

INT RE = dc.perform ();

IF (re> 0)

Response.write ("Successfully Remove" Re.toTString () "Sum Record");

Else

Response.write ("No Delete Record");

} catch (plexception exp)

{

IF (exp.errorType == ErrorTypes. Restricter)

Response.write ("encountered cascaded deletion constraints");

Else

Throw Exp;

}

Step analysis:

1. Instantiate the delete standard, you need to specify the entity type

2. Create a delete condition, you can get multiple conditions, and the relationship between "OR"

3. Call Perform () execution, return to the number of affected

4. Capture the class or delete the constraint, friendly tips

Multi-note

This is the same as other standards, there are two ways:

Deletecriteria DC = New DeleteCriteria (TypeEOf (Studentent, "DB

2"

);

or:

Deletecriteria DC = New DeleteCriteria (TypeEntent);

Dc.DatabaseName = "DB

2"

;

This allows you to delete the specified data source.

The SQLString property can return the execution SQL statement to debug statements.

Support memory storage, that is, if the entity is stored, then after executing Delete, the memory is automatically updated, transparent to the developer

Support transaction processing

to sum up:

There is a common point for these standard operations, which are all for single entities, support memory storage functions; Different is that Retrievecriteira can return in a variety of ways, and such a single entity query is for system services. Saying is still far enough, so for the query, will introduce the Joint query function of the Query class later, and these operations are enough for Updatecriteria and DeleteCriteria, which can be dismantled when the business is more complicated. These methods are implemented.

Details and transaction processing about Condition will explain separately.

Listen to

November 2004

MSN: TINTOWN_LIU@hotmail.com

转载请注明原文地址:https://www.9cbs.com/read-80662.html

New Post(0)