Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
zhaoyufei133
litepal
提交
87d4880b
L
litepal
项目概览
zhaoyufei133
/
litepal
与 Fork 源项目一致
从无法访问的项目Fork
通知
4
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
L
litepal
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
87d4880b
编写于
5月 23, 2020
作者:
G
guolindev
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add return value for saveAll method representing the saving result. Maybe true or false.
上级
eb5236c0
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
30 addition
and
13 deletion
+30
-13
core/src/main/java/org/litepal/Operator.java
core/src/main/java/org/litepal/Operator.java
+6
-5
sample/src/androidTest/java/com/litepaltest/test/crud/save/SaveAllTest.java
...Test/java/com/litepaltest/test/crud/save/SaveAllTest.java
+24
-8
未找到文件。
core/src/main/java/org/litepal/Operator.java
浏览文件 @
87d4880b
...
...
@@ -92,9 +92,7 @@ public class Operator {
* @return A writable SQLiteDatabase instance
*/
public
static
SQLiteDatabase
getDatabase
()
{
synchronized
(
LitePalSupport
.
class
)
{
return
Connector
.
getDatabase
();
}
return
Connector
.
getDatabase
();
}
/**
...
...
@@ -1474,8 +1472,9 @@ public class Operator {
*
* @param collection
* Holds all models to save.
* @return True if all records in collection are saved. False none record in collection is saved. There won't be partial saved condition.
*/
public
static
<
T
extends
LitePalSupport
>
void
saveAll
(
Collection
<
T
>
collection
)
{
public
static
<
T
extends
LitePalSupport
>
boolean
saveAll
(
Collection
<
T
>
collection
)
{
synchronized
(
LitePalSupport
.
class
)
{
SQLiteDatabase
db
=
Connector
.
getDatabase
();
db
.
beginTransaction
();
...
...
@@ -1483,8 +1482,10 @@ public class Operator {
SaveHandler
saveHandler
=
new
SaveHandler
(
db
);
saveHandler
.
onSaveAll
(
collection
);
db
.
setTransactionSuccessful
();
return
true
;
}
catch
(
Exception
e
)
{
throw
new
LitePalSupportException
(
e
.
getMessage
(),
e
);
e
.
printStackTrace
();
return
false
;
}
finally
{
db
.
endTransaction
();
}
...
...
sample/src/androidTest/java/com/litepaltest/test/crud/save/SaveAllTest.java
浏览文件 @
87d4880b
...
...
@@ -20,6 +20,7 @@ import java.util.Random;
import
java.util.UUID
;
import
static
junit
.
framework
.
TestCase
.
assertEquals
;
import
static
junit
.
framework
.
TestCase
.
assertFalse
;
import
static
junit
.
framework
.
TestCase
.
assertTrue
;
@SmallTest
...
...
@@ -45,7 +46,7 @@ public class SaveAllTest {
cellPhone
.
setSerial
(
UUID
.
randomUUID
().
toString
());
cellList
.
add
(
cellPhone
);
}
LitePal
.
saveAll
(
cellList
);
assertTrue
(
LitePal
.
saveAll
(
cellList
)
);
for
(
Cellphone
cell
:
cellList
)
{
assertTrue
(
cell
.
isSaved
());
}
...
...
@@ -61,7 +62,7 @@ public class SaveAllTest {
student
.
setAge
(
new
Random
().
nextInt
(
20
));
classroom
.
getStudentCollection
().
add
(
student
);
}
LitePal
.
saveAll
(
classroom
.
getStudentCollection
(
));
assertTrue
(
LitePal
.
saveAll
(
classroom
.
getStudentCollection
()
));
classroom
.
save
();
List
<
Student
>
list
=
LitePal
.
where
(
classroomTable
+
"_id = ?"
,
String
.
valueOf
(
classroom
.
get_id
())).
find
(
Student
.
class
);
...
...
@@ -81,7 +82,7 @@ public class SaveAllTest {
student
.
setClassroom
(
classroom
);
studentList
.
add
(
student
);
}
LitePal
.
saveAll
(
studentList
);
assertTrue
(
LitePal
.
saveAll
(
studentList
)
);
classroom
.
save
();
List
<
Student
>
list
=
LitePal
.
where
(
classroomTable
+
"_id = ?"
,
String
.
valueOf
(
classroom
.
get_id
())).
find
(
Student
.
class
);
...
...
@@ -102,8 +103,8 @@ public class SaveAllTest {
idcardList
.
add
(
idcard
);
studentList
.
add
(
student
);
}
LitePal
.
saveAll
(
idcardList
);
LitePal
.
saveAll
(
studentList
);
assertTrue
(
LitePal
.
saveAll
(
idcardList
)
);
assertTrue
(
LitePal
.
saveAll
(
studentList
)
);
for
(
Student
student
:
studentList
)
{
List
<
IdCard
>
result
=
LitePal
.
where
(
studentTable
+
"_id=?"
,
String
.
valueOf
(
student
.
getId
())).
find
(
IdCard
.
class
);
...
...
@@ -139,8 +140,8 @@ public class SaveAllTest {
student
.
getTeachers
().
add
(
teacherList
.
get
(
index3
));
studentList
.
add
(
student
);
}
LitePal
.
saveAll
(
studentList
);
LitePal
.
saveAll
(
teacherList
);
assertTrue
(
LitePal
.
saveAll
(
studentList
)
);
assertTrue
(
LitePal
.
saveAll
(
teacherList
)
);
String
studentTable
=
DBUtility
.
getTableNameByClassName
(
Student
.
class
.
getName
());
String
teacherTable
=
DBUtility
.
getTableNameByClassName
(
Teacher
.
class
.
getName
());
String
tableName
=
DBUtility
.
getIntermediateTableName
(
studentTable
,
teacherTable
);
...
...
@@ -167,7 +168,7 @@ public class SaveAllTest {
}
classroomList
.
add
(
classroom
);
}
LitePal
.
saveAll
(
classroomList
);
assertTrue
(
LitePal
.
saveAll
(
classroomList
)
);
assertEquals
(
50
,
classroomList
.
size
());
for
(
Classroom
classroom
:
classroomList
)
{
assertTrue
(
classroom
.
isSaved
());
...
...
@@ -178,4 +179,19 @@ public class SaveAllTest {
}
}
@Test
public
void
testSaveAllFailed
()
{
List
<
Cellphone
>
cellphones
=
new
ArrayList
<>();
String
serial
=
UUID
.
randomUUID
().
toString
();
for
(
int
i
=
0
;
i
<
20
;
i
++)
{
Cellphone
cellphone
=
new
Cellphone
();
cellphone
.
setBrand
(
"Apple"
);
cellphone
.
setSerial
(
serial
+
(
i
%
10
));
// serial is unique, so this should save failed
cellphones
.
add
(
cellphone
);
}
assertFalse
(
LitePal
.
saveAll
(
cellphones
));
List
<
Cellphone
>
list
=
LitePal
.
where
(
"serial like ?"
,
serial
+
"%"
).
find
(
Cellphone
.
class
);
assertTrue
(
list
.
isEmpty
());
}
}
\ No newline at end of file
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录