Common classes and methods for Windows Phone 7 perst Databases

Source: Internet
Author: User

I. perst. Database Class
Database is a database class provided by perst, which is equivalent to simulating a relational database. Database classes allow you to create/Delete tables, add/delete indexes, create/update/delete records, and write and execute queries. Of course, these are all through the surface

To the object.
Database (storage); // create a database using a storage instance
Database (storage, bool multithreaded); // whether multithreading is supported
Database (storage, bool multithreaded, bool autoregistertables, fulltextsearchhelper helper );
Autoregistertables automatically creates a table when a new instance is inserted into the database.
Type: perst. Fulltext. fulltextsearchhelper global index help

Database. Storage Properties get the storage associated with the database
Whether the database. enableautoindices attribute allows automatic indexing

Int database. countrecords (type table)
Int database. countrecords (type table, bool forupdate)
The total number of records in the returned table. The table is the class you define to store data.
If the table is being updated, set the forupdate parameter to true to prevent deadlock.

Public bool createindex (type table, string key, bool unique)
True indicates that the index is successfully created. False indicates that the index has been created.
Table type
Key table Field
Unique or not

Public bool dropindex (type table, string key) // delete an index

Public bool createtable (type table) // create a table

Public bool droptable (type table) // delete a table

Public bool deleterecord (Object Record)
Public bool deleterecord (type table, Object Record) // deletes an instance of record records

Public ienumerable getrecords (type table)
Public ienumerable getrecords (type table, bool forupdate) // return all records of the table

Public fulltextsearchresult search (fulltextquery query, int maxresults, int timelimit)
Public fulltextsearchresult search (string query, string language, int maxresults, int timelimit) // query

Public fulltextsearchresult searchprefix (string prefix, int maxresults, int timelimit, bool sort)
// Query all records with prefix by prefix

Public ienumerable select (type table, string predicate)
// The SELECT statement equivalent to the SQL statement predicate is the statement after the WHERE clause

Public void updatekey (Object Record, string key, object value)
Public void updatekey (type table, Object Record, string key, object Value) // update a field of a record
 

Ii. perst. Storage Data Storage
Storage storage = storagefactory. instance. createstorage () create a new storage instance
Storage. Open ("perstdemodb. DBs"); // storage opens the database through the database path

Query createquery () // create a query object

Int getoid (Object OBJ) // obtain the unique OID value of the record object

Object getproperty (string name) // obtain the value of a property of the record object
--------------------------------------------------------------------
Use storage and database for database data backup and import XML

Data backup
Storage DB = storagefactory. instance. createstorage ();
DB. Open ("test. DBs", pagepoolsize); // open the database
Filestream stream = new filestream ("test. BCK", filemode. Create, fileaccess. Write );
DB. Backup (Stream); // database backup method
Stream. Close ();
DB. Close ();

Export XML
Storage DB = storagefactory. instance. createstorage ();
DB. Open ("test1.dbs", pagepoolsize );

Streamwriter writer = new streamwriter ("test. xml ");
DB. exportxml (writer );
Writer. Close ();
DB. Close ();
DB. Open ("test2.dbs", pagepoolsize );
Reader reader = new streamreader ("test. xml ");
DB. importxml (Reader); // XML export method of Database

Reader. Close ();
DB. Close ();
---------------------------------------------------------------------

//////////////////////////////////////// //////////////////////////////////////// ///////////
The table in the perst. Persistent class perst database must inherit this base class
The persistent. OID attribute has a unique OID value for each Persistent Object.
Persistent. Storage attribute the storage object to which the Persistent Object belongs
Store () Save records
Deallocate () delete record

Syntax:
Using perst;

Public class yourpersistentclass: Persistent {
Int X;
String y;
Link links;
...

Void doupdate (){
X = 1;
Y = "Hello World ";
Links = storage. CreateLink ();
Store (); // save changes in the database
}
}

Iii. perst. Fulltext. fulltextsearchresult class and perst. Fulltext. fulltextsearchhit class

These two classes are commonly used in full-text search of databases, which is very convenient and concise in syntax.

Result set of all text searches in the perst. Fulltext. fulltextsearchresult class
Public fulltextsearchhit [] hits
Fulltextsearchresult. Hits attribute returns the fulltextsearchhit array.

The perst. Fulltext. fulltextsearchhit class is a subset element of the result set of fulltextsearchresult.
Public fulltextsearchhit (storage, int OID, float rank) // create a fulltextsearchhit
Fulltextsearchhit. Document is a record object of a table object in the database.
Fulltextsearchhit. Rank sorting Float Type

Iv. perst. codegenerator provides classes that support perst jsql syntax queries
The perst. codegenerator method is as follows:
Void predicate (code) // query by query Conditions
Perst. Code
 
The following is how to write the perst jsql statement corresponding to the SQL statement. I personally feel that this jsql syntax is not easy to use.
Bytes ------------------------------------------------------------------------------------
SQL query
Select * from car where color in ('black, 'Silver ')
And mileage <100000 and price <20000 and (aircondition or climatcontrol)
Order by price DESC, model ASC;

Codegenerator Query
Perst. query = dB. createquery (typeof (CAR ));
Codegenerator code = query. getcodegenerator ();
Code. predicate (code. And (
Code. In (code. Field ("color"), Code. List (code. Literal ("black"), Code. Literal ("Silver "))
),
Code. LT (code. Field ("Mileage"), Code. literals (10000 ))
),
Code. LT (code. Field ("price"), Code. Literal (20000 ))
),
Code. Or (code. Field ("aircondition"), Code. Field ("climatcontrol "))
)
);
Code. orderby ("price", false );
Code. orderby ("model", true );

Foreach (car in query)
{
...
}
------------------------------------------------------------------------

SQL query syntax
Select * From person where salary> 10000 and age <= 40 order by name;

use the perst database query syntax
foreach (person P in dB. select (typeof (person), "salary> 10000 and age <= 40 order by name")
{< br>...
}

Use perst codegenerator syntax
Query query = dB. createquery (typeof (person ));
Codegenerator code = query. getcodegenerator ();
Code. predicate (code. And (code. gt (code. Field ("salary"), Code. Literal (10000 )),
Code. Le (code. Field ("Age"), Code. Literal (40 ))));
Code. orderby ("name ");
Foreach (person P in query)
{
...
}

Introduction to perst Embedded Database

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.