Welcome to mirror list, hosted at ThFree Co, Russian Federation.

DatabaseConnection.java « database « owncloudnewsreader « luhmer « de « java « main « src « News-Android-App - github.com/nextcloud/news-android.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 49e8481b75d6d8b376fac9bf87b47a941d624ef8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
/**
* Android ownCloud News
*
* @author David Luhmer
* @copyright 2013 David Luhmer david-dev@live.de
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
* License as published by the Free Software Foundation; either
* version 3 of the License, or any later version.
*
* This library 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 AFFERO GENERAL PUBLIC LICENSE for more details.
*
* You should have received a copy of the GNU Affero General Public
* License along with this library.  If not, see <http://www.gnu.org/licenses/>.
*
*/

package de.luhmer.owncloudnewsreader.database;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import android.util.SparseArray;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import de.luhmer.owncloudnewsreader.Constants;
import de.luhmer.owncloudnewsreader.ListView.UnreadFolderCount;
import de.luhmer.owncloudnewsreader.model.AudioPodcastItem;
import de.luhmer.owncloudnewsreader.model.PodcastFeedItem;
import de.luhmer.owncloudnewsreader.model.RssFile;

import static de.luhmer.owncloudnewsreader.ListView.SubscriptionExpandableListAdapter.SPECIAL_FOLDERS.ALL_ITEMS;
import static de.luhmer.owncloudnewsreader.ListView.SubscriptionExpandableListAdapter.SPECIAL_FOLDERS.ALL_STARRED_ITEMS;
import static de.luhmer.owncloudnewsreader.ListView.SubscriptionExpandableListAdapter.SPECIAL_FOLDERS.ALL_UNREAD_ITEMS;

public class DatabaseConnection {
	private DatabaseHelper openHelper;
    private SQLiteDatabase database;


    public SparseArray<String> getUnreadItemCountForFolder(Context mContext) {
        String buildSQL = "SELECT f.rowid, COUNT(" + RSS_ITEM_RSSITEM_ID + ")" +
                " FROM " + RSS_ITEM_TABLE + " rss " +
                " JOIN " + SUBSCRIPTION_TABLE + " st ON rss." + RSS_ITEM_SUBSCRIPTION_ID + " = st.rowid" +
                " JOIN " + FOLDER_TABLE + " f ON st." + SUBSCRIPTION_FOLDER_ID + " = f.rowid" +
                " WHERE " + RSS_ITEM_READ_TEMP + " != 1 " +
                " GROUP BY f.rowid";

        SparseArray<String> values = getSparseArrayFromSQL(buildSQL, 0, 1);


        values.put(ALL_UNREAD_ITEMS.getValue(), new UnreadFolderCount(mContext, ALL_UNREAD_ITEMS.getValueString()).getText());
        values.put(ALL_STARRED_ITEMS.getValue(), new UnreadFolderCount(mContext, ALL_STARRED_ITEMS.getValueString()).getText());


        return values;
    }

    public SparseArray<String> getUnreadItemCountForFeed() {
        String buildSQL = "SELECT " + RSS_ITEM_SUBSCRIPTION_ID + ", COUNT(" + RSS_ITEM_RSSITEM_ID + ")" + // rowid as _id,
                " FROM " + RSS_ITEM_TABLE +
                " WHERE " + RSS_ITEM_READ_TEMP + " != 1 " +
                " GROUP BY " + RSS_ITEM_SUBSCRIPTION_ID;

        return getSparseArrayFromSQL(buildSQL, 0, 1);
    }

    public SparseArray<String> getUrlsToFavIcons() {
        String buildSQL = "SELECT " + SUBSCRIPTION_ID + ", " + SUBSCRIPTION_FAVICON_URL +
                " FROM " + SUBSCRIPTION_TABLE;
        return getSparseArrayFromSQL(buildSQL, 0, 1);
    }



    public static final String FOLDER_TABLE = "folder";
    public static final String FOLDER_LABEL = "label";
    public static final String FOLDER_LABEL_ID = "label_id";

    public static final String SUBSCRIPTION_TABLE = "subscription";
    public static final String SUBSCRIPTION_HEADERTEXT = "header_text";
    //public static final String SUBSCRIPTION_SUBSCRIPTION_ID = "subscription_id_subscription";
    public static final String SUBSCRIPTION_FOLDER_ID = "folder_idfolder";
    public static final String SUBSCRIPTION_ID = "subscription_id";
    public static final String SUBSCRIPTION_FAVICON_URL = "favicon_url";
    public static final String SUBSCRIPTION_LINK = "link";
    public static final String SUBSCRIPTION_AVG_COLOUR = "avg_colour";

    public static final String RSS_ITEM_TABLE = "rss_item";
    public static final String RSS_ITEM_SUBSCRIPTION_ID = "subscription_id_subscription";
    public static final String RSS_ITEM_TITLE = "title";
    public static final String RSS_ITEM_LINK = "link";
    public static final String RSS_ITEM_BODY = "body";
    public static final String RSS_ITEM_READ = "read";
    public static final String RSS_ITEM_RSSITEM_ID = "rssitem_id";
    public static final String RSS_ITEM_STARRED = "starred";
    public static final String RSS_ITEM_PUBDATE = "pubdate";
    public static final String RSS_ITEM_AUTHOR = "author";
    public static final String RSS_ITEM_GUID = "guid";
    public static final String RSS_ITEM_GUIDHASH = "guidHash";
    public static final String RSS_ITEM_LAST_MODIFIED = "lastModified";

    public static final String RSS_ITEM_READ_TEMP = "read_temp";
    public static final String RSS_ITEM_STARRED_TEMP = "starred_temp";


    public static final String RSS_CURRENT_VIEW_TABLE = "rss_current_view";
    public static final String RSS_CURRENT_VIEW_RSS_ITEM_ID = "rss_current_view_rss_item_id";

    public static final String RSS_ITEM_ENC_MIME = "enclosureMime";
    public static final String RSS_ITEM_ENC_LINK = "enclosureLink";



    public enum SORT_DIRECTION { asc, desc }


    public static final boolean DATABASE_DEBUG_MODE = false; //(false && Constants.DEBUG_MODE) ? true: false;


    public DatabaseConnection(Context aContext) {
        //openHelper = new DatabaseHelper(aContext);
    	openHelper = DatabaseHelper.getHelper(aContext);
        openDatabase();
    }

    public SQLiteDatabase getDatabase()
    {
    	return database;
    }

    public DatabaseHelper getHelper() {
    	return openHelper;
    }

