Skip to content

Commit cb46b91

Browse files
committed
1. Added new method in db helper for inserting data in to database using transaction for faster data insertion and faster execution time.
1 parent 0159e65 commit cb46b91

File tree

3 files changed

+250
-12
lines changed

3 files changed

+250
-12
lines changed

app/src/main/java/com/amit/db/DBHelper.java

Lines changed: 157 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public class DBHelper
2626
private static final String TAG = DBHelper.class.getSimpleName();
2727

2828
private final Database db;
29-
29+
3030
private ArrayList<DbData> dbDataArrayList = new ArrayList<>();
3131
private ArrayList<DbColumn> dbColumnArrayList = new ArrayList<>();
3232

@@ -1036,6 +1036,152 @@ public DBHelper insertData(String tableName)
10361036

10371037
return this;
10381038
}
1039+
1040+
//#region COMMENTS FOR insertDataWithTransaction method
1041+
/**
1042+
* 2019 January 09 - Wednesday - 06:49 PM
1043+
* insert data with transaction method
1044+
*
1045+
* @param tableName - name of the table where the data is to be inserted
1046+
*
1047+
* this method will insert data into table using database transaction
1048+
* this method is useful for inserting bulk records into table in less time
1049+
**/
1050+
//#endregion COMMENTS FOR insertDataWithTransaction method
1051+
@Deprecated
1052+
public DBHelper insertDataWithTransaction(String tableName)
1053+
{
1054+
if (dbDataArrayList == null || dbDataArrayList.size() == 0)
1055+
{
1056+
Log.e(TAG, "insertDataWithTransaction: Db Data was not provided. Cannot insert data in table.");
1057+
return this;
1058+
}
1059+
1060+
// tree set is used for removing duplicate column name from data array list
1061+
TreeSet<String> treeSet = new TreeSet<>();
1062+
1063+
// this array list will hold unique column name from data array list
1064+
ArrayList<String> columnsArrayList = new ArrayList<>();
1065+
1066+
// loop for removing duplicate values from data array list
1067+
// for (int i = 0; i < dbDataArrayList.size(); i++)
1068+
for (int i = 0; i < dbDataArrayList.size(); i++)
1069+
{
1070+
for (DbData item : dbDataArrayList)
1071+
{
1072+
// checking if tree set contains columns from data array list
1073+
// if not contains then adding it to columns array list
1074+
if (!treeSet.contains(item.columnName))
1075+
{
1076+
// column name not present in columns array list
1077+
// adding to columns array list and tree set
1078+
treeSet.add(item.columnName);
1079+
columnsArrayList.add(item.columnName);
1080+
}
1081+
}
1082+
}
1083+
1084+
// getting columns count for generating insert query
1085+
// and inserting records into corresponding columns
1086+
int columnCount = columnsArrayList.size();
1087+
1088+
// this string builder is used to append names of columns for the query
1089+
// for saving records into corresponding columns
1090+
StringBuilder queryBuilder = new StringBuilder();
1091+
1092+
// this string builder is used to append indexes for the query
1093+
StringBuilder indexesBuilder = new StringBuilder();
1094+
1095+
// generating insert query
1096+
queryBuilder.append("INSERT INTO ").append(tableName).append(" (");
1097+
indexesBuilder.append(" VALUES (");
1098+
1099+
// loop for generating insert query with columns name and indexes
1100+
for (int i = 0; i < columnCount; i++)
1101+
{
1102+
indexesBuilder.append("?");
1103+
queryBuilder.append(columnsArrayList.get(i));
1104+
1105+
// checking if column's count is equals to i
1106+
// if yes then appending brackets
1107+
// else appending comma
1108+
if (i == columnCount - 1)
1109+
{
1110+
queryBuilder.append(")");
1111+
indexesBuilder.append(")");
1112+
}
1113+
else
1114+
{
1115+
queryBuilder.append(" , ");
1116+
indexesBuilder.append(" , ");
1117+
}
1118+
}
1119+
1120+
// this is final query
1121+
String query = queryBuilder.toString() + indexesBuilder.toString();
1122+
Log.e(TAG, "insertDataWithTransaction: Insert query with transaction is: " + query);
1123+
1124+
// starting database transaction for inserting records
1125+
db.getWritableDatabase().beginTransaction();
1126+
1127+
// compiling insert query with indexes
1128+
SQLiteStatement statement = db.getWritableDatabase().compileStatement(query);
1129+
1130+
// this position is used for SQLite statement
1131+
// for binding data with columns
1132+
int position = 0;
1133+
1134+
// loop for inserting records with statement
1135+
for (int i = 0; i <= dbDataArrayList.size(); i++)
1136+
{
1137+
// checking if position is equals to column count
1138+
// this check will make sure that only those records get inserted
1139+
// for which the columns are passed
1140+
// irrespective of no of columns table has
1141+
// if yes then executing the statement
1142+
if (position == columnCount)
1143+
{
1144+
position = 0;
1145+
statement.execute();
1146+
statement.clearBindings();
1147+
}
1148+
1149+
// checking if i is equals to data array list's size
1150+
// if yes then breaking loop so the below code is not executed
1151+
// this check will ensure that last records are inserted
1152+
// and no index out of bound exception occurs
1153+
if (i == dbDataArrayList.size())
1154+
{
1155+
continue;
1156+
}
1157+
1158+
// increasing the position value by 1 for mapping data with column
1159+
position += 1;
1160+
1161+
// retrieving data from data array list
1162+
Object columnData = dbDataArrayList.get(i).columnData;
1163+
1164+
// checking the type of data and binding to corresponding type
1165+
if (columnData instanceof Integer)
1166+
{
1167+
statement.bindLong(position, Integer.parseInt(columnData.toString()));
1168+
}
1169+
else if (columnData instanceof String )
1170+
{
1171+
statement.bindString(position, columnData.toString());
1172+
}
1173+
else if (columnData instanceof Double || columnData instanceof Float)
1174+
{
1175+
statement.bindDouble(position, Double.parseDouble(columnData.toString()));
1176+
}
1177+
}
1178+
1179+
db.getWritableDatabase().setTransactionSuccessful();
1180+
db.getWritableDatabase().endTransaction();
1181+
1182+
dbDataArrayList = new ArrayList<>();
1183+
return this;
1184+
}
10391185

