{"id":18656850,"url":"https://github.com/zendesk/android-db-commons","last_synced_at":"2025-07-13T07:39:39.621Z","repository":{"id":10078061,"uuid":"12134164","full_name":"zendesk/android-db-commons","owner":"zendesk","description":"Some common utilities for ContentProvider/ContentResolver/Cursor and other db-related android stuff","archived":false,"fork":false,"pushed_at":"2023-04-09T15:19:54.000Z","size":766,"stargazers_count":222,"open_issues_count":8,"forks_count":23,"subscribers_count":105,"default_branch":"master","last_synced_at":"2025-03-24T16:11:05.262Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":"litehelpers/Cordova-SQLitePlugin-legacy-iOS","license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/zendesk.png","metadata":{"files":{"readme":"README.md","changelog":"changelog.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2013-08-15T12:46:08.000Z","updated_at":"2024-06-10T16:08:30.000Z","dependencies_parsed_at":"2024-11-07T07:41:46.576Z","dependency_job_id":null,"html_url":"https://github.com/zendesk/android-db-commons","commit_stats":null,"previous_names":["futuresimple/android-db-commons"],"tags_count":40,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zendesk%2Fandroid-db-commons","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zendesk%2Fandroid-db-commons/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zendesk%2Fandroid-db-commons/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zendesk%2Fandroid-db-commons/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/zendesk","download_url":"https://codeload.github.com/zendesk/android-db-commons/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246763939,"owners_count":20829799,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-07T07:25:34.552Z","updated_at":"2025-04-02T06:08:07.848Z","avatar_url":"https://github.com/zendesk.png","language":"Java","funding_links":[],"categories":[],"sub_categories":[],"readme":"android-db-commons\n==================\n\nWARNING: This library is under heavy development. We can't guarantee both stability of the library itself and the API. However, if you'll find some troubles, bugs, problems please submit an issue here so we can fix it!\n\nSome common utilities for ContentProvider/ContentResolver/Cursor and other db-related android stuff\n\nCurrently it's just a builder for ContentResolver-related crap.\nIf you feel tired of this:\n```java\ngetContentResolver().query(uri, \n  new String[] { People.NAME, People.AGE }, \n  People.NAME + \"=? AND \" + People.AGE + \"\u003e?\", \n  new String[] { \"Ian\", \"18\" }, \n  null\n);\n```\nor:\n```java\ngetContentResolver().query(uri, null, null, null, null);\n```\nUsing this lib you can replace it with something like:\n```java\nProviderAction.newQuery(uri)\n  .projection(People.NAME, People.AGE)\n  .where(People.NAME + \"=?\", \"Ian\")\n  .where(People.AGE + \"\u003e?\", 18)\n  .perform(getContentResolver());\n```\n\nWhat's next? You may want to transform your Cursor to some collection of something. Using this util you can easily do:\n\n```java\nProviderAction.newQuery(uri)\n  .projection(People.NAME, People.AGE)\n  .where(People.NAME + \"=?\", \"Ian\")\n  .where(People.AGE + \"\u003e?\", 18)\n  .perform(getContentResolver());\n  .transform(new Function\u003cCursor, String\u003e() {\n    @Override public String apply(Cursor cursor) {\n      return cursor.getString(cursor.getColumnIndexOrThrow(People.NAME));\n    }\n  })\n  .filter(new Predicate\u003cString\u003e() {\n    @Override public boolean apply(String string) {\n      return string.length()%2 == 0;\n    }\n  });\n  \n```\nLoaders\n-------\nLoaders are fine. They do some hard work for you which otherwise you would need to do manually. But maybe they can be even funnier? \n\nThis is a standard way of creating CursorLoader.\n```java\nlong age = 18L;\nfinal CursorLoader loader = new CursorLoader(getActivity());\nloader.setUri(uri);\nloader.setProjection(new String[] { People.NAME });\nloader.setSelection(People.AGE + \"\u003e?\");\nloader.setSelectionArgs(new String[] { String.valueOf(age) });\n```\nUsing android-db-commons you can build it using this builder:\n```java\nCursorLoaderBuilder.forUri(uri)\n  .projection(People.NAME)\n  .where(People.AGE + \"\u003e?\", 18)\n  .build(getActivity());\n```\nLooks nice, isn't it? Yeah, but it's still not a big change. Anyway, all of us know this:\n```java\n@Override public void onLoadFinished(Loader\u003cCursor\u003e loader, Cursor result) {\n  RealResult result = ReaulResult.veryExpensiveOperationOnMainUiThread(result);\n  myFancyView.setResult(result);\n}\n```\nUsing this library you are able to perform additional operations inside Loader's doInBackground().\n```java\nprivate static final Function\u003cCursor,RealResult\u003e TRANSFORM = new Function\u003cCursor, RealResult\u003e() {\n  @Override public RealResult apply(Cursor input) {\n    return RealResult.veryExpensiveOperationOnMainUiThread(result);\n  }\n};\n\nCursorLoaderBuilder.forUri(uri)\n  .projection(People.NAME)\n  .where(People.AGE + \"\u003e?\", 18)\n  .transform(TRANSFORM)\n  .build(getActivity());\n```\nWanna transform your Cursor into a collection of something? Easy.\n```java\n\nprivate static final Function\u003cCursor,String\u003e ROW_TRANSFORM = new Function\u003cCursor, String\u003e() {\n    @Override public String apply(Cursor cursor) {\n      return cursor.getString(cursor.getColumnIndexOrThrow(People.NAME));\n    }\n  };\n\nCursorLoaderBuilder.forUri(uri)\n  .projection(People.NAME)\n  .where(People.AGE + \"\u003e?\", 18)\n  .transformRow(ROW_TRANSFORM)\n  .build(getActivity());\n```\nYour Loader will return List\u003cString\u003e as a result in this case. But we still can do better - instead of writing simple row transformations by hand we can use a factory method:\n```java\nimport static com.getbase.android.db.cursors.SingleRowTransforms.getColumn;\n\nCursorLoaderBuilder.forUri(uri)\n  .projection(People.NAME)\n  .where(People.AGE + \"\u003e?\", 18)\n  .transformRow(getColumn(People.NAME).asString())\n  .build(getActivity());\n```\nWhat if the Cursor has 100K rows? Transforming it into collection would take way too much time and would have huge memory footprint.\n```java\nCursorLoaderBuilder.forUri(uri)\n  .projection(People.NAME)\n  .where(People.AGE + \"\u003e?\", 18)\n  .transformRow(getColumn(People.NAME).asString())\n  .lazy()\n  .build(getActivity());\n```\nThe result of this loader is lazy list. We do not iterate through your 100K-rows Cursor. Every row's transformation is calculated at its access time.\n\nSure, you can still transform() your transformedRows().\n```java\n// Functions contants here\nCursorLoaderBuilder.forUri(uri)\n  .projection(People.NAME)\n  .where(People.AGE + \"\u003e?\", 18)\n  .transformRow(CURSOR_ROW_TO_STRING)\n  .transformRow(STRING_TO_INTEGER)\n  .transform(LIST_OF_INTEGER_TO_REAL_RESULT)\n  .build(getActivity());\n```\nWARNING: Please make sure you don't leak any Fragment/Activities/other resources when constructing your Loader. In Java, all anonymous nested classes are non-static which means that they are holding a reference to the parent class. As the  Function instances are cached in created Loader instance (which is being reused among multiple Activities/Fragments instances) using anonymous classes can lead to awful memory leaks or even crashes in runtime.\n\nExample leaking code (let's assume it's Fragment instance):\n```java\n@Override\npublic Loader\u003cList\u003cString\u003e\u003e onCreateLoader(int id, Bundle args) {\n  CursorLoaderBuilder.forUri(uri)\n    .projection(People.NAME)\n    .where(People.AGE + \"\u003e?\", 18)\n    .transformRow(new Function\u003cCursor, String\u003e() { // Leaking Fragment's instance here. DO NOT DO THAT!\n      @Override public String apply(Cursor cursor) {\n        return cursor.getString(0);\n      }\n    })\n    .build(getActivity());\n}\n```\n\nIf you don't want to extract all your functions to constants you can use our LoaderHelper that tends to make client's code simpler:\n```java\nprivate static final LoaderHelper\u003cList\u003cString\u003e\u003e loaderHelper = new LoaderHelper\u003cList\u003cString\u003e\u003e(LOADER_ID) {\n  @Override\n  protected Loader\u003cList\u003cString\u003e\u003e onCreateLoader(Context context, Bundle args) {\n    return CursorLoaderBuilder.forUri(Contract.People.CONTENT_URI)\n        .projection(Contract.People.FIRST_NAME, Contract.People.SECOND_NAME)\n        .transformRow(new Function\u003cCursor, String\u003e() {\n          @Override\n          public String apply(Cursor cursor) {\n            return String.format(\"%s %s\", cursor.getString(0), cursor.getString(1));\n          }\n        })\n        .build(context);\n    }\n};\n\n```\nAnd then, when you want to initialize your Loader (let's say in fragment):\n```java\n// let's assume that 'this' implements LoaderHelper.LoaderDataCallbacks\u003cResult\u003e interface.\nloaderHelper.initLoader(getActivity(), bundleArgs, this); \n```\nLoaderHelper.LoaderDataCallbacks' interface is very similar to the one provided by default support-library's LoaderCallbacks so the convertion will be simple and easy.\n\nWrap function is applyied in Loader's doInBackground() so you don't have to worry about ANRs in case you want to do something more complex in there.\n\nFluent SQLite API\n-----------------\nConcatenating SQL strings is not fun. It's very easy to make a syntax error - miss the space, comma or closing bracket - and cause the runtime error. The other problem arises when you want to modify existing query, for example to apply some filters. To ameliorate this issues, we have created fluent API for basic SQLite operations:\n\n```java\nselect()\n    .columns(People.NAME, People.AGE)\n    .from(Tables.PEOPLE)\n    .where(column(People.NAME).eq().arg(), \"Ian\")\n    .where(column(People.AGE).gt().arg(), 18)\n    .perform(db);\n```\n\nNote that `perform()` returns `FluentCursor`, which allows you to easily transform query results into POJOs.\n\nUsage\n-----\nJust add repository and the dependency to your `build.gradle`:\n\n```groovy\ndependencies {\n    compile 'com.getbase.android.db:library:0.15.0'\n}\n```\n\nminSdkVersion = 15\n------------------\nThe last with minSdkVersion = 10 was [v0.10.1](https://github.com/futuresimple/android-db-commons/tree/v0.10.1).\nThe last with minSdkVersion \u003c 10 Android versions was [v0.6.3](https://github.com/futuresimple/android-db-commons/tree/v0.6.3).\n\nOther libraries\n---------------\nandroid-db-commons works even better when combined with some other cool libraries. You may want to try them!\n\n[MicroOrm](https://github.com/chalup/microorm)\n```java\nCursorLoaderBuilder.forUri(myLittleUri)\n  .projection(microOrm.getProjection(Person.class))\n  .transform(microOrm.getFunctionFor(Person.class))\n  .build(getActivity());\n```\n\n## Publishing new version\n\n1. Update `VERSION_NAME` in `gradle.properties` file\n2. Merge PR to master\n3. Create new GitHub release with tag name `v` followed by version - e.g. `v0.15.0`\n4. GitHub Actions will automatically build and publish new package in Maven repository\n\n## Copyright and license\n\nCopyright 2013 Zendesk\n\nLicensed under the [Apache License, Version 2.0](LICENSE)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzendesk%2Fandroid-db-commons","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fzendesk%2Fandroid-db-commons","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzendesk%2Fandroid-db-commons/lists"}