    public void clearDatabaseOverSize()
	{
		//If i have 9023 rows in the database, when i run that query it should delete 8023 rows and leave me with 1000
		//database.execSQL("DELETE FROM " + RSS_ITEM_TABLE + " WHERE " +  + "ORDER BY rowid DESC LIMIT 1000 *

		//Let's say it said 1005 - you need to delete 5 rows.
		//DELETE FROM table ORDER BY dateRegistered ASC LIMIT 5


    	int max = Constants.maxItemsCount;
    	int total = (int) getLongValueBySQL("SELECT COUNT(*) FROM rss_item");
    	int unread = (int) getLongValueBySQL("SELECT COUNT(*) FROM rss_item WHERE read_temp != 1");
    	int read = total - unread;

    	if(total > max)
    	{
    		int overSize = total - max;
    		//Soll verhindern, dass ungelesene Artikel gelöscht werden
    		if(overSize > read)
    			overSize = read;
     		database.execSQL("DELETE FROM rss_item WHERE rowid IN (SELECT rowid FROM rss_item WHERE read_temp = 1 AND starred_temp != 1 ORDER BY rssitem_id asc LIMIT " + overSize + ")");
    		/* SELECT * FROM rss_item WHERE read_temp = 1 ORDER BY rowid asc LIMIT 3; */
    	}
	}

    /*
    public void markAllItemsAsRead(List<Integer> itemIds)
	{
		List<String> items = new ArrayList<String>();
		for(Integer itemId : itemIds)
			items.add(String.valueOf(itemId));
		markAllItemsAsReadUnread(items, true);
	}
    */

    public void markAllItemsAsReadForCurrentView()
	{
    	String sql = "UPDATE " + RSS_ITEM_TABLE + " SET " + RSS_ITEM_READ_TEMP + " = 1 WHERE " + RSS_ITEM_RSSITEM_ID +
    			" IN (SELECT " + RSS_CURRENT_VIEW_RSS_ITEM_ID + " FROM " + RSS_CURRENT_VIEW_TABLE + ")";
		database.execSQL(sql);
	}

    public void markAllItemsAsReadUnread(List<String> itemIds, boolean markAsRead)
	{
		if(itemIds != null)
			for(String idItem : itemIds)
				updateIsReadOfItem(idItem, markAsRead);
	}

    /**
     * Changes the read unread state of the item. This is NOT the temp value!!!
     * @param itemIds
     * @param markAsRead
     */
    public void change_readUnreadStateOfItem(List<String> itemIds, boolean markAsRead)
	{
		if(itemIds != null)
			for(String idItem : itemIds)
				updateIsReadOfItemNotTemp(idItem, markAsRead);
	}

    /**
     * Changes the starred unstarred state of the item. This is NOT the temp value!!!
     * @param itemIds
     * @param markAsStarred
     */
    public void change_starrUnstarrStateOfItem(List<String> itemIds, boolean markAsStarred)
	{
		if(itemIds != null)
			for(String idItem : itemIds)
				updateIsStarredOfFeedNotTemp(idItem, markAsStarred);
	}

    public int getCountOfAllItems(boolean execludeStarred)
    {
    	String buildSQL = "SELECT count(*) FROM " + RSS_ITEM_TABLE;
    	if(execludeStarred)
    		buildSQL += " WHERE " + RSS_ITEM_STARRED_TEMP + " != 1";

        int result = 0;

        Cursor cursor = database.rawQuery(buildSQL, null);
        try
        {
        	if(cursor != null)
        	{
        		cursor.moveToFirst();
        		result = cursor.getInt(0);
        	}
        } finally {
        	cursor.close();
        }

        return result;
    }

    public long getLastModified()
    {
    	String buildSQL = "SELECT MAX(" + RSS_ITEM_LAST_MODIFIED + ") FROM " + RSS_ITEM_TABLE;
        long result = 0;

        Cursor cursor = database.rawQuery(buildSQL, null);
        try
        {
        	if(cursor != null)
        	{
        		cursor.moveToFirst();
        		result = cursor.getLong(0);
        	}
        } finally {
        	cursor.close();
        }

        return result;
    }

    public long getItemDbIdAtPosition(int positionXRow)
    {
    	String buildSQL = "SELECT rowid FROM " + RSS_ITEM_TABLE;
        long result = -1;

        Cursor cursor = database.rawQuery(buildSQL, null);
        try
        {
        	if(cursor != null)
        	{
        		if(cursor.move(cursor.getCount() - (positionXRow - 1)))
        			result = cursor.getLong(0);
        	}
        } finally {
        	cursor.close();
        }

        return result;
    }

    public List<String> getAllNewReadItems()
    {
    	String buildSQL = "SELECT " + RSS_ITEM_RSSITEM_ID + " FROM " + RSS_ITEM_TABLE + " WHERE "
    						+ RSS_ITEM_READ_TEMP + " = 1 AND "
    						+ RSS_ITEM_READ + " = 0";
    	Cursor cursor = database.rawQuery(buildSQL, null);
    	return convertCursorToStringArray(cursor, 0);
    }

    public List<String> getAllNewUnreadItems()
    {
    	String buildSQL = "SELECT " + RSS_ITEM_RSSITEM_ID + " FROM " + RSS_ITEM_TABLE + " WHERE "
    						+ RSS_ITEM_READ_TEMP + " = 0 AND "
    						+ RSS_ITEM_READ + " = 1";
    	Cursor cursor = database.rawQuery(buildSQL, null);
    	return convertCursorToStringArray(cursor, 0);
    }

    public List<String> getAllNewStarredItems()
    {
    	String buildSQL = "SELECT " + RSS_ITEM_RSSITEM_ID + " FROM " + RSS_ITEM_TABLE + " WHERE "
    						+ RSS_ITEM_STARRED_TEMP + " = 1 AND "
    						+ RSS_ITEM_STARRED + " = 0";
    	Cursor cursor = database.rawQuery(buildSQL, null);
    	return convertCursorToStringArray(cursor, 0);
    }

    public List<String> getAllNewUnstarredItems()
    {
    	String buildSQL = "SELECT " + RSS_ITEM_RSSITEM_ID + " FROM " + RSS_ITEM_TABLE + " WHERE "
    						+ RSS_ITEM_STARRED_TEMP + " = 0 AND "
    						+ RSS_ITEM_STARRED + " = 1";
    	Cursor cursor = database.rawQuery(buildSQL, null);
    	return convertCursorToStringArray(cursor, 0);
    }

    public Cursor getAllItemsWithIdHigher(String id_item) {
		//String buildSQL = "SELECT rowid as _id, * FROM " + SUBSCRIPTION_TABLE + " WHERE subscription_id_subscription IS NULL";
		String buildSQL = "SELECT rowid as _id, * FROM " + RSS_ITEM_TABLE + " WHERE " + RSS_ITEM_RSSITEM_ID + " > " + id_item;


        if(DATABASE_DEBUG_MODE)
        	Log.d("DB_HELPER", "getAllItemsWithIdHigher SQL: " + buildSQL);
        return database.rawQuery(buildSQL, null);
    }

    public long getLowestItemId(boolean onlyStarred)
    {
    	String buildSQL = "SELECT MIN(" + RSS_ITEM_RSSITEM_ID + ") FROM " + RSS_ITEM_TABLE;

    	if(onlyStarred)
    		buildSQL += " WHERE " + RSS_ITEM_STARRED_TEMP + " = 1";

    	return getLongValueBySQL(buildSQL);
    }

