From d48e7c320282b12469875087e759551ccb12ea72 Mon Sep 17 00:00:00 2001 From: BoD Date: Mon, 29 Dec 2014 20:31:21 +0100 Subject: [PATCH] - Do not modify input projection. - Bump to 1.8.4. --- README.md | 4 +- .../sample/provider/SampleProvider.java | 71 ++++++++++++------- pom.xml | 2 +- .../contentprovider.ftl | 17 +++-- 4 files changed, 58 insertions(+), 36 deletions(-) diff --git a/README.md b/README.md index 65d8643..8ee1b00 100644 --- a/README.md +++ b/README.md @@ -122,7 +122,7 @@ https://github.com/BoD/android-contentprovider-generator/releases/latest ### Run the tool -`java -jar android-contentprovider-generator-1.8.3-bundle.jar -i -o ` +`java -jar android-contentprovider-generator-1.8.4-bundle.jar -i -o ` - Input folder: where to find `_config.json` and your entity json files - Output folder: where the resulting files will be generated @@ -211,7 +211,7 @@ You need maven to build this tool. `mvn package` -This will produce `android-contentprovider-generator-1.8.3-bundle.jar` in the `target` folder. +This will produce `android-contentprovider-generator-1.8.4-bundle.jar` in the `target` folder. Similar tools diff --git a/etc/sample/app/src/org/jraf/androidcontentprovidergenerator/sample/provider/SampleProvider.java b/etc/sample/app/src/org/jraf/androidcontentprovidergenerator/sample/provider/SampleProvider.java index 7a620c1..e8aa96f 100644 --- a/etc/sample/app/src/org/jraf/androidcontentprovidergenerator/sample/provider/SampleProvider.java +++ b/etc/sample/app/src/org/jraf/androidcontentprovidergenerator/sample/provider/SampleProvider.java @@ -6,19 +6,19 @@ * \___/_/|_/_/ |_/_/ (_)___/_/ \_, / * /___/ * repository. - * + * * Copyright (C) 2012-2014 Benoit 'BoD' Lubek (BoD@JRAF.org) - * + * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. - * + * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. - * + * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ @@ -42,11 +42,11 @@ import android.util.Log; import org.jraf.androidcontentprovidergenerator.sample.BuildConfig; -import org.jraf.androidcontentprovidergenerator.sample.provider.serialnumber.SerialNumberColumns; -import org.jraf.androidcontentprovidergenerator.sample.provider.personteam.PersonTeamColumns; -import org.jraf.androidcontentprovidergenerator.sample.provider.team.TeamColumns; import org.jraf.androidcontentprovidergenerator.sample.provider.company.CompanyColumns; import org.jraf.androidcontentprovidergenerator.sample.provider.person.PersonColumns; +import org.jraf.androidcontentprovidergenerator.sample.provider.personteam.PersonTeamColumns; +import org.jraf.androidcontentprovidergenerator.sample.provider.serialnumber.SerialNumberColumns; +import org.jraf.androidcontentprovidergenerator.sample.provider.team.TeamColumns; public class SampleProvider extends ContentProvider { private static final String TAG = SampleProvider.class.getSimpleName(); @@ -223,21 +223,24 @@ public Cursor query(Uri uri, String[] projection, String selection, String[] sel Log.d(TAG, "query uri=" + uri + " selection=" + selection + " selectionArgs=" + Arrays.toString(selectionArgs) + " sortOrder=" + sortOrder + " groupBy=" + groupBy); QueryParams queryParams = getQueryParams(uri, selection, projection); - ensureIdIsFullyQualified(projection, queryParams.table); - Cursor res = mSampleSQLiteOpenHelper.getReadableDatabase().query(queryParams.tablesWithJoins, projection, queryParams.selection, selectionArgs, groupBy, - null, sortOrder == null ? queryParams.orderBy : sortOrder); + projection = ensureIdIsFullyQualified(projection, queryParams.table); + Cursor res = mSampleSQLiteOpenHelper.getReadableDatabase().query(queryParams.tablesWithJoins, projection, queryParams.selection, selectionArgs, + groupBy, null, sortOrder == null ? queryParams.orderBy : sortOrder); res.setNotificationUri(getContext().getContentResolver(), uri); return res; } - private void ensureIdIsFullyQualified(String[] projection, String tableName) { - if (projection != null) { - for (int i = 0; i < projection.length; ++i) { - if (projection[i].equals(BaseColumns._ID)) { - projection[i] = tableName + "." + BaseColumns._ID + " AS " + BaseColumns._ID; - } + private String[] ensureIdIsFullyQualified(String[] projection, String tableName) { + if (projection == null) return null; + String[] res = new String[projection.length]; + for (int i = 0; i < projection.length; i++) { + if (projection[i].equals(BaseColumns._ID)) { + res[i] = tableName + "." + BaseColumns._ID + " AS " + BaseColumns._ID; + } else { + res[i] = projection[i]; } } + return res; } @Override @@ -293,19 +296,28 @@ private QueryParams getQueryParams(Uri uri, String selection, String[] projectio res.table = PersonTeamColumns.TABLE_NAME; res.tablesWithJoins = PersonTeamColumns.TABLE_NAME; if (PersonColumns.hasColumns(projection)) { - res.tablesWithJoins += " LEFT OUTER JOIN " + PersonColumns.TABLE_NAME + " AS " + PersonTeamColumns.PREFIX_PERSON + " ON " + PersonTeamColumns.TABLE_NAME + "." + PersonTeamColumns.PERSON_ID + "=" + PersonTeamColumns.PREFIX_PERSON + "." + PersonColumns._ID; + res.tablesWithJoins += " LEFT OUTER JOIN " + PersonColumns.TABLE_NAME + " AS " + PersonTeamColumns.PREFIX_PERSON + " ON " + + PersonTeamColumns.TABLE_NAME + "." + PersonTeamColumns.PERSON_ID + "=" + PersonTeamColumns.PREFIX_PERSON + "." + + PersonColumns._ID; } - if (TeamColumns.hasColumns(projection) || CompanyColumns.hasColumns(projection) || SerialNumberColumns.hasColumns(projection) || SerialNumberColumns.hasColumns(projection)) { - res.tablesWithJoins += " LEFT OUTER JOIN " + TeamColumns.TABLE_NAME + " AS " + PersonTeamColumns.PREFIX_TEAM + " ON " + PersonTeamColumns.TABLE_NAME + "." + PersonTeamColumns.TEAM_ID + "=" + PersonTeamColumns.PREFIX_TEAM + "." + TeamColumns._ID; + if (TeamColumns.hasColumns(projection) || CompanyColumns.hasColumns(projection) || SerialNumberColumns.hasColumns(projection) + || SerialNumberColumns.hasColumns(projection)) { + res.tablesWithJoins += " LEFT OUTER JOIN " + TeamColumns.TABLE_NAME + " AS " + PersonTeamColumns.PREFIX_TEAM + " ON " + + PersonTeamColumns.TABLE_NAME + "." + PersonTeamColumns.TEAM_ID + "=" + PersonTeamColumns.PREFIX_TEAM + "." + TeamColumns._ID; } if (CompanyColumns.hasColumns(projection) || SerialNumberColumns.hasColumns(projection)) { - res.tablesWithJoins += " LEFT OUTER JOIN " + CompanyColumns.TABLE_NAME + " AS " + TeamColumns.PREFIX_COMPANY + " ON " + PersonTeamColumns.PREFIX_TEAM + "." + TeamColumns.COMPANY_ID + "=" + TeamColumns.PREFIX_COMPANY + "." + CompanyColumns._ID; + res.tablesWithJoins += " LEFT OUTER JOIN " + CompanyColumns.TABLE_NAME + " AS " + TeamColumns.PREFIX_COMPANY + " ON " + + PersonTeamColumns.PREFIX_TEAM + "." + TeamColumns.COMPANY_ID + "=" + TeamColumns.PREFIX_COMPANY + "." + CompanyColumns._ID; } if (SerialNumberColumns.hasColumns(projection)) { - res.tablesWithJoins += " LEFT OUTER JOIN " + SerialNumberColumns.TABLE_NAME + " AS " + CompanyColumns.PREFIX_SERIAL_NUMBER + " ON " + TeamColumns.PREFIX_COMPANY + "." + CompanyColumns.SERIAL_NUMBER_ID + "=" + CompanyColumns.PREFIX_SERIAL_NUMBER + "." + SerialNumberColumns._ID; + res.tablesWithJoins += " LEFT OUTER JOIN " + SerialNumberColumns.TABLE_NAME + " AS " + CompanyColumns.PREFIX_SERIAL_NUMBER + " ON " + + TeamColumns.PREFIX_COMPANY + "." + CompanyColumns.SERIAL_NUMBER_ID + "=" + CompanyColumns.PREFIX_SERIAL_NUMBER + "." + + SerialNumberColumns._ID; } if (SerialNumberColumns.hasColumns(projection)) { - res.tablesWithJoins += " LEFT OUTER JOIN " + SerialNumberColumns.TABLE_NAME + " AS " + TeamColumns.PREFIX_SERIAL_NUMBER + " ON " + PersonTeamColumns.PREFIX_TEAM + "." + TeamColumns.SERIAL_NUMBER_ID + "=" + TeamColumns.PREFIX_SERIAL_NUMBER + "." + SerialNumberColumns._ID; + res.tablesWithJoins += " LEFT OUTER JOIN " + SerialNumberColumns.TABLE_NAME + " AS " + TeamColumns.PREFIX_SERIAL_NUMBER + " ON " + + PersonTeamColumns.PREFIX_TEAM + "." + TeamColumns.SERIAL_NUMBER_ID + "=" + TeamColumns.PREFIX_SERIAL_NUMBER + "." + + SerialNumberColumns._ID; } res.orderBy = PersonTeamColumns.DEFAULT_ORDER; break; @@ -315,13 +327,18 @@ private QueryParams getQueryParams(Uri uri, String selection, String[] projectio res.table = TeamColumns.TABLE_NAME; res.tablesWithJoins = TeamColumns.TABLE_NAME; if (CompanyColumns.hasColumns(projection) || SerialNumberColumns.hasColumns(projection)) { - res.tablesWithJoins += " LEFT OUTER JOIN " + CompanyColumns.TABLE_NAME + " AS " + TeamColumns.PREFIX_COMPANY + " ON " + TeamColumns.TABLE_NAME + "." + TeamColumns.COMPANY_ID + "=" + TeamColumns.PREFIX_COMPANY + "." + CompanyColumns._ID; + res.tablesWithJoins += " LEFT OUTER JOIN " + CompanyColumns.TABLE_NAME + " AS " + TeamColumns.PREFIX_COMPANY + " ON " + + TeamColumns.TABLE_NAME + "." + TeamColumns.COMPANY_ID + "=" + TeamColumns.PREFIX_COMPANY + "." + CompanyColumns._ID; } if (SerialNumberColumns.hasColumns(projection)) { - res.tablesWithJoins += " LEFT OUTER JOIN " + SerialNumberColumns.TABLE_NAME + " AS " + CompanyColumns.PREFIX_SERIAL_NUMBER + " ON " + TeamColumns.PREFIX_COMPANY + "." + CompanyColumns.SERIAL_NUMBER_ID + "=" + CompanyColumns.PREFIX_SERIAL_NUMBER + "." + SerialNumberColumns._ID; + res.tablesWithJoins += " LEFT OUTER JOIN " + SerialNumberColumns.TABLE_NAME + " AS " + CompanyColumns.PREFIX_SERIAL_NUMBER + " ON " + + TeamColumns.PREFIX_COMPANY + "." + CompanyColumns.SERIAL_NUMBER_ID + "=" + CompanyColumns.PREFIX_SERIAL_NUMBER + "." + + SerialNumberColumns._ID; } if (SerialNumberColumns.hasColumns(projection)) { - res.tablesWithJoins += " LEFT OUTER JOIN " + SerialNumberColumns.TABLE_NAME + " AS " + TeamColumns.PREFIX_SERIAL_NUMBER + " ON " + TeamColumns.TABLE_NAME + "." + TeamColumns.SERIAL_NUMBER_ID + "=" + TeamColumns.PREFIX_SERIAL_NUMBER + "." + SerialNumberColumns._ID; + res.tablesWithJoins += " LEFT OUTER JOIN " + SerialNumberColumns.TABLE_NAME + " AS " + TeamColumns.PREFIX_SERIAL_NUMBER + " ON " + + TeamColumns.TABLE_NAME + "." + TeamColumns.SERIAL_NUMBER_ID + "=" + TeamColumns.PREFIX_SERIAL_NUMBER + "." + + SerialNumberColumns._ID; } res.orderBy = TeamColumns.DEFAULT_ORDER; break; @@ -331,7 +348,9 @@ private QueryParams getQueryParams(Uri uri, String selection, String[] projectio res.table = CompanyColumns.TABLE_NAME; res.tablesWithJoins = CompanyColumns.TABLE_NAME; if (SerialNumberColumns.hasColumns(projection)) { - res.tablesWithJoins += " LEFT OUTER JOIN " + SerialNumberColumns.TABLE_NAME + " AS " + CompanyColumns.PREFIX_SERIAL_NUMBER + " ON " + CompanyColumns.TABLE_NAME + "." + CompanyColumns.SERIAL_NUMBER_ID + "=" + CompanyColumns.PREFIX_SERIAL_NUMBER + "." + SerialNumberColumns._ID; + res.tablesWithJoins += " LEFT OUTER JOIN " + SerialNumberColumns.TABLE_NAME + " AS " + CompanyColumns.PREFIX_SERIAL_NUMBER + " ON " + + CompanyColumns.TABLE_NAME + "." + CompanyColumns.SERIAL_NUMBER_ID + "=" + CompanyColumns.PREFIX_SERIAL_NUMBER + "." + + SerialNumberColumns._ID; } res.orderBy = CompanyColumns.DEFAULT_ORDER; break; diff --git a/pom.xml b/pom.xml index 5bca82f..677aeee 100644 --- a/pom.xml +++ b/pom.xml @@ -3,7 +3,7 @@ org.jraf android_contentprovider_generator - 1.8.3 + 1.8.4 jar GenerateAndroidProvider diff --git a/src/main/resources/org/jraf/androidcontentprovidergenerator/contentprovider.ftl b/src/main/resources/org/jraf/androidcontentprovidergenerator/contentprovider.ftl index cb851f9..f581c89 100644 --- a/src/main/resources/org/jraf/androidcontentprovidergenerator/contentprovider.ftl +++ b/src/main/resources/org/jraf/androidcontentprovidergenerator/contentprovider.ftl @@ -169,21 +169,24 @@ public class ${config.providerClassName} extends ContentProvider { Log.d(TAG, "query uri=" + uri + " selection=" + selection + " selectionArgs=" + Arrays.toString(selectionArgs) + " sortOrder=" + sortOrder + " groupBy=" + groupBy); QueryParams queryParams = getQueryParams(uri, selection, projection); - ensureIdIsFullyQualified(projection, queryParams.table); + projection = ensureIdIsFullyQualified(projection, queryParams.table); Cursor res = m${config.sqliteOpenHelperClassName}.getReadableDatabase().query(queryParams.tablesWithJoins, projection, queryParams.selection, selectionArgs, groupBy, null, sortOrder == null ? queryParams.orderBy : sortOrder); res.setNotificationUri(getContext().getContentResolver(), uri); return res; } - private void ensureIdIsFullyQualified(String[] projection, String tableName) { - if (projection != null) { - for (int i = 0; i < projection.length; ++i) { - if (projection[i].equals(BaseColumns._ID)) { - projection[i] = tableName + "." + BaseColumns._ID + " AS " + BaseColumns._ID; - } + private String[] ensureIdIsFullyQualified(String[] projection, String tableName) { + if (projection == null) return null; + String[] res = new String[projection.length]; + for (int i = 0; i < projection.length; i++) { + if (projection[i].equals(BaseColumns._ID)) { + res[i] = tableName + "." + BaseColumns._ID + " AS " + BaseColumns._ID; + } else { + res[i] = projection[i]; } } + return res; } @Override