Preface:
These two days are a little cool, except for the previous spam fans who have moved to three more posts, and recently they have been tossing some tools to four more pages. Occasionally, they are eager to write articles, recently, many of my friends are interested in CYQ. I am interested in Text Databases in Data V5. I would like to explain the technical principles of Text Databases and give them an explanation.
CYQ. Data framework stability and advancement:
CYQ. for the Data V4 series, the version number is no longer improved in V4.55, mainly to keep the most stable version, except for Bug fixes, there will be no Versions later than V4.N.
In fact, in the version V5, over the past one or two years, the Code has been greatly changed and the internal class structure has been adjusted a lot. Due to the new addition of functions, 40 or 50 items have been recorded, there is no number of records, but V5 has been accumulating in various regions for nearly a year, and the stability is also very strong.
CYQ. Data Text Database:
I used to write related articles: Play Code First with a text database on weekends.
For text database applications, the original oau2's login component is applied. The user stores the AccessToken and the corresponding account. I don't know if you have noticed this.
Here I will refer to the CYQ. Data. ProjectTool project configuration tool released yesterday (package source code), which saves the user configuration as a text database.
Technical Principles of CYQ. Data Text Database:
First look at the image:
Simple project description:
In this project, we mainly read the table structure and generate corresponding enumeration classes or entity classes. At the same time, we need to save the user's configuration and allow multiple configuration items to be saved Based on the configuration name, in addition, you need to restore the latest configuration items of the user each time you enable it.
What is used to save configurations?
In the past, I used an App. config to save the configuration. Of course, Xml and INI files can also be used. However, code writing is relatively laborious, and people are always trying to use something, there will be a solution to solve them, So text database is such a simple solution.
The following code snippets in the source code are used to decrypt the essence of the text database:
This simple project contains the basic addition, deletion, modification, and query operations of the database:
1: add user configuration.
2: update the user configuration with the same configuration name.
3: delete the user configuration (it is estimated that I have forgotten it. This function is not added)
4: From All configurations, query the data marked as IsMain = true and restore it to the default configuration.
A ProjectConfig object class is created in the project, and the following code is used:
This entity has one more ORM inheritance to enable it with the basic functions of the ORM:
The constructor must specify the table name and Database Link (it can be the configuration name. The internal judgment is based on whether there are spaces. If there is no space, it will be retrieved from web. config. If there is space, it will be used as the link)
The object class code is as follows:
CYQ. Data. ProjectTool
Namespace CYQ. Data. ProjectTool
{
Public class ProjectConfig: CYQ. Data. Orm. OrmBase
{
Public ProjectConfig ()
{
Base. SetInit (this, "ProjectConfig", "Txt Path = {0 }");
}
Private int _ ID;
/// <Summary>
/// ID
/// </Summary>
Public int ID
{
Get
{
Return _ ID;
}
Set
{
_ ID = value;
}
}
Private string _ Name;
/// <Summary>
/// Configuration name
/// </Summary>
Public string Name
{
Get
{
Return _ Name;
}
Set
{
_ Name = value;
}
}
Private string _ Conn;
/// <Summary>
/// Link string
/// </Summary>
Public string Conn
{
Get
{
Return _ Conn;
}
Set
{
_ Conn = value;
}
}
Private string _ DBType;
/// <Summary>
/// Database Type
/// </Summary>
Public string DBType
{
Get
{
Return _ DBType;
}
Set
{
_ DBType = value;
}
}
Private bool _ MutilDatabase;
/// <Summary>
/// Supports multi-database mode
/// </Summary>
Public bool MutilDatabase
{
Get
{
Return _ MutilDatabase;
}
Set
{
_ MutilDatabase = value;
}
}
Private string _ ProjectPath;
Public string ProjectPath
{
Get
{
Return _ ProjectPath;
}
Set
{
_ ProjectPath = value;
}
}
Private bool _ IsMain;
Public bool IsMain
{
Get
{
Return _ IsMain;
}
Set
{
_ IsMain = value;
}
}
Private string _ BuildMode;
/// <Summary>
/// Creation mode (enumeration mode; ORM entity Class Mode)
/// </Summary>
Public string BuildMode
{
Get
{
Return _ BuildMode;
}
Set
{
_ BuildMode = value;
}
}
Private string _ NameSpace;
/// <Summary>
/// Default namespace
/// </Summary>
Public string NameSpace
{
Get
{
Return _ NameSpace;
}
Set
{
_ NameSpace = value;
}
}
}
PS: If your database link is changed from Txt Path to Xml Path, you will find that the storage format is changed to Xml.
Take a look at the code snippet that adds Configuration:
When you click "test link" or "generate file", if the link is detected to be successful, the current configuration information is automatically saved. The Code is as follows:
String SaveConfig (){
String name = ddlName. Text. Trim ();
If (string. IsNullOrEmpty (name ))
{
Name = "DefaultConn ";
}
ResetMainState ();
Using (ProjectConfig config = new ProjectConfig ())
{
Config. SetAutoParentControl (gbConn, gbBuild );
If (config. Fill ("Name = '" + name + "'"))
{
Config. IsMain = true;
Config. Update (null, true );
}
Else
{
Config. IsMain = true;
If (config. Insert (true ))
{
DdlName. Items. Add (name );
}
}
}
Return name;
}
Let's look at this line of code in the object class New:
Using (ProjectConfig config = new ProjectConfig ())
Using Syntax:
The disponse method is automatically called at the end of the process, and the related resources are automatically closed and released.
New initialization of the constructor:
The system will obtain the attribute members in the object class, form a table structure, and write them to the specified path in the database link.
After you run the software, a file named ProjectConfig. ts is added to the specified directory.
The table structure is stored in the table. The content is as follows:
ID, Int, False, False, 0,; Name, NVarChar, False, True, 0,; Conn, NVarChar, False, True, 0,; DBType, NVarChar, False, true, 0,; MutilDatabase, Bit, False, True, 0,; ProjectPath, NVarChar, False, True, 0,; IsMain, Bit, False, True, 0,; BuildMode, NVarChar, False, True, 0,; NameSpace, NVarChar, False, True, 0 ,;
Simple Description:
Table Structure Storage Format: name, type, can be blank, read-only, length, default value.
For a Text Database, "table name. ts" is created. If it is a link to another database, the corresponding table is directly created in the database.
The following attribute assignment and basic ORM operation methods are skipped:
Here we use the combination of the UI method: config. SetAutoParentControl (gbConn, gbBuild );
Automatically add values to the child controls in two groupboxes.
The IsMain attribute does not appear in the control. Therefore, you need to assign values separately.
By combining with the UI, SetToAll () can be used to restore the configuration item:
The source code contains such a piece of code, which restores all the Configurations Based on the configuration name. You can take a look at it at a Glance:
Void LoadConfig (string name)
{
If (! String. IsNullOrEmpty (name ))
{
Using (ProjectConfig config = new ProjectConfig ())
{
If (config. Fill ("Name = '" + name + "'"))
{
Config. SetToAll (gbConn, gbBuild );
}
}
}
}
For a text database, the table structure is automatically generated and stored as the "*. ts" file.
What about data storage?
CYQ. Data has two core classes for this database:
1: JsonHelper: class used to deal with Json.
2: MDataTalle: memory table with powerful functions. It can load and output data in Json or Xml format.
The implementation of Text Database is based on these two classes.
If multiple texts exist during storage, Multiple Static MDataTable
MDataTable interacts with Json (or Xml) and loads and inputs.
So after running, you can see the file ProjectConfig.txt, which stores the json file as follows:
{"ID": "System. int32 "," Name ":" System. string "," Conn ":" System. string "," DBType ":" System. string "," MutilDatabase ":" System. boolean "," ProjectPath ":" System. string "," IsMain ":" System. boolean "," BuildMode ":" System. string "," NameSpace ":" System. string "},
{"ID": "1", "Name": "DefaultConn", "Conn": "server = .; database = qblog; uid = sa; pwd = 123456 "," DBType ":" Mssql "," MutilDatabase ":" False "," ProjectPath ":" "," IsMain ": "True", "BuildMode": "real-size (ORM operation mode)", "NameSpace": "Web. entity "}
Simple Description:
The first row may store data types in some cases. This is because the basic data structure still exists when the table structure is lost and the data structure is restored from Json to MDataTable.
Of course, the first line may be json data directly. The system identifies the first line based on some specific identifiers.
Principles of adding, deleting, modifying, and querying file databases:
If you are familiar with DataTable, I believe you are also familiar with MDataTable. addition, deletion, modification, and query of text databases are all performed in MDataTable.
Unified Framework: Paging and SQL query syntax:
For text, the storage result is json. For the unification of multiple data, it must have the basic database functions, paging and SQL statement syntax query.
To this end, I added an important function of MDataTable, parsed SQL statements, compared columns, and customized sorting of data rows to filter out the final result.
To this end, MDataTable is actually a powerful table class with paging and query functions, and can be detached from the database. After obtaining data, you can continue querying by page.
Other features of MDataTalle:
At the same time, MDataTable also has the basic batch insert and update functions, which are very important (CYQ. DBImport: This function is used to export data from multiple databases. You can query an MDataTable from a database and call the AccpertChange function to transfer data to other databases in batches, several lines of code are implemented, which is very convenient)
Summary:
There is no secret to the basic file database here:
1: automatically generate the table structure based on the object class (if the table structure already exists, it is automatically loaded ).
2: The storage format is Json, which relies on JsonHelper and MDataTable for loading and writing interactions.
3: add, delete, modify, and query data rows. It depends on MDataTable.
4: The CodeFirst mode is essentially implemented based on MAction.
Welcome to passing...