Skip to content

Instantly share code, notes, and snippets.

@crearo
Created May 9, 2016 12:24
Show Gist options
  • Save crearo/6c09888d7283ed825954aa8b98c3dcdf to your computer and use it in GitHub Desktop.
Save crearo/6c09888d7283ed825954aa8b98c3dcdf to your computer and use it in GitHub Desktop.
Sugar ORM Example
import android.util.Log;
import com.orm.SugarRecord;
import com.orm.query.Condition;
import com.orm.query.Select;
import com.orm.util.NamingHelper;
/**
* Created by rish on 6/5/16.
* This is an example of how to use SugarORM (https://github.com/satyan/sugar) - a brilliant lightweight sql handler for Android
* I have used different datatypes for the table columns, and have written various querying methods.
*/
public class VoteDataStorage extends SugarRecord {
private int photoID;
private String voteType;
private long timeStamp;
private boolean acked;
public boolean isAcked() {
return acked;
}
public void setAcked(boolean acked) {
this.acked = acked;
}
public int getPhotoID() {
return photoID;
}
public void setPhotoID(int photoID) {
this.photoID = photoID;
}
public String getVoteType() {
return voteType;
}
public void setVoteType(String voteType) {
this.voteType = voteType;
}
public long getTimeStamp() {
return timeStamp;
}
public void setTimeStamp(long timeStamp) {
this.timeStamp = timeStamp;
}
public VoteDataStorage() {
}
public VoteDataStorage(int photoID, String voteType, long timeStamp, boolean acked) {
this.photoID = photoID;
this.voteType = voteType;
this.timeStamp = timeStamp;
this.acked = acked;
}
public static VoteDataStorage findByPhotoIDandVoteType(int photoID, String voteType) {
VoteDataStorage voteDataStorage = Select.from(VoteDataStorage.class)
.where(Condition.prop(NamingHelper.toSQLNameDefault("photoID")).eq(photoID))
.where(Condition.prop(NamingHelper.toSQLNameDefault("voteType")).eq(voteType))
.first();
if (voteDataStorage != null)
Log.d("VDS", "Found " + photoID);
else Log.d("VDS", "Not found, is null " + photoID);
return voteDataStorage;
}
public static void deleteByPhotoIDandVoteType(int photoID, String voteType) {
VoteDataStorage voteDataStorage = Select.from(VoteDataStorage.class)
.where(Condition.prop(NamingHelper.toSQLNameDefault("photoID")).eq(photoID))
.where(Condition.prop(NamingHelper.toSQLNameDefault("voteType")).eq(voteType))
.first();
if (voteDataStorage != null) {
voteDataStorage.delete();
}
}
public static void addByPhotoIDandVoteType(int photoID, String voteType) {
VoteDataStorage voteDataStorage = new VoteDataStorage(photoID, voteType, System.currentTimeMillis(), false);
voteDataStorage.save();
}
public static void setAckedByPhotoIDandVoteType(int photoID, String voteType, boolean acked) {
VoteDataStorage voteDataStorage = findByPhotoIDandVoteType(photoID, voteType);
if (voteDataStorage != null) {
voteDataStorage.setAcked(acked);
voteDataStorage.save();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment