GithubHelp home page GithubHelp logo

sqldb's Introduction

sqldb

SqlDb is a wrapper that hides thread and connection management for android SQLite databases and provides both a sync and async api - it is not an ORM.

To use SqlDb, construct it by providing

  • an android SQLLiteOpenHelper which is used to retreive the underlying database
  • an ExecutorService to provide thread(s) on which to return the result of database operations
  • a flag to indicate if SQLite SYNCHRONOUS pragma is to be disabled for performance gains, recommended.

After you have a SqlDb instance, you can perform typical database operations with signatures that are very similar to the android provided api - with one difference. All methods in SqlDb take a callback in order to provide an async api.

To use Sqldb.query methods you will need to provide an instance of the CursorHandler interface. This requires you to implement two methods - handle() and callback(). The handle method should have application code that reades a cursor and converts it into some object which is useful for the application. The callback method then receives this application object and uses it. This abstraction is required for two reasons, firstly, to ensure that that the cursor is executed on the same thread as the underlying SQLConnection this is done by calling CursorHandler.handle in the database reader thread, and secondly, to ensure that the appication receives the result on a seprate thread so that the database thread is freed up for other operations - this is done by calling CursorHandler.callback in a thread from the application provided ExecutorService which was injected in the SqlDb constructor.

Here is an example on how to use a query method, note that Fruit is an application class.

SqlDb db = new SqlDb(sqliteOpenHelper, appExecutorService, false);

CursorHandler<Fruit> handler = new CursorHandler<Fruit>()
{

  @Override
  Fruit handle(Cursor c)
  {
      //Iterate over cursor, extract fields and make a Fruit
      
      return fruit;
  }
  
  @Override
  void callback(Fruit result)
  {
    //Use fruit in app
    String color = result.getColor();
    String name = result.getName();
  }
};

db.query("fruits", columns, selection, selectionArgs, handler); 

Methods that write to the db such as 'replace', 'insert' or 'delete' are very much like the ones provided by android and much like the query method they take a simple callback of type DBCallback which is called after the operation is complete. Here is an example

DBCallback callback = new DBCallback()
{
  @Override
  void exec(long l)
  {
    //'l' indicates the number of rows affected by this operation.
  }

  @Override
  void onError(Exception e)
  {
    //called with an exception if one occurs while executing
  }
};

db.replace("fruits", null, initialValues, callback);

Note that all methods in SqlDb return a Future<T> of the type used in CursorHandler<T> for queries and of the type Long for writes. This future is useful to make these async methods sync by allowing the calling application thread to block by calling the Future.get method - which blocks the calling thread till a result is available. For most usecases we will just ignore this Future as we don't want to wait for the result and will instead rely on the callback.

Future<T> f = db.replace(...);
f.get() //This blocks the current thread till SqlDb finishes processing and has a result available

There are also methods that allow the execution of 'rawQuery', 'batchQuery' and the usage of transactions.

PS: Please file github issues, for bugs/features/suggestions and pull requests are welcome.

githalytics.com alpha

sqldb's People

Contributors

esoxjem avatar kashifrazzaqui avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.