├── .gitignore ├── AutoDesignOperate.md ├── README.md ├── ZxyLibrary.md ├── ZxyTable.md ├── app ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── com │ │ └── ellen │ │ └── dhcsqlite │ │ └── ExampleInstrumentedTest.java │ ├── main │ ├── AndroidManifest.xml │ ├── java │ │ └── com │ │ │ └── ellen │ │ │ └── dhcsqlite │ │ │ ├── App.java │ │ │ ├── MyJxFormat.java │ │ │ ├── NewActivtiy.java │ │ │ ├── bean │ │ │ ├── Father.java │ │ │ ├── Student.java │ │ │ └── TestDefault.java │ │ │ └── sql │ │ │ ├── AppLibrary.java │ │ │ ├── MyAutoDesignOperate.java │ │ │ └── NewStudentTable.java │ └── res │ │ ├── drawable-v24 │ │ └── ic_launcher_foreground.xml │ │ ├── drawable │ │ └── ic_launcher_background.xml │ │ ├── layout │ │ └── activity_main.xml │ │ ├── mipmap-anydpi-v26 │ │ ├── ic_launcher.xml │ │ └── ic_launcher_round.xml │ │ ├── mipmap-hdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-mdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xxhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xxxhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ └── values │ │ ├── colors.xml │ │ ├── strings.xml │ │ └── styles.xml │ └── test │ └── java │ └── com │ └── ellen │ └── dhcsqlite │ └── ExampleUnitTest.java ├── build.gradle ├── dhcsqlitelibrary ├── .gitignore ├── build.gradle ├── consumer-rules.pro ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── com │ │ └── ellen │ │ └── dhcsqlitelibrary │ │ └── ExampleInstrumentedTest.java │ ├── main │ ├── AndroidManifest.xml │ ├── java │ │ └── com │ │ │ └── ellen │ │ │ └── dhcsqlitelibrary │ │ │ └── table │ │ │ ├── annotation │ │ │ ├── auto │ │ │ │ ├── Delete.java │ │ │ │ ├── Search.java │ │ │ │ ├── SearchByMajorKey.java │ │ │ │ ├── TotalSql.java │ │ │ │ ├── Update.java │ │ │ │ └── Value.java │ │ │ ├── field │ │ │ │ ├── DataStructure.java │ │ │ │ ├── DhcSqlFieldName.java │ │ │ │ ├── Ignore.java │ │ │ │ ├── Operate.java │ │ │ │ ├── OperateEnum.java │ │ │ │ ├── SqlType.java │ │ │ │ └── bound │ │ │ │ │ ├── Check.java │ │ │ │ │ ├── CreateEndString.java │ │ │ │ │ ├── Default.java │ │ │ │ │ ├── DefaultValueEnum.java │ │ │ │ │ ├── EndAutoString.java │ │ │ │ │ ├── MajorKey.java │ │ │ │ │ ├── NotNull.java │ │ │ │ │ └── Unique.java │ │ │ └── table │ │ │ │ └── DataClass.java │ │ │ ├── exception │ │ │ ├── BoolNoCanSaveException.java │ │ │ ├── JsonNoCanFormatException.java │ │ │ ├── NoCanSaveToSqlException.java │ │ │ ├── NoMajorKeyException.java │ │ │ └── SqlFieldDuplicateException.java │ │ │ ├── helper │ │ │ ├── CursorHelper.java │ │ │ ├── ReflectHelper.java │ │ │ └── json │ │ │ │ ├── FastJxFormat.java │ │ │ │ ├── GsonFormat.java │ │ │ │ ├── JsonLibraryType.java │ │ │ │ ├── JxFormat.java │ │ │ │ └── JxHelper.java │ │ │ ├── impl │ │ │ ├── BaseZxyLibrary.java │ │ │ ├── BaseZxyTable.java │ │ │ ├── CommonSetting.java │ │ │ ├── ZxyLibrary.java │ │ │ ├── ZxySQLiteHelper.java │ │ │ └── ZxyTable.java │ │ │ ├── operate │ │ │ ├── BaseOperate.java │ │ │ ├── DebugListener.java │ │ │ ├── SqlOperate.java │ │ │ ├── TotalListener.java │ │ │ ├── ZxySqlCreate.java │ │ │ ├── add │ │ │ │ └── Add.java │ │ │ ├── create │ │ │ │ ├── Create.java │ │ │ │ └── OnCreateTableCallback.java │ │ │ ├── delete │ │ │ │ └── Delete.java │ │ │ ├── search │ │ │ │ └── Search.java │ │ │ ├── table │ │ │ │ └── Table.java │ │ │ └── update │ │ │ │ └── Update.java │ │ │ ├── proxy │ │ │ ├── AutoDesignOperate.java │ │ │ └── AutoOperateProxy.java │ │ │ └── type │ │ │ ├── BasicTypeSupport.java │ │ │ ├── DataStructureSupport.java │ │ │ ├── Intercept.java │ │ │ ├── ObjectTypeSupport.java │ │ │ └── TypeSupport.java │ └── res │ │ └── values │ │ └── strings.xml │ └── test │ └── java │ └── com │ └── ellen │ └── dhcsqlitelibrary │ └── ExampleUnitTest.java ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat └── settings.gradle /.gitignore: -------------------------------------------------------------------------------- 1 | .idea 2 | *.iml 3 | .gradle 4 | /local.properties 5 | /.idea/caches 6 | /.idea/libraries 7 | /.idea/modules.xml 8 | /.idea/workspace.xml 9 | /.idea/navEditor.xml 10 | /.idea/assetWizardSettings.xml 11 | .DS_Store 12 | /build 13 | /captures 14 | .externalNativeBuild 15 | .cxx 16 | -------------------------------------------------------------------------------- /AutoDesignOperate.md: -------------------------------------------------------------------------------- 1 | 2 | # 1.简介 3 | 4 |   因为笔者考虑到导入此框架后代码简洁优雅性问题,所以加入了一个动态代理的机制,这个接口一方面为了让代码更加简洁,另一方面让您的数据库操纵业务化,那么怎样业务化呢?就是笔者提供了一系列注解,帮您完成很多数据库表增删改查乃至更多的操作问题,而且代码写起来非常简洁。 5 | 6 | # api介绍 7 | 8 | public interface MyAutoDesignOperate extends AutoDesignOperate { 9 | 10 | /** 11 | * 查找 12 | * @return 13 | */ 14 | @Search(whereSql = "your_age > @ageValue and my_name = '@name'",orderSql = "id ASC") 15 | List getSearchList1(@Value("ageValue") int ageValue, @Value("name") String name); 16 | 17 | /** 18 | * 查找且排序 19 | * @return 20 | */ 21 | @Search(whereSql = "id > 80",orderSql = "id DESC") 22 | List getSearchList2(); 23 | 24 | /** 25 | * 删除 26 | */ 27 | @Delete("id = @id and my_name = '@name'") 28 | void deleteData(@Value("id") int id,@Value("name") String name); 29 | 30 | @TotalSearchSql("SELECT * FROM Student WHERE your_age > 50;") 31 | List search(); 32 | 33 | @TotalUpdateSql("UPDATE Student SET my_name = '@newName' WHERE my_name = '@oldName';") 34 | void update(@Value("newName") String newName,@Value("oldName") String oldName); 35 | 36 | /** 37 | * 根据主键查询数据 38 | * @param id_value 39 | * @return 40 | */ 41 | @SearchByMajorKey(whereSql = "> @id_value") 42 | List searchByMajorKey(@Value("id_value") int id_value); 43 | 44 | } 45 | 46 | 使用方法: 47 | 48 | ZxyLibrary zxyLibrary = new AppLibrary(this, "sqlite_library", 1); 49 | NewStudentTable studentTable = new NewStudentTable(zxyLibrary,Student.class, MyAutoDesignOperate.class); 50 | 51 | //获取代理对象 52 | MyAutoDesignOperate myAutoDesignOperate = studentTable.getAutoDesignOperate(); 53 | 54 | 55 | //以下是通过动态代理直接调用,无需实现这个接口 56 | List studentList1 = myAutoDesignOperate.getSearchList1(3,"Ellen2018"); 57 | List studentList2 = myAutoDesignOperate.getSearchList2(); 58 | myAutoDesignOperate.deleteData(3,"Ellen2018"); 59 | List studentList3 = myAutoDesignOperate.search(); 60 | myAutoDesignOperate.update("新的名字","Ellen2018"); 61 | List studentList4 = myAutoDesignOperate.searchByMajorKey(0); 62 | 63 | 64 | 这个接口就是笔者在[ZxyTable](https://github.com/Ellen2018/DhcSQLite/blob/master/ZxyTable.md)中提到的元操作接口AutoDesignOperate,这么写有什么好处呢,就是让您的关于数据库操作的业务代码非常清晰,维护性非常好,任性修改,这也是笔者设计它的初衷。下面笔者来着重讲一讲元操作接口AutoDesignOperate中的注解的用法: 65 | 66 | - @Search 67 | 68 |   此注解用来完成搜素功能的,因此您使用的方法的返回值一定是个List集合,他需要传递2个String类型的参数值,一个参数值为whereSql(定义搜素条件),另外一个就是orderSql(定义结果的排序方式),whereSql和orderSql必须以Sql语句的方式传递,order不传值的时候啊,它不具备排序效果,如果您的sql语句中需要有变动的值,那么您可以结合@Value注解加上 “@值的名字” 进行动态sql语句定义,什么意思呢?比如我现在有个需求就是查询到年龄大于某个值的数据,查询的结果按照id进行升序,那么您的方法应该这么定义: 69 | 70 | @Search(whereSql = "your_age > @ageValue and my_name = '@name'",orderSql = "id ASC") 71 | List getSearchList1(@Value("ageValue") int ageValue, @Value("name") String name); 72 | 73 |   您可以看到在whereSql中有个@ageValue,它的意思就是会被方法参数列表的int ageValue所取代,因为这个int ageValue有一个@Value注解,且它的名字为"ageValue",用过Retrofit库的人应该这里一下子就能看懂。比如现在我这样调用getSearchList1(3,"周杰伦"),那么它执行的SQL语句为:"SELECT * FROM Student WHERE your_age > 3 and my_name = '周杰伦' ORDER BY id ASC;",如果我现在不想它按照id 进行排序,我想传递一个String的参数,让它按照这个传递参数值对应的字段进行排序呢,那么您应该这么改造您的方法: 74 | 75 | 76 | 77 | @Search(whereSql = "your_age > @ageValue and my_name = '@name'",orderSql = "@sortName ASC") 78 | List getSearchList1(@Value("ageValue") int ageValue, @Value("name") String name,@Value("sortName") String sortName); 79 | 80 | 你在调用时: 81 | 82 | //相当于执行了:SELECT * FROM Student WHERE your_age > 3 and my_name = '周杰伦' ORDER BY your_age ASC; 83 | myAutoDesignOperate.getSearchList1(3,"周杰伦","your_age"); 84 | 85 | //相当于执行了:SELECT * FROM Student WHERE your_age > 3 and my_name = '周杰伦' ORDER BY address ASC; 86 | myAutoDesignOperate.getSearchList1(3,"周杰伦","address"); 87 | 88 | - @SearchByMajorKey 89 | 90 |   这个注解就是用来和主键相关的查询,它的使用方式除了具备@Serach所有的功能之外,他还有在字符串中以"{}"替代主键字段名,啥啥意思呢,我们来个例子: 91 | 92 | @SearchByMajorKey(whereSql = "{} > @id_value") 93 | List searchByMajorKey(@Value("id_value") int id_value); 94 | 95 | 调用时 96 | 97 | //相当于执行了SELECT * FROM Student WHERE id > 6; 98 | myAutoDesignOperate.searchByMajorKey(6); 99 | 100 | 从代码中可以看出"{}"被"id"进行了取代,因为主键的字段名为"id",如果您的条件仅有一个且与主键相关,那么您连"{}"这个也不需要写,示例代码如下: 101 | 102 | @SearchByMajorKey(whereSql = "> @id_value") 103 | List searchByMajorKey(@Value("id_value") int id_value); 104 | 105 | 这和上面是一样的效果,注意下面这种方式是错误的: 106 | 107 | //这是一种错误的定义方式,因为“{}”不写的前提是您的条件仅仅且一个 108 | @SearchByMajorKey(whereSql = "> @id_value && {} < 27") 109 | List searchByMajorKey(@Value("id_value") int id_value); 110 | 111 | - @Update 112 | 113 |   从名字上来看它就是用来更新数据的,它规定的返回值为null(笔者后期可以改成int类型来记录您修改的数据个数,后面版本再修改吧),代码示例: 114 | 115 | @Update(valueSql = "name = '@newName'", whereSql = "sid = @sid") 116 | void updateStudentNameById(@Value("sid") int sid, @Value("newName") String newName); 117 | 118 | 调用时: 119 | 120 | //相当于执行了:UPDATE Student SET name = 'Ellen2020' WHERE sid = 3; 121 | myAutoDesignOperate.updateStudentNameById(3,"Ellen2020"); 122 | 123 | - @Delete 124 | 125 |   从名字上看就可以看出它是用来删除数据的,它规定的返回值也是void(笔者后期可以改成int类型来记录您删除的数据个数,后面版本再修改吧),代码示例: 126 | 127 | /** 128 | * 删除 129 | */ 130 | @Delete("id = @id and my_name = '@name'") 131 | void deleteData(@Value("id") int id,@Value("name") String name); 132 | 133 | 调用时: 134 | 135 | //相当于执行了:DELETE FROM Student WHERE id = 3 and my_name = 'Ellen2018'; 136 | myAutoDesignOperate.deleteData(3,"Ellen2018"); 137 | 138 | - @TotalSql 139 | 140 |   这个注解是用来完成整段sql执行的,也就是说通过它,您可以完成任何sql语句的执行,能能和@Value配合使用,除此之外呢需要注意的是执行sql语句无非是有2种结果,第一种就是无返回值的sql语句执行,第二种就是有返回值的sql语句执行,所以在此注解种笔者封装了一个isReturnValue(默认值为false)的参数去记录是否有返回值,如果您将它设置为true,那么你最好是接收一个List的返回值,否则为false的话,返回值为void即可,下面来代码演示一下: 141 | 142 | @TotalSql(sql = "SELECT * FROM Student WHERE my_name = '@name'",isReturnValue = true) 143 | List getStudentByName(@Value("name") String name); 144 | 145 | @TotalSql(sql = "DELETE FROM Student WHERE id = @id_value and my_name = '@name';") 146 | void delete(@Value("id_value")int id,@Value("name") String name); 147 | 148 | 调用时: 149 | 150 | //相当于执行了:SELECT * FROM Student WHERE my_name = 'Ellen2018' 151 | myAutoDesignOperate.getStudentByName("Ellen2018"); 152 | 153 | //相当于执行了:DELETE FROM Student WHERE id = 3 and my_name = '周杰伦'; 154 | myAutoDesignOperate.delete(3,"周杰伦"); 155 | 156 | - @Value 157 | 158 |   这个注解的作用就不用笔者解释了吧,看懂上面就知道了它的用法。 159 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | ## 0.如何导入? 3 | 4 | [![](https://jitpack.io/v/Ellen2018/DhcSQLite.svg)](https://jitpack.io/#Ellen2018/DhcSQLite) 5 | 6 | **当前最新版本:1.1.28** 7 | 8 |   首先你需要在项目的build.gradle中配置以下代码: 9 | 10 | allprojects { 11 | repositories { 12 | ... 13 | maven { url 'https://jitpack.io' }//加上这句即可 14 | } 15 | } 16 | 17 |   然后你在要使用该库的module中添加以下依赖: 18 | 19 | implementation 'com.github.Ellen2018:DhcSQLite:x.y.z' 20 | 21 |   x,y,z是笔者库的版本值,例如:1.0.0 22 | 23 | ## 1.框架简介 24 | 25 |   DhcSQLite是一款基于注解加反射的第三方SQLite库,基于SQLite进行的封装。您可以轻轻松松管理您的数据库,框架内部使用到了反射,注解,动态代理等技术,最最主要的是您要是觉得SQL语句写起来太麻烦,笔者提供给您另外一个SQL语句构建库[ZxySQLiteCreate](https://github.com/Ellen2018/ZxySQLiteCreate),整个框架内部也有使用到它。 26 | 27 | 28 | ## 2.快速入门 29 | 30 |   例子:学生成绩管理系统,学生属性包括:姓名(name),学号(sid),年龄(age),科目以及成绩(Map),父亲(Father(有姓名以及联系方式属性))等等。业务包括:查看总分排名,根据学号查询学生所有信息,查看姓名里带"李"的学生等等。 31 | 32 | ### 2.1 第一步:新建bean类 33 | 34 | Student类代码: 35 | 36 | public class Student { 37 | 38 | /** 39 | * 学号 40 | */ 41 | @MajorKey(isAutoIncrement = true)//主键 & isAutoIncrement = true表示自增 42 | private int sid; 43 | 44 | /** 45 | * 姓名 46 | */ 47 | private String name; 48 | 49 | /** 50 | * 年龄 51 | */ 52 | private int age; 53 | 54 | /** 55 | * 科目以及成绩 56 | */ 57 | @DataStructure //声明它是属于数据结构的属性 58 | private Map subjectMap; 59 | 60 | /** 61 | * 总分 62 | */ 63 | private int allGrade; 64 | 65 | /** 66 | * 是否为男生 67 | */ 68 | private boolean isMan; 69 | 70 | @SqlType(sqlFiledType = SQLFieldTypeEnum.TEXT)//映射为TEXT类型,不限制长度 71 | @Operate(operate = OperateEnum.JSON)//映射成Json 72 | private Father father; 73 | 74 | ...... 75 | 76 | } 77 | 78 | ### 2.2 步骤二:定义元操作接口(声明一个接口去继承AutoDesignOperate) 79 | 80 | public interface StudentOperate extends AutoDesignOperate { 81 | 82 | /** 83 | * 通过sid删除指定数据 84 | * @param sid 85 | */ 86 | @Delete("sid = @sid") 87 | void deleteBySid(@Value("sid") int sid); 88 | 89 | /** 90 | * 更新名字通过sid(学号) 91 | * 92 | * @param sid 93 | * @param newName 94 | */ 95 | @Update(valueSql = "name = '@newName'", whereSql = "sid = @sid") 96 | void updateStudentNameById(@Value("sid") int sid, @Value("newName") String newName); 97 | 98 | /** 99 | * 查询名字中带有str的数据,结果按照sid进行排序 100 | * 101 | * @param str 102 | * @return 103 | */ 104 | @Search(whereSql = "name like '%@str%'", orderSql = "sid ASC") 105 | List searchByLikeName(@Value("str") String str); 106 | 107 | } 108 | 109 | ### 2.3 步骤三:声明ZxyLibrary类(声明一个类去继承ZxyLibrary) 110 | 111 | public class AppLibrary extends ZxyLibrary { 112 | 113 | public AppLibrary(Context context, String name, int version) { 114 | super(context, name, version); 115 | } 116 | 117 | public AppLibrary(Context context, String libraryPath, String name, int version) { 118 | super(context, libraryPath, name, version); 119 | } 120 | 121 | @Override 122 | public void onZxySQLiteCreate(SQLiteDatabase db) { 123 | 124 | } 125 | 126 | @Override 127 | public void onZxySQLiteUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 128 | 129 | } 130 | } 131 | 132 | ### 2.4 步骤四:声明ZxyTable类(声明一个类去继承ZxyTable) 133 | 134 | public class StudentTable extends ZxyTable { 135 | 136 | private SQLiteDatabase db; 137 | 138 | public StudentTable(SQLiteDatabase db, String tableName, Class dataClass, Class autoClass) { 139 | super(db, tableName, dataClass, autoClass); 140 | } 141 | 142 | public StudentTable(SQLiteDatabase db, Class dataClass, Class autoClass) { 143 | super(db, dataClass, autoClass); 144 | } 145 | 146 | public StudentTable(ZxyLibrary zxyLibrary, String tableName, Class dataClass, Class autoClass) { 147 | super(zxyLibrary, tableName, dataClass, autoClass); 148 | } 149 | 150 | public StudentTable(ZxyLibrary zxyLibrary, Class dataClass, Class autoClass) { 151 | super(zxyLibrary, dataClass, autoClass); 152 | } 153 | 154 | 155 | @Override 156 | protected Object resumeDataStructure(String classFieldName, Class fieldClass, String json) { 157 | if(classFieldName.equals("fathers")){ 158 | Type founderSetType = new TypeToken() {}.getType(); 159 | Father[] fathers = new Gson().fromJson(json, founderSetType); 160 | return fathers; 161 | 162 | } 163 | return null; 164 | } 165 | 166 | /** 167 | * 库内部公共设置 168 | * @param commonSetting 169 | */ 170 | @Override 171 | protected void setting(CommonSetting commonSetting) { 172 | super.setting(commonSetting); 173 | //是否设置为多线程模式 174 | //true:设置为多线程模式,false:设置为非多线程模式 175 | commonSetting.setMultiThreadSafety(true); 176 | //设置库内部的Json解析器为Gson 177 | commonSetting.setJsonLibraryType(JsonLibraryType.Gson); 178 | //设置库内部的Json解析器为FastJson 179 | commonSetting.setJsonLibraryType(JsonLibraryType.FastJson); 180 | //设置库内部的Json解析为自定义的MyJsonFormat 181 | commonSetting.setJxFormat(new MyJxFormat()); 182 | } 183 | } 184 | 185 | ### 2.5 步骤五:开始创建数据库,表 & 进行一系列数据库操作 186 | 187 | [此处请直接点击查看如何操作数据库的代码](https://github.com/Ellen2018/DhcSqlteTest/blob/master/app/src/main/java/com/ellen/dhcsqltetest/MainActivity.java) 188 | 189 | 190 | [点击查看演示的项目](https://github.com/Ellen2018/DhcSqlteTest) 191 | 192 | ## 3.理论介绍 & 深入学习 193 | 194 |   DhcSqlite里面有4个比较重要的角色: 195 | 196 | - ZxySQLiteCreate(SQL语句制作者) 197 |   这个笔者已经提到了,它是用来帮助您完成各种Sql语句构建的,如果您想要完整看此框架的用法,请移步到[**基于Android SQLite语句构建库的轮子**](https://blog.csdn.net/ClAndEllen/article/details/103909339),一定要注意的是此框架DhcSqlite已经内部集成了,您无须单独集成,整个项目任何位置均可使用。 198 | 199 | - ZxyLibrary(数据库管理者) 200 |   这个是用来对数据库进行一系列操作的,您可以通过此类对库的名字,存储地址进行定义,它还包括对表的删除,重命名,清空等等操作,反是对表操作的都在里面已经封装好了,一个对象对应一个库。 201 | 202 |   **详细用法请查看**[**ZxyLibrary**](https://github.com/Ellen2018/DhcSQLite/blob/master/ZxyLibrary.md) 203 | 204 | - ZxyTable(数据库中表的管理者) 205 |   这个是用来对数据库中表进行一系列操作的,例如:增删改查,清空等等操作,它是一个泛型类,就是说泛型的参数指定的是关联的映射类, 此外还提供了好几个注解给您的bean类提供方便的数据库映射逻辑,例如:@MajorKey是用来bean类中指定主键的注解,@Ignore是用来bean类中不想该属性映射到数据库中的注解,还有很多其他的注解。 206 | 207 |   **详细用法请查看**[**ZxyTable**](https://github.com/Ellen2018/DhcSQLite/blob/master/ZxyTable.md) 208 | 209 | - AutoDesignOperate(自定义元操作者) 210 |   这个是一个接口,它是用来具体定义您项目中具体业务逻辑的接口,以动态代理的方式为您完成各种骚操作,您只需要在实现AutoDesignOperate里面声明方法,方法中使用@Search,@Update,@Value等注解帮您自动操作数据库,好处就是你不需要自动手动写代码,您只需要注意sql语句的填写即可。 211 | 212 |   **详细用法请查看**[**AutoDesignOperate**](https://github.com/Ellen2018/DhcSQLite/blob/master/AutoDesignOperate.md) 213 | -------------------------------------------------------------------------------- /ZxyLibrary.md: -------------------------------------------------------------------------------- 1 | 2 | # 1.简介 3 | 4 |   你使用此库的第一步就是创建一个ZxyLibrary的类,在创建库的位置new 一个此类的对象出来即可。您可以根据构造器选择库存储的目录,可以存在于data/data......目录下,也可以存储在外部存储目录下,只是调用不同的构造器罢了。 5 | 6 | # 2.代码介绍 7 | 8 | 9 | 类的代码: 10 | 11 | public class AppLibrary extends ZxyLibrary { 12 | 13 | /** 14 | * 15 | * @param context 上下文 16 | * @param name 数据库的名字 17 | * @param version 数据库的版本 18 | */ 19 | public AppLibrary(Context context, String name, int version) { 20 | super(context, name, version); 21 | } 22 | 23 | /** 24 | * 25 | * @param context 上下文 26 | * @param libraryPath 数据库所在的父目录 27 | * @param name 数据库的名字 28 | * @param version 数据库版本号 29 | */ 30 | public AppLibrary(Context context, String libraryPath, String name, int version) { 31 | super(context, libraryPath, name, version); 32 | } 33 | 34 | /** 35 | * 创建数据库时回调 36 | * @param db 37 | */ 38 | @Override 39 | public void onZxySQLiteCreate(SQLiteDatabase db) { 40 | 41 | } 42 | 43 | /** 44 | * 数据库版本升级时回调 45 | * @param db 46 | * @param oldVersion 47 | * @param newVersion 48 | */ 49 | @Override 50 | public void onZxySQLiteUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 51 | 52 | } 53 | } 54 | 55 | 如何使用? 56 | 57 | //这种方式创建的数据库存放于data/data......目录下 58 | AppLibrary appLibrary = new AppLibrary(this, "您的数据库名字", 1); 59 | 60 | //这种方式创建的数据库存放于外部目录下(注意这里需要先申请到文件读写权限才行) 61 | AppLibrary appLibrary = new AppLibrary(this, "父目录","您的数据库名字", 1); 62 | 63 | //获取SQLiteDatabase对象 64 | SQLiteDatabase sqLiteDatabase = appLibrary.getWriteDataBase(); 65 | 66 | # 3.常规操作 67 | 68 | - 删除表 69 | 70 | appLibrary.deleteTable("表名"); 71 | 72 | - 重命名表 73 | 74 | appLibrary.deleteTable("旧的表名","新的表名"); 75 | 76 | - 清空表 77 | 78 | appLibrary.clearLibrary(); 79 | 80 | - 执行任意sql语句(建议不使用这种,因为笔者封装了另外一套,方便简洁) 81 | 82 | appLibrary.getWriteDataBase().exeSQL("sql语句"); 83 | 84 | - 删除库中所有表 85 | 86 | appLibrary.clearLibrary(); 87 | 88 | - 获取所有表名 89 | 90 | String[] tableNameArray = appLibrary.getAllTableName() 91 | 92 | - 获取表的个数 93 | 94 | appLibrary.getTableCount() 95 | 96 | 97 | -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 28 5 | defaultConfig { 6 | applicationId "com.ellen.dhcsqlite" 7 | minSdkVersion 19 8 | targetSdkVersion 28 9 | versionCode 1 10 | versionName "1.0" 11 | testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" 12 | } 13 | buildTypes { 14 | release { 15 | minifyEnabled false 16 | proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' 17 | } 18 | } 19 | } 20 | 21 | dependencies { 22 | implementation fileTree(dir: 'libs', include: ['*.jar']) 23 | implementation 'com.android.support:appcompat-v7:28.0.0' 24 | implementation project(path: ':dhcsqlitelibrary') 25 | implementation 'com.google.code.gson:gson:2.8.5' 26 | } 27 | -------------------------------------------------------------------------------- /app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # You can control the set of applied configuration files using the 3 | # proguardFiles setting in build.gradle. 4 | # 5 | # For more details, see 6 | # http://developer.android.com/guide/developing/tools/proguard.html 7 | 8 | # If your project uses WebView with JS, uncomment the following 9 | # and specify the fully qualified class name to the JavaScript interface 10 | # class: 11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 12 | # public *; 13 | #} 14 | 15 | # Uncomment this to preserve the line number information for 16 | # debugging stack traces. 17 | #-keepattributes SourceFile,LineNumberTable 18 | 19 | # If you keep the line number information, uncomment this to 20 | # hide the original source file name. 21 | #-renamesourcefileattribute SourceFile 22 | -------------------------------------------------------------------------------- /app/src/androidTest/java/com/ellen/dhcsqlite/ExampleInstrumentedTest.java: -------------------------------------------------------------------------------- 1 | package com.ellen.dhcsqlite; 2 | 3 | import android.content.Context; 4 | 5 | import androidx.test.platform.app.InstrumentationRegistry; 6 | import androidx.test.ext.junit.runners.AndroidJUnit4; 7 | 8 | import org.junit.Test; 9 | import org.junit.runner.RunWith; 10 | 11 | import static org.junit.Assert.*; 12 | 13 | /** 14 | * Instrumented test, which will execute on an Android device. 15 | * 16 | * @see Testing documentation 17 | */ 18 | @RunWith(AndroidJUnit4.class) 19 | public class ExampleInstrumentedTest { 20 | @Test 21 | public void useAppContext() { 22 | // Context of the app under test. 23 | Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext(); 24 | 25 | assertEquals("com.ellen.dhcsqlite", appContext.getPackageName()); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /app/src/main/java/com/ellen/dhcsqlite/App.java: -------------------------------------------------------------------------------- 1 | package com.ellen.dhcsqlite; 2 | 3 | import android.app.Application; 4 | import android.util.Log; 5 | 6 | import com.ellen.dhcsqlitelibrary.table.operate.TotalListener; 7 | import com.ellen.dhcsqlitelibrary.table.impl.ZxyTable; 8 | 9 | public class App extends Application { 10 | 11 | @Override 12 | public void onCreate() { 13 | super.onCreate(); 14 | //全局sql语句操作监听 15 | ZxyTable.setTotalListener(new TotalListener() { 16 | @Override 17 | public void exeSql(String tableName, String sql) { 18 | Log.e("Ellen2018","全局监听SQL语句("+tableName+"):"+sql); 19 | } 20 | }); 21 | 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /app/src/main/java/com/ellen/dhcsqlite/MyJxFormat.java: -------------------------------------------------------------------------------- 1 | package com.ellen.dhcsqlite; 2 | 3 | 4 | import com.ellen.dhcsqlitelibrary.table.helper.json.JxFormat; 5 | import com.google.gson.Gson; 6 | 7 | /** 8 | * 自定义Json解析器 9 | */ 10 | public class MyJxFormat implements JxFormat { 11 | 12 | private Gson gson; 13 | 14 | public MyJxFormat(){ 15 | gson = new Gson(); 16 | } 17 | 18 | @Override 19 | public String toJxString(Object obj) { 20 | return gson.toJson(obj); 21 | } 22 | 23 | @Override 24 | public E toObject(String json, Class jsonClass) { 25 | return (E) gson.fromJson(json,jsonClass); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /app/src/main/java/com/ellen/dhcsqlite/NewActivtiy.java: -------------------------------------------------------------------------------- 1 | package com.ellen.dhcsqlite; 2 | 3 | import android.app.Activity; 4 | import android.database.sqlite.SQLiteDatabase; 5 | import android.os.Bundle; 6 | import android.util.Log; 7 | 8 | import androidx.annotation.Nullable; 9 | 10 | import com.ellen.dhcsqlite.bean.Father; 11 | import com.ellen.dhcsqlite.bean.Student; 12 | import com.ellen.dhcsqlite.sql.AppLibrary; 13 | import com.ellen.dhcsqlite.sql.MyAutoDesignOperate; 14 | import com.ellen.dhcsqlite.sql.NewStudentTable; 15 | import com.ellen.dhcsqlitelibrary.table.operate.create.OnCreateTableCallback; 16 | import com.ellen.dhcsqlitelibrary.table.impl.ZxyLibrary; 17 | 18 | import java.util.ArrayList; 19 | import java.util.List; 20 | 21 | public class NewActivtiy extends Activity { 22 | 23 | @Override 24 | protected void onCreate(@Nullable Bundle savedInstanceState) { 25 | super.onCreate(savedInstanceState); 26 | setContentView(R.layout.activity_main); 27 | ZxyLibrary zxyLibrary = new AppLibrary(this, "sqlite_library", 1); 28 | zxyLibrary.clearLibrary(); 29 | SQLiteDatabase sqLiteDatabase = zxyLibrary.getWriteDataBase(); 30 | 31 | sqLiteDatabase.enableWriteAheadLogging();//开启数据库的多线程读写 32 | 33 | NewStudentTable studentTable = new NewStudentTable(zxyLibrary,Student.class, MyAutoDesignOperate.class); 34 | 35 | 36 | if (studentTable.isExist()) { 37 | studentTable.deleteTable(); 38 | } 39 | 40 | Log.e("Ellen2018", "表是否存在:" + studentTable.isExist()); 41 | 42 | studentTable.onCreateTableIfNotExits(new OnCreateTableCallback() { 43 | @Override 44 | public void onCreateTableFailure(String errMessage, String tableName, String createSQL) { 45 | 46 | } 47 | 48 | @Override 49 | public void onCreateTableSuccess(String tableName, String createSQL) { 50 | 51 | } 52 | }); 53 | 54 | Log.e("Ellen2018", "表是否存在:" + studentTable.isExist()); 55 | 56 | Student student = new Student("Ellen2018", 19, "18272167574", "火星"); 57 | student.setMan(true); 58 | Father father = new Father("Ellen2019", "1", "尼玛"); 59 | student.setFather(father); 60 | 61 | //单条数据添加 62 | studentTable.saveData(student); 63 | 64 | List studentList = new ArrayList<>(); 65 | for (int i = 0; i < 10; i++) { 66 | student = new Student("Ellen2018_" + i, i, "18272167574", "火星"); 67 | father = new Father("Ellen2019", "1", "尼玛" + i); 68 | student.setFather(father); 69 | if(i == 3){ 70 | student.setMan(true); 71 | } 72 | studentList.add(student); 73 | } 74 | studentTable.saveData(studentList,4); 75 | 76 | //通过代理接口调用 77 | studentTable.getAutoDesignOperate().update1("新的名字","Ellen2018_0"); 78 | 79 | for (Student student1 : studentTable.getAllData()) { 80 | Log.e("Ellen2018", "数据:" + student1.toString()); 81 | } 82 | 83 | } 84 | } 85 | -------------------------------------------------------------------------------- /app/src/main/java/com/ellen/dhcsqlite/bean/Father.java: -------------------------------------------------------------------------------- 1 | package com.ellen.dhcsqlite.bean; 2 | 3 | public class Father { 4 | 5 | private String name; 6 | private String id; 7 | private String student; 8 | 9 | public Father(){} 10 | 11 | public Father(String name, String id,String s) { 12 | this.name = name; 13 | this.id = id; 14 | this.student = s; 15 | } 16 | 17 | public String getName() { 18 | return name; 19 | } 20 | 21 | public void setName(String name) { 22 | this.name = name; 23 | } 24 | 25 | public String getId() { 26 | return id; 27 | } 28 | 29 | public void setId(String id) { 30 | this.id = id; 31 | } 32 | 33 | 34 | @Override 35 | public String toString() { 36 | return "Father{" + 37 | "name='" + name + '\'' + 38 | ", id='" + id + '\'' + 39 | ", student='" + student + '\'' + 40 | '}'; 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /app/src/main/java/com/ellen/dhcsqlite/bean/Student.java: -------------------------------------------------------------------------------- 1 | package com.ellen.dhcsqlite.bean; 2 | 3 | import com.ellen.dhcsqlitelibrary.table.annotation.field.bound.Check; 4 | import com.ellen.dhcsqlitelibrary.table.annotation.field.bound.CreateEndString; 5 | import com.ellen.dhcsqlitelibrary.table.annotation.field.DataStructure; 6 | import com.ellen.dhcsqlitelibrary.table.annotation.field.DhcSqlFieldName; 7 | import com.ellen.dhcsqlitelibrary.table.annotation.field.bound.Default; 8 | import com.ellen.dhcsqlitelibrary.table.annotation.field.bound.DefaultValueEnum; 9 | import com.ellen.dhcsqlitelibrary.table.annotation.field.Ignore; 10 | import com.ellen.dhcsqlitelibrary.table.annotation.field.bound.MajorKey; 11 | import com.ellen.dhcsqlitelibrary.table.annotation.field.Operate; 12 | import com.ellen.dhcsqlitelibrary.table.annotation.field.OperateEnum; 13 | 14 | import java.util.Arrays; 15 | 16 | @CreateEndString("整个表的约束") 17 | public class Student { 18 | 19 | //主键 20 | @MajorKey(isAutoIncrement = true) 21 | @DhcSqlFieldName(sqlFieldName = "id") 22 | private int id; 23 | @DhcSqlFieldName(sqlFieldName = "my_name") //映射数据库中字段名字为my_name 24 | private String name; 25 | @DhcSqlFieldName(sqlFieldName = "your_age") 26 | @Default(defaultValueEnum = DefaultValueEnum.BYTE,byteValue = 3) 27 | @Check("{} > -1") 28 | private int age; 29 | private String phoneNumber; 30 | private String address; 31 | @Ignore //不映射这个属性到数据库中 32 | private String ingoreString; 33 | @DhcSqlFieldName(sqlFieldName = "真不真") 34 | private Boolean isMan; 35 | @Operate(operate = OperateEnum.VALUE,valueName = "student") 36 | private Father father; 37 | private Father father2; 38 | @DataStructure //表示这个属性是数据类型属性,需要用注解区分,才能正确的进行json映射,否则会报错 39 | @DhcSqlFieldName(sqlFieldName = "爸爸们") 40 | private Father[] fathers; 41 | private double f = 3.6; 42 | 43 | public Student(String name, int age, String phoneNumber, String address) { 44 | this.name = name; 45 | this.age = age; 46 | this.phoneNumber = phoneNumber; 47 | this.address = address; 48 | 49 | fathers = new Father[5]; 50 | for (int i = 0; i < fathers.length; i++) { 51 | fathers[i] = new Father("名字:" + i, "ID:" + i,"好尼玛"+i); 52 | } 53 | } 54 | 55 | @Override 56 | public String toString() { 57 | return "Student{" + 58 | "id=" + id + 59 | ", name='" + name + '\'' + 60 | ", age=" + age + 61 | ", phoneNumber='" + phoneNumber + '\'' + 62 | ", address='" + address + '\'' + 63 | ", ingoreString='" + ingoreString + '\'' + 64 | ", isMan=" + isMan + 65 | ", father=" + father + 66 | ", father2=" + father2 + 67 | ", fathers=" + Arrays.toString(fathers) + 68 | ", f=" + f + 69 | '}'; 70 | } 71 | 72 | public String getName() { 73 | return name; 74 | } 75 | 76 | public void setName(String name) { 77 | this.name = name; 78 | } 79 | 80 | public int getAge() { 81 | return age; 82 | } 83 | 84 | public void setAge(int age) { 85 | this.age = age; 86 | } 87 | 88 | public String getPhoneNumber() { 89 | return phoneNumber; 90 | } 91 | 92 | public void setPhoneNumber(String phoneNumber) { 93 | this.phoneNumber = phoneNumber; 94 | } 95 | 96 | public String getAddress() { 97 | return address; 98 | } 99 | 100 | public void setAddress(String address) { 101 | this.address = address; 102 | } 103 | 104 | public Father getFather() { 105 | return father; 106 | } 107 | 108 | public void setFather(Father father) { 109 | this.father = father; 110 | } 111 | 112 | public boolean isMan() { 113 | return isMan; 114 | } 115 | 116 | public void setMan(boolean man) { 117 | isMan = man; 118 | } 119 | } 120 | 121 | -------------------------------------------------------------------------------- /app/src/main/java/com/ellen/dhcsqlite/bean/TestDefault.java: -------------------------------------------------------------------------------- 1 | package com.ellen.dhcsqlite.bean; 2 | 3 | import com.ellen.dhcsqlitelibrary.table.annotation.field.bound.Default; 4 | import com.ellen.dhcsqlitelibrary.table.annotation.field.bound.DefaultValueEnum; 5 | 6 | public class TestDefault { 7 | 8 | @Default(defaultValueEnum = DefaultValueEnum.STRING,strValue = "字符串默认值") 9 | private byte a1; 10 | @Default(defaultValueEnum = DefaultValueEnum.SHORT,shortValue = 2) 11 | private short a2; 12 | @Default(defaultValueEnum = DefaultValueEnum.INT,intValue = 3) 13 | private int a3; 14 | @Default(defaultValueEnum = DefaultValueEnum.LONG,intValue = 4) 15 | private long a4; 16 | 17 | 18 | 19 | 20 | } 21 | -------------------------------------------------------------------------------- /app/src/main/java/com/ellen/dhcsqlite/sql/AppLibrary.java: -------------------------------------------------------------------------------- 1 | package com.ellen.dhcsqlite.sql; 2 | 3 | import android.content.Context; 4 | import android.database.sqlite.SQLiteDatabase; 5 | 6 | import com.ellen.dhcsqlitelibrary.table.impl.ZxyLibrary; 7 | 8 | public class AppLibrary extends ZxyLibrary { 9 | 10 | /** 11 | * 12 | * @param context 上下文 13 | * @param name 数据库的名字 14 | * @param version 数据库的版本 15 | */ 16 | public AppLibrary(Context context, String name, int version) { 17 | super(context, name, version); 18 | } 19 | 20 | /** 21 | * 22 | * @param context 上下文 23 | * @param libraryPath 数据库所在的父目录 24 | * @param name 数据库的名字 25 | * @param version 数据库版本号 26 | */ 27 | public AppLibrary(Context context, String libraryPath, String name, int version) { 28 | super(context, libraryPath, name, version); 29 | } 30 | 31 | /** 32 | * 创建数据库时回调 33 | * @param db 34 | */ 35 | @Override 36 | public void onZxySQLiteCreate(SQLiteDatabase db) { 37 | 38 | } 39 | 40 | /** 41 | * 数据库版本升级时回调 42 | * @param db 43 | * @param oldVersion 44 | * @param newVersion 45 | */ 46 | @Override 47 | public void onZxySQLiteUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 48 | 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /app/src/main/java/com/ellen/dhcsqlite/sql/MyAutoDesignOperate.java: -------------------------------------------------------------------------------- 1 | package com.ellen.dhcsqlite.sql; 2 | 3 | import com.ellen.dhcsqlite.bean.Student; 4 | import com.ellen.dhcsqlitelibrary.table.proxy.AutoDesignOperate; 5 | import com.ellen.dhcsqlitelibrary.table.annotation.auto.Delete; 6 | import com.ellen.dhcsqlitelibrary.table.annotation.auto.Search; 7 | import com.ellen.dhcsqlitelibrary.table.annotation.auto.SearchByMajorKey; 8 | import com.ellen.dhcsqlitelibrary.table.annotation.auto.TotalSql; 9 | import com.ellen.dhcsqlitelibrary.table.annotation.auto.Update; 10 | import com.ellen.dhcsqlitelibrary.table.annotation.auto.Value; 11 | 12 | import java.util.List; 13 | 14 | public interface MyAutoDesignOperate extends AutoDesignOperate { 15 | 16 | /** 17 | * 查找 18 | * @return 19 | */ 20 | @Search(whereSql = "your_age > @ageValue and my_name = '@name'",orderSql = "@sortName ASC") 21 | List getSearchList1(@Value("ageValue") int ageValue, @Value("name") String name,@Value("sortName") String sortName); 22 | 23 | /** 24 | * 查找且排序 25 | * @return 26 | */ 27 | @Search(whereSql = "id > 3",orderSql = "id DESC") 28 | List getSearchList2(); 29 | 30 | /** 31 | * 删除 32 | */ 33 | @Delete("id = @id and my_name = '@name'") 34 | void deleteData(@Value("id") int id,@Value("name") String name); 35 | 36 | @Update(valueSql = " my_name = '@newName'",whereSql = "my_name = '@oldName'") 37 | void update1(@Value("newName") String newName,@Value("oldName") String oldName); 38 | 39 | /** 40 | * 根据主键查询数据 41 | * @param id_value 42 | * @return 43 | */ 44 | @SearchByMajorKey(whereSql = "{} > @id_value") 45 | List searchByMajorKey(@Value("id_value") int id_value); 46 | 47 | @TotalSql(sql = "SELECT id,my_name,father FROM Student WHERE my_name = '@name'",isReturnValue = true) 48 | List getStudentByName(@Value("name") String name); 49 | 50 | @TotalSql(sql = "DELETE FROM Student WHERE id = @id_value and my_name = '@name';") 51 | void delete(@Value("id_value")int id,@Value("name") String name); 52 | 53 | } 54 | 55 | -------------------------------------------------------------------------------- /app/src/main/java/com/ellen/dhcsqlite/sql/NewStudentTable.java: -------------------------------------------------------------------------------- 1 | package com.ellen.dhcsqlite.sql; 2 | 3 | import android.database.sqlite.SQLiteDatabase; 4 | 5 | import com.ellen.dhcsqlite.MyJxFormat; 6 | import com.ellen.dhcsqlite.bean.Father; 7 | import com.ellen.dhcsqlite.bean.Student; 8 | import com.ellen.dhcsqlitelibrary.table.helper.json.JsonLibraryType; 9 | import com.ellen.dhcsqlitelibrary.table.impl.CommonSetting; 10 | import com.ellen.dhcsqlitelibrary.table.impl.ZxyLibrary; 11 | import com.ellen.dhcsqlitelibrary.table.impl.ZxyTable; 12 | import com.google.gson.Gson; 13 | import com.google.gson.reflect.TypeToken; 14 | 15 | import java.lang.reflect.Type; 16 | 17 | public class NewStudentTable extends ZxyTable { 18 | 19 | private SQLiteDatabase db; 20 | 21 | public NewStudentTable(SQLiteDatabase db, String tableName, Class dataClass, Class autoClass) { 22 | super(db, tableName, dataClass, autoClass); 23 | } 24 | 25 | public NewStudentTable(SQLiteDatabase db, Class dataClass, Class autoClass) { 26 | super(db, dataClass, autoClass); 27 | } 28 | 29 | public NewStudentTable(ZxyLibrary zxyLibrary, String tableName, Class dataClass, Class autoClass) { 30 | super(zxyLibrary, tableName, dataClass, autoClass); 31 | } 32 | 33 | public NewStudentTable(ZxyLibrary zxyLibrary, Class dataClass, Class autoClass) { 34 | super(zxyLibrary, dataClass, autoClass); 35 | } 36 | 37 | 38 | @Override 39 | protected Object resumeDataStructure(String classFieldName, Class fieldClass, String json) { 40 | if(classFieldName.equals("fathers")){ 41 | Type founderSetType = new TypeToken() {}.getType(); 42 | Father[] fathers = new Gson().fromJson(json, founderSetType); 43 | return fathers; 44 | 45 | } 46 | return null; 47 | } 48 | 49 | /** 50 | * 库内部公共设置 51 | * @param commonSetting 52 | */ 53 | @Override 54 | protected void setting(CommonSetting commonSetting) { 55 | super.setting(commonSetting); 56 | //是否设置为多线程模式 57 | //true:设置为多线程模式,false:设置为非多线程模式 58 | commonSetting.setMultiThreadSafety(true); 59 | //设置库内部的Json解析器为Gson 60 | commonSetting.setJsonLibraryType(JsonLibraryType.GSON); 61 | //设置库内部的Json解析器为FastJson 62 | commonSetting.setJsonLibraryType(JsonLibraryType.FAST_JSON); 63 | //设置库内部的Json解析为自定义的MyJsonFormat 64 | commonSetting.setJxFormat(new MyJxFormat()); 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /app/src/main/res/drawable-v24/ic_launcher_foreground.xml: -------------------------------------------------------------------------------- 1 | 7 | 12 | 13 | 19 | 22 | 25 | 26 | 27 | 28 | 34 | 35 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_launcher_background.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 10 | 15 | 20 | 25 | 30 | 35 | 40 | 45 | 50 | 55 | 60 | 65 | 70 | 75 | 80 | 85 | 90 | 95 | 100 | 105 | 110 | 115 | 120 | 125 | 130 | 135 | 140 | 145 | 150 | 155 | 160 | 165 | 170 | 171 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 8 | 9 | 15 | 16 | 22 | 23 | 29 | 30 | 36 | 37 | 41 | 42 | -------------------------------------------------------------------------------- /app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /app/src/main/res/mipmap-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ellen2018/DhcSQLite/7eb8704053f0a54a142cec3b8bcca186bec50828/app/src/main/res/mipmap-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-hdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ellen2018/DhcSQLite/7eb8704053f0a54a142cec3b8bcca186bec50828/app/src/main/res/mipmap-hdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ellen2018/DhcSQLite/7eb8704053f0a54a142cec3b8bcca186bec50828/app/src/main/res/mipmap-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ellen2018/DhcSQLite/7eb8704053f0a54a142cec3b8bcca186bec50828/app/src/main/res/mipmap-mdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ellen2018/DhcSQLite/7eb8704053f0a54a142cec3b8bcca186bec50828/app/src/main/res/mipmap-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ellen2018/DhcSQLite/7eb8704053f0a54a142cec3b8bcca186bec50828/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ellen2018/DhcSQLite/7eb8704053f0a54a142cec3b8bcca186bec50828/app/src/main/res/mipmap-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ellen2018/DhcSQLite/7eb8704053f0a54a142cec3b8bcca186bec50828/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ellen2018/DhcSQLite/7eb8704053f0a54a142cec3b8bcca186bec50828/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ellen2018/DhcSQLite/7eb8704053f0a54a142cec3b8bcca186bec50828/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/values/colors.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | #008577 4 | #00574B 5 | #D81B60 6 | 7 | -------------------------------------------------------------------------------- /app/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | DhcSQLite 3 | 4 | -------------------------------------------------------------------------------- /app/src/main/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /app/src/test/java/com/ellen/dhcsqlite/ExampleUnitTest.java: -------------------------------------------------------------------------------- 1 | package com.ellen.dhcsqlite; 2 | 3 | import org.junit.Test; 4 | 5 | import static org.junit.Assert.*; 6 | 7 | /** 8 | * Example local unit test, which will execute on the development machine (host). 9 | * 10 | * @see Testing documentation 11 | */ 12 | public class ExampleUnitTest { 13 | @Test 14 | public void addition_isCorrect() { 15 | assertEquals(4, 2 + 2); 16 | } 17 | } -------------------------------------------------------------------------------- /build.gradle: -------------------------------------------------------------------------------- 1 | // Top-level build file where you can add configuration options common to all sub-projects/modules. 2 | 3 | buildscript { 4 | repositories { 5 | google() 6 | jcenter() 7 | 8 | } 9 | dependencies { 10 | classpath 'com.android.tools.build:gradle:4.0.0' 11 | 12 | // NOTE: Do not place your application dependencies here; they belong 13 | // in the individual module build.gradle files 14 | } 15 | } 16 | 17 | allprojects { 18 | repositories { 19 | google() 20 | jcenter() 21 | maven { url 'https://www.jitpack.io' } 22 | } 23 | } 24 | 25 | task clean(type: Delete) { 26 | delete rootProject.buildDir 27 | } 28 | -------------------------------------------------------------------------------- /dhcsqlitelibrary/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /dhcsqlitelibrary/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.library' 2 | 3 | android { 4 | compileSdkVersion 28 5 | 6 | 7 | defaultConfig { 8 | minSdkVersion 16 9 | targetSdkVersion 28 10 | versionCode 1 11 | versionName "1.0" 12 | 13 | testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" 14 | consumerProguardFiles 'consumer-rules.pro' 15 | } 16 | 17 | buildTypes { 18 | release { 19 | minifyEnabled false 20 | proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' 21 | } 22 | } 23 | 24 | } 25 | 26 | dependencies { 27 | implementation fileTree(dir: 'libs', include: ['*.jar']) 28 | 29 | implementation 'com.android.support:appcompat-v7:28.0.0' 30 | api 'com.github.Ellen2018:ZxySQLiteCreate:1.0.12' 31 | } 32 | -------------------------------------------------------------------------------- /dhcsqlitelibrary/consumer-rules.pro: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ellen2018/DhcSQLite/7eb8704053f0a54a142cec3b8bcca186bec50828/dhcsqlitelibrary/consumer-rules.pro -------------------------------------------------------------------------------- /dhcsqlitelibrary/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # You can control the set of applied configuration files using the 3 | # proguardFiles setting in build.gradle. 4 | # 5 | # For more details, see 6 | # http://developer.android.com/guide/developing/tools/proguard.html 7 | 8 | # If your project uses WebView with JS, uncomment the following 9 | # and specify the fully qualified class name to the JavaScript interface 10 | # class: 11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 12 | # public *; 13 | #} 14 | 15 | # Uncomment this to preserve the line number information for 16 | # debugging stack traces. 17 | #-keepattributes SourceFile,LineNumberTable 18 | 19 | # If you keep the line number information, uncomment this to 20 | # hide the original source file name. 21 | #-renamesourcefileattribute SourceFile 22 | -------------------------------------------------------------------------------- /dhcsqlitelibrary/src/androidTest/java/com/ellen/dhcsqlitelibrary/ExampleInstrumentedTest.java: -------------------------------------------------------------------------------- 1 | package com.ellen.dhcsqlitelibrary; 2 | 3 | import android.content.Context; 4 | 5 | import androidx.test.platform.app.InstrumentationRegistry; 6 | import androidx.test.ext.junit.runners.AndroidJUnit4; 7 | 8 | import org.junit.Test; 9 | import org.junit.runner.RunWith; 10 | 11 | import static org.junit.Assert.*; 12 | 13 | /** 14 | * Instrumented test, which will execute on an Android device. 15 | * 16 | * @see Testing documentation 17 | */ 18 | @RunWith(AndroidJUnit4.class) 19 | public class ExampleInstrumentedTest { 20 | @Test 21 | public void useAppContext() { 22 | // Context of the app under test. 23 | Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext(); 24 | 25 | assertEquals("com.ellen.dhcsqlitelibrary.test", appContext.getPackageName()); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /dhcsqlitelibrary/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 3 | -------------------------------------------------------------------------------- /dhcsqlitelibrary/src/main/java/com/ellen/dhcsqlitelibrary/table/annotation/auto/Delete.java: -------------------------------------------------------------------------------- 1 | package com.ellen.dhcsqlitelibrary.table.annotation.auto; 2 | 3 | import java.lang.annotation.ElementType; 4 | import java.lang.annotation.Retention; 5 | import java.lang.annotation.RetentionPolicy; 6 | import java.lang.annotation.Target; 7 | 8 | /** 9 | * 10 | * 用于删除 11 | * 使用者可以自定义查找方法封装 12 | */ 13 | @Target(ElementType.METHOD) 14 | @Retention(RetentionPolicy.RUNTIME) 15 | public @interface Delete { 16 | 17 | String value(); 18 | } 19 | -------------------------------------------------------------------------------- /dhcsqlitelibrary/src/main/java/com/ellen/dhcsqlitelibrary/table/annotation/auto/Search.java: -------------------------------------------------------------------------------- 1 | package com.ellen.dhcsqlitelibrary.table.annotation.auto; 2 | 3 | import java.lang.annotation.ElementType; 4 | import java.lang.annotation.Retention; 5 | import java.lang.annotation.RetentionPolicy; 6 | import java.lang.annotation.Target; 7 | 8 | /** 9 | * 10 | * 用于查找的 11 | * 使用者可以自定义查找方法封装 12 | */ 13 | @Target(ElementType.METHOD) 14 | @Retention(RetentionPolicy.RUNTIME) 15 | public @interface Search { 16 | 17 | String whereSql(); 18 | String orderSql() default ""; 19 | } -------------------------------------------------------------------------------- /dhcsqlitelibrary/src/main/java/com/ellen/dhcsqlitelibrary/table/annotation/auto/SearchByMajorKey.java: -------------------------------------------------------------------------------- 1 | package com.ellen.dhcsqlitelibrary.table.annotation.auto; 2 | 3 | import java.lang.annotation.ElementType; 4 | import java.lang.annotation.Retention; 5 | import java.lang.annotation.RetentionPolicy; 6 | import java.lang.annotation.Target; 7 | 8 | /** 9 | * 10 | * 用于查找的 11 | * 使用者可以自定义查找方法封装 12 | */ 13 | @Target(ElementType.METHOD) 14 | @Retention(RetentionPolicy.RUNTIME) 15 | public @interface SearchByMajorKey { 16 | String whereSql(); 17 | String orderSql() default ""; 18 | } -------------------------------------------------------------------------------- /dhcsqlitelibrary/src/main/java/com/ellen/dhcsqlitelibrary/table/annotation/auto/TotalSql.java: -------------------------------------------------------------------------------- 1 | package com.ellen.dhcsqlitelibrary.table.annotation.auto; 2 | 3 | import java.lang.annotation.ElementType; 4 | import java.lang.annotation.Retention; 5 | import java.lang.annotation.RetentionPolicy; 6 | import java.lang.annotation.Target; 7 | 8 | /** 9 | * 此类能完成所有sql语句 10 | * 多复杂的都可以 11 | */ 12 | @Target(ElementType.METHOD) 13 | @Retention(RetentionPolicy.RUNTIME) 14 | public @interface TotalSql { 15 | String sql(); 16 | boolean isReturnValue() default false; 17 | } 18 | -------------------------------------------------------------------------------- /dhcsqlitelibrary/src/main/java/com/ellen/dhcsqlitelibrary/table/annotation/auto/Update.java: -------------------------------------------------------------------------------- 1 | package com.ellen.dhcsqlitelibrary.table.annotation.auto; 2 | 3 | import java.lang.annotation.ElementType; 4 | import java.lang.annotation.Retention; 5 | import java.lang.annotation.RetentionPolicy; 6 | import java.lang.annotation.Target; 7 | 8 | /** 9 | * 用于更新数据 10 | */ 11 | @Target(ElementType.METHOD) 12 | @Retention(RetentionPolicy.RUNTIME) 13 | public @interface Update { 14 | String valueSql(); 15 | String whereSql(); 16 | } 17 | -------------------------------------------------------------------------------- /dhcsqlitelibrary/src/main/java/com/ellen/dhcsqlitelibrary/table/annotation/auto/Value.java: -------------------------------------------------------------------------------- 1 | package com.ellen.dhcsqlitelibrary.table.annotation.auto; 2 | 3 | import java.lang.annotation.ElementType; 4 | import java.lang.annotation.Retention; 5 | import java.lang.annotation.RetentionPolicy; 6 | import java.lang.annotation.Target; 7 | 8 | @Target(ElementType.PARAMETER) 9 | @Retention(RetentionPolicy.RUNTIME) 10 | public @interface Value { 11 | String value(); 12 | } 13 | -------------------------------------------------------------------------------- /dhcsqlitelibrary/src/main/java/com/ellen/dhcsqlitelibrary/table/annotation/field/DataStructure.java: -------------------------------------------------------------------------------- 1 | package com.ellen.dhcsqlitelibrary.table.annotation.field; 2 | 3 | import java.lang.annotation.ElementType; 4 | import java.lang.annotation.Retention; 5 | import java.lang.annotation.RetentionPolicy; 6 | import java.lang.annotation.Target; 7 | 8 | /** 9 | * 应对于数据结构类型的数据 10 | * 支持: 11 | * 数组 12 | * List 13 | * Set 14 | * Map 15 | * Stack 16 | */ 17 | @Target(ElementType.FIELD) 18 | @Retention(RetentionPolicy.RUNTIME) 19 | public @interface DataStructure { 20 | 21 | /** 22 | * 操作类型 23 | * 固定值类型 24 | */ 25 | OperateEnum operate() default OperateEnum.JSON; 26 | } -------------------------------------------------------------------------------- /dhcsqlitelibrary/src/main/java/com/ellen/dhcsqlitelibrary/table/annotation/field/DhcSqlFieldName.java: -------------------------------------------------------------------------------- 1 | package com.ellen.dhcsqlitelibrary.table.annotation.field; 2 | 3 | import java.lang.annotation.ElementType; 4 | import java.lang.annotation.Retention; 5 | import java.lang.annotation.RetentionPolicy; 6 | import java.lang.annotation.Target; 7 | 8 | @Target(ElementType.FIELD) 9 | @Retention(RetentionPolicy.RUNTIME) 10 | public @interface DhcSqlFieldName { 11 | String sqlFieldName(); 12 | } 13 | -------------------------------------------------------------------------------- /dhcsqlitelibrary/src/main/java/com/ellen/dhcsqlitelibrary/table/annotation/field/Ignore.java: -------------------------------------------------------------------------------- 1 | package com.ellen.dhcsqlitelibrary.table.annotation.field; 2 | 3 | import java.lang.annotation.ElementType; 4 | import java.lang.annotation.Retention; 5 | import java.lang.annotation.RetentionPolicy; 6 | import java.lang.annotation.Target; 7 | 8 | @Target(ElementType.FIELD) 9 | @Retention(RetentionPolicy.RUNTIME) 10 | public @interface Ignore { 11 | } 12 | -------------------------------------------------------------------------------- /dhcsqlitelibrary/src/main/java/com/ellen/dhcsqlitelibrary/table/annotation/field/Operate.java: -------------------------------------------------------------------------------- 1 | package com.ellen.dhcsqlitelibrary.table.annotation.field; 2 | 3 | import java.lang.annotation.ElementType; 4 | import java.lang.annotation.Retention; 5 | import java.lang.annotation.RetentionPolicy; 6 | import java.lang.annotation.Target; 7 | 8 | @Target(ElementType.FIELD) 9 | @Retention(RetentionPolicy.RUNTIME) 10 | public @interface Operate { 11 | 12 | /** 13 | * 操作类型 14 | * 固定值类型 15 | */ 16 | OperateEnum operate() default OperateEnum.JSON; 17 | 18 | /** 19 | * 存储哪个值 需要指定属性名字 20 | */ 21 | 22 | String valueName() default ""; 23 | 24 | } -------------------------------------------------------------------------------- /dhcsqlitelibrary/src/main/java/com/ellen/dhcsqlitelibrary/table/annotation/field/OperateEnum.java: -------------------------------------------------------------------------------- 1 | package com.ellen.dhcsqlitelibrary.table.annotation.field; 2 | 3 | public enum OperateEnum { 4 | 5 | /** 6 | * 固定值映射模式 7 | */ 8 | VALUE, 9 | /** 10 | * Json化映射模式 11 | */ 12 | JSON; 13 | 14 | } 15 | -------------------------------------------------------------------------------- /dhcsqlitelibrary/src/main/java/com/ellen/dhcsqlitelibrary/table/annotation/field/SqlType.java: -------------------------------------------------------------------------------- 1 | package com.ellen.dhcsqlitelibrary.table.annotation.field; 2 | 3 | import com.ellen.sqlitecreate.createsql.helper.SQLFieldTypeEnum; 4 | 5 | import java.lang.annotation.ElementType; 6 | import java.lang.annotation.Retention; 7 | import java.lang.annotation.RetentionPolicy; 8 | import java.lang.annotation.Target; 9 | 10 | @Deprecated 11 | @Target(ElementType.FIELD) 12 | @Retention(RetentionPolicy.RUNTIME) 13 | public @interface SqlType { 14 | 15 | /** 16 | * 数据类型 17 | * @return 18 | */ 19 | SQLFieldTypeEnum sqlFiledType(); 20 | 21 | /** 22 | * 数据长度 23 | * <= 0 无固定长度 24 | * > 0 设置固定长度 25 | * @return 26 | */ 27 | int length() default -1; 28 | } -------------------------------------------------------------------------------- /dhcsqlitelibrary/src/main/java/com/ellen/dhcsqlitelibrary/table/annotation/field/bound/Check.java: -------------------------------------------------------------------------------- 1 | package com.ellen.dhcsqlitelibrary.table.annotation.field.bound; 2 | 3 | import java.lang.annotation.ElementType; 4 | import java.lang.annotation.Retention; 5 | import java.lang.annotation.RetentionPolicy; 6 | import java.lang.annotation.Target; 7 | 8 | @Target(ElementType.FIELD) 9 | @Retention(RetentionPolicy.RUNTIME) 10 | public @interface Check { 11 | String value(); 12 | } 13 | -------------------------------------------------------------------------------- /dhcsqlitelibrary/src/main/java/com/ellen/dhcsqlitelibrary/table/annotation/field/bound/CreateEndString.java: -------------------------------------------------------------------------------- 1 | package com.ellen.dhcsqlitelibrary.table.annotation.field.bound; 2 | 3 | import java.lang.annotation.ElementType; 4 | import java.lang.annotation.Retention; 5 | import java.lang.annotation.RetentionPolicy; 6 | import java.lang.annotation.Target; 7 | 8 | /** 9 | * 用于给整张表加约束的 10 | */ 11 | @Target(ElementType.TYPE) 12 | @Retention(RetentionPolicy.RUNTIME) 13 | public @interface CreateEndString { 14 | String value(); 15 | } 16 | -------------------------------------------------------------------------------- /dhcsqlitelibrary/src/main/java/com/ellen/dhcsqlitelibrary/table/annotation/field/bound/Default.java: -------------------------------------------------------------------------------- 1 | package com.ellen.dhcsqlitelibrary.table.annotation.field.bound; 2 | 3 | import java.lang.annotation.ElementType; 4 | import java.lang.annotation.Retention; 5 | import java.lang.annotation.RetentionPolicy; 6 | import java.lang.annotation.Target; 7 | 8 | @Target(ElementType.FIELD) 9 | @Retention(RetentionPolicy.RUNTIME) 10 | public @interface Default { 11 | DefaultValueEnum defaultValueEnum(); 12 | byte byteValue() default 0; 13 | short shortValue() default 0; 14 | int intValue() default 0; 15 | long longValue() default 0; 16 | char charValue() default 0; 17 | String strValue() default ""; 18 | float floatValue() default 0; 19 | double doubleValue() default 0; 20 | 21 | } 22 | -------------------------------------------------------------------------------- /dhcsqlitelibrary/src/main/java/com/ellen/dhcsqlitelibrary/table/annotation/field/bound/DefaultValueEnum.java: -------------------------------------------------------------------------------- 1 | package com.ellen.dhcsqlitelibrary.table.annotation.field.bound; 2 | 3 | public enum DefaultValueEnum { 4 | 5 | BYTE,SHORT,INT,LONG, 6 | FLOAT,DOUBLE, 7 | CHAR,STRING; 8 | } 9 | -------------------------------------------------------------------------------- /dhcsqlitelibrary/src/main/java/com/ellen/dhcsqlitelibrary/table/annotation/field/bound/EndAutoString.java: -------------------------------------------------------------------------------- 1 | package com.ellen.dhcsqlitelibrary.table.annotation.field.bound; 2 | 3 | import java.lang.annotation.ElementType; 4 | import java.lang.annotation.Retention; 5 | import java.lang.annotation.RetentionPolicy; 6 | import java.lang.annotation.Target; 7 | 8 | /** 9 | * 自定义尾部 10 | */ 11 | @Target(ElementType.FIELD) 12 | @Retention(RetentionPolicy.RUNTIME) 13 | public @interface EndAutoString { 14 | String value(); 15 | } 16 | -------------------------------------------------------------------------------- /dhcsqlitelibrary/src/main/java/com/ellen/dhcsqlitelibrary/table/annotation/field/bound/MajorKey.java: -------------------------------------------------------------------------------- 1 | package com.ellen.dhcsqlitelibrary.table.annotation.field.bound; 2 | 3 | import java.lang.annotation.ElementType; 4 | import java.lang.annotation.Retention; 5 | import java.lang.annotation.RetentionPolicy; 6 | import java.lang.annotation.Target; 7 | 8 | @Target(ElementType.FIELD) 9 | @Retention(RetentionPolicy.RUNTIME) 10 | public @interface MajorKey{ 11 | boolean isAutoIncrement() default false; 12 | } 13 | -------------------------------------------------------------------------------- /dhcsqlitelibrary/src/main/java/com/ellen/dhcsqlitelibrary/table/annotation/field/bound/NotNull.java: -------------------------------------------------------------------------------- 1 | package com.ellen.dhcsqlitelibrary.table.annotation.field.bound; 2 | 3 | import java.lang.annotation.ElementType; 4 | import java.lang.annotation.Retention; 5 | import java.lang.annotation.RetentionPolicy; 6 | import java.lang.annotation.Target; 7 | 8 | @Target(ElementType.FIELD) 9 | @Retention(RetentionPolicy.RUNTIME) 10 | public @interface NotNull{ 11 | } 12 | -------------------------------------------------------------------------------- /dhcsqlitelibrary/src/main/java/com/ellen/dhcsqlitelibrary/table/annotation/field/bound/Unique.java: -------------------------------------------------------------------------------- 1 | package com.ellen.dhcsqlitelibrary.table.annotation.field.bound; 2 | 3 | import java.lang.annotation.ElementType; 4 | import java.lang.annotation.Retention; 5 | import java.lang.annotation.RetentionPolicy; 6 | import java.lang.annotation.Target; 7 | 8 | /** 9 | * 确保某列值是不同的 10 | */ 11 | @Target(ElementType.FIELD) 12 | @Retention(RetentionPolicy.RUNTIME) 13 | public @interface Unique { 14 | 15 | } 16 | -------------------------------------------------------------------------------- /dhcsqlitelibrary/src/main/java/com/ellen/dhcsqlitelibrary/table/annotation/table/DataClass.java: -------------------------------------------------------------------------------- 1 | package com.ellen.dhcsqlitelibrary.table.annotation.table; 2 | 3 | import java.lang.annotation.ElementType; 4 | import java.lang.annotation.Retention; 5 | import java.lang.annotation.RetentionPolicy; 6 | import java.lang.annotation.Target; 7 | 8 | /** 9 | * 方便 10 | */ 11 | @Target(ElementType.FIELD) 12 | @Retention(RetentionPolicy.RUNTIME) 13 | public @interface DataClass { 14 | Class value(); 15 | } 16 | -------------------------------------------------------------------------------- /dhcsqlitelibrary/src/main/java/com/ellen/dhcsqlitelibrary/table/exception/BoolNoCanSaveException.java: -------------------------------------------------------------------------------- 1 | package com.ellen.dhcsqlitelibrary.table.exception; 2 | 3 | public class BoolNoCanSaveException extends RuntimeException { 4 | 5 | 6 | private String errorMessage; 7 | 8 | public BoolNoCanSaveException(String errorMessage){ 9 | this.errorMessage = errorMessage; 10 | } 11 | 12 | @Override 13 | public String getMessage() { 14 | return errorMessage; 15 | } 16 | 17 | } 18 | -------------------------------------------------------------------------------- /dhcsqlitelibrary/src/main/java/com/ellen/dhcsqlitelibrary/table/exception/JsonNoCanFormatException.java: -------------------------------------------------------------------------------- 1 | package com.ellen.dhcsqlitelibrary.table.exception; 2 | 3 | public class JsonNoCanFormatException extends RuntimeException { 4 | 5 | 6 | private String errorMessage; 7 | 8 | public JsonNoCanFormatException(String errorMessage){ 9 | this.errorMessage = errorMessage; 10 | } 11 | 12 | @Override 13 | public String getMessage() { 14 | return errorMessage; 15 | } 16 | 17 | } 18 | -------------------------------------------------------------------------------- /dhcsqlitelibrary/src/main/java/com/ellen/dhcsqlitelibrary/table/exception/NoCanSaveToSqlException.java: -------------------------------------------------------------------------------- 1 | package com.ellen.dhcsqlitelibrary.table.exception; 2 | 3 | public class NoCanSaveToSqlException extends RuntimeException { 4 | 5 | 6 | private String errorMessage; 7 | 8 | public NoCanSaveToSqlException(String errorMessage){ 9 | this.errorMessage = errorMessage; 10 | } 11 | 12 | @Override 13 | public String getMessage() { 14 | return errorMessage; 15 | } 16 | 17 | } 18 | -------------------------------------------------------------------------------- /dhcsqlitelibrary/src/main/java/com/ellen/dhcsqlitelibrary/table/exception/NoMajorKeyException.java: -------------------------------------------------------------------------------- 1 | package com.ellen.dhcsqlitelibrary.table.exception; 2 | 3 | public class NoMajorKeyException extends RuntimeException { 4 | 5 | private String errorMessage; 6 | 7 | public NoMajorKeyException(String errorMessage){ 8 | this.errorMessage = errorMessage; 9 | } 10 | 11 | @Override 12 | public String getMessage() { 13 | return errorMessage; 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /dhcsqlitelibrary/src/main/java/com/ellen/dhcsqlitelibrary/table/exception/SqlFieldDuplicateException.java: -------------------------------------------------------------------------------- 1 | package com.ellen.dhcsqlitelibrary.table.exception; 2 | 3 | public class SqlFieldDuplicateException extends RuntimeException{ 4 | 5 | private String errorMessage; 6 | 7 | public SqlFieldDuplicateException(String errorMessage){ 8 | this.errorMessage = errorMessage; 9 | } 10 | 11 | @Override 12 | public String getMessage() { 13 | return errorMessage; 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /dhcsqlitelibrary/src/main/java/com/ellen/dhcsqlitelibrary/table/helper/CursorHelper.java: -------------------------------------------------------------------------------- 1 | package com.ellen.dhcsqlitelibrary.table.helper; 2 | 3 | import android.database.Cursor; 4 | import android.util.Log; 5 | 6 | import com.ellen.sqlitecreate.createsql.create.createtable.SQLField; 7 | import com.ellen.sqlitecreate.createsql.helper.SQLFieldTypeEnum; 8 | 9 | import java.lang.reflect.Field; 10 | 11 | public class CursorHelper { 12 | 13 | public Object readValueFromCursor(Cursor cursor, Field field, SQLField sqlField, String sqlDataType){ 14 | Object value = null; 15 | int index = cursor.getColumnIndex(sqlField.getName()); 16 | if(index < 0)return null; 17 | Class type = field.getType(); 18 | if (sqlDataType.equals(SQLFieldTypeEnum.INTEGER.getTypeName())) { 19 | value = cursor.getInt(index); 20 | } else if (sqlDataType.equals(SQLFieldTypeEnum.BIG_INT.getTypeName())) { 21 | value = cursor.getLong(index); 22 | } else if (sqlDataType.equals(SQLFieldTypeEnum.REAL.getTypeName())) { 23 | if (type == Float.class || type.getName().equals("float")) { 24 | value = cursor.getFloat(index); 25 | } else if (type == Double.class || type.getName().equals("double")) { 26 | value = cursor.getDouble(index); 27 | } else { 28 | value = cursor.getDouble(index); 29 | } 30 | } else if(sqlDataType.equals(SQLFieldTypeEnum.MEDIUM_TEXT.getTypeName())){ 31 | if (type == Character.class || type.getName().equals("char")) { 32 | String str = cursor.getString(index); 33 | if (str != null) { 34 | value = cursor.getString(index).charAt(0); 35 | } else { 36 | value = null; 37 | } 38 | } else { 39 | value = cursor.getString(index); 40 | } 41 | } else if(sqlDataType.equals(SQLFieldTypeEnum.LONG_TEXT.getTypeName())){ 42 | if (type == Character.class || type.getName().equals("char")) { 43 | String str = cursor.getString(index); 44 | if (str != null) { 45 | value = cursor.getString(index).charAt(0); 46 | } else { 47 | value = null; 48 | } 49 | } else { 50 | value = cursor.getString(index); 51 | } 52 | } else if (sqlDataType.equals(SQLFieldTypeEnum.TEXT.getTypeName())) { 53 | if (type == Character.class || type.getName().equals("char")) { 54 | String str = cursor.getString(index); 55 | if (str != null) { 56 | value = cursor.getString(index).charAt(0); 57 | } else { 58 | value = null; 59 | } 60 | } else { 61 | value = cursor.getString(index); 62 | } 63 | } else if (sqlDataType.equals(SQLFieldTypeEnum.BLOB.getTypeName())) { 64 | value = cursor.getBlob(index); 65 | } else if (sqlDataType.equals(SQLFieldTypeEnum.DATE.getTypeName())) { 66 | value = cursor.getString(index); 67 | } else if (sqlDataType.equals(SQLFieldTypeEnum.NUMERIC.getTypeName())) { 68 | value = cursor.getString(index); 69 | } 70 | return value; 71 | } 72 | 73 | } 74 | -------------------------------------------------------------------------------- /dhcsqlitelibrary/src/main/java/com/ellen/dhcsqlitelibrary/table/helper/ReflectHelper.java: -------------------------------------------------------------------------------- 1 | package com.ellen.dhcsqlitelibrary.table.helper; 2 | 3 | import com.ellen.dhcsqlitelibrary.table.annotation.field.Ignore; 4 | import com.ellen.dhcsqlitelibrary.table.annotation.field.bound.Default; 5 | import com.ellen.sqlitecreate.createsql.helper.SQLFieldTypeEnum; 6 | 7 | import java.lang.reflect.Constructor; 8 | import java.lang.reflect.Field; 9 | import java.lang.reflect.InvocationTargetException; 10 | import java.lang.reflect.Modifier; 11 | import java.util.ArrayList; 12 | import java.util.List; 13 | 14 | public class ReflectHelper { 15 | 16 | public List getClassFieldList(Class dataClass, boolean isAddStatic) { 17 | List fieldList = new ArrayList<>(); 18 | Field[] fields = dataClass.getDeclaredFields(); 19 | if (fields != null && fields.length != 0) { 20 | for (int i = 0; i < fields.length; i++) { 21 | Field field = fields[i]; 22 | field.setAccessible(true); 23 | //过滤掉静态的字段 24 | boolean isStatic = Modifier.isStatic(field.getModifiers()); 25 | if (!isStatic) { 26 | Ignore ignore = field.getAnnotation(Ignore.class); 27 | if (ignore == null) { 28 | fieldList.add(field); 29 | } 30 | } else { 31 | if (isAddStatic) { 32 | Ignore ignore = field.getAnnotation(Ignore.class); 33 | if (ignore == null) { 34 | fieldList.add(field); 35 | } 36 | } 37 | } 38 | } 39 | } 40 | return fieldList; 41 | } 42 | 43 | public Object[] getValueArray(Object value) { 44 | if(value == null)return null; 45 | String name = value.getClass().getSimpleName(); 46 | Object[] objectArray = null; 47 | if(name.equals("byte[]")){ 48 | byte[] byteArray = (byte[]) value; 49 | objectArray = new Byte[byteArray.length]; 50 | for (int i = 0; i < byteArray.length; i++) { 51 | objectArray[i] = byteArray[i]; 52 | } 53 | }else if(name.equals("short[]")){ 54 | short[] shortArray = (short[]) value; 55 | objectArray = new Short[shortArray.length]; 56 | for (int i = 0; i < shortArray.length; i++) { 57 | objectArray[i] = shortArray[i]; 58 | } 59 | } else if (name.equals("int[]")) { 60 | int[] intArray = (int[]) value; 61 | objectArray = new Integer[intArray.length]; 62 | for (int i = 0; i < intArray.length; i++) { 63 | objectArray[i] = intArray[i]; 64 | } 65 | }else if(name.equals("long[]")){ 66 | long[] longArray = (long[]) value; 67 | objectArray = new Long[longArray.length]; 68 | for (int i = 0; i < longArray.length; i++) { 69 | objectArray[i] = longArray[i]; 70 | } 71 | }else if(name.equals("boolean[]")){ 72 | boolean[] booleanArray = (boolean[])value; 73 | objectArray = new Boolean[booleanArray.length]; 74 | for (int i = 0; i < booleanArray.length; i++) { 75 | objectArray[i] = booleanArray[i]; 76 | } 77 | }else if(name.equals("float[]")){ 78 | float[] floatArray = (float[]) value; 79 | objectArray = new Float[floatArray.length]; 80 | for (int i = 0; i < floatArray.length; i++) { 81 | objectArray[i] = floatArray[i]; 82 | } 83 | }else if(name.equals("double[]")){ 84 | double[] doubleArray = (double[]) value; 85 | objectArray = new Double[doubleArray.length]; 86 | for (int i = 0; i < doubleArray.length; i++) { 87 | objectArray[i] = doubleArray[i]; 88 | } 89 | }else if(name.equals("char[]")){ 90 | char[] charArray = (char[]) value; 91 | objectArray = new Character[charArray.length]; 92 | for (int i = 0; i < charArray.length; i++) { 93 | objectArray[i] = charArray[i]; 94 | } 95 | }else { 96 | objectArray = (Object[]) value; 97 | } 98 | return objectArray; 99 | } 100 | 101 | public Object getValue(Object obj, Field targetField) { 102 | targetField.setAccessible(true); 103 | Object value = null; 104 | try { 105 | value = targetField.get(obj); 106 | } catch (IllegalAccessException e) { 107 | e.printStackTrace(); 108 | } 109 | return value; 110 | } 111 | 112 | public boolean isBooleanType(Field field){ 113 | return field.getType() == Boolean.class || field.getType().getName().equals("boolean"); 114 | } 115 | 116 | public boolean isBasicType(Field field) { 117 | boolean b = false; 118 | if (field.getType() == Byte.class || field.getType().getName().equals("byte")) { 119 | b = true; 120 | } else if (field.getType() == Short.class || field.getType().getName().equals("short")) { 121 | b = true; 122 | } else if (field.getType() == Integer.class || field.getType().getName().equals("int")) { 123 | b = true; 124 | } else if (field.getType() == Long.class || field.getType().getName().equals("long")) { 125 | b = true; 126 | } else if (field.getType() == Float.class || field.getType().getName().equals("float")) { 127 | b = true; 128 | } else if (field.getType() == Double.class || field.getType().getName().equals("double")) { 129 | b = true; 130 | } else if (field.getType() == Boolean.class || field.getType().getName().equals("boolean")) { 131 | b = true; 132 | } else if (field.getType() == Character.class || field.getType().getName().equals("char")) { 133 | b = true; 134 | } else if (field.getType() == String.class) { 135 | b = true; 136 | } 137 | return b; 138 | } 139 | 140 | /** 141 | * 根据Value值判断它的类型 142 | * @param value 143 | * @return 144 | */ 145 | public boolean isBasicType(Object value) { 146 | boolean b; 147 | Class classType = value.getClass(); 148 | if (classType == Byte.class) { 149 | b = true; 150 | } else if (classType == Short.class) { 151 | b = true; 152 | } else if (classType == Integer.class) { 153 | b = true; 154 | } else if (classType == Long.class) { 155 | b = true; 156 | } else if (classType == Float.class) { 157 | b = true; 158 | } else if (classType == Double.class) { 159 | b = true; 160 | } else if (classType == Boolean.class) { 161 | b = true; 162 | } else if (classType == Character.class) { 163 | b = true; 164 | } else if (classType == String.class) { 165 | b = true; 166 | } else { 167 | b = false; 168 | } 169 | return b; 170 | } 171 | 172 | public Object getDefaultValue(Class classType) { 173 | if (classType == Byte.class || classType.getName().equals("byte")) { 174 | return 0; 175 | } else if (classType == Short.class || classType.getName().equals("short")) { 176 | return 0; 177 | } else if (classType == Integer.class || classType.getName().equals("int")) { 178 | return 0; 179 | } else if (classType == Long.class || classType.getName().equals("long")) { 180 | return 0; 181 | } else if (classType == Float.class || classType.getName().equals("float")) { 182 | return 0; 183 | } else if (classType == Double.class || classType.getName().equals("double")) { 184 | return 0; 185 | } else if (classType == Boolean.class || classType.getName().equals("boolean")) { 186 | return false; 187 | } else if (classType == Character.class || classType.getName().equals("char")) { 188 | return '0'; 189 | } else if (classType == String.class) { 190 | return null; 191 | } else { 192 | return null; 193 | } 194 | } 195 | 196 | public SQLFieldTypeEnum getSqlStringType(Class type) { 197 | SQLFieldTypeEnum sqlType = null; 198 | if (type == Byte.class || type.getName().equals("byte")) { 199 | sqlType = SQLFieldTypeEnum.INTEGER; 200 | } else if (type == Short.class || type.getName().equals("short")) { 201 | sqlType = SQLFieldTypeEnum.INTEGER; 202 | } else if (type == Integer.class || type.getName().equals("int")) { 203 | sqlType = SQLFieldTypeEnum.INTEGER; 204 | } else if (type == Long.class || type.getName().equals("long")) { 205 | sqlType = SQLFieldTypeEnum.BIG_INT; 206 | } else if (type == Float.class || type.getName().equals("float")) { 207 | sqlType = SQLFieldTypeEnum.REAL; 208 | } else if (type == Double.class || type.getName().equals("double")) { 209 | sqlType = SQLFieldTypeEnum.REAL; 210 | } else if (type == Boolean.class || type.getName().equals("boolean")) { 211 | sqlType = SQLFieldTypeEnum.INTEGER; 212 | } else if (type == Character.class || type.getName().equals("char")) { 213 | sqlType = SQLFieldTypeEnum.TEXT; 214 | } else if (type == String.class) { 215 | sqlType = SQLFieldTypeEnum.TEXT; 216 | } else { 217 | sqlType = SQLFieldTypeEnum.TEXT; 218 | } 219 | return sqlType; 220 | } 221 | 222 | public Object getDefaultValue(Default d){ 223 | Object obj = null; 224 | switch (d.defaultValueEnum()){ 225 | case BYTE: 226 | obj = d.byteValue(); 227 | break; 228 | case SHORT: 229 | obj = d.shortValue(); 230 | break; 231 | case INT: 232 | obj = d.intValue(); 233 | break; 234 | case LONG: 235 | obj = d.longValue(); 236 | break; 237 | case FLOAT: 238 | obj = d.floatValue(); 239 | break; 240 | case DOUBLE: 241 | obj = d.doubleValue(); 242 | break; 243 | case CHAR: 244 | obj = d.charValue(); 245 | break; 246 | case STRING: 247 | obj = d.strValue(); 248 | break; 249 | } 250 | return obj; 251 | } 252 | 253 | public E getT(Class dataClass) throws IllegalAccessException, InvocationTargetException, InstantiationException { 254 | Constructor[] constructors = dataClass.getDeclaredConstructors(); 255 | Constructor constructor = constructors[0]; 256 | constructor.setAccessible(true); 257 | Class[] classArray = constructor.getParameterTypes(); 258 | if (classArray != null && classArray.length > 0) { 259 | Object[] objects = new Object[classArray.length]; 260 | for (int i = 0; i < classArray.length; i++) { 261 | Object value = getDefaultValue(classArray[i]); 262 | objects[i] = value; 263 | } 264 | return (E) constructor.newInstance(objects); 265 | } else { 266 | return (E) constructor.newInstance(); 267 | } 268 | } 269 | 270 | } 271 | -------------------------------------------------------------------------------- /dhcsqlitelibrary/src/main/java/com/ellen/dhcsqlitelibrary/table/helper/json/FastJxFormat.java: -------------------------------------------------------------------------------- 1 | package com.ellen.dhcsqlitelibrary.table.helper.json; 2 | 3 | import java.lang.reflect.InvocationTargetException; 4 | import java.lang.reflect.Method; 5 | 6 | class FastJxFormat implements JxFormat { 7 | 8 | private Class fastJsonClass; 9 | 10 | public FastJxFormat(Class fastJsonClass) { 11 | this.fastJsonClass = fastJsonClass; 12 | } 13 | 14 | @Override 15 | public String toJxString(Object obj) { 16 | String json = null; 17 | try { 18 | Method toJson = fastJsonClass.getMethod("toJSONString", Object.class); 19 | toJson.setAccessible(true); 20 | json = (String) toJson.invoke(null,obj); 21 | } catch (NoSuchMethodException e) { 22 | e.printStackTrace(); 23 | } catch (IllegalAccessException e) { 24 | e.printStackTrace(); 25 | } catch (InvocationTargetException e) { 26 | e.printStackTrace(); 27 | } 28 | return json; 29 | } 30 | 31 | @Override 32 | public E toObject(String json, Class jsonClass) { 33 | Object object = null; 34 | try { 35 | Method m = fastJsonClass.getMethod("parseObject", String.class,Class.class); 36 | object = m.invoke(null,json,jsonClass); 37 | } catch (NoSuchMethodException e) { 38 | e.printStackTrace(); 39 | } catch (IllegalAccessException e) { 40 | e.printStackTrace(); 41 | } catch (InvocationTargetException e) { 42 | e.printStackTrace(); 43 | } 44 | return (E) object; 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /dhcsqlitelibrary/src/main/java/com/ellen/dhcsqlitelibrary/table/helper/json/GsonFormat.java: -------------------------------------------------------------------------------- 1 | package com.ellen.dhcsqlitelibrary.table.helper.json; 2 | 3 | import java.lang.reflect.InvocationTargetException; 4 | import java.lang.reflect.Method; 5 | 6 | class GsonFormat implements JxFormat { 7 | 8 | private Object gsonObject; 9 | 10 | GsonFormat(Object gsonObject) { 11 | this.gsonObject = gsonObject; 12 | } 13 | 14 | @Override 15 | public String toJxString(Object obj) { 16 | Class gsonClass = gsonObject.getClass(); 17 | Method toJson = null; 18 | String json = null; 19 | try { 20 | toJson = gsonClass.getDeclaredMethod("toJson",Object.class); 21 | toJson.setAccessible(true); 22 | json = (String) toJson.invoke(gsonObject,obj); 23 | } catch (NoSuchMethodException e) { 24 | e.printStackTrace(); 25 | } catch (IllegalAccessException e) { 26 | e.printStackTrace(); 27 | } catch (InvocationTargetException e) { 28 | e.printStackTrace(); 29 | } 30 | return json; 31 | } 32 | 33 | @Override 34 | public E toObject(String json, Class jsonClass) { 35 | Class gsonClass = gsonObject.getClass(); 36 | Method toObject = null; 37 | Object obj = null; 38 | try { 39 | toObject = gsonClass.getDeclaredMethod("fromJson",String.class,Class.class); 40 | toObject.setAccessible(true); 41 | obj = toObject.invoke(gsonObject,json,jsonClass); 42 | } catch (NoSuchMethodException e) { 43 | e.printStackTrace(); 44 | } catch (IllegalAccessException e) { 45 | e.printStackTrace(); 46 | } catch (InvocationTargetException e) { 47 | e.printStackTrace(); 48 | } 49 | return (E) obj; 50 | } 51 | 52 | } 53 | -------------------------------------------------------------------------------- /dhcsqlitelibrary/src/main/java/com/ellen/dhcsqlitelibrary/table/helper/json/JsonLibraryType.java: -------------------------------------------------------------------------------- 1 | package com.ellen.dhcsqlitelibrary.table.helper.json; 2 | 3 | public enum JsonLibraryType { 4 | 5 | GSON, 6 | FAST_JSON; 7 | 8 | } 9 | -------------------------------------------------------------------------------- /dhcsqlitelibrary/src/main/java/com/ellen/dhcsqlitelibrary/table/helper/json/JxFormat.java: -------------------------------------------------------------------------------- 1 | package com.ellen.dhcsqlitelibrary.table.helper.json; 2 | 3 | /** 4 | * 解析器 5 | * Json 6 | * xml 7 | * 其它格式都行 8 | */ 9 | public interface JxFormat { 10 | 11 | String toJxString(Object obj); 12 | E toObject(String json,Class jsonClass); 13 | } 14 | -------------------------------------------------------------------------------- /dhcsqlitelibrary/src/main/java/com/ellen/dhcsqlitelibrary/table/helper/json/JxHelper.java: -------------------------------------------------------------------------------- 1 | package com.ellen.dhcsqlitelibrary.table.helper.json; 2 | 3 | import com.ellen.dhcsqlitelibrary.table.exception.JsonNoCanFormatException; 4 | import java.lang.reflect.Constructor; 5 | import java.lang.reflect.InvocationTargetException; 6 | 7 | public class JxHelper implements JxFormat { 8 | 9 | private JxFormat jxFormat; 10 | 11 | public JxHelper(JsonLibraryType jsonLibraryType) { 12 | Class gsonClass = null; 13 | Class fastJsonClass = null; 14 | try { 15 | if (jsonLibraryType == JsonLibraryType.GSON) { 16 | //使用Gson 17 | gsonClass = Class.forName("com.google.gson.Gson"); 18 | } else if (jsonLibraryType == JsonLibraryType.FAST_JSON) { 19 | //使用FastJson 20 | fastJsonClass = Class.forName("com.alibaba.fastjson.JSONObject"); 21 | } 22 | } catch (ClassNotFoundException e) { 23 | e.printStackTrace(); 24 | gsonClass = null; 25 | fastJsonClass = null; 26 | } 27 | 28 | if (gsonClass == null && fastJsonClass == null) { 29 | //报异常 30 | throw new JsonNoCanFormatException("无法进行json映射,无可用的Json解析器"); 31 | } else { 32 | try { 33 | if (gsonClass != null) { 34 | jxFormat = new GsonFormat(getT(gsonClass)); 35 | } else if (fastJsonClass != null) { 36 | jxFormat = new FastJxFormat(fastJsonClass); 37 | } 38 | } catch (IllegalAccessException e) { 39 | e.printStackTrace(); 40 | } catch (InvocationTargetException e) { 41 | e.printStackTrace(); 42 | } catch (InstantiationException e) { 43 | e.printStackTrace(); 44 | } 45 | } 46 | } 47 | 48 | private T getT(Class dataClass) throws IllegalAccessException, InvocationTargetException, InstantiationException { 49 | Constructor[] constructors = dataClass.getDeclaredConstructors(); 50 | Constructor targetConstructor = null; 51 | for (Constructor constructor : constructors) { 52 | constructor.setAccessible(true); 53 | Class[] classArray = constructor.getParameterTypes(); 54 | if (classArray != null && classArray.length == 0) { 55 | targetConstructor = constructor; 56 | break; 57 | } 58 | } 59 | return (T) targetConstructor.newInstance(); 60 | } 61 | 62 | @Override 63 | public String toJxString(Object obj) { 64 | return jxFormat.toJxString(obj); 65 | } 66 | 67 | @Override 68 | public E toObject(String json, Class jsonClass) { 69 | return jxFormat.toObject(json, jsonClass); 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /dhcsqlitelibrary/src/main/java/com/ellen/dhcsqlitelibrary/table/impl/BaseZxyLibrary.java: -------------------------------------------------------------------------------- 1 | package com.ellen.dhcsqlitelibrary.table.impl; 2 | 3 | public interface BaseZxyLibrary { 4 | 5 | /** 6 | * 重命名表 7 | * @param oldTableName 旧的名字 8 | * @param newTableName 新的名字 9 | */ 10 | void reNameTable(String oldTableName,String newTableName); 11 | 12 | /** 13 | * 清空表tableName的数据 14 | * @param tableName 15 | */ 16 | void clearTable(String tableName); 17 | 18 | /** 19 | * 是否存在tableName的表 20 | * @param tableName 21 | * @return 22 | */ 23 | boolean isExist(String tableName); 24 | 25 | /** 26 | * 将整个库里的表全部删除 27 | */ 28 | void clearLibrary(); 29 | 30 | /** 31 | * 获取所有的库中所有表名 32 | * @return 33 | */ 34 | String[] getAllTableName(); 35 | 36 | /** 37 | * 获取表的个数 38 | * @return 39 | */ 40 | int getTableCount(); 41 | } 42 | -------------------------------------------------------------------------------- /dhcsqlitelibrary/src/main/java/com/ellen/dhcsqlitelibrary/table/impl/BaseZxyTable.java: -------------------------------------------------------------------------------- 1 | package com.ellen.dhcsqlitelibrary.table.impl; 2 | 3 | import android.database.Cursor; 4 | import android.database.sqlite.SQLiteDatabase; 5 | 6 | import com.ellen.dhcsqlitelibrary.table.proxy.AutoDesignOperate; 7 | import com.ellen.sqlitecreate.createsql.add.AddManyRowToTable; 8 | import com.ellen.sqlitecreate.createsql.add.AddSingleRowToTable; 9 | import com.ellen.sqlitecreate.createsql.add.AddTableColumn; 10 | import com.ellen.sqlitecreate.createsql.create.createtable.CreateTable; 11 | import com.ellen.sqlitecreate.createsql.delete.DeleteTable; 12 | import com.ellen.sqlitecreate.createsql.delete.DeleteTableDataRow; 13 | import com.ellen.sqlitecreate.createsql.order.Order; 14 | import com.ellen.sqlitecreate.createsql.serach.SerachTableData; 15 | import com.ellen.sqlitecreate.createsql.serach.SerachTableExist; 16 | import com.ellen.sqlitecreate.createsql.update.UpdateTableDataRow; 17 | import com.ellen.sqlitecreate.createsql.update.UpdateTableName; 18 | import com.ellen.sqlitecreate.createsql.where.Between; 19 | import com.ellen.sqlitecreate.createsql.where.Where; 20 | import com.ellen.sqlitecreate.createsql.where.WhereIn; 21 | 22 | class BaseZxyTable { 23 | 24 | protected SQLiteDatabase db; 25 | protected Class dataClass; 26 | protected Class autoClass; 27 | private String tableName; 28 | 29 | public BaseZxyTable(SQLiteDatabase db, Class dataClass, Class autoClass, String tableName) { 30 | this.db = db; 31 | this.dataClass = dataClass; 32 | this.autoClass = autoClass; 33 | this.tableName = tableName; 34 | } 35 | 36 | SQLiteDatabase getSQLiteDatabase(){ 37 | return db; 38 | } 39 | 40 | public void exeSQL(String sql){ 41 | getSQLiteDatabase().execSQL(sql); 42 | } 43 | 44 | Cursor searchBySQL(String sql){ 45 | return getSQLiteDatabase().rawQuery(sql,null); 46 | } 47 | 48 | /** 49 | * 创建表的SQL语句生产类 50 | */ 51 | public CreateTable getCreateTable(){ 52 | return CreateTable.getInstance(); 53 | } 54 | 55 | /** 56 | * 删除表SQL语句生产类 57 | * @return 58 | */ 59 | public DeleteTable getDeleteTable() { 60 | return DeleteTable.getInstance(); 61 | } 62 | /** 63 | * 动态添加表的列的SQL语句生产类 64 | * @return 65 | */ 66 | public AddTableColumn getAddTableColumn() { 67 | return AddTableColumn.getInstance(); 68 | } 69 | /** 70 | * 修改表的名字的SQL语句生产类 71 | * @return 72 | */ 73 | public UpdateTableName getUpdateTableName() { 74 | return UpdateTableName.getInstance(); 75 | } 76 | /** 77 | * 查询表是否存在的SQL语句生产类 78 | */ 79 | public SerachTableExist getSerachTableExist() { 80 | return SerachTableExist.getInstance(); 81 | } 82 | /** 83 | * 增加数据(单条)的SQL语句生产类 84 | */ 85 | public AddSingleRowToTable getAddSingleRowToTable() { 86 | return AddSingleRowToTable.getInstance(); 87 | } 88 | /** 89 | * 增加数据(多条)的SQL语句生产类 90 | * example: 91 | * INSERT INTO student (id,name,sex) VALUES (3,'李三','男'),(4,'王五','女'); 92 | */ 93 | public AddManyRowToTable getAddManyRowToTable() { 94 | return AddManyRowToTable.getInstance(); 95 | } 96 | /** 97 | * 删除数据的SQL语句生产类 98 | */ 99 | public DeleteTableDataRow getDeleteTableDataRow() { 100 | return DeleteTableDataRow.getInstance(); 101 | } 102 | /** 103 | * 修改数据的SQL语句生产类 104 | */ 105 | public UpdateTableDataRow getUpdateTableDataRow() { 106 | return UpdateTableDataRow.getInstance(); 107 | } 108 | /** 109 | * 查询数据的SQL语句生产类 110 | */ 111 | public SerachTableData getSearchTableData() { 112 | return SerachTableData.getInstance(); 113 | } 114 | /** 115 | * 普通where生产 116 | * example: 117 | * WHERE id = 5 AND name = 'Ellen' 118 | * @param isContainsWhere 生产出的Between是否包含Where 119 | * 120 | */ 121 | public Where getWhere(boolean isContainsWhere) { 122 | return Where.getInstance(isContainsWhere); 123 | } 124 | 125 | /** 126 | * WhereIn生产的SQL语句生产类 127 | * example: 128 | * WHERE name IN ('李一','王二','张三') 129 | * @param isContainsWhere 生产出的Between是否包含Where 130 | * @return 131 | */ 132 | public WhereIn getWhereIn(boolean isContainsWhere) { 133 | return WhereIn.getInstance(isContainsWhere); 134 | } 135 | 136 | /** 137 | * Between语句生产类 138 | * example: 139 | * BETWEEN 3 AND 8 140 | * @param isContainsWhere 生产出的Between是否包含Where 141 | * @return 142 | */ 143 | public Between getBetween(boolean isContainsWhere) { 144 | return Between.getInstance(isContainsWhere); 145 | } 146 | 147 | /** 148 | * 排序Order生成的SQL语句生产类 149 | * example1: 150 | * ORDER BY id ASC 151 | * example2: 152 | * ORDER BY id,name ASC 153 | * @param isContainsOrderBy 生产出的Order是否包含ORDER BY 154 | * @return 155 | */ 156 | public Order getOrder(boolean isContainsOrderBy) { 157 | return Order.getInstance(isContainsOrderBy); 158 | } 159 | } 160 | -------------------------------------------------------------------------------- /dhcsqlitelibrary/src/main/java/com/ellen/dhcsqlitelibrary/table/impl/CommonSetting.java: -------------------------------------------------------------------------------- 1 | package com.ellen.dhcsqlitelibrary.table.impl; 2 | 3 | import com.ellen.dhcsqlitelibrary.table.helper.json.JxFormat; 4 | import com.ellen.dhcsqlitelibrary.table.helper.json.JsonLibraryType; 5 | 6 | /** 7 | * 设置库内部表操作的公共配置 8 | */ 9 | public class CommonSetting { 10 | 11 | /** 12 | * 自定义Json解析器 13 | */ 14 | private JxFormat jxFormat = null; 15 | /** 16 | * 指定内部Json解析器 17 | */ 18 | private JsonLibraryType jsonLibraryType = JsonLibraryType.GSON; 19 | /** 20 | * 是否开启多线程模式 21 | */ 22 | private boolean isMultiThreadSafety = false; 23 | 24 | public JxFormat getJxFormat() { 25 | return jxFormat; 26 | } 27 | 28 | public void setJxFormat(JxFormat jxFormat) { 29 | this.jxFormat = jxFormat; 30 | } 31 | 32 | public JsonLibraryType getJsonLibraryType() { 33 | return jsonLibraryType; 34 | } 35 | 36 | public void setJsonLibraryType(JsonLibraryType jsonLibraryType) { 37 | this.jsonLibraryType = jsonLibraryType; 38 | } 39 | 40 | public boolean isMultiThreadSafety() { 41 | return isMultiThreadSafety; 42 | } 43 | 44 | public void setMultiThreadSafety(boolean multiThreadSafety) { 45 | isMultiThreadSafety = multiThreadSafety; 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /dhcsqlitelibrary/src/main/java/com/ellen/dhcsqlitelibrary/table/impl/ZxyLibrary.java: -------------------------------------------------------------------------------- 1 | package com.ellen.dhcsqlitelibrary.table.impl; 2 | 3 | import android.content.Context; 4 | import android.database.Cursor; 5 | import android.database.sqlite.SQLiteDatabase; 6 | import android.util.Log; 7 | 8 | import com.ellen.sqlitecreate.createsql.delete.DeleteTable; 9 | import com.ellen.sqlitecreate.createsql.delete.DeleteTableDataRow; 10 | import com.ellen.sqlitecreate.createsql.serach.SerachTableData; 11 | import com.ellen.sqlitecreate.createsql.serach.SerachTableExist; 12 | import com.ellen.sqlitecreate.createsql.update.UpdateTableName; 13 | 14 | import java.lang.reflect.Array; 15 | import java.util.ArrayList; 16 | import java.util.List; 17 | 18 | public abstract class ZxyLibrary implements BaseZxyLibrary { 19 | 20 | private ZxySQLiteHelper zxySQLiteHelper; 21 | private String name = null; 22 | private String filePath = null; 23 | private CommonSetting commonSetting; 24 | 25 | public ZxyLibrary(Context context, String name, int version) { 26 | zxySQLiteHelper = new ZxySQLiteHelper(context, name, null, version); 27 | zxySQLiteHelper.setZxySQLiteHelperCallback(new ZxySQLiteHelper.ZxySQLiteHelperCallback() { 28 | @Override 29 | public void onCreate(SQLiteDatabase db) { 30 | onZxySQLiteCreate(db); 31 | } 32 | 33 | @Override 34 | public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 35 | onZxySQLiteUpgrade(db, oldVersion, newVersion); 36 | } 37 | }); 38 | } 39 | 40 | public ZxyLibrary(Context context, String libraryPath, String name, int version) { 41 | zxySQLiteHelper = new ZxySQLiteHelper(context, libraryPath, name, null, version); 42 | zxySQLiteHelper.setZxySQLiteHelperCallback(new ZxySQLiteHelper.ZxySQLiteHelperCallback() { 43 | @Override 44 | public void onCreate(SQLiteDatabase db) { 45 | onZxySQLiteCreate(db); 46 | } 47 | 48 | @Override 49 | public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 50 | onZxySQLiteUpgrade(db, oldVersion, newVersion); 51 | } 52 | }); 53 | } 54 | 55 | public SQLiteDatabase getWriteDataBase() { 56 | return zxySQLiteHelper.getWritableDatabase(); 57 | } 58 | 59 | public SQLiteDatabase getReadDataBase() { 60 | return zxySQLiteHelper.getReadableDatabase(); 61 | } 62 | 63 | public String getDatabaseName() { 64 | return zxySQLiteHelper.getDatabaseName(); 65 | } 66 | 67 | public ZxySQLiteHelper getZxySQLiteHelper() { 68 | return zxySQLiteHelper; 69 | } 70 | 71 | public abstract void onZxySQLiteCreate(SQLiteDatabase db); 72 | 73 | public abstract void onZxySQLiteUpgrade(SQLiteDatabase db, int oldVersion, int newVersion); 74 | 75 | public void deleteTable(String tableName) { 76 | String deleteTableSql = DeleteTable.getInstance().setTableName(tableName).createSQL(); 77 | getWriteDataBase().execSQL(deleteTableSql); 78 | } 79 | 80 | public void reNameTable(String oldTableName, String newTableName) { 81 | String updateTableName = UpdateTableName.getInstance().setOldTableName(oldTableName).setNewTableName(newTableName).createSQL(); 82 | getWriteDataBase().execSQL(updateTableName); 83 | } 84 | 85 | public void clearTable(String tableName) { 86 | String clearTableSql = DeleteTableDataRow.getInstance().setTableName(tableName).createDeleteAllDataSQL(); 87 | getWriteDataBase().execSQL(clearTableSql); 88 | } 89 | 90 | /** 91 | * 判断表是否存在 92 | * 93 | * @return 94 | */ 95 | public boolean isExist(String tableName) { 96 | String searchTableExistSql = SerachTableExist.getInstance() 97 | .setTableName(tableName) 98 | .createSQL(); 99 | Cursor cursor = zxySQLiteHelper.getWritableDatabase().rawQuery(searchTableExistSql, null); 100 | int count = cursor.getCount(); 101 | if (cursor != null) { 102 | cursor.close(); 103 | } 104 | return count != 0; 105 | } 106 | 107 | @Override 108 | public void clearLibrary() { 109 | String[] tableNames = getAllTableName(); 110 | if (tableNames != null && tableNames.length > 0) { 111 | for (String tableName : tableNames) { 112 | getWriteDataBase().execSQL(DeleteTable.getInstance().setTableName(tableName).createSQL()); 113 | } 114 | } 115 | } 116 | 117 | @Override 118 | public String[] getAllTableName() { 119 | //先查询到所有的表名 120 | String searchSql = SerachTableData.getInstance(). 121 | setTableName("sqlite_master") 122 | .setIsAddField(true) 123 | .addSelectField("name") 124 | .getTableAllDataSQL(null); 125 | List stringList = new ArrayList<>(); 126 | Cursor cursor = zxySQLiteHelper.getWritableDatabase().rawQuery(searchSql, null); 127 | 128 | while (cursor.moveToNext()) { 129 | int index = cursor.getColumnIndex("name"); 130 | String name = cursor.getString(index); 131 | if (!(name.equals("android_metadata") || name.equals("sqlite_sequence"))) { 132 | //属于系统的表 133 | stringList.add(name); 134 | } 135 | 136 | } 137 | if (cursor != null) { 138 | cursor.close(); 139 | } 140 | if (stringList.size() > 0) { 141 | String[] stringArray = new String[stringList.size()]; 142 | for (int i = 0; i < stringArray.length; i++) { 143 | stringArray[i] = stringList.get(i); 144 | } 145 | return stringArray; 146 | } else { 147 | return null; 148 | } 149 | } 150 | 151 | @Override 152 | public int getTableCount() { 153 | String[] s = getAllTableName(); 154 | return s == null ? 0 : s.length; 155 | } 156 | 157 | public CommonSetting getCommonSetting() { 158 | return commonSetting; 159 | } 160 | 161 | public void setCommonSetting(CommonSetting commonSetting) { 162 | this.commonSetting = commonSetting; 163 | } 164 | } 165 | -------------------------------------------------------------------------------- /dhcsqlitelibrary/src/main/java/com/ellen/dhcsqlitelibrary/table/impl/ZxySQLiteHelper.java: -------------------------------------------------------------------------------- 1 | package com.ellen.dhcsqlitelibrary.table.impl; 2 | 3 | import android.content.Context; 4 | import android.database.sqlite.SQLiteDatabase; 5 | import android.database.sqlite.SQLiteOpenHelper; 6 | import androidx.annotation.Nullable; 7 | 8 | import java.io.File; 9 | 10 | class ZxySQLiteHelper extends SQLiteOpenHelper { 11 | 12 | private ZxySQLiteHelperCallback zxySQLiteHelperCallback; 13 | 14 | ZxySQLiteHelper(@Nullable Context context, @Nullable String fatherFile, @Nullable String name, @Nullable SQLiteDatabase.CursorFactory factory, int version){ 15 | super(context,new File(fatherFile,name+".db").getAbsolutePath(),factory,version); 16 | } 17 | 18 | ZxySQLiteHelper(@Nullable Context context, @Nullable String name, @Nullable SQLiteDatabase.CursorFactory factory, int version) { 19 | super(context, name, factory, version); 20 | } 21 | 22 | @Override 23 | public void onCreate(SQLiteDatabase db) { 24 | if(zxySQLiteHelperCallback != null){ 25 | zxySQLiteHelperCallback.onCreate(db); 26 | } 27 | } 28 | 29 | @Override 30 | public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 31 | if(zxySQLiteHelperCallback != null){ 32 | zxySQLiteHelperCallback.onUpgrade(db,oldVersion,newVersion); 33 | } 34 | } 35 | 36 | public ZxySQLiteHelperCallback getZxySQLiteHelperCallback() { 37 | return zxySQLiteHelperCallback; 38 | } 39 | 40 | void setZxySQLiteHelperCallback(ZxySQLiteHelperCallback zxySQLiteHelperCallback) { 41 | this.zxySQLiteHelperCallback = zxySQLiteHelperCallback; 42 | } 43 | 44 | interface ZxySQLiteHelperCallback{ 45 | void onCreate(SQLiteDatabase db); 46 | void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion); 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /dhcsqlitelibrary/src/main/java/com/ellen/dhcsqlitelibrary/table/impl/ZxyTable.java: -------------------------------------------------------------------------------- 1 | package com.ellen.dhcsqlitelibrary.table.impl; 2 | 3 | 4 | import android.database.sqlite.SQLiteDatabase; 5 | 6 | import com.ellen.dhcsqlitelibrary.table.helper.json.JxFormat; 7 | import com.ellen.dhcsqlitelibrary.table.helper.json.JxHelper; 8 | import com.ellen.dhcsqlitelibrary.table.operate.BaseOperate; 9 | import com.ellen.dhcsqlitelibrary.table.operate.DebugListener; 10 | import com.ellen.dhcsqlitelibrary.table.operate.TotalListener; 11 | import com.ellen.dhcsqlitelibrary.table.proxy.AutoDesignOperate; 12 | import com.ellen.dhcsqlitelibrary.table.operate.SqlOperate; 13 | import com.ellen.dhcsqlitelibrary.table.helper.ReflectHelper; 14 | import com.ellen.dhcsqlitelibrary.table.operate.add.Add; 15 | import com.ellen.dhcsqlitelibrary.table.operate.create.Create; 16 | import com.ellen.dhcsqlitelibrary.table.operate.create.OnCreateTableCallback; 17 | import com.ellen.dhcsqlitelibrary.table.operate.delete.Delete; 18 | import com.ellen.dhcsqlitelibrary.table.operate.search.Search; 19 | import com.ellen.dhcsqlitelibrary.table.operate.table.Table; 20 | import com.ellen.dhcsqlitelibrary.table.operate.update.Update; 21 | import com.ellen.dhcsqlitelibrary.table.proxy.AutoOperateProxy; 22 | import com.ellen.dhcsqlitelibrary.table.type.BasicTypeSupport; 23 | import com.ellen.dhcsqlitelibrary.table.type.DataStructureSupport; 24 | import com.ellen.dhcsqlitelibrary.table.type.Intercept; 25 | 26 | import java.lang.reflect.ParameterizedType; 27 | import java.lang.reflect.Type; 28 | import java.util.List; 29 | 30 | public class ZxyTable implements Create, Add, Search, Delete, Update, Table { 31 | 32 | private Class autoClass; 33 | private Class dataClass; 34 | private O autoDesignOperate; 35 | 36 | private String tableName; 37 | private ReflectHelper reflectHelper; 38 | private JxFormat jxFormat; 39 | private CommonSetting commonSetting; 40 | 41 | //数据操作 42 | private SqlOperate sqlOperate; 43 | 44 | private DataStructureSupport dataStructureSupport = null; 45 | private BasicTypeSupport basicTypeSupport = null; 46 | protected SQLiteDatabase db; 47 | private ZxyLibrary zxyLibrary = null; 48 | 49 | public ZxyTable(SQLiteDatabase db, String tableName,Class dataClass,Class autoClass) { 50 | this.dataClass = dataClass; 51 | this.autoClass = autoClass; 52 | init(db, tableName); 53 | } 54 | 55 | public ZxyTable(SQLiteDatabase db,Class dataClass,Class autoClass) { 56 | this.dataClass = dataClass; 57 | this.autoClass = autoClass; 58 | init(db, null); 59 | } 60 | 61 | public ZxyTable(ZxyLibrary zxyLibrary, String tableName,Class dataClass,Class autoClass) { 62 | this.zxyLibrary = zxyLibrary; 63 | this.dataClass = dataClass; 64 | this.autoClass = autoClass; 65 | init(zxyLibrary.getWriteDataBase(), tableName); 66 | } 67 | 68 | public ZxyTable(ZxyLibrary zxyLibrary,Class dataClass,Class autoClass) { 69 | this.zxyLibrary = zxyLibrary; 70 | this.dataClass = dataClass; 71 | this.autoClass = autoClass; 72 | init(zxyLibrary.getWriteDataBase(), null); 73 | } 74 | 75 | /** 76 | * 不安全的Class获取方式,弃用 77 | * @param index 78 | * @return 79 | */ 80 | private Class getClassByIndex(int index) { 81 | Class zxyTableClass = this.getClass(); 82 | Type typeZxy = zxyTableClass.getGenericSuperclass(); 83 | ParameterizedType parameterizedType = (ParameterizedType) typeZxy; 84 | Type type = parameterizedType.getActualTypeArguments()[index]; 85 | return (Class) type; 86 | } 87 | 88 | private void init(SQLiteDatabase db, String tableName) { 89 | this.db = db; 90 | if (tableName == null) { 91 | this.tableName = dataClass.getSimpleName(); 92 | } else { 93 | this.tableName = tableName; 94 | } 95 | if(zxyLibrary == null){ 96 | commonSetting = new CommonSetting(); 97 | }else { 98 | if (zxyLibrary.getCommonSetting() == null) { 99 | commonSetting = new CommonSetting(); 100 | } else { 101 | commonSetting = zxyLibrary.getCommonSetting(); 102 | } 103 | } 104 | setting(commonSetting); 105 | //是否设置多线程安全 106 | if (commonSetting.isMultiThreadSafety()) { 107 | //设置多线程安全(读写之间不阻塞,写与写之间阻塞) 108 | this.db.enableWriteAheadLogging(); 109 | } 110 | reflectHelper = new ReflectHelper<>(); 111 | jxFormat = commonSetting.getJxFormat(); 112 | if (jxFormat == null) { 113 | jxFormat = new JxHelper(commonSetting.getJsonLibraryType()); 114 | } 115 | //数据结构支持 116 | dataStructureSupport = new DataStructureSupport(jxFormat, new DataStructureSupport.ToObject() { 117 | @Override 118 | public Object toObj(String fieldName, Class fieldClass, String json) { 119 | return resumeDataStructure(fieldName, fieldClass, json); 120 | } 121 | }); 122 | //基本类型支持 123 | basicTypeSupport = new BasicTypeSupport(reflectHelper, new BasicTypeSupport.SetBooleanValue() { 124 | @Override 125 | public Object setBooleanValue(String classFieldName, boolean value) { 126 | return ZxyTable.this.setBooleanValue(classFieldName, value); 127 | } 128 | }); 129 | sqlOperate = new SqlOperate<>(db, dataClass, jxFormat, reflectHelper, dataStructureSupport, basicTypeSupport, this); 130 | 131 | //完成代理 132 | autoDesignOperate = AutoOperateProxy.newMapperProxy(autoClass, this); 133 | } 134 | 135 | protected void setting(CommonSetting commonSetting) { 136 | } 137 | 138 | public static void setTotalListener(TotalListener totalListener) { 139 | BaseOperate.setTotalListener(totalListener); 140 | } 141 | 142 | protected Object resumeDataStructure(String classFieldName, Class fieldClass, String json) { 143 | return null; 144 | } 145 | 146 | protected Object setBooleanValue(String classFieldName, boolean value) { 147 | if (value) { 148 | return "1"; 149 | } else { 150 | return "0"; 151 | } 152 | } 153 | 154 | /** 155 | * 监听Sql语句执行 156 | * 方便调试 157 | * 158 | * @param debugListener 159 | */ 160 | public void setDebugListener(DebugListener debugListener) { 161 | sqlOperate.setDebugListener(debugListener); 162 | } 163 | 164 | /** 165 | * 添加拦截 166 | * 167 | * @param intercept 168 | */ 169 | public void addIntercept(Intercept intercept) { 170 | sqlOperate.addIntercept(intercept); 171 | } 172 | 173 | /** 174 | * 移除拦截 175 | * 176 | * @param intercept 177 | */ 178 | public void removeIntercept(Intercept intercept) { 179 | sqlOperate.removeIntercept(intercept); 180 | } 181 | 182 | public O getAutoDesignOperate() { 183 | return autoDesignOperate; 184 | } 185 | 186 | public String getTableName() { 187 | return tableName; 188 | } 189 | 190 | @Override 191 | public void onCreateTable() { 192 | sqlOperate.onCreateTable(); 193 | } 194 | 195 | @Override 196 | public void onCreateTable(OnCreateTableCallback onCreateTableCallback) { 197 | sqlOperate.onCreateTable(onCreateTableCallback); 198 | } 199 | 200 | @Override 201 | public void onCreateTableIfNotExits() { 202 | sqlOperate.onCreateTableIfNotExits(); 203 | } 204 | 205 | @Override 206 | public void onCreateTableIfNotExits(OnCreateTableCallback onCreateTableCallback) { 207 | sqlOperate.onCreateTableIfNotExits(onCreateTableCallback); 208 | } 209 | 210 | @Override 211 | public void saveData(T data) { 212 | sqlOperate.saveData(data); 213 | } 214 | 215 | @Override 216 | public void saveData(List dataList) { 217 | sqlOperate.saveData(dataList); 218 | } 219 | 220 | @Override 221 | public void saveDataAndDeleteAgo(List dataList) { 222 | sqlOperate.saveDataAndDeleteAgo(dataList); 223 | } 224 | 225 | @Override 226 | public void saveDataAndDeleteAgo(T data) { 227 | sqlOperate.saveDataAndDeleteAgo(data); 228 | } 229 | 230 | @Override 231 | public void saveData(List dataList, int segment) { 232 | sqlOperate.saveData(dataList, segment); 233 | } 234 | 235 | @Override 236 | public List search(String whereSQL, String orderSQL) { 237 | return sqlOperate.search(whereSQL, orderSQL); 238 | } 239 | 240 | @Override 241 | public List getAllData() { 242 | return sqlOperate.getAllData(); 243 | } 244 | 245 | @Override 246 | public List getAllData(String orderSql) { 247 | return sqlOperate.getAllData(orderSql); 248 | } 249 | 250 | @Override 251 | public List searchDataBySql(String sql) { 252 | return sqlOperate.searchDataBySql(sql); 253 | } 254 | 255 | @Override 256 | public boolean isContainsByMajorKey(T t) { 257 | return sqlOperate.isContainsByMajorKey(t); 258 | } 259 | 260 | @Override 261 | public T searchByMajorKey(Object value) { 262 | return sqlOperate.searchByMajorKey(value); 263 | } 264 | 265 | @Override 266 | public boolean isExist() { 267 | return sqlOperate.isExist(); 268 | } 269 | 270 | @Override 271 | public boolean deleteTable() { 272 | return sqlOperate.deleteTable(); 273 | } 274 | 275 | @Override 276 | public int deleteReturnCount(String whereSql) { 277 | return sqlOperate.deleteReturnCount(whereSql); 278 | } 279 | 280 | @Override 281 | public void delete(String whereSql) { 282 | sqlOperate.delete(whereSql); 283 | } 284 | 285 | 286 | @Override 287 | public boolean deleteByMajorKey(Object majorKeyValue) { 288 | return sqlOperate.deleteByMajorKey(majorKeyValue); 289 | } 290 | 291 | @Override 292 | public void clear() { 293 | sqlOperate.clear(); 294 | } 295 | 296 | @Override 297 | public void saveOrUpdateByMajorKey(T t) { 298 | sqlOperate.saveOrUpdateByMajorKey(t); 299 | } 300 | 301 | @Override 302 | public void saveOrUpdateByMajorKey(List tList) { 303 | sqlOperate.saveOrUpdateByMajorKey(tList); 304 | } 305 | 306 | @Override 307 | public boolean updateByMajorKeyReturn(T t) { 308 | return sqlOperate.updateByMajorKeyReturn(t); 309 | } 310 | 311 | @Override 312 | public void updateByMajorKey(T t) { 313 | sqlOperate.updateByMajorKey(t); 314 | } 315 | 316 | @Override 317 | public int updateReturnCount(T t, String whereSQL) { 318 | int count = 0; 319 | count = sqlOperate.updateReturnCount(t, whereSQL); 320 | return count; 321 | } 322 | 323 | @Override 324 | public void update(T t, String whereSQL) { 325 | sqlOperate.update(t, whereSQL); 326 | } 327 | 328 | @Override 329 | public boolean reNameTable(String newName) { 330 | if (sqlOperate.reNameTable(newName)) { 331 | tableName = newName; 332 | return true; 333 | } else { 334 | return false; 335 | } 336 | } 337 | 338 | @Override 339 | public String getMajorKeyName() { 340 | return sqlOperate.getMajorKeyName(); 341 | } 342 | 343 | @Override 344 | public void exeSql(String sql) { 345 | sqlOperate.exeSql(sql); 346 | } 347 | 348 | @Override 349 | public void close() { 350 | sqlOperate.close(); 351 | } 352 | 353 | } 354 | -------------------------------------------------------------------------------- /dhcsqlitelibrary/src/main/java/com/ellen/dhcsqlitelibrary/table/operate/BaseOperate.java: -------------------------------------------------------------------------------- 1 | package com.ellen.dhcsqlitelibrary.table.operate; 2 | 3 | import android.database.Cursor; 4 | import android.database.sqlite.SQLiteDatabase; 5 | 6 | import com.ellen.dhcsqlitelibrary.table.helper.CursorHelper; 7 | import com.ellen.dhcsqlitelibrary.table.helper.ReflectHelper; 8 | import com.ellen.dhcsqlitelibrary.table.impl.ZxyTable; 9 | import com.ellen.dhcsqlitelibrary.table.type.BasicTypeSupport; 10 | import com.ellen.dhcsqlitelibrary.table.type.DataStructureSupport; 11 | import com.ellen.dhcsqlitelibrary.table.type.Intercept; 12 | import com.ellen.dhcsqlitelibrary.table.type.ObjectTypeSupport; 13 | import com.ellen.dhcsqlitelibrary.table.type.TypeSupport; 14 | import com.ellen.sqlitecreate.createsql.create.createtable.SQLField; 15 | 16 | import java.lang.reflect.Field; 17 | import java.lang.reflect.InvocationTargetException; 18 | import java.util.ArrayList; 19 | import java.util.HashMap; 20 | import java.util.List; 21 | 22 | public class BaseOperate extends ZxySqlCreate { 23 | 24 | protected SQLiteDatabase db; 25 | 26 | protected Class dataClass; 27 | protected List sqlFieldList; 28 | protected HashMap sqlNameMap; 29 | protected CursorHelper cursorHelper; 30 | protected ReflectHelper reflectHelper = null; 31 | protected DebugListener debugListener; 32 | 33 | //数据类型支持 34 | protected BasicTypeSupport basicTypeSupport; 35 | protected DataStructureSupport dataStructureSupport; 36 | protected ObjectTypeSupport objectTypeSupport; 37 | 38 | protected List interceptList; 39 | private static TotalListener totalListener; 40 | private ZxyTable zxyTable; 41 | 42 | public static void setTotalListener(TotalListener totalListener) { 43 | BaseOperate.totalListener = totalListener; 44 | } 45 | 46 | public void setDebugListener(DebugListener debugListener) { 47 | this.debugListener = debugListener; 48 | } 49 | 50 | public void addIntercept(Intercept intercept) { 51 | if (interceptList == null) { 52 | interceptList = new ArrayList<>(); 53 | } 54 | interceptList.add(0, intercept); 55 | } 56 | 57 | public void removeIntercept(Intercept intercept){ 58 | if(interceptList == null){ 59 | return; 60 | }else { 61 | interceptList.remove(intercept); 62 | } 63 | } 64 | 65 | public BaseOperate(SQLiteDatabase db,ZxyTable zxyTable) { 66 | this.db = db; 67 | this.zxyTable = zxyTable; 68 | } 69 | 70 | protected void exeSql(String sql) { 71 | if (debugListener != null) { 72 | debugListener.exeSql(sql); 73 | } 74 | if(totalListener != null){ 75 | totalListener.exeSql(zxyTable.getTableName(),sql); 76 | } 77 | db.beginTransaction(); 78 | try { 79 | db.execSQL(sql); 80 | //设置事务处理成功,不设置会自动回滚不提交。 81 | //如果没有执行这里,事务会自动回滚 82 | db.setTransactionSuccessful(); 83 | }catch (Exception e){ 84 | //这里进行事务回滚 85 | throw e; 86 | }finally { 87 | db.endTransaction(); 88 | } 89 | } 90 | 91 | protected List search(String sql) { 92 | if (debugListener != null) { 93 | debugListener.exeSql(sql); 94 | } 95 | if(totalListener != null){ 96 | totalListener.exeSql(zxyTable.getTableName(),sql); 97 | } 98 | return getDataByCursor(db.rawQuery(sql, null)); 99 | } 100 | 101 | protected Cursor searchReturnCursor(String sql){ 102 | if (debugListener != null) { 103 | debugListener.exeSql(sql); 104 | } 105 | if(totalListener != null){ 106 | totalListener.exeSql(zxyTable.getTableName(),sql); 107 | } 108 | Cursor cursor = db.rawQuery(sql,null); 109 | return cursor; 110 | } 111 | 112 | private List getDataByCursor(Cursor cursor) { 113 | List dataList = new ArrayList<>(); 114 | while (cursor.moveToNext()) { 115 | T t = null; 116 | try { 117 | t = reflectHelper.getT(dataClass); 118 | for (int i = 0; i < sqlFieldList.size(); i++) { 119 | Field field = sqlNameMap.get(sqlFieldList.get(i)); 120 | String sqlDataType = null; 121 | TypeSupport typeSupport = getTypeSupport(field); 122 | sqlDataType = typeSupport.setSQLiteType(field).getTypeString(); 123 | Object value = cursorHelper.readValueFromCursor(cursor, field, sqlFieldList.get(i), sqlDataType); 124 | field.set(t, typeSupport.toObj(field, value)); 125 | } 126 | } catch (IllegalAccessException e) { 127 | e.printStackTrace(); 128 | } catch (InvocationTargetException e) { 129 | e.printStackTrace(); 130 | } catch (InstantiationException e) { 131 | e.printStackTrace(); 132 | } 133 | dataList.add(t); 134 | } 135 | if (cursor != null) { 136 | cursor.close(); 137 | } 138 | return dataList; 139 | } 140 | 141 | protected TypeSupport getTypeSupport(Field field) { 142 | if (interceptList != null) { 143 | TypeSupport typeSupportResult = null; 144 | for (TypeSupport typeSupport : interceptList) { 145 | if (typeSupport.isType(field)) { 146 | typeSupportResult = typeSupport; 147 | break; 148 | } 149 | } 150 | if(typeSupportResult != null) { 151 | return typeSupportResult; 152 | } 153 | } 154 | if (basicTypeSupport.isType(field)) { 155 | return basicTypeSupport; 156 | } 157 | if (dataStructureSupport.isType(field)) { 158 | return dataStructureSupport; 159 | } 160 | if (objectTypeSupport.isType(field)) { 161 | return objectTypeSupport; 162 | } 163 | return null; 164 | } 165 | 166 | 167 | } 168 | -------------------------------------------------------------------------------- /dhcsqlitelibrary/src/main/java/com/ellen/dhcsqlitelibrary/table/operate/DebugListener.java: -------------------------------------------------------------------------------- 1 | package com.ellen.dhcsqlitelibrary.table.operate; 2 | 3 | /** 4 | * 方便调试 5 | */ 6 | public interface DebugListener { 7 | void exeSql(String sql); 8 | } 9 | -------------------------------------------------------------------------------- /dhcsqlitelibrary/src/main/java/com/ellen/dhcsqlitelibrary/table/operate/SqlOperate.java: -------------------------------------------------------------------------------- 1 | package com.ellen.dhcsqlitelibrary.table.operate; 2 | 3 | import android.database.Cursor; 4 | import android.database.SQLException; 5 | import android.database.sqlite.SQLiteDatabase; 6 | 7 | import com.ellen.dhcsqlitelibrary.table.annotation.field.bound.Check; 8 | import com.ellen.dhcsqlitelibrary.table.annotation.field.bound.Default; 9 | import com.ellen.dhcsqlitelibrary.table.annotation.field.bound.EndAutoString; 10 | import com.ellen.dhcsqlitelibrary.table.annotation.field.bound.MajorKey; 11 | import com.ellen.dhcsqlitelibrary.table.annotation.field.bound.NotNull; 12 | import com.ellen.dhcsqlitelibrary.table.annotation.field.bound.Unique; 13 | import com.ellen.dhcsqlitelibrary.table.exception.NoMajorKeyException; 14 | import com.ellen.dhcsqlitelibrary.table.exception.SqlFieldDuplicateException; 15 | import com.ellen.dhcsqlitelibrary.table.helper.CursorHelper; 16 | import com.ellen.dhcsqlitelibrary.table.helper.ReflectHelper; 17 | import com.ellen.dhcsqlitelibrary.table.helper.json.JxFormat; 18 | import com.ellen.dhcsqlitelibrary.table.operate.add.Add; 19 | import com.ellen.dhcsqlitelibrary.table.operate.create.Create; 20 | import com.ellen.dhcsqlitelibrary.table.operate.create.OnCreateTableCallback; 21 | import com.ellen.dhcsqlitelibrary.table.operate.delete.Delete; 22 | import com.ellen.dhcsqlitelibrary.table.operate.search.Search; 23 | import com.ellen.dhcsqlitelibrary.table.operate.table.Table; 24 | import com.ellen.dhcsqlitelibrary.table.operate.update.Update; 25 | import com.ellen.dhcsqlitelibrary.table.type.BasicTypeSupport; 26 | import com.ellen.dhcsqlitelibrary.table.type.DataStructureSupport; 27 | import com.ellen.dhcsqlitelibrary.table.impl.ZxyTable; 28 | import com.ellen.dhcsqlitelibrary.table.type.Intercept; 29 | import com.ellen.dhcsqlitelibrary.table.type.ObjectTypeSupport; 30 | import com.ellen.dhcsqlitelibrary.table.type.TypeSupport; 31 | import com.ellen.sqlitecreate.createsql.add.AddManyRowToTable; 32 | import com.ellen.sqlitecreate.createsql.add.AddSingleRowToTable; 33 | import com.ellen.sqlitecreate.createsql.create.createtable.SQLField; 34 | import com.ellen.sqlitecreate.createsql.helper.SQLFieldType; 35 | import com.ellen.sqlitecreate.createsql.helper.Value; 36 | import com.ellen.sqlitecreate.createsql.helper.WhereSymbolEnum; 37 | import com.ellen.sqlitecreate.createsql.serach.SerachTableData; 38 | import com.ellen.sqlitecreate.createsql.serach.SerachTableExist; 39 | import com.ellen.sqlitecreate.createsql.update.UpdateTableDataRow; 40 | import com.ellen.sqlitecreate.createsql.where.Where; 41 | 42 | import java.lang.reflect.Field; 43 | import java.util.ArrayList; 44 | import java.util.HashMap; 45 | import java.util.List; 46 | 47 | /** 48 | * 包含增删改查等等操作 49 | * 50 | * @param 51 | */ 52 | public class SqlOperate extends BaseOperate implements Create, Add, Search, Delete, Update, Table { 53 | 54 | private Field majorKeyField = null; 55 | private SQLField majorKeySqlField = null; 56 | private boolean isAutoIncrement = false; 57 | //Helper 58 | private JxFormat jxFormat; 59 | private ZxyTable zxyTable; 60 | 61 | public SqlOperate(SQLiteDatabase db, Class dataClass, JxFormat jxFormat, 62 | ReflectHelper reflectHelper, DataStructureSupport dataStructureSupport, 63 | BasicTypeSupport basicTypeSupport, ZxyTable zxyTable) { 64 | super(db, zxyTable); 65 | this.dataClass = dataClass; 66 | this.jxFormat = jxFormat; 67 | this.reflectHelper = reflectHelper; 68 | this.basicTypeSupport = basicTypeSupport; 69 | this.dataStructureSupport = dataStructureSupport; 70 | this.zxyTable = zxyTable; 71 | init(); 72 | //进行解析 73 | parsing(); 74 | 75 | } 76 | 77 | private void init() { 78 | cursorHelper = new CursorHelper(); 79 | objectTypeSupport = new ObjectTypeSupport(reflectHelper, jxFormat); 80 | sqlFieldList = new ArrayList<>(); 81 | sqlNameMap = new HashMap<>(); 82 | } 83 | 84 | @Override 85 | public void setDebugListener(DebugListener debugListener) { 86 | super.setDebugListener(debugListener); 87 | } 88 | 89 | /** 90 | * 判断是否是主键且自增 91 | * 92 | * @return 93 | */ 94 | protected boolean isHaveMajorKeyAndAuto() { 95 | boolean isHaveMajorAndAuto = false; 96 | if (majorKeySqlField != null) { 97 | if (isAutoIncrement) { 98 | isHaveMajorAndAuto = true; 99 | } 100 | } 101 | return isHaveMajorAndAuto; 102 | } 103 | 104 | /** 105 | * 进行解析 106 | */ 107 | private void parsing() { 108 | List fieldList = reflectHelper.getClassFieldList(dataClass, false); 109 | for (Field field : fieldList) { 110 | TypeSupport typeSupport = getTypeSupport(field); 111 | SQLFieldType sqlFieldType = typeSupport.setSQLiteType(field); 112 | SQLField sqlField = handlerSqlField(field, typeSupport.setSqlFieldName(field), sqlFieldType.getSQLFieldTypeString()); 113 | sqlNameMap.put(sqlField, field); 114 | for (SQLField s : sqlFieldList) { 115 | if (s.getName().equals(sqlField.getName())) { 116 | throw new SqlFieldDuplicateException("存在重复字段名:" + sqlField.getName() + ""); 117 | } 118 | } 119 | sqlFieldList.add(sqlField); 120 | } 121 | } 122 | 123 | /** 124 | * 处理映射的字段sql数据 125 | * 126 | * @param field 127 | * @param filedName 128 | * @param filedType 129 | * @return 130 | */ 131 | private SQLField handlerSqlField(Field field, String filedName, String filedType) { 132 | SQLField sqlField = null; 133 | //先判断有没有EndAutoString 134 | EndAutoString endAutoString = field.getAnnotation(EndAutoString.class); 135 | if (endAutoString != null) { 136 | sqlField = SQLField.getAutoEndStringField(filedName, filedType, endAutoString.value()); 137 | } else { 138 | sqlField = SQLField.getInstance(filedName, filedType); 139 | //判断有没有主键 140 | MajorKey majorKey = field.getAnnotation(MajorKey.class); 141 | if (majorKey != null) { 142 | sqlField.setMajorKey(true); 143 | if (majorKey.isAutoIncrement()) { 144 | sqlField.setAuto(true); 145 | isAutoIncrement = true; 146 | } 147 | majorKeySqlField = sqlField; 148 | majorKeyField = field; 149 | } 150 | if (field.getAnnotation(NotNull.class) != null) { 151 | sqlField.setNotNull(true); 152 | } 153 | if (field.getAnnotation(Unique.class) != null) { 154 | sqlField.setUnique(true); 155 | } 156 | Default defaultA = field.getAnnotation(Default.class); 157 | if (defaultA != null) { 158 | sqlField.setDefaultValue(reflectHelper.getDefaultValue(defaultA)); 159 | } 160 | Check check = field.getAnnotation(Check.class); 161 | if (check != null) { 162 | String checkWhereSql = check.value(); 163 | if (checkWhereSql.contains("{}")) { 164 | checkWhereSql = checkWhereSql.replace("{}", filedName); 165 | } 166 | sqlField.setCheckWhereSql(checkWhereSql); 167 | } 168 | sqlField.createSqlFiled(); 169 | } 170 | return sqlField; 171 | } 172 | 173 | @Override 174 | public String getMajorKeyName() { 175 | String majorKeyName = null; 176 | if (majorKeySqlField != null) { 177 | majorKeyName = majorKeySqlField.getName(); 178 | } 179 | return majorKeyName; 180 | } 181 | 182 | @Override 183 | public void exeSql(String sql) { 184 | super.exeSql(sql); 185 | } 186 | 187 | @Override 188 | public void close() { 189 | db.close(); 190 | } 191 | 192 | @Override 193 | public void saveData(T data) { 194 | if (data == null) { 195 | return; 196 | } 197 | AddSingleRowToTable addSingleRowToTable = getAddSingleRowToTable(); 198 | addSingleRowToTable.setTableName(getTableName()); 199 | for (int i = 0; i < sqlFieldList.size(); i++) { 200 | SQLField sqlField = sqlFieldList.get(i); 201 | Field field = sqlNameMap.get(sqlFieldList.get(i)); 202 | Object value = null; 203 | TypeSupport typeSupport = getTypeSupport(field); 204 | value = typeSupport.toValue(field, reflectHelper.getValue(data, field)); 205 | //先判断是否有主键且自增 206 | if (isHaveMajorKeyAndAuto()) { 207 | if (!sqlField.getName().equals(majorKeySqlField.getName())) { 208 | addSingleRowToTable.addData(new Value(sqlFieldList.get(i).getName(), value)); 209 | } 210 | } else { 211 | addSingleRowToTable.addData(new Value(sqlFieldList.get(i).getName(), value)); 212 | } 213 | } 214 | String addDataSql = addSingleRowToTable.createSQL(); 215 | exeSql(addDataSql); 216 | } 217 | 218 | @Override 219 | public void saveData(List dataList) { 220 | if (dataList == null || dataList.size() == 0) { 221 | return; 222 | } 223 | AddManyRowToTable addManyRowToTable = getAddManyRowToTable(); 224 | addManyRowToTable.setTableName(getTableName()); 225 | if (isHaveMajorKeyAndAuto()) { 226 | List currentSqlFieldList = new ArrayList<>(); 227 | for (SQLField sqlField : sqlFieldList) { 228 | if (!sqlField.getName().equals(majorKeySqlField.getName())) { 229 | currentSqlFieldList.add(sqlField); 230 | } 231 | } 232 | addManyRowToTable.addFieldList(currentSqlFieldList); 233 | } else { 234 | addManyRowToTable.addFieldList(sqlFieldList); 235 | } 236 | for (int i = 0; i < dataList.size(); i++) { 237 | List list = new ArrayList(); 238 | for (int j = 0; j < sqlFieldList.size(); j++) { 239 | Field field = sqlNameMap.get(sqlFieldList.get(j)); 240 | Object value = null; 241 | TypeSupport typeSupport = getTypeSupport(field); 242 | value = typeSupport.toValue(field, reflectHelper.getValue(dataList.get(i), field)); 243 | //此处可以添加记录数据库数据之前的操作:加密数据等 244 | if (isHaveMajorKeyAndAuto()) { 245 | if (!sqlFieldList.get(j).getName().equals(majorKeySqlField.getName())) { 246 | list.add(value); 247 | } 248 | } else { 249 | list.add(value); 250 | } 251 | } 252 | addManyRowToTable.addValueList(list); 253 | } 254 | String addDataSql = addManyRowToTable.createSQL(); 255 | exeSql(addDataSql); 256 | } 257 | 258 | private void saveDataToSegment(List dataList) { 259 | if (dataList == null || dataList.size() == 0) { 260 | return; 261 | } 262 | 263 | AddManyRowToTable addManyRowToTable = getAddManyRowToTable(); 264 | addManyRowToTable.setTableName(getTableName()); 265 | if (isHaveMajorKeyAndAuto()) { 266 | List currentSqlFieldList = new ArrayList<>(); 267 | for (SQLField sqlField : sqlFieldList) { 268 | if (!sqlField.getName().equals(majorKeySqlField.getName())) { 269 | currentSqlFieldList.add(sqlField); 270 | } 271 | } 272 | addManyRowToTable.addFieldList(currentSqlFieldList); 273 | } else { 274 | addManyRowToTable.addFieldList(sqlFieldList); 275 | } 276 | for (int i = 0; i < dataList.size(); i++) { 277 | List list = new ArrayList(); 278 | for (int j = 0; j < sqlFieldList.size(); j++) { 279 | Field field = sqlNameMap.get(sqlFieldList.get(j)); 280 | Object value = null; 281 | TypeSupport typeSupport = getTypeSupport(field); 282 | value = typeSupport.toValue(field, reflectHelper.getValue(dataList.get(i), field)); 283 | //此处可以添加记录数据库数据之前的操作:加密数据等 284 | if (isHaveMajorKeyAndAuto()) { 285 | if (!sqlFieldList.get(j).getName().equals(majorKeySqlField.getName())) { 286 | list.add(value); 287 | } 288 | } else { 289 | list.add(value); 290 | } 291 | } 292 | addManyRowToTable.addValueList(list); 293 | } 294 | String addDataSql = addManyRowToTable.createSQL(); 295 | exeSql(addDataSql); 296 | } 297 | 298 | @Override 299 | public void saveDataAndDeleteAgo(List dataList) { 300 | clear(); 301 | saveData(dataList); 302 | } 303 | 304 | @Override 305 | public void saveDataAndDeleteAgo(T data) { 306 | clear(); 307 | saveData(data); 308 | } 309 | 310 | /** 311 | * 分组存储 312 | * 避免数据太多而导致SQL语句太长问题 313 | * @param dataList 存储的集合 314 | * @param segmentCount 每组的数目个数 315 | */ 316 | @Override 317 | public void saveData(List dataList, int segmentCount) { 318 | if (dataList == null && dataList.size() == 0) { 319 | return; 320 | } 321 | int current = 0; 322 | int sCount = dataList.size() / segmentCount + 1; 323 | if (sCount == 0 && segmentCount > dataList.size()) { 324 | //当分组的数目大于集合本身 325 | saveData(dataList); 326 | } else { 327 | for (int i = 0; i < sCount; i++) { 328 | current = segmentCount * i; 329 | int end = current + segmentCount; 330 | if (end > dataList.size()) { 331 | end = dataList.size(); 332 | } 333 | List zList = dataList.subList(current, end); 334 | saveDataToSegment(zList); 335 | } 336 | } 337 | } 338 | 339 | @Override 340 | public void onCreateTable() { 341 | String createTableSql = getCreateTable() 342 | .setTableName(getTableName()) 343 | .addField(sqlFieldList) 344 | .createSQL(); 345 | 346 | exeSql(createTableSql); 347 | } 348 | 349 | @Override 350 | public void onCreateTable(OnCreateTableCallback onCreateTableCallback) { 351 | boolean isException = false; 352 | String createTableSql = getCreateTable() 353 | .setTableName(getTableName()) 354 | .addField(sqlFieldList) 355 | .createSQL(); 356 | try { 357 | exeSql(createTableSql); 358 | } catch (SQLException e) { 359 | onCreateTableCallback.onCreateTableFailure(e.getMessage(), getTableName(), createTableSql); 360 | isException = true; 361 | } 362 | if (!isException) 363 | onCreateTableCallback.onCreateTableSuccess(getTableName(), createTableSql); 364 | } 365 | 366 | @Override 367 | public void onCreateTableIfNotExits() { 368 | 369 | String createTableSql = getCreateTable() 370 | .setTableName(getTableName()) 371 | .addField(sqlFieldList) 372 | .createSQLIfNotExists(); 373 | 374 | exeSql(createTableSql); 375 | } 376 | 377 | @Override 378 | public void onCreateTableIfNotExits(OnCreateTableCallback onCreateTableCallback) { 379 | boolean isException = false; 380 | String createTableSql = getCreateTable() 381 | .setTableName(getTableName()) 382 | .addField(sqlFieldList) 383 | .createSQLIfNotExists(); 384 | try { 385 | exeSql(createTableSql); 386 | } catch (SQLException e) { 387 | onCreateTableCallback.onCreateTableFailure(e.getMessage(), getTableName(), createTableSql); 388 | isException = true; 389 | } 390 | if (!isException) 391 | onCreateTableCallback.onCreateTableSuccess(getTableName(), createTableSql); 392 | } 393 | 394 | private String getTableName() { 395 | return zxyTable.getTableName(); 396 | } 397 | 398 | @Override 399 | public List search(String whereSQL, String orderSQL) { 400 | SerachTableData serachTableData = getSearchTableData().setTableName(getTableName()); 401 | serachTableData.setIsAddField(false); 402 | String searchSql = null; 403 | if (orderSQL != null) { 404 | searchSql = serachTableData.createSQLAutoWhere(whereSQL, orderSQL); 405 | } else { 406 | searchSql = serachTableData.createSQLAutoWhere(whereSQL); 407 | } 408 | return searchDataBySql(searchSql); 409 | } 410 | 411 | @Override 412 | public List getAllData() { 413 | return getAllData(null); 414 | } 415 | 416 | @Override 417 | public List getAllData(String orderSql) { 418 | SerachTableData serachTableData = getSearchTableData().setTableName(getTableName()); 419 | serachTableData.setIsAddField(false); 420 | String getAllTableDataSQL = serachTableData.getTableAllDataSQL(orderSql); 421 | return searchDataBySql(getAllTableDataSQL); 422 | } 423 | 424 | @Override 425 | public List searchDataBySql(String sql) { 426 | return search(sql); 427 | } 428 | 429 | /** 430 | * 是否包含此主键 431 | * 432 | * @param t 433 | * @return 434 | */ 435 | @Override 436 | public boolean isContainsByMajorKey(T t) { 437 | boolean isContains = false; 438 | if (majorKeySqlField == null) { 439 | //说明没有主键,抛出无主键异常 440 | throw new NoMajorKeyException("没有主键,无法根据主键查询数据的存在!"); 441 | } else { 442 | String whereSql = Where.getInstance(false) 443 | .addAndWhereValue(majorKeySqlField.getName(), WhereSymbolEnum.EQUAL, reflectHelper.getValue(t, majorKeyField)) 444 | .createSQL(); 445 | List tList = search(whereSql, null); 446 | if (tList != null && tList.size() > 0) { 447 | isContains = true; 448 | } else { 449 | isContains = false; 450 | } 451 | } 452 | return isContains; 453 | } 454 | 455 | @Override 456 | public T searchByMajorKey(Object value) { 457 | T t = null; 458 | if (majorKeySqlField == null) { 459 | //说明没有主键,抛出无主键异常 460 | throw new NoMajorKeyException("没有主键,无法根据主键查询数据!"); 461 | } else { 462 | String whereSql = Where.getInstance(false) 463 | .addAndWhereValue(majorKeySqlField.getName(), WhereSymbolEnum.EQUAL, value) 464 | .createSQL(); 465 | List tList = search(whereSql, null); 466 | if (tList != null && tList.size() > 0) { 467 | t = tList.get(0); 468 | } 469 | } 470 | return t; 471 | } 472 | 473 | /** 474 | * 判断表是否存在 475 | * 476 | * @return 477 | */ 478 | @Override 479 | public boolean isExist() { 480 | String searchTableExistSql = SerachTableExist.getInstance() 481 | .setTableName(getTableName()) 482 | .createSQL(); 483 | Cursor cursor = searchReturnCursor(searchTableExistSql); 484 | int count = cursor.getCount(); 485 | if (cursor != null) { 486 | cursor.close(); 487 | } 488 | return count != 0; 489 | } 490 | 491 | @Override 492 | public boolean deleteTable() { 493 | boolean isDelete = false; 494 | if (isExist()) { 495 | isDelete = true; 496 | String deleteTableSQL = getDeleteTable().setTableName(getTableName()).createSQL(); 497 | exeSql(deleteTableSQL); 498 | } 499 | return isDelete; 500 | } 501 | 502 | @Override 503 | public int deleteReturnCount(String whereSql) { 504 | int count = getSearchDataCount(whereSql); 505 | if (count > 0) { 506 | String deleteSQL = getDeleteTableDataRow().setTableName(getTableName()).createSQLAutoWhere(whereSql); 507 | exeSql(deleteSQL); 508 | return count; 509 | } 510 | return 0; 511 | } 512 | 513 | @Override 514 | public void delete(String whereSql) { 515 | String deleteSQL = getDeleteTableDataRow().setTableName(getTableName()).createSQLAutoWhere(whereSql); 516 | exeSql(deleteSQL); 517 | } 518 | 519 | private boolean isHaveData(String whereSql) { 520 | return getSearchDataCount(whereSql) > 0; 521 | } 522 | 523 | private int getSearchDataCount(String whereSql) { 524 | List tList = search(whereSql, null); 525 | if (tList != null && tList.size() > 0) { 526 | return tList.size(); 527 | } else { 528 | return 0; 529 | } 530 | } 531 | 532 | @Override 533 | public boolean deleteByMajorKey(Object majorKeyValue) { 534 | if (majorKeyField == null) { 535 | //说明没有主键,抛出无主键异常 536 | throw new NoMajorKeyException("没有主键,无法根据主键删除数据!"); 537 | } else { 538 | String whereSql = getWhere(false) 539 | .addAndWhereValue(majorKeySqlField.getName(), WhereSymbolEnum.EQUAL, majorKeyValue) 540 | .createSQL(); 541 | return deleteReturnCount(whereSql) > 0; 542 | } 543 | } 544 | 545 | @Override 546 | public void clear() { 547 | String clearTableSQL = getDeleteTableDataRow().setTableName(getTableName()).createDeleteAllDataSQL(); 548 | exeSql(clearTableSQL); 549 | } 550 | 551 | @Override 552 | public void saveOrUpdateByMajorKey(T t) { 553 | if (isContainsByMajorKey(t)) { 554 | //进行更新 555 | updateByMajorKey(t); 556 | } else { 557 | saveData(t); 558 | } 559 | } 560 | 561 | @Override 562 | public void saveOrUpdateByMajorKey(List tList) { 563 | List saveList = new ArrayList<>(); 564 | for (T t : tList) { 565 | if (isContainsByMajorKey(t)) { 566 | updateByMajorKey(t); 567 | } else { 568 | saveList.add(t); 569 | } 570 | } 571 | saveData(saveList); 572 | } 573 | 574 | @Override 575 | public boolean updateByMajorKeyReturn(T t) { 576 | if (majorKeySqlField == null) { 577 | //说明没有主键,抛出无主键异常 578 | throw new NoMajorKeyException("没有主键,无法根据主键更新数据!"); 579 | } else { 580 | String whereSql = Where.getInstance(false) 581 | .addAndWhereValue(majorKeySqlField.getName(), WhereSymbolEnum.EQUAL, reflectHelper.getValue(t, majorKeyField)) 582 | .createSQL(); 583 | if (isHaveData(whereSql)) { 584 | update(t, whereSql); 585 | return true; 586 | } else { 587 | return false; 588 | } 589 | } 590 | } 591 | 592 | @Override 593 | public void updateByMajorKey(T t) { 594 | String whereSql = Where.getInstance(false) 595 | .addAndWhereValue(majorKeySqlField.getName(), WhereSymbolEnum.EQUAL, reflectHelper.getValue(t, majorKeyField)) 596 | .createSQL(); 597 | update(t, whereSql); 598 | } 599 | 600 | @Override 601 | public int updateReturnCount(T t, String whereSQL) { 602 | int count = getSearchDataCount(whereSQL); 603 | if (count > 0) { 604 | update(t, whereSQL); 605 | return count; 606 | } else { 607 | return 0; 608 | } 609 | } 610 | 611 | @Override 612 | public void update(T t, String whereSQL) { 613 | UpdateTableDataRow updateTableDataRow = getUpdateTableDataRow(); 614 | updateTableDataRow.setTableName(getTableName()); 615 | for (int i = 0; i < sqlFieldList.size(); i++) { 616 | String fieldName = sqlFieldList.get(i).getName(); 617 | SQLField sqlField = sqlFieldList.get(i); 618 | Field field = sqlNameMap.get(sqlFieldList.get(i)); 619 | Object value = null; 620 | TypeSupport typeSupport = getTypeSupport(field); 621 | value = typeSupport.toValue(field, reflectHelper.getValue(t, field)); 622 | if (isHaveMajorKeyAndAuto()) { 623 | //不更新主键 624 | if (!sqlField.getName().equals(majorKeySqlField.getName())) { 625 | //不是主键 626 | updateTableDataRow.addSetValue(fieldName, value); 627 | } 628 | } else { 629 | updateTableDataRow.addSetValue(fieldName, value); 630 | } 631 | } 632 | String updateSql = updateTableDataRow.createSQLAutoWhere(whereSQL); 633 | exeSql(updateSql); 634 | } 635 | 636 | @Override 637 | public boolean reNameTable(String newName) { 638 | boolean isCanRename = true; 639 | //判断新表是否存在 640 | String searchTableExistSql = SerachTableExist.getInstance() 641 | .setTableName(newName) 642 | .createSQL(); 643 | Cursor cursor = db.rawQuery(searchTableExistSql, null); 644 | if (cursor.getCount() != 0) { 645 | isCanRename = false; 646 | } 647 | if(isCanRename) { 648 | String reNameTableSql = getUpdateTableName() 649 | .setOldTableName(getTableName()) 650 | .setNewTableName(newName) 651 | .createSQL(); 652 | exeSql(reNameTableSql); 653 | } 654 | if (cursor != null) { 655 | cursor.close(); 656 | } 657 | return true; 658 | } 659 | 660 | @Override 661 | public void addIntercept(Intercept intercept) { 662 | super.addIntercept(intercept); 663 | } 664 | 665 | @Override 666 | public void removeIntercept(Intercept intercept) { 667 | super.removeIntercept(intercept); 668 | } 669 | } 670 | -------------------------------------------------------------------------------- /dhcsqlitelibrary/src/main/java/com/ellen/dhcsqlitelibrary/table/operate/TotalListener.java: -------------------------------------------------------------------------------- 1 | package com.ellen.dhcsqlitelibrary.table.operate; 2 | 3 | public interface TotalListener{ 4 | void exeSql(String tableName,String sql); 5 | } 6 | -------------------------------------------------------------------------------- /dhcsqlitelibrary/src/main/java/com/ellen/dhcsqlitelibrary/table/operate/ZxySqlCreate.java: -------------------------------------------------------------------------------- 1 | package com.ellen.dhcsqlitelibrary.table.operate; 2 | 3 | import com.ellen.sqlitecreate.createsql.add.AddManyRowToTable; 4 | import com.ellen.sqlitecreate.createsql.add.AddSingleRowToTable; 5 | import com.ellen.sqlitecreate.createsql.add.AddTableColumn; 6 | import com.ellen.sqlitecreate.createsql.create.createtable.CreateTable; 7 | import com.ellen.sqlitecreate.createsql.delete.DeleteTable; 8 | import com.ellen.sqlitecreate.createsql.delete.DeleteTableDataRow; 9 | import com.ellen.sqlitecreate.createsql.order.Order; 10 | import com.ellen.sqlitecreate.createsql.serach.SerachTableData; 11 | import com.ellen.sqlitecreate.createsql.serach.SerachTableExist; 12 | import com.ellen.sqlitecreate.createsql.update.UpdateTableDataRow; 13 | import com.ellen.sqlitecreate.createsql.update.UpdateTableName; 14 | import com.ellen.sqlitecreate.createsql.where.Between; 15 | import com.ellen.sqlitecreate.createsql.where.Where; 16 | import com.ellen.sqlitecreate.createsql.where.WhereIn; 17 | 18 | /** 19 | * SQL语句制作封装类 20 | * 能完成大部分SQL语句的拼接 21 | */ 22 | public class ZxySqlCreate { 23 | 24 | /** 25 | * 创建表的SQL语句生产类 26 | */ 27 | public CreateTable getCreateTable(){ 28 | return CreateTable.getInstance(); 29 | } 30 | 31 | /** 32 | * 删除表SQL语句生产类 33 | * @return 34 | */ 35 | public DeleteTable getDeleteTable() { 36 | return DeleteTable.getInstance(); 37 | } 38 | /** 39 | * 动态添加表的列的SQL语句生产类 40 | * @return 41 | */ 42 | public AddTableColumn getAddTableColumn() { 43 | return AddTableColumn.getInstance(); 44 | } 45 | /** 46 | * 修改表的名字的SQL语句生产类 47 | * @return 48 | */ 49 | public UpdateTableName getUpdateTableName() { 50 | return UpdateTableName.getInstance(); 51 | } 52 | /** 53 | * 查询表是否存在的SQL语句生产类 54 | */ 55 | public SerachTableExist getSerachTableExist() { 56 | return SerachTableExist.getInstance(); 57 | } 58 | /** 59 | * 增加数据(单条)的SQL语句生产类 60 | */ 61 | public AddSingleRowToTable getAddSingleRowToTable() { 62 | return AddSingleRowToTable.getInstance(); 63 | } 64 | /** 65 | * 增加数据(多条)的SQL语句生产类 66 | * example: 67 | * INSERT INTO student (id,name,sex) VALUES (3,'李三','男'),(4,'王五','女'); 68 | */ 69 | public AddManyRowToTable getAddManyRowToTable() { 70 | return AddManyRowToTable.getInstance(); 71 | } 72 | /** 73 | * 删除数据的SQL语句生产类 74 | */ 75 | public DeleteTableDataRow getDeleteTableDataRow() { 76 | return DeleteTableDataRow.getInstance(); 77 | } 78 | /** 79 | * 修改数据的SQL语句生产类 80 | */ 81 | public UpdateTableDataRow getUpdateTableDataRow() { 82 | return UpdateTableDataRow.getInstance(); 83 | } 84 | /** 85 | * 查询数据的SQL语句生产类 86 | */ 87 | public SerachTableData getSearchTableData() { 88 | return SerachTableData.getInstance(); 89 | } 90 | /** 91 | * 普通where生产 92 | * example: 93 | * WHERE id = 5 AND name = 'Ellen' 94 | * @param isContainsWhere 生产出的Between是否包含Where 95 | * 96 | */ 97 | public Where getWhere(boolean isContainsWhere) { 98 | return Where.getInstance(isContainsWhere); 99 | } 100 | 101 | /** 102 | * WhereIn生产的SQL语句生产类 103 | * example: 104 | * WHERE name IN ('李一','王二','张三') 105 | * @param isContainsWhere 生产出的Between是否包含Where 106 | * @return 107 | */ 108 | public WhereIn getWhereIn(boolean isContainsWhere) { 109 | return WhereIn.getInstance(isContainsWhere); 110 | } 111 | 112 | /** 113 | * Between语句生产类 114 | * example: 115 | * BETWEEN 3 AND 8 116 | * @param isContainsWhere 生产出的Between是否包含Where 117 | * @return 118 | */ 119 | public Between getBetween(boolean isContainsWhere) { 120 | return Between.getInstance(isContainsWhere); 121 | } 122 | 123 | /** 124 | * 排序Order生成的SQL语句生产类 125 | * example1: 126 | * ORDER BY id ASC 127 | * example2: 128 | * ORDER BY id,name ASC 129 | * @param isContainsOrderBy 生产出的Order是否包含ORDER BY 130 | * @return 131 | */ 132 | public Order getOrder(boolean isContainsOrderBy) { 133 | return Order.getInstance(isContainsOrderBy); 134 | } 135 | 136 | } 137 | -------------------------------------------------------------------------------- /dhcsqlitelibrary/src/main/java/com/ellen/dhcsqlitelibrary/table/operate/add/Add.java: -------------------------------------------------------------------------------- 1 | package com.ellen.dhcsqlitelibrary.table.operate.add; 2 | 3 | import java.util.List; 4 | 5 | public interface Add { 6 | /** 7 | * 保存条数据 8 | * @param data 数据 9 | * @return 保存了多少条数据 10 | */ 11 | void saveData(T data); 12 | 13 | /** 14 | * 保存多条数据 15 | */ 16 | void saveData(List dataList); 17 | 18 | /** 19 | * 保存之前先清空表 20 | * @param dataList 21 | */ 22 | void saveDataAndDeleteAgo(List dataList); 23 | 24 | /** 25 | * 保存之前先清空表 26 | * @param data 27 | */ 28 | void saveDataAndDeleteAgo(T data); 29 | 30 | /** 31 | * 分段存储您的数据 32 | * 以防数据过多时,一条SQL语句太大了 33 | * @param dataList 34 | * @param segment 35 | */ 36 | void saveData(List dataList,int segment); 37 | } 38 | -------------------------------------------------------------------------------- /dhcsqlitelibrary/src/main/java/com/ellen/dhcsqlitelibrary/table/operate/create/Create.java: -------------------------------------------------------------------------------- 1 | package com.ellen.dhcsqlitelibrary.table.operate.create; 2 | 3 | public interface Create { 4 | void onCreateTable(); 5 | void onCreateTable(OnCreateTableCallback onCreateTableCallback); 6 | void onCreateTableIfNotExits(); 7 | void onCreateTableIfNotExits(OnCreateTableCallback onCreateTableCallback); 8 | } 9 | -------------------------------------------------------------------------------- /dhcsqlitelibrary/src/main/java/com/ellen/dhcsqlitelibrary/table/operate/create/OnCreateTableCallback.java: -------------------------------------------------------------------------------- 1 | package com.ellen.dhcsqlitelibrary.table.operate.create; 2 | 3 | /** 4 | * 加入回调的目的是方便调试 5 | */ 6 | public interface OnCreateTableCallback { 7 | /** 8 | * 创建表失败 9 | * @param errMessage 错误信息 10 | * @param tableName 表名 11 | * @param createSQL 创建表的SQL语句 12 | */ 13 | void onCreateTableFailure(String errMessage, String tableName, String createSQL); 14 | 15 | /** 16 | * 创建表成功 17 | * @param tableName 表名 18 | * @param createSQL 创建表的SQL语句 19 | */ 20 | void onCreateTableSuccess(String tableName, String createSQL); 21 | } 22 | -------------------------------------------------------------------------------- /dhcsqlitelibrary/src/main/java/com/ellen/dhcsqlitelibrary/table/operate/delete/Delete.java: -------------------------------------------------------------------------------- 1 | package com.ellen.dhcsqlitelibrary.table.operate.delete; 2 | 3 | public interface Delete { 4 | boolean deleteTable(); 5 | int deleteReturnCount(String whereSql); 6 | void delete(String whereSql); 7 | boolean deleteByMajorKey(Object majorKeyValue); 8 | void clear(); 9 | } 10 | -------------------------------------------------------------------------------- /dhcsqlitelibrary/src/main/java/com/ellen/dhcsqlitelibrary/table/operate/search/Search.java: -------------------------------------------------------------------------------- 1 | package com.ellen.dhcsqlitelibrary.table.operate.search; 2 | 3 | import java.util.List; 4 | 5 | public interface Search { 6 | List search(String whereSQL, String orderSQL); 7 | List getAllData(); 8 | List getAllData(String orderSql); 9 | List searchDataBySql(String sql); 10 | boolean isContainsByMajorKey(T t); 11 | T searchByMajorKey(Object value); 12 | 13 | /** 14 | * 判断表是否存在 15 | * 16 | * @return 17 | */ 18 | boolean isExist(); 19 | } 20 | -------------------------------------------------------------------------------- /dhcsqlitelibrary/src/main/java/com/ellen/dhcsqlitelibrary/table/operate/table/Table.java: -------------------------------------------------------------------------------- 1 | package com.ellen.dhcsqlitelibrary.table.operate.table; 2 | 3 | public interface Table { 4 | String getMajorKeyName(); 5 | void exeSql(String sql); 6 | 7 | /** 8 | * 关闭db,以防资源浪费 9 | */ 10 | void close(); 11 | } 12 | -------------------------------------------------------------------------------- /dhcsqlitelibrary/src/main/java/com/ellen/dhcsqlitelibrary/table/operate/update/Update.java: -------------------------------------------------------------------------------- 1 | package com.ellen.dhcsqlitelibrary.table.operate.update; 2 | 3 | import java.util.List; 4 | 5 | public interface Update{ 6 | void saveOrUpdateByMajorKey(T t); 7 | void saveOrUpdateByMajorKey(List tList); 8 | boolean updateByMajorKeyReturn(T t); 9 | void updateByMajorKey(T t); 10 | int updateReturnCount(T t, String whereSQL); 11 | void update(T t, String whereSQL); 12 | boolean reNameTable(String newName); 13 | } 14 | -------------------------------------------------------------------------------- /dhcsqlitelibrary/src/main/java/com/ellen/dhcsqlitelibrary/table/proxy/AutoDesignOperate.java: -------------------------------------------------------------------------------- 1 | package com.ellen.dhcsqlitelibrary.table.proxy; 2 | 3 | /** 4 | * 自动化设计你的操作 5 | * 用一个接口继承该接口,你可以自定义你所有想要的操作 6 | * 并且这些操作都可以通过sql语句进行定义,方便简洁的管理你的数据库 7 | * 8 | * 原理实际上就是动态代理 9 | * 10 | * 在此接口中你可以完成一些比较复杂的sql语句 11 | */ 12 | public interface AutoDesignOperate{ 13 | 14 | } 15 | -------------------------------------------------------------------------------- /dhcsqlitelibrary/src/main/java/com/ellen/dhcsqlitelibrary/table/proxy/AutoOperateProxy.java: -------------------------------------------------------------------------------- 1 | package com.ellen.dhcsqlitelibrary.table.proxy; 2 | 3 | import android.text.TextUtils; 4 | 5 | import com.ellen.dhcsqlitelibrary.table.exception.NoMajorKeyException; 6 | import com.ellen.dhcsqlitelibrary.table.annotation.auto.Delete; 7 | import com.ellen.dhcsqlitelibrary.table.annotation.auto.SearchByMajorKey; 8 | import com.ellen.dhcsqlitelibrary.table.annotation.auto.Search; 9 | import com.ellen.dhcsqlitelibrary.table.annotation.auto.TotalSql; 10 | import com.ellen.dhcsqlitelibrary.table.annotation.auto.Update; 11 | import com.ellen.dhcsqlitelibrary.table.annotation.auto.Value; 12 | import com.ellen.dhcsqlitelibrary.table.impl.ZxyTable; 13 | 14 | import java.lang.annotation.Annotation; 15 | import java.lang.reflect.InvocationHandler; 16 | import java.lang.reflect.Method; 17 | import java.lang.reflect.Proxy; 18 | import java.util.HashMap; 19 | import java.util.Map; 20 | import java.util.Set; 21 | 22 | public class AutoOperateProxy implements InvocationHandler { 23 | 24 | private ZxyTable zxyTable; 25 | 26 | public AutoOperateProxy(ZxyTable zxyTable) { 27 | this.zxyTable = zxyTable; 28 | } 29 | 30 | private Map getArgValue(Method method, Object[] args) { 31 | Map map = new HashMap<>(); 32 | Annotation[][] annotations = method.getParameterAnnotations(); 33 | if(annotations.length > 0){ 34 | for(int i= 0;i 0) { 44 | Map valueMap = getArgValue(method, args); 45 | Set stringSet = valueMap.keySet(); 46 | for (String name : stringSet) { 47 | sql = sql.replace("@" + name, valueMap.get(name).toString()); 48 | } 49 | } 50 | return sql; 51 | } 52 | 53 | @Override 54 | public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { 55 | //查询 56 | Search search = method.getAnnotation(Search.class); 57 | if (search != null) { 58 | String whereSql = search.whereSql(); 59 | String orderSql = search.orderSql(); 60 | if(orderSql != null && orderSql.equals("")){ 61 | orderSql = null; 62 | } 63 | if(orderSql != null){ 64 | orderSql = newSql(orderSql,method,args); 65 | } 66 | return zxyTable.search(newSql(whereSql, method, args), orderSql); 67 | } 68 | Delete delete = method.getAnnotation(Delete.class); 69 | if (delete != null) { 70 | String deleteSql = delete.value(); 71 | zxyTable.delete(newSql(deleteSql, method, args)); 72 | return null; 73 | } 74 | TotalSql totalSql = method.getAnnotation(TotalSql.class); 75 | if (totalSql != null) { 76 | String totalSqlString = totalSql.sql(); 77 | boolean isReturnValue = totalSql.isReturnValue(); 78 | if(isReturnValue){ 79 | //具有返回值 80 | String sql = newSql(totalSqlString, method, args); 81 | return zxyTable.searchDataBySql(sql); 82 | }else { 83 | //不具有返回值 84 | String sql = newSql(totalSqlString, method, args); 85 | zxyTable.exeSql(sql); 86 | return null; 87 | } 88 | } 89 | Update update = method.getAnnotation(Update.class); 90 | if (update != null) { 91 | String valueSql = update.valueSql(); 92 | String whereSql = update.whereSql(); 93 | valueSql = newSql(valueSql,method,args); 94 | whereSql = newSql(whereSql,method,args); 95 | StringBuilder stringBuilder = new StringBuilder("UPDATE "+ zxyTable.getTableName()+" SET "); 96 | stringBuilder.append(valueSql); 97 | stringBuilder.append(" WHERE "); 98 | stringBuilder.append(whereSql); 99 | stringBuilder.append(";"); 100 | String updateSql = stringBuilder.toString(); 101 | zxyTable.exeSql(updateSql); 102 | return null; 103 | } 104 | SearchByMajorKey searchByMajorKey = method.getAnnotation(SearchByMajorKey.class); 105 | if(searchByMajorKey != null){ 106 | String whereSql = searchByMajorKey.whereSql(); 107 | String orderSql = searchByMajorKey.orderSql(); 108 | if(orderSql != null && orderSql.equals("")){ 109 | orderSql = null; 110 | } 111 | if(orderSql != null){ 112 | orderSql = newSql(orderSql,method,args); 113 | } 114 | String majorKeyName = zxyTable.getMajorKeyName(); 115 | if(!TextUtils.isEmpty(majorKeyName)) { 116 | if(whereSql.contains("{}")) { 117 | whereSql = whereSql.replace("{}", zxyTable.getMajorKeyName()); 118 | }else { 119 | whereSql = zxyTable.getMajorKeyName() + " "+whereSql; 120 | } 121 | }else { 122 | throw new NoMajorKeyException("没有主键,无法根据主键查询数据!"); 123 | } 124 | return zxyTable.search(newSql(whereSql,method,args),orderSql); 125 | } 126 | return null; 127 | } 128 | 129 | public static T newMapperProxy(Class mapperInterface, ZxyTable zxyTable) { 130 | ClassLoader classLoader = mapperInterface.getClassLoader(); 131 | Class[] interfaces = new Class[]{mapperInterface}; 132 | AutoOperateProxy proxy = new AutoOperateProxy(zxyTable); 133 | return (T) Proxy.newProxyInstance(classLoader, interfaces, proxy); 134 | } 135 | 136 | } 137 | -------------------------------------------------------------------------------- /dhcsqlitelibrary/src/main/java/com/ellen/dhcsqlitelibrary/table/type/BasicTypeSupport.java: -------------------------------------------------------------------------------- 1 | package com.ellen.dhcsqlitelibrary.table.type; 2 | 3 | import com.ellen.dhcsqlitelibrary.table.annotation.field.DhcSqlFieldName; 4 | import com.ellen.dhcsqlitelibrary.table.exception.BoolNoCanSaveException; 5 | import com.ellen.dhcsqlitelibrary.table.helper.ReflectHelper; 6 | import com.ellen.sqlitecreate.createsql.helper.SQLFieldType; 7 | import com.ellen.sqlitecreate.createsql.helper.SQLFieldTypeEnum; 8 | 9 | import java.lang.reflect.Field; 10 | 11 | /** 12 | * 基本数据类型支持类 13 | */ 14 | public class BasicTypeSupport implements TypeSupport { 15 | 16 | private ReflectHelper reflectHelper; 17 | private SetBooleanValue setBooleanValue; 18 | 19 | public BasicTypeSupport(ReflectHelper reflectHelper, SetBooleanValue setBooleanValue) { 20 | this.reflectHelper = reflectHelper; 21 | this.setBooleanValue = setBooleanValue; 22 | } 23 | 24 | @Override 25 | public String setSqlFieldName(Field field) { 26 | DhcSqlFieldName dhcSqlFieldName = field.getAnnotation(DhcSqlFieldName.class); 27 | if (dhcSqlFieldName != null) { 28 | if (dhcSqlFieldName.sqlFieldName() != null && dhcSqlFieldName.sqlFieldName().length() > 0) { 29 | return dhcSqlFieldName.sqlFieldName(); 30 | } else { 31 | return field.getName(); 32 | } 33 | } else { 34 | return field.getName(); 35 | } 36 | } 37 | 38 | @Override 39 | public SQLFieldType setSQLiteType(Field field) { 40 | SQLFieldType sqlField = null; 41 | if (reflectHelper.isBooleanType(field)) { 42 | Object booleanSaveValue = setBooleanValue.setBooleanValue(field.getName(), true); 43 | if (reflectHelper.isBasicType(booleanSaveValue)) { 44 | //是基本类型 45 | SQLFieldTypeEnum sqlFieldTypeEnum = reflectHelper.getSqlStringType(booleanSaveValue.getClass()); 46 | sqlField = new SQLFieldType(sqlFieldTypeEnum, null); 47 | } else { 48 | //不是 抛出异常 49 | throw new BoolNoCanSaveException("布尔值类型只能映射为基本类型或者String类型,您映射的类型:" + booleanSaveValue.getClass().getName() + " 不被支持!"); 50 | } 51 | } else { 52 | sqlField = new SQLFieldType(reflectHelper.getSqlStringType(field.getType()), null); 53 | } 54 | return sqlField; 55 | } 56 | 57 | @Override 58 | public boolean isType(Field field) { 59 | return reflectHelper.isBasicType(field); 60 | } 61 | 62 | @Override 63 | public Object toObj(Field field, Object sqlValue) { 64 | if (reflectHelper.isBooleanType(field)) { 65 | if(sqlValue == null && field.getType() == Boolean.class){ 66 | return null; 67 | } 68 | if(sqlValue == null && field.getType().getName().equals("boolean")){ 69 | return false; 70 | } 71 | Object trueValue = setBooleanValue.setBooleanValue(field.getName(), true); 72 | if (trueValue.getClass() == Character.class) { 73 | char cr = (char) trueValue; 74 | trueValue = String.valueOf(cr); 75 | } else if (trueValue.getClass() == Boolean.class) { 76 | Boolean aBoolean = (Boolean) trueValue; 77 | if (aBoolean) { 78 | trueValue = 1; 79 | } else { 80 | trueValue = 0; 81 | } 82 | } 83 | if (trueValue.equals(sqlValue)) { 84 | return true; 85 | } else { 86 | return false; 87 | } 88 | } else { 89 | if(sqlValue == null){ 90 | return reflectHelper.getDefaultValue(field.getType()); 91 | }else { 92 | return sqlValue; 93 | } 94 | } 95 | } 96 | 97 | @Override 98 | public Object toValue(Field field, Object dataValue) { 99 | if (reflectHelper.isBooleanType(field)) { 100 | if (dataValue != null) { 101 | boolean bool = (boolean) dataValue; 102 | Object value = setBooleanValue.setBooleanValue(field.getName(), bool); 103 | if (value.getClass() == Character.class) { 104 | Character character = (Character) value; 105 | return character.toString(); 106 | } else if (value.getClass() == Boolean.class) { 107 | Boolean b = (Boolean) value; 108 | if (b) { 109 | return 1; 110 | } else { 111 | return 0; 112 | } 113 | } else { 114 | return value; 115 | } 116 | } else { 117 | return null; 118 | } 119 | } else { 120 | return dataValue; 121 | } 122 | } 123 | 124 | public interface SetBooleanValue { 125 | Object setBooleanValue(String classFieldName, boolean value); 126 | } 127 | } 128 | -------------------------------------------------------------------------------- /dhcsqlitelibrary/src/main/java/com/ellen/dhcsqlitelibrary/table/type/DataStructureSupport.java: -------------------------------------------------------------------------------- 1 | package com.ellen.dhcsqlitelibrary.table.type; 2 | 3 | import com.ellen.dhcsqlitelibrary.table.annotation.field.DhcSqlFieldName; 4 | import com.ellen.dhcsqlitelibrary.table.helper.json.JxFormat; 5 | import com.ellen.sqlitecreate.createsql.helper.SQLFieldType; 6 | import com.ellen.sqlitecreate.createsql.helper.SQLFieldTypeEnum; 7 | 8 | import java.lang.reflect.Field; 9 | import java.util.ArrayList; 10 | import java.util.HashMap; 11 | import java.util.HashSet; 12 | import java.util.LinkedList; 13 | import java.util.TreeMap; 14 | import java.util.TreeSet; 15 | import java.util.Vector; 16 | 17 | /** 18 | * 基础数据结构支持 19 | */ 20 | public class DataStructureSupport implements TypeSupport { 21 | 22 | private JxFormat jxFormat; 23 | private ToObject toObject; 24 | 25 | public DataStructureSupport(JxFormat jxFormat, ToObject toObject) { 26 | this.jxFormat = jxFormat; 27 | this.toObject = toObject; 28 | } 29 | 30 | @Override 31 | public String setSqlFieldName(Field field) { 32 | DhcSqlFieldName dhcSqlFieldName = field.getAnnotation(DhcSqlFieldName.class); 33 | if(dhcSqlFieldName != null){ 34 | if(dhcSqlFieldName.sqlFieldName() != null && dhcSqlFieldName.sqlFieldName().length() > 0){ 35 | return dhcSqlFieldName.sqlFieldName(); 36 | }else { 37 | return field.getName(); 38 | } 39 | }else { 40 | return field.getName(); 41 | } 42 | } 43 | 44 | @Override 45 | public SQLFieldType setSQLiteType(Field field) { 46 | return new SQLFieldType(SQLFieldTypeEnum.TEXT,null); 47 | } 48 | 49 | @Override 50 | public boolean isType(Field field) { 51 | Class fieldClass = field.getType(); 52 | //数组 53 | boolean isDataStructure = false; 54 | if (fieldClass.isArray()) { 55 | isDataStructure = true; 56 | } 57 | //List 58 | if (fieldClass == ArrayList.class || fieldClass == LinkedList.class || fieldClass == Vector.class) { 59 | isDataStructure = true; 60 | } 61 | //Set 62 | if (fieldClass == HashSet.class || fieldClass == TreeSet.class) { 63 | isDataStructure = true; 64 | } 65 | //Map 66 | if (fieldClass == HashMap.class || fieldClass == TreeMap.class) { 67 | isDataStructure = true; 68 | } 69 | return isDataStructure; 70 | } 71 | 72 | @Override 73 | public Object toObj(Field field, Object value) { 74 | if (value != null) { 75 | return toObject.toObj(field.getName(),field.getType(), (String) value); 76 | } else { 77 | return null; 78 | } 79 | } 80 | 81 | @Override 82 | public Object toValue(Field field,Object dataValue) { 83 | return dataValue == null ? null : jxFormat.toJxString(dataValue); 84 | } 85 | 86 | public interface ToObject{ 87 | Object toObj(String fieldName, Class fieldClass, String json); 88 | } 89 | } 90 | -------------------------------------------------------------------------------- /dhcsqlitelibrary/src/main/java/com/ellen/dhcsqlitelibrary/table/type/Intercept.java: -------------------------------------------------------------------------------- 1 | package com.ellen.dhcsqlitelibrary.table.type; 2 | 3 | import com.ellen.dhcsqlitelibrary.table.annotation.field.DhcSqlFieldName; 4 | 5 | import java.lang.reflect.Field; 6 | 7 | /** 8 | * 拦截器 9 | * @param 10 | * @param 11 | */ 12 | public abstract class Intercept implements TypeSupport{ 13 | 14 | @Override 15 | public String setSqlFieldName(Field field) { 16 | DhcSqlFieldName dhcSqlFieldName = field.getAnnotation(DhcSqlFieldName.class); 17 | if (dhcSqlFieldName != null) { 18 | if (dhcSqlFieldName.sqlFieldName() != null && dhcSqlFieldName.sqlFieldName().length() > 0) { 19 | return dhcSqlFieldName.sqlFieldName(); 20 | } else { 21 | return field.getName(); 22 | } 23 | } else { 24 | return field.getName(); 25 | } 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /dhcsqlitelibrary/src/main/java/com/ellen/dhcsqlitelibrary/table/type/ObjectTypeSupport.java: -------------------------------------------------------------------------------- 1 | package com.ellen.dhcsqlitelibrary.table.type; 2 | 3 | import com.ellen.dhcsqlitelibrary.table.annotation.field.DhcSqlFieldName; 4 | import com.ellen.dhcsqlitelibrary.table.annotation.field.Operate; 5 | import com.ellen.dhcsqlitelibrary.table.annotation.field.OperateEnum; 6 | import com.ellen.dhcsqlitelibrary.table.annotation.field.SqlType; 7 | import com.ellen.dhcsqlitelibrary.table.exception.NoCanSaveToSqlException; 8 | import com.ellen.dhcsqlitelibrary.table.helper.json.JxFormat; 9 | import com.ellen.dhcsqlitelibrary.table.helper.ReflectHelper; 10 | import com.ellen.sqlitecreate.createsql.helper.SQLFieldType; 11 | import com.ellen.sqlitecreate.createsql.helper.SQLFieldTypeEnum; 12 | 13 | import java.lang.reflect.Field; 14 | import java.lang.reflect.InvocationTargetException; 15 | 16 | /** 17 | * 引用类型支持 18 | */ 19 | public class ObjectTypeSupport implements TypeSupport { 20 | 21 | private ReflectHelper reflectHelper; 22 | private JxFormat jxFormat; 23 | 24 | public ObjectTypeSupport(ReflectHelper reflectHelper, JxFormat jxFormat) { 25 | this.reflectHelper = reflectHelper; 26 | this.jxFormat = jxFormat; 27 | } 28 | 29 | @Override 30 | public String setSqlFieldName(Field field) { 31 | DhcSqlFieldName dhcSqlFieldName = field.getAnnotation(DhcSqlFieldName.class); 32 | if(dhcSqlFieldName != null){ 33 | if(dhcSqlFieldName.sqlFieldName() != null && dhcSqlFieldName.sqlFieldName().length() > 0){ 34 | return dhcSqlFieldName.sqlFieldName(); 35 | }else { 36 | return field.getName(); 37 | } 38 | }else { 39 | return field.getName(); 40 | } 41 | } 42 | 43 | @Override 44 | public SQLFieldType setSQLiteType(Field field) { 45 | Operate operate = field.getAnnotation(Operate.class); 46 | SqlType sqlType = field.getAnnotation(SqlType.class); 47 | SQLFieldType sqlFieldType = null; 48 | if (operate != null) { 49 | if (operate.operate() == OperateEnum.JSON) { 50 | if (sqlType != null) { 51 | if (sqlType.sqlFiledType() == SQLFieldTypeEnum.TEXT) { 52 | if(sqlType.length() > 0) { 53 | sqlFieldType = new SQLFieldType(sqlType.sqlFiledType(), sqlType.length()); 54 | }else { 55 | sqlFieldType = new SQLFieldType(SQLFieldTypeEnum.TEXT, null); 56 | } 57 | } else { 58 | sqlFieldType = new SQLFieldType(SQLFieldTypeEnum.TEXT, null); 59 | } 60 | } else { 61 | sqlFieldType = new SQLFieldType(SQLFieldTypeEnum.TEXT, null); 62 | } 63 | } else { 64 | //如果不是以Json方式保存 65 | 66 | //先进行纠错 67 | String filedName = operate.valueName(); 68 | Field targetField = null; 69 | try { 70 | targetField = field.getType().getDeclaredField(filedName); 71 | targetField.setAccessible(true); 72 | } catch (NoSuchFieldException e) { 73 | e.printStackTrace(); 74 | String errMessage = field.getType().getCanonicalName()+"下不存在属性:"+filedName; 75 | throw new NoCanSaveToSqlException(errMessage); 76 | } 77 | 78 | //判断是否为基本类型 79 | if (!reflectHelper.isBasicType(targetField)) { 80 | String errMessage = field.getType().getCanonicalName() + "." + targetField.getName() + "不是基本类型,无法映射到数据库中"; 81 | throw new NoCanSaveToSqlException(errMessage); 82 | } 83 | //获取到对应的存储类型 84 | SQLFieldTypeEnum sqlFieldTypeEnum = reflectHelper.getSqlStringType(targetField.getType()); 85 | if (sqlType == null) { 86 | sqlFieldType = new SQLFieldType(sqlFieldTypeEnum,null); 87 | } else { 88 | if(sqlType.sqlFiledType() == sqlFieldTypeEnum){ 89 | if(sqlType.length() > 0) { 90 | sqlFieldType = new SQLFieldType(sqlFieldTypeEnum, sqlType.length()); 91 | }else { 92 | sqlFieldType = new SQLFieldType(sqlFieldTypeEnum, null); 93 | } 94 | }else { 95 | //进行容错 96 | sqlFieldType = new SQLFieldType(sqlFieldTypeEnum,null); 97 | } 98 | } 99 | } 100 | } else { 101 | //没有写,那就按照Json进行处理 102 | if (sqlType == null) { 103 | sqlFieldType = new SQLFieldType(SQLFieldTypeEnum.TEXT, null); 104 | } else { 105 | //容错机制 106 | if (sqlType.sqlFiledType() == SQLFieldTypeEnum.TEXT) { 107 | if(sqlType.length() > 0) { 108 | sqlFieldType = new SQLFieldType(sqlType.sqlFiledType(), sqlType.length()); 109 | }else { 110 | sqlFieldType = new SQLFieldType(SQLFieldTypeEnum.TEXT, null); 111 | } 112 | } else { 113 | sqlFieldType = new SQLFieldType(SQLFieldTypeEnum.TEXT, null); 114 | } 115 | } 116 | } 117 | 118 | return sqlFieldType; 119 | } 120 | 121 | @Override 122 | public boolean isType(Field field) { 123 | return true; 124 | } 125 | 126 | @Override 127 | public Object toObj(Field field, Object sqlValue) { 128 | Operate operate = field.getAnnotation(Operate.class); 129 | Class typeClass = field.getType(); 130 | Object object = null; 131 | if(sqlValue == null){ 132 | return null; 133 | } 134 | if (operate != null) { 135 | String filedName = operate.valueName(); 136 | OperateEnum operateEnum = operate.operate(); 137 | if (operateEnum == OperateEnum.VALUE) { 138 | try { 139 | object = reflectHelper.getT(typeClass); 140 | Field targetField = typeClass.getDeclaredField(filedName); 141 | targetField.setAccessible(true); 142 | if (sqlValue != null && object != null) { 143 | targetField.set(object, sqlValue); 144 | } 145 | } catch (IllegalAccessException e) { 146 | e.printStackTrace(); 147 | } catch (InvocationTargetException e) { 148 | e.printStackTrace(); 149 | } catch (InstantiationException e) { 150 | e.printStackTrace(); 151 | } catch (NoSuchFieldException e) { 152 | e.printStackTrace(); 153 | } 154 | } else { 155 | object = jxFormat.toObject((String) sqlValue, typeClass); 156 | } 157 | } else { 158 | object = jxFormat.toObject((String) sqlValue, typeClass); 159 | } 160 | return object; 161 | } 162 | 163 | @Override 164 | public Object toValue(Field field, Object dataValue) { 165 | Operate operate = field.getAnnotation(Operate.class); 166 | Object value = null; 167 | Class typeClass = field.getType(); 168 | if(dataValue == null){ 169 | return null; 170 | } 171 | if (operate != null) { 172 | //先看转换类型的操作 173 | OperateEnum operateEnum = operate.operate(); 174 | if (operateEnum == OperateEnum.VALUE) { 175 | //仅仅存值 176 | String valueName = operate.valueName(); 177 | Field valueField = null; 178 | try { 179 | valueField = typeClass.getDeclaredField(valueName); 180 | valueField.setAccessible(true); 181 | } catch (NoSuchFieldException e) { 182 | e.printStackTrace(); 183 | } 184 | value = reflectHelper.getValue(dataValue, valueField); 185 | } else { 186 | //Json存储 187 | value = jxFormat.toJxString(dataValue); 188 | } 189 | } else { 190 | //Json存储 191 | value = jxFormat.toJxString(dataValue); 192 | } 193 | return value; 194 | } 195 | } 196 | -------------------------------------------------------------------------------- /dhcsqlitelibrary/src/main/java/com/ellen/dhcsqlitelibrary/table/type/TypeSupport.java: -------------------------------------------------------------------------------- 1 | package com.ellen.dhcsqlitelibrary.table.type; 2 | 3 | import com.ellen.sqlitecreate.createsql.helper.SQLFieldType; 4 | 5 | import java.lang.reflect.Field; 6 | 7 | /** 8 | * 9 | * @param T为您要拦截的类型,E为数据库中保存的类型 10 | * @param 11 | */ 12 | public interface TypeSupport { 13 | 14 | /** 15 | * 设置该属性映射到数据库中的字段名字 16 | * @param field 17 | * @return 18 | */ 19 | String setSqlFieldName(Field field); 20 | 21 | /** 22 | * 设置类型在数据库中的存储类型 23 | * @param field 24 | * @return 25 | */ 26 | SQLFieldType setSQLiteType(Field field); 27 | 28 | /** 29 | * 判断是否为该类型 30 | * 31 | * @return 32 | */ 33 | boolean isType(Field field); 34 | 35 | /** 36 | * 从数据库获取的值中恢复 37 | * 38 | * @param sqlValue 39 | * @return 40 | */ 41 | T toObj(Field field, E sqlValue); 42 | 43 | /** 44 | * 类型映射为数据库能存储的值 45 | * 46 | * @param dataValue 47 | * @return 48 | */ 49 | E toValue(Field field, T dataValue); 50 | } 51 | -------------------------------------------------------------------------------- /dhcsqlitelibrary/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | dhcsqlitelibrary 3 | 4 | -------------------------------------------------------------------------------- /dhcsqlitelibrary/src/test/java/com/ellen/dhcsqlitelibrary/ExampleUnitTest.java: -------------------------------------------------------------------------------- 1 | package com.ellen.dhcsqlitelibrary; 2 | 3 | import org.junit.Test; 4 | 5 | import static org.junit.Assert.*; 6 | 7 | /** 8 | * Example local unit test, which will execute on the development machine (host). 9 | * 10 | * @see Testing documentation 11 | */ 12 | public class ExampleUnitTest { 13 | @Test 14 | public void addition_isCorrect() { 15 | assertEquals(4, 2 + 2); 16 | } 17 | } -------------------------------------------------------------------------------- /gradle.properties: -------------------------------------------------------------------------------- 1 | # Project-wide Gradle settings. 2 | # IDE (e.g. Android Studio) users: 3 | # Gradle settings configured through the IDE *will override* 4 | # any settings specified in this file. 5 | # For more details on how to configure your build environment visit 6 | # http://www.gradle.org/docs/current/userguide/build_environment.html 7 | # Specifies the JVM arguments used for the daemon process. 8 | # The setting is particularly useful for tweaking memory settings. 9 | org.gradle.jvmargs=-Xmx1536m 10 | # When configured, Gradle will run in incubating parallel mode. 11 | # This option should only be used with decoupled projects. More details, visit 12 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects 13 | # org.gradle.parallel=true 14 | # AndroidX package structure to make it clearer which packages are bundled with the 15 | # Android operating system, and which are packaged with your app's APK 16 | # https://developer.android.com/topic/libraries/support-library/androidx-rn 17 | android.useAndroidX=true 18 | # Automatically convert third-party libraries to use AndroidX 19 | android.enableJetifier=true 20 | 21 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ellen2018/DhcSQLite/7eb8704053f0a54a142cec3b8bcca186bec50828/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Wed Jul 01 19:08:13 CST 2020 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=https\://services.gradle.org/distributions/gradle-6.1.1-all.zip 7 | -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env sh 2 | 3 | ############################################################################## 4 | ## 5 | ## Gradle start up script for UN*X 6 | ## 7 | ############################################################################## 8 | 9 | # Attempt to set APP_HOME 10 | # Resolve links: $0 may be a link 11 | PRG="$0" 12 | # Need this for relative symlinks. 13 | while [ -h "$PRG" ] ; do 14 | ls=`ls -ld "$PRG"` 15 | link=`expr "$ls" : '.*-> \(.*\)$'` 16 | if expr "$link" : '/.*' > /dev/null; then 17 | PRG="$link" 18 | else 19 | PRG=`dirname "$PRG"`"/$link" 20 | fi 21 | done 22 | SAVED="`pwd`" 23 | cd "`dirname \"$PRG\"`/" >/dev/null 24 | APP_HOME="`pwd -P`" 25 | cd "$SAVED" >/dev/null 26 | 27 | APP_NAME="Gradle" 28 | APP_BASE_NAME=`basename "$0"` 29 | 30 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 31 | DEFAULT_JVM_OPTS="" 32 | 33 | # Use the maximum available, or set MAX_FD != -1 to use that value. 34 | MAX_FD="maximum" 35 | 36 | warn () { 37 | echo "$*" 38 | } 39 | 40 | die () { 41 | echo 42 | echo "$*" 43 | echo 44 | exit 1 45 | } 46 | 47 | # OS specific support (must be 'true' or 'false'). 48 | cygwin=false 49 | msys=false 50 | darwin=false 51 | nonstop=false 52 | case "`uname`" in 53 | CYGWIN* ) 54 | cygwin=true 55 | ;; 56 | Darwin* ) 57 | darwin=true 58 | ;; 59 | MINGW* ) 60 | msys=true 61 | ;; 62 | NONSTOP* ) 63 | nonstop=true 64 | ;; 65 | esac 66 | 67 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 68 | 69 | # Determine the Java command to use to start the JVM. 70 | if [ -n "$JAVA_HOME" ] ; then 71 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 72 | # IBM's JDK on AIX uses strange locations for the executables 73 | JAVACMD="$JAVA_HOME/jre/sh/java" 74 | else 75 | JAVACMD="$JAVA_HOME/bin/java" 76 | fi 77 | if [ ! -x "$JAVACMD" ] ; then 78 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 79 | 80 | Please set the JAVA_HOME variable in your environment to match the 81 | location of your Java installation." 82 | fi 83 | else 84 | JAVACMD="java" 85 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 86 | 87 | Please set the JAVA_HOME variable in your environment to match the 88 | location of your Java installation." 89 | fi 90 | 91 | # Increase the maximum file descriptors if we can. 92 | if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then 93 | MAX_FD_LIMIT=`ulimit -H -n` 94 | if [ $? -eq 0 ] ; then 95 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 96 | MAX_FD="$MAX_FD_LIMIT" 97 | fi 98 | ulimit -n $MAX_FD 99 | if [ $? -ne 0 ] ; then 100 | warn "Could not set maximum file descriptor limit: $MAX_FD" 101 | fi 102 | else 103 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 104 | fi 105 | fi 106 | 107 | # For Darwin, add options to specify how the application appears in the dock 108 | if $darwin; then 109 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 110 | fi 111 | 112 | # For Cygwin, switch paths to Windows format before running java 113 | if $cygwin ; then 114 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 115 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 116 | JAVACMD=`cygpath --unix "$JAVACMD"` 117 | 118 | # We build the pattern for arguments to be converted via cygpath 119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 120 | SEP="" 121 | for dir in $ROOTDIRSRAW ; do 122 | ROOTDIRS="$ROOTDIRS$SEP$dir" 123 | SEP="|" 124 | done 125 | OURCYGPATTERN="(^($ROOTDIRS))" 126 | # Add a user-defined pattern to the cygpath arguments 127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 129 | fi 130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 131 | i=0 132 | for arg in "$@" ; do 133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 135 | 136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 138 | else 139 | eval `echo args$i`="\"$arg\"" 140 | fi 141 | i=$((i+1)) 142 | done 143 | case $i in 144 | (0) set -- ;; 145 | (1) set -- "$args0" ;; 146 | (2) set -- "$args0" "$args1" ;; 147 | (3) set -- "$args0" "$args1" "$args2" ;; 148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;; 149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 154 | esac 155 | fi 156 | 157 | # Escape application args 158 | save () { 159 | for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done 160 | echo " " 161 | } 162 | APP_ARGS=$(save "$@") 163 | 164 | # Collect all arguments for the java command, following the shell quoting and substitution rules 165 | eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS" 166 | 167 | # by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong 168 | if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then 169 | cd "$(dirname "$0")" 170 | fi 171 | 172 | exec "$JAVACMD" "$@" 173 | -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | set DIRNAME=%~dp0 12 | if "%DIRNAME%" == "" set DIRNAME=. 13 | set APP_BASE_NAME=%~n0 14 | set APP_HOME=%DIRNAME% 15 | 16 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 17 | set DEFAULT_JVM_OPTS= 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windows variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | 53 | :win9xME_args 54 | @rem Slurp the command line arguments. 55 | set CMD_LINE_ARGS= 56 | set _SKIP=2 57 | 58 | :win9xME_args_slurp 59 | if "x%~1" == "x" goto execute 60 | 61 | set CMD_LINE_ARGS=%* 62 | 63 | :execute 64 | @rem Setup the command line 65 | 66 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 67 | 68 | @rem Execute Gradle 69 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 70 | 71 | :end 72 | @rem End local scope for the variables with windows NT shell 73 | if "%ERRORLEVEL%"=="0" goto mainEnd 74 | 75 | :fail 76 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 77 | rem the _cmd.exe /c_ return code! 78 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 79 | exit /b 1 80 | 81 | :mainEnd 82 | if "%OS%"=="Windows_NT" endlocal 83 | 84 | :omega 85 | -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | include ':app', ':dhcsqlitelibrary' 2 | rootProject.name='DhcSQLite' 3 | --------------------------------------------------------------------------------