{"action":"created","comment":{"url":"https://api.github.com/repos/yamblz-native/fragments/pulls/comments/74455803","id":74455803,"diff_hunk":"@@ -0,0 +1,138 @@\n+package ru.yandex.yamblz.data_base;\n+\n+import android.content.ContentValues;\n+import android.content.Context;\n+import android.database.Cursor;\n+import android.database.sqlite.SQLiteDatabase;\n+import android.support.annotation.VisibleForTesting;\n+\n+import java.util.List;\n+\n+import ru.yandex.yamblz.model.Artist;\n+\n+class DbBackend implements DbContract {\n+\n+    private final DbOpenHelper mDbOpenHelper;\n+\n+    DbBackend(Context context) {\n+        mDbOpenHelper = new DbOpenHelper(context);\n+        mDbOpenHelper.setWriteAheadLoggingEnabled(true);\n+    }\n+\n+    @VisibleForTesting\n+    DbBackend(DbOpenHelper dbOpenHelper) {\n+        mDbOpenHelper = dbOpenHelper;\n+    }\n+\n+    public Cursor getArtistsList() {\n+        SQLiteDatabase db = mDbOpenHelper.getWritableDatabase();\n+        String tables = ARTISTS + \" INNER JOIN \" + ARTISTS_GENRES + \" ON \" +\n+                ARTISTS + \".\" + Artists.ID + \"=\" + ARTISTS_GENRES + \".\" + ArtistsGenres.ARTIST_ID +\n+                \" INNER JOIN \" + GENRES + \" ON \" +\n+                ARTISTS_GENRES + \".\" + ArtistsGenres.GENRE_ID + \"=\" + GENRES + \".\" + Genres.ID;\n+\n+        String[] columns = new String[]{\n+                ARTISTS + \".\" + Artists.ID,\n+                ARTISTS + \".\" + Artists.NAME,\n+                ARTISTS + \".\" + Artists.LINK,\n+                ARTISTS + \".\" + Artists.TRACKS,\n+                ARTISTS + \".\" + Artists.ALBUMS,\n+                ARTISTS + \".\" + Artists.COVER_BIG,\n+                ARTISTS + \".\" + Artists.COVER_SMALL,\n+                ARTISTS + \".\" + Artists.DESCRIPTION,\n+                \"GROUP_CONCAT(\" + GENRES + \".\" + Genres.NAME + \") AS \" + Artists.GENRES_LIST\n+        };\n+\n+        String groupBy = ARTISTS + \".\" + Artists.NAME;\n+        Cursor c = db.query(tables, columns, null, null, groupBy, null, null);\n+\n+        if (c != null) {\n+            c.moveToFirst();\n+        }\n+\n+        return c;\n+    }\n+\n+    public void addArtistsList(List<Artist> artistList) {\n+        SQLiteDatabase db = mDbOpenHelper.getWritableDatabase();\n+        db.beginTransaction();\n+        try {\n+            Long artistId;\n+            for (Artist artist : artistList) {\n+                artistId = queryArtistByName(db, artist.getName());\n+\n+                if (artistId == null) {\n+                    artistId = addArtist(db, artist);\n+                    addGenres(db, artistId, artist.getGenres());\n+                }\n+            }\n+            db.setTransactionSuccessful();\n+        } finally {\n+            db.endTransaction();\n+        }\n+    }\n+\n+    private Long queryArtistByName(SQLiteDatabase db, String artistName) {\n+        Cursor c = db.query(ARTISTS, new String[]{Artists.ID}, Artists.NAME + \"=?\",\n+                new String[]{artistName}, null, null, null);\n+        return DbUtils.getResultLongAndClose(c);\n+    }\n+\n+    private long addArtist(SQLiteDatabase db, Artist artist) {\n+        ContentValues values = new ContentValues();\n+        values.put(Artists.NAME, artist.getName());\n+        values.put(Artists.LINK, artist.getLink());\n+        values.put(Artists.ALBUMS, artist.getAlbums());\n+        values.put(Artists.TRACKS, artist.getTracks());\n+        values.put(Artists.COVER_BIG, artist.getCover().getBig());\n+        values.put(Artists.COVER_SMALL, artist.getCover().getSmall());\n+        values.put(Artists.DESCRIPTION, artist.getDescription());\n+        return db.insert(ARTISTS, null, values);\n+    }\n+\n+    private void addGenres(SQLiteDatabase db, Long artistId, List<String> genres) {\n+        Long genreId;\n+        Long artistGenreId;\n+        for (String genre : genres) {\n+            genreId = queryGenreByName(db, genre);\n+            if (genreId == null) {\n+                genreId = addGenre(db, genre);\n+            }\n+            artistGenreId = queryArtistGenreByIds(db, artistId, genreId);\n+            if (artistGenreId == null) {\n+                addArtistGenre(db, artistId, genreId);\n+            }\n+        }\n+    }\n+\n+    private long addGenre(SQLiteDatabase db, String genre) {\n+        ContentValues values = new ContentValues();\n+        values.put(Genres.NAME, genre);\n+        return db.insert(GENRES, null, values);\n+    }\n+\n+    private long addArtistGenre(SQLiteDatabase db, Long artistId, Long genreId) {\n+        ContentValues values = new ContentValues();\n+        values.put(ArtistsGenres.ARTIST_ID, artistId);\n+        values.put(ArtistsGenres.GENRE_ID, genreId);\n+        return db.insert(ARTISTS_GENRES, null, values);\n+    }\n+\n+    private Long queryArtistGenreByIds(SQLiteDatabase db, Long artistId, Long genreId) {\n+        Cursor c = db.query(ARTISTS_GENRES, new String[]{ArtistsGenres.ID},\n+                ArtistsGenres.ARTIST_ID + \"=? and \" + ArtistsGenres.GENRE_ID + \"=?\",\n+                new String[]{String.valueOf(artistId), String.valueOf(genreId)}, null, null, null);\n+        return DbUtils.getResultLongAndClose(c);\n+    }\n+\n+    private Long queryGenreByName(SQLiteDatabase db, String genre) {\n+        Cursor c = db.query(GENRES, new String[]{Genres.ID},\n+                Genres.NAME + \"=?\", new String[]{genre}, null, null, null);\n+        return DbUtils.getResultLongAndClose(c);\n+    }\n+\n+//    public void clear() {\n+//        SQLiteDatabase db = mDbOpenHelper.getWritableDatabase();\n+//        mDbOpenHelper.onUpgrade(db, 12, 12);\n+//    }\n+}","path":"provider/src/main/java/ru/yandex/yamblz/data_base/DbBackend.java","position":138,"original_position":138,"commit_id":"f6df72e2c7acc9c15df2249bb20fc537958beced","original_commit_id":"f6df72e2c7acc9c15df2249bb20fc537958beced","user":{"login":"matreshkin","id":5363053,"avatar_url":"https://avatars.githubusercontent.com/u/5363053?v=3","gravatar_id":"","url":"https://api.github.com/users/matreshkin","html_url":"https://github.com/matreshkin","followers_url":"https://api.github.com/users/matreshkin/followers","following_url":"https://api.github.com/users/matreshkin/following{/other_user}","gists_url":"https://api.github.com/users/matreshkin/gists{/gist_id}","starred_url":"https://api.github.com/users/matreshkin/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/matreshkin/subscriptions","organizations_url":"https://api.github.com/users/matreshkin/orgs","repos_url":"https://api.github.com/users/matreshkin/repos","events_url":"https://api.github.com/users/matreshkin/events{/privacy}","received_events_url":"https://api.github.com/users/matreshkin/received_events","type":"User","site_admin":false},"body":"Тестов не будет?","created_at":"2016-08-11T16:28:02Z","updated_at":"2016-08-11T16:28:02Z","html_url":"https://github.com/yamblz-native/fragments/pull/17#discussion_r74455803","pull_request_url":"https://api.github.com/repos/yamblz-native/fragments/pulls/17","_links":{"self":{"href":"https://api.github.com/repos/yamblz-native/fragments/pulls/comments/74455803"},"html":{"href":"https://github.com/yamblz-native/fragments/pull/17#discussion_r74455803"},"pull_request":{"href":"https://api.github.com/repos/yamblz-native/fragments/pulls/17"}}},"pull_request":{"url":"https://api.github.com/repos/yamblz-native/fragments/pulls/17","id":80869238,"html_url":"https://github.com/yamblz-native/fragments/pull/17","diff_url":"https://github.com/yamblz-native/fragments/pull/17.diff","patch_url":"https://github.com/yamblz-native/fragments/pull/17.patch","issue_url":"https://api.github.com/repos/yamblz-native/fragments/issues/17","number":17,"state":"open","locked":false,"title":"Atroshchenko Sergey - @SerG3z","user":{"login":"SerG3z","id":8364447,"avatar_url":"https://avatars.githubusercontent.com/u/8364447?v=3","gravatar_id":"","url":"https://api.github.com/users/SerG3z","html_url":"https://github.com/SerG3z","followers_url":"https://api.github.com/users/SerG3z/followers","following_url":"https://api.github.com/users/SerG3z/following{/other_user}","gists_url":"https://api.github.com/users/SerG3z/gists{/gist_id}","starred_url":"https://api.github.com/users/SerG3z/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/SerG3z/subscriptions","organizations_url":"https://api.github.com/users/SerG3z/orgs","repos_url":"https://api.github.com/users/SerG3z/repos","events_url":"https://api.github.com/users/SerG3z/events{/privacy}","received_events_url":"https://api.github.com/users/SerG3z/received_events","type":"User","site_admin":false},"body":"контент провайдер ,бд и фрагменты","created_at":"2016-08-10T21:17:11Z","updated_at":"2016-08-11T16:28:02Z","closed_at":null,"merged_at":null,"merge_commit_sha":"7ec0b342b584707700127a6249c15362dddccb84","assignee":null,"assignees":[],"milestone":null,"commits_url":"https://api.github.com/repos/yamblz-native/fragments/pulls/17/commits","review_comments_url":"https://api.github.com/repos/yamblz-native/fragments/pulls/17/comments","review_comment_url":"https://api.github.com/repos/yamblz-native/fragments/pulls/comments{/number}","comments_url":"https://api.github.com/repos/yamblz-native/fragments/issues/17/comments","statuses_url":"https://api.github.com/repos/yamblz-native/fragments/statuses/f6df72e2c7acc9c15df2249bb20fc537958beced","head":{"label":"SerG3z:master","ref":"master","sha":"f6df72e2c7acc9c15df2249bb20fc537958beced","user":{"login":"SerG3z","id":8364447,"avatar_url":"https://avatars.githubusercontent.com/u/8364447?v=3","gravatar_id":"","url":"https://api.github.com/users/SerG3z","html_url":"https://github.com/SerG3z","followers_url":"https://api.github.com/users/SerG3z/followers","following_url":"https://api.github.com/users/SerG3z/following{/other_user}","gists_url":"https://api.github.com/users/SerG3z/gists{/gist_id}","starred_url":"https://api.github.com/users/SerG3z/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/SerG3z/subscriptions","organizations_url":"https://api.github.com/users/SerG3z/orgs","repos_url":"https://api.github.com/users/SerG3z/repos","events_url":"https://api.github.com/users/SerG3z/events{/privacy}","received_events_url":"https://api.github.com/users/SerG3z/received_events","type":"User","site_admin":false},"repo":{"id":65082733,"name":"fragments","full_name":"SerG3z/fragments","owner":{"login":"SerG3z","id":8364447,"avatar_url":"https://avatars.githubusercontent.com/u/8364447?v=3","gravatar_id":"","url":"https://api.github.com/users/SerG3z","html_url":"https://github.com/SerG3z","followers_url":"https://api.github.com/users/SerG3z/followers","following_url":"https://api.github.com/users/SerG3z/following{/other_user}","gists_url":"https://api.github.com/users/SerG3z/gists{/gist_id}","starred_url":"https://api.github.com/users/SerG3z/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/SerG3z/subscriptions","organizations_url":"https://api.github.com/users/SerG3z/orgs","repos_url":"https://api.github.com/users/SerG3z/repos","events_url":"https://api.github.com/users/SerG3z/events{/privacy}","received_events_url":"https://api.github.com/users/SerG3z/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/SerG3z/fragments","description":"fragments homework","fork":true,"url":"https://api.github.com/repos/SerG3z/fragments","forks_url":"https://api.github.com/repos/SerG3z/fragments/forks","keys_url":"https://api.github.com/repos/SerG3z/fragments/keys{/key_id}","collaborators_url":"https://api.github.com/repos/SerG3z/fragments/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/SerG3z/fragments/teams","hooks_url":"https://api.github.com/repos/SerG3z/fragments/hooks","issue_events_url":"https://api.github.com/repos/SerG3z/fragments/issues/events{/number}","events_url":"https://api.github.com/repos/SerG3z/fragments/events","assignees_url":"https://api.github.com/repos/SerG3z/fragments/assignees{/user}","branches_url":"https://api.github.com/repos/SerG3z/fragments/branches{/branch}","tags_url":"https://api.github.com/repos/SerG3z/fragments/tags","blobs_url":"https://api.github.com/repos/SerG3z/fragments/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/SerG3z/fragments/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/SerG3z/fragments/git/refs{/sha}","trees_url":"https://api.github.com/repos/SerG3z/fragments/git/trees{/sha}","statuses_url":"https://api.github.com/repos/SerG3z/fragments/statuses/{sha}","languages_url":"https://api.github.com/repos/SerG3z/fragments/languages","stargazers_url":"https://api.github.com/repos/SerG3z/fragments/stargazers","contributors_url":"https://api.github.com/repos/SerG3z/fragments/contributors","subscribers_url":"https://api.github.com/repos/SerG3z/fragments/subscribers","subscription_url":"https://api.github.com/repos/SerG3z/fragments/subscription","commits_url":"https://api.github.com/repos/SerG3z/fragments/commits{/sha}","git_commits_url":"https://api.github.com/repos/SerG3z/fragments/git/commits{/sha}","comments_url":"https://api.github.com/repos/SerG3z/fragments/comments{/number}","issue_comment_url":"https://api.github.com/repos/SerG3z/fragments/issues/comments{/number}","contents_url":"https://api.github.com/repos/SerG3z/fragments/contents/{+path}","compare_url":"https://api.github.com/repos/SerG3z/fragments/compare/{base}...{head}","merges_url":"https://api.github.com/repos/SerG3z/fragments/merges","archive_url":"https://api.github.com/repos/SerG3z/fragments/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/SerG3z/fragments/downloads","issues_url":"https://api.github.com/repos/SerG3z/fragments/issues{/number}","pulls_url":"https://api.github.com/repos/SerG3z/fragments/pulls{/number}","milestones_url":"https://api.github.com/repos/SerG3z/fragments/milestones{/number}","notifications_url":"https://api.github.com/repos/SerG3z/fragments/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/SerG3z/fragments/labels{/name}","releases_url":"https://api.github.com/repos/SerG3z/fragments/releases{/id}","deployments_url":"https://api.github.com/repos/SerG3z/fragments/deployments","created_at":"2016-08-06T13:01:14Z","updated_at":"2016-08-06T13:01:15Z","pushed_at":"2016-08-11T01:52:49Z","git_url":"git://github.com/SerG3z/fragments.git","ssh_url":"git@github.com:SerG3z/fragments.git","clone_url":"https://github.com/SerG3z/fragments.git","svn_url":"https://github.com/SerG3z/fragments","homepage":null,"size":2279,"stargazers_count":0,"watchers_count":0,"language":"Java","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"yamblz-native:master","ref":"master","sha":"9041fcf8dddd13d7a4c122036d2353e6cf7ac5a6","user":{"login":"yamblz-native","id":20421172,"avatar_url":"https://avatars.githubusercontent.com/u/20421172?v=3","gravatar_id":"","url":"https://api.github.com/users/yamblz-native","html_url":"https://github.com/yamblz-native","followers_url":"https://api.github.com/users/yamblz-native/followers","following_url":"https://api.github.com/users/yamblz-native/following{/other_user}","gists_url":"https://api.github.com/users/yamblz-native/gists{/gist_id}","starred_url":"https://api.github.com/users/yamblz-native/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/yamblz-native/subscriptions","organizations_url":"https://api.github.com/users/yamblz-native/orgs","repos_url":"https://api.github.com/users/yamblz-native/repos","events_url":"https://api.github.com/users/yamblz-native/events{/privacy}","received_events_url":"https://api.github.com/users/yamblz-native/received_events","type":"Organization","site_admin":false},"repo":{"id":64746710,"name":"fragments","full_name":"yamblz-native/fragments","owner":{"login":"yamblz-native","id":20421172,"avatar_url":"https://avatars.githubusercontent.com/u/20421172?v=3","gravatar_id":"","url":"https://api.github.com/users/yamblz-native","html_url":"https://github.com/yamblz-native","followers_url":"https://api.github.com/users/yamblz-native/followers","following_url":"https://api.github.com/users/yamblz-native/following{/other_user}","gists_url":"https://api.github.com/users/yamblz-native/gists{/gist_id}","starred_url":"https://api.github.com/users/yamblz-native/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/yamblz-native/subscriptions","organizations_url":"https://api.github.com/users/yamblz-native/orgs","repos_url":"https://api.github.com/users/yamblz-native/repos","events_url":"https://api.github.com/users/yamblz-native/events{/privacy}","received_events_url":"https://api.github.com/users/yamblz-native/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/yamblz-native/fragments","description":"fragments homework","fork":false,"url":"https://api.github.com/repos/yamblz-native/fragments","forks_url":"https://api.github.com/repos/yamblz-native/fragments/forks","keys_url":"https://api.github.com/repos/yamblz-native/fragments/keys{/key_id}","collaborators_url":"https://api.github.com/repos/yamblz-native/fragments/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/yamblz-native/fragments/teams","hooks_url":"https://api.github.com/repos/yamblz-native/fragments/hooks","issue_events_url":"https://api.github.com/repos/yamblz-native/fragments/issues/events{/number}","events_url":"https://api.github.com/repos/yamblz-native/fragments/events","assignees_url":"https://api.github.com/repos/yamblz-native/fragments/assignees{/user}","branches_url":"https://api.github.com/repos/yamblz-native/fragments/branches{/branch}","tags_url":"https://api.github.com/repos/yamblz-native/fragments/tags","blobs_url":"https://api.github.com/repos/yamblz-native/fragments/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/yamblz-native/fragments/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/yamblz-native/fragments/git/refs{/sha}","trees_url":"https://api.github.com/repos/yamblz-native/fragments/git/trees{/sha}","statuses_url":"https://api.github.com/repos/yamblz-native/fragments/statuses/{sha}","languages_url":"https://api.github.com/repos/yamblz-native/fragments/languages","stargazers_url":"https://api.github.com/repos/yamblz-native/fragments/stargazers","contributors_url":"https://api.github.com/repos/yamblz-native/fragments/contributors","subscribers_url":"https://api.github.com/repos/yamblz-native/fragments/subscribers","subscription_url":"https://api.github.com/repos/yamblz-native/fragments/subscription","commits_url":"https://api.github.com/repos/yamblz-native/fragments/commits{/sha}","git_commits_url":"https://api.github.com/repos/yamblz-native/fragments/git/commits{/sha}","comments_url":"https://api.github.com/repos/yamblz-native/fragments/comments{/number}","issue_comment_url":"https://api.github.com/repos/yamblz-native/fragments/issues/comments{/number}","contents_url":"https://api.github.com/repos/yamblz-native/fragments/contents/{+path}","compare_url":"https://api.github.com/repos/yamblz-native/fragments/compare/{base}...{head}","merges_url":"https://api.github.com/repos/yamblz-native/fragments/merges","archive_url":"https://api.github.com/repos/yamblz-native/fragments/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/yamblz-native/fragments/downloads","issues_url":"https://api.github.com/repos/yamblz-native/fragments/issues{/number}","pulls_url":"https://api.github.com/repos/yamblz-native/fragments/pulls{/number}","milestones_url":"https://api.github.com/repos/yamblz-native/fragments/milestones{/number}","notifications_url":"https://api.github.com/repos/yamblz-native/fragments/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/yamblz-native/fragments/labels{/name}","releases_url":"https://api.github.com/repos/yamblz-native/fragments/releases{/id}","deployments_url":"https://api.github.com/repos/yamblz-native/fragments/deployments","created_at":"2016-08-02T10:10:49Z","updated_at":"2016-08-02T10:11:12Z","pushed_at":"2016-08-11T15:49:14Z","git_url":"git://github.com/yamblz-native/fragments.git","ssh_url":"git@github.com:yamblz-native/fragments.git","clone_url":"https://github.com/yamblz-native/fragments.git","svn_url":"https://github.com/yamblz-native/fragments","homepage":null,"size":2245,"stargazers_count":0,"watchers_count":0,"language":"Java","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":20,"mirror_url":null,"open_issues_count":18,"forks":20,"open_issues":18,"watchers":0,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/yamblz-native/fragments/pulls/17"},"html":{"href":"https://github.com/yamblz-native/fragments/pull/17"},"issue":{"href":"https://api.github.com/repos/yamblz-native/fragments/issues/17"},"comments":{"href":"https://api.github.com/repos/yamblz-native/fragments/issues/17/comments"},"review_comments":{"href":"https://api.github.com/repos/yamblz-native/fragments/pulls/17/comments"},"review_comment":{"href":"https://api.github.com/repos/yamblz-native/fragments/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/yamblz-native/fragments/pulls/17/commits"},"statuses":{"href":"https://api.github.com/repos/yamblz-native/fragments/statuses/f6df72e2c7acc9c15df2249bb20fc537958beced"}}}}
{"actor":{"display_login":"matreshkin"}}