In the previous blog, "Build Android ORM Framework Opendroid (ii)--Create a database automatically," We describe how opendroid can automatically help us create a database and put together the SQL statements that create the database through reflection, and then the above blog, I'm going to introduce you today. Opendroid database Persistence (that is, insert operations) is how a process.
Not much nonsense, we immediately entered the subject.
...
Remember how we saved the data to the database by opendroid? It was called the Save method inherited from the Opendroid class, to review it.
Student stu = new Student () stu.setstuname ("Bingbin"); Stu.setstuage (+); Stu.save ();
So we save student information to the student table, about the student table is how to create, the previous blog has been introduced, the focus of today is the Save () method.
By convention, we locate the source of the Save ().
/** * Insert Data * @return last inserted ID */public long Save () {try {class<opendroid> klass = (class<opendroid>) getclass () ; Contentvalues CV = new Contentvalues () Generatedata (Klass, CV); return Crud.insert (Klass.getsimplename (), CV, Ssqlitedatabase);} catch (Exception e) {e.printstacktrace ();} return-1;}
This method is still relatively simple, only 11 lines of code, we come to a sentence analysis.
First, the 7th line, through the GetClass method, obtains the current class that represents the current object.
Then the 8th line new A contentvalues, I believe you will be familiar with it.
9 lines, called the Generatedata method, this method has two parameters, the first is a class object, the 7th line of time we have obtained, the second parameter is a contentvalues, used to store the data to be inserted into the database.
Then, in 11 rows, called the Insert static method in the Crud class to hold the data, the first parameter of the Insert method is the table name to insert, because our table and Bean are one by one corresponding, so the current class name is the name of the table we want to manipulate. The second parameter is a Sqlitedatabase object.
The Crud.insert method returns a long type of return value, which I believe you have guessed, and the return value is the ID of our newly inserted data.
After analyzing this method, let's take a look at the Generatedata method called in line 9th.
/** * Generate Data * @param tableName to get the table name * @param values to get data * @throws nosuchmethodexception * @throws Illegalaccessexcepti On * @throws invocationtargetexception */private void Generatedata (class<opendroid> klass, contentvalues values) Throws Nosuchmethodexception, illegalaccessexception,invocationtargetexception {field[] fields = Klass.getdeclaredfields (); Gets all fields in the class method m; String FieldName; String methodname;for (Field field:fields) {///If it is public, ignore if (field.isaccessible ()) {continue;} Gets the type of the field class<?> FieldType = Field.gettype (); fieldName = Field.getname (); Get field name//capitalize the first letter of the field name, prepare the Assembly getter (getName) MethodName = character.touppercase (Fieldname.charat (0)) + Fieldname.substring (1);//here also to determine whether the type is boolean,//if it is a Boolean, it is not getxxx, but isxxxif (FieldType = = Boolean.class | | FieldType = = Boolean.class) {m = Klass.getdeclaredmethod ("is" + methodName);} else {m = Klass.getdeclaredmethod ("Get" + Me Thodname); Get method}//Gets the return value of the method object value = M.invoke (this);//not supported for some types: Not foundSolution if (value = = null) {///If NULL, add a null//values.putnull (fieldName) in contentvalues; continue;} By judging the type of field, insert the corresponding data to the contentvalues if (FieldType = = Integer.class | | fieldtype = = int.class) {values.put (FieldName, Integer.parseint (M.invoke (this). ToString ()));} else if (FieldType = = Boolean.class | | fieldtype = = boolean.class) {values.put (FieldName, Boolean.parseboolean (M.invoke ( This). ToString ()));} else {values.put (FieldName, M.invoke (this). ToString ());}}}
In today's approach, this method is the longest one, it doesn't matter, we can look at the line.
The parameters of the method we have explained above, here does not repeat, at the beginning of the method, a succession of definitions of 4 variables, the first variable is the class we obtained through Klass all the fields, next we want to save the value and the field name. The rest of the variables we use are used again.
17 rows, entered a for loop, traversed all the fields, loops, 19~21 lines, we still go to determine whether the field is public, if it is public, it proves that the field is not mapped to the database.
24 rows, gets the type of the field, because in the following we want to set the value by type.
26~28, we're going to piece together the Getter method.
Then 32~36 line, but also a judgment, mainly for our Java Bean Definition of the specification, if it is a Boolean type, we are in the definition of the method is not isxxx?
OK, we get the method that will be called by reflection, and the next 39 lines will get the specific value by getter method.
42~46 line is a failure, not yet the outcome for the type of not considered how to support, but also does not affect the commonly used types have been supported.
49~55 line, by the type of the field, to save different types of values to Contentvalues, you can see that we use the call M.invoke method to perform the reflected method.
Now that we have saved the data into the contentvalues, the next step is to call the Android native API code and save the database in the database.
and save into the database is definitely called the Crud.insert method, hurry to see this method.
/** * Insert data * @param t corresponding Bean * @param DB database operation * @return newly inserted ID */protected static long insert (String tableName, Contentv Alues CV, Sqlitedatabase db) {Long id = db.insert (tableName, NULL, CV); return ID;}
It's so happy! Only two lines of code! Look, we directly call the Sqlitedatabase Insert method to save the data into the database. I believe that there is a general understanding of the whole process of Save as you see here.
Well, Opendroid's data persistence process is here, and in the next blog we'll learn about Opendroid's other operations.
Opendroid's Open source address:http://git.oschina.net/qibin/OpenDroid
Build Android ORM Framework Opendroid (iii)--persistent data