SQLiteColumnInfo.java

  1. package net.zer0bandwidth.android.lib.database;

  2. import android.database.Cursor;
  3. import android.database.sqlite.SQLiteDatabase;

  4. import java.util.ArrayList;
  5. import java.util.HashMap;
  6. import java.util.List;
  7. import java.util.Map;

  8. /**
  9.  * Contains information returned by the {@code table_info} pragma in SQLite.
  10.  * @since zer0bandwidth-net/android 0.1.4 (#26)
  11.  */
  12. public class SQLiteColumnInfo
  13. {
  14. /**
  15.  * Gathers information about the columns in the specified table, and returns
  16.  * the column descriptions in a list.
  17.  * @param db the database to be analyzed
  18.  * @param sTableName the name of the table to be analyzed
  19.  * @return a list of objects describing the columns of the table
  20.  * @see SQLitePortal#getColumnListForTable
  21.  */
  22. public static List<SQLiteColumnInfo> gatherColumnList(
  23. SQLiteDatabase db, String sTableName )
  24. {
  25. Cursor crs = null ;
  26. List<SQLiteColumnInfo> aInfo = new ArrayList<>() ;
  27. try
  28. {
  29. crs = db.rawQuery(
  30. String.format( "PRAGMA main.table_info('%s')", sTableName ),
  31. null ) ;
  32. if( crs.moveToFirst() )
  33. {
  34. do
  35. {
  36. SQLiteColumnInfo info = new SQLiteColumnInfo() ;
  37. info.sTableName = sTableName ;
  38. info.nColumnID = crs.getInt(0) ;
  39. info.sColumnName = crs.getString(1) ;
  40. info.sColumnType = crs.getString(2) ;
  41. info.bNotNull = SQLitePortal.intToBool( crs.getInt(3) ) ;
  42. info.sDefault = crs.getString(4) ;
  43. info.bPrimaryKey = SQLitePortal.intToBool( crs.getInt(5) ) ;
  44. aInfo.add(info) ;
  45. } while( crs.moveToNext() ) ;
  46. }
  47. }
  48. finally
  49. { SQLitePortal.closeCursor(crs) ; }

  50. return aInfo ;
  51. }

  52. /**
  53.  * Gathers information about the columns in the specified table, and returns
  54.  * the column descriptions in a map.
  55.  * @param db the database to be analyzed
  56.  * @param sTableName the name of the table to be analyzed
  57.  * @return a map of column names to objects describing those columns
  58.  * @see SQLitePortal#getColumnMapForTable
  59.  */
  60. public static Map<String,SQLiteColumnInfo> gatherColumnMap(
  61. SQLiteDatabase db, String sTableName )
  62. {
  63. List<SQLiteColumnInfo> aInfo = gatherColumnList( db, sTableName ) ;
  64. Map<String,SQLiteColumnInfo> mapInfo = new HashMap<>() ;
  65. for( SQLiteColumnInfo info : aInfo )
  66. mapInfo.put( info.sColumnName, info ) ;
  67. return mapInfo ;
  68. }

  69. /** The name of the table containing this column. */
  70. public String sTableName = null ;

  71. /** The numeric ID of the column in any given row. */
  72. public int nColumnID = -1 ;

  73. /** The name of the column in the table definition. */
  74. public String sColumnName = null ;

  75. /** The magic SQLite token indicating this column's data type. */
  76. public String sColumnType = null ;

  77. /** Indicates whether the column is defined {@code NOT NULL}. */
  78. public boolean bNotNull = false ;

  79. /** The default value of the column, as a string. */
  80. public String sDefault = null ;

  81. /** Indicates whether the column is the table's primary key. */
  82. public boolean bPrimaryKey = false ;
  83. }