Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

change modified column data type to INTEGER #416

Merged
merged 2 commits into from
Jun 17, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ public void setCategory(String category) {

@Override
public String toString() {
return "R" + getRemoteId() + " " + (isFavorite() ? " (*) " : " ") + getCategory() + " / " + getTitle() + " (" + getModified(NoteSQLiteOpenHelper.DATE_FORMAT) + " / " + getEtag() + ")";
final String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";
return "R" + getRemoteId() + " " + (isFavorite() ? " (*) " : " ") + getCategory() + " / " + getTitle() + " (" + getModified(DATE_FORMAT) + " / " + getEtag() + ")";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,10 @@
import android.text.TextUtils;
import android.util.Log;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import it.niedermann.owncloud.notes.android.appwidget.NoteListWidget;
Expand All @@ -35,9 +32,7 @@
*/
public class NoteSQLiteOpenHelper extends SQLiteOpenHelper {

public static final String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";

private static final int database_version = 7;
private static final int database_version = 8;
private static final String database_name = "OWNCLOUD_NOTES";
private static final String table_notes = "NOTES";
private static final String key_id = "ID";
Expand Down Expand Up @@ -82,19 +77,24 @@ public NoteServerSyncHelper getNoteServerSyncHelper() {
*/
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE " + table_notes + " ( " +
createTable(db, table_notes);
}

private void createTable(SQLiteDatabase db, String tableName) {
db.execSQL("CREATE TABLE " + tableName + " ( " +
key_id + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
key_remote_id + " INTEGER, " +
key_status + " VARCHAR(50), " +
key_title + " TEXT, " +
key_modified + " TEXT, " +
key_modified + " INTEGER DEFAULT 0, " +
key_content + " TEXT, " +
key_favorite + " INTEGER DEFAULT 0, " +
key_category + " TEXT NOT NULL DEFAULT '', " +
key_etag + " TEXT)");
createIndexes(db);
}


@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
if (oldVersion < 3) {
Expand All @@ -117,6 +117,14 @@ public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("ALTER TABLE " + table_notes + " ADD COLUMN " + key_etag + " TEXT");
createIndexes(db);
}
if (oldVersion < 8) {
final String table_temp = "NOTES_TEMP";
createTable(db, table_temp);
db.execSQL(String.format("INSERT INTO %s(%s,%s,%s,%s,%s,%s,%s,%s,%s) ", table_temp, key_id, key_remote_id, key_status, key_title, key_modified, key_content, key_favorite, key_category, key_etag)
+ String.format("SELECT %s,%s,%s,%s,strftime('%%s',%s),%s,%s,%s,%s FROM %s", key_id, key_remote_id, key_status, key_title, key_modified, key_content, key_favorite, key_category, key_etag, table_notes));
db.execSQL(String.format("DROP TABLE %s", table_notes));
db.execSQL(String.format("ALTER TABLE %s RENAME TO %s", table_temp, table_notes));
}
}

@Override
Expand Down Expand Up @@ -206,7 +214,7 @@ long addNote(CloudNote note) {
values.put(key_remote_id, note.getRemoteId());
}
values.put(key_title, note.getTitle());
values.put(key_modified, note.getModified(NoteSQLiteOpenHelper.DATE_FORMAT));
values.put(key_modified, note.getModified().getTimeInMillis() / 1000);
values.put(key_content, note.getContent());
values.put(key_favorite, note.isFavorite());
values.put(key_category, note.getCategory());
Expand Down Expand Up @@ -258,13 +266,7 @@ private List<DBNote> getNotesCustom(@NonNull String selection, @NonNull String[]
@NonNull
private DBNote getNoteFromCursor(@NonNull Cursor cursor) {
Calendar modified = Calendar.getInstance();
try {
String modifiedStr = cursor.getString(4);
if (modifiedStr != null)
modified.setTime(new SimpleDateFormat(DATE_FORMAT, Locale.GERMANY).parse(modifiedStr));
} catch (ParseException e) {
e.printStackTrace();
}
modified.setTimeInMillis(cursor.getLong(4) * 1000);
return new DBNote(cursor.getLong(0), cursor.getLong(1), modified, cursor.getString(3), cursor.getString(5), cursor.getInt(6) > 0, cursor.getString(7), cursor.getString(8), DBStatus.parse(cursor.getString(2)));
}

Expand Down Expand Up @@ -440,7 +442,7 @@ public DBNote updateNoteAndSync(@NonNull DBNote oldNote, @Nullable String newCon
values.put(key_status, newNote.getStatus().getTitle());
values.put(key_title, newNote.getTitle());
values.put(key_category, newNote.getCategory());
values.put(key_modified, newNote.getModified(DATE_FORMAT));
values.put(key_modified, newNote.getModified().getTimeInMillis() / 1000);
values.put(key_content, newNote.getContent());
int rows = db.update(table_notes, values, key_id + " = ? AND (" + key_content + " != ? OR " + key_category + " != ?)", new String[]{String.valueOf(newNote.getId()), newNote.getContent(), newNote.getCategory()});
// if data was changed, set new status and schedule sync (with callback); otherwise invoke callback directly.
Expand Down Expand Up @@ -482,7 +484,7 @@ int updateNote(long id, @NonNull CloudNote remoteNote, @Nullable DBNote forceUnc
values.clear();
values.put(key_status, DBStatus.VOID.getTitle());
values.put(key_title, remoteNote.getTitle());
values.put(key_modified, remoteNote.getModified(DATE_FORMAT));
values.put(key_modified, remoteNote.getModified().getTimeInMillis() / 1000);
values.put(key_content, remoteNote.getContent());
values.put(key_favorite, remoteNote.isFavorite());
values.put(key_category, remoteNote.getCategory());
Expand All @@ -500,7 +502,7 @@ int updateNote(long id, @NonNull CloudNote remoteNote, @Nullable DBNote forceUnc
// used by: NoteServerSyncHelper.SyncTask.pullRemoteChanges()
// update only, if not modified locally (i.e. STATUS="") and if modified remotely (i.e. any (!) column has changed)
whereClause = key_id + " = ? AND " + key_status + " = ? AND (" + key_modified + "!=? OR " + key_title + "!=? OR " + key_favorite + "!=? OR " + key_category + "!=? OR " + (remoteNote.getEtag() != null ? key_etag + " IS NULL OR " : "") + key_etag + "!=? OR " + key_content + "!=?)";
whereArgs = new String[]{String.valueOf(id), DBStatus.VOID.getTitle(), remoteNote.getModified(DATE_FORMAT), remoteNote.getTitle(), remoteNote.isFavorite() ? "1" : "0", remoteNote.getCategory(), remoteNote.getEtag(), remoteNote.getContent()};
whereArgs = new String[]{String.valueOf(id), DBStatus.VOID.getTitle(), Long.toString(remoteNote.getModified().getTimeInMillis() / 1000), remoteNote.getTitle(), remoteNote.isFavorite() ? "1" : "0", remoteNote.getCategory(), remoteNote.getEtag(), remoteNote.getContent()};
}
int i = db.update(table_notes, values, whereClause, whereArgs);
Log.d(getClass().getSimpleName(), "updateNote: " + remoteNote + " || forceUnchangedDBNoteState: " + forceUnchangedDBNoteState + " => " + i + " rows updated");
Expand Down