Java Code Examples for com.j256.ormlite.dao.Dao#create()

The following examples show how to use com.j256.ormlite.dao.Dao#create() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: UserDao.java    From renrenpay-android with Apache License 2.0 6 votes vote down vote up
public static User add(User user) {

        User res = null;
        try {
            Dao<User, Integer> dao = DatabaseHelper.getDbHelper().getDao(User.class);
            User old = dao.queryBuilder().where().eq("mobile_phone", user.getMobile_phone()).queryForFirst();
            if(old == null) {
                dao.create(user);
                res = user;
            } else {
                old.setToken(user.getToken());
                old.setUserid(user.getUserid());
                dao.update(old);
                res = old;
            }
        } catch(SQLException e) {
            e.printStackTrace();
        }
        return res;
    }
 
Example 2
Source File: WhereTest.java    From ormlite-core with ISC License 6 votes vote down vote up
@Test
public void testMultipleFuture() throws Exception {
	Dao<Foo, String> dao = createDao(Foo.class, true);

	Foo foo1 = new Foo();
	foo1.val = 123;
	foo1.stringField = "fjewpjfew";
	dao.create(foo1);

	QueryBuilder<Foo, String> qb = dao.queryBuilder();
	Where<Foo, String> where = qb.where();

	where.eq(Foo.VAL_COLUMN_NAME, foo1.val);
	where.and();
	where.not();
	where.like(Foo.STRING_COLUMN_NAME, "hello");
	List<Foo> results = where.query();
	assertNotNull(results);
	assertEquals(1, results.size());
	assertEquals(foo1.id, results.get(0).id);
}
 
Example 3
Source File: MappedPreparedQueryTest.java    From ormlite-core with ISC License 6 votes vote down vote up
@Test
public void testLimit() throws Exception {
	Dao<LocalFoo, Integer> fooDao = createDao(LocalFoo.class, true);
	List<LocalFoo> foos = new ArrayList<LocalFoo>();
	LocalFoo foo = new LocalFoo();
	// create foo #1
	fooDao.create(foo);
	foos.add(foo);
	foo = new LocalFoo();
	// create foo #2
	fooDao.create(foo);
	foos.add(foo);

	TableInfo<LocalFoo, Integer> tableInfo = new TableInfo<LocalFoo, Integer>(databaseType, LocalFoo.class);
	MappedPreparedStmt<LocalFoo, Integer> preparedQuery = new MappedPreparedStmt<LocalFoo, Integer>(fooDao,
			tableInfo, "select * from " + TABLE_NAME, new FieldType[0], tableInfo.getFieldTypes(),
			new ArgumentHolder[0], 1L, StatementType.SELECT, false);

	checkResults(foos, preparedQuery, 1);
	preparedQuery = new MappedPreparedStmt<LocalFoo, Integer>(fooDao, tableInfo, "select * from " + TABLE_NAME,
			new FieldType[0], tableInfo.getFieldTypes(), new ArgumentHolder[0], null, StatementType.SELECT, false);
	checkResults(foos, preparedQuery, 2);
}
 
Example 4
Source File: FieldTypeTest.java    From ormlite-core with ISC License 6 votes vote down vote up
@Test
public void testForeignInCache() throws Exception {
	Dao<ForeignParent, Integer> parentDao = createDao(ForeignParent.class, true);
	Dao<ForeignForeign, Integer> foreignDao = createDao(ForeignForeign.class, true);
	foreignDao.setObjectCache(true);

	ForeignForeign foreign = new ForeignForeign();
	foreign.stuff = "hello";
	foreignDao.create(foreign);

	assertSame(foreign, foreignDao.queryForId(foreign.id));

	ForeignParent parent = new ForeignParent();
	parent.foreign = foreign;
	parentDao.create(parent);

	ForeignParent result = parentDao.queryForId(parent.id);
	assertNotSame(parent, result);
	assertSame(foreign, result.foreign);
}
 
