Disclaimer: This article is reprinted
Django encapsulates the persistent layer well and supports popular relational databases such as MySQL, PostgreSQL, Oracle, and sqlite3. The top layer provides unified model encapsulation. users do not have to worry about the underlying details of real databases, or even know little about DBMS itself.
One of Django's ways to do this is to abstract and encapsulate it. The SQL statement itself is no longer exposed, and even the specific DBMS is isolated for users on a unified interface. Users only see the same model, or some command lines, the communication between these models or commands and the actual DBMS is encapsulated in a highly abstract backend module. Shows an overview of the Django Persistence Layer Model:
The Python module encapsulated by Django on the persistent layer is written in Django. DB. in the backends package, each package under it corresponds to a specific database type. MySQL, SQLite, and so on mentioned above exist in Django as a package. DB. under backends. Taking MYSQL as an example, I sorted out its class hierarchy. The modeling of python is different from that of Java in the process of drawing a class chart, because a module in python can have multiple class structures and some process-oriented statements. Java is generally a file-class (except internal classes, of course). It is easy to use class charts for modeling.
Based on the "abstract" thinking, Django provides consistent interfaces for the underlying database types it supports. However, this interface is not a common operation for all its interfaces, it provides a superset for all the operations below. For example, the databasseoperations class method has the convert_values () method in sqlite3, which is not available in MySQL operations. However, value_to_db_datetime () and value_to_db_time () are available in MySQL () and other operations not available for sqlite3. These operations are available in the parent class basedatabaseoperations.
Taking MYSQL as an example, the relationship between class diagrams in backends is drawn. Details are shown in. In fact, the support for other database implementations is similar.
The original model call is in the _ init _. py at the top. The load_backends () method imports the corresponding dB driver and backend module according to the configuration item in settings. py. There is a clever and typical PythonCodeIn the load_backend (backend_name) function of DB. _ init _. py:
The modified statement is returned when an import backend exception occurs. If the backend database is not supported, the system will throw an exception if the system cannot find the corresponding backend instance, at the same time, we will list the currently supported databases by scanning the files in the Django/DB/backends directory (as mentioned above, each actually supported database backend must create sub-directories under the backends directory and inherit from them to implement the blue base classes in the second figure ), if the file F is a sub-directory and the directory name is not ". ", these directory names are merged into a list and returned with a value assigned to the available_backends variable. This is a function completed by a complete statement, which is very concise than Java code.
Available_backends = [ F For F In OS . Listdir ( Backend_dir ) \ If OS . Path . Isdir ( OS . Path . Join ( Backend_dir , F )) \ And Not F . Startswith ( '.' )]
For more detailed database encapsulation implementation, MySQL can be used as an example to refer to DjangoSource codeThe source code tree is located in Django/DB/backends/MySQL.