Private food Lecture 2 database encapsulation Extension

Source: Internet
Author: User

Private food Lecture 2 database encapsulation Extension

In the first lecture, we talked about the encapsulation of the underlying database. Today we are talking about why we add a layer of data to the base layer of the database.

What is the value of this layer in the library service layer? How do we deal with other special applications?
Let's take a look at the code that has been extended:

<? Php <br/>/** <br/> * database of the driver class. <br/> * @ Author: <sanshi0815@tom.com> sanshi0815 (sanshi) <br/> * @ package: database operate class of the system. <br/> * @ copyright: Copyright (c) 2005-2010 <br/> * @ version: $ ID: DB _. class. PHP 07:09:11 $ <br/> * @ show note: background User Group Management Module <br/> */<br/>/* @ package of the Database Class */<br/> class db_admin_group extends pdomysql <B R/>{< br/>/* MySQL table name */<br/> Public $ tablename = "admin_group "; </P> <p>/** <br/> * construction this database object. <br/> * @ Author: Bruce (sanshi ). <br/> * @ show: Get the pdomysql object and set the default charset. <br/> * @ return :.. <br/> */<br/> public function _ construct () <br/>{< br/> parent ::__ construct (); <br/>}</P> <p>/** <br/> * execute the insert method. <br/> * <Br/> * @ Author: Bruce (sanshi ). <br/> * @ show: Execute 'insert' method by the pdomysql object. <br/> * @ Param: $ values array -- key is the table coulnum and value is that values. <br/> * @ return: false or Int. <br/> */<br/> Public Function insert_admin_group ($ values) <br/> {<br/> return $ this-> insert ($ this-> tablename, $ values); <br/>}</P> <p>/** <br/> * execute the update method. <br/> * <br/> * @ Author: Bruce (sanshi ). <br/> * @ show: Execute 'update' method by the pdomysql object. <br/> * @ Param: $ values array -- key is the table coulnum and value is that values. <br/> * @ Param: @ where array -- key is the table coulnum and value is the where values. <br/> * @ return: false or Int. <br/> */<br/> Public Function update_admin_group ($ values, $ where) <br/>{< br/> return $ this-> Update ($ thi S-> tablename, $ values, $ where); <br/>}</P> <p>/** <br/> * execute the delete method. <br/> * @ Author: Bruce (sanshi ). <br/> * @ show: Execute 'delete' method by the pdomysql object. <br/> * @ Param: @ where array -- key is the table coulnum and value is the where values. <br/> * @ return: false or Int. <br/> */<br/> Public Function delete_admin_group ($ where) <br/>{< br/> return $ this-> Delete ($ This-> tablename, $ where); <br/>}</P> <p>/** <br/> * execute the select method. <br/> * @ Author: Bruce (sanshi) <br/> * @ show: Execute 'select' method by the pdomysql object, return the item set. <br/> * @ Param: $ Where string -- search where. <br/> * @ Param: $ Col string -- search order. <br/> * @ Param: $ numrows int -- search item num. <br/> * @ Param: $ offset int -- search item Set offset. <Br/> * @ Param: $ field string -- search item name. <br/> * @ return: false or array. <br/> */<br/> Public Function getall ($ where = '1', $ Col = '', $ numrows =-1, $ offset =-1, $ field = "*") <br/>{< br/> $ SQL = sprintf ("select % s from % s where % s", $ field, $ this-> tablename, $ where); <br/> return $ this-> select ($ SQL, $ Col, $ numrows, $ offset ); <br/>}</P> <p>/** <br/> * Get one item. <br/> * @ Author: Bruce (sanshi) <br/> * @ show: search the database. return one item. <br/> * @ Param: $ index array -- key is the index key. value is the index value. <br/> * @ return: array. <br/> */<br/> Public Function getone ($ admin_group_id) <br/>{< br/> $ SQL = sprintf ("select * from % s where admin_group_id = '% S'", $ this-> tablename, </P> <p> $ admin_group_id); <br/> $ result = $ this-> select ($ SQL, 'admin _ group_id ', 1, 0); <br/> return isset ($ result [0])? $ Result [0]: array (); <br/>}</P> <p>/* public function is the class method, design by hooker framework designer */<br/>/* createtime: 07:09:11 */</P> <p>?>

 

 

 

This class is encapsulated. When we use this class, we can ignore what the table name is. You can also use basic classes.

.

Next let's talk about how to insert data from two tables for one business?
When this problem occurs, we can use several solutions:
1. Expand the business, that is, let the business step 2. This was solved during design.
2. Solve the problem at the control layer, that is, when we write a program, we can determine which table is inserted according to the received data.
3. It is processed at the data layer. This is generally because the program has been established, and a single table solves the data storage problem. In the later stage, the single table is checked.

The cable speed is reduced and the Master/Slave table is used.

 

Public Function insert_admin_group ($ values) <br/>{< br/> // data analysis <br/> $ temp ['s'] = $ valuse ['s']; <br/> unset ($ valuse ['s ']); <br/> $ result = $ this-> insert ($ this-> tablename. "_ Sub", $ temp); <br/> $ value ['id'] = $ result; <br/> return $ result? $ This-> insert ($ this-> tablename, $ values): $ result; </P> <p>}

 

Table sharding is completed.
4. Another case is table sharding. This is a horizontal table sharding, that is, using a specific value as the criteria for table sharding, such

We use the title as the table sharding label.

Public Function insert_admin_group ($ values) <br/>{< br/> $ table_id = get_table_id ($ values ['title']); <br/> $ result = $ this-> insert ($ this-> tablename. $ table_id, $ values); <br/>}

 

Of course, we only described various processing operations during insertion, but in fact, we also need to do some operations on the database, such as update and deletion.

Add parameters to solve the masking problem. We will not demonstrate it here.

Another scenario is multi-database operations:
What should we do with this situation? Are we all single-database operations?
This requires the corresponding extension of the database base class. If we only need multiple database operations for one operation, we only need

You can process the corresponding method, for example:

Public Function insert_admin_group ($ values) <br/>{< br/> mysql_connect ("localhost", "root ",""); <br/> mysql_select ("db_name"); <br/> mysql_query ("the operation you want to perform "); <br/> return $ this-> insert ($ this-> tablename, $ values); </P> <p>}

 

What if we want frequent multi-database switching?
Now Let's rebuild the connection function of the basic database we talked about last time.

Function init_db ($ is_master_db = true, $ dbname = false, $ db_con) <br/>{< br/> global $ db_config_master, $ db_config_slave; <br/> $ db_config_master_con = empty ($ db_con )? $ Db_config_master: $ db_con; <br/> $ db_config_slave_con = empty ($ db_con )? $ Db_config_slave: $ db_con; </P> <p> if ($ is_master_db) // is master <br/>{< br/> If (! $ Dbname) <br/>{< br/> $ dbname = $ db_config_master_con ['dbname']; <br/>}< br/> $ DSN = "MYSQL: host = ". $ db_config_master_con ['host']. "; Port = ". </P> <p> $ db_config_master ['Port']. "; dbname = ". $ dbname; <br/> $ user = $ db_config_master_con ['user']; <br/> $ passwd = $ db_config_master_con ['passwd']; <br/>}< br/> else // is slave <br/>{< br/> If (! $ Dbname) <br/>{< br/> $ dbname = $ db_config_slave_con ['dbname']; <br/>}< br/> $ DSN = "MYSQL: host = ". $ db_config_slave_con ['host']. "; Port = ". </P> <p> $ db_config_slave ['Port']. "; dbname = ". $ dbname; <br/> $ user = $ db_config_slave_con ['user']; <br/> $ passwd = $ db_config_slave_con ['passwd']; <br/>}</P> <p> $ this-> db_obj = new PDO ($ DSN, $ user, $ passwd); </P> <p>}

 

There is also a bug here, because we have considered the case of not connecting a single data many times during optimization, so we have performed data operations here.

What should we do for handling records?
1. Not in the record handle
2. Rebuild the handle.
I am not perfect here. I will leave it for my homework.

The operation encapsulation of the database is finished here.

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.