    public long getLowestItemIdByFeed(String id_feed)
    {
    	String buildSQL = "SELECT MIN(" + RSS_ITEM_RSSITEM_ID + ") FROM " + RSS_ITEM_TABLE + " WHERE " + RSS_ITEM_SUBSCRIPTION_ID + " = " + id_feed;
    	return getLongValueBySQL(buildSQL);
    }

    public long getLowestItemIdByFolder(String id_folder)
    {
    	String buildSQL = "SELECT MIN(" + RSS_ITEM_RSSITEM_ID + ") " +
    			"FROM " + RSS_ITEM_TABLE + " rss " +
    			"JOIN " + SUBSCRIPTION_TABLE + " sc ON rss." + RSS_ITEM_SUBSCRIPTION_ID + " = sc.rowid " +
    			"WHERE " + SUBSCRIPTION_FOLDER_ID + " = " + id_folder;
    	return getLongValueBySQL(buildSQL);
    }

    public long getLowestItemIdStarred()
    {
    	String buildSQL = "SELECT MIN(" + RSS_ITEM_RSSITEM_ID + ") FROM " + RSS_ITEM_TABLE + " WHERE " + RSS_ITEM_STARRED_TEMP + " = 1";
    	return getLongValueBySQL(buildSQL);
    }

    public long getLowestItemIdUnread()
    {
    	String buildSQL = "SELECT MIN(" + RSS_ITEM_RSSITEM_ID + ") FROM " + RSS_ITEM_TABLE + " WHERE " + RSS_ITEM_READ_TEMP + " != 1";
    	return getLongValueBySQL(buildSQL);
    }

    public long getHighestItemId()
    {
    	String buildSQL = "SELECT MAX(" + RSS_ITEM_RSSITEM_ID + ") FROM " + RSS_ITEM_TABLE;
        return getLongValueBySQL(buildSQL);
    }

    public int removeAllItemsWithIdLowerThan(String id_db)
    {
    	return database.delete(RSS_ITEM_TABLE, "rowid < ?", new String[] { id_db });
    }

    public void removeXLatestItems(int limit, String execludeFeed) {
        String sql = "DELETE FROM " + RSS_ITEM_TABLE + " WHERE rowid IN (SELECT rowid FROM " + RSS_ITEM_TABLE + " WHERE " + RSS_ITEM_READ + " != 0 " +
                " AND " + RSS_ITEM_READ_TEMP + " != 0 XX ORDER BY " + RSS_ITEM_PUBDATE + " desc LIMIT " + limit + ")";

        if(execludeFeed != null)
            sql = sql.replace("XX", "AND " + RSS_ITEM_SUBSCRIPTION_ID +" != " + execludeFeed);
        else
            sql = sql.replace("XX", "");

        database.execSQL(sql);
    }

    /*
    public void removeReadItems(int limit) {
        String sql = "DELETE FROM " + RSS_ITEM_TABLE + " WHERE rowid IN (SELECT rowid FROM " + RSS_ITEM_TABLE + " WHERE " + RSS_ITEM_READ_TEMP + " = 1 " +
                " AND " + RSS_ITEM_READ + " = 1 ORDER BY " + RSS_ITEM_PUBDATE + " desc LIMIT " + limit +  ")";
        database.execSQL(sql);
    }
    */

    /*
	public Cursor getAllData(String TABLE_NAME) {
        String buildSQL = "SELECT rowid as _id, * FROM " + TABLE_NAME;
        Log.d("DB_HELPER", "getAllData SQL: " + buildSQL);
        return database.rawQuery(buildSQL, null);
    }
	*/

	public Cursor getAllTopSubscriptions(boolean onlyUnread) {
		//String buildSQL = "SELECT DISTINCT(f.rowid) as _id, * FROM " + FOLDER_TABLE + " f ";


        String buildSQL = "SELECT DISTINCT(f.rowid) as _id, " + FOLDER_LABEL +
                            " FROM " + FOLDER_TABLE + " f ";

        if(onlyUnread)
        {
            buildSQL += " JOIN " + SUBSCRIPTION_TABLE + " sc ON f.rowid = sc." + SUBSCRIPTION_FOLDER_ID +
                        " JOIN " + RSS_ITEM_TABLE + " rss ON sc.rowid = rss." + RSS_ITEM_SUBSCRIPTION_ID +
                        " WHERE " + RSS_ITEM_READ_TEMP + " != 1" +
                        " GROUP BY f.rowid " +
                        " HAVING COUNT(*) > 0";
        }

        if(DATABASE_DEBUG_MODE)
        	Log.d("DB_HELPER", "getAllTopSubscriptions SQL: " + buildSQL);
        return database.rawQuery(buildSQL, null);
	}

	public Cursor getAllTopSubscriptionsWithoutFolder(boolean onlyUnread) {
		//String buildSQL = "SELECT DISTINCT(sc.rowid) as _id, * FROM " + SUBSCRIPTION_TABLE + " sc ";
        String buildSQL = "SELECT DISTINCT(sc.rowid) as _id, " + SUBSCRIPTION_HEADERTEXT + ", " + SUBSCRIPTION_ID + ", " + SUBSCRIPTION_FAVICON_URL +
                            " FROM " + SUBSCRIPTION_TABLE + " sc ";

		if(onlyUnread)
		{
			buildSQL += " JOIN " + RSS_ITEM_TABLE + " rss ON sc.rowid = rss." + RSS_ITEM_SUBSCRIPTION_ID +
						" WHERE " + SUBSCRIPTION_FOLDER_ID + " IS NULL" +
						" AND " + RSS_ITEM_READ_TEMP + " != 1" +
                        " GROUP BY sc.rowid " +
                        " HAVING COUNT(*) > 0";
        }
		else
			buildSQL += " WHERE " + SUBSCRIPTION_FOLDER_ID + " IS NULL";

        if(DATABASE_DEBUG_MODE)
        	Log.d("DB_HELPER", "getAllTopSubscriptions SQL: " + buildSQL);
        return database.rawQuery(buildSQL, null);
	}

    /*
	public Cursor getAllTopSubscriptionsWithUnreadFeeds() {
		//String buildSQL = "SELECT rowid as _id, * FROM " + SUBSCRIPTION_TABLE + " WHERE subscription_id_subscription IS NULL";
		String buildSQL = "SELECT f.rowid as _id, * FROM " + FOLDER_TABLE + " f "+
							" JOIN " + SUBSCRIPTION_TABLE + " sc ON f.rowid = sc." + SUBSCRIPTION_FOLDER_ID +
							" JOIN " + RSS_ITEM_TABLE + " rss ON sc.rowid = rss." + RSS_ITEM_SUBSCRIPTION_ID +
							" GROUP BY f.rowid " +
							" HAVING COUNT(*) > 0";
        Log.d("DB_HELPER", "getAllTopData SQL: " + buildSQL);
        return database.rawQuery(buildSQL, null);
	}*/


