Seaking In the lightweight data persistence layer. in the PL introduction and suggestion set, the seaking. PL (hereinafter referred to as PL) is briefly introduced, and the usage of the main object session in PL is described. This article describes another important object query in PL. (Some of the terms and examples in this article are from the introduction of seaking. pl in the lightweight data persistence layer and the collection of suggestions)
Query is a data query object and does not have Session Object operation functions. However, query functions are more powerful, including View query and join and condition), order (SORT), alias (alias) and so on.
Instructions for use:
1. Declaration and instantiation.
Like session, a query object cannot be instantiated using new, but must be generated by a dataprovider object.
Query = DP. newquery (employee. tablename );
The parameter can be any data table name or view name. In the example, "employee. tablename" is the name of the table corresponding to the employee object class, which is equivalent to the string "employee ". To use the table alias, you can declare it as follows:
Query = DP. newquery (employee. tablename, " Em " );
You can also specify the tablename attribute value and tablealias attribute value of the query after declaring the query instance:
Query = DP. newquery ();
Query. tablename = Employee. tablename;
Query. tablealias = " Em " ;
If the table alias is not specified, the table alias is the table name (the same below ).
2. Add the Field List (fields) in the query result ).
Query. Fields. Add (employee. fieldname_id );
Query. Fields. Add (employee. fieldname_name );
A parameter is the field name of a data table or view. If you need to specify the field alias, you can declare it as follows:
Query. Fields. Add (employee. fieldname_id,"Emid");
Query. Fields. Add (employee. fieldname_name,"Emname");
When you use join queries, aliases of tables (or views) and fields are very useful.
If no field list is added, query automatically adds all fields by default.
3. Add query conditions ).
A query object has a very important attribute conditions, which is used to specify query conditions. The use of conditions is relatively complex and will be described separately in the next article.
4. Add the sorting fields and the sorting method (orders ).
Query. Orders. Add (employee. fieldname_id );
The parameter is the name of the sorting field. By default, query is sorted in ascending order (ASC). If you use descending order, you must explicitly specify:
Query. Orders. Add (employee. fieldname_id, orderkind. DESC );
You can add multiple sorting fields in the order of first and second, respectively ...... Sort fields.
5. join query.
Query. Joins. Add ( " Sale " );
The parameter is the name of the data table or view to be joined. If you need to specify an alias, you can declare it as follows:
Query. Joins. Add ( " Sale " , " Mysale " );
For easy differentiation, we name the table (or view) referred to by the query tablename as the primary table, and the table (or view) referred to by the join here as the join table.
The default join mode of query is inner join. Other join modes must be specified, for example:
Query. Joins [ " Mysale " ]. Joinkind = Joinkind. leftouterjoin;
Each join must have at least one oncondition condition. Otherwise, an exception is thrown.
Query. Joins [ " Mysale " ]. Addoncondition ( " ID " , " Employeeid " );
The first parameter is the field name of the primary table, and the second parameter is the field name of the joined table. The default query join method is equivalent join. Other join methods must be explicitly specified, for example:
Query. Joins [ " Mysale " ]. Addoncondition ( " ID " , " Employeeid " , Comparekind. Greater );
You can add a field list and query conditions to a join table like a primary table, for example:
Query. Joins [ " Mysale " ]. Fields. Add ( " Saledate " );
Query. Joins [ " Mysale " ]. Fields. Add ( " Salesum " );
You can also sort the query results based on the joined table fields. Unlike the field list and query conditions, the sorting fields of the joined table should be specified in the query, for example:
Query. Orders. Add ( " Mysale " , " Saledate " , Orderkind. DESC );
It can be seen that the operation to add a sorting field is consistent with that of the master table, but the table name must be specified. In fact, this format can also be used to add the sorting field of the primary table, but the table name of the primary table can be omitted.
One or more joins can be added for each query.
6. Get or fill the dataset.
After completing one or more of the preceding tasks as needed, you can obtain or fill the dataset to obtain the expected results.
Dataset = Query. getdataset ();
Or
Dataset = New Dataset ();
Query. filldataset (Dataset );
The table (datatable) in the dataset is named tablealias specified in the query.
Others:
1. Although the query object is relatively powerful, it still cannot meet the requirements of some complex queries. We recommend that you create a view for a complex query.
2. To facilitate user debugging, the query object provides the getselectstring () method to view the query string generated by the query.
End
After the introduction of seaking. pl in the persistence layer of lightweight data and the release of the recommendation set, many friends paid attention to it and expressed their views. Thank you for your attention. At the same time, you are welcome to continue to follow seaking. pl and provide valuable suggestions!