Skip to content

Commit

Permalink
practice green dao in progress
Browse files Browse the repository at this point in the history
  • Loading branch information
moisesaq committed Apr 9, 2016
1 parent 7a7891e commit 163fc63
Show file tree
Hide file tree
Showing 11 changed files with 941 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions app/app.iml
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@
<orderEntry type="library" exported="" name="support-annotations-23.1.0" level="project" />
<orderEntry type="library" exported="" name="appcompat-v7-23.1.0" level="project" />
<orderEntry type="library" exported="" name="design-23.1.0" level="project" />
<orderEntry type="library" exported="" name="greendao-2.1.0" level="project" />
<orderEntry type="library" exported="" name="support-v4-23.1.0" level="project" />
</component>
</module>
2 changes: 2 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,6 @@ dependencies {
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.1.0'
compile 'com.android.support:design:23.1.0'

compile 'de.greenrobot:greendao:2.1.0'
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package com.apaza.moises.practicegreendao.database;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import de.greenrobot.dao.AbstractDaoMaster;
import de.greenrobot.dao.identityscope.IdentityScopeType;

import com.apaza.moises.practicegreendao.database.UserDao;
import com.apaza.moises.practicegreendao.database.PlaceDao;

// THIS CODE IS GENERATED BY greenDAO, DO NOT EDIT.
/**
* Master of DAO (schema version 1): knows all DAOs.
*/
public class DaoMaster extends AbstractDaoMaster {
public static final int SCHEMA_VERSION = 1;

/** Creates underlying database table using DAOs. */
public static void createAllTables(SQLiteDatabase db, boolean ifNotExists) {
UserDao.createTable(db, ifNotExists);
PlaceDao.createTable(db, ifNotExists);
}

/** Drops underlying database table using DAOs. */
public static void dropAllTables(SQLiteDatabase db, boolean ifExists) {
UserDao.dropTable(db, ifExists);
PlaceDao.dropTable(db, ifExists);
}

public static abstract class OpenHelper extends SQLiteOpenHelper {

public OpenHelper(Context context, String name, CursorFactory factory) {
super(context, name, factory, SCHEMA_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {
Log.i("greenDAO", "Creating tables for schema version " + SCHEMA_VERSION);
createAllTables(db, false);
}
}

/** WARNING: Drops all table on Upgrade! Use only during development. */
public static class DevOpenHelper extends OpenHelper {
public DevOpenHelper(Context context, String name, CursorFactory factory) {
super(context, name, factory);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.i("greenDAO", "Upgrading schema from version " + oldVersion + " to " + newVersion + " by dropping all tables");
dropAllTables(db, true);
onCreate(db);
}
}

public DaoMaster(SQLiteDatabase db) {
super(db, SCHEMA_VERSION);
registerDaoClass(UserDao.class);
registerDaoClass(PlaceDao.class);
}

public DaoSession newSession() {
return new DaoSession(db, IdentityScopeType.Session, daoConfigMap);
}

public DaoSession newSession(IdentityScopeType type) {
return new DaoSession(db, type, daoConfigMap);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package com.apaza.moises.practicegreendao.database;

import android.database.sqlite.SQLiteDatabase;

import java.util.Map;

import de.greenrobot.dao.AbstractDao;
import de.greenrobot.dao.AbstractDaoSession;
import de.greenrobot.dao.identityscope.IdentityScopeType;
import de.greenrobot.dao.internal.DaoConfig;

import com.apaza.moises.practicegreendao.database.User;
import com.apaza.moises.practicegreendao.database.Place;

import com.apaza.moises.practicegreendao.database.UserDao;
import com.apaza.moises.practicegreendao.database.PlaceDao;

// THIS CODE IS GENERATED BY greenDAO, DO NOT EDIT.

/**
* {@inheritDoc}
*
* @see de.greenrobot.dao.AbstractDaoSession
*/
public class DaoSession extends AbstractDaoSession {

private final DaoConfig userDaoConfig;
private final DaoConfig placeDaoConfig;

private final UserDao userDao;
private final PlaceDao placeDao;

public DaoSession(SQLiteDatabase db, IdentityScopeType type, Map<Class<? extends AbstractDao<?, ?>>, DaoConfig>
daoConfigMap) {
super(db);

userDaoConfig = daoConfigMap.get(UserDao.class).clone();
userDaoConfig.initIdentityScope(type);

placeDaoConfig = daoConfigMap.get(PlaceDao.class).clone();
placeDaoConfig.initIdentityScope(type);

userDao = new UserDao(userDaoConfig, this);
placeDao = new PlaceDao(placeDaoConfig, this);

registerDao(User.class, userDao);
registerDao(Place.class, placeDao);
}

public void clear() {
userDaoConfig.getIdentityScope().clear();
placeDaoConfig.getIdentityScope().clear();
}

public UserDao getUserDao() {
return userDao;
}

public PlaceDao getPlaceDao() {
return placeDao;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
package com.apaza.moises.practicegreendao.database;

import com.apaza.moises.practicegreendao.database.DaoSession;
import de.greenrobot.dao.DaoException;

// THIS CODE IS GENERATED BY greenDAO, DO NOT EDIT. Enable "keep" sections if you want to edit.
/**
* Entity mapped to table "PLACE".
*/
public class Place {

private Long id;
/** Not-null value. */
private String name;
private Float rating;
private String address;
private String description;
private String pathImage;
private long userId;

/** Used to resolve relations */
private transient DaoSession daoSession;

/** Used for active entity operations. */
private transient PlaceDao myDao;

private User user;
private Long user__resolvedKey;


public Place() {
}

public Place(Long id) {
this.id = id;
}

public Place(Long id, String name, Float rating, String address, String description, String pathImage, long userId) {
this.id = id;
this.name = name;
this.rating = rating;
this.address = address;
this.description = description;
this.pathImage = pathImage;
this.userId = userId;
}

/** called by internal mechanisms, do not call yourself. */
public void __setDaoSession(DaoSession daoSession) {
this.daoSession = daoSession;
myDao = daoSession != null ? daoSession.getPlaceDao() : null;
}

public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

/** Not-null value. */
public String getName() {
return name;
}

/** Not-null value; ensure this value is available before it is saved to the database. */
public void setName(String name) {
this.name = name;
}

public Float getRating() {
return rating;
}

public void setRating(Float rating) {
this.rating = rating;
}

public String getAddress() {
return address;
}

public void setAddress(String address) {
this.address = address;
}

public String getDescription() {
return description;
}

public void setDescription(String description) {
this.description = description;
}

public String getPathImage() {
return pathImage;
}

public void setPathImage(String pathImage) {
this.pathImage = pathImage;
}

public long getUserId() {
return userId;
}

public void setUserId(long userId) {
this.userId = userId;
}

/** To-one relationship, resolved on first access. */
public User getUser() {
long __key = this.userId;
if (user__resolvedKey == null || !user__resolvedKey.equals(__key)) {
if (daoSession == null) {
throw new DaoException("Entity is detached from DAO context");
}
UserDao targetDao = daoSession.getUserDao();
User userNew = targetDao.load(__key);
synchronized (this) {
user = userNew;
user__resolvedKey = __key;
}
}
return user;
}

public void setUser(User user) {
if (user == null) {
throw new DaoException("To-one property 'userId' has not-null constraint; cannot set to-one to null");
}
synchronized (this) {
this.user = user;
userId = user.getId();
user__resolvedKey = userId;
}
}

/** Convenient call for {@link AbstractDao#delete(Object)}. Entity must attached to an entity context. */
public void delete() {
if (myDao == null) {
throw new DaoException("Entity is detached from DAO context");
}
myDao.delete(this);
}

/** Convenient call for {@link AbstractDao#update(Object)}. Entity must attached to an entity context. */
public void update() {
if (myDao == null) {
throw new DaoException("Entity is detached from DAO context");
}
myDao.update(this);
}

/** Convenient call for {@link AbstractDao#refresh(Object)}. Entity must attached to an entity context. */
public void refresh() {
if (myDao == null) {
throw new DaoException("Entity is detached from DAO context");
}
myDao.refresh(this);
}

}
Loading

0 comments on commit 163fc63

Please sign in to comment.