	public String getAvgColourOfFeedByDbId(String feedId) {
		String buildSQL = "SELECT " + SUBSCRIPTION_AVG_COLOUR + " FROM " + SUBSCRIPTION_TABLE + " WHERE rowid = " + feedId;
		return getStringValueBySQL(buildSQL);
	}

	public int setAvgColourOfFeedByDbId(String feedId, String colour) {
		ContentValues args = new ContentValues();
		args.put(SUBSCRIPTION_AVG_COLOUR, colour);
		return database.update(SUBSCRIPTION_TABLE, args, "rowid=?", new String[] { feedId });
	}

	public Cursor getAllSubSubscriptions() {
		//String buildSQL = "SELECT rowid as _id, * FROM " + SUBSCRIPTION_TABLE + " WHERE subscription_id_subscription IS NOT NULL";
		String buildSQL = "SELECT DISTINCT(rowid) as _id, * FROM " + SUBSCRIPTION_TABLE;

		if(DATABASE_DEBUG_MODE)
        	Log.d("DB_HELPER", "getAllSubSubscriptions SQL: " + buildSQL);
        return database.rawQuery(buildSQL, null);
	}

    public Cursor getItemByDbID(String ID_ITEM_DB) {//Feeds
        String buildSQL = "SELECT rowid as _id, * FROM " + RSS_ITEM_TABLE + " WHERE rowid = '" + ID_ITEM_DB + "'";

        if(DATABASE_DEBUG_MODE)
            Log.d("DB_HELPER", "getSubSubscriptionsByID SQL: " + buildSQL);
        return database.rawQuery(buildSQL, null);
    }

	public Cursor getFeedByDbID(String ID_FEED_DB) {//Feeds
		String buildSQL = "SELECT rowid as _id, * FROM " + SUBSCRIPTION_TABLE + " WHERE rowid = '" + ID_FEED_DB + "'";

		if(DATABASE_DEBUG_MODE)
        	Log.d("DB_HELPER", "getSubSubscriptionsByID SQL: " + buildSQL);
        return database.rawQuery(buildSQL, null);
	}

	public Cursor getFeedByFeedID(String ID_FEED) {//Feeds
		String buildSQL = "SELECT rowid as _id, * FROM " + SUBSCRIPTION_TABLE + " WHERE " + SUBSCRIPTION_ID + " = '" + ID_FEED + "'";

		if(DATABASE_DEBUG_MODE)
        	Log.d("DB_HELPER", "getSubSubscriptionsByID SQL: " + buildSQL);
        return database.rawQuery(buildSQL, null);
	}

	public int getCountItemsForSubscription(String ID_SUBSCRIPTION, boolean onlyUnread, boolean execludeStarredItems) {

		String buildSQL = "SELECT COUNT(*) " +
	 			" FROM " + RSS_ITEM_TABLE +
				//" WHERE read != 1" +
				" WHERE " + RSS_ITEM_READ_TEMP +" != 1 AND " + RSS_ITEM_STARRED + " != 1" +
					" AND subscription_id_subscription IN " +
					"(SELECT rowid " +
					"FROM subscription " +
					"WHERE rowid = " + ID_SUBSCRIPTION + ");";



		if(!onlyUnread)
			buildSQL = buildSQL.replace("read_temp != 1 AND", "");

		if(!execludeStarredItems)
			buildSQL = buildSQL.replace(RSS_ITEM_STARRED + " != 1 AND", RSS_ITEM_STARRED_TEMP + " = 1 AND");

		return (int)getLongValueBySQL(buildSQL);
    }

	public Boolean isFeedUnreadStarred(String FEED_ID, Boolean checkUnread) {
		String buildSQL;
		/*
		if(checkUnread)
			buildSQL = "SELECT read ";
		else//Wenn nicht checkRead auf true steht, soll geprueft werden ob das Feed Markiert ist.
			buildSQL = "SELECT starred ";
			*/

		if(checkUnread)
			buildSQL = "SELECT read_temp ";
		else//Wenn nicht checkRead auf true steht, soll geprueft werden ob das Feed Markiert ist.
			buildSQL = "SELECT starred_temp ";

		buildSQL += " FROM " + RSS_ITEM_TABLE +
					" WHERE rowid = " + FEED_ID;

		return checkSqlForBoolean(buildSQL);
    }

	private Boolean checkSqlForBoolean(String buildSQL)
	{
		Boolean result = false;
		Cursor cursor = database.rawQuery(buildSQL, null);
        if (cursor != null)
        {
        	if(cursor.moveToFirst())
        	{
        		String val = cursor.getString(0);
        		if(val != null)
        			if(val.equals("1"))
        				result = true;
        	}
        }
        cursor.close();

        return result;
	}

	public void updateIsReadOfItem(String ITEM_ID, Boolean isRead) {
		ContentValues args = new ContentValues();
		//args.put(RSS_ITEM_READ, isRead);
		args.put(RSS_ITEM_READ_TEMP, isRead);
		int result = database.update(RSS_ITEM_TABLE, args, "rowid=?", new String[] { ITEM_ID });

		if(DATABASE_DEBUG_MODE)
			Log.d("RESULT UPDATE DATABASE", "RESULT: " + result);
    }

	public void updateIsReadOfItemNotTemp(String ITEM_ID, Boolean isRead) {
		ContentValues args = new ContentValues();
		//args.put(RSS_ITEM_READ, isRead);
		args.put(RSS_ITEM_READ, isRead);
		int result = database.update(RSS_ITEM_TABLE, args, RSS_ITEM_RSSITEM_ID + "=?", new String[] { ITEM_ID });

		if(DATABASE_DEBUG_MODE)
			Log.d("RESULT UPDATE DATABASE", "RESULT: " + result);
    }

	public void updateIsStarredOfFeedNotTemp(String FEED_ID, Boolean isStarred) {
		ContentValues args = new ContentValues();
		//args.put(RSS_ITEM_READ, isRead);
		args.put(RSS_ITEM_STARRED, isStarred);
		int result = database.update(RSS_ITEM_TABLE, args, RSS_ITEM_RSSITEM_ID + "=?", new String[] { FEED_ID });

		if(DATABASE_DEBUG_MODE)
			Log.d("RESULT UPDATE DATABASE", "RESULT: " + result);
    }

	public void updateIsStarredOfItem(String ITEM_ID, Boolean isStarred) {

		if(isStarred)//Wenn ein Feed markiert ist muss es auch als gelesen markiert werden.
			updateIsReadOfItem(ITEM_ID, true);


		ContentValues args = new ContentValues();
		//args.put(RSS_ITEM_STARRED, isStarred);
		args.put(RSS_ITEM_STARRED_TEMP, isStarred);
		int result = database.update(RSS_ITEM_TABLE, args, "rowid=?", new String[] { ITEM_ID });

		if(DATABASE_DEBUG_MODE)
			Log.d("RESULT UPDATE DATABASE", "RESULT: " + result);
    }

	private String getAllFeedsSelectStatement()
	{
		return "SELECT DISTINCT(rowid) as _id, " + RSS_ITEM_TITLE + ", " + RSS_ITEM_RSSITEM_ID + ", " + RSS_ITEM_LINK + ", " + RSS_ITEM_BODY + ", " + RSS_ITEM_READ + ", " + RSS_ITEM_SUBSCRIPTION_ID + ", "
					+ RSS_ITEM_PUBDATE + ", " + RSS_ITEM_STARRED + ", " + RSS_ITEM_GUIDHASH + ", " + RSS_ITEM_GUID + ", " + RSS_ITEM_STARRED_TEMP + ", " + RSS_ITEM_READ_TEMP + ", " + RSS_ITEM_AUTHOR;
	}

	@Deprecated
	public Cursor getAllItemsForFeed(String ID_SUBSCRIPTION, boolean onlyUnread, boolean onlyStarredItems, SORT_DIRECTION sortDirection) {

		String buildSQL =  getAllFeedsSelectStatement() +
	 			" FROM " + RSS_ITEM_TABLE +
				" WHERE subscription_id_subscription IN " +
					"(SELECT rowid " +
					"FROM subscription " +
					"WHERE rowid = " + ID_SUBSCRIPTION + ")";

		if(onlyUnread && !onlyStarredItems)
			buildSQL += " AND " + RSS_ITEM_READ_TEMP + " != 1";
		else if(onlyStarredItems)
			buildSQL += " AND " + RSS_ITEM_STARRED_TEMP + " = 1";

        buildSQL += " ORDER BY " + RSS_ITEM_PUBDATE + " " + sortDirection.toString();

		if(DATABASE_DEBUG_MODE)
			Log.d("DB_HELPER", "getAllItemsForFeed SQL: " + buildSQL);
        return database.rawQuery(buildSQL, null);
    }

	public String getAllItemsIdsForFeedSQL(String ID_SUBSCRIPTION, boolean onlyUnread, boolean onlyStarredItems, SORT_DIRECTION sortDirection) {

		String buildSQL =  "SELECT " + RSS_ITEM_RSSITEM_ID +
	 			" FROM " + RSS_ITEM_TABLE +
				" WHERE subscription_id_subscription IN " +
					"(SELECT rowid " +
					"FROM subscription " +
					"WHERE rowid = " + ID_SUBSCRIPTION + ")";

		if(onlyUnread && !onlyStarredItems)
			buildSQL += " AND " + RSS_ITEM_READ_TEMP + " != 1";
		else if(onlyStarredItems)
			buildSQL += " AND " + RSS_ITEM_STARRED_TEMP + " = 1";

        buildSQL += " ORDER BY " + RSS_ITEM_PUBDATE + " " + sortDirection.toString();

		return buildSQL;
    }

    /*
    public String getUrlToArticleByRowid(String article_rowid) {

        String buildSQL =  "SELECT " + RSS_ITEM_LINK +
                " FROM " + RSS_ITEM_TABLE +
                " WHERE rowid =?";

        return getStringValueBySQL(buildSQL);
    }
    */


    public Cursor getArticleByID(String ID_FEED) {
		String buildSQL = getAllFeedsSelectStatement() +
	 			" FROM " + RSS_ITEM_TABLE +
				" WHERE rowid = " + ID_FEED;

		if(DATABASE_DEBUG_MODE)
			Log.d("DB_HELPER", "getFeedByID SQL: " + buildSQL);
        return database.rawQuery(buildSQL, null);
    }

    public String getRowIdOfFeedByItemID(String ID_FEED)
    {
        String buildSQL = "SELECT rowid " +
                " FROM " + RSS_ITEM_TABLE +
                " WHERE " + RSS_ITEM_RSSITEM_ID + " = " + ID_FEED;

        return getStringValueBySQL(buildSQL);
    }

    public void insertIntoRssCurrentViewTable(String SQL_SELECT) {
    	SQL_SELECT = "INSERT INTO " + RSS_CURRENT_VIEW_TABLE +
				" (" + RSS_CURRENT_VIEW_RSS_ITEM_ID + ") " + SQL_SELECT;
    	openHelper.createRssCurrentViewTable(database);
    	database.execSQL(SQL_SELECT);
    }

    public Cursor getCurrentSelectedRssItems(SORT_DIRECTION sortDirection) {

    	String query1 = getAllFeedsSelectStatement() + " FROM " + RSS_ITEM_TABLE;
    	String query2 = "SELECT " + RSS_CURRENT_VIEW_RSS_ITEM_ID + " FROM " + RSS_CURRENT_VIEW_TABLE;

    	String query = query1 + " WHERE " + RSS_ITEM_RSSITEM_ID + " IN (" + query2 + ")";
    	//query += " ORDER BY " + RSS_ITEM_PUBDATE + " " +
    	query += " ORDER BY " + RSS_ITEM_PUBDATE + " " + sortDirection.toString();

    	return database.rawQuery(query, null);
    }


	public int getCountFeedsForFolder(String ID_FOLDER, boolean onlyUnread) {
		Cursor cursor = database.rawQuery(getAllItemsIdsForFolderSQL(ID_FOLDER, onlyUnread, SORT_DIRECTION.desc), null);
		int count = cursor.getCount();
		cursor.close();

		return count;
	}















































	@Deprecated
	public Cursor getAllItemsForFolder(String ID_FOLDER, boolean onlyUnread, SORT_DIRECTION sortDirection) {
		String buildSQL = getAllFeedsSelectStatement() +
	 			" FROM " + RSS_ITEM_TABLE;

	 	if(!(ID_FOLDER.equals(ALL_UNREAD_ITEMS.getValueString()) || ID_FOLDER.equals(ALL_STARRED_ITEMS.getValueString()) || ID_FOLDER.equals(ALL_ITEMS.getValueString())))//Wenn nicht Alle Artikel ausgewaehlt wurde (-10) oder (-11) fuer Starred Feeds
	 	{
			buildSQL += " WHERE subscription_id_subscription IN " +
					"(SELECT sc.rowid " +
					"FROM subscription sc " +
					"JOIN folder f ON sc." + SUBSCRIPTION_FOLDER_ID + " = f.rowid " +
					"WHERE f.rowid = " + ID_FOLDER + ")";

			if(onlyUnread)
				buildSQL += " AND " + RSS_ITEM_READ_TEMP + " != 1";
	 	}
	 	//else if(ID_FOLDER.equals(SubscriptionExpandableListAdapter.ALL_UNREAD_ITEMS) && onlyUnread)//only unRead should only be null when testing the size of items
	 	else if(ID_FOLDER.equals(ALL_UNREAD_ITEMS.getValueString()))
	 		buildSQL += " WHERE " + RSS_ITEM_STARRED_TEMP + " != 1 AND " + RSS_ITEM_READ_TEMP + " != 1";
	 	else if(ID_FOLDER.equals(ALL_STARRED_ITEMS.getValueString()))
	 		buildSQL += " WHERE " + RSS_ITEM_STARRED_TEMP + " = 1";


	 	buildSQL += " ORDER BY " + RSS_ITEM_PUBDATE + " " + sortDirection.toString();

	 	//	buildSQL += " WHERE starred = 1";

	 	if(DATABASE_DEBUG_MODE)
	 		Log.d("DB_HELPER", "getAllFeedData SQL: " + buildSQL);
        return database.rawQuery(buildSQL, null);
    }

