Use C language consortium and enumeration to describe the database

Source: Internet
Author: User

The database stores almost all business data, and the data is not static after all, so we must write programs to complete data processing and integration, the database only stores the results after processing and integration. Therefore, it is inevitable that we read the data in the database to the program and store the data in the database after processing, we need to maintain the structure of the database table in the program. Obviously, we need to define many struct or consortium arrays in the program to save the database query results. Each struct represents a record, many records are represented by struct arrays. As a result, the program will be very bloated. For example, the following struct will appear:
Struct girl {
Char * Name
Int age;
Int sexy;
Int height;
Int weight;
...
}
The size of the struct depends entirely on the columns in the database table. This method is feasible but not reasonable. If we use another method, it may make things easier. We read the d2i and I2D code of OpenSSL and found that people did not adopt this bloated design, although OpenSSL does not involve databases, after all, it involves formatted files, such as PEM/der files. In our current discussion, these formatting files are consistent with those in the database, the OpenSSL method deserves our consideration.
Instead of considering the specific nature of the database table, we can only use a column of the table as a "name-value" pair, in this way, each record can be considered as a "pair" array. The size of the array is the number of columns. In the preceding example, a record {"Lili", 21,100 0000 ..., 175,100 ,...}, we can define {name, "Lili"}, {age, 21}, {sexy, 1000000...}, as follows ...}, {height, 175}, {weight, 100 },...}, to unify, we can design items such as {name, "Lili"} into a struct, that is, the struct of "name-value:
Struct ABC {
Char * Name;
Char * value;
}
Let's see if this is reasonable and there is no room for improvement ,... it is reasonable, but although all the results read from the database are of the string char * type, our program needs to convert it into a reasonable data type for processing, that is to say, the value is not all string types, but may be any type. The name is obviously the column name. Since value can be of any type, we need to define it as a consortium:
Struct ABC {
Char * Name;
Union {
Char * db_char; // raw data read from the database
Int int_value; // if it is int type, int_value is converted to int type data, the same below...
...
} Value;
}
Next, let's take a look at the space without improvement. Let's look at the name field, which only represents the column name. Is it really necessary? No! In fact, it only serves as an index to help the program find a specific column. Since we are going to design the structure ABC into an array according to the record column, the subscript of the array can complete this task, designing a Name field is redundant, so we decided to cut it down:
Struct ABC {
Union {
Char * db_char; // raw data read from the database
Int int_value; // if it is int type, int_value is converted to int type data, the same below...
...
} Value;
}
Then how can I index the database record columns with array subscripts? After all, it is only meaningless numbers. To make the program more readable and maintainable, We need to assign meaningless numbers as meanings, the following enumeration is defined:
Enum {
Girl_name = 0,
Girl_age,
Girl_sexy,
Girl_height,
Girl_weight,
...
Total
}
Therefore, we only need to assign a total struct ABC for each database record, and then directly use the enumeration type to index the specific elements of the array. Eventually, enumeration and union are very helpful. Enumeration gives meaning to numbers, while association makes data definitions uniform. In the future, we can refer to this method when writing programs, at the same time, I also felt that the C language is really powerful.

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.