private long update(SQLiteDatabase db, Pista pista) {
   db.beginTransaction();
   try {
     // save pista
     int row =
         db.update(
             TABLE,
             getContentValues(pista),
             Columns._UUID.getColumnName() + " = ? ",
             new String[] {pista.getUuid()});
     // save local
     pista.getLocal().setUuidLocalizavel(pista.getUuid());
     localDAO.save(db, pista.getLocal());
     // save horario
     pista.getHorario().setUuidLocalizavel(pista.getUuid());
     horarioDAO.save(db, pista.getHorario());
     // save esportes
     localizavelEsporteDAO.save(db, pista.getUuid(), pista.getEsportes());
     db.setTransactionSuccessful();
     return row;
   } catch (Exception ex) {
     Log.e(TAG, "Error updating pista.", ex);
   } finally {
     db.endTransaction();
   }
   return -1;
 }
 private long delete(SQLiteDatabase db, String uuid) {
   // delete local
   localDAO.delete(db, uuid);
   // delete horario
   horarioDAO.delete(db, uuid);
   // delete esportes
   localizavelEsporteDAO.delete(db, uuid);
   // delete pista
   return db.delete(TABLE, Columns._UUID.getColumnName() + " = ? ", new String[] {uuid});
 }
 private long insert(SQLiteDatabase db, Pista pista) {
   db.beginTransaction();
   try {
     // save pista
     long rowID = db.insertOrThrow(TABLE, null, getContentValues(pista));
     // save local
     pista.getLocal().setUuidLocalizavel(pista.getUuid());
     localDAO.save(db, pista.getLocal());
     // save horario
     pista.getHorario().setUuidLocalizavel(pista.getUuid());
     horarioDAO.save(db, pista.getHorario());
     // save esportes
     localizavelEsporteDAO.save(db, pista.getUuid(), pista.getEsportes());
     db.setTransactionSuccessful();
     return rowID;
   } catch (Exception ex) {
     Log.e(TAG, "Error inserting pista.", ex);
   } finally {
     db.endTransaction();
   }
   return -1;
 }
  private List<Pista> getPistas(
      String uuid, String name, String estado, String esporte, String responsavelUuid) {
    SQLiteDatabase db = dbHelper.getReadableDatabase();
    List<Pista> pistas = new ArrayList<>();
    try {
      SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder();
      queryBuilder.setTables(
          TABLE
              + " INNER JOIN "
              + LocalDAO.TABLE
              + " ON "
              + Columns._UUID.getColumnNameWithTable()
              + " = "
              + LocalDAO.Columns._UUID.getColumnNameWithTable()
              + " LEFT OUTER JOIN "
              + HorarioDAO.TABLE
              + " ON "
              + Columns._UUID.getColumnNameWithTable()
              + " = "
              + HorarioDAO.Columns._UUID.getColumnNameWithTable()
              + " LEFT OUTER JOIN "
              + LocalizavelEsporteDAO.TABLE
              + " ON "
              + Columns._UUID.getColumnNameWithTable()
              + " = "
              + LocalizavelEsporteDAO.Columns._UUID.getColumnNameWithTable()
              + " INNER JOIN "
              + EsporteDAO.TABLE
              + " ON "
              + LocalizavelEsporteDAO.Columns._ESPORTE_ID.getColumnNameWithTable()
              + " = "
              + EsporteDAO.Columns._ID.getColumnNameWithTable());

      StringBuilder selection = new StringBuilder("1 = 1");
      List<String> selectionArgs = new ArrayList<>();
      if (uuid != null && !uuid.isEmpty()) {
        selection.append(" AND ").append(Columns._UUID.getColumnNameWithTable()).append(" = ? ");
        selectionArgs.add(uuid);
      }
      if (name != null && !name.isEmpty()) {
        selection.append(" AND ").append(Columns._NOME.getColumnNameWithTable()).append(" LIKE ? ");
        selectionArgs.add(name + "%");
      }
      if (estado != null && !estado.isEmpty()) {
        selection
            .append(" AND ")
            .append(LocalDAO.Columns._ESTADO.getColumnNameWithTable())
            .append(" = ? ");
        selectionArgs.add(estado);
      }
      if (esporte != null && !esporte.isEmpty()) {
        selection
            .append(" AND ")
            .append(EsporteDAO.Columns._CATEGORIA.getColumnNameWithTable())
            .append(" = ? ");
        selectionArgs.add(esporte);
      }
      if (responsavelUuid != null && !responsavelUuid.isEmpty()) {
        selection
            .append(" AND ")
            .append(Columns._RESPONSAVEL_UUID.getColumnNameWithTable())
            .append(" = ? ");
        selectionArgs.add(responsavelUuid);
      }

      Cursor cursor =
          queryBuilder.query(
              db,
              getColumnsProjectionWithAlias(
                  Columns.class, LocalDAO.Columns.class, HorarioDAO.Columns.class),
              selection.toString(),
              selectionArgs.toArray(new String[selectionArgs.size()]),
              getGroupBy(Columns.class, LocalDAO.Columns.class, HorarioDAO.Columns.class),
              null,
              null);
      if (cursor.moveToFirst()) {
        do {
          Pista pista = getPista(cursor, true);
          pista.setLocal(localDAO.getLocal(cursor, true));
          pista.setHorario(horarioDAO.getHorario(cursor, true));
          pista.setEsportes(localizavelEsporteDAO.getEsportes(db, pista.getUuid()));
          pistas.add(pista);
        } while (cursor.moveToNext());
      }
      cursor.close();
    } catch (Exception ex) {
      Log.e(TAG, "Error getting pistas.", ex);
    } finally {
      dbHelper.close(db);
    }
    return pistas;
  }