10401186
//#region COMMENTS FOR insertDataWithTransaction method
10411187
/**
@@ -1044,11 +1190,13 @@ public DBHelper insertData(String tableName)
10441190
*
10451191
* @param tableName - name of the table where the data is to be inserted
10461192
*
1193+
* @param dbColumnCount - total number of columns in the table you want to insert data
1194+
*
10471195
* this method will insert data into table using database transaction
10481196
* this method is useful for inserting bulk records into table in less time
10491197
**/
10501198
//#endregion COMMENTS FOR insertDataWithTransaction method
1051-
public DBHelper insertDataWithTransaction(String tableName)
1199+
public DBHelper insertDataWithTransaction(String tableName, int dbColumnCount)
10521200
{
10531201
if (dbDataArrayList == null || dbDataArrayList.size() == 0)
10541202
{
@@ -1063,10 +1211,16 @@ public DBHelper insertDataWithTransaction(String tableName)
10631211
ArrayList<String> columnsArrayList = new ArrayList<>();
10641212

10651213
// loop for removing duplicate values from data array list
1066-
for (int i = 0; i < dbDataArrayList.size(); i++)
1214+
// for (int i = 0; i < dbDataArrayList.size(); i++)
1215+
for (int i = 0; i < dbColumnCount; i++)
10671216
{
10681217
for (DbData item : dbDataArrayList)
10691218
{
1219+
if (columnsArrayList.size() == dbColumnCount)
1220+
{
1221+
break;
1222+
}
1223+
10701224
// checking if tree set contains columns from data array list
10711225
// if not contains then adding it to columns array list
10721226
if (!treeSet.contains(item.columnName))

app/src/main/java/com/amit/utilities/TextUtils.java

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* Created By AMIT JANGID
55
* 2018 April 17 - Tuesday - 12:52 PM
66
**/
7-
@SuppressWarnings("unused")
7+
@SuppressWarnings({"unused", "WeakerAccess"})
88
public class TextUtils
99
{
1010
/**
@@ -80,6 +80,35 @@ else if (stringToReplace.equalsIgnoreCase(""))
8080
return Integer.parseInt(stringToReplace);
8181
}
8282
}
83+
84+
/**
85+
* replace null method
86+
* this method will replace null with empty space
87+
*
88+
* @param string - string where you want to replace null
89+
* @return it will return empty string
90+
**/
91+
public static String replaceNullWithDash(String string)
92+
{
93+
if (string == null)
94+
{
95+
return "-";
96+
}
97+
else if (string.equalsIgnoreCase("null"))
98+
{
99+
return "-";
100+
}
101+
else if (string.equalsIgnoreCase(" "))
102+
{
103+
return "-";
104+
}
105+
else if (string.equalsIgnoreCase(""))
106+
{
107+
return "-";
108+
}
109+
110+
return string;
111+
}
83112

84113
/**
85114
* 2018 September 14 - Friday - 12:34 PM

app/src/main/java/com/amit/views/DatePickerFragment.java

Lines changed: 63 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,18 @@
2020
* this class will display a date picker dialog
2121
* it will return selected date in the format defined
2222
**/
23-
@SuppressWarnings({"unused", "deprecation"})
23+
@SuppressWarnings({"unused", "deprecation", "DeprecatedIsStillUsed"})
2424
public class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener
2525
{
2626
private static final String TAG = DatePickerFragment.class.getSimpleName();
2727

28-
private Calendar calendar;
28+
private Calendar mCalendar;
29+
30+
@Deprecated
2931
private SelectedDate mSelectedDate;
3032

33+
private OnDateSelectedListener mOnDateSelectedListener;
34+
3135
private static boolean mIsCurrentDateMin;
3236
private static String mSelectedDateFormat;
3337

@@ -44,6 +48,7 @@ public class DatePickerFragment extends DialogFragment implements DatePickerDial
4448
*
4549
* @param isCurrentDateMin - pass true to set current date as minimum date else pass false.
4650
**/
51+
@Deprecated
4752
public void showDatePickerDialog(@NonNull Context context, @NonNull SelectedDate selectedDate,
4853
@NonNull String selectedDateFormat, boolean isCurrentDateMin)
4954
{
@@ -55,15 +60,40 @@ public void showDatePickerDialog(@NonNull Context context, @NonNull SelectedDate
5560
datePickerFragment.show(((Activity) context).getFragmentManager(), "DatePicker");
5661
}
5762

63+
/**
64+
* 2018 October 24 - Wednesday - 03:34 PM
65+
* show date picker dialog method
66+
*
67+
* this method will show the date picker dialog fragment
68+
*
69+
* @param context - context of the application
70+
* @param onDateSelectedListener - interface of date picker fragment for getting the selected date value
71+
* @param selectedDateFormat - format in which you want the date.
72+
* Example: yyyy-MM-dd hh:mm:ss
73+
*
74+
* @param isCurrentDateMin - pass true to set current date as minimum date else pass false.
75+
**/
76+
public void showDatePickerDialog(@NonNull Context context,
77+
@NonNull OnDateSelectedListener onDateSelectedListener,
78+
@NonNull String selectedDateFormat, boolean isCurrentDateMin)
79+
{
80+
mIsCurrentDateMin = isCurrentDateMin;
81+
mSelectedDateFormat = selectedDateFormat;
82+
83+
DatePickerFragment datePickerFragment = new DatePickerFragment();
84+
datePickerFragment.initializeInterface(onDateSelectedListener);
85+
datePickerFragment.show(((Activity) context).getFragmentManager(), "DatePicker");
86+
}
87+
5888
@Override
5989
public Dialog onCreateDialog(Bundle savedInstanceState)
6090
{
6191
// Use the current date as the default date in the picker
62-
calendar = Calendar.getInstance();
92+
mCalendar = Calendar.getInstance();
6393

64-
int year = calendar.get(Calendar.YEAR);
65-
int month = calendar.get(Calendar.MONTH);
66-
int day = calendar.get(Calendar.DAY_OF_MONTH);
94+
int year = mCalendar.get(Calendar.YEAR);
95+
int month = mCalendar.get(Calendar.MONTH);
96+
int day = mCalendar.get(Calendar.DAY_OF_MONTH);
6797

6898
// Create a new instance of DatePickerDialog and return it
6999
DatePickerDialog datePickerDialog = new DatePickerDialog(getActivity(), this, year, month, day);
@@ -79,25 +109,50 @@ public Dialog onCreateDialog(Bundle savedInstanceState)
79109
@Override
80110
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth)
81111
{
82-
calendar.set(year, month, dayOfMonth);
112+
mCalendar.set(year, month, dayOfMonth);
83113
SimpleDateFormat sdf = new SimpleDateFormat(mSelectedDateFormat, Locale.getDefault());
84114

85-
String selectedDate = sdf.format(calendar.getTime());
115+
String selectedDate = sdf.format(mCalendar.getTime());
86116
Log.e(TAG, "onDateSet: selected date is: " + selectedDate);
87117

88118
if (mSelectedDate != null)
89119
{
90120
mSelectedDate.selectedDate(selectedDate);
91121
}
122+
else
123+
{
124+
Log.e(TAG, "onDateSet: selectedDate interface is null.");
125+
}
126+
127+
if (mOnDateSelectedListener != null)
128+
{
129+
mOnDateSelectedListener.onDateSelected(selectedDate);
130+
}
131+
else
132+
{
133+
Log.e(TAG, "onDateSet: onDateSelectedListener interface is null.");
134+
}
92135
}
93136

137+
@Deprecated
94138
private void initializeInterface(SelectedDate selectedDate)
95139
{
96140
this.mSelectedDate = selectedDate;
97141
}
98142

143+
private void initializeInterface(OnDateSelectedListener onDateSelectedListener)
144+
{
145+
this.mOnDateSelectedListener = onDateSelectedListener;
146+
}
147+
148+
@Deprecated
99149
public interface SelectedDate
100150
{
101151
void selectedDate(String selectedDate);
102152
}
153+
154+
public interface OnDateSelectedListener
155+
{
156+
void onDateSelected(String selectedDate);
157+
}
103158
}

0 commit comments

Comments
 (0)