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

Adding capability to specify primary key #78

Closed
wants to merge 3 commits into from
Closed
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
library/bin
library/build
library/gen
library/.project
library/.classpath
library/library.iml
*.class
.DS_Store
41 changes: 21 additions & 20 deletions library/src/com/orm/SugarRecord.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class SugarRecord<T>{
@Ignore
String tableName = getSqlName();

protected Long id = null;
public Long id = null;

public void delete() {
SQLiteDatabase db = getSugarContext().getDatabase().getDB();
Expand Down Expand Up @@ -91,31 +91,25 @@ void save(SQLiteDatabase db) {
if (!"id".equalsIgnoreCase(column.getName())) {
if (columnType.equals(Short.class) || columnType.equals(short.class)) {
values.put(columnName, (Short) columnValue);
}
else if (columnType.equals(Integer.class) || columnType.equals(int.class)) {
} else if (columnType.equals(Integer.class) || columnType.equals(int.class)) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd like to keep style changes separate from feature additions.

values.put(columnName, (Integer) columnValue);
}
else if (columnType.equals(Long.class) || columnType.equals(long.class)) {
} else if (columnType.equals(Long.class) || columnType.equals(long.class)) {
values.put(columnName, (Long) columnValue);
}
else if (columnType.equals(Float.class) || columnType.equals(float.class)) {
} else if (columnType.equals(Float.class) || columnType.equals(float.class)) {
values.put(columnName, (Float) columnValue);
}
else if (columnType.equals(Double.class) || columnType.equals(double.class)) {
} else if (columnType.equals(Double.class) || columnType.equals(double.class)) {
values.put(columnName, (Double) columnValue);
}
else if (columnType.equals(Boolean.class) || columnType.equals(boolean.class)) {
} else if (columnType.equals(Boolean.class) || columnType.equals(boolean.class)) {
values.put(columnName, (Boolean) columnValue);
}
else if (Date.class.equals(columnType)) {
} else if (Date.class.equals(columnType)) {
values.put(columnName, ((Date) column.get(this)).getTime());
}
else if (Calendar.class.equals(columnType)) {
} else if (Calendar.class.equals(columnType)) {
values.put(columnName, ((Calendar) column.get(this)).getTimeInMillis());
}else{
} else {
values.put(columnName, String.valueOf(columnValue));
}

} else {
id = (Long) columnValue;
}
}

Expand All @@ -124,10 +118,17 @@ else if (Calendar.class.equals(columnType)) {
}
}

if (id == null)
int result = 0;

if (id != null) {
result = db.update(getSqlName(), values, "ID = ?", new String[]{String.valueOf(id)});
if (result == 0) {
values.put("id", id);
}
}
if (id == null || result == 0) {
id = db.insert(getSqlName(), null, values);
else
db.update(getSqlName(), values, "ID = ?", new String[]{String.valueOf(id)});
}

Log.i("Sugar", getClass().getSimpleName() + " saved : " + id);
}
Expand Down