Build Android ORM Framework Opendroid (v)--Implementation of data update

Source: Internet
Author: User

In the previous blog, "Build Android ORM Framework Opendroid (iv)--Elegant Delete data", we describe how opendroid gracefully removes data from the database, and can see Opendroid's design is so simple, In fact, Opendroid is just me as an interest or to hold the attitude of trying to write, of course, it certainly has a lot of shortcomings, but this does not affect us to understand an ORM framework of the process.

Nonsense not to say, the following into the theme, today I choose to understand is the opendroid update process, in fact, for those who have already understood the delete operation, today's update process is certainly so familiar, because its general process and delete operations basically consistent, Just one more step to the correspondence of the data.

As a rule, let's familiarize ourselves with how opendroid updates a piece of data.


Student stu = new Student ();  Stu.setstuname ("loader");  


Student stu = new Student ();  Stu.setstuname ("loader");  Stu.update ("_ID>?", "4");

Contentvalues CV = new Contentvalues ();  Cv.put ("Stuname", "loader");  Opendroid.update (Student.class, CV, "_ID>? Or name like? "," 1 ","%q% ");

Well, Opendroid provides these three ways to update the operation. The third is the use of contentvalues in the form of updates, I am sure you are very familiar with.

Oh yes, in advance, as in the case of delete, these three ways will eventually be returned to a method.


First we will locate the source of the first update.

/**  * Update Data  * @param IDs to update the ID of the data  * @return affect the number of rows *  /public  int update (int ... ids) {      try {          Clas S<opendroid> Klass = (class<opendroid>) getclass ();          Contentvalues CV = new Contentvalues ();          Generatedata (Klass, CV);                        if (Ids.length = = 0) {              return update (Klass, CV, NULL, NULL);          }                        StringBuilder builder = new StringBuilder ("_id in (");          string[] Whereargs = new String[ids.length];          Buildin (builder, Whereargs, IDS);                        Return update (Klass, CV, builder.tostring (), Whereargs);      } catch (Exception e) {          e.printstacktrace ();      }      return-1;  }

In line 8th, we get the class of the current object to get the fields and names in it by reflection.

Next a new Contentvalues object, remember the above I said "these three ways will eventually return to a method", from here we can generally guess, and finally return to the

Opendroid.update (Student.class, CV, "_ID>? Or name like? "," 1 ","%q% ");
  

this method. Keep this speculation and continue to analyze the code.

Followed by a Generatedata method, this method we have in the "Build Android ORM Framework Opendroid (three)-persistent data" in detail to analyze, if you do not know the role of this method, you can refer to the previous blog.

12~14, by judging if no ID is passed, it is possible to update all the data, where the static Update method is called, and the last two arguments are passed null.

16~18 these three lines of code, you should also be familiar with it, we explain the delete when there are so three words, these three words is to build an in SQL statement, and set its parameters, if you are not familiar with it, you can refer to the "Build Android ORM Framework Opendroid (four) --Elegant Delete data, this blog on the Buildin method is also explained, here is not repeated to say.

At the end of the method, a static Update method is also called, and the number of rows affected is returned.


Next, let's navigate to the second Update method!

/**  * Update Data  * @param where Condition  * @param whereargs condition parameter  * @return affect number of rows *  /public int update (String WH Ere, String ... whereargs) {      try {          class<opendroid> klass = (class<opendroid>) getclass ();          Contentvalues CV = new Contentvalues ();          Generatedata (Klass, CV);                        Return update (Klass, CV, where, Whereargs);      } catch (Exception e) {          e.printstacktrace ();      }      return-1;  }


In fact, this method and the above, but the above need in the Update method to combine conditions, and here the conditions by the user incoming, so this method we do not talk about, next we look at the three brothers rendezvous, that is, third-party method called the static approach! (Oh, yes, here's the proof that our guess is right!) )

/**  * Update Data  * @param klass to update the table corresponding to the class  * @param CV to update the data  * @param where WHERE Condition  * @param whereargs Bar The parameters of the pieces  * @return affect the number of rows *  /public  static <t extends opendroid> int update (Class<t> Klass, Contentvalues CV,          string where, String ... whereargs) {      string tableName = Klass.getsimplename ();      Return Crud.update (TableName, CV, where, Whereargs, ssqlitedatabase);  }

for code control, it's really disappointing, just two lines of code! Haven't commented much yet! Nima! (Heart, thousands grass mud horse flew over)

1/2 lines of code get the Klass class name, which is the name of the table we want to manipulate.

2/2 lines, directly called the Crud.update method to update the database, but also crud ... Let's go and have a look.

/**  * Update Data  * @param tableName table name  * @param CV updated data  * @param where updated condition  * @param whereargs updated condition parameters 
   
    * @param db Database  * @return affect number of rows *  /protected static <t extends opendroid> int update (String tableName, C Ontentvalues CV,          String where, string[] Whereargs, Sqlitedatabase db) {      int count = db.update (TableName, CV, wher E, Whereargs);      return count;  }
   


OK, again two lines of code, and directly call the Sqlitedatabase Update method to refresh the data, go to this step, the data to be used must have been ready. TableName We have previously obtained, contentvalues is used Generatedata to populate or user's own new, where and whereargs whether it is updated by the ID or conditional update, we have been determined. Just call the Android native Update method to update the database to OK, and finally return the number of rows affected.


Today's update operation is really simple, because many of our code is detailed in the previous blog, if you do not know some of the details, you can spend a few minutes to see the previous several blog.


To summarize the opendroid update process.

1, in the business logic can be updated by the three update methods, in addition to one static, the other two need to operate on the Bean object.

2, regardless of what kind of operation, finally will go to Opendroid.update (class<t> Klass, Contentvalues cv,string where, String ... Whereargs) this method.

3, then through the Crud.update method to call the Android native Upate method to update the database, and then return the number of affected rows.


After completing the analysis of the update operation, the basic operation of Opendroid has completed a large part, now only select operation is not analyzed, I believe you can now completely according to this opendroid idea to do a self-ORM framework. Of course, the following blog will continue to complete the analysis of select operations, of course, there will be a opendroid database upgrade mechanism.


Opendroid's Open source address: http://git.oschina.net/qibin/OpenDroid









Build Android ORM Framework Opendroid (v)--Implementation of data update

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.