Overview
In general, when designing tables, the primary keys are auto-incrementing. We use the primary key auto-incrementing mechanism provided by the database itself. sqlserver is generally identified by identity, oracle generally uses triggers. In most application scenarios, these mechanisms are sufficient. However, in some scenarios, if you need to customize the primary key generation mechanism, for example, if you generate these primary keys, such as l0000001, 010001, and 000001, you cannot use the mechanisms of the relational database itself.
Here we will introduce a way to flexibly customize a primary key. It mainly generates a primary key that meets the requirements through a certain generation mechanism. Here we will introduce a primary key value initialization value:
|
Primary key initialization value |
Prefix |
Length |
Remarks |
|
L0000001 |
L |
7 |
|
|
010001 |
01 |
4 |
|
|
000001 |
|
6 |
|
Requirement Analysis and Design
|
|
Function |
Quality |
Constraints |
|
Organization, user, and development |
1. Flexible customization of primary keys;
2. Data Structure Design of the primary key generation table;
3. Generate a primary key and encapsulate the called API;
|
1. Security;
2. versatility;
3. Performance;
4. Flexibility; |
1. read/write thread problems;
2. the maximum value of the primary key is automatically recorded without additional configuration; |
Data Structure Design
|
Name |
Code |
Note |
Data Type |
Length |
Primary Key? |
|
Table Name |
Gc017_table_name |
|
Varchar (100) |
100 |
True |
|
Prefix Encoding |
Gc017_parent_code |
|
Varchar (20) |
20 |
True |
|
Encoding level |
Gc017_code_level |
|
Varchar (20) |
20 |
True |
|
Maximum Encoding |
Gc017_max_code |
|
Varchar (20) |
20 |
False |
|
Column name |
Gc017_column_name |
|
Varchar (20) |
20 |
True |
|
Column Type |
Gc017_column_type |
|
Varchar (10) |
10 |
False |
Code Appreciation
1. There are two core codes: The itablecode interface and its implementation class tablecode. The Calling method is as follows:
Itablecode tablecode = new tablecode (); // Mode 1: generate the primary key ID. The parameters are the table name, primary key, and primary key generation length in sequence. Input 6 to indicate 000001 string id = tablecode. newcode ("tablename", "column", "6"); // Mode 2: generate a primary key ID, which indicates 0010001 id = tablecode. newcode ("tablename", "column", "2", "001", "4 ");
2. Core methods for generating encoding in tablecode:
Core code
Private Static readonly readerwriterlock L = new readerwriterlock (); Private Static readonly string prefix = string. empty; /// <summary> /// generate the primary key tablecode according to the rule // </Summary> /// <Param name = "ptblname"> table name </param> // /<Param name = "pcolumn"> column name </param> // <Param name = "plevel"> the level is empty by default </param> /// <Param name = "pparentcode"> prefix </param> /// <Param name = "plength"> length </param> /// <returns> </returns> Public s Tring newcode (string ptblname, string pcolumn, string plevel, string pparentcode, string plength) {try {L. acquirewriterlock (timeout. infinite); If (pparentcode. equals (string. empty) pparentcode = "0"; if (plevel. equals (string. empty) plevel = "1"; arraylist objlist; objlist = getdata (ptblname, pcolumn, plevel, pparentcode); If (! Plevel. Equals ("1") {If (! Pparentcode. equals ("0") {If (objlist. equals (null) {string newcode = pparentcode + inccode (string. empty, plength); insertdata (ptblname, pcolumn, plevel, pparentcode, newcode); Return newcode;} If (objlist. count = 0) {string newcode = pparentcode + inccode (string. empty, plength); insertdata (ptblname, pcolumn, plevel, pparentcode, newcode); Return newcode;} If (objlist. count = 1) {string newcode = inccode (tablecodeinfo) objlist [0]). maxcode, plength); setdata (ptblname, pcolumn, plevel, pparentcode, newcode); Return newcode ;}}if (objlist. equals (null) {string newcode = inccode (string. empty, plength); insertdata (ptblname, pcolumn, plevel, pparentcode, newcode); Return newcode;} If (objlist. count = 0) {string newcode = inccode (string. empty, plength); insertdata (ptblname, pcolumn, plevel, pparentcode, newcode); Return newcode;} If (objlist. count = 1) {string newcode = inccode (tablecodeinfo) objlist [0]). maxcode, plength); setdata (ptblname, pcolumn, plevel, pparentcode, newcode); Return newcode ;}} finally {L. releasewriterlock ();} return string. empty ;}
Here the multi-threaded readerwriterlock is used, and the method calls L. acquirewriterlock (timeout. infinite); this ensures multi-thread reading and single-thread writing to avoid primary key disorder;
Download the complete code Demo: systablecode.rar