PGTop for Android
Things I learned making this app


        Mark Wong
   markwkm@postgresql.org
  Port1and, Oregon, PostgreSQL Users Group


          20 January 2011
PGTop for Android




                                  A PostgresSQL stats monitoring app
                                  for Android:
                                  http://github.com/markwkm/PGTop




    markwkm (PDXPUG)   PGTop for Android                 20 January 2011   2 / 37
How did I get started?




  Once upon a time I got developer edition G1 from Google...




     markwkm (PDXPUG)              PGTop for Android           20 January 2011   3 / 37
And it sat idle...




    • Postgres JDBC driver for Android platform didn’t work
    • Native C development kit too hard for me to just build just the libpq
      library from the postgres source




     markwkm (PDXPUG)               PGTop for Android                20 January 2011   4 / 37
And then some motivation...


            __     __
           / ~~~/  . o O ( PostgreSQL contest! )
     ,----(     oo    )
    /      __     __/
   /|         ( |(
  ^    /___ / |
     |__|   |__|-"




    markwkm (PDXPUG)       PGTop for Android         20 January 2011   5 / 37
/
                /
        _.-""""-._
      .’           ‘.
     /    O     O     . o O ( Free Nexus One! )
 __ +----------------+ __
/ +----------------+/ 
| ||                  || |
| ||                  || |
| ||                  || |
| ||                  || |
__/|                 |__/
     __    __     __/
        | | | |
        | | | |
        __/ __/
  markwkm (PDXPUG)         PGTop for Android       20 January 2011   6 / 37
New developments




  Dave Cramer builds a version of the postgres JDBC jar that works on Android:
  http://groups.google.com/group/pgandroid/attach/4ee44940c9f7319f/postgresql.jar?part=2




     markwkm (PDXPUG)                  PGTop for Android                     20 January 2011   7 / 37
Slapped together and entered PGTop for Android:




   markwkm (PDXPUG)             PGTop for Android   20 January 2011   8 / 37
Not a winner. :(




   markwkm (PDXPUG)   PGTop for Android   20 January 2011   9 / 37
Oh well, continue to make a few improvement...




   markwkm (PDXPUG)             PGTop for Android   20 January 2011   10 / 37
What did I learn along the way?




   markwkm (PDXPUG)               PGTop for Android   20 January 2011   11 / 37
How to set up a development environment


    • Requires 32-bit java development environment
    • Requires Android sdk
      • Android emulator
      • Android jars
      • Android packaging tools
    • Optional to have Eclipse
      • ui designer gui
      • hides xml

    • Optional to have Android ndk for developing native C library




     markwkm (PDXPUG)              PGTop for Android                 20 January 2011   12 / 37
How to make a launcher icon
  I need to learn how to use Inkscape or Gimp better to make the ascii art icon
  meet the recommended guidelines better:




     markwkm (PDXPUG)               PGTop for Android                20 January 2011   13 / 37
About layouts




  Only used scroll view and linear layouts.




     markwkm (PDXPUG)                PGTop for Android   20 January 2011   14 / 37
About ui widgets



    • TextViews (labels)
    • EditViews (input text)
    • Spinners (selection)
    • Buttons
    • custom menus




     markwkm (PDXPUG)          PGTop for Android   20 January 2011   15 / 37
How the threading model works




    • child threads don’t have access to ui objects
    • use android.os.Handler to pass messages to parent thread




     markwkm (PDXPUG)               PGTop for Android            20 January 2011   16 / 37
How PGTop uses threads
  Create a Handler:
  private Handler handler = new Handler() {
      @Override
      public void handleMessage(Message msg) {
          // Update UI objects...
      }
  }

  Child thread loops continuously to query PostgreSQL database for stats and
  updates class variables with data.
  Pass a message to a handler to call the method that updates the UI:
  handler.sendEmptyMessage(0);
     markwkm (PDXPUG)              PGTop for Android               20 January 2011   17 / 37
How to use SQLite3 interface
  To store database connection information (oversimplified sample):
  public class PGConnectionOpenHelper extends SQLiteOpenHelper


  SQLiteDatabase db = openHelper.getReadableDatabase();


  Cursor c = db.rawQuery(SELECT_CONNECTIONS, null);


  while (c.moveToNext()) {
      c.getString(c.getColumnIndex("database"));
  }

     markwkm (PDXPUG)              PGTop for Android                 20 January 2011   18 / 37
How to read the stdout/stderr log
  The debugger was too hard for me.




     markwkm (PDXPUG)            PGTop for Android   20 January 2011   19 / 37
__     __
         / ~~~/  . o O ( Code review time! )
   ,----(     oo    )
  /      __     __/
 /|         ( |(
^    /___ / |
   |__|   |__|-"




  markwkm (PDXPUG)       PGTop for Android       20 January 2011   20 / 37
PostgreSQL JDBC




  Now for some simple examples for connecting to a PostgreSQL database and
  querying some data. Warning, code formatted to fit better on these slides. . .




     markwkm (PDXPUG)               PGTop for Android                20 January 2011   21 / 37
Open a Database Connection
  Load the PostgreSQL JDBC driver and open a database connection using SSL:
  Class.forName("org.postgresql.Driver");
  String url;
  url = "jdbc:postgresql://pghost:5432/pgdatabase" +
        "?sslfactory=org.postgresql.ssl.NonValidatingFactory" +
        "&ssl=true";
  Connection conn = DriverManager.getConnection(url,
                                                "pguser",
                                                "pgpass");
   Don’t forget to close the connection when you’re done.
   conn.close();


     markwkm (PDXPUG)            PGTop for Android              20 January 2011   22 / 37
Execute a Query
  Building on the previous slide, select the name of all relations from pg class
  and iterate through every row returned:
  String sql;
  sql = "SELECT relname FROM pg_class WHERE relkind = ’r’;"

  Statement st = conn.createStatement();
  ResultSet rs = st.executeQuery(sql);
  while (rs.next()) {
       Columns are enumerated starting with 1.
      String relname = rs.getString(1);
  }
  rs.close();
  st.close();
     markwkm (PDXPUG)               PGTop for Android                 20 January 2011   23 / 37
Execute a Query with a Bind Value
  Building on the previous slide, select the number of all relations from pg class:
  String sql = "SELECT COUNT(*) FROM pg_class WHERE relkind = ?;"

  PreparedStatement ps = conn.createStatement();
  // Bind variables are enumerated starting with 1;
  ps.setString(1, "r");
  ResultSet rs = ps.executeQuery(sql);

  rs.next();
  long count = rs.getLong(1);

  rs.close();
  ps.close();
     markwkm (PDXPUG)                PGTop for Android                 20 January 2011   24 / 37
More JDBC Code Examples




  The PostgreSQL JDBC documentation has more examples at
  http://jdbc.postgresql.org/documentation/head/




    markwkm (PDXPUG)            PGTop for Android          20 January 2011   25 / 37
Using cursors




   markwkm (PDXPUG)   PGTop for Android   20 January 2011   26 / 37
Executing INSERT, UPDATE, DELETE SQL statements




  markwkm (PDXPUG)          PGTop for Android     20 January 2011   27 / 37
Creating and modifying database objects




   markwkm (PDXPUG)             PGTop for Android   20 January 2011   28 / 37
Calling stored functions




   markwkm (PDXPUG)        PGTop for Android   20 January 2011   29 / 37
Handling binary data




   markwkm (PDXPUG)    PGTop for Android   20 January 2011   30 / 37
JDBC escapes e.g. strings, outer joins, date-time, scalar functions




   markwkm (PDXPUG)               PGTop for Android                   20 January 2011   31 / 37
PostgreSQL Extensions to the JDBC API
  • Geometric data types
  • Large objects
  • Listen/Notify
  • Server prepared statements




   markwkm (PDXPUG)           PGTop for Android   20 January 2011   32 / 37
Connection pools




   markwkm (PDXPUG)   PGTop for Android   20 January 2011   33 / 37
Further JDBC Reading




  JDBC API Documentation and JDBC Specification
  http://jdbc.postgresql.org/documentation/head/reading.html




    markwkm (PDXPUG)         PGTop for Android         20 January 2011   34 / 37
__     __
         / ~~~/  . o O ( Thank you! )
   ,----(     oo    )
  /      __     __/
 /|         ( |(
^    /___ / |
   |__|   |__|-"




  markwkm (PDXPUG)       PGTop for Android   20 January 2011   35 / 37
Acknowledgements

  Hayley Jane Wakenshaw

            __     __
           / ~~~/ 
     ,----(     oo    )
    /      __     __/
   /|         ( |(
  ^    /___ / |
     |__|   |__|-"




    markwkm (PDXPUG)      PGTop for Android   20 January 2011   36 / 37
License



  This work is licensed under a Creative Commons Attribution 3.0 Unported
  License. To view a copy of this license, (a) visit
  http://creativecommons.org/licenses/by/3.0/us/; or, (b) send a
  letter to Creative Commons, 171 2nd Street, Suite 300, San Francisco,
  California, 94105, USA.




     markwkm (PDXPUG)             PGTop for Android               20 January 2011   37 / 37

PGTop for Android: Things I learned making this app

  • 1.
    PGTop for Android ThingsI learned making this app Mark Wong markwkm@postgresql.org Port1and, Oregon, PostgreSQL Users Group 20 January 2011
  • 2.
    PGTop for Android A PostgresSQL stats monitoring app for Android: http://github.com/markwkm/PGTop markwkm (PDXPUG) PGTop for Android 20 January 2011 2 / 37
  • 3.
    How did Iget started? Once upon a time I got developer edition G1 from Google... markwkm (PDXPUG) PGTop for Android 20 January 2011 3 / 37
  • 4.
    And it satidle... • Postgres JDBC driver for Android platform didn’t work • Native C development kit too hard for me to just build just the libpq library from the postgres source markwkm (PDXPUG) PGTop for Android 20 January 2011 4 / 37
  • 5.
    And then somemotivation... __ __ / ~~~/ . o O ( PostgreSQL contest! ) ,----( oo ) / __ __/ /| ( |( ^ /___ / | |__| |__|-" markwkm (PDXPUG) PGTop for Android 20 January 2011 5 / 37
  • 6.
    / / _.-""""-._ .’ ‘. / O O . o O ( Free Nexus One! ) __ +----------------+ __ / +----------------+/ | || || | | || || | | || || | | || || | __/| |__/ __ __ __/ | | | | | | | | __/ __/ markwkm (PDXPUG) PGTop for Android 20 January 2011 6 / 37
  • 7.
    New developments Dave Cramer builds a version of the postgres JDBC jar that works on Android: http://groups.google.com/group/pgandroid/attach/4ee44940c9f7319f/postgresql.jar?part=2 markwkm (PDXPUG) PGTop for Android 20 January 2011 7 / 37
  • 8.
    Slapped together andentered PGTop for Android: markwkm (PDXPUG) PGTop for Android 20 January 2011 8 / 37
  • 9.
    Not a winner.:( markwkm (PDXPUG) PGTop for Android 20 January 2011 9 / 37
  • 10.
    Oh well, continueto make a few improvement... markwkm (PDXPUG) PGTop for Android 20 January 2011 10 / 37
  • 11.
    What did Ilearn along the way? markwkm (PDXPUG) PGTop for Android 20 January 2011 11 / 37
  • 12.
    How to setup a development environment • Requires 32-bit java development environment • Requires Android sdk • Android emulator • Android jars • Android packaging tools • Optional to have Eclipse • ui designer gui • hides xml • Optional to have Android ndk for developing native C library markwkm (PDXPUG) PGTop for Android 20 January 2011 12 / 37
  • 13.
    How to makea launcher icon I need to learn how to use Inkscape or Gimp better to make the ascii art icon meet the recommended guidelines better: markwkm (PDXPUG) PGTop for Android 20 January 2011 13 / 37
  • 14.
    About layouts Only used scroll view and linear layouts. markwkm (PDXPUG) PGTop for Android 20 January 2011 14 / 37
  • 15.
    About ui widgets • TextViews (labels) • EditViews (input text) • Spinners (selection) • Buttons • custom menus markwkm (PDXPUG) PGTop for Android 20 January 2011 15 / 37
  • 16.
    How the threadingmodel works • child threads don’t have access to ui objects • use android.os.Handler to pass messages to parent thread markwkm (PDXPUG) PGTop for Android 20 January 2011 16 / 37
  • 17.
    How PGTop usesthreads Create a Handler: private Handler handler = new Handler() { @Override public void handleMessage(Message msg) { // Update UI objects... } } Child thread loops continuously to query PostgreSQL database for stats and updates class variables with data. Pass a message to a handler to call the method that updates the UI: handler.sendEmptyMessage(0); markwkm (PDXPUG) PGTop for Android 20 January 2011 17 / 37
  • 18.
    How to useSQLite3 interface To store database connection information (oversimplified sample): public class PGConnectionOpenHelper extends SQLiteOpenHelper SQLiteDatabase db = openHelper.getReadableDatabase(); Cursor c = db.rawQuery(SELECT_CONNECTIONS, null); while (c.moveToNext()) { c.getString(c.getColumnIndex("database")); } markwkm (PDXPUG) PGTop for Android 20 January 2011 18 / 37
  • 19.
    How to readthe stdout/stderr log The debugger was too hard for me. markwkm (PDXPUG) PGTop for Android 20 January 2011 19 / 37
  • 20.
    __ __ / ~~~/ . o O ( Code review time! ) ,----( oo ) / __ __/ /| ( |( ^ /___ / | |__| |__|-" markwkm (PDXPUG) PGTop for Android 20 January 2011 20 / 37
  • 21.
    PostgreSQL JDBC Now for some simple examples for connecting to a PostgreSQL database and querying some data. Warning, code formatted to fit better on these slides. . . markwkm (PDXPUG) PGTop for Android 20 January 2011 21 / 37
  • 22.
    Open a DatabaseConnection Load the PostgreSQL JDBC driver and open a database connection using SSL: Class.forName("org.postgresql.Driver"); String url; url = "jdbc:postgresql://pghost:5432/pgdatabase" + "?sslfactory=org.postgresql.ssl.NonValidatingFactory" + "&ssl=true"; Connection conn = DriverManager.getConnection(url, "pguser", "pgpass"); Don’t forget to close the connection when you’re done. conn.close(); markwkm (PDXPUG) PGTop for Android 20 January 2011 22 / 37
  • 23.
    Execute a Query Building on the previous slide, select the name of all relations from pg class and iterate through every row returned: String sql; sql = "SELECT relname FROM pg_class WHERE relkind = ’r’;" Statement st = conn.createStatement(); ResultSet rs = st.executeQuery(sql); while (rs.next()) { Columns are enumerated starting with 1. String relname = rs.getString(1); } rs.close(); st.close(); markwkm (PDXPUG) PGTop for Android 20 January 2011 23 / 37
  • 24.
    Execute a Querywith a Bind Value Building on the previous slide, select the number of all relations from pg class: String sql = "SELECT COUNT(*) FROM pg_class WHERE relkind = ?;" PreparedStatement ps = conn.createStatement(); // Bind variables are enumerated starting with 1; ps.setString(1, "r"); ResultSet rs = ps.executeQuery(sql); rs.next(); long count = rs.getLong(1); rs.close(); ps.close(); markwkm (PDXPUG) PGTop for Android 20 January 2011 24 / 37
  • 25.
    More JDBC CodeExamples The PostgreSQL JDBC documentation has more examples at http://jdbc.postgresql.org/documentation/head/ markwkm (PDXPUG) PGTop for Android 20 January 2011 25 / 37
  • 26.
    Using cursors markwkm (PDXPUG) PGTop for Android 20 January 2011 26 / 37
  • 27.
    Executing INSERT, UPDATE,DELETE SQL statements markwkm (PDXPUG) PGTop for Android 20 January 2011 27 / 37
  • 28.
    Creating and modifyingdatabase objects markwkm (PDXPUG) PGTop for Android 20 January 2011 28 / 37
  • 29.
    Calling stored functions markwkm (PDXPUG) PGTop for Android 20 January 2011 29 / 37
  • 30.
    Handling binary data markwkm (PDXPUG) PGTop for Android 20 January 2011 30 / 37
  • 31.
    JDBC escapes e.g.strings, outer joins, date-time, scalar functions markwkm (PDXPUG) PGTop for Android 20 January 2011 31 / 37
  • 32.
    PostgreSQL Extensions tothe JDBC API • Geometric data types • Large objects • Listen/Notify • Server prepared statements markwkm (PDXPUG) PGTop for Android 20 January 2011 32 / 37
  • 33.
    Connection pools markwkm (PDXPUG) PGTop for Android 20 January 2011 33 / 37
  • 34.
    Further JDBC Reading JDBC API Documentation and JDBC Specification http://jdbc.postgresql.org/documentation/head/reading.html markwkm (PDXPUG) PGTop for Android 20 January 2011 34 / 37
  • 35.
    __ __ / ~~~/ . o O ( Thank you! ) ,----( oo ) / __ __/ /| ( |( ^ /___ / | |__| |__|-" markwkm (PDXPUG) PGTop for Android 20 January 2011 35 / 37
  • 36.
    Acknowledgements HayleyJane Wakenshaw __ __ / ~~~/ ,----( oo ) / __ __/ /| ( |( ^ /___ / | |__| |__|-" markwkm (PDXPUG) PGTop for Android 20 January 2011 36 / 37
  • 37.
    License Thiswork is licensed under a Creative Commons Attribution 3.0 Unported License. To view a copy of this license, (a) visit http://creativecommons.org/licenses/by/3.0/us/; or, (b) send a letter to Creative Commons, 171 2nd Street, Suite 300, San Francisco, California, 94105, USA. markwkm (PDXPUG) PGTop for Android 20 January 2011 37 / 37