Example 5
Source File: LogDao.java    From renrenpay-android with Apache License 2.0 5 votes vote down vote up
public static void add(Logs logs) {
    try {
        Dao<Logs, Integer> dao = DatabaseHelper.getDbHelper().getDao(Logs.class);
        dao.create(logs);
    } catch (SQLException e) {
        e.printStackTrace();
    }
}
 
Example 6
Source File: QueryBuilderTest.java    From ormlite-core with ISC License 5 votes vote down vote up
@Test
public void testLeftJoinTwoColumns() throws Exception {
	Dao<Foo, Integer> fooDao = createDao(Foo.class, true);
	Dao<StringColumnArg, Integer> scaDao = createDao(StringColumnArg.class, true);

	Foo foo1 = new Foo();
	foo1.val = 123213213;
	foo1.stringField = "stuff";
	fooDao.create(foo1);

	StringColumnArg sca1 = new StringColumnArg();
	sca1.str1 = foo1.stringField;
	scaDao.create(sca1);

	StringColumnArg sca2 = new StringColumnArg();
	sca2.str1 = "something eles";
	scaDao.create(sca2);

	QueryBuilder<Foo, Integer> fooQb = fooDao.queryBuilder();
	QueryBuilder<StringColumnArg, Integer> scaQb = scaDao.queryBuilder();
	scaQb.join(StringColumnArg.STR1_FIELD, Foo.STRING_COLUMN_NAME, fooQb);
	List<StringColumnArg> results = scaQb.query();
	assertNotNull(results);
	assertEquals(1, results.size());
	assertEquals(sca1.id, results.get(0).id);

	scaQb.reset();
	scaQb.join(StringColumnArg.STR1_FIELD, Foo.STRING_COLUMN_NAME, fooQb, JoinType.LEFT, JoinWhereOperation.AND);
	results = scaQb.query();
	assertNotNull(results);
	assertEquals(2, results.size());
	assertEquals(sca1.id, results.get(0).id);
	assertEquals(sca2.id, results.get(1).id);
}
 
Example 7
Source File: MappedCreateTest.java    From ormlite-core with ISC License 5 votes vote down vote up
@Test
public void testCreateWithJustGeneratedId() throws Exception {
	Dao<GeneratedId, Integer> dao = createDao(GeneratedId.class, true);
	GeneratedId genId = new GeneratedId();
	dao.create(genId);
	GeneratedId genId2 = dao.queryForId(genId.genId);
	assertEquals(genId.genId, genId2.genId);
}
 
Example 8
Source File: MappedCreateTest.java    From ormlite-core with ISC License 5 votes vote down vote up
@Test
public void testCreateReserverdTable() throws Exception {
	Dao<Where, String> dao = createDao(Where.class, true);
	String id = "from-string";
	Where where = new Where();
	where.id = id;
	dao.create(where);
	Where where2 = dao.queryForId(id);
	assertEquals(id, where2.id);
	assertEquals(1, dao.delete(where2));
	assertNull(dao.queryForId(id));
}
 
Example 9
Source File: TableInfoTest.java    From ormlite-core with ISC License 5 votes vote down vote up
/**
 * Test to make sure that we can call a private constructor
 */
@Test
public void testPrivateConstructor() throws Exception {
	Dao<PrivateConstructor, Object> packConstDao = createDao(PrivateConstructor.class, true);
	int id = 12312321;
	PrivateConstructor pack1 = PrivateConstructor.makeOne(id);
	assertEquals(id, pack1.id);
	packConstDao.create(pack1);
	// we should be able to look it up
	PrivateConstructor pack2 = packConstDao.queryForId(id);
	// and the id should match
	assertEquals(id, pack2.id);
}
 
Example 10
Source File: BaseMappedQueryTest.java    From ormlite-core with ISC License 5 votes vote down vote up
@Test
public void testCacheWithSelectColumns() throws Exception {
	Dao<CacheWithSelectColumns, Object> dao = createDao(CacheWithSelectColumns.class, true);
	// Add object to database
	CacheWithSelectColumns foo = new CacheWithSelectColumns();
	foo.text = "some text";
	dao.create(foo);
	// enable object cache
	dao.setObjectCache(true);

	// fetch the object, but only with its id field
	QueryBuilder<CacheWithSelectColumns, Object> qb =
			dao.queryBuilder().selectColumns(CacheWithSelectColumns.COLUMN_NAME_ID);
	CacheWithSelectColumns result = qb.queryForFirst();
	assertNotNull(result);
	assertNull(result.text);

	// fetch the same object again, this time asking for the text column as well
	qb = dao.queryBuilder().selectColumns(CacheWithSelectColumns.COLUMN_NAME_ID,
			CacheWithSelectColumns.COLUMN_NAME_TEXT);
	result = qb.queryForFirst();
	assertNotNull(result);
	assertEquals(foo.text, result.text);

	// fetch the same object again, this time asking for everything
	qb = dao.queryBuilder();
	result = qb.queryForFirst();
	assertNotNull(result);
	assertEquals(foo.text, result.text);
}
 
Example 11
Source File: JdbcDatabaseConnectionTest.java    From ormlite-jdbc with ISC License 5 votes vote down vote up
@Test(expected = SQLException.class)
public void testGeneratedIdNoReturn() throws Exception {
	createDao(FooNotGeneratedId.class, true);
	Dao<FooInt, Object> genDao = createDao(FooInt.class, false);
	FooInt foo = new FooInt();
	foo.stuff = "hello";
	genDao.create(foo);
}
 
Example 12
Source File: JdbcQueryBuilderTest.java    From ormlite-jdbc with ISC License 5 votes vote down vote up
protected void createPartial(Dao<PartialData, Integer> partialDao, List<Integer> ids, List<String> firsts,
		List<String> lasts, String first, String last) throws SQLException {
	PartialData partial = new PartialData();
	partial.first = first;
	partial.last = last;
	partialDao.create(partial);
	ids.add(partial.id);
	firsts.add(partial.first);
	lasts.add(partial.last);
	checkPartial(partialDao.queryForId(partial.id), ids, firsts, lasts, ids.size() - 1, false, false);
}
 
Example 13
Source File: BigIntegerTypeTest.java    From ormlite-core with ISC License 5 votes vote down vote up
@Test
public void testDefaultValue() throws Exception {
	Dao<BigIntegerDefaultValue, Object> dao = createDao(BigIntegerDefaultValue.class, true);
	BigIntegerDefaultValue foo = new BigIntegerDefaultValue();
	dao.create(foo);

	assertNull(foo.bigInteger);
	dao.refresh(foo);
	assertEquals(new BigInteger(DEFAULT_VALUE), foo.bigInteger);
}
 
Example 14
Source File: BigDecimalTypeTest.java    From ormlite-core with ISC License 5 votes vote down vote up
@Test
public void testDefaultValue() throws Exception {
	Dao<BigDecimalDefaultValue, Object> dao = createDao(BigDecimalDefaultValue.class, true);
	BigDecimalDefaultValue foo = new BigDecimalDefaultValue();
	dao.create(foo);

	assertNull(foo.bigDecimal);
	dao.refresh(foo);
	assertEquals(new BigDecimal(DEFAULT_VALUE), foo.bigDecimal);
}
 
Example 15
Source File: BigIntegerTypeTest.java    From ormlite-core with ISC License 5 votes vote down vote up
@Test
public void testBigIntegerId() throws Exception {
	Dao<BigIntegerId, BigInteger> dao = createDao(BigIntegerId.class, true);
	BigIntegerId foo = new BigIntegerId();
	dao.create(foo);
	assertEquals(BigInteger.ONE, foo.id);
	BigIntegerId result = dao.queryForId(BigInteger.ONE);
	assertNotNull(result);
	assertEquals(foo.id, result.id);
}
 
Example 16
Source File: AddressBook.java    From AndroidDatabaseLibraryComparison with MIT License 5 votes vote down vote up
/**
 * We have to do this separate step because OrmLite doesn't automatically insert children
 */
public void insertNewAddresses(Dao<AddressBook, Integer> addressBookDao, Dao<AddressItem, Integer> addressItemDao) throws SQLException {
    if (this.nonDaoAddresses == null) {
        return;
    }
    addressBookDao.assignEmptyForeignCollection(this, "addresses");
    for (AddressItem addressItem : nonDaoAddresses) {
        addressItem.setAddressBook(this);
        addressItemDao.create(addressItem);
        addresses.add(addressItem);
    }
}
 
Example 17
Source File: AddressBook.java    From AndroidDatabaseLibraryComparison with MIT License 5 votes vote down vote up
/**
 * We have to do this separate step because OrmLite doesn't automatically insert children
 */
public void insertNewContacts(Dao<AddressBook, Integer> addressBookDao, Dao<Contact, Integer> contactDao) throws SQLException {
    if (this.nonDaoContacts == null) {
        return;
    }
    addressBookDao.assignEmptyForeignCollection(this, "contacts");
    for (Contact contact : nonDaoContacts) {
        contact.setAddressBook(this);
        contactDao.create(contact);
        contacts.add(contact);
    }
}
 
Example 18
Source File: AddressBook.java    From AndroidDatabaseLibraryComparison with MIT License 5 votes vote down vote up
/**
 * We have to do this separate step because OrmLite doesn't automatically insert children
 */
public void insertNewAddresses(Dao<AddressBook, Integer> addressBookDao, Dao<AddressItem, Integer> addressItemDao) throws SQLException {
    if (this.nonDaoAddresses == null) {
        return;
    }
    addressBookDao.assignEmptyForeignCollection(this, "addresses");
    for (AddressItem addressItem : nonDaoAddresses) {
        addressItem.setAddressBook(this);
        addressItemDao.create(addressItem);
        addresses.add(addressItem);
    }
}
 
Example 19
Source File: FieldType.java    From ormlite-core with ISC License 4 votes vote down vote up
/**
 * Pass the foreign data argument to the foreign {@link Dao#create(Object)} method.
 */
public <T> int createWithForeignDao(T foreignData) throws SQLException {
	@SuppressWarnings("unchecked")
	Dao<T, ?> castDao = (Dao<T, ?>) foreignDao;
	return castDao.create(foreignData);
}
 
Example 20
Source File: QueryBuilderTest.java    From ormlite-core with ISC License 4 votes vote down vote up
@Test
public void testJoinTwoColumns() throws Exception {
	Dao<Foo, Integer> fooDao = createDao(Foo.class, true);
	Dao<StringColumnArg, Integer> scaDao = createDao(StringColumnArg.class, true);

	Foo foo1 = new Foo();
	foo1.val = 123213213;
	foo1.stringField = "stuff";
	fooDao.create(foo1);

	Foo foo2 = new Foo();
	foo2.stringField = "not stuff";
	fooDao.create(foo2);

	StringColumnArg sca1 = new StringColumnArg();
	sca1.str1 = foo1.stringField;
	scaDao.create(sca1);

	StringColumnArg sca2 = new StringColumnArg();
	sca2.str1 = foo2.stringField;
	scaDao.create(sca2);

	StringColumnArg sca3 = new StringColumnArg();
	sca3.str1 = "some other field";
	scaDao.create(sca3);

	QueryBuilder<Foo, Integer> fooQb = fooDao.queryBuilder();
	fooQb.where().eq(Foo.VAL_COLUMN_NAME, foo1.val);

	QueryBuilder<StringColumnArg, Integer> scaQb = scaDao.queryBuilder();
	scaQb.join(StringColumnArg.STR1_FIELD, Foo.STRING_COLUMN_NAME, fooQb);
	List<StringColumnArg> results = scaQb.query();
	assertNotNull(results);
	assertEquals(1, results.size());
	assertEquals(sca1.id, results.get(0).id);

	fooQb.reset();
	fooQb.where().eq(Foo.VAL_COLUMN_NAME, foo2.val);

	scaQb.reset();
	scaQb.join(StringColumnArg.STR1_FIELD, Foo.STRING_COLUMN_NAME, fooQb);
	results = scaQb.query();
	assertNotNull(results);
	assertEquals(1, results.size());
	assertEquals(sca2.id, results.get(0).id);
}