Created
May 9, 2016 12:24
-
-
Save crearo/6c09888d7283ed825954aa8b98c3dcdf to your computer and use it in GitHub Desktop.
Sugar ORM Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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