	public String getAllItemsIdsForFolderSQL(String ID_FOLDER, boolean onlyUnread, SORT_DIRECTION sortDirection) {
		String buildSQL = "SELECT " + RSS_ITEM_RSSITEM_ID +
	 			" FROM " + RSS_ITEM_TABLE;

	 	if(!(ID_FOLDER.equals(ALL_UNREAD_ITEMS.getValueString()) || ID_FOLDER.equals(ALL_STARRED_ITEMS.getValueString()) || ID_FOLDER.equals(ALL_ITEMS.getValueString())))//Wenn nicht Alle Artikel ausgewaehlt wurde (-10) oder (-11) fuer Starred Feeds
	 	{
			buildSQL += " WHERE subscription_id_subscription IN " +
					"(SELECT sc.rowid " +
					"FROM subscription sc " +
					"JOIN folder f ON sc." + SUBSCRIPTION_FOLDER_ID + " = f.rowid " +
					"WHERE f.rowid = " + ID_FOLDER + ")";

			if(onlyUnread)
				buildSQL += " AND " + RSS_ITEM_READ_TEMP + " != 1";
	 	}
	 	//else if(ID_FOLDER.equals(SubscriptionExpandableListAdapter.ALL_UNREAD_ITEMS) && onlyUnread)//only unRead should only be null when testing the size of items
	 	else if(ID_FOLDER.equals(ALL_UNREAD_ITEMS.getValueString()))
            buildSQL += " WHERE " + RSS_ITEM_READ_TEMP + " != 1";
	 		//buildSQL += " WHERE " + RSS_ITEM_STARRED_TEMP + " != 1 AND " + RSS_ITEM_READ_TEMP + " != 1";
	 	//else if(ID_FOLDER.equals(SubscriptionExpandableListAdapter.ALL_UNREAD_ITEMS))
	 	//	buildSQL += " WHERE " + RSS_ITEM_STARRED + " != 1";
	 	//else if(ID_FOLDER.equals(SubscriptionExpandableListAdapter.ALL_STARRED_ITEMS) && onlyUnread)
	 	//	buildSQL += " WHERE " + RSS_ITEM_STARRED_TEMP + " = 1 AND " + RSS_ITEM_READ_TEMP + " != 1";
	 	else if(ID_FOLDER.equals(ALL_STARRED_ITEMS.getValueString()))
	 		buildSQL += " WHERE " + RSS_ITEM_STARRED_TEMP + " = 1";


	 	buildSQL += " ORDER BY " + RSS_ITEM_PUBDATE + " " + sortDirection.toString();

	 	//	buildSQL += " WHERE starred = 1";

	 	if(DATABASE_DEBUG_MODE)
	 		Log.d("DB_HELPER", "getAllFeedData SQL: " + buildSQL);
        return buildSQL;
    }

	public Cursor getAllSubscriptionForFolder(String ID_FOLDER, boolean onlyUnread) {
        //String buildSQL = "SELECT sc.rowid as _id, sc.* " +
		String buildSQL = "SELECT sc.rowid as _id, sc." + SUBSCRIPTION_HEADERTEXT + ", sc." + SUBSCRIPTION_ID + ", sc." + SUBSCRIPTION_FAVICON_URL +
							" FROM " + SUBSCRIPTION_TABLE + " sc " +
							" LEFT OUTER JOIN folder f ON sc.folder_idfolder = f.rowid ";

		if(ID_FOLDER.equals("-11"))//Starred
        {
        	buildSQL += " JOIN " + RSS_ITEM_TABLE + " rss ON sc.rowid = rss." + RSS_ITEM_SUBSCRIPTION_ID +
                    " WHERE rss." + RSS_ITEM_STARRED_TEMP + " = 1" +
                    " GROUP BY sc.rowid " +
                    " HAVING COUNT(*) > 0";
        }
		else if(onlyUnread || ID_FOLDER.equals(ALL_UNREAD_ITEMS.getValueString()) /*ID_SUBSCRIPTION.matches("-10|-11")*/)
        {
            buildSQL += " JOIN " + RSS_ITEM_TABLE + " rss ON sc.rowid = rss." + RSS_ITEM_SUBSCRIPTION_ID +
                        " WHERE f.rowid = " + ID_FOLDER  + " AND rss." + RSS_ITEM_READ_TEMP + " != 1" +
                        " GROUP BY sc.rowid " +
                        " HAVING COUNT(*) > 0";

            if(ID_FOLDER.equals(ALL_UNREAD_ITEMS.getValueString()))
            	buildSQL = buildSQL.replace("f.rowid = " + ID_FOLDER + " AND", "");//Remove to ID stuff because i want the result of all feeds where are unread items in
        }
        else
            buildSQL += "WHERE f.rowid = " + ID_FOLDER;

		if(DATABASE_DEBUG_MODE)
			Log.d("DB_HELPER", "getAllSub_SubscriptionForSubscription SQL: " + buildSQL);
        return database.rawQuery(buildSQL, null);
    }

	public void insertNewFolder (String label, String label_path) {
        ContentValues contentValues = new ContentValues();
        contentValues.put(FOLDER_LABEL, label);
        contentValues.put(FOLDER_LABEL_ID, label_path);
        database.insert(FOLDER_TABLE, null, contentValues);
    }

	public void insertNewFeed (String headerText, String ID_FOLDER, String subscription_id, String FAVICON_URL) {
        ContentValues contentValues = new ContentValues();
        contentValues.put(SUBSCRIPTION_HEADERTEXT, headerText);
        contentValues.put(SUBSCRIPTION_FOLDER_ID, ID_FOLDER);
        contentValues.put(SUBSCRIPTION_ID , subscription_id);
        contentValues.put(SUBSCRIPTION_FAVICON_URL, FAVICON_URL);
        database.insert(SUBSCRIPTION_TABLE, null, contentValues);
    }

	public int updateFeed (String headerText, String ID_FOLDER, String subscription_id, String FAVICON_URL) {
        ContentValues contentValues = new ContentValues();
        contentValues.put(SUBSCRIPTION_HEADERTEXT, headerText);
        contentValues.put(SUBSCRIPTION_FOLDER_ID, ID_FOLDER);
        contentValues.put(SUBSCRIPTION_FAVICON_URL, FAVICON_URL);
        return database.update(SUBSCRIPTION_TABLE, contentValues, SUBSCRIPTION_ID  + "= ?", new String[] { subscription_id });
    }

    public void insertNewItems(RssFile[] items) {
        database.beginTransaction();

        List<Integer> rssItems = getRssItemsIds();

        try {
            for(RssFile item : items) {
                if(item != null) {
                    Integer itemId = Integer.parseInt(item.getItem_Id());
                    boolean isFeedAlreadyInDatabase = rssItems.contains(itemId); //doesRssItemAlreadyExsists(item.getItem_Id());
                    insertNewItem(item, !isFeedAlreadyInDatabase);
                }
            }
            database.setTransactionSuccessful();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            database.endTransaction();
        }
    }

	public void insertNewItem (RssFile item, boolean insertItem) {
        ContentValues contentValues = new ContentValues();
        contentValues.put(RSS_ITEM_TITLE, item.getTitle());
        contentValues.put(RSS_ITEM_LINK, item.getLink());
        contentValues.put(RSS_ITEM_BODY, item.getDescription());
        contentValues.put(RSS_ITEM_READ, item.getRead());
        contentValues.put(RSS_ITEM_SUBSCRIPTION_ID, String.valueOf(item.getFeedID_Db()));
        contentValues.put(RSS_ITEM_RSSITEM_ID, item.getItem_Id());
        contentValues.put(RSS_ITEM_PUBDATE, item.getDate().getTime());
        contentValues.put(RSS_ITEM_STARRED, item.getStarred());
        contentValues.put(RSS_ITEM_GUID, item.getGuid());
        contentValues.put(RSS_ITEM_GUIDHASH, item.getGuidHash());
        contentValues.put(RSS_ITEM_LAST_MODIFIED, item.getLastModified());
        contentValues.put(RSS_ITEM_AUTHOR, item.getAuthor());

        contentValues.put(RSS_ITEM_READ_TEMP, item.getRead());
		contentValues.put(RSS_ITEM_STARRED_TEMP, item.getStarred());

        contentValues.put(RSS_ITEM_ENC_LINK, item.getEnclosureLink());
        contentValues.put(RSS_ITEM_ENC_MIME, item.getEnclosureMime());

        long result;
        if(insertItem)
            result = database.insert(RSS_ITEM_TABLE, null, contentValues);
        else
            result = database.update(RSS_ITEM_TABLE, contentValues, RSS_ITEM_RSSITEM_ID + "=?", new String[] { item.getItem_Id() });

        //Log.d("DatabaseConnection", "Inserted Rows: " + result);
    }

    private static final String ALLOWED_PODCASTS_TYPES = "audio/mp3', 'audio/mpeg', 'audio/ogg', 'audio/opus', 'audio/ogg;codecs=opus";

    public List<PodcastFeedItem> getListOfFeedsWithAudioPodcasts() {

        String buildSQL = "SELECT DISTINCT sc.rowid, " + SUBSCRIPTION_HEADERTEXT + ", COUNT(rss.rowid) FROM " + SUBSCRIPTION_TABLE + " sc " +
                            " JOIN " + RSS_ITEM_TABLE + " rss ON sc.rowid = rss." + RSS_ITEM_SUBSCRIPTION_ID +
                            " WHERE rss." + RSS_ITEM_ENC_MIME + " IN ('" + ALLOWED_PODCASTS_TYPES + "') GROUP BY sc.rowid";

        List<PodcastFeedItem> result = new ArrayList<PodcastFeedItem>();
        Cursor cursor = database.rawQuery(buildSQL, null);
        try
        {
            if(cursor != null)
            {
                if(cursor.getCount() > 0)
                {
                    cursor.moveToFirst();
                    do {
                        PodcastFeedItem podcastItem = new PodcastFeedItem();
                        podcastItem.itemId = cursor.getString(0);
                        podcastItem.title = cursor.getString(1);
                        podcastItem.count = cursor.getInt(2);
                        result.add(podcastItem);
                    } while(cursor.moveToNext());
                }
            }
        } finally {
            cursor.close();
        }

        return result;
    }

    public List<AudioPodcastItem> getListOfAudioPodcastsForFeed(String feedId) {

        String buildSQL = "SELECT rowid, " + RSS_ITEM_TITLE + ", " + RSS_ITEM_ENC_LINK + ", " + RSS_ITEM_ENC_MIME + " FROM " + RSS_ITEM_TABLE +
                " WHERE " + RSS_ITEM_ENC_MIME + " IN ('" + ALLOWED_PODCASTS_TYPES + "') AND " + RSS_ITEM_SUBSCRIPTION_ID + " = " + feedId;


        List<AudioPodcastItem> result = new ArrayList<AudioPodcastItem>();
        Cursor cursor = database.rawQuery(buildSQL, null);
        try
        {
            if(cursor != null)
            {
                if(cursor.getCount() > 0)
                {
                    cursor.moveToFirst();
                    do {
                        AudioPodcastItem podcastItem = new AudioPodcastItem();
                        podcastItem.itemId = cursor.getString(0);
                        podcastItem.title = cursor.getString(1);
                        podcastItem.link = cursor.getString(2);
                        podcastItem.mimeType = cursor.getString(3);
                        result.add(podcastItem);
                    } while(cursor.moveToNext());
                }
            }
        } finally {
            cursor.close();
        }

        return result;
    }

    public HashMap<String, String> getListOfFolders () {
        //String buildSQL = "SELECT rowid as _id FROM " + SUBSCRIPTION_TABLE + " WHERE " + SUBSCRIPTION_HEADERTEXT  + " = '" + SubscriptionName + "'";
        String buildSQL = "SELECT label, label_id FROM " + FOLDER_TABLE;

        /*
        Cursor cursor = database.rawQuery(buildSQL, null);
        List<String> folderNames = convertCursorToStringArray(cursor, 0);

        cursor = database.rawQuery(buildSQL, null);
        List<String> folderIds = convertCursorToStringArray(cursor, 1);

        HashMap<String, String> folders = new HashMap<String, String>();

        for(int i = 0; i < folderNames.size(); i++) {
            folders.put(folderNames.get(i), folderIds.get(i));
        }
        */

        return getHashMapFromSQL(buildSQL, 0, 1); //folders;
    }

	public String getIdOfFolder (String FolderName) {
		//String buildSQL = "SELECT rowid as _id FROM " + SUBSCRIPTION_TABLE + " WHERE " + SUBSCRIPTION_HEADERTEXT  + " = '" + SubscriptionName + "'";
		String buildSQL = "SELECT rowid as _id FROM " + FOLDER_TABLE + " WHERE " + FOLDER_LABEL  + " = '" + FolderName + "'";

		return getStringValueBySQL(buildSQL);
    }

	public String getIdOfFolderByLabelPath (String FolderLabelPath) {
		//String buildSQL = "SELECT rowid as _id FROM " + SUBSCRIPTION_TABLE + " WHERE " + SUBSCRIPTION_HEADERTEXT  + " = '" + SubscriptionName + "'";
		String buildSQL = "SELECT rowid as _id FROM " + FOLDER_TABLE + " WHERE " + FOLDER_LABEL_ID  + " = '" + FolderLabelPath + "'";

		return getStringValueBySQL(buildSQL);
    }

    public SparseArray<String> getFeedIds() {
        String buildSQL = "SELECT " + SUBSCRIPTION_ID + ", rowid as _id FROM " + SUBSCRIPTION_TABLE;
        return getSparseArrayFromSQL(buildSQL, 0, 1);
    }

	public String getRowIdBySubscriptionID (String StreamID) {
		//String buildSQL = "SELECT rowid as _id FROM " + SUBSCRIPTION_TABLE + " WHERE " + SUBSCRIPTION_HEADERTEXT  + " = '" + SubscriptionName + "'";
		String buildSQL = "SELECT rowid as _id FROM " + SUBSCRIPTION_TABLE + " WHERE " + SUBSCRIPTION_ID  + " = '" + StreamID + "'";
		return getStringValueBySQL(buildSQL);
    }

	public String getSubscriptionIdByRowID (String ID) {

		String buildSQL = "SELECT " + SUBSCRIPTION_ID + " FROM " + SUBSCRIPTION_TABLE + " WHERE rowid = '" + ID + "'";
		return getStringValueBySQL(buildSQL);
    }

	public String getTitleOfFolderByID (String FolderID) {
		//String buildSQL = "SELECT " + SUBSCRIPTION_HEADERTEXT + " FROM " + SUBSCRIPTION_TABLE + " WHERE rowid = '" + SubscriptionID + "'";
		String buildSQL = "SELECT " + FOLDER_LABEL + " FROM " + FOLDER_TABLE + " WHERE rowid = '" + FolderID + "'";
		return getStringValueBySQL(buildSQL);
    }

	public String getTitleOfSubscriptionByRowID (String SubscriptionID) {
		String buildSQL = "SELECT " + SUBSCRIPTION_HEADERTEXT + " FROM " + SUBSCRIPTION_TABLE + " WHERE rowid = '" + SubscriptionID + "'";
		return getStringValueBySQL(buildSQL);
    }

	public String getTitleOfSubscriptionByRSSItemID (String RssItemID) {
		String buildSQL = "SELECT " + SUBSCRIPTION_HEADERTEXT + " FROM " + SUBSCRIPTION_TABLE + " sc " +
							"JOIN " + RSS_ITEM_TABLE + " rss ON sc.rowid = rss." + RSS_ITEM_SUBSCRIPTION_ID + " " +
							"WHERE rss.rowid = '" + RssItemID + "'";
		return getStringValueBySQL(buildSQL);
    }

	public int removeTopSubscriptionItemByTag(String TAG)
	{
		return database.delete(SUBSCRIPTION_TABLE, SUBSCRIPTION_HEADERTEXT + " = ?", new String[] { TAG });
	}

	public int removeFolderByFolderLabel(String FolderLabel)
	{
		return database.delete(FOLDER_TABLE, FOLDER_LABEL + " = ?", new String[] { FolderLabel });
	}

	public int removeItemByItemId(String idRssItem)
	{
		return database.delete(RSS_ITEM_TABLE, RSS_ITEM_RSSITEM_ID + " = ?", new String[] { idRssItem });
	}

    public boolean doesRssItemAlreadyExsists(String idRssItem)//idRssItem = id aus dem Google Reader Stream
    {
        String buildSQL = "SELECT " + RSS_ITEM_TITLE + " FROM " + RSS_ITEM_TABLE + " WHERE " + RSS_ITEM_RSSITEM_ID + " = '" + idRssItem + "'";
        Cursor cursor = database.rawQuery(buildSQL, null);
        int count = cursor.getCount();
        cursor.close();
        if(count > 0)
            return true;
        else
            return false;
    }

    public List<Integer> getRssItemsIds() {
        String buildSQL = "SELECT " + RSS_ITEM_RSSITEM_ID + " FROM " + RSS_ITEM_TABLE;
        Cursor cursor = database.rawQuery(buildSQL, null);
        return convertCursorToIntArray(cursor, 0);
    }

    public List<Integer> convertCursorToIntArray(Cursor cursor, int column_id)
    {
        List<Integer> items = new ArrayList<Integer>();
        try
        {
            if(cursor != null)
            {
                if(cursor.getCount() > 0)
                {
                    cursor.moveToFirst();
                    do {
                        items.add(cursor.getInt(column_id));
                    } while(cursor.moveToNext());
                }
            }
        } finally {
            cursor.close();
        }
        return items;
    }

	public List<String> convertCursorToStringArray(Cursor cursor, int column_id)
	{
	    List<String> items = new ArrayList<String>();
	    try
	    {
	    	if(cursor != null)
	    	{
			    if(cursor.getCount() > 0)
			    {
				    cursor.moveToFirst();
				    do {
				    	items.add(cursor.getString(column_id));
				    } while(cursor.moveToNext());
			    }
	    	}
	    } finally {
	    	cursor.close();
	    }
	    return items;
	}

	private String getStringValueBySQL(String buildSQL)
    {
    	String result = null;

        Cursor cursor = database.rawQuery(buildSQL, null);
        try
        {
        	if(cursor != null)
        	{
        		if(cursor.moveToFirst())
        			result = cursor.getString(0);
        	}
        } finally {
        	cursor.close();
        }

        return result;
    }

	public long getLongValueBySQL(String buildSQL)
    {
    	long result = -1;

        Cursor cursor = database.rawQuery(buildSQL, null);
        try
        {
        	if(cursor != null)
        	{
        		if(cursor.moveToFirst())
        			result = cursor.getLong(0);
        	}
        } finally {
        	cursor.close();
        }

        return result;
    }

    public HashMap<String, String> getHashMapFromSQL(String buildSQL, int index1, int index2) {
        HashMap<String, String> result = new HashMap<String, String>();

        Cursor cursor = database.rawQuery(buildSQL, null);
        try
        {
            if(cursor != null)
            {
                if(cursor.getCount() > 0)
                {
                    cursor.moveToFirst();
                    do {
                        String key = cursor.getString(index1);
                        String value = cursor.getString(index2);
                        result.put(key, value);
                    } while(cursor.moveToNext());
                }
            }
        } finally {
            cursor.close();
        }

        return result;
    }

    public SparseArray<String> getSparseArrayFromSQL(String buildSQL, int index1, int index2) {
        SparseArray<String> result = new SparseArray<String>();

        Cursor cursor = database.rawQuery(buildSQL, null);
        try
        {
            if(cursor != null)
            {
                if(cursor.getCount() > 0)
                {
                    cursor.moveToFirst();
                    do {
                        int key = cursor.getInt(index1);
                        String value = cursor.getString(index2);
                        result.put(key, value);
                    } while(cursor.moveToNext());
                }
            }
        } finally {
            cursor.close();
        }

        return result;
    }



	public int resetRssItemsDatabase()
	{
        int result = database.delete(RSS_ITEM_TABLE, null, null);
        return result;
	}

	public void resetDatabase()
	{
		openHelper.resetDatabase(database);
	}

	public void openDatabase()
    {
		if(database == null)
			database = openHelper.getWritableDatabase();
    }

	public void closeDatabase()
	{
		//database.close();
	}
}