diff --git a/README.md b/README.md index d8978d4b0adb01c2012de9dda02ee9c4f884e893..6ad33fd82946a6ad8771f842e29293a74b4a2d90 100644 --- a/README.md +++ b/README.md @@ -35,8 +35,8 @@ # 包组件 - Annotations **注释包** - lang **常用或未分类的工具包** +- lang.net **网络工具包** - Porcessing **处理工具包** -- Porcessing.Interface **集成处理接口** - Porcessing.Sort **排序工具包** - Porcessing.Stop **中断工具包** - Porcessing.sql **数据库工具包** diff --git a/build.gradle b/build.gradle index 03ed5f8368b4f00f096a5244bba7ec6d46f2ded6..1a757f24bd759d84a5cff0735240f197db0a36a0 100644 --- a/build.gradle +++ b/build.gradle @@ -28,6 +28,6 @@ ext { junitVersion = '4.12' } dependencies { -// compile fileTree(dir: 'lib', includes: ['*.jar']) + compile fileTree(dir: 'lib', includes: ['*.jar']) // https://mvnrepository.com/artifact/org.jetbrains/annotations } diff --git a/jar/PDFLibrary.jar b/jar/PDFLibrary.jar index 9febb1de9a0e86e9eaa5176b6fb9bdfb16587e5a..478bc06712e26d569ff6ba30e407ed37cfcd7d25 100644 Binary files a/jar/PDFLibrary.jar and b/jar/PDFLibrary.jar differ diff --git a/src/main/java/fybug/nulll/pdfunctionlibrary/Annotations/ByteDef.java b/src/main/java/fybug/nulll/pdfunctionlibrary/Annotations/ByteDef.java index 0525986bc5b3dbf89af66967f7fec0b960876549..bac83ffd8a21665b7277353a798ed43339616ec1 100644 --- a/src/main/java/fybug/nulll/pdfunctionlibrary/Annotations/ByteDef.java +++ b/src/main/java/fybug/nulll/pdfunctionlibrary/Annotations/ByteDef.java @@ -5,7 +5,7 @@ import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; /** *
该注释表示该 变量/方法参数 应使用其中的常量
+ *该注释指定应该使用的值
* * @author fybug * @version 0.0.2 @@ -15,5 +15,6 @@ import java.lang.annotation.Target; @Target( {ElementType.ANNOTATION_TYPE} ) public @interface ByteDef { + /**应使用的常量.
*/ byte[] value() default {}; } diff --git a/src/main/java/fybug/nulll/pdfunctionlibrary/Annotations/CanSynchronized.java b/src/main/java/fybug/nulll/pdfunctionlibrary/Annotations/CanSynchronized.java index ad187f47e99dc41cc7aff8c2f9b995e10226dbd6..4d5a0b065668f911bc40c258f047d98ba3888353 100644 --- a/src/main/java/fybug/nulll/pdfunctionlibrary/Annotations/CanSynchronized.java +++ b/src/main/java/fybug/nulll/pdfunctionlibrary/Annotations/CanSynchronized.java @@ -5,11 +5,12 @@ import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; /** *使用该类注解类标识 该方法 或 该类下的方法 是否可用于并发
- *静态方法理应单独标记
*- * 代表标识范围的方法支持并发 + * 使用该类注解类标识 该方法 或 该类下的方法 是否可用于并发 + * + * 静态方法理应单独标记 *+ *
代表标识范围的方法支持并发
* * @author fybug * @version 0.0.2 diff --git a/src/main/java/fybug/nulll/pdfunctionlibrary/Annotations/IntDef.java b/src/main/java/fybug/nulll/pdfunctionlibrary/Annotations/IntDef.java index abff3a9cf8037752999446b73038ef7fd5af13b6..1a14ac4be4ddf20d9181273366def1447a5b3420 100644 --- a/src/main/java/fybug/nulll/pdfunctionlibrary/Annotations/IntDef.java +++ b/src/main/java/fybug/nulll/pdfunctionlibrary/Annotations/IntDef.java @@ -5,7 +5,7 @@ import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; /** *该注释表示该 变量/方法参数 应使用其中的常量
+ *该注释指定应该使用的值
* * @author fybug * @version 0.0.2 @@ -15,5 +15,6 @@ import java.lang.annotation.Target; @Target( {ElementType.ANNOTATION_TYPE} ) public @interface IntDef { + /**应使用的常量.
*/ int[] value() default {}; } diff --git a/src/main/java/fybug/nulll/pdfunctionlibrary/Annotations/NoSynchronized.java b/src/main/java/fybug/nulll/pdfunctionlibrary/Annotations/NoSynchronized.java index a58f0da0cef4d0646fd9063a9875b2e0f000dbb8..2647f72d338273a03b0ef67a45ee6ddd7a2e824b 100644 --- a/src/main/java/fybug/nulll/pdfunctionlibrary/Annotations/NoSynchronized.java +++ b/src/main/java/fybug/nulll/pdfunctionlibrary/Annotations/NoSynchronized.java @@ -5,11 +5,11 @@ import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; /** *使用该类注解类标识 该方法 或 该类下的方法 是否可用于并发
- *静态方法理应单独标记
*- * 代表标识范围的方法不支持并发 - *+ * 使用该类注解类标识 该方法 或 该类下的方法 是否可用于并发 + * + * 静态方法理应单独标记 + *
代表标识范围的方法不支持并发
* * @author fybug * @version 0.0.2 diff --git a/src/main/java/fybug/nulll/pdfunctionlibrary/Annotations/TestPackage.java b/src/main/java/fybug/nulll/pdfunctionlibrary/Annotations/TestPackage.java new file mode 100644 index 0000000000000000000000000000000000000000..bc4382cffc2acaf3ad568b5cfb1550a0793f6c2a --- /dev/null +++ b/src/main/java/fybug/nulll/pdfunctionlibrary/Annotations/TestPackage.java @@ -0,0 +1,19 @@ +package fybug.nulll.pdfunctionlibrary.Annotations; +import java.lang.annotation.Documented; +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; +/** + *用于标识该包是测试版本的包
+ * + * @author fybug + * @version 0.0.1 + * @since PDF 1.3 + */ +@Documented +@Retention( RetentionPolicy.SOURCE ) +@Target( {ElementType.PACKAGE} ) +public +@interface TestPackage {} \ No newline at end of file diff --git a/src/main/java/fybug/nulll/pdfunctionlibrary/Processing/FuckChar.java b/src/main/java/fybug/nulll/pdfunctionlibrary/Processing/FuckChar.java index 714cd5add33bbb214213b4ac62d252c62bb7c349..de917fadc3206c84526a7c7c2a2647521389e1d1 100644 --- a/src/main/java/fybug/nulll/pdfunctionlibrary/Processing/FuckChar.java +++ b/src/main/java/fybug/nulll/pdfunctionlibrary/Processing/FuckChar.java @@ -1,159 +1,152 @@ package fybug.nulll.pdfunctionlibrary.Processing; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import java.lang.ref.SoftReference; +import java.util.HashMap; + +import static fybug.nulll.pdfunctionlibrary.Processing.sql.Sql.ANDPR; +import static fybug.nulll.pdfunctionlibrary.Processing.sql.Sql.ANNOTATION; +import static fybug.nulll.pdfunctionlibrary.Processing.sql.Sql.ENDMARK; +import static fybug.nulll.pdfunctionlibrary.Processing.sql.Sql.GREATER; +import static fybug.nulll.pdfunctionlibrary.Processing.sql.Sql.LESS; +import static fybug.nulll.pdfunctionlibrary.Processing.sql.Sql.NAME; +import static fybug.nulll.pdfunctionlibrary.Processing.sql.Sql.PERCENTAGE; +import static fybug.nulll.pdfunctionlibrary.Processing.sql.Sql.ROD; +import static fybug.nulll.pdfunctionlibrary.Processing.sql.Sql.STR; +import static fybug.nulll.pdfunctionlibrary.lang.net.bdpos.BDPos.CLASS; +import static fybug.nulll.pdfunctionlibrary.lang.net.bdpos.BDPos.NAMEMARK; +import static fybug.nulll.pdfunctionlibrary.lang.net.bdpos.BDPos.RN; /** *- * 提供各种条件下的字符过滤 - *+ *
提供各种条件下的字符过滤
* * @author fybug - * @version 0.0.2 - * @since PDF 1.2 expander 2 + * @version 0.0.4 + * @since PDF 1.3 */ @SuppressWarnings( "all" ) public final class FuckChar { - @NotNull public final static char[] KEYTAB_TO_AT = {'@', '0', '2'}, KEYTAB_TO_AND = - {'@', '0', '1'}, KEYTAB_TO_EQUAL = {'@', '0', '0'}; - @NotNull public final static char KEYTAB_AT = '@', KEYTAB_EQUAL = '=', KEYTAB_AND = '&'; + /**过滤表.
*/ + @NotNull private static SoftReference<@Nullable HashMapFuck Keytab KeyWord.
- *
- * {@code
- * = -> @00
- * & -> @01
- * @ -> @02}
- *
+ * 过滤 Sql 语句.
* - * @param c 要过滤的字符集合 + * @param str 要过滤的数据 * - * @return 过滤后的字符 + * @return 过滤后的数据 */ @NotNull public static - String fuckKeytab(@NotNull final CharSequence c) { - @NotNull final StringBuilder s = new StringBuilder(c); - char firstchar; // 当前解析的字符 - @NotNull char[] adds; - - check: - for ( int mark = 0, lenght = s.length(); mark < lenght; ){ - // 检查第一个字符 - firstchar = s.charAt(mark); - - ck: - { - switch ( firstchar ) { - case KEYTAB_AT: - adds = KEYTAB_TO_AT; - break; - case KEYTAB_EQUAL: - adds = KEYTAB_TO_EQUAL; - break; - case KEYTAB_AND: - adds = KEYTAB_TO_AND; - break; - default: - break ck; - } - s.deleteCharAt(mark); - - // 检查位置 - if (mark < lenght - 1) { - s.insert(mark, adds); - mark += 3; - lenght += 2; - continue check; - } else { - /* 到达尾部 */ - s.append(adds); - break check; - } - } - - mark++; - } - - firstchar = 0; - s.trimToSize(); + String fuckSql(@NotNull final CharSequence s) { + @NotNull String str = s.toString(); + @NotNull final HashMap恢复被过滤 Sql 语句.
+ * + * @param str 要恢复的数据 + * + * @return 恢复后的数据 + */ + @NotNull + public static + String refuckSql(@NotNull final CharSequence s) { + @NotNull String str = s.toString(); + @NotNull final HashMapRestore Keytab KeyWord.
- *恢复{@link #fuckKeytab(CharSequence)} 处理过的字符
- *
- * {@code
- * @00 -> =
- * @01 -> &
- * @02 -> @}
- *
+ * 过滤 BDPos 标识符.
* - * @param c 要恢复的字符集合 + * @param s 要过滤的数据 * - * @return 恢复后的字符 + * @return 过滤后的数据 + */ + @NotNull + public static + String fuckBDPos(@NotNull final CharSequence s) { + @NotNull String str = s.toString(); + @NotNull final HashMap恢复过滤的 BDPos 标识符.
* - * @see #fuckKeytab(CharSequence) + * @param s 要恢复的数据 + * + * @return 恢复后的数据 */ @NotNull public static - String rollbackKeytab(@NotNull final CharSequence c) { - @NotNull final StringBuilder s = new StringBuilder(c); - @NotNull char[] checkNow = {'@', 0, 0}; // 标识缓存 - char firstchar; // 当前解析的字符 - - check: - for ( int mark = 0, lenght = s.length() - 2; mark < lenght; ){ - firstchar = s.charAt(mark); - - sw: - // 检查 - switch ( firstchar ) { - case KEYTAB_AT: - checkNow[1] = s.charAt(mark + 1); - checkNow[2] = s.charAt(mark + 2); - - // 检查基本标识 - switch ( checkNow[1] ) { - case '0': - break; - default: - break sw; - } - - // 检查标识 - switch ( checkNow[2] ) { - case '0': - s.setCharAt(mark, KEYTAB_EQUAL); - break; - case '1': - s.setCharAt(mark, KEYTAB_AND); - break; - case '2': - s.setCharAt(mark, KEYTAB_AT); - break; - default: - break sw; - } - s.delete(mark + 1, mark + 3); - - lenght -= 2; - } - - mark++; - } + String refuckBDPos(@NotNull final CharSequence s) { + @NotNull String str = s.toString(); + @NotNull final HashMap对文本进行排序.
- *使用{@link #UP}模式来排序
- * - * @param arrayList 即将进行排序的字符集合 - * @param对字符集合进行排序.
*排序不会影响传入的对象.
@@ -172,21 +156,6 @@ class SelectionSort { return arrayList; } - /** - *对文本进行排序.
- *使用{@link #UP}模式来排序
- * - * @param s 即将进行排序的文本 - * - * @return 已排序的文本 - * - * @see SelectionSort#sortString(String[], int) - */ - @NotNull - public static - String[] sortString(@Nullable final String[] s) - { return sortString(s, DOWN); } - /** *对文本数组进行排序.
*排序不会影响传入的对象.
@@ -287,26 +256,6 @@ class SelectionSort { return arrayList; } - /** - *对文件列表进行排序.
- *采用{@link #MODE_DEFAULT}和{@link #DOWN}模式排序
- * - *- * 排序方式: - * 逐字符对比,char编码较大的在上面 - *- * - * @param f 即将进行排序的文件表 - * - * @return 返回被排序好的文件列表 - * - * @see #sortFile(File[], int, int) - */ - @NotNull - public static - File[] sortFile(@Nullable final File[] f) - { return sortFile(f, MODE_DEFAULT, DOWN); } - /** *
对文件列表进行排序.
* @@ -355,7 +304,7 @@ class SelectionSort { } /* 分开文件/文件夹 */ - for ( int i = 0; i < f.length; i++ ) + for ( int i = 0, length = f.length; i < length; i++ ) separationFile(dirs, files, f[i]); /* 用选择排序对其进行排序 */ @@ -375,28 +324,6 @@ class SelectionSort { return file[0].toArray(new File[file[0].size()]); } - /** - *对文件列表进行排序.
- *采用{@link #MODE_DEFAULT}和{@link #DOWN}模式排序
- * - *- * 排序方式: - * 逐字符对比,char编码较大的在上面 - *- * - * @param f 即将进行排序的文件表 - * - * @return 返回被排序好的文件列表 - * - * @throws NullPointerException 参数为空 - * @see #sortFile(List
转化为数组后用{@link #sortFile(File[], int, int)}进行排序.
* @@ -459,12 +386,15 @@ class SelectionSort { return file[0]; } + /** + * 排序文件 + */ private static void sortFile(@SortMode int sort, LinkedList检查是否所有数据都相同.
+ *+ * 检查是否以检查到最大可检查的长度 + * 如果是,则根据排序方式 按照数据长度 进行额外排序 + *+ * + * @param sort 排序方式 + * @param index 当前标记位置 + * @param nowLeight 当前解析的长度 + * @param i1 第一个字符串的长度 + * @param i2 第二个字符串的长度 + * @param stringLenght 最大对比的长度 + * @param s 当前检查位置 * - * @return 应使用的索引 + * @return 检查后应使用的位置 */ private static int checkIndex(@SortMode final int sort, int index, final int nowLeight, final int i1, final int i2, final int stringLenght, final int s) { + // 检查是否已对比完成 if (nowLeight == stringLenght) { + // 按照排序方式进行额外排序 switch ( sort ) { case DOWN: if (i1 <= i2) diff --git a/src/main/java/fybug/nulll/pdfunctionlibrary/Processing/sql/Enum/HowWhere.java b/src/main/java/fybug/nulll/pdfunctionlibrary/Processing/sql/Enum/Comparison.java similarity index 53% rename from src/main/java/fybug/nulll/pdfunctionlibrary/Processing/sql/Enum/HowWhere.java rename to src/main/java/fybug/nulll/pdfunctionlibrary/Processing/sql/Enum/Comparison.java index 754e450292e86a28a81cc3e2413e05098195a12b..34058b5e3518a53f605563dad7fde4627e45ff80 100644 --- a/src/main/java/fybug/nulll/pdfunctionlibrary/Processing/sql/Enum/HowWhere.java +++ b/src/main/java/fybug/nulll/pdfunctionlibrary/Processing/sql/Enum/Comparison.java @@ -1,23 +1,34 @@ package fybug.nulll.pdfunctionlibrary.Processing.sql.Enum; - import com.sun.istack.internal.NotNull; + +import org.jetbrains.annotations.Nullable; /** - *
等于.
*/ + EQULE(" = "), + /**大于.
*/ MORE(" > "), + /**小于.
*/ LESS(" < "), + /**大于等于.
*/ MORE_AND(" >= "), + /**小于等于.
*/ LESS_AND(" <= "), - NO(" <> "); + /**不等于.
*/ + NO(" <> "), + /**严格检查.
*/ + STRICT(" <=> "); @NotNull final private String s; - HowWhere(@NotNull final CharSequence s) {this.s = s.toString();} + Comparison(@NotNull final CharSequence s) {this.s = s.toString();} @Override public @@ -30,10 +41,13 @@ enum HowWhere { * * @return 转化成的枚举 */ + @Nullable public final - HowWhere getMde(@NotNull final CharSequence ss) { - @NotNull final String s = ss.toString(); + Comparison getMde(@NotNull final CharSequence ss) { + @NotNull final String s = String.valueOf(ss).trim(); switch ( s ) { + case "=": + return EQULE; case ">": return MORE; case "<": @@ -43,7 +57,10 @@ enum HowWhere { case "<=": return LESS_AND; case "<>": + case "!=": return NO; + case "<=>": + return STRICT; default: return null; } diff --git a/src/main/java/fybug/nulll/pdfunctionlibrary/Processing/sql/Enum/InserMode.java b/src/main/java/fybug/nulll/pdfunctionlibrary/Processing/sql/Enum/InserMode.java new file mode 100644 index 0000000000000000000000000000000000000000..01c4af536b0c295dbd4aee2ef9adf915441f8461 --- /dev/null +++ b/src/main/java/fybug/nulll/pdfunctionlibrary/Processing/sql/Enum/InserMode.java @@ -0,0 +1,31 @@ +package fybug.nulll.pdfunctionlibrary.Processing.sql.Enum; +/** + *指定了当前数据的插入方式和内容
+ * 所有键都需要包装和过滤 + * + * @author fybug + * @version 0.0.1 + * @since PDF 1.3 + */ +@SuppressWarnings( "all" ) +public +enum InserMode { + /** + *插入 Null 值.
+ * 后面不需要值 + */ + NULL, + /**插入一个值.
*/ + VAL, + /** + *插入一个值.
+ * 不提供包装 + */ + OUT, + /** + *使用赋值 sql 语句.
+ * 不过滤 + */ + SQL; +} diff --git a/src/main/java/fybug/nulll/pdfunctionlibrary/Processing/sql/Enum/Relation.java b/src/main/java/fybug/nulll/pdfunctionlibrary/Processing/sql/Enum/Logical.java similarity index 54% rename from src/main/java/fybug/nulll/pdfunctionlibrary/Processing/sql/Enum/Relation.java rename to src/main/java/fybug/nulll/pdfunctionlibrary/Processing/sql/Enum/Logical.java index 76b2989ceae415da38a7cc3d670b45546078e4c4..6a397021f4acd70aede24aaef197288588b8b8f6 100644 --- a/src/main/java/fybug/nulll/pdfunctionlibrary/Processing/sql/Enum/Relation.java +++ b/src/main/java/fybug/nulll/pdfunctionlibrary/Processing/sql/Enum/Logical.java @@ -1,21 +1,29 @@ package fybug.nulll.pdfunctionlibrary.Processing.sql.Enum; import com.sun.istack.internal.NotNull; + +import org.jetbrains.annotations.Nullable; /** - *并且.
*/ AND(" AND "), - OR(" OR "); + /**或者.
*/ + OR(" OR "), + /**异或.
*/ + XOR(" XOR "); @NotNull final private String s; - Relation(@NotNull final CharSequence s) {this.s = s.toString();} + Logical(@NotNull final CharSequence s) {this.s = s.toString();} @Override + @NotNull public String toString() { return s; } @@ -26,14 +34,17 @@ enum Relation { * * @return 转换后的枚举 */ + @Nullable public - Relation getRelation(CharSequence ss) { - @NotNull final String s = ss.toString().toUpperCase(); + Logical getRelation(@Nullable final CharSequence ss) { + @NotNull final String s = String.valueOf(ss).trim().toUpperCase(); switch ( s ) { case "AND": return AND; case "OR": return OR; + case "XOR": + return XOR; default: return null; } diff --git a/src/main/java/fybug/nulll/pdfunctionlibrary/Processing/sql/Enum/OrderByMode.java b/src/main/java/fybug/nulll/pdfunctionlibrary/Processing/sql/Enum/OrderByMode.java deleted file mode 100644 index 26360cf7d407adf65cf1343d1fd3b12fd49f6267..0000000000000000000000000000000000000000 --- a/src/main/java/fybug/nulll/pdfunctionlibrary/Processing/sql/Enum/OrderByMode.java +++ /dev/null @@ -1,34 +0,0 @@ -package fybug.nulll.pdfunctionlibrary.Processing.sql.Enum; -import org.jetbrains.annotations.NotNull; -/** - *转化字符串为排序方式枚举.
- * - * @param ss 要转化的字符串 - * - * @return 转化后的枚举 - */ - public static - OrderByMode getOrderByMode(CharSequence ss) { - @NotNull final String s = ss.toString().toUpperCase(); - switch ( s ) { - case "ASC": - return ASC; - case "DESC": - return DESC; - default: - return null; - } - } -} diff --git a/src/main/java/fybug/nulll/pdfunctionlibrary/Processing/sql/Enum/UpdataMode.java b/src/main/java/fybug/nulll/pdfunctionlibrary/Processing/sql/Enum/UpdataMode.java new file mode 100644 index 0000000000000000000000000000000000000000..f5d7ff52d5d0a257e8a4aef09c74635b5a3babfd --- /dev/null +++ b/src/main/java/fybug/nulll/pdfunctionlibrary/Processing/sql/Enum/UpdataMode.java @@ -0,0 +1,78 @@ +package fybug.nulll.pdfunctionlibrary.Processing.sql.Enum; +import org.jetbrains.annotations.NotNull; +/** + *指定了当前数据的更新方式和内容
+ * 所有键都需要包装和过滤 + * + * @author fybug + * @version 0.0.2 + * @since PDF 1.3 + */ +@SuppressWarnings( "all" ) +public +enum UpdataMode { + /** + *更新为 Null 值.
+ * 后面不需要值 + */ + NULL, + /**更新为一个值.
*/ + VAL, + /** + *插入一个值.
+ * 不提供包装 + */ + OUT, + /** + *使用赋值 sql 语句.
+ * 不过滤 + */ + SQL, + /** + *更新为自增.
+ * 参数为自增的值 + */ + UP("+"), + /** + *更新为自减.
+ * 参数为自减的值 + */ + DOWN("-"), + /** + *更新为自乘.
+ * 参数为自乘的值 + */ + MUL("*"), + /** + *更新为自除.
+ * 参数为自除的值 + */ + EXCE("/"), + /** + *更新为自取商.
+ */ + DIV(" DIV "), + /**更新为自取模.
*/ + MOD(" MOD "), + /**按位取或.
*/ + BITOR("|"), + /**按位取于.
*/ + BITAND("&"), + /**按位异或.
*/ + BITXOR("^"), + /**位左移.
*/ + BITLEFT("<<"), + /**位右移.
*/ + BITRIGHT(">>"); + @NotNull private String s; + + UpdataMode() { this(""); } + + UpdataMode(@NotNull final String s) { this.s = s; } + + @Override + @NotNull + public + String toString() { return s; } +} diff --git a/src/main/java/fybug/nulll/pdfunctionlibrary/Processing/sql/Enum/package-info.java b/src/main/java/fybug/nulll/pdfunctionlibrary/Processing/sql/Enum/package-info.java index 00dff268e76b2e0d44e585e79924c9f501711ee2..dc52388c75386ea5ef51910a4493397507e6546c 100644 --- a/src/main/java/fybug/nulll/pdfunctionlibrary/Processing/sql/Enum/package-info.java +++ b/src/main/java/fybug/nulll/pdfunctionlibrary/Processing/sql/Enum/package-info.java @@ -3,7 +3,7 @@ *用来代表 SQL 的相关状态标识
* * @author fybug - * @version 0.0.1 + * @version 0.0.2 * @since PDF 1.2 */ package fybug.nulll.pdfunctionlibrary.Processing.sql.Enum; \ No newline at end of file diff --git a/src/main/java/fybug/nulll/pdfunctionlibrary/Processing/sql/Mysql.java b/src/main/java/fybug/nulll/pdfunctionlibrary/Processing/sql/Mysql.java index d398ca3d96e5126e64726835c19d67e6779eaa48..125a7da96d12bfd67166573c511b749aa7017f48 100644 --- a/src/main/java/fybug/nulll/pdfunctionlibrary/Processing/sql/Mysql.java +++ b/src/main/java/fybug/nulll/pdfunctionlibrary/Processing/sql/Mysql.java @@ -4,18 +4,13 @@ import com.sun.istack.internal.Nullable; import java.sql.ResultSet; import java.sql.SQLException; -import java.util.Map; import fybug.nulll.pdfunctionlibrary.Annotations.CanSynchronized; -import fybug.nulll.pdfunctionlibrary.Processing.sql.Enum.OrderByMode; -import fybug.nulll.pdfunctionlibrary.Processing.sql.Util.QureySet; -import fybug.nulll.pdfunctionlibrary.Processing.sql.Util.sqlWhereBulid; -import fybug.nulll.pdfunctionlibrary.Util.DataMap; -import fybug.nulll.pdfunctionlibrary.Util.Map.MapFile; - -import static fybug.nulll.pdfunctionlibrary.Processing.sql.Util.sqlWhereBulid.getWhere; +import fybug.nulll.pdfunctionlibrary.Processing.CObject; +import fybug.nulll.pdfunctionlibrary.Processing.sql.mysql.MysqlSql; +import fybug.nulll.pdfunctionlibrary.Processing.sql.mysql.Updata; /** - ** 使用该工具类进行 Sql 操作 * 该工具会进行字段过滤和包装出 sql @@ -23,15 +18,15 @@ import static fybug.nulll.pdfunctionlibrary.Processing.sql.Util.sqlWhereBulid.ge ** * @author fybug - * @version 0.0.2 + * @version 0.0.4 * @see Sql - * @since PDF 1.2 + * @since PDF 1.3 */ @SuppressWarnings( "all" ) @CanSynchronized public class Mysql extends Sql { - // Mysql 连接名 + // mysql 连接名 @NotNull private static final String URL = "jdbc:mysql://"; // JDBC 驱动名 @NotNull private static final String JDBC_DRIVER = "com.mysql.cj.jdbc.Driver"; @@ -72,256 +67,58 @@ class Mysql extends Sql { pass)); } - // 增 - /** - *
插入数据到指定表中.
+ *执行 Mysql 语句.
+ *+ * 通过语句对象对查询方式进行包装 + * 并保证安全和完整性 + ** - * @param tabname 表名 - * @param field 要插入的数据,使用映射表,要按顺序 - */ - public - void insert(@NotNull String tabname, @NotNull final MapFile
插入数据到指定表中如有数据则会进行更新.
+ * @param sql 要执行的语句对象 * - * @param tabname 表名 - * @param field 要插入的数据,使用映射表,要按顺序 - * @param f 更新的字段 + * @return 是否发生异常 * - * @since PDF 1.3 + * @see MysqlSql */ + @Nullable public - void insertORupdate(@NotNull String tabname, - @NotNull final MapFile删除数据表中数据.
- * - * @param talename 表名 - * @param sqlWhereBulid 删除条件 - */ - public - void dele(@NotNull String talename, @NotNull final sqlWhereBulid sqlWhereBulid) - { - talename = "`" + fuckSql(talename) + "`"; - StringBuilder sql = new StringBuilder("DELETE FROM "); - sql.append(talename); - - getWhere(sqlWhereBulid, sql); - - try { - // 获取插入语句 - exe(sql.toString()); + exe(lock, sql.toString()); + return true; } catch ( Exception e ) { - throw new RuntimeException(e); + e.printStackTrace(); + return false; } finally { - sql.setLength(0); - sql.trimToSize(); + close(lock); } } - // 改 - /** *更新数据库内容.
*
* 通过传入的字段映射和查询条件对数据库中的数据进行更新
*
*
- * @param tabname 表名
- * @param f 更新的字段映射
- * @param whele 更新条件集合
+ * @param updata 更新语句对象
+ *
+ * @return 影响的行数
*
- * @return 是否执行成功
+ * @see Updata
*/
public
- void updata(@NotNull String tabname, @NotNull final Map查询数据库数据.
*
- * 通过传入的字段对数据库进行查询,可指定是否排序
+ * 查询功能使用直接 sql 进行
*
*
- * @param qureySet 查询参数集
+ * @param sql 查询的 sql
*
* @return 查询的结果, 如果为 {@code Null} 则查询失败
*/
@Nullable
public
- ResultSet qurey(@NotNull final QureySet qureySet)
+ ResultSet qurey(@NotNull final String sql)
{
- int size = qureySet.field.size(); // 总字段数量
- if (size == 0)
- return null;
-
@Nullable ResultSet set;
- @NotNull final StringBuilder sql = new StringBuilder(
- "SELECT " + (qureySet.distinct ? "DISTINCT " : ""));
+ @NotNull final CObject lock = new CObject();
try {
// 获取查询数据
- set = get(getQureySql(qureySet, sql));
+ set = get(lock, sql);
} catch ( Exception r ) {
throw new RuntimeException(r);
} finally {
- sql.setLength(0);
- sql.trimToSize();
}
- return set;
- }
-
- /**
- * 查询两个数据表并集的数据.
- *- * 通过传入的字段对数据库进行查询 - * 部分数据取第一个,部分数据取第二个 - *- * - * @param first 查询参数集 - * @param all 是否查询全部 - * @param nexxt 查询参数集 - * - * @return 查询的结果, 如果为 {@code Null} 则查询失败 - */ - @Nullable - public - ResultSet unionQurey(@NotNull final QureySet first, final boolean all, - @NotNull final QureySet nexxt) - { - int size = first.field.size() + nexxt.field.size(); // 总字段数量 - if (size == 0) - return null; - @Nullable ResultSet set = null; - @NotNull final StringBuilder sql = new StringBuilder("SELECT ").append( - (first.distinct ? "DISTINCT " : "")); - - try { - getQureySql(new QureySet(first.tabname, first.field, first.whele, first.distinct, null, - first.limit), sql); - sql.ensureCapacity(sql.length() + 11); - sql.append(" UNION ").append(all ? "ALL " : ""); - - // 获取查询数据 - set = get(getQureySql(nexxt, sql)); - } catch ( Exception e ) { - throw new RuntimeException(e); - } finally { - sql.setLength(0); - sql.trimToSize(); - } return set; } - - // 获取查询语句 - private - String getQureySql(@NotNull final QureySet qureySet, @NotNull final StringBuilder sql) { - boolean ids = false; - String tmp; - - for ( int i = 0; i < qureySet.field.size(); i++ ){ - tmp = fuckSql(qureySet.field.get(i)); - - /* 检查位置 */ - if (ids) { - sql.ensureCapacity(sql.length() + 2 + tmp.length()); - sql.append(", "); - } else { - sql.ensureCapacity(sql.length() + tmp.length()); - ids = true; - } - - sql.append("`").append(tmp).append("`"); - } - - sql.ensureCapacity(sql.length() + 6 + qureySet.tabname.length()); - - // 查询条件 - sql.append(" FROM ").append("`").append(fuckSql(qureySet.tabname)).append("`"); - getWhere(qureySet.whele, sql); - - // 排序 - if (qureySet.orderSet != null) { - ids = false; - - @Nullable DataMap
@@ -18,7 +20,7 @@ import fybug.nulll.pdfunctionlibrary.Annotations.CanSynchronized; ** * @author fybug - * @version 0.0.2 + * @version 0.0.3 * @see database * @since PDF 1.2 */ @@ -27,8 +29,8 @@ import fybug.nulll.pdfunctionlibrary.Annotations.CanSynchronized; public class Sql implements AutoCloseable { @NotNull protected Sql.database database; - @NotNull private Connection con; - @NotNull private Statement statement; + @NotNull private HashMap
使用语句查询数据.
* - * @param sql 查询语句 + * @param cObject 事务标识 + * @param sql 查询语句 * - * @return 返回的查询集合 - * - * @throws SQLException 驱动丢失或者已经有链接 + * @return 返回的查询集合,如果执行失败则会返回{@code null} */ @Nullable public - ResultSet get(@NotNull final CharSequence sql) throws SQLException { + ResultSet get(@Nullable final CObject cObject, @NotNull final CharSequence sql) + throws SQLException + { @Nullable ResultSet rs = null; + @NotNull Connection c = null; + @NotNull Statement s; - synchronized ( this ){ - if (con == null) - //getConnection()方法,连接数据库!! - con = DriverManager.getConnection(database.DB_URL, database.USER, database.PASS); - if (statement != null) - try { - statement.close(); - } catch ( SQLException e ) { - // fuck - } + if (!con.containsKey(cObject)) { + // getConnection()方法,连接数据库 + con.put(cObject, + DriverManager.getConnection(database.DB_URL, database.USER, database.PASS)); + } + c = con.get(cObject); - //创建statement类对象,用来执行SQL语句!! - statement = con.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY, - ResultSet.HOLD_CURSORS_OVER_COMMIT); - //ResultSet类,用来存放获取的结果集!! - rs = statement.executeQuery(sql.toString()); + if (!statement.containsKey(cObject)) { + statement.put(cObject, c.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, + ResultSet.CONCUR_READ_ONLY, + ResultSet.HOLD_CURSORS_OVER_COMMIT)); } + s = statement.get(cObject); + //ResultSet类,用来存放获取的结果集!! + rs = s.executeQuery(sql.toString()); return rs; } + /** + *执行更新语句.
+ * + * @param cObject 事务标识 + * @param sql 要执行的更新语句 + * + * @return 受影响的行数 + */ + public + int updata(@Nullable final CObject cObject, @NotNull final CharSequence sql) throws SQLException + { + @Nullable ResultSet rs = null; + @NotNull Connection c = null; + @NotNull Statement s; + + if (!con.containsKey(cObject)) { + // getConnection()方法,连接数据库 + con.put(cObject, + DriverManager.getConnection(database.DB_URL, database.USER, database.PASS)); + } + c = con.get(cObject); + + if (!statement.containsKey(cObject)) { + statement.put(cObject, + c.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY, + ResultSet.HOLD_CURSORS_OVER_COMMIT)); + } + s = statement.get(cObject); + + return s.executeUpdate(sql.toString()); + } + /** *执行语句.
* - * @param sql 查询语句 + * @param cObject 事务标识 + * @param sql 查询语句 * * @return 执行是否成功 - * - * @throws SQLException 驱动丢失或者已经有链接 */ public - void exe(@NotNull final CharSequence sql) throws SQLException { - synchronized ( this ){ - if (con == null) - //getConnection()方法,连接数据库!! - con = DriverManager.getConnection(database.DB_URL, database.USER, database.PASS); - if (statement != null) - try { - statement.close(); - } catch ( SQLException e ) { - } + boolean exe(@Nullable final CObject cObject, @NotNull final CharSequence sql) + throws SQLException + { + @Nullable ResultSet rs = null; + @NotNull Connection c = null; + @NotNull Statement s; + + if (!con.containsKey(cObject)) { + // getConnection()方法,连接数据库 + con.put(cObject, + DriverManager.getConnection(database.DB_URL, database.USER, database.PASS)); + } + c = con.get(cObject); - //创建statement类对象,用来执行SQL语句!! - statement = con.createStatement(); - statement.execute(sql.toString()); + if (!statement.containsKey(cObject)) { + statement.put(cObject, + c.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY, + ResultSet.HOLD_CURSORS_OVER_COMMIT)); } + s = statement.get(cObject); + + try { + s.execute(sql.toString()); + } catch ( SQLException e ) { + e.printStackTrace(); + return false; + } + return true; } /** - *过滤 Sql 语句.
- *- * 过滤了什么自己看源码 - *+ *
关闭对应的连接.
* - * @param str 要过滤的数据 + * @param cObject 连接标识 * - * @return 过滤后的数据 + * @since PDF 1.3 */ - @NotNull - public static - String fuckSql(@NotNull CharSequence s) { - String str = s.toString(); - str = str.replaceAll(";", ""); - str = str.replaceAll("&", "&"); - str = str.replaceAll("<", "<"); - str = str.replaceAll(">", ">"); - str = str.replaceAll("'", ""); - str = str.replaceAll("--", ""); - str = str.replaceAll("/", ""); - str = str.replaceAll("%", ""); - return str; + public + void close(@Nullable final CObject cObject) { + @Nullable final Connection c = con.remove(cObject); + if (c != null) + try { + c.commit(); + c.close(); + } catch ( Throwable e ) { + } + + @Nullable final Statement s = statement.remove(cObject); + if (s != null) + try { + s.close(); + } catch ( Throwable e ) { + } } + /** + *关闭所有连接.
+ * 并释放事务映射表 + */ @Override public void close() throws SQLException { synchronized ( this ){ - if (statement != null) + for ( Connection c : con.values() ){ try { - statement.close(); - } catch ( SQLException e ) { - // fuck + c.commit(); + c.close(); + } catch ( Throwable e ) { } + } + con.clear(); + con = null; + + for ( Statement s : statement.values() ){ + try { + s.close(); + } catch ( Throwable e ) { + } + } + statement.clear(); statement = null; - if (con != null) - con.close(); } database = null; } @@ -161,7 +223,7 @@ class Sql implements AutoCloseable { * * @author fybug * @version 0.0.1 - * @since JDK 1.8 + * @since PDF 1.2 */ public static class database implements Serializable { @@ -176,4 +238,8 @@ class Sql implements AutoCloseable { PASS = pass; } } + + /* 过滤字段 */ + public final static String ENDMARK = ";", ANDPR = "&", GREATER = "<", LESS = ">", STR = "'", + NAME = "`", ANNOTATION = "--", ROD = "/", PERCENTAGE = "%"; } diff --git a/src/main/java/fybug/nulll/pdfunctionlibrary/Processing/sql/Util/InsertVal.java b/src/main/java/fybug/nulll/pdfunctionlibrary/Processing/sql/Util/InsertVal.java new file mode 100644 index 0000000000000000000000000000000000000000..3f0f7c89f5ae944096df9a4119f20b18b16e0355 --- /dev/null +++ b/src/main/java/fybug/nulll/pdfunctionlibrary/Processing/sql/Util/InsertVal.java @@ -0,0 +1,183 @@ +package fybug.nulll.pdfunctionlibrary.Processing.sql.Util; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import java.lang.ref.SoftReference; + +import fybug.nulll.pdfunctionlibrary.Annotations.NoSynchronized; +import fybug.nulll.pdfunctionlibrary.Processing.sql.Enum.InserMode; +import fybug.nulll.pdfunctionlibrary.Util.DataMap; +import fybug.nulll.pdfunctionlibrary.Util.Map.ArrayMap; + +import static fybug.nulll.pdfunctionlibrary.Processing.FuckChar.fuckSql; +/** + *用于保存插入的参数和类型,并生成 {@code (??, ??) VALUE (??, ??)} 语句
+ * + * @author fybug + * @version 0.0.1 + * @see InserMode + * @since PDF 1.3 + */ +@NoSynchronized +@SuppressWarnings( "All" ) +public +class InsertVal extends ArrayMap插入空类型的指向缓存.
*/ + @NotNull private transient static SoftReference获取空类型指向的缓存.
+ * 使用软引用 + * + * @return {@link InserMode#NULL} + */ + @NotNull + protected + DataMap加入空插入.
+ * + * @param name 要插入的属性名 + * + * @return this + * + * @see InserMode#NULL + */ + @NotNull + public + InsertVal addNull(@NotNull final String name) { + add(fuckSql(name).trim(), getNullCache()); + return this; + } + + /** + *加入值插入.
+ * + * @param name 要插入的属性名 + * @param val 要插入的属性,为空则转化为空插入 + * + * @return this + * + * @see InserMode#VAL + */ + @NotNull + public + InsertVal addVal(@NotNull final String name, @Nullable final String val) { + add(fuckSql(name).trim(), + val == null ? getNullCache() : DataMap.getDataMap(InserMode.VAL, fuckSql(val).trim())); + return this; + } + + /** + *加入值插入.
+ * 插入非字符串的值,不提供包装 + * + * @param name 要插入的属性名 + * @param val 要插入的属性,为空则转化为空插入 + * + * @return this + * + * @see InserMode#OUT + */ + @NotNull + public + InsertVal addOut(@NotNull final String name, @Nullable final Object val) { + add(fuckSql(name).trim(), + val == null ? getNullCache() : DataMap.getDataMap(InserMode.OUT, val)); + return this; + } + + /** + *加入 sql 插入.
+ * + * @param name 要插入的属性名 + * @param sql 要插入的 sql 语句,为空则转化为空插入 + * + * @return this + * + * @see InserMode#SQL + */ + @NotNull + public + InsertVal addSql(@NotNull final String name, @Nullable final String sql) { + add(fuckSql(name).trim(), + sql == null ? getNullCache() : DataMap.getDataMap(InserMode.SQL, sql)); + return this; + } + + // 检查数据并加入 + private + void checkData(@NotNull final StringBuilder key, @NotNull final StringBuilder val, + @NotNull final DataMap转化为插入属性的语句.
+ * + * @return {@code (??) VALUE (??)} + */ + @NotNull + @Override + public + String toString() { + @NotNull final StringBuilder inst = new StringBuilder("("), val = new StringBuilder( + ") VALUE ("); + + /* tmp */ + @Nullable DataMap- * 用来存放 Sql 查询参数 - * 需传入 数据表,要查询的字段,条件集,是否去重复,排序方式集,是否只取前几条数据 - *- * - * @author fybug - * @version 0.0.2 - * @since PDF 1.2 - */ -public final -class QureySet { - public @NotNull String tabname; - public @NotNull List
构造查询参数集.
- * - * @param tabname 表名 - * @param field 要查询的字段 - * @param whele 查询条件 - * @param distinct 去重复 - * @param orderSet 排序模式,为{@code null} 则不排序 - * @param limit 查询头几条数据,小于{@code -1} 则不指定 - */ - public - QureySet(@NotNull String tabname, @NotNull final List用于保存更新的参数和类型,并生成 {@code ??=??, ??=??} 语句
+ * + * @author fybug + * @version 0.0.1 + * @see UpdataMode + * @since PDF 1.3 + */ +@NoSynchronized +@SuppressWarnings( "All" ) +public +class UpDataVal extends ArrayMap插入空类型的指向缓存.
*/ + @NotNull private transient static SoftReference获取空类型指向的缓存.
+ * 使用软引用 + * + * @return {@link UpdataMode#NULL} + */ + @NotNull + protected + DataMap检查字段类型,并进行过滤.
+ * + * @param o 要检查的字段 + * + * @return 检查并进行相应处理的字段 + */ + @NotNull + private + String checkFidle(@com.sun.istack.internal.Nullable final Object o) { + if (o instanceof String) + return '\'' + fuckSql(o.toString()).trim() + '\''; + + return fuckSql(String.valueOf(o)).trim(); + } + + /** + *更新为空值.
+ * + * @param name 要更新的属性名 + * + * @return this + * + * @see UpdataMode#NULL + */ + @NotNull + public + UpDataVal addNull(@NotNull final String name) { + add(fuckSql(name).trim(), getNullCache()); + return this; + } + + /** + *加入值更新.
+ * + * @param name 要更新的属性名 + * @param val 要更新的属性,为空则转化为空更新 + * + * @return this + * + * @see UpdataMode#VAL + */ + @NotNull + public + UpDataVal addVal(@NotNull final String name, @Nullable final String val) { + add(fuckSql(name).trim(), val == null ? getNullCache() + : DataMap.getDataMap(UpdataMode.VAL, '\'' + fuckSql(val).trim() + '\'')); + return this; + } + + /** + *加入值更新.
+ * 插入非字符串的值,不提供包装 + * + * @param name 要更新的属性名 + * @param val 要更新的属性,为空则转化为空更新 + * + * @return this + * + * @see UpdataMode#OUT + */ + @NotNull + public + UpDataVal addOut(@NotNull final String name, @Nullable final Object val) { + add(fuckSql(name).trim(), + val == null ? getNullCache() : DataMap.getDataMap(UpdataMode.OUT, checkFidle(val))); + return this; + } + + /** + * /** + *加入 sql 更新.
+ * + * @param name 要更新的属性名 + * @param sql 要更新的 sql 语句,为空则转化为空更新 + * + * @return this + * + * @see UpdataMode#SQL + */ + @NotNull + public + UpDataVal addSql(@NotNull final String name, @Nullable final String sql) { + add(fuckSql(name).trim(), + sql == null ? getNullCache() : DataMap.getDataMap(UpdataMode.SQL, sql)); + return this; + } + + /** + *加入自增.
+ * + * @param name 要更新的属性名 + * @param val 自增的量 + * + * @return this + * + * @see UpdataMode#UP + */ + @NotNull + public + UpDataVal addUp(@NotNull final String name, final Object val) { + add(fuckSql(name).trim(), DataMap.getDataMap(UpdataMode.UP, checkFidle(val))); + return this; + } + + /** + *加入自减.
+ * + * @param name 要更新的属性名 + * @param val 自减的量 + * + * @return this + * + * @see UpdataMode#DOWN + */ + @NotNull + public + UpDataVal addDown(@NotNull final String name, final Object val) { + add(fuckSql(name).trim(), DataMap.getDataMap(UpdataMode.DOWN, checkFidle(val))); + return this; + } + + /** + *加入自除.
+ * + * @param name 要更新的属性名 + * @param val 自除的量 + * + * @return this + * + * @see UpdataMode#EXCE + */ + @NotNull + public + UpDataVal addExce(@NotNull final String name, final Object val) { + add(fuckSql(name).trim(), DataMap.getDataMap(UpdataMode.EXCE, checkFidle(val))); + return this; + } + + /** + *加入取值.
+ * + * @param name 要更新的属性名 + * @param val 对应的属性 + * @param div 求商还是求模 + * + * @return this + * + * @see UpdataMode#DIV + * @see UpdataMode#MOD + */ + @NotNull + public + UpDataVal addExce(@NotNull final String name, final Object val, final boolean div) { + add(fuckSql(name).trim(), + DataMap.getDataMap(div ? UpdataMode.DIV : UpdataMode.MOD, checkFidle(val))); + return this; + } + + /** + *加入自乘.
+ * + * @param name 要更新的属性名 + * @param val 自乘的量 + * + * @return this + * + * @see UpdataMode#MUL + */ + @NotNull + public + UpDataVal addMul(@NotNull final String name, final Object val) { + add(fuckSql(name).trim(), DataMap.getDataMap(UpdataMode.MUL, checkFidle(val))); + return this; + } + + /** + *加入位运算.
+ * + * @param name 要更新的属性名 + * @param bit 运算方式 + * @param val 量 + * + * @return this + * + * @see UpdataMode + */ + @NotNull + public + UpDataVal addBit(@NotNull final String name, @NotNull final UpdataMode bit, final Object val) { + add(fuckSql(name).trim(), DataMap.getDataMap(bit, checkFidle(val))); + return this; + } + + // 检查数据并加入 + private + void checkData(@NotNull final StringBuilder key, + @NotNull final DataMap转化为更新属性的语句.
+ * + * @return {@code ??=??, ??=??} + */ + @NotNull + @Override + public + String toString() { + @NotNull final StringBuilder inst = new StringBuilder(); + + /* tmp */ + @Nullable DataMap+ * 存放 sql 查询条件的对象,使用字符串进行缓存 + * 可使用内部指定参数进行添加,将会过滤输入的值 + * 或直接添加 sql 语句,会接在当前语句后面,但不过滤 + *+ *
插入的语句展示符号说明: + * ( * ) 代表着使用一个枚举对象作为这里的参数,通常是固定的语句 + * [ * ] 代表着这个语句会根据你输入的参数决定是否出现,通常为取反语句 + * [ * | * ] 代表这这个语句会根据你输入的参数决定使用哪个参数 + * \(\) 代表这个不是上面的括号,和转义符一样 + * \[\] 同上 + * 大写代表 sql 语句 + * 小写代表 变量 + *+ * + * @author fybug + * @version 0.0.4 + * @see Logical + * @see Comparison + * @since PDF 1.3 + */ +@NoSynchronized +@SuppressWarnings( "All" ) +public +class WhereSet extends ConsistentField implements Cleanable, Serializable, Cloneable { + // 语句缓存 + @NotNull private StringBuilder set = new StringBuilder(); + + @Override + protected + boolean consistent(@Nullable final Object obj) + { return obj.toString().equals(set.toString()); } + + @Override + @NotNull + public + String toString() { + @NotNull final StringBuilder sql = new StringBuilder(); + + getWhere(this, sql); + + sql.trimToSize(); + return sql.toString(); + } + + @Override + protected + void finalize() throws Throwable { + super.finalize(); + set.setLength(0); + set.trimToSize(); + set = null; + } + + @NotNull + @Override + public + Object clone() throws CloneNotSupportedException { + @NotNull final WhereSet s = (WhereSet) super.clone(); + + set.trimToSize(); + s.set = new StringBuilder(set.toString()); + s.set.trimToSize(); + + return s; + } + + /* + * 释放 + */ + + @Override + public + void clean() { + set.setLength(0); + set.trimToSize(); + } + + @Override + public + void free() { clean(); } + + @Override + public + void close() { + free(); + set = null; + } + + /* + * check + */ + + /** + *
检查字段类型,并进行过滤.
+ * + * @param o 要检查的字段 + * + * @return 检查并进行相应处理的字段 + */ + @NotNull + private + String checkFidle(@Nullable final Object o) { + if (o instanceof String) + return '\'' + fuckSql(o.toString()).trim() + '\''; + + return fuckSql(String.valueOf(o)).trim(); + } + + // 加入关系 + @NotNull + private + WhereSet addRelation(@NotNull final Logical logical) { + if (set.length() > 0) + set.append(logical); + return this; + } + + /* + * Add + */ + + /** + *添加等值条件.
+ *
+ * 添加条件如下 {@code "( Logical ) key [ ! ]= val"}
+ * 即添加一个检查该键是否等于该值的条件
+ *
+ *
+ * @param logical 与前面条件的关系
+ * @param key 键
+ * @param val 要等于的值,如不是字符串类型则不会包装
+ * @param negation 是否取反,即使用 !=
+ *
+ * @return this
+ */
+ @NotNull
+ public
+ WhereSet addEqual(@NotNull final Logical logical, @NotNull String key,
+ @Nullable final Object val, final boolean negation)
+ {
+ // 过滤
+ key = '`' + fuckSql(key).trim() + '`';
+ @NotNull final String v = checkFidle(val);
+
+ // 放入 sql
+ addRelation(logical).set.ensureCapacity(set.length() + key.length() + 4 + v.length());
+ set.append(key).append(negation ? " !" : " ").append("= ").append(v);
+
+ return this;
+ }
+
+ /**
+ * 添加键值逻辑条件.
+ *
+ * 添加条件如下 {@code "( Logical ) key ( Comparison ) val"}
+ * 与等值条件不同的是,你可以指定键和值的逻辑运算符
+ *
+ *
+ * @param logical 与前面条件的关系
+ * @param key 键
+ * @param how 逻辑运算符
+ * @param val 要判断的值,如不是字符串则不会进行包装
+ *
+ * @return this
+ */
+ @NotNull
+ public
+ WhereSet addWhere(@NotNull final Logical logical, @NotNull String key,
+ @NotNull final Comparison how, @NotNull final Object val)
+ {
+ key = '`' + fuckSql(key).trim() + '`';
+ @NotNull final String v = checkFidle(val);
+
+ addRelation(logical).set.ensureCapacity(
+ set.length() + key.length() + how.toString().length() + v.length());
+ set.append(key).append(how).append(v);
+
+ return this;
+ }
+
+ /**
+ * 添加空检查.
+ *
+ * 添加条件如下 {@code "( Logical ) key IS [ NOT ] NULL"}
+ *
+ *
+ * @param logical 与前面的条件的关系
+ * @param key 要检查的键
+ * @param negation 取反
+ *
+ * @return this
+ */
+ @NotNull
+ public
+ WhereSet addNullCheck(@NotNull final Logical logical, @NotNull String key,
+ final boolean negation)
+ {
+ key = fuckSql(key).trim();
+
+ addRelation(logical).set.ensureCapacity(set.length() + 14 + key.length());
+ set.append("`")
+ .append(key)
+ .append("`")
+ .append(" IS")
+ .append(negation ? " NOT" : " ")
+ .append(" Null");
+
+ return this;
+ }
+
+ /**
+ * 添加范围检查.
+ *
+ * 添加条件如下 {@code "( Logical ) key [ NOT ] BETWEEN val AND val"}
+ *
+ *
+ * @param logical 与前面的条件的关系
+ * @param key 要检查的键
+ * @param num1 开始范围,如不是字符串则不会进行包装
+ * @param num2 结束范围,如不是字符串则不会进行包装
+ * @param negation 取反
+ *
+ * @return this
+ */
+ @NotNull
+ public
+ WhereSet addBetween(@NotNull final Logical logical, @NotNull String key, @NotNull Object num1,
+ @NotNull Object num2, final boolean negation)
+ {
+ key = '`' + fuckSql(key).trim() + '`';
+ num1 = checkFidle(num1);
+ num2 = checkFidle(num2);
+
+ addRelation(logical).set.ensureCapacity(
+ 17 + set.length() + key.length() + num1.toString().length() +
+ num2.toString().length());
+ set.append(key).append(negation ? "NOT " : " ").append("BETWEEN ").append(num1).append(
+ " AND ").append(num2);
+
+ return this;
+ }
+
+ /**
+ * 加入列举检查.
+ *
+ * 添加条件如下 {@code "( Logical ) key [ NOT ] IN (val, val)"}
+ *
+ *
+ * @param logical 与前面的条件的关系
+ * @param key 要检查的键
+ * @param negation 是否取反
+ * @param val 列举的值,如不是字符串则不会进行包装
+ *
+ * @return this
+ */
+ @NotNull
+ public
+ WhereSet addIn(@NotNull final Logical logical, @NotNull String key, final boolean negation,
+ @NotNull final Object... val)
+ {
+ key = '`' + fuckSql(key).trim() + '`';
+
+ addRelation(logical).set.ensureCapacity(set.length() + 10 + key.length());
+ set.append(key).append(negation ? " NOT IN " : " IN ").append("(");
+
+ @NotNull String v;
+ boolean f = true;
+
+ /* 加入列表组 */
+ for ( int i = 0, lenght = val.length; i < lenght; i++ ){
+ v = checkFidle(val[i]);
+
+ if (!f) {
+ set.ensureCapacity(v.length() + 2 + set.length());
+ set.append(", ");
+ } else {
+ set.ensureCapacity(set.length() + v.length());
+ f = false;
+ }
+
+ set.append(v);
+ }
+
+ set.append(")");
+ return this;
+ }
+
+ /**
+ * 加入模糊查询.
+ *
+ * 添加条件如下 {@code "( Logical ) key [ NOT ] LIKE val"}
+ *
+ *
+ * @param logical 与前面的条件的关系
+ * @param key 要检查的键
+ * @param val 条件,不支持除字符串以外的类型
+ * @param negation 是否取反
+ *
+ * @return this
+ */
+ @NotNull
+ public
+ WhereSet addLike(@NotNull final Logical logical, @NotNull String key, @NotNull final String val,
+ final boolean negation)
+ {
+ key = '`' + fuckSql(key).trim() + '`';
+
+ addRelation(logical).set.ensureCapacity(set.length() + key.length() + val.length() + 9);
+ set.append(key).append(negation ? "NOT " : " ").append("LIKE '").append(val).append('\'');
+
+ return this;
+ }
+
+ /**
+ * 加入正则查询.
+ *
+ * 添加条件如下 {@code "( Logical ) [ NOT ( ] key REGEXP val [ ) ]"}
+ *
+ *
+ * @param logical 与前面的条件的关系
+ * @param key 要检查的键
+ * @param val 正则,不支持除字符串以外的类型
+ * @param negation 是否取反
+ *
+ * @return this
+ */
+ @NotNull
+ public
+ WhereSet addRegexp(@NotNull final Logical logical, @NotNull String key,
+ @NotNull final String val, final boolean negation)
+ {
+ key = '`' + fuckSql(key).trim() + '`';
+
+ addRelation(logical).set.ensureCapacity(set.length() + key.length() + val.length() + 9);
+ set.append(negation ? "NOT (" : "")
+ .append(key)
+ .append("REGEXP '")
+ .append(val)
+ .append('\'')
+ .append(negation ? ")" : "");
+
+ return this;
+ }
+
+ /**
+ * 添加 Sql 语句.
+ *+ * 添加的语句在 Where 后,请不要添加条件语句以外的语句 + *+ * + * @param logical 与前面语句的关系 + * @param sql 要添加的 Sql 语句,不支持除字符串以外的类型,并且不进行过滤和包装 + * + * @return this + */ + @NotNull + public + WhereSet addSql(@NotNull final Logical logical, @NotNull final String sql) { + addRelation(logical).set.append(sql); + return this; + } + + /** + *
添加整合条件语句.
+ *
+ * 即使用 () 将语句包起来,可提高该串语句的优先度
+ * 添加条件如下 {@code "( Logical ) [ NOT ] \( ( WhereSet ) \)"}
+ *
+ *
+ * @param logical 与前面语句的关系
+ * @param whereSet 要插入的语句,不会进行过滤和包装
+ * @param negation 是否取反
+ *
+ * @return this
+ */
+ @NotNull
+ public
+ WhereSet addMoreSql(@NotNull final Logical logical, @NotNull final WhereSet whereSet,
+ final boolean negation)
+ {
+ @Nullable final String s = whereSet.get().trim(); // 语句缓存
+ if (s == null)
+ return this;
+
+ addRelation(logical).set.ensureCapacity(6 + set.length() + s.length());
+ set.append(negation ? "NOT " : " ").append("(").append(s).append(")");
+
+ return this;
+ }
+
+ /**
+ * 获取语句.
+ * + * @return 没参数为 {@code Null} + */ + @Nullable + private + String get() { + if (set.length() == 0) + return null; + return set.toString(); + } + + /** + *获取查询条件.
+ *+ * 获取条件集里面的条件并添加到传入的字符缓存区 + *+ * + * @param whele 要提取的条件集 + * @param sql 要添加的字符缓冲区 + */ + public static + void getWhere(@NotNull final WhereSet whele, @NotNull final StringBuilder sql) + { + // 条件语句 + @Nullable final String s = whele.get(); + if (s == null) + return; + + sql.ensureCapacity(7 + s.length() + sql.length()); + sql.append(" WHERE ").append(s); + } +} diff --git a/src/main/java/fybug/nulll/pdfunctionlibrary/Processing/sql/Util/orderSet.java b/src/main/java/fybug/nulll/pdfunctionlibrary/Processing/sql/Util/orderSet.java deleted file mode 100644 index d6ddf3f26de2bf1386e1acf1b308a5ca95018720..0000000000000000000000000000000000000000 --- a/src/main/java/fybug/nulll/pdfunctionlibrary/Processing/sql/Util/orderSet.java +++ /dev/null @@ -1,58 +0,0 @@ -package fybug.nulll.pdfunctionlibrary.Processing.sql.Util; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; - -import fybug.nulll.pdfunctionlibrary.Processing.sql.Enum.OrderByMode; -import fybug.nulll.pdfunctionlibrary.Util.DataMap; -import fybug.nulll.pdfunctionlibrary.Util.Map.MapFile; -/** - *
- * 用来存放 Sql 语句查询的排序参数集合
- * 使用键值存放,{@code 属性名 -> 排序方式}
- *
- *
- * @author fybug
- * @version 0.0.3
- * @see MapFile
- * @see OrderByMode
- * @since PDF 1.2
- */
-@SuppressWarnings( "All" )
-public
-class orderSet {
- @NotNull private MapFile添加参数.
*/ - @NotNull - public - orderSet add(@NotNull final CharSequence key, @Nullable final OrderByMode how) - { - set.add(key, how); - return this; - } - - /** - *获取参数.
- * - * @return 没参数为 {@code Null} - */ - @Nullable - public - DataMap提供 Sql 执行辅助工具+ *
提供 Sql 执行辅助对象* * @author fybug - * @version 0.0.1 + * @version 0.0.2 * @since PDF 1.2 */ package fybug.nulll.pdfunctionlibrary.Processing.sql.Util; \ No newline at end of file diff --git a/src/main/java/fybug/nulll/pdfunctionlibrary/Processing/sql/Util/sqlWhereBulid.java b/src/main/java/fybug/nulll/pdfunctionlibrary/Processing/sql/Util/sqlWhereBulid.java deleted file mode 100644 index e323d3bf8483bb55e9b99b4a735ff707cbc0d579..0000000000000000000000000000000000000000 --- a/src/main/java/fybug/nulll/pdfunctionlibrary/Processing/sql/Util/sqlWhereBulid.java +++ /dev/null @@ -1,1038 +0,0 @@ -package fybug.nulll.pdfunctionlibrary.Processing.sql.Util; -import com.sun.istack.internal.NotNull; -import com.sun.istack.internal.Nullable; - -import fybug.nulll.pdfunctionlibrary.Processing.sql.Enum.HowWhere; -import fybug.nulll.pdfunctionlibrary.Processing.sql.Enum.Relation; -import fybug.nulll.pdfunctionlibrary.lang.Cleanable; -import fybug.nulll.pdfunctionlibrary.lang.ConsistentField; - -import static fybug.nulll.pdfunctionlibrary.Processing.sql.Sql.fuckSql; -/** - *
- * 用来生成 Sql 语句的条件的生成器 - * 可使用内部的便捷方式添加条件,或添加自定义 Sql - *- * - * @author fybug - * @version 0.0.2 - * @see Relation - * @see HowWhere - * @since PDF 1.2 - */ -@SuppressWarnings( "All" ) -public -class sqlWhereBulid extends ConsistentField implements Cleanable { - // 语句缓存 - @NotNull private StringBuilder set = new StringBuilder(); - - @Override - public - int hashCode() - { return (super.hashCode() + get().hashCode()) << 5; } - - @Override - protected - boolean consistent(@Nullable final Object obj) - { return obj.toString().equals(set.toString()); } - - @Override - @NotNull - public - String toString() { return get(); } - - @Override - protected - void finalize() throws Throwable { - super.finalize(); - set.setLength(0); - set.trimToSize(); - set = null; - } - - @Override - public - Object clone() throws CloneNotSupportedException { - sqlWhereBulid s = (sqlWhereBulid) super.clone(); - s.set = new StringBuilder(set.toString()); - return s; - } - - @Override - public - void clean() { - set.setLength(0); - set.trimToSize(); - } - - @Override - public - void free() { clean(); } - - @Override - public - void close() { - free(); - set = null; - } - - /* - * AddEqual - */ - - /** - *
添加等值条件.
- *
- * 添加条件如下 {@code "AND key [ ! ]= val"}
- * 即添加一个检查该键是否等于该值的条件
- *
- *
- * @param key 键
- * @param val 要等于的值
- * @param negation 是否取反,即使用 !=
- */
- @NotNull
- public
- sqlWhereBulid addEqual(@NotNull final String key, @NotNull final String val,
- final boolean negation)
- {return addEqual(Relation.AND, key, val, negation);}
-
- /**
- * 添加等值条件.
- *
- * 添加条件如下 {@code "( AND | OR ) key [ ! ]= val"}
- * 即添加一个检查该键是否等于该值的条件
- *
- *
- * @param relation 与前面条件的关系
- * @param key 键
- * @param val 要等于的值
- * @param negation 是否取反,即使用 !=
- */
- @NotNull
- public
- sqlWhereBulid addEqual(@NotNull final Relation relation, @NotNull String key,
- @NotNull String val, final boolean negation)
- {
- key = fuckSql(key);
- val = fuckSql(val);
-
- addRelation(relation).set.ensureCapacity(set.length() + key.length() + 8 + val.length());
- set.append("`").append(key).append("` ").append((negation ? "!" : "")).append("= ").append(
- "'").append(val).append("'");
- return this;
- }
-
- /**
- * 添加等值条件.
- *
- * 添加条件如下 {@code "AND key [ ! ]= val"}
- * 即添加一个检查该键是否等于该值的条件
- *
- *
- * @param key 键
- * @param val 要等于的值
- * @param negation 是否取反,即使用 !=
- */
- @NotNull
- public
- sqlWhereBulid addEqual(@NotNull final String key, @NotNull final long val,
- final boolean negation)
- {return addEqual(Relation.AND, key, val, negation);}
-
- /**
- * 添加等值条件.
- *
- * 添加条件如下 {@code "( AND | OR ) key [ ! ]= val"}
- * 即添加一个检查该键是否等于该值的条件
- *
- *
- * @param relation 与前面条件的关系
- * @param key 键
- * @param val 要等于的值
- * @param negation 是否取反,即使用 !=
- */
- @NotNull
- public
- sqlWhereBulid addEqual(@NotNull final Relation relation, @NotNull String key,
- @NotNull final long val, final boolean negation)
- {
- key = fuckSql(key);
- @NotNull final String s = Long.toString(val);
-
- addRelation(relation).set.ensureCapacity(set.length() + key.length() + 10 + s.length());
- set.append("`")
- .append(key)
- .append("` ")
- .append(negation ? "!" : "")
- .append("= '")
- .append(s)
- .append("'");
- return this;
- }
-
- /**
- * 添加等值条件.
- *
- * 添加条件如下 {@code "AND key [ ! ]= val"}
- * 即添加一个检查该键是否等于该值的条件
- *
- *
- * @param key 键
- * @param val 要等于的值
- * @param negation 是否取反,即使用 !=
- */
- @NotNull
- public
- sqlWhereBulid addEqual(@NotNull final String key, @NotNull final float val,
- final boolean negation)
- {return addEqual(Relation.AND, key, val, negation);}
-
- /**
- * 添加等值条件.
- *
- * 添加条件如下 {@code "( AND | OR ) key [ ! ]= val"}
- * 即添加一个检查该键是否等于该值的条件
- *
- *
- * @param relation 与前面条件的关系
- * @param key 键
- * @param val 要等于的值
- * @param negation 是否取反,即使用 !=
- */
- @NotNull
- public
- sqlWhereBulid addEqual(@NotNull final Relation relation, @NotNull String key,
- @NotNull final float val, final boolean negation)
- {
- key = fuckSql(key);
- @NotNull final String s = Float.toString(val);
-
- addRelation(relation).set.ensureCapacity(set.length() + key.length() + 10 + s.length());
- set.append("`")
- .append(key)
- .append("` ")
- .append(negation ? "!" : "")
- .append("= '")
- .append(s)
- .append("'");
- return this;
- }
-
- /*
- * AddEqualPrioriy
- */
-
- /**
- * 添加串联等值条件.
- *
- * 添加条件如下 {@code " AND \( key [ ! ]= val ( AND | OR ) key [ ! ]= val \)"}
- * 即添加一个检查所指定的表达式是否等于的条件
- * 即两个等值条件,中间使用逻辑关系
- *
- *
- * @param key 键
- * @param val 要判断的值
- * @param negation 是否取反,即使用 !=
- * @param r 与第一个条件的关系
- * @param key2 键
- * @param val2 要判断的值
- * @param negation2 是否取反,即使用 !=
- */
- @NotNull
- public
- sqlWhereBulid addEqualPrioriy(@NotNull final String key, @NotNull final String val,
- final boolean negation, @NotNull final Relation r, @NotNull final String key2,
- @NotNull final String val2, final boolean negation2)
- {return addEqualPrioriy(Relation.AND, key, val, negation, r, key2, val2, negation2);}
-
- /**
- * 添加串联等值条件.
- *
- * 添加条件如下 {@code "( AND | OR ) \( key [ ! ]= val ( AND | OR ) key [ ! ]= val \)"}
- * 即添加一个检查所指定的表达式是否等于的条件
- * 即两个等值条件,中间使用逻辑关系
- *
- *
- * @param relation 与前面条件的关系
- * @param key 键
- * @param val 要判断的值
- * @param negation 是否取反,即使用 !=
- * @param r 与第一个条件的关系
- * @param key2 键
- * @param val2 要判断的值
- * @param negation2 是否取反,即使用 !=
- */
- @NotNull
- public
- sqlWhereBulid addEqualPrioriy(@NotNull final Relation relation, @NotNull String key,
- @NotNull String val, final boolean negation, @NotNull final Relation r,
- @NotNull String key2, @NotNull String val2, final boolean negation2)
- {
- key = fuckSql(key);
- val = fuckSql(val);
- key2 = fuckSql(key2);
- val2 = fuckSql(val2);
-
- // 第一个条件
- addRelation(relation).set.ensureCapacity(set.length() + 10 + key.length() + val.length());
- set.append("( `").append(key).append("` ").append(negation ? "!" : "").append("= ").append(
- "'").append(val).append("'");
-
- addRelation(relation).set.ensureCapacity(set.length() + 10 + key2.length() + val2.length());
- set.append("`").append(key2).append("` ").append(negation2 ? "!" : "").append("= ").append(
- "'").append(val2).append("'").append(" )");
- return this;
- }
-
- /**
- * 添加串联等值条件.
- *
- * 添加条件如下 {@code " AND \( key [ ! ]= val ( AND | OR ) key [ ! ]= val \)"}
- * 即添加一个检查所指定的表达式是否等于的条件
- * 即两个等值条件,中间使用逻辑关系
- *
- *
- * @param key 键
- * @param val 要判断的值
- * @param negation 是否取反,即使用 !=
- * @param r 与第一个条件的关系
- * @param key2 键
- * @param val2 要判断的值
- * @param negation2 是否取反,即使用 !=
- */
- @NotNull
- public
- sqlWhereBulid addEqualPrioriy(@NotNull final String key, @NotNull final long val,
- final boolean negation, @NotNull final Relation r, @NotNull final String key2,
- @NotNull final long val2, final boolean negation2)
- {return addEqualPrioriy(Relation.AND, key, val, negation, r, key2, val2, negation2);}
-
- /**
- * 添加串联等值条件.
- *
- * 添加条件如下 {@code "( AND | OR ) \( key [ ! ]= val ( AND | OR ) key [ ! ]= val \)"}
- * 即添加一个检查所指定的表达式是否等于的条件
- * 即两个等值条件,中间使用逻辑关系
- *
- *
- * @param relation 与前面条件的关系
- * @param key 键
- * @param val 要判断的值
- * @param negation 是否取反,即使用 !=
- * @param r 与第一个条件的关系
- * @param key2 键
- * @param val2 要判断的值
- * @param negation2 是否取反,即使用 !=
- */
- @NotNull
- public
- sqlWhereBulid addEqualPrioriy(@NotNull final Relation relation, @NotNull String key,
- @NotNull final long val, final boolean negation, @NotNull final Relation r,
- @NotNull String key2, @NotNull final long val2, final boolean negation2)
- {
- key = fuckSql(key);
- String s = Long.toString(val);
- key2 = fuckSql(key2);
- String s2 = Long.toString(val2);
-
- // 第一个条件
- addRelation(relation).set.ensureCapacity(set.length() + 10 + key.length() + s.length());
- set.append("( `").append(key).append("` ").append(negation ? "!" : "").append("= '").append(
- s).append("'");
-
- addRelation(relation).set.ensureCapacity(set.length() + 10 + key2.length() + s2.length());
- set.append("`").append(key2).append("` ").append(negation2 ? "!" : "").append("= '").append(
- s2).append("' )");
- return this;
- }
-
- /**
- * 添加串联等值条件.
- *
- * 添加条件如下 {@code " AND \( key [ ! ]= val ( AND | OR ) key [ ! ]= val \)"}
- * 即添加一个检查所指定的表达式是否等于的条件
- * 即两个等值条件,中间使用逻辑关系
- *
- *
- * @param key 键
- * @param val 要判断的值
- * @param negation 是否取反,即使用 !=
- * @param r 与第一个条件的关系
- * @param key2 键
- * @param val2 要判断的值
- * @param negation2 是否取反,即使用 !=
- */
- @NotNull
- public
- sqlWhereBulid addEqualPrioriy(@NotNull final String key, @NotNull final float val,
- final boolean negation, @NotNull final Relation r, @NotNull final String key2,
- @NotNull final float val2, final boolean negation2)
- {return addEqualPrioriy(Relation.AND, key, val, negation, r, key2, val2, negation2);}
-
- /**
- * 添加串联等值条件.
- *
- * 添加条件如下 {@code "( AND | OR ) \( key [ ! ]= val ( AND | OR ) key [ ! ]= val \)"}
- * 即添加一个检查所指定的表达式是否等于的条件
- * 即两个等值条件,中间使用逻辑关系
- *
- *
- * @param key 键
- * @param val 要判断的值
- * @param negation 是否取反,即使用 !=
- * @param r 与第一个条件的关系
- * @param key2 键
- * @param val2 要判断的值
- * @param negation2 是否取反,即使用 !=
- */
- @NotNull
- public
- sqlWhereBulid addEqualPrioriy(@NotNull final Relation relation, @NotNull String key,
- @NotNull final float val, final boolean negation, @NotNull final Relation r,
- @NotNull String key2, @NotNull final float val2, final boolean negation2)
- {
- key = fuckSql(key);
- String s = Float.toString(val);
- key2 = fuckSql(key2);
- String s2 = Float.toString(val2);
-
- // 第一个条件
- addRelation(relation).set.ensureCapacity(set.length() + 10 + key.length() + s.length());
- set.append("( `").append(key).append("` ").append(negation ? "!" : "").append("= '").append(
- s).append("'");
-
- addRelation(relation).set.ensureCapacity(set.length() + 10 + key2.length() + s2.length());
- set.append("`").append(key2).append("` ").append(negation2 ? "!" : "").append("= '").append(
- s2).append("' )");
- return this;
- }
-
- /*
- * AddWhere
- */
-
- /**
- * 添加键值逻辑条件.
- *
- * 添加条件如下 {@code "AND key ( < | > | <= | >= | <> ) val"}
- * 与等值条件不同的是,你可以指定键和值的逻辑运算符
- *
- *
- * @param key 键
- * @param how 逻辑运算符
- * @param val 要判断的值
- */
- @NotNull
- public
- sqlWhereBulid addWhere(@NotNull final String key, @NotNull final HowWhere how,
- @NotNull final String val)
- {return addWhere(Relation.AND, key, how, val);}
-
- /**
- * 添加键值逻辑条件.
- *
- * 添加条件如下 {@code "( AND | OR ) key ( < | > | <= | >= | <> ) val"}
- * 与等值条件不同的是,你可以指定键和值的逻辑运算符
- *
- *
- * @param relation 与前面条件的关系
- * @param key 键
- * @param how 逻辑运算符
- * @param val 要判断的值
- */
- @NotNull
- public
- sqlWhereBulid addWhere(@NotNull final Relation relation, @NotNull String key,
- @NotNull final HowWhere how, @NotNull String val)
- {
- key = fuckSql(key);
- val = fuckSql(val);
-
- addRelation(relation).set.ensureCapacity(
- set.length() + 4 + key.length() + val.length() + how.toString().length());
- set.append("`").append(key).append("`").append(how).append("'").append(val).append("'");
- return this;
- }
-
- /**
- * 添加键值逻辑条件.
- *
- * 添加条件如下 {@code "AND key ( < | > | <= | >= | <> ) val"}
- * 与等值条件不同的是,你可以指定键和值的逻辑运算符
- *
- *
- * @param key 键
- * @param how 逻辑运算符
- * @param val 要判断的值
- */
- @NotNull
- public
- sqlWhereBulid addWhere(@NotNull final String key, @NotNull final HowWhere how,
- @NotNull final long val)
- {return addWhere(Relation.AND, key, how, val);}
-
- /**
- * 添加键值逻辑条件.
- *
- * 添加条件如下 {@code "( AND | OR ) key ( < | > | <= | >= | <> ) val"}
- * 与等值条件不同的是,你可以指定键和值的逻辑运算符
- *
- *
- * @param relation 与前面条件的关系
- * @param key 键
- * @param how 逻辑运算符
- * @param val 要判断的值
- */
- @NotNull
- public
- sqlWhereBulid addWhere(@NotNull final Relation relation, @NotNull String key,
- @NotNull final HowWhere how, @NotNull final long val)
- {
- key = fuckSql(key);
- String s = Long.toString(val);
-
- addRelation(relation).set.ensureCapacity(
- set.length() + key.length() + 4 + s.length() + how.toString().length());
- set.append("`").append(key).append("`").append(how).append("'").append(s).append("'");
- return this;
- }
-
- /**
- * 添加键值逻辑条件.
- *
- * 添加条件如下 {@code "AND key ( < | > | <= | >= | <> ) val"}
- * 与等值条件不同的是,你可以指定键和值的逻辑运算符
- *
- *
- * @param key 键
- * @param how 逻辑运算符
- * @param val 要判断的值
- */
- @NotNull
- public
- sqlWhereBulid addWhere(@NotNull final String key, @NotNull final HowWhere how,
- @NotNull final float val)
- {return addWhere(Relation.AND, key, how, val);}
-
- /**
- * 添加键值逻辑条件.
- *
- * 添加条件如下 {@code "( AND | OR ) key ( < | > | <= | >= | <> ) val"}
- * 与等值条件不同的是,你可以指定键和值的逻辑运算符
- *
- *
- * @param relation 与前面条件的关系
- * @param key 键
- * @param how 逻辑运算符
- * @param val 要判断的值
- */
- @NotNull
- public
- sqlWhereBulid addWhere(@NotNull final Relation relation, @NotNull String key,
- @NotNull final HowWhere how, @NotNull final float val)
- {
- key = fuckSql(key);
- String s = Float.toString(val);
-
- addRelation(relation).set.ensureCapacity(
- set.length() + 4 + key.length() + s.length() + how.toString().length());
- set.append("`").append(key).append("`").append(how).append("'").append(s).append("'");
- return this;
- }
-
- /*
- * AddPriority
- */
-
- /**
- * 添加串联键值逻辑条件.
- *
- * 添加条件如下 {@code " AND \( key ( < | > | <= | >= | <> ) val ( AND | OR ) key ( < | > | <= | >= | <> ) val \)"}
- * 即添加一个检查所指定的表达式是否等于的条件
- * 即两个键值逻辑条件,中间使用逻辑关系
- *
- *
- * @param key 键
- * @param how 逻辑运算符
- * @param val 要判断的值
- * @param r 与第一个条件的关系
- * @param key2 键
- * @param how2 逻辑运算符
- * @param val2 要判断的值
- */
- @NotNull
- public
- sqlWhereBulid addPriority(@NotNull final String key, @NotNull final HowWhere how,
- @NotNull final String val, @NotNull final Relation r, @NotNull final String key2,
- @NotNull final HowWhere how2, @NotNull final String val2)
- {return addPriority(Relation.AND, key, how, val, r, key2, how2, val2);}
-
- /**
- * 添加串联键值逻辑条件.
- *
- * 添加条件如下 {@code "( AND | OR ) \( key ( < | > | <= | >= | <> ) val ( AND | OR ) key ( < | > | <= | >= | <> ) val \)"}
- * 即添加一个检查所指定的表达式是否等于的条件
- * 即两个键值逻辑条件,中间使用逻辑关系
- *
- *
- * @param relation 与前面条件的关系
- * @param key 键
- * @param how 逻辑运算符
- * @param val 要判断的值
- * @param r 与第一个条件的关系
- * @param key2 键
- * @param how2 逻辑运算符
- * @param val2 要判断的值
- */
- @NotNull
- public
- sqlWhereBulid addPriority(@NotNull final Relation relation, @NotNull String key,
- @NotNull final HowWhere how, @NotNull String val, @NotNull final Relation r,
- @NotNull String key2, @NotNull final HowWhere how2, @NotNull String val2)
- {
- key = fuckSql(key);
- key2 = fuckSql(key2);
- val = fuckSql(val);
- val2 = fuckSql(val2);
-
- // 第一个条件
- addRelation(relation).set.ensureCapacity(
- set.length() + 6 + key.length() + val.length() + how.toString().length());
- set.append("( `").append(key).append("`").append(how).append("'").append(val).append("'");
-
- addRelation(r).set.ensureCapacity(
- set.length() + 6 + key2.length() + val2.length() + how2.toString().length());
- set.append("`").append(key2).append("`").append(how2).append("'").append(val2).append(
- "' )");
- return this;
- }
-
- /**
- * 添加串联键值逻辑条件.
- *
- * 添加条件如下 {@code " AND \( key ( < | > | <= | >= | <> ) val ( AND | OR ) key ( < | > | <= | >= | <> ) val \)"}
- * 即添加一个检查所指定的表达式是否等于的条件
- * 即两个键值逻辑条件,中间使用逻辑关系
- *
- *
- * @param key 键
- * @param how 逻辑运算符
- * @param val 要判断的值
- * @param r 与第一个条件的关系
- * @param key2 键
- * @param how2 逻辑运算符
- * @param val2 要判断的值
- */
- @NotNull
- public
- sqlWhereBulid addPriority(@NotNull final String key, @NotNull final HowWhere how,
- @NotNull final long val, @NotNull final Relation r, @NotNull final String key2,
- @NotNull final HowWhere how2, @NotNull final long val2)
- {return addPriority(Relation.AND, key, how, val, r, key2, how2, val2);}
-
- /**
- * 添加串联键值逻辑条件.
- *
- * 添加条件如下 {@code "( AND | OR ) \( key ( < | > | <= | >= | <> ) val ( AND | OR ) key ( < | > | <= | >= | <> ) val \)"}
- * 即添加一个检查所指定的表达式是否等于的条件
- * 即两个键值逻辑条件,中间使用逻辑关系
- *
- *
- * @param relation 与前面条件的关系
- * @param key 键
- * @param how 逻辑运算符
- * @param val 要判断的值
- * @param r 与第一个条件的关系
- * @param key2 键
- * @param how2 逻辑运算符
- * @param val2 要判断的值
- */
- @NotNull
- public
- sqlWhereBulid addPriority(@NotNull final Relation relation, @NotNull String key,
- @NotNull final HowWhere how, @NotNull final long val, @NotNull final Relation r,
- @NotNull String key2, @NotNull final HowWhere how2, @NotNull final long val2)
- {
- key = fuckSql(key);
- key2 = fuckSql(key2);
- String s = Long.toString(val), s2 = Long.toString(val2);
-
- // 第一个条件
- addRelation(relation).set.ensureCapacity(
- set.length() + 6 + key.length() + s.length() + how.toString().length());
- set.append("( `").append(key).append("`").append(how).append("'").append(val).append("'");
-
- addRelation(r).set.ensureCapacity(
- set.length() + 6 + key2.length() + s2.length() + how2.toString().length());
- set.append("`").append(key2).append("`").append(how2).append("'").append(val2).append(
- "' )");
- return this;
- }
-
- /**
- * 添加串联键值逻辑条件.
- *
- * 添加条件如下 {@code " AND \( key ( < | > | <= | >= | <> ) val ( AND | OR ) key ( < | > | <= | >= | <> ) val \)"}
- * 即添加一个检查所指定的表达式是否等于的条件
- * 即两个键值逻辑条件,中间使用逻辑关系
- *
- *
- * @param key 键
- * @param how 逻辑运算符
- * @param val 要判断的值
- * @param r 与第一个条件的关系
- * @param key2 键
- * @param how2 逻辑运算符
- * @param val2 要判断的值
- */
- @NotNull
- public
- sqlWhereBulid addPriority(@NotNull final String key, @NotNull final HowWhere how,
- @NotNull final float val, @NotNull final Relation r, @NotNull final String key2,
- @NotNull final HowWhere how2, @NotNull final float val2)
- {return addPriority(Relation.AND, key, how, val, r, key2, how2, val2);}
-
- /**
- * 添加串联键值逻辑条件.
- *
- * 添加条件如下 {@code "( AND | OR ) \( key ( < | > | <= | >= | <> ) val ( AND | OR ) key ( < | > | <= | >= | <> ) val \)"}
- * 即添加一个检查所指定的表达式是否等于的条件
- * 即两个键值逻辑条件,中间使用逻辑关系
- *
- *
- * @param relation 与前面条件的关系
- * @param key 键
- * @param how 逻辑运算符
- * @param val 要判断的值
- * @param r 与第一个条件的关系
- * @param key2 键
- * @param how2 逻辑运算符
- * @param val2 要判断的值
- */
- @NotNull
- public
- sqlWhereBulid addPriority(@NotNull final Relation relation, @NotNull String key,
- @NotNull final HowWhere how, @NotNull final float val, @NotNull final Relation r,
- @NotNull String key2, @NotNull final HowWhere how2, @NotNull final float val2)
- {
- key = fuckSql(key);
- key2 = fuckSql(key2);
- String s = Float.toString(val), s2 = Float.toString(val2);
-
- // 第一个条件
- addRelation(relation).set.ensureCapacity(
- set.length() + 6 + key.length() + s.length() + how.toString().length());
- set.append("( `").append(key).append("`").append(how).append("'").append(val).append("'");
-
- addRelation(r).set.ensureCapacity(
- set.length() + 6 + key2.length() + s2.length() + how2.toString().length());
- set.append("`").append(key2).append("`").append(how2).append("'").append(val2).append(
- "' )");
- return this;
- }
-
- /*
- * NullCheck
- */
-
- /**
- * 添加空检查.
- *
- * 添加条件如下 {@code " AND key IS NULL"}
- *
- *
- * @param key 要检查的键
- */
- @NotNull
- public
- sqlWhereBulid addNullCheck(@NotNull final String key)
- { return addNullCheck(Relation.AND, key); }
-
- /**
- * 添加空检查.
- *
- * 添加条件如下 {@code "( AND | OR ) key IS NULL"}
- *
- *
- * @param relation 与前面的条件的关系
- * @param key 要检查的键
- */
- @NotNull
- public
- sqlWhereBulid addNullCheck(@NotNull final Relation relation, @NotNull String key) {
- key = fuckSql(key);
-
- addRelation(relation).set.ensureCapacity(set.length() + 10 + key.length());
- set.append("`").append(key).append("`").append(" IS Null");
- return this;
- }
-
- /*
- * PriorityNullCheck
- */
-
- /**
- * 添加串联空检查.
- *
- * 添加条件如下 {@code " AND \( key IS NULL ( AND | OR ) key IS NULL \)"}
- * 即添加两个空检查并使用逻辑条件进行检查
- *
- *
- * @param key 要检查的键
- * @param relation 与第一个条件的关系
- * @param key2 要检查的键
- */
- @NotNull
- public
- sqlWhereBulid addPriorityNullCheck(@NotNull final String key, @NotNull final Relation r,
- @NotNull final String key2)
- { return addPriorityNullCheck(Relation.AND, key, r, key2); }
-
- /**
- * 添加串联空检查.
- *
- * 添加条件如下 {@code "( AND | OR ) \( key IS NULL ( AND | OR ) key IS NULL \)"}
- * 即添加两个空检查并使用逻辑条件进行检查
- *
- *
- * @param relation 与前面的条件的关系
- * @param key 要检查的键
- * @param relation 与第一个条件的关系
- * @param key2 要检查的键
- */
- @NotNull
- public
- sqlWhereBulid addPriorityNullCheck(@NotNull final Relation relation, @NotNull String key,
- @NotNull final Relation r, @NotNull String key2)
- {
- key = fuckSql(key);
- key2 = fuckSql(key2);
-
- addRelation(relation).set.ensureCapacity(set.length() + 13 + key.length());
- set.append("( `").append(key).append("`").append(" IS Null");
-
- addRelation(r).set.ensureCapacity(set.length() + 13 + key2.length());
- set.append("`").append(key2).append("`").append(" IS NULL").append(" )");
- return this;
- }
-
- /**
- * 添加 Sql 语句.
- *- * 添加的语句在 Where 后,请不要添加条件语句以外的语句 - *- * - * @param relation 与前面语句的关系 - * @param sql 要添加的 Sql 语句 - */ - @NotNull - public - sqlWhereBulid addSql(@NotNull final Relation relation, @NotNull final String sql) { - addRelation(relation).set.append(" ").append(sql); - return this; - } - - /** - *
获取语句.
- * - * @return 没参数为 {@code Null} - */ - @Nullable - public - String get() { - if (set.length() == 0) - return null; - return set.toString(); - } - - // 加入关系 - private - sqlWhereBulid addRelation(Relation relation) { - if (set.length() > 0) - set.append(relation); - return this; - } - - /** - *获取查询条件.
- *- * 获取条件集里面的条件并添加到传入的字符缓存区 - *- * - * @param whele 要提取的条件集 - * @param sql 要添加的字符缓冲区 - */ - public static - void getWhere(@NotNull final sqlWhereBulid whele, @NotNull final StringBuilder sql) - { - // 条件语句 - @Nullable final String s = whele.get(); - if (s == null) - return; - - sql.ensureCapacity(7 + s.length() + sql.length()); - sql.append(" WHERE ").append(s); - } - - /** - *
将 post 来的数据转化成sql条件对象.
- * - * @param s 传入的数据 - * @param bulid 要添加的对象 - */ - public static - void toPost(@NotNull String s, @NotNull final sqlWhereBulid bulid) { - int mark; - @NotNull //* 操作类型 *// - final String key = s.substring(0, mark = s.indexOf("##")).trim().toUpperCase(); - s = s.substring(mark += 2); - mark = 0; - - switch ( key ) { - case "ADDEQUAL": - getWhereForPost.addEqual(s, bulid); - break; - case "ADDEQUALPRIORIY": - getWhereForPost.addEqualPrioriy(s, bulid); - break; - case "ADDWHERE": - getWhereForPost.addWhere(s, bulid); - break; - case "ADDPRIORITY": - getWhereForPost.addPriority(s, bulid); - break; - case "ADDNULLCHECK": - getWhereForPost.addNullCheck(s, bulid); - break; - case "ADDPRIORITYNULLCHECK": - getWhereForPost.addPriorityNullCheck(s, bulid); - break; - case "ADDSQL": - bulid.addSql(Relation.valueOf(s.substring(0, mark = s.indexOf("##"))), - s.substring(mark += 2)); - break; - } - } - - /** - *将字符串的交换信息转化为 sql 条件对象
- * - * @author fybug - * @version 0.0.1 - * @since JDK 1.8 - */ - public static final - class getWhereForPost { - public static - void addEqual(@NotNull final String stringBuilder, @NotNull final sqlWhereBulid bulid) - { - int mark; - @NotNull final String relation = stringBuilder.substring(0, mark = - stringBuilder.indexOf("##")).trim(); - @NotNull final String key = stringBuilder.substring(mark += 2, mark = - stringBuilder.indexOf("##", mark)).trim(); - @NotNull final String val; - final boolean ne; - - val = stringBuilder.substring(mark += 2, mark = stringBuilder.indexOf("##", mark)) - .trim(); - ne = Boolean.parseBoolean(stringBuilder.substring(mark += 2).trim()); - - bulid.addEqual(Relation.valueOf(relation), key, val, ne); - } - - public static - void addEqualPrioriy(@NotNull final String stringBuilder, - @NotNull final sqlWhereBulid bulid) - { - int mark = 0; - @NotNull final String relation = stringBuilder.substring(0, mark = - stringBuilder.indexOf("##")).trim(); - @NotNull final String key = stringBuilder.substring(mark += 2, mark = - stringBuilder.indexOf("##", mark)).trim(); - @NotNull final String val = stringBuilder.substring(mark += 2, mark = - stringBuilder.indexOf("##", mark)).trim(); - final boolean ne = Boolean.valueOf( - stringBuilder.substring(mark += 2, mark = stringBuilder.indexOf("##", mark)) - .trim()); - @NotNull final String relation2 = stringBuilder.substring(mark += 2, mark = - stringBuilder.indexOf("##", mark)); - @NotNull final String key2 = stringBuilder.substring(mark += 2, mark = - stringBuilder.indexOf("##", mark)); - @NotNull final String val2 = stringBuilder.substring(mark += 2, mark = - stringBuilder.indexOf("##", mark)).trim(); - final boolean ne2 = Boolean.valueOf(stringBuilder.substring(mark += 2).trim()); - - bulid.addEqualPrioriy(Relation.valueOf(relation), key, val, ne, - Relation.valueOf(relation2), key2, val2, ne2); - - } - - public static - void addWhere(@NotNull final String stringBuilder, @NotNull final sqlWhereBulid bulid) - { - int mark; - @NotNull final String relation = stringBuilder.substring(0, mark = - stringBuilder.indexOf("##")).trim(); - @NotNull final String key = stringBuilder.substring(mark += 2, mark = - stringBuilder.indexOf("##", mark)).trim(); - @NotNull final String how = stringBuilder.substring(mark += 2, mark = - stringBuilder.indexOf("##", mark)).trim(); - @NotNull final String val = stringBuilder.substring(mark += 2).trim(); - - bulid.addWhere(Relation.valueOf(relation), key, HowWhere.valueOf(how), val); - } - - public static - void addPriority(@NotNull final String stringBuilder, @NotNull final sqlWhereBulid bulid) - { - int mark; - @NotNull final String relation = stringBuilder.substring(0, mark = - stringBuilder.indexOf("##")).trim(); - @NotNull final String key = stringBuilder.substring(mark += 2, mark = - stringBuilder.indexOf("##", mark)).trim(); - @NotNull final String how = stringBuilder.substring(mark += 2, mark = - stringBuilder.indexOf("##", mark)).trim(); - @NotNull final String val = stringBuilder.substring(mark += 2, mark = - stringBuilder.indexOf("##", mark)).trim(); - @NotNull final String relation2 = stringBuilder.substring(0, mark = - stringBuilder.indexOf("##")).trim(); - @NotNull final String key2 = stringBuilder.substring(mark += 2, mark = - stringBuilder.indexOf("##", mark)).trim(); - @NotNull final String how2 = stringBuilder.substring(mark += 2, mark = - stringBuilder.indexOf("##", mark)).trim(); - @NotNull final String val2 = stringBuilder.substring(mark += 2).trim(); - - bulid.addPriority(Relation.valueOf(relation), key, HowWhere.valueOf(how), val, - Relation.valueOf(relation2), key2, HowWhere.valueOf(how2), val2); - } - - public static - void addNullCheck(@NotNull final String stringBuilder, @NotNull final sqlWhereBulid bulid) - { - int mark; - @NotNull final String relation = stringBuilder.substring(0, mark = - stringBuilder.indexOf("##")).trim(); - @NotNull final String key = stringBuilder.substring(mark += 2).trim(); - - bulid.addNullCheck(Relation.valueOf(relation), key); - } - - public static - void addPriorityNullCheck(@NotNull final String s, @NotNull final sqlWhereBulid bulid) { - - int mark; - @NotNull final String relation = s.substring(0, mark = s.indexOf("##")).trim(); - @NotNull final String key = s.substring(mark += 2, mark = s.indexOf("##", mark)).trim(); - @NotNull final String relation2 = s.substring(mark += 2, mark = s.indexOf("##", mark)) - .trim(); - @NotNull final String key2 = s.substring(mark += 2).trim(); - - bulid.addPriorityNullCheck(Relation.valueOf(relation), key, Relation.valueOf(relation2), - key2); - } - } -} diff --git a/src/main/java/fybug/nulll/pdfunctionlibrary/Processing/sql/mysql/Delete.java b/src/main/java/fybug/nulll/pdfunctionlibrary/Processing/sql/mysql/Delete.java new file mode 100644 index 0000000000000000000000000000000000000000..1e40779ecafb3a61a72c55fa808bfd0a9641ceda --- /dev/null +++ b/src/main/java/fybug/nulll/pdfunctionlibrary/Processing/sql/mysql/Delete.java @@ -0,0 +1,170 @@ +package fybug.nulll.pdfunctionlibrary.Processing.sql.mysql; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import java.util.ArrayList; +import java.util.Collection; + +import fybug.nulll.pdfunctionlibrary.Annotations.NoSynchronized; +import fybug.nulll.pdfunctionlibrary.Processing.sql.Util.WhereSet; +import fybug.nulll.pdfunctionlibrary.lang.ConsistentField; + +import static fybug.nulll.pdfunctionlibrary.Processing.FuckChar.fuckSql; +/** + *
+ * 该对象包含一个完整的 {@code Mysql} 删除语句
+ * 需传入所需要的 条件语句 对象
+ *
+ *
+ * @author fybug
+ * @version 0.0.1
+ * @see MysqlSql
+ * @since PDF 1.3
+ */
+@NoSynchronized
+@SuppressWarnings( "All" )
+public final
+class Delete extends ConsistentField implements MysqlSql {
+ @Nullable protected ArrayList构造一个空的删除语句对象.
*/ + public + Delete() {} + + /** + *构造一个删除语句并初始化.
+ * + * @param tab 要删除的表 + * @param w 删除的条件 + */ + public + Delete(@NotNull final String tab, @NotNull final WhereSet w) { + setWhere(w); + addTabname(tab); + } + + /** + *添加要操作的表名.
+ * + * @param s 要操作的表 + * + * @return this + */ + @Override + public @NotNull + Delete addTabname(@NotNull String s) { + s = fuckSql(s).trim(); + if (!s.equals("")) + this.tabname.add(s); + else + tabname = null; + return this; + } + + /** + *获取当前要操作的表的列表.
+ * + * @return 要操作的表 + */ + @NotNull + public + Collection设置操作条件.
+ * + * @param where 如果是{@code null} 则会清空条件 + * + * @return this + */ + @NotNull + public + Delete setWhere(@Nullable final WhereSet where) { + if (where == null) + this.where = null; + else + this.where = where; + return this; + } + + /** + *获取条件对象.
+ * + * @return {@link WhereSet} + */ + @Nullable + public + WhereSet getWhere() { return where; } + + @Override + protected + boolean consistent(@NotNull final Object obj) + { return hashCode() == obj.hashCode(); } + + @Override + public + int hashCode() { + return ((super.hashCode() + (tabname == null ? -1 : tabname.hashCode())) << 5) + + ((where == null ? -1 : where.hashCode()) << 5); + } + + @Override + @NotNull + protected + Object clone() throws CloneNotSupportedException { + @NotNull final Delete d = (Delete) super.clone(); + d.tabname = tabname; + d.where = where == null ? null : (WhereSet) where.clone(); + return d; + } + + @NotNull + @Override + public + String toString() { + if (where == null || tabname == null) + throw new NullPointerException("No Field Sql!"); + + boolean f = true; + @NotNull final StringBuilder sql = new StringBuilder("DELETE FROM "); + tabname.trimToSize(); + + /* 获取插入表组 */ + for ( String key : tabname ){ + if (f) { + sql.ensureCapacity(sql.length() + 2 + key.length()); + f = false; + } else { + sql.ensureCapacity(sql.length() + key.length() + 4); + sql.append(", "); + } + sql.append('`').append(key).append('`'); + } + + WhereSet.getWhere(where, sql); + + sql.trimToSize(); + return sql.toString(); + } + + @Override + public + void clean() { + tabname.clear(); + where = null; + } + + @Override + public + void free() { clean(); } + + @Override + public + void close() { + where.close(); + free(); + tabname = null; + } +} \ No newline at end of file diff --git a/src/main/java/fybug/nulll/pdfunctionlibrary/Processing/sql/mysql/Insert.java b/src/main/java/fybug/nulll/pdfunctionlibrary/Processing/sql/mysql/Insert.java new file mode 100644 index 0000000000000000000000000000000000000000..1c73f7c642b7954bdd8ea26ef11d9447e981f713 --- /dev/null +++ b/src/main/java/fybug/nulll/pdfunctionlibrary/Processing/sql/mysql/Insert.java @@ -0,0 +1,194 @@ +package fybug.nulll.pdfunctionlibrary.Processing.sql.mysql; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import fybug.nulll.pdfunctionlibrary.Annotations.NoSynchronized; +import fybug.nulll.pdfunctionlibrary.Processing.sql.Util.InsertVal; +import fybug.nulll.pdfunctionlibrary.Processing.sql.Util.UpDataVal; +import fybug.nulll.pdfunctionlibrary.lang.ConsistentField; + +import static fybug.nulll.pdfunctionlibrary.Processing.FuckChar.fuckSql; +/** + *
+ * 该对象包含一个完整的 {@code Mysql} 插入语句
+ * 需传入所需要的 操作段语句 对象
+ * 如果传入了 更新操作段 对象则会在 已有插入 的情况下进行更新
+ *
+ *
+ * @author fybug
+ * @version 0.0.1
+ * @see MysqlSql
+ * @since PDF 1.3
+ */
+@NoSynchronized
+@SuppressWarnings( "all" )
+public final
+class Insert extends ConsistentField implements MysqlSql {
+ @Nullable protected String tabname = null;
+ @Nullable protected InsertVal In = null;
+ @Nullable protected UpDataVal up = null;
+
+ /**
+ * 构造一个插入语句并初始化.
+ * + * @param tabname 要插入的表 + * @param insertVal 插入值的语句 + */ + public + Insert(@NotNull final String tabname, @NotNull final InsertVal insertVal) { + setInserVal(insertVal); + addTabname(tabname); + } + + /**构造一个空的插入语句对象.
*/ + public + Insert() {} + + /** + *设置要操作的表名.
+ * + * @param s 要操作的表 + * + * @return this + */ + @Override + public @NotNull + Insert addTabname(@NotNull String s) { + tabname = fuckSql(s).trim(); + if (!s.equals("")) + this.tabname = s; + else + tabname = null; + return this; + } + + /** + *获取当前要操作的表名.
+ * + * @return 要操作的表的名称 + */ + @Nullable + public + String getTabname() { return tabname; } + + /** + *设置插入值语句.
+ * + * @param bv 如果是{@code Null} 则会清空语句对象 + * + * @return this + * + * @see InsertVal + */ + @NotNull + public + Insert setInserVal(@Nullable final InsertVal bv) { + In = bv; + return this; + } + + /** + *获取插入值语句.
+ * + * @return {@link InsertVal} + */ + @Nullable + public + InsertVal getInserVal() { return In; } + + /** + *设置更新语句.
+ * + * @param u 如果是{@code Null} 则会清空语句对象 + * + * @return this + * + * @see UpDataVal + */ + @NotNull + public + Insert serUpDataVal(@Nullable final UpDataVal u) { + up = u; + return this; + } + + /** + *获取更新语句.
+ * + * @return {@link UpDataVal} + */ + @Nullable + public + UpDataVal getUpDataVal() { return up; } + + @NotNull + @Override + public + String toString() { + if (In == null || In.size() == 0 || tabname == null) + throw new NullPointerException("No Field Sql!"); + + @NotNull final StringBuilder sql = new StringBuilder("INSERT INTO "); + + /* 插入语句部分 */ + sql.ensureCapacity(sql.length() + 3 + tabname.length()); + sql.append('`').append(tabname).append("`").append(In); + + /* 更新语句部分 */ + if (up != null) { + sql.append(" ON DUPLICATE KEY UPDATE ").append(up); + } + + sql.append(';'); + sql.trimToSize(); + return sql.toString(); + } + + @Override + protected + boolean consistent(@NotNull final Object obj) + { return obj.hashCode() == this.hashCode(); } + + @Override + public + int hashCode() { + return ((super.hashCode() + (tabname == null ? -1 : tabname.hashCode())) << 5) + + ((In == null ? -1 : In.hashCode()) << 5) + ((up == null ? -1 : up.hashCode()) << 5); + } + + @Override + @NotNull + public + Object clone() throws CloneNotSupportedException { + @NotNull final Insert i = (Insert) super.clone(); + i.tabname = this.tabname; + i.In = In == null ? null : (InsertVal) this.In.clone(); + i.up = up == null ? null : (UpDataVal) up.clone(); + return i; + } + + /* + * 释放 + */ + + @Override + public + void clean() { + tabname = null; + In = null; + up = null; + } + + @Override + public + void free() { clean(); } + + @Override + public + void close() { + In.clean(); + up.clean(); + free(); + } +} \ No newline at end of file diff --git a/src/main/java/fybug/nulll/pdfunctionlibrary/Processing/sql/mysql/MysqlSql.java b/src/main/java/fybug/nulll/pdfunctionlibrary/Processing/sql/mysql/MysqlSql.java new file mode 100644 index 0000000000000000000000000000000000000000..14b414b2dcf5e07a646528b6e4022f6b9259240e --- /dev/null +++ b/src/main/java/fybug/nulll/pdfunctionlibrary/Processing/sql/mysql/MysqlSql.java @@ -0,0 +1,40 @@ +package fybug.nulll.pdfunctionlibrary.Processing.sql.mysql; +import org.jetbrains.annotations.NotNull; + +import java.io.Serializable; + +import fybug.nulll.pdfunctionlibrary.Processing.sql.Mysql; +import fybug.nulll.pdfunctionlibrary.lang.Cleanable; +/** + *
+ * 此接口表示当前对象代表一个完整的 Mysql 操作语句
+ * 可通过 {@link #toString()} 打印该语句
+ *
+ *
+ * @author fybug
+ * @version 0.0.1
+ * @see Mysql
+ * @since PDF 1.3
+ */
+@SuppressWarnings( "all" )
+public
+interface MysqlSql extends Serializable, Cloneable, Cleanable {
+ /**
+ * 打印当前操作的 sql 语句.
+ * + * @return 当前操作的 SQL 语句 + */ + @NotNull + String toString(); + + /** + *添加要操作的表名.
+ * + * @param s 要操作的表 + * + * @return this + */ + @NotNull + MysqlSql addTabname(@NotNull final String s); +} diff --git a/src/main/java/fybug/nulll/pdfunctionlibrary/Processing/sql/mysql/Updata.java b/src/main/java/fybug/nulll/pdfunctionlibrary/Processing/sql/mysql/Updata.java new file mode 100644 index 0000000000000000000000000000000000000000..5d0d65674ad3f144d756182f62e88d7a6851aa9d --- /dev/null +++ b/src/main/java/fybug/nulll/pdfunctionlibrary/Processing/sql/mysql/Updata.java @@ -0,0 +1,177 @@ +package fybug.nulll.pdfunctionlibrary.Processing.sql.mysql; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import fybug.nulll.pdfunctionlibrary.Annotations.NoSynchronized; +import fybug.nulll.pdfunctionlibrary.Processing.sql.Util.UpDataVal; +import fybug.nulll.pdfunctionlibrary.Processing.sql.Util.WhereSet; +import fybug.nulll.pdfunctionlibrary.lang.ConsistentField; + +import static fybug.nulll.pdfunctionlibrary.Processing.FuckChar.fuckSql; +/** + *
+ * 该对象包含一个完整的 {@code Mysql} 更新语句
+ * 需传入所需要的 条件语句 对象
+ * 以及需要的 更新语句段 对象
+ *
+ *
+ * @author fybug
+ * @version 0.0.1
+ * @see MysqlSql
+ * @since PDF 1.3
+ */
+@SuppressWarnings( "all" )
+@NoSynchronized
+public final
+class Updata extends ConsistentField implements MysqlSql {
+ @Nullable protected String tabname = null;
+ @Nullable protected WhereSet where = null;
+ @Nullable protected UpDataVal up = null;
+
+ /**
+ * 添加要操作的表名.
+ * + * @param s 要操作的表 + * + * @return this + */ + @Override + public @NotNull + Updata addTabname(@NotNull final String s) { + tabname = fuckSql(s).trim(); + if (!s.equals("")) + this.tabname = s; + else + tabname = null; + return this; + } + + /** + *获取当前要操作的表名.
+ * + * @return 要操作的表 + */ + @Nullable + public + String getTabname() { return tabname; } + + /** + *设置操作条件.
+ * + * @param where 如果是{@code null} 则会清空条件 + * + * @return this + */ + @NotNull + public + Updata setWhere(@Nullable final WhereSet where) { + if (where == null) + this.where = null; + else + this.where = where; + return this; + } + + /** + *获取条件对象.
+ * + * @return {@link WhereSet} + */ + @Nullable + public + WhereSet getWhere() { return where; } + + /** + *设置更新语句段.
+ * + * @param updata 如果为{@code null} 则会清空对应的语句段 + * + * @return this + * + * @see UpDataVal + */ + @NotNull + public + Updata setUpdata(@Nullable final UpDataVal updata) { + if (updata == null) + up = null; + else + up = updata; + return this; + } + + /** + *获取更新语句段.
+ * + * @return {@link UpDataVal} + */ + @Nullable + public + UpDataVal getUpData() { return up; } + + @Override + protected + boolean consistent(@NotNull final Object obj) + { return hashCode() == obj.hashCode(); } + + @Override + public + int hashCode() { + return ((super.hashCode() + (tabname == null ? -1 : tabname.hashCode())) << 5) + + ((where == null ? -1 : where.hashCode()) << 5) + + ((up == null ? -1 : up.hashCode()) << 5); + } + + @Override + @NotNull + protected + Object clone() throws CloneNotSupportedException { + @NotNull final Updata d = (Updata) super.clone(); + d.tabname = tabname; + d.where = where == null ? null : (WhereSet) where.clone(); + d.up = up == null ? null : (UpDataVal) up.clone(); + return d; + } + + @NotNull + @Override + public + String toString() { + if (up == null || up.size() == 0 || tabname == null) + throw new NullPointerException("No Field Sql!"); + + @NotNull final StringBuilder sql = new StringBuilder("UPDATE "); + @NotNull final String fs = up.toString(); + + // 过滤表名 + sql.ensureCapacity(sql.length() + tabname.length() + 6 + fs.length()); + sql.append('`').append(tabname).append('`').append(" SET ").append(fs); + + // 获取条件 + WhereSet.getWhere(where, sql); + + sql.trimToSize(); + return sql.toString(); + } + + @Override + public + void clean() { + up = null; + tabname = null; + where = null; + } + + @Override + public + void free() { clean(); } + + @Override + public + void close() { + up.close(); + where.close(); + free(); + } +} diff --git a/src/main/java/fybug/nulll/pdfunctionlibrary/Processing/sql/mysql/package-info.java b/src/main/java/fybug/nulll/pdfunctionlibrary/Processing/sql/mysql/package-info.java new file mode 100644 index 0000000000000000000000000000000000000000..2b7bc9bdff628b40a85323d95c34b49337ec63bc --- /dev/null +++ b/src/main/java/fybug/nulll/pdfunctionlibrary/Processing/sql/mysql/package-info.java @@ -0,0 +1,8 @@ +/** + *提供一个存放一组键值的数据映射对象 清除键的数据. 获取键数据. 检查当前键是否为空. 清除值数据. {@link #clean()} {@link #free()} 获取{@link DataMap} 的实现类. 使用了数组实现的映射集. 使用了链表实现的映射集. 添加一个映射. 添加要关闭的对象. 关闭该类下的所有可关闭对象. 构造一个空的数据解析对象. 构造一个数据解析对象. 加载 post 数据并解析. 解析数据. 处理解析后的数据. 放入指令类. 获取指令类. 删除类. 打印当前的数据. 指令名. 构造一个空的指令. 构造一个指令,并载入数据. 加载数据. 放入属性数据. 获取属性数据. 删除属性数据. 类名. 构造一个空的指令类. 构造一个指令类,并载入数据. 加载数据. 放入指令数据. 获取指令数据. 删除指令数据. 属性名. 存放解析出来的属性. 构造一个空的属性. 构造一个属性,并载入数据. 载入属性. 放入属性. 放入属性. 获取属性.
@@ -18,7 +17,7 @@ import static fybug.nulll.pdfunctionlibrary.lang.CanEmpty.checkNull;
* @param 数据映射集.
*
@@ -19,7 +20,8 @@ import fybug.nulll.pdfunctionlibrary.Util.DataMap;
* @param
*
* @author fybug
- * @version 0.0.5
+ * @version 0.0.6
* @see ConsistentField
* @see AutoCloseable
* @see MaybeSynchronized
@@ -60,6 +60,22 @@ class CloseAll extends ConsistentField implements AutoCloseable, MaybeSynchroniz
public abstract
CloseAll append(@Nullable final AutoCloseable... close);
+ /**
+ * BDPos 数据容器.
+ *
+ * 该类存放载入的所有的 {@link PosClass} 对象,并使用对应的名称存放
+ * 需要加载 {@code post} 数据,也可单独进行放入
+ *
+ *
+ * @author fybug
+ * @version 0.0.1
+ * @since PDF 1.3
+ */
+@NoSynchronized
+@SuppressWarnings( "all" )
+public
+class BDPos extends CObject implements CanEmpty, Serializable {
+ private static final long serialVersionUID = -2918982630884148383L;
+ @Nullable protected HashMap处理解析后的数据.
+ *
+ * @author fybug
+ * @see #passPost(StringBuilder, String, PutData)
+ */
+ static
+ interface PutData {
+ /**
+ * 指令数据.
+ *
+ * 该类存放载入的所有的 {@link PostField} 对象,并使用对应的名称存放
+ * 需要加载 {@code post} 数据,也可单独进行放入
+ *
+ *
+ * @author fybug
+ * @version 0.0.1
+ * @since PDF 1.3
+ */
+@NoSynchronized
+@SuppressWarnings( "all" )
+public
+class Instruct implements CanEmpty, Serializable, Cloneable {
+ /** BDPos 指令类.
+ *
+ * 该类存放载入的所有的 {@link Instruct} 对象,并使用对应的名称存放
+ * 需要加载 {@code post} 数据,也可单独进行放入
+ *
+ *
+ * @author fybug
+ * @version 0.0.1
+ * @since PDF 1.3
+ */
+@NoSynchronized
+@SuppressWarnings( "all" )
+public
+class PosClass extends CObject implements CanEmpty, Serializable {
+ /** 属性集.
+ *
+ * @author fybug
+ * @version 0.0.1
+ * @see #data
+ * @since PDF 1.3
+ */
+@SuppressWarnings( "All" )
+@NoSynchronized
+public
+class PostField implements CanEmpty, Serializable, Cloneable {
+ /**
+ * 大小大于 {@code 1} 则为多内容
+ * 内容为 {@link String} 则为单属性
+ * 内容为 {@link String[]} 则为多属性
+ * 使用 {@link Class#isArray()} 进行判断
+ *
+ */
+ @NotNull protected LinkedList data;
+
+ /** BDPos 工具包.
+ *
+ * BDPos 是一种网络消息传输格式
+ * 用于像服务器发送消息,服务器应按要求执行对应的函数,一般不要求响应
+ *
+ * 格式
+ * {@code ###} 该消息域为一个类,类中需包含类名和下列标签
+ * {@code ##} 该消息域为一条指令,需包含指令属性
+ * {@code #} 该消息域为一组属性,通常包含键值
+ * {@code &} 通常用于有多组内容的属性,用于分割每个属性值,一般属性不需要
+ * {@code \n} 通常为分割当前消息域中的特殊属性,该属性有多组内容
+ *
+ * {@code
+ * // 类级
+ * ### Delete:
+ * // 指令级
+ * ## Info:
+ * // 属性级 **以上的换行均为方便示例,实际中不应出现换行
+ * # name:test # url:127.0.0.1 ## mess: # where:key&val // 多属性
+ * key&val // 多内容
+ * key }
+ *
+ *
+ * @author fybug
+ * @version 0.0.1
+ * @since PDF 1.3
+ */
+package fybug.nulll.pdfunctionlibrary.lang.net.bdpos;
diff --git a/src/main/java/fybug/nulll/pdfunctionlibrary/lang/net/package-info.java b/src/main/java/fybug/nulll/pdfunctionlibrary/lang/net/package-info.java
new file mode 100644
index 0000000000000000000000000000000000000000..2b1312b08ea43d2e4def45636f7c2a00a3dca26a
--- /dev/null
+++ b/src/main/java/fybug/nulll/pdfunctionlibrary/lang/net/package-info.java
@@ -0,0 +1,8 @@
+/**
+ * 网络工具包.
+ *
+ * @author fybug
+ * @version 0.0.1
+ * @since PDF 1.3
+ */
+package fybug.nulll.pdfunctionlibrary.lang.net;
\ No newline at end of file
diff --git a/src/test/java/TestBDPost.java b/src/test/java/TestBDPost.java
new file mode 100644
index 0000000000000000000000000000000000000000..bb370296114858e5eaa4bc1b6ad97373063890e5
--- /dev/null
+++ b/src/test/java/TestBDPost.java
@@ -0,0 +1,88 @@
+import org.junit.Before;
+import org.junit.Test;
+
+import fybug.nulll.pdfunctionlibrary.lang.net.bdpos.BDPos;
+public
+class TestBDPost {
+ String da;
+
+ @Before
+ public
+ void init() {
+ StringBuilder stringBuilder = new StringBuilder(" ### c1:");
+ {
+ stringBuilder.append(" ## I1:");
+ {
+ stringBuilder.append(" # f1:");
+ {
+ stringBuilder.append("as");
+ }
+ stringBuilder.append(" # f2:");
+ {
+ stringBuilder.append("a1").append("&");
+ stringBuilder.append("a2").append("&");
+ stringBuilder.append("a3");
+ }
+ }
+ stringBuilder.append(" ## I2:");
+ {
+ stringBuilder.append(" # f1:");
+ {
+ stringBuilder.append("sw");
+ stringBuilder.append("ewq");
+ }
+ {
+ stringBuilder.append("ewq").append("&");
+ stringBuilder.append("tret");
+ }
+ }
+ }
+ stringBuilder.append(" ### :");
+ {
+ stringBuilder.append(" ## I1:");
+ {
+ stringBuilder.append(" # f1:");
+ {
+ stringBuilder.append("as");
+ }
+ stringBuilder.append(" # f2:");
+ {
+ stringBuilder.append("a1").append("&");
+ stringBuilder.append("a2").append("&");
+ stringBuilder.append("a3");
+ }
+ }
+ stringBuilder.append(" ## I2:");
+ {
+ stringBuilder.append(" # f1:");
+ {
+ stringBuilder.append("sw");
+ stringBuilder.append("ewq");
+ }
+ {
+ stringBuilder.append("ewq").append("&");
+ stringBuilder.append("tret");
+ }
+ }
+ }
+
+ stringBuilder.trimToSize();
+ da = stringBuilder.toString();
+ }
+
+ @Test
+ public
+ void TestBDPos() {
+ BDPos pos = new BDPos(da);
+ System.out.println(pos.toString());
+ }
+
+ @Test
+ public
+ void TestDBPost() {
+ BDPos pos = new BDPos(da);
+ System.out.println(pos.getClass("").name);
+ System.out.println(pos.getClass("").getInstruct("I1").name);
+ System.out.println(pos.getClass("").getInstruct("I1").getPostField("f1").getField());
+ }
+}
\ No newline at end of file
diff --git a/src/test/java/TestMysql.java b/src/test/java/TestMysql.java
new file mode 100644
index 0000000000000000000000000000000000000000..05ab1ac6e48a828f92a5e4fdf80e4f243ac4b324
--- /dev/null
+++ b/src/test/java/TestMysql.java
@@ -0,0 +1,79 @@
+import org.junit.Before;
+import org.junit.Test;
+
+import java.sql.SQLException;
+
+import fybug.nulll.pdfunctionlibrary.Processing.sql.Enum.Logical;
+import fybug.nulll.pdfunctionlibrary.Processing.sql.Mysql;
+import fybug.nulll.pdfunctionlibrary.Processing.sql.Util.InsertVal;
+import fybug.nulll.pdfunctionlibrary.Processing.sql.Util.UpDataVal;
+import fybug.nulll.pdfunctionlibrary.Processing.sql.Util.WhereSet;
+import fybug.nulll.pdfunctionlibrary.Processing.sql.mysql.Delete;
+import fybug.nulll.pdfunctionlibrary.Processing.sql.mysql.Insert;
+import fybug.nulll.pdfunctionlibrary.Processing.sql.mysql.Updata;
+public
+class TestMysql {
+ Mysql mysql;
+
+ @Before
+ public
+ void init() {
+ try {
+ mysql.close();
+ } catch ( Throwable ignored ) {
+ }
+ try {
+ mysql = Mysql.getDatabase("127.0.0.1", 3306, "pageviews", "root", "2650693483");
+ } catch ( SQLException e ) {
+ e.printStackTrace();
+ }
+ }
+
+ @Test
+ public
+ void TestDelete() {
+ Delete delete = new Delete();
+ WhereSet set = new WhereSet();
+
+ set.addEqual(Logical.AND, "number", 1, false);
+
+ delete.addTabname("record");
+ delete.setWhere(set);
+
+ System.out.println(mysql.exe(delete));
+ }
+
+ @Test
+ public
+ void TestUpdate() {
+ Updata updata = new Updata();
+ WhereSet set = new WhereSet();
+ UpDataVal val = new UpDataVal();
+
+ set.addIn(Logical.AND, "date", false, "2018-12-05", "2018-12-06");
+
+ updata.addTabname("record");
+ updata.setWhere(set);
+
+ val.addMul("number", 2);
+ updata.setUpdata(val);
+
+ System.out.println(mysql.updata(updata));
+ }
+
+ @Test
+ public
+ void TestInser() {
+ Insert insert = new Insert();
+ InsertVal val = new InsertVal();
+
+ val.addVal("name", "Blog");
+ val.addVal("date", "2018-12-09");
+ val.addVal("number", "0");
+
+ insert.addTabname("record");
+ insert.setInserVal(val);
+
+ System.out.println(mysql.exe(insert));
+ }
+}