Read Activeandroid source code (c)

Source: Internet
Author: User

In the previous chapter, we finished reading the Conguration class. Finally, we find that conguration is for the initialization of the database. It contains the memory size, database name, database version, parser information.

 Public Static synchronized voidInitialize (configuration configuration) {if(sisinitialized) {LOG.V ("Activeandroid already initialized."); return; } Scontext=Configuration.getcontext (); Smodelinfo=Newmodelinfo (configuration); Sdatabasehelper=Newdatabasehelper (configuration); //Todo:it would is nice-to-override sizeOf here and calculate the memory//actually used, however at the-it seems like the reflection//required would is too costly to being of any benefit. We ' ll just set a max//object size instead.Sentities =NewLrucache<string, model>(Configuration.getcachesize ());        OpenDatabase (); Sisinitialized=true; LOG.V ("Activeandroid initialized successfully."); }

The previous chapter reads here that a databasehelperneeds to be initialized as an auxiliary class for the database. To read this class today:

 Public Final classDatabasehelperextendsSqliteopenhelper {//////////////////////////////////////////////////////////////////////////////////////    //Public CONSTANTS    //////////////////////////////////////////////////////////////////////////////////////     Public Final StaticString Migration_path = "Migrations"; //////////////////////////////////////////////////////////////////////////////////////    //PRIVATE Fields    //////////////////////////////////////////////////////////////////////////////////////    Private FinalString Msqlparser; //////////////////////////////////////////////////////////////////////////////////////    //Constructors    //////////////////////////////////////////////////////////////////////////////////////     Publicdatabasehelper (Configuration configuration) {Super(Configuration.getcontext (), Configuration.getdatabasename (),NULL, Configuration.getdatabaseversion ());        Copyattacheddatabase (Configuration.getcontext (), Configuration.getdatabasename ()); Msqlparser=Configuration.getsqlparser (); } ...}

Because the Databasehelper class inherits the Sqliteopenhelper, constructs the parent class first:

     Public int version) {        thisnull);    }

This construction method needs to enter the context, name, version, and these three parameters are available in the Conguration class.

Will come down is the Copyattacheddatabase method

     Public voidCopyattacheddatabase (Context context, String DatabaseName) {FinalFile DbPath =Context.getdatabasepath (databaseName); //If The database already exists, return        if(Dbpath.exists ()) {return; }        //Make sure we had a path to the filedbpath.getparentfile (). Mkdirs (); //Try to copy database file        Try {            FinalInputStream InputStream =context.getassets (). open (DatabaseName); FinalOutputStream output =NewFileOutputStream (DbPath); byte[] buffer =New byte[8192]; intlength;  while(length = inputstream.read (buffer, 0, 8192)) > 0) {output.write (buffer,0, length);            } output.flush ();            Output.close ();        Inputstream.close (); }        Catch(IOException e) {LOG.E ("Failed to open File", E); }    }

This method is primarily final inputstream InputStream = Context.getassets (). open (DatabaseName); Gets the database resources in the assets. If the database already exists in the same directory, no subsequent operations will be performed.

Then, looking down is the OnCreate method of the database:

    @Override    publicvoid  onCreate (sqlitedatabase db) {        executepragmas (db) ;        Executecreate (db);         -1, Db.getversion ());        Executecreateindex (db);    }

One way to look at:

    Private void Executepragmas (Sqlitedatabase db) {        if  (sqliteutils.foreign_keys_supported) {            Db.execsql ("PRAGMA foreign_keys=on;" );            LOG.I ("Foreign Keys supported. Enabling foreign key features. " );        }    }

This method is primarily used to start foreign keys.

About foreign key, I as a wild pseudo programmer, looked under the Baidu know

Continue looking at the second method in the database OnCreate method executecreate (db);

  

    Private void executecreate (Sqlitedatabase db) {        db.begintransaction ();         Try {            for  (Tableinfo TableInfo:Cache.getTableInfos ()) {                Db.execsql ( Sqliteutils.createtabledefinition (Tableinfo));            }            Db.settransactionsuccessful ();        }         finally {            db.endtransaction ();        }    }

This method is used to create some of the tables, first look at Cache.gettableinfos () This method:

     Public Static synchronized Collection<tableinfo> Gettableinfos () {        return  Smodelinfo.gettableinfos ();    }

Read here is broken, because we do not know what Modelinfo is, at the beginning of this chapter we see:

 Public Static synchronized voidInitialize (configuration configuration) {if(sisinitialized) {LOG.V ("Activeandroid already initialized."); return; } Scontext=Configuration.getcontext (); Smodelinfo=Newmodelinfo (configuration); Sdatabasehelper=Newdatabasehelper (configuration); //Todo:it would is nice-to-override sizeOf here and calculate the memory//actually used, however at the-it seems like the reflection//required would is too costly to being of any benefit. We ' ll just set a max//object size instead.Sentities =NewLrucache<string, model>(Configuration.getcachesize ());        OpenDatabase (); Sisinitialized=true; LOG.V ("Activeandroid initialized successfully."); }

We directly begin to analyze Sdatabasehelper = new Databasehelper (configuration), and do not read Smodelinfo = new Modelinfo (configuration); So we had to go over and read the Modelinfo class. This mistake is a bit hurt, next time to read:

Done

Read Activeandroid source code (c)

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.