├── .gitignore ├── DatabaseDialects.xls ├── LICENSE ├── README.md ├── README_ENG.md ├── command.bat ├── core ├── LICENSE ├── pom.xml └── src │ ├── main │ └── java │ │ └── com │ │ └── github │ │ └── drinkjava2 │ │ ├── jbeanbox │ │ ├── ClassScanner.java │ │ └── NameMatchUtil.java │ │ ├── jdialects │ │ ├── ArrayUtils.java │ │ ├── ClassCacheUtils.java │ │ ├── DDLCreateUtils.java │ │ ├── DDLDropUtils.java │ │ ├── DDLFeatures.java │ │ ├── DDLFormatter.java │ │ ├── DebugUtils.java │ │ ├── Dialect.java │ │ ├── DialectException.java │ │ ├── DialectFunctionTemplate.java │ │ ├── DialectFunctionTranslator.java │ │ ├── DialectFunctionUtils.java │ │ ├── DialectPaginationTemplate.java │ │ ├── DialectType.java │ │ ├── DialectTypeMappingTemplate.java │ │ ├── GuessDialectUtils.java │ │ ├── Java8DateUtils.java │ │ ├── JdbcUtil.java │ │ ├── NamingConversion.java │ │ ├── PackageInfo.java │ │ ├── ReservedDBWords.java │ │ ├── SqlFormatter.java │ │ ├── StrUtils.java │ │ ├── TableModelUtils.java │ │ ├── TableModelUtilsOfDb.java │ │ ├── TableModelUtilsOfEntity.java │ │ ├── TableModelUtilsOfExcel.java │ │ ├── TableModelUtilsOfJavaSrc.java │ │ ├── Type.java │ │ ├── TypeUtils.java │ │ ├── annotation │ │ │ ├── jdia │ │ │ │ ├── AutoId.java │ │ │ │ ├── COLUMN.java │ │ │ │ ├── CreateTimestamp.java │ │ │ │ ├── CreatedBy.java │ │ │ │ ├── FKey.java │ │ │ │ ├── FKey1.java │ │ │ │ ├── FKey2.java │ │ │ │ ├── FKey3.java │ │ │ │ ├── IdentityId.java │ │ │ │ ├── LastModifiedBy.java │ │ │ │ ├── PKey.java │ │ │ │ ├── ShardDatabase.java │ │ │ │ ├── ShardTable.java │ │ │ │ ├── SingleFKey.java │ │ │ │ ├── SingleIndex.java │ │ │ │ ├── SingleUnique.java │ │ │ │ ├── Snowflake.java │ │ │ │ ├── TimeStampId.java │ │ │ │ ├── UUID.java │ │ │ │ ├── UUID25.java │ │ │ │ ├── UUID26.java │ │ │ │ ├── UUID32.java │ │ │ │ ├── UUID36.java │ │ │ │ ├── UUIDAny.java │ │ │ │ └── UpdateTimestamp.java │ │ │ └── jpa │ │ │ │ ├── Column.java │ │ │ │ ├── Convert.java │ │ │ │ ├── Entity.java │ │ │ │ ├── EnumType.java │ │ │ │ ├── Enumerated.java │ │ │ │ ├── GeneratedValue.java │ │ │ │ ├── GenerationType.java │ │ │ │ ├── Id.java │ │ │ │ ├── Index.java │ │ │ │ ├── SequenceGenerator.java │ │ │ │ ├── Table.java │ │ │ │ ├── TableGenerator.java │ │ │ │ ├── Temporal.java │ │ │ │ ├── TemporalType.java │ │ │ │ ├── Transient.java │ │ │ │ ├── UniqueConstraint.java │ │ │ │ └── Version.java │ │ ├── converter │ │ │ ├── BasicJavaConverter.java │ │ │ ├── BasicJavaToJdbcConverter.java │ │ │ ├── BasicJdbcToJavaConverter.java │ │ │ ├── JavaConverter.java │ │ │ ├── JavaToJdbcConverter.java │ │ │ └── JdbcToJavaConverter.java │ │ ├── id │ │ │ ├── AutoIdGenerator.java │ │ │ ├── IdGenerator.java │ │ │ ├── IdentityIdGenerator.java │ │ │ ├── SequenceIdGenerator.java │ │ │ ├── SnowflakeCreator.java │ │ │ ├── SnowflakeGenerator.java │ │ │ ├── SortedUUIDGenerator.java │ │ │ ├── TableIdGenerator.java │ │ │ ├── TimeStampIdGenerator.java │ │ │ ├── UUID25Generator.java │ │ │ ├── UUID26Generator.java │ │ │ ├── UUID32Generator.java │ │ │ ├── UUID36Generator.java │ │ │ ├── UUIDAnyGenerator.java │ │ │ └── UUIDGenerator.java │ │ ├── model │ │ │ ├── ColumnModel.java │ │ │ ├── FKeyModel.java │ │ │ ├── IndexModel.java │ │ │ ├── TableModel.java │ │ │ └── UniqueModel.java │ │ └── springsrc │ │ │ ├── PackageInfo.java │ │ │ └── utils │ │ │ ├── Assert.java │ │ │ ├── ClassUtils.java │ │ │ ├── CollectionUtils.java │ │ │ ├── ConcurrentReferenceHashMap.java │ │ │ ├── ObjectUtils.java │ │ │ ├── ReflectionUtils.java │ │ │ └── StringUtils.java │ │ └── jlogs │ │ ├── ConsoleLog.java │ │ ├── EmptyLog.java │ │ ├── Log.java │ │ ├── LogFactory.java │ │ └── SimpleSLF4JLog.java │ └── test │ ├── java │ └── Readme.java │ └── resources │ ├── common-logging.properties │ └── log4j.properties ├── demo ├── demo-jsqlbox │ ├── maven_clean.bat │ ├── maven_eclipse_clean.bat │ ├── maven_eclipse_eclipse.bat │ ├── maven_test.bat │ ├── pom.xml │ └── src │ │ └── test │ │ ├── java │ │ └── com │ │ │ └── github │ │ │ └── drinkjava2 │ │ │ └── demo │ │ │ └── TestDemo.java │ │ └── resources │ │ ├── common-logging.properties │ │ └── log4j.properties ├── demo-mybatis │ ├── maven_clean.bat │ ├── maven_eclipse_clean.bat │ ├── maven_eclipse_eclipse.bat │ ├── maven_test.bat │ ├── pom.xml │ └── src │ │ └── test │ │ ├── java │ │ └── com │ │ │ └── github │ │ │ └── drinkjava2 │ │ │ └── demo │ │ │ └── TestDemo.java │ │ └── resources │ │ ├── common-logging.properties │ │ └── log4j.properties └── demo-pure-jdbc │ ├── maven_clean.bat │ ├── maven_eclipse_clean.bat │ ├── maven_eclipse_eclipse.bat │ ├── maven_test.bat │ ├── pom.xml │ └── src │ └── test │ ├── java │ └── com │ │ └── github │ │ └── drinkjava2 │ │ └── demo │ │ └── TestDemo.java │ └── resources │ ├── common-logging.properties │ └── log4j.properties └── jdialects-logo.png /.gitignore: -------------------------------------------------------------------------------- 1 | /build/ 2 | target/ 3 | bin/ 4 | .project 5 | .classpath 6 | .settings/ 7 | *~ 8 | *.orig 9 | *.new 10 | .idea/ 11 | *.iml 12 | *.ipr 13 | test/**/build/ 14 | examples/**/build/ -------------------------------------------------------------------------------- /DatabaseDialects.xls: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/drinkjava2/jDialects/e08cc354c2b3be6f4a710a8c3659d8c0b2609914/DatabaseDialects.xls -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 |

2 | 3 | English instruction see "README_ENG.md" 4 | 5 |

6 |

7 | 8 | jdialects-logo 9 | 10 |

11 | 12 |

13 | 一个通用数据库方言工具 14 |

15 | 16 | # 简介 | Intro 17 | 18 | jDialects支持多达80多种数据库方言的DDL创建、实体源码生成、分页、函数变换、类型变换及主要的JPA注解解析功能。 19 | 它通常与JDBC工具组合使用,也可以作为其它Dao工具(如jSqlBox)的一个组成部分。 20 | 21 | # 优点 | Advantages 22 | 23 | - **无侵入**:jDialects工作原理基于对SQL文本进行变换,不会对您现有的持久层工具产生任何影响。 24 | - **依赖少**:仅依赖单个jar文件, 大小约280k。 25 | - **从Annotation创建DDL**:提供对一些主要的JPA注解的支持 26 | - **从Java方法创建DDL**:提供Java方法配置来创建DDL,同样的语法也可以在运行期修改配置。 27 | - **从数据库生成实体类源码**:可以读取数据库结构,生成注解风格的实体类POJO或ActiveRecord实体类源码 28 | - **主键生成器**:提供十种主键生成器,和一个分布式主键生成器。 29 | - **分页**:提供跨数据库的分页方法。 30 | - **函数变换**:对不同的数据库解析成对应方言的函数,尽量做到一次SQL到处运行。 31 | - **类型变换**:对不同的数据库字段类型,提供与Java类型的互相变换。 32 | - **保留字检查**:提供数据库保留字检查功能。 33 | 34 | # 文档 | Documentation 35 | 36 | [中文](https://gitee.com/drinkjava2/jdialects/wikis/pages) | [English](https://gitee.com/drinkjava2/jdialects/wiki) 37 | 38 | [JavaDoc](http://search.maven.org/#search%7Cga%7C1%7Ca%3A%22jdialects%22) 39 | 40 | # 应用示例 | Demo 41 | 42 | [在纯JDBC中使用](https://gitee.com/drinkjava2/jdialects/wikis/pages?title=8.1-%E5%9C%A8%E7%BA%AFJDBC%E9%A1%B9%E7%9B%AE%E4%B8%AD%E4%BD%BF%E7%94%A8&parent=%E7%94%A8%E6%88%B7%E6%89%8B%E5%86%8C) 43 | 44 | [在jSqlBox中使用](https://gitee.com/drinkjava2/jdialects/wikis/pages?title=8.2-%E5%9C%A8jSqlBox%E9%A1%B9%E7%9B%AE%E4%B8%AD%E4%BD%BF%E7%94%A8&parent=%E7%94%A8%E6%88%B7%E6%89%8B%E5%86%8C) 45 | 46 | [在MyBatis中使用](https://gitee.com/drinkjava2/jdialects/wikis/pages?title=8.3-%E5%9C%A8MyBatis%E4%B8%AD%E4%BD%BF%E7%94%A8&parent=%E7%94%A8%E6%88%B7%E6%89%8B%E5%86%8C) 47 | 48 | # 下载地址 | Download 49 | 50 | [点此去下载](http://search.maven.org/#search%7Cga%7C1%7Ca%3A%22jdialects%22) 51 | 或在项目pom.xml中添加: 52 | ```xml 53 | 54 | com.github.drinkjava2 55 | jdialects 56 | 5.0.12.jre8 57 | 58 | ``` 59 | 60 | # 相关开源项目 | Other Projects 61 | 62 | - [基于DbUtils和jDialects的持久层工具 jSqlBox](https://gitee.com/drinkjava2/jSqlBox) 63 | - [一个独立的声明式事务工具 jTransactions](https://gitee.com/drinkjava2/jTransactions) 64 | - [一个微型IOC/AOP工具 jBeanBox](https://gitee.com/drinkjava2/jBeanBox) 65 | 66 | # 期望 | Futures 67 | 68 | 欢迎发issue提出更好的意见或提交PR,帮助完善 jDialects 69 | 70 | # 版权 | License 71 | 72 | [Apache 2.0](http://www.apache.org/licenses/LICENSE-2.0) 73 | 74 | # 关注我 | About Me 75 | [Github](https://github.com/drinkjava2) 76 | [码云](https://gitee.com/drinkjava2) 77 | -------------------------------------------------------------------------------- /README_ENG.md: -------------------------------------------------------------------------------- 1 |

2 | 3 | jdialects-logo 4 | 5 |

6 | 7 |

8 | A Database Dialect Tool 9 |

10 | 11 | # Introduction 12 | 13 | jDialects supports DDL creation, paging, function transformation, type conversion and major JPA annotation parsing functions for up to 80 database dialects. 14 | It is usually used in combination with JDBC tools and as part of other Dao tools such as jSqlBox. 15 | jDialects requires JDK1.6+ 16 | 17 | # Advantages 18 | - **No intrusion**: jDialects works based on transforming SQL text without any impact on your existing persistence layer tools. 19 | - ** Less dependency**: Only rely on a single jar file, about 280k in size. 20 | - **Create DDL from Annotation**: Support build DDL from major JPA annotations 21 | - **Create DDL from Java methods**: Provide Java method configuration to create DDL, the same Java methods can also used at runtime. 22 | - **Primary Key Generator**: Provides ten primary key generators and supports custom primary key generators. A distributed primary key generator is also provided. 23 | - **Paging**: Provides a cross-database pagination method. 24 | - **Function transformation**: Parse different databases' functions corresponding dialects. 25 | - ** type conversion**: Provides mutual transformation with Java types for different database data types. 26 | - **Reserved Word Check**: Provides database reserved word checking. 27 | 28 | # Documentation 29 | 30 | [Chinese](https://gitee.com/drinkjava2/jdialects/wikis/pages) | [English](https://gitee.com/drinkjava2/jdialects/wiki) 31 | 32 | [JavaDoc](http://search.maven.org/#search%7Cga%7C1%7Ca%3A%22jdialects%22) 33 | 34 | # Demo 35 | 36 | [For Pure JDBC](https://github.com/drinkjava2/jDialects/tree/master/demo/demo-pure-jdbc) 37 | 38 | [For jSqlBox](https://github.com/drinkjava2/jDialects/tree/master/demo/demo-jsqlbox) 39 | 40 | [For MyBatis](https://github.com/drinkjava2/jDialects/tree/master/demo/demo-mybatis) 41 | 42 | # Download 43 | 44 | [Download here](http://search.maven.org/#search%7Cga%7C1%7Ca%3A%22jdialects%22) 45 | Or put below in pom.xml: 46 | ```xml 47 | 48 | com.github.drinkjava2 49 | jdialects 50 | 5.0.12.jre8 51 | 52 | ``` 53 | 54 | # Other Author's Projects 55 | 56 | - [jSqlBox](https://gitee.com/drinkjava2/jSqlBox) 57 | - [jTransactions](https://gitee.com/drinkjava2/jTransactions) 58 | - [jBeanBox](https://gitee.com/drinkjava2/jBeanBox) 59 | 60 | # Futures 61 | 62 | Welcome post issue to help improve jDialects. 63 | 64 | # License 65 | 66 | [Apache 2.0](http://www.apache.org/licenses/LICENSE-2.0) 67 | 68 | # About Me 69 | [Github](https://github.com/drinkjava2) 70 | [Gitee](https://gitee.com/drinkjava2) 71 | -------------------------------------------------------------------------------- /command.bat: -------------------------------------------------------------------------------- 1 | cmd -------------------------------------------------------------------------------- /core/src/main/java/com/github/drinkjava2/jbeanbox/NameMatchUtil.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 3 | * use this file except in compliance with the License. You may obtain a copy of 4 | * the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by 5 | * applicable law or agreed to in writing, software distributed under the 6 | * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS 7 | * OF ANY KIND, either express or implied. See the License for the specific 8 | * language governing permissions and limitations under the License. 9 | */ 10 | package com.github.drinkjava2.jbeanbox; 11 | 12 | /** 13 | * A simple string matcher tool 14 | * 15 | * @author Yong Zhu 16 | * @since 2.5.0 17 | */ 18 | public class NameMatchUtil { // NOSONAR 19 | /** 20 | * A simple string matcher, only 1 * allowed, but many regex string can 21 | * seperated by "|" char, for example:
22 | * "abc.ef*|*gh|ijk*lmn" matches "abc.efxxx", "xxxgh","ijkxxxlmn" 23 | */ 24 | public static boolean nameMatch(String regex, String name) { 25 | if (regex == null || regex.length() == 0 || name == null || name.length() == 0) 26 | return false; 27 | do { 28 | int i = regex.indexOf('|'); 29 | if (i < 0) 30 | return doSingleNameMatch(regex, name); 31 | if (doSingleNameMatch(regex.substring(0, i), name)) 32 | return true; 33 | regex = regex.substring(i + 1);// NOSONAR 34 | } while (true); 35 | } 36 | 37 | /** A simple matcher, only 1 * allowed represents any string */ 38 | private static boolean doSingleNameMatch(String regex, String name) { 39 | if (regex == null || regex.length() == 0 || name == null || name.length() == 0) 40 | return false; 41 | if ('*' == (regex.charAt(0))) { 42 | return name.endsWith(regex.substring(1)); 43 | } else if (regex.endsWith("*")) { 44 | return name.startsWith(regex.substring(0, regex.length() - 1)); 45 | } else { 46 | int starPos = regex.indexOf('*'); 47 | if (-1 == starPos) 48 | return regex.equals(name); 49 | return name.startsWith(regex.substring(0, starPos)) && name.endsWith(regex.substring(starPos + 1)); 50 | } 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /core/src/main/java/com/github/drinkjava2/jdialects/ArrayUtils.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 | * use this file except in compliance with the License. You may obtain a copy of 6 | * the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by 7 | * applicable law or agreed to in writing, software distributed under the 8 | * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS 9 | * OF ANY KIND, either express or implied. See the License for the specific 10 | * language governing permissions and limitations under the License. 11 | */ 12 | package com.github.drinkjava2.jdialects; 13 | 14 | import java.util.ArrayList; 15 | import java.util.List; 16 | 17 | /** 18 | * Array Utilities usually used inside of framework 19 | * 20 | * @author Yong Zhu 21 | * @since 2.0.1 22 | */ 23 | public class ArrayUtils {// NOSONAR 24 | 25 | /** Insert an Object at front of array */ 26 | public static Object[] insertArray(Object obj, Object[] arr) { 27 | Object[] newArr = new Object[arr.length + 1]; 28 | System.arraycopy(arr, 0, newArr, 1, arr.length); 29 | newArr[0] = obj; 30 | return newArr; 31 | } 32 | 33 | /** Append an Object at end of array */ 34 | public static Object[] appendArray(Object[] arr, Object obj) { 35 | Object[] newArr = new Object[arr.length + 1]; 36 | System.arraycopy(arr, 0, newArr, 0, arr.length); 37 | newArr[arr.length] = obj; 38 | return newArr; 39 | } 40 | 41 | /** Append a String at end of String array */ 42 | public static String[] appendStrArray(String[] arr, String str) { 43 | String[] newArr = new String[arr.length + 1]; 44 | System.arraycopy(arr, 0, newArr, 0, arr.length); 45 | newArr[arr.length] = str; 46 | return newArr; 47 | } 48 | 49 | /** Transfer a String array to String List */ 50 | public static List strArrayToList(String[] arr) { 51 | List result = new ArrayList(); 52 | if (arr == null || arr.length == 0) 53 | return result; 54 | for (String str : arr) 55 | result.add(str); 56 | return result; 57 | } 58 | 59 | /** Transfer a String List to String array */ 60 | public static String[] strListToArray(List list) { 61 | if (list == null) 62 | return new String[0]; 63 | return list.toArray(new String[list.size()]); 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /core/src/main/java/com/github/drinkjava2/jdialects/DDLFormatter.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 | * use this file except in compliance with the License. You may obtain a copy of 6 | * the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by 7 | * applicable law or agreed to in writing, software distributed under the 8 | * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS 9 | * OF ANY KIND, either express or implied. See the License for the specific 10 | * language governing permissions and limitations under the License. 11 | */ 12 | package com.github.drinkjava2.jdialects; 13 | 14 | /** 15 | * @author Yong Zhu 16 | * @since 2.0.4 17 | */ 18 | public class DDLFormatter { 19 | 20 | private static boolean isSpace(char c) { 21 | return c <= 32 || c == '\t' || c == '\r' || c == '\n'; 22 | } 23 | 24 | public static String format(String template) { 25 | StringBuilder sb = new StringBuilder(); 26 | char[] chars = (" " + template + " ").toCharArray(); 27 | int inBrackets = 0; 28 | boolean justReturned = false; 29 | for (int i = 2; i < chars.length - 2; i++) { 30 | char c = chars[i]; 31 | if (isSpace(c)) { 32 | if (!justReturned) 33 | sb.append(c); 34 | } else if (c == '(') { 35 | sb.append(c); 36 | if (inBrackets == 0) { 37 | sb.append("\n"); 38 | justReturned = true; 39 | } 40 | inBrackets++; 41 | } else if (c == ')') { 42 | inBrackets--; 43 | if (inBrackets == 0) { 44 | sb.append("\n"); 45 | justReturned = true; 46 | } 47 | sb.append(c); 48 | } else if (c == ',') { 49 | sb.append(c); 50 | if (inBrackets == 1) { 51 | sb.append("\n"); 52 | justReturned = true; 53 | } 54 | } else { 55 | sb.append(c); 56 | justReturned = false; 57 | } 58 | } 59 | return sb.toString(); 60 | } 61 | } -------------------------------------------------------------------------------- /core/src/main/java/com/github/drinkjava2/jdialects/DialectException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 | * use this file except in compliance with the License. You may obtain a copy of 6 | * the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by 7 | * applicable law or agreed to in writing, software distributed under the 8 | * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS 9 | * OF ANY KIND, either express or implied. See the License for the specific 10 | * language governing permissions and limitations under the License. 11 | */ 12 | package com.github.drinkjava2.jdialects; 13 | 14 | /** 15 | * DialectException for jDialects 16 | * 17 | * @author Yong Zhu 18 | * @since 1.0.0 19 | */ 20 | public class DialectException extends RuntimeException { 21 | private static final long serialVersionUID = 1L; 22 | 23 | public DialectException() { 24 | // Default constructor 25 | } 26 | 27 | public DialectException(String message) { 28 | super(message); 29 | } 30 | 31 | public DialectException(Throwable cause) { 32 | super(cause); 33 | } 34 | 35 | public DialectException(String message, Throwable cause) { 36 | super(message, cause); 37 | } 38 | 39 | public static Object throwEX(String errorMsg, Throwable e) { 40 | throw new DialectException(errorMsg, e); 41 | } 42 | 43 | public static Object throwEX(Throwable cause) { 44 | throw new DialectException(cause); 45 | } 46 | 47 | public static Object throwEX(String errorMsg) { 48 | throw new DialectException(errorMsg); 49 | } 50 | 51 | public static void eatException(Exception e) { 52 | // do nothing here 53 | } 54 | 55 | public static void assureNotNull(Object obj, String... optionMessages) { 56 | if (obj == null) 57 | throw new DialectException( 58 | optionMessages.length == 0 ? "Assert error, Object parameter can not be null" : optionMessages[0]); 59 | } 60 | 61 | public static void assureNotEmpty(String str, String... optionMessages) { 62 | if (str == null || str.length() == 0) 63 | throw new DialectException( 64 | optionMessages.length == 0 ? "Assert error, String parameter can not be empty" : optionMessages[0]); 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /core/src/main/java/com/github/drinkjava2/jdialects/DialectFunctionUtils.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 | * use this file except in compliance with the License. You may obtain a copy of 6 | * the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by 7 | * applicable law or agreed to in writing, software distributed under the 8 | * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS 9 | * OF ANY KIND, either express or implied. See the License for the specific 10 | * language governing permissions and limitations under the License. 11 | */ 12 | package com.github.drinkjava2.jdialects; 13 | 14 | /** 15 | * Dialect function Utils, detail see render method 16 | * 17 | * @author Yong Zhu 18 | * @since 1.0.2 19 | */ 20 | public class DialectFunctionUtils {// NOSONAR 21 | 22 | /** 23 | * The render method translate function template to real SQL piece 24 | * 25 | *
 26 | 	 * Template can be:
 27 | 	 * "*": standard SQL function, identical to abc($Params)
 28 | 	 * "abc($Params)": template with special parameter format:  
 29 | 	   				"$P1, $P2, $P3, $P4, $P5, $P6..."="$Params"
 30 | 					"$P1,$P2,$P3,$P4,$P5,$P6..."="$Compact_Params"
 31 | 					"$P1||$P2||$P3||$P4||$P5||$P6..."="$Lined_Params"
 32 | 					"$P1+$P2+$P3+$P4+$P5+$P6..."="$Add_Params");
 33 | 					"$P1 in $P2 in $P3 in $P4 in $P5 in $P6..."="$IN_Params"
 34 | 			        "$P1%pattern$P2%pattern$P3%pattern$P4%pattern$P5%pattern$P6..."="$Pattern_Params"
 35 | 					"11%startswith$P2%startswith$P3%startswith$P4%startswith$P5%startswith$P6..."= "$Startswith_Params");
 36 | 					 "nvl($P1, nvl($P2, nvl($P3, nvl($P4, nvl($P5, $P6...)))))"="$NVL_Params");
 37 | 	 * 
 38 | 	 * "0=abc()": function do not support parameter
 39 | 	 * "1=abc($P1)": function only support 1 parameter
 40 | 	 * "2=abc($P1,$P2)": function only support 2 parameters
 41 | 	 * "0=abc()|1=abc($P1)|3=abc($P1,$P2,$P3)": function support 0 or 1 or 3 parameters
 42 | 	 * 
 43 | 	 * 
44 | * 45 | * @param functionName function name 46 | * @param args function parameters 47 | * @return A SQL function piece 48 | */ 49 | protected static String render(Dialect d, String functionName, String... args) { 50 | String template = d.functions.get(functionName.toLowerCase()); 51 | DialectException.assureNotEmpty(template, "Dialect \"" + d + "\" does not support \"" + functionName 52 | + "\" function, a full list of supported functions of this dialect can see \"DatabaseDialects.xls\""); 53 | if ("*".equals(template)) 54 | template = functionName + "($Params)"; 55 | char c = template.charAt(1); 56 | if (c != '=') { 57 | if (template.indexOf("$Params") >= 0) { 58 | return StrUtils.replace(template, "$Params", StrUtils.arrayToString(args, ", ")); 59 | } 60 | if (template.indexOf("$Compact_Params") >= 0) { 61 | return StrUtils.replace(template, "$Compact_Params", StrUtils.arrayToString(args, ",")); 62 | } 63 | if (template.indexOf("$Lined_Params") >= 0) { 64 | return StrUtils.replace(template, "$Lined_Params", StrUtils.arrayToString(args, "||")); 65 | } 66 | if (template.indexOf("$Add_Params") >= 0) { 67 | return StrUtils.replace(template, "$Add_Params", StrUtils.arrayToString(args, "+")); 68 | } 69 | if (template.indexOf("$IN_Params") >= 0) { 70 | return StrUtils.replace(template, "$IN_Params", StrUtils.arrayToString(args, " in ")); 71 | } 72 | if (template.indexOf("$Pattern_Params") >= 0) { 73 | return StrUtils.replace(template, "$Pattern_Params", StrUtils.arrayToString(args, "%pattern")); 74 | } 75 | if (template.indexOf("$Startswith_Params") >= 0) { 76 | return StrUtils.replace(template, "$Startswith_Params", StrUtils.arrayToString(args, "%startswith")); 77 | } 78 | if (template.indexOf("$NVL_Params") >= 0) { 79 | if (args == null || args.length < 2) 80 | DialectException.throwEX("Nvl function require at least 2 parameters"); 81 | else { 82 | String s = "nvl(" + args[args.length - 2] + ", " + args[args.length - 1] + ")"; 83 | for (int i = args.length - 3; i > -1; i--) 84 | s = "nvl(" + args[i] + ", " + s + ")"; 85 | return StrUtils.replace(template, "$NVL_Params", s); 86 | } 87 | } 88 | return (String) DialectException.throwEX("jDialect found a template bug error, please submit this bug"); 89 | } else { 90 | int argsCount = 0; 91 | if (args != null) 92 | argsCount = args.length; 93 | String searchStr = Integer.toString(argsCount) + "="; 94 | if (template.indexOf(searchStr) < 0) 95 | DialectException.throwEX("Dialect " + d + "'s function \"" + functionName + "\" only support " 96 | + allowedParameterQTY(template) + " parameters"); 97 | String result = StrUtils.substringBetween(template + "|", searchStr, "|"); 98 | for (int i = 0; args != null && i < args.length; i++) { 99 | result = StrUtils.replace(result, "$P" + (i + 1), "" + args[i]); 100 | } 101 | return result; 102 | } 103 | } 104 | 105 | private static String allowedParameterQTY(String template) { 106 | StringBuilder sb = new StringBuilder(); 107 | for (int i = 0; i < 5; i++) { 108 | if (template.indexOf(Integer.toString(i) + "=") >= 0) 109 | sb.append(Integer.toString(i)).append(" or "); 110 | } 111 | if (sb.length() > 0) 112 | sb.setLength(sb.length() - 4); 113 | return sb.toString(); 114 | } 115 | } 116 | -------------------------------------------------------------------------------- /core/src/main/java/com/github/drinkjava2/jdialects/DialectType.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 | * use this file except in compliance with the License. You may obtain a copy of 6 | * the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by 7 | * applicable law or agreed to in writing, software distributed under the 8 | * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS 9 | * OF ANY KIND, either express or implied. See the License for the specific 10 | * language governing permissions and limitations under the License. 11 | */ 12 | package com.github.drinkjava2.jdialects; 13 | 14 | /** 15 | * DialectType is a enum list all dialects, if customized dialect, use 16 | * "Customized" type. 17 | * 18 | * @author Yong Zhu 19 | * @since 1.7.0 20 | */ 21 | @SuppressWarnings("all") 22 | public enum DialectType { //这个类没啥用,主要用于方便使用case遍历 23 | Customized, // 24 | DerbyDialect, OracleDialect, Oracle9Dialect, DamengDialect, GBaseDialect, AccessDialect, CobolDialect, DbfDialect, ExcelDialect, ParadoxDialect, SQLiteDialect, TextDialect, XMLDialect, Cache71Dialect, CUBRIDDialect, DataDirectOracle9Dialect, DB2390Dialect, DB2390V8Dialect, DB2400Dialect, DB297Dialect, DB2Dialect, DerbyTenFiveDialect, DerbyTenSevenDialect, DerbyTenSixDialect, FirebirdDialect, FrontBaseDialect, H2Dialect, HANAColumnStoreDialect, HANARowStoreDialect, HSQLDialect, Informix10Dialect, InformixDialect, Ingres10Dialect, Ingres9Dialect, IngresDialect, InterbaseDialect, JDataStoreDialect, MariaDB102Dialect, MariaDB103Dialect, MariaDB10Dialect, MariaDB53Dialect, MariaDBDialect, MckoiDialect, MimerSQLDialect, MySQL55Dialect, MySQL57Dialect, MySQL57InnoDBDialect, MySQL5Dialect, MySQL5InnoDBDialect, MySQL8Dialect, MySQLDialect, MySQLInnoDBDialect, MySQLMyISAMDialect, Oracle10gDialect, Oracle12cDialect, Oracle8iDialect, Oracle9iDialect, PointbaseDialect, PostgresPlusDialect, PostgreSQL81Dialect, PostgreSQL82Dialect, PostgreSQL91Dialect, PostgreSQL92Dialect, PostgreSQL93Dialect, PostgreSQL94Dialect, PostgreSQL95Dialect, PostgreSQL9Dialect, PostgreSQLDialect, ProgressDialect, RDMSOS2200Dialect, SAPDBDialect, SQLServer2005Dialect, SQLServer2008Dialect, SQLServer2012Dialect, SQLServerDialect, Sybase11Dialect, SybaseAnywhereDialect, SybaseASE157Dialect, SybaseASE15Dialect, SybaseDialect, Teradata14Dialect, TeradataDialect, TimesTenDialect; 25 | 26 | public static DialectType getDialectType(Dialect d) { 27 | DialectType diaType = null; 28 | try { 29 | diaType = DialectType.valueOf(d.getName()); 30 | } catch (Exception e) { 31 | diaType = DialectType.Customized; 32 | } 33 | return diaType; 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /core/src/main/java/com/github/drinkjava2/jdialects/Java8DateUtils.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 | * use this file except in compliance with the License. You may obtain a copy of 6 | * the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by 7 | * applicable law or agreed to in writing, software distributed under the 8 | * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS 9 | * OF ANY KIND, either express or implied. See the License for the specific 10 | * language governing permissions and limitations under the License. 11 | */ 12 | package com.github.drinkjava2.jdialects; 13 | 14 | /*- JAVA8_BEGIN */ 15 | import java.sql.Timestamp; 16 | import java.time.Instant; 17 | import java.time.LocalDate; 18 | import java.time.LocalDateTime; 19 | import java.time.LocalTime; 20 | import java.time.OffsetDateTime; 21 | import java.time.OffsetTime; 22 | import java.time.ZoneId; 23 | import java.time.ZonedDateTime; 24 | import java.util.Date; 25 | 26 | public abstract class Java8DateUtils {// NOSONAR 27 | 28 | private static final ZoneId zoneId = ZoneId.systemDefault(); 29 | 30 | public static LocalDate date2LocalDate(java.util.Date date) { 31 | Instant instant = new Date(date.getTime()).toInstant(); 32 | return instant.atZone(zoneId).toLocalDate(); 33 | } 34 | 35 | public static LocalDateTime date2LocalDateTime(Date date) { 36 | Instant instant = date.toInstant(); 37 | return instant.atZone(zoneId).toLocalDateTime(); 38 | } 39 | 40 | public static Date localDate2Date(LocalDate localDate) { 41 | ZonedDateTime zdt = localDate.atStartOfDay(zoneId); 42 | return Date.from(zdt.toInstant()); 43 | } 44 | 45 | public static Date localDateTime2Date(LocalDateTime localDateTime) { 46 | ZonedDateTime zdt = localDateTime.atZone(zoneId); 47 | return new Date(Date.from(zdt.toInstant()).getTime()); 48 | } 49 | 50 | // below to conver java8 date types to sql type 51 | 52 | public static java.sql.Date localDate2SqlDate(LocalDate localDate) { 53 | ZonedDateTime zdt = localDate.atStartOfDay(zoneId); 54 | return new java.sql.Date(Date.from(zdt.toInstant()).getTime()); 55 | } 56 | 57 | public static java.sql.Time localTime2SqlTime(LocalTime localTime) { 58 | return java.sql.Time.valueOf(localTime); 59 | } 60 | 61 | public static java.sql.Time offsetTime2SqlTime(OffsetTime offsetTime) { 62 | return java.sql.Time.valueOf(offsetTime.toLocalTime()); 63 | } 64 | 65 | public static java.sql.Timestamp instant2SqlTimestamp(Instant instant) { 66 | return new java.sql.Timestamp(Date.from(instant).getTime()); 67 | } 68 | 69 | public static java.sql.Timestamp localDateTime2SqlTimestamp(LocalDateTime localDateTime) { 70 | return Timestamp.valueOf(localDateTime); 71 | } 72 | 73 | public static java.sql.Timestamp offsetDateTime2SqlTimestamp(OffsetDateTime offsetDateTime) { 74 | return Timestamp.valueOf(offsetDateTime.toLocalDateTime()); 75 | } 76 | 77 | public static java.sql.Timestamp zonedDateTime2SqlTimestamp(ZonedDateTime zonedDateTime) { 78 | return Timestamp.valueOf(zonedDateTime.toLocalDateTime()); 79 | } 80 | 81 | // below to conver sql date types to java8 types 82 | public static LocalDate sqlDate2localDate(java.sql.Date date) { 83 | Instant instant = new Date(date.getTime()).toInstant(); 84 | return instant.atZone(zoneId).toLocalDate(); 85 | } 86 | 87 | public static LocalDateTime sqlDate2localDateTime(java.sql.Date date) { 88 | Instant instant = new Date(date.getTime()).toInstant(); 89 | return instant.atZone(zoneId).toLocalDateTime(); 90 | } 91 | 92 | public static LocalTime sqlTime2LocalTime(java.sql.Time time) { 93 | return time.toLocalTime(); 94 | } 95 | 96 | // extra 97 | public static LocalDateTime sqlTime2LocalDateTime(java.sql.Time time) { 98 | Instant instant = new Date(time.getTime()).toInstant(); 99 | return instant.atZone(zoneId).toLocalDateTime(); 100 | } 101 | 102 | public static OffsetTime sqlTime2OffsetTime(java.sql.Time time) { 103 | Instant instant = new Date(time.getTime()).toInstant(); 104 | return OffsetTime.ofInstant(instant, zoneId); 105 | } 106 | 107 | public static OffsetTime sqlTimestamp2OffsetTime(Timestamp t) { 108 | return OffsetTime.ofInstant(t.toInstant(), zoneId); 109 | } 110 | 111 | public static Instant sqlTimestamp2instant(Timestamp t) { 112 | return t.toInstant(); 113 | } 114 | 115 | public static LocalDateTime sqlTimestamp2LocalDateTime(Timestamp t) { 116 | return t.toLocalDateTime(); 117 | } 118 | 119 | public static OffsetDateTime sqlTimestamp2OffsetDateTime(Timestamp t) { 120 | return OffsetDateTime.ofInstant(t.toInstant(), zoneId); 121 | } 122 | 123 | public static ZonedDateTime sqlTimestamp2ZonedDateTime(Timestamp t) { 124 | return ZonedDateTime.ofInstant(t.toInstant(), zoneId); 125 | } 126 | 127 | public static LocalDate sqlTimestamp2LocalDate(Timestamp t) { 128 | Instant instant = new Date(t.getTime()).toInstant(); 129 | return instant.atZone(zoneId).toLocalDate(); 130 | } 131 | 132 | public static LocalTime sqlTimestamp2LocalTime(Timestamp t) { 133 | Instant instant = new Date(t.getTime()).toInstant(); 134 | return instant.atZone(zoneId).toLocalTime(); 135 | } 136 | } 137 | /* JAVA8_END */ -------------------------------------------------------------------------------- /core/src/main/java/com/github/drinkjava2/jdialects/JdbcUtil.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (C) 2016 Yong Zhu. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 | * use this file except in compliance with the License. You may obtain a copy of 6 | * the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 | * License for the specific language governing permissions and limitations under 14 | * the License. 15 | */ 16 | package com.github.drinkjava2.jdialects; 17 | 18 | import java.sql.Connection; 19 | import java.sql.PreparedStatement; 20 | import java.sql.ResultSet; 21 | import java.sql.SQLException; 22 | 23 | import javax.sql.DataSource; 24 | 25 | /** 26 | * A tiny pure JDBC tool to access database 27 | * 28 | * 29 | * @author Yong Zhu 30 | * @version 1.0.0 31 | */ 32 | public abstract class JdbcUtil {//NOSONAR 33 | 34 | /** 35 | * Return sql query result object in first row first column 36 | * @param Connection 37 | * @param sql 38 | * @param params 39 | * @return object in first row first column 40 | */ 41 | @SuppressWarnings("unchecked") 42 | public static T qryOneObject(Connection conneciton, String sql, Object... params) { 43 | ResultSet rs = null; 44 | try (PreparedStatement pst = conneciton.prepareStatement(sql)) { 45 | int i = 1; 46 | for (Object obj : params) 47 | pst.setObject(i++, obj); 48 | rs = pst.executeQuery(); 49 | if (rs.next()) 50 | return (T) rs.getObject(1); 51 | else 52 | return null; 53 | } catch (SQLException e) { 54 | throw new RuntimeException(e); 55 | } finally { 56 | if (rs != null) 57 | try { 58 | rs.close(); 59 | } catch (SQLException e) { 60 | //eat exception 61 | } 62 | } 63 | } 64 | 65 | /** 66 | * Execute a sql, return rows quantity be affected 67 | * @param connection 68 | * @param sql 69 | * @param params 70 | * @return rows quantity be affected 71 | */ 72 | public static int execute(Connection connection, String sql, Object... params) { 73 | try (PreparedStatement pst = connection.prepareStatement(sql)) { 74 | int i = 1; 75 | for (Object obj : params) 76 | pst.setObject(i++, obj); 77 | pst.execute(); 78 | return 1; 79 | } catch (SQLException e) { 80 | throw new RuntimeException(e); 81 | } 82 | } 83 | 84 | /** 85 | * Return sql query result object in first row first column 86 | * @param dataSource 87 | * @param sql 88 | * @param params 89 | * @return object in first row first column 90 | */ 91 | @SuppressWarnings("unchecked") 92 | public static T qryOneObject(DataSource dataSource, String sql, Object... params) { 93 | ResultSet rs = null; 94 | try (Connection con = dataSource.getConnection(); // 95 | PreparedStatement pst = con.prepareStatement(sql);) { 96 | int i = 1; 97 | for (Object obj : params) 98 | pst.setObject(i++, obj); 99 | rs = pst.executeQuery();// NOSONAR 100 | if (rs.next()) 101 | return (T) rs.getObject(1); 102 | else 103 | return null; 104 | } catch (SQLException e) { 105 | throw new RuntimeException(e); 106 | } finally { 107 | if (rs != null) 108 | try { 109 | rs.close(); 110 | } catch (SQLException e) { 111 | //eat exception 112 | } 113 | } 114 | } 115 | 116 | /** 117 | * Execute a sql, return rows quantity be affected 118 | * @param dataSource 119 | * @param sql 120 | * @param params 121 | * @return rows quantity be affected 122 | */ 123 | public static int execute(DataSource dataSource, String sql, Object... params) { 124 | try (Connection con = dataSource.getConnection(); // 125 | PreparedStatement pst = con.prepareStatement(sql);) { 126 | int i = 1; 127 | for (Object obj : params) 128 | pst.setObject(i++, obj); 129 | pst.execute(); 130 | return 1; 131 | } catch (SQLException e) { 132 | throw new RuntimeException(e); 133 | } 134 | } 135 | 136 | } 137 | -------------------------------------------------------------------------------- /core/src/main/java/com/github/drinkjava2/jdialects/NamingConversion.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 | * use this file except in compliance with the License. You may obtain a copy of 6 | * the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by 7 | * applicable law or agreed to in writing, software distributed under the 8 | * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS 9 | * OF ANY KIND, either express or implied. See the License for the specific 10 | * language governing permissions and limitations under the License. 11 | */ 12 | package com.github.drinkjava2.jdialects; 13 | 14 | /** 15 | * Set global NamingConversion for entity which have no @table and @column annotation 16 | * 17 | *
18 |  * For example: 
19 |  *   //OrderDetail.class map to OrderDetail database table, entity field OrderPrice map to OrderPrice column 
20 |  *   Dialect.setGlobalNamingRule(NamingRule.NONE);
21 |  *   
22 |  *   //OrderDetail.class map to order_detail database table, entity field OrderPrice map to order_price column
23 |  *   Dialect.setGlobalNamingRule(NamingRule.LOWER_CASE_UNDERSCORE);  //
24 |  *   
25 |  *   //OrderDetail.class map to ORDER_DETAIL database table, entity field OrderPrice map to ORDER_PRICE column
26 |  *   Dialect.setGlobalNamingRule(NamingRule.UPPER_CASE_UNDERSCORE);
27 |  * 
28 | * 29 | * @author Yong 30 | * @since 5.0.10 31 | */ 32 | public interface NamingConversion { 33 | 34 | /** Get table name from entity class */ 35 | public String getTableName(Class clazz); 36 | 37 | /** Get column name from entity field */ 38 | public String getColumnName(String entityField); 39 | 40 | public static final NamingConversion NONE = null; 41 | public static final NamingConversion LOWER_CASE_UNDERSCORE = new LowerCaseUnderscoreConversion(); 42 | public static final NamingConversion UPPER_CASE_UNDERSCORE = new UpperCaseUnderscoreConversion(); 43 | 44 | 45 | public static class LowerCaseUnderscoreConversion implements NamingConversion { 46 | @Override 47 | public String getTableName(Class clazz) { 48 | return StrUtils.camelToLowerCaseUnderScore(clazz.getSimpleName()); 49 | } 50 | 51 | @Override 52 | public String getColumnName(String entityField) { 53 | return StrUtils.camelToLowerCaseUnderScore(entityField); 54 | } 55 | } 56 | 57 | public static class UpperCaseUnderscoreConversion implements NamingConversion { 58 | @Override 59 | public String getTableName(Class clazz) { 60 | return StrUtils.camelToLowerCaseUnderScore(clazz.getSimpleName()).toUpperCase(); 61 | } 62 | 63 | @Override 64 | public String getColumnName(String entityField) { 65 | return StrUtils.camelToLowerCaseUnderScore(entityField).toUpperCase(); 66 | } 67 | } 68 | 69 | } 70 | -------------------------------------------------------------------------------- /core/src/main/java/com/github/drinkjava2/jdialects/PackageInfo.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 | * use this file except in compliance with the License. You may obtain a copy of 6 | * the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by 7 | * applicable law or agreed to in writing, software distributed under the 8 | * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS 9 | * OF ANY KIND, either express or implied. See the License for the specific 10 | * language governing permissions and limitations under the License. 11 | */ 12 | package com.github.drinkjava2.jdialects; 13 | 14 | /** 15 | * jDialect is a separate project has its own Maven central releases. 16 | */ 17 | public class PackageInfo {// NOSONAR 18 | 19 | } -------------------------------------------------------------------------------- /core/src/main/java/com/github/drinkjava2/jdialects/SqlFormatter.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 | * use this file except in compliance with the License. You may obtain a copy of 6 | * the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by 7 | * applicable law or agreed to in writing, software distributed under the 8 | * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS 9 | * OF ANY KIND, either express or implied. See the License for the specific 10 | * language governing permissions and limitations under the License. 11 | */ 12 | package com.github.drinkjava2.jdialects; 13 | 14 | import java.util.HashSet; 15 | import java.util.Set; 16 | 17 | /** 18 | * @author Yong Zhu 19 | * @since 2.0.4 20 | */ 21 | public class SqlFormatter { 22 | private static final Set keywords = new HashSet(); 23 | static { 24 | keywords.add("select"); 25 | keywords.add("insert"); 26 | keywords.add("update"); 27 | keywords.add("delete"); 28 | keywords.add("create"); 29 | keywords.add("drop"); 30 | keywords.add("alter"); 31 | 32 | keywords.add("from"); 33 | keywords.add("in"); 34 | keywords.add("exists"); 35 | keywords.add("where"); 36 | 37 | keywords.add("order"); 38 | keywords.add("group"); 39 | keywords.add("union"); 40 | 41 | keywords.add("inner"); 42 | keywords.add("left"); 43 | keywords.add("right"); 44 | keywords.add("outer"); 45 | } 46 | 47 | private static boolean isSpace(char c) { 48 | return c <= 32 || c == '\t' || c == '\r' || c == '\n'; 49 | } 50 | 51 | public static String format(String template) { 52 | StringBuilder sb = new StringBuilder(); 53 | char[] chars = (" " + template + " ").toCharArray(); 54 | // 0 0 ' 1 1 1 ' 0 0 0 55 | boolean inStr = false; 56 | boolean justAddedSpace = true; 57 | StringBuilder lastWord = new StringBuilder(); 58 | for (int i = 2; i < chars.length - 2; i++) { 59 | char c = chars[i]; 60 | if (isSpace(c)) { 61 | if (inStr) { 62 | sb.append(c); 63 | } else if (!justAddedSpace) { 64 | addLast(sb, lastWord); 65 | sb.append(' '); 66 | justAddedSpace = true; 67 | } 68 | } else if (c == '\'') { 69 | if (!inStr) 70 | addLast(sb, lastWord); 71 | sb.append('\''); 72 | inStr = !inStr; 73 | justAddedSpace = false; 74 | } else { 75 | justAddedSpace = false; 76 | if (inStr) 77 | sb.append(c); 78 | else 79 | lastWord.append(c); 80 | } 81 | } 82 | addLast(sb, lastWord); 83 | return sb.toString(); 84 | } 85 | 86 | private static void addLast(StringBuilder sb, StringBuilder lastWord) { 87 | if (lastWord.length() == 0) 88 | return; 89 | String last = lastWord.toString(); 90 | if (sb.length() > 0 && keywords.contains(last.toLowerCase())) 91 | sb.append("\n"); 92 | sb.append(last); 93 | lastWord.setLength(0); 94 | } 95 | 96 | } -------------------------------------------------------------------------------- /core/src/main/java/com/github/drinkjava2/jdialects/Type.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 | * use this file except in compliance with the License. You may obtain a copy of 6 | * the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by 7 | * applicable law or agreed to in writing, software distributed under the 8 | * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS 9 | * OF ANY KIND, either express or implied. See the License for the specific 10 | * language governing permissions and limitations under the License. 11 | */ 12 | package com.github.drinkjava2.jdialects; 13 | 14 | 15 | /** 16 | * Virtual SQL Type definitions 17 | * 18 | * @author Yong Zhu 19 | * @since 1.0.0 20 | */ 21 | public enum Type { 22 | BIGINT// 23 | , BINARY// 24 | , BIT// 25 | , BLOB// 26 | , BOOLEAN// 27 | , CHAR// 28 | , CLOB// 29 | , DATE// 30 | , DECIMAL// 31 | , DOUBLE// 32 | , FLOAT// 33 | , INTEGER// 34 | , JAVA_OBJECT// 35 | , LONGNVARCHAR// 36 | , LONGVARBINARY// 37 | , LONGVARCHAR// 38 | , NCHAR// 39 | , NCLOB// 40 | , NUMERIC// 41 | , NVARCHAR// 42 | , UNKNOW// 43 | , REAL// 44 | , SMALLINT// 45 | , TIME// 46 | , TIMESTAMP// 47 | , TINYINT// 48 | , VARBINARY// 49 | , VARCHAR 50 | //mysql 51 | , DATETIME 52 | , MEDIUMINT 53 | , INT 54 | , TINYBLOB 55 | , TINYTEXT 56 | , TEXT 57 | , MEDIUMBLOB 58 | , MEDIUMTEXT 59 | , LONGBLOB 60 | , LONGTEXT 61 | , YEAR 62 | , JSON 63 | //oracle 64 | , BINARY_FLOAT 65 | , DOUBLE_PRECISION 66 | , BINARY_DOUBLE 67 | , TIMESTAMP_WITH_TIME_ZONE 68 | , TIMESTAMP_WITH_LOCAL_TIME_ZONE 69 | , VARCHAR2 70 | ,INTERVAL_YEAR_TO_MONTH 71 | ,INTERVAL_DAY_TO_SECOND 72 | ; 73 | 74 | //TODO 此处需要考虑数据类型多对一的情况 75 | public static Type getByTypeName(String typeName) { 76 | for (Type val : Type.values()) { 77 | if (val.name().equalsIgnoreCase(typeName)) { 78 | return val; 79 | } 80 | } 81 | throw new DialectException("'" + typeName + "' can not be map to a dialect type"); 82 | } 83 | 84 | } 85 | -------------------------------------------------------------------------------- /core/src/main/java/com/github/drinkjava2/jdialects/annotation/jdia/AutoId.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 | * use this file except in compliance with the License. You may obtain a copy of 6 | * the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by 7 | * applicable law or agreed to in writing, software distributed under the 8 | * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS 9 | * OF ANY KIND, either express or implied. See the License for the specific 10 | * language governing permissions and limitations under the License. 11 | */ 12 | package com.github.drinkjava2.jdialects.annotation.jdia; 13 | 14 | import static java.lang.annotation.RetentionPolicy.RUNTIME; 15 | import static java.lang.annotation.ElementType.FIELD; 16 | 17 | import java.lang.annotation.Retention; 18 | import java.lang.annotation.Target; 19 | 20 | /** 21 | * A shortcut annotation equal to @GenertedValue(strategy=GenerationType.AUTO) 22 | */ 23 | @Target(FIELD) 24 | @Retention(RUNTIME) 25 | public @interface AutoId { 26 | } -------------------------------------------------------------------------------- /core/src/main/java/com/github/drinkjava2/jdialects/annotation/jdia/COLUMN.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 | * use this file except in compliance with the License. You may obtain a copy of 6 | * the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by 7 | * applicable law or agreed to in writing, software distributed under the 8 | * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS 9 | * OF ANY KIND, either express or implied. See the License for the specific 10 | * language governing permissions and limitations under the License. 11 | */ 12 | package com.github.drinkjava2.jdialects.annotation.jdia; 13 | 14 | import static java.lang.annotation.ElementType.FIELD; 15 | import static java.lang.annotation.RetentionPolicy.RUNTIME; 16 | 17 | import java.lang.annotation.Retention; 18 | import java.lang.annotation.Target; 19 | 20 | /** 21 | * Similar like JPA's @Column annotation, but enhenced with extra fields 22 | * like tail, comment, createTimestamp, UpdateTimestamp, createdBy, 23 | * LastModifiedBy 24 | * 25 | * @author Yong Zhu 26 | * @since 4.0.2 27 | */ 28 | @Target({ FIELD }) 29 | @Retention(RUNTIME) 30 | public @interface COLUMN { 31 | 32 | /** 33 | * (Optional) The name of the column. Defaults to the property or field name. 34 | */ 35 | String name() default ""; 36 | 37 | /** 38 | * (Optional) Whether the column is a unique key. This is a shortcut for the 39 | * UniqueConstraint annotation at the table level and is useful for 40 | * when the unique key constraint corresponds to only a single column. This 41 | * constraint applies in addition to any constraint entailed by primary key 42 | * mapping and to constraints specified at the table level. 43 | */ 44 | boolean unique() default false; 45 | 46 | /** 47 | * (Optional) Whether the database column is nullable. 48 | */ 49 | boolean nullable() default true; 50 | 51 | /** 52 | * (Optional) Whether the column is included in SQL INSERT statements generated 53 | * by the persistence provider. 54 | */ 55 | boolean insertable() default true; 56 | 57 | /** 58 | * (Optional) Whether the column is included in SQL UPDATE statements generated 59 | * by the persistence provider. 60 | */ 61 | boolean updatable() default true; 62 | 63 | /** 64 | * (Optional) The SQL fragment that is used when generating the DDL for the 65 | * column. 66 | *

67 | * Defaults to the generated SQL to create a column of the inferred type. 68 | */ 69 | String columnDefinition() default ""; 70 | 71 | /** 72 | * (Optional) The name of the table that contains the column. If absent the 73 | * column is assumed to be in the primary table. 74 | */ 75 | String table() default ""; 76 | 77 | /** 78 | * (Optional) The column length. (Applies only if a string-valued column is 79 | * used.) 80 | */ 81 | int length() default 255; 82 | 83 | /** 84 | * (Optional) The precision for a decimal (exact numeric) column. (Applies only 85 | * if a decimal column is used.) Value must be set by developer if used when 86 | * generating the DDL for the column. 87 | */ 88 | int precision() default 0; 89 | 90 | /** 91 | * (Optional) The scale for a decimal (exact numeric) column. (Applies only if a 92 | * decimal column is used.) 93 | */ 94 | int scale() default 0; 95 | 96 | /** (Optional)The comment String, usage: @Column(comment="abc"); */ 97 | String comment() default ""; 98 | 99 | /** (Optional)The Tail String, usage: @Column(tail="default now()"); */ 100 | String tail() default ""; 101 | 102 | /** (Optional)When insert bean will write current timestamp value */ 103 | boolean createTimestamp() default false; 104 | 105 | /** (Optional)When update bean will update current timestamp value */ 106 | boolean updateTimestamp() default false; 107 | 108 | /** 109 | * (Optional)When insert bean will write current userId, usage: 110 | * 111 | *

112 | 	 * 1. Write a class which have a getCurrentAuditor method
113 | 	 * 2. Call JBEANBOX.bind("AuditorAware", XxxClass.class) to bind it
114 | 	 * 
115 | 	 * For example: 
116 | 	 * public class XxxxUserAuditor{
117 | 	 * 	public Object getCurrentAuditor() { 
118 | 	 *     return XxxxSecurityTool.getUserXxxxId();
119 | 	 * 	}
120 | 	 * }
121 | 	 * 
122 | 	 * JBEANBOX.bind("AuditorAware", XxxxUserAuditor.class);
123 | 	 * 
124 | */ 125 | boolean createdBy() default false; 126 | 127 | /** (Optional)When update bean will write current userId, usage see createdBy */ 128 | boolean lastModifiedBy() default false; 129 | } 130 | -------------------------------------------------------------------------------- /core/src/main/java/com/github/drinkjava2/jdialects/annotation/jdia/CreateTimestamp.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 | * use this file except in compliance with the License. You may obtain a copy of 6 | * the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by 7 | * applicable law or agreed to in writing, software distributed under the 8 | * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS 9 | * OF ANY KIND, either express or implied. See the License for the specific 10 | * language governing permissions and limitations under the License. 11 | */ 12 | package com.github.drinkjava2.jdialects.annotation.jdia; 13 | 14 | import static java.lang.annotation.ElementType.FIELD; 15 | import static java.lang.annotation.RetentionPolicy.RUNTIME; 16 | 17 | import java.lang.annotation.Retention; 18 | import java.lang.annotation.Target; 19 | 20 | /** 21 | * Mark a CreateTimestamp column, explained by ORM tool 22 | * 23 | * @author Yong Zhu 24 | * @since 4.0.2 25 | */ 26 | @Target({ FIELD }) 27 | @Retention(RUNTIME) 28 | 29 | public @interface CreateTimestamp { 30 | } 31 | -------------------------------------------------------------------------------- /core/src/main/java/com/github/drinkjava2/jdialects/annotation/jdia/CreatedBy.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 | * use this file except in compliance with the License. You may obtain a copy of 6 | * the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by 7 | * applicable law or agreed to in writing, software distributed under the 8 | * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS 9 | * OF ANY KIND, either express or implied. See the License for the specific 10 | * language governing permissions and limitations under the License. 11 | */ 12 | package com.github.drinkjava2.jdialects.annotation.jdia; 13 | 14 | import static java.lang.annotation.ElementType.FIELD; 15 | import static java.lang.annotation.RetentionPolicy.RUNTIME; 16 | 17 | import java.lang.annotation.Retention; 18 | import java.lang.annotation.Target; 19 | 20 | /** 21 | * Mark a CreatedBy column, explained by ORM tool 22 | * 23 | * @author Yong Zhu 24 | * @since 4.0.2 25 | */ 26 | @Target({ FIELD }) 27 | @Retention(RUNTIME) 28 | 29 | public @interface CreatedBy { 30 | } 31 | -------------------------------------------------------------------------------- /core/src/main/java/com/github/drinkjava2/jdialects/annotation/jdia/FKey.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 | * use this file except in compliance with the License. You may obtain a copy of 6 | * the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by 7 | * applicable law or agreed to in writing, software distributed under the 8 | * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS 9 | * OF ANY KIND, either express or implied. See the License for the specific 10 | * language governing permissions and limitations under the License. 11 | */ 12 | package com.github.drinkjava2.jdialects.annotation.jdia; 13 | 14 | import static java.lang.annotation.ElementType.TYPE; 15 | import static java.lang.annotation.RetentionPolicy.RUNTIME; 16 | 17 | import java.lang.annotation.Retention; 18 | import java.lang.annotation.Target; 19 | 20 | /** 21 | * Specifies the foreign key 22 | * 23 | *
24 |  *   Example:
25 |  *
26 |  * @FKey(name = "fkey1", ddl = true, columns = { "field1", "field2" }, refs = { "Entity1", "field1", "field2" })
27 |  *   public class SomeClass()
28 |  * 
29 | * 30 | * @since jDialects 1.0.5 31 | */ 32 | @Target(TYPE) 33 | @Retention(RUNTIME) 34 | public @interface FKey { 35 | /** 36 | * (Optional) The name of the foreign key. 37 | */ 38 | String name() default ""; 39 | 40 | /** 41 | * Columns in this table 42 | */ 43 | String[] columns() default {}; 44 | 45 | /** 46 | * Referenced table name and columns, first is table name, followed by column 47 | * names, like "table1, col1, col2..." 48 | */ 49 | String[] refs() default {}; 50 | 51 | /** 52 | * if ddl set to false, will not output DDL when call TableModelUtils's 53 | * entity2Model() and oneEntity2Model methods 54 | */ 55 | boolean ddl() default true; 56 | } 57 | -------------------------------------------------------------------------------- /core/src/main/java/com/github/drinkjava2/jdialects/annotation/jdia/FKey1.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 | * use this file except in compliance with the License. You may obtain a copy of 6 | * the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by 7 | * applicable law or agreed to in writing, software distributed under the 8 | * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS 9 | * OF ANY KIND, either express or implied. See the License for the specific 10 | * language governing permissions and limitations under the License. 11 | */ 12 | package com.github.drinkjava2.jdialects.annotation.jdia; 13 | 14 | import static java.lang.annotation.ElementType.TYPE; 15 | import static java.lang.annotation.RetentionPolicy.RUNTIME; 16 | 17 | import java.lang.annotation.Retention; 18 | import java.lang.annotation.Target; 19 | 20 | 21 | /** 22 | * Specifies the foreign key 23 | 24 | *
25 |  *   Example:
26 |  *
27 |  *   @FKey1(name="fk_1", columns="field1,field2", ref="OtherTable, field1, field2")
28 |  *   public class SomeClass()
29 |  * 
30 | * 31 | * @since jDialects 1.0.5 32 | */ 33 | @Target(TYPE) 34 | @Retention(RUNTIME) 35 | public @interface FKey1 { 36 | /** 37 | * (Optional) The name of the foreign key. 38 | */ 39 | String name() default ""; 40 | 41 | 42 | /** 43 | * Columns in this table 44 | */ 45 | String[] columns() default {}; 46 | 47 | /** 48 | * Referenced table name and columns, first is table name, followed by column 49 | * names, like "table1, col1, col2..." 50 | */ 51 | String[] refs() default {}; 52 | 53 | /** 54 | * if ddl set to false, will not output DDL when call TableModelUtils's 55 | * entity2Model() and oneEntity2Model methods 56 | */ 57 | boolean ddl() default true; 58 | } 59 | -------------------------------------------------------------------------------- /core/src/main/java/com/github/drinkjava2/jdialects/annotation/jdia/FKey2.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 | * use this file except in compliance with the License. You may obtain a copy of 6 | * the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by 7 | * applicable law or agreed to in writing, software distributed under the 8 | * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS 9 | * OF ANY KIND, either express or implied. See the License for the specific 10 | * language governing permissions and limitations under the License. 11 | */ 12 | package com.github.drinkjava2.jdialects.annotation.jdia; 13 | 14 | import static java.lang.annotation.ElementType.TYPE; 15 | import static java.lang.annotation.RetentionPolicy.RUNTIME; 16 | 17 | import java.lang.annotation.Retention; 18 | import java.lang.annotation.Target; 19 | 20 | 21 | /** 22 | * Specifies the foreign key 23 | 24 | *
25 |  *   Example:
26 |  *
27 |  *   @FKey2(name="fk_1", columns="field1,field2", ref="OtherTable, field1, field2")
28 |  *   public class SomeClass()
29 |  * 
30 | * 31 | * @since jDialects 1.0.5 32 | */ 33 | @Target(TYPE) 34 | @Retention(RUNTIME) 35 | public @interface FKey2 { 36 | /** 37 | * (Optional) The name of the foreign key. 38 | */ 39 | String name() default ""; 40 | 41 | 42 | /** 43 | * Columns in this table 44 | */ 45 | String[] columns() default {}; 46 | 47 | /** 48 | * Referenced table name and columns, first is table name, followed by column 49 | * names, like "table1, col1, col2..." 50 | */ 51 | String[] refs() default {}; 52 | 53 | /** 54 | * if ddl set to false, will not output DDL when call TableModelUtils's 55 | * entity2Model() and oneEntity2Model methods 56 | */ 57 | boolean ddl() default true; 58 | } 59 | -------------------------------------------------------------------------------- /core/src/main/java/com/github/drinkjava2/jdialects/annotation/jdia/FKey3.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 | * use this file except in compliance with the License. You may obtain a copy of 6 | * the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by 7 | * applicable law or agreed to in writing, software distributed under the 8 | * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS 9 | * OF ANY KIND, either express or implied. See the License for the specific 10 | * language governing permissions and limitations under the License. 11 | */ 12 | package com.github.drinkjava2.jdialects.annotation.jdia; 13 | 14 | import static java.lang.annotation.ElementType.TYPE; 15 | import static java.lang.annotation.RetentionPolicy.RUNTIME; 16 | 17 | import java.lang.annotation.Retention; 18 | import java.lang.annotation.Target; 19 | 20 | 21 | /** 22 | * Specifies the foreign key 23 | 24 | *
25 |  *   Example:
26 |  *
27 |  *   @FKey3(name="fk_1", columns="field1,field2", ref="OtherTable, field1, field2")
28 |  *   public class SomeClass()
29 |  * 
30 | * 31 | * @since jDialects 1.0.5 32 | */ 33 | @Target(TYPE) 34 | @Retention(RUNTIME) 35 | public @interface FKey3 { 36 | /** 37 | * (Optional) The name of the foreign key. 38 | */ 39 | String name() default ""; 40 | 41 | 42 | /** 43 | * Columns in this table 44 | */ 45 | String[] columns() default {}; 46 | 47 | /** 48 | * Referenced table name and columns, first is table name, followed by column 49 | * names, like "table1, col1, col2..." 50 | */ 51 | String[] refs() default {}; 52 | 53 | /** 54 | * if ddl set to false, will not output DDL when call TableModelUtils's 55 | * entity2Model() and oneEntity2Model methods 56 | */ 57 | boolean ddl() default true; 58 | } 59 | -------------------------------------------------------------------------------- /core/src/main/java/com/github/drinkjava2/jdialects/annotation/jdia/IdentityId.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 | * use this file except in compliance with the License. You may obtain a copy of 6 | * the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by 7 | * applicable law or agreed to in writing, software distributed under the 8 | * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS 9 | * OF ANY KIND, either express or implied. See the License for the specific 10 | * language governing permissions and limitations under the License. 11 | */ 12 | package com.github.drinkjava2.jdialects.annotation.jdia; 13 | 14 | import static java.lang.annotation.RetentionPolicy.RUNTIME; 15 | import static java.lang.annotation.ElementType.FIELD; 16 | 17 | import java.lang.annotation.Retention; 18 | import java.lang.annotation.Target; 19 | 20 | /** 21 | * A shortcut annotation equal to 22 | * @GenertedValue(strategy=GenerationType.IDENTITY) 23 | */ 24 | @Target(FIELD) 25 | @Retention(RUNTIME) 26 | public @interface IdentityId { 27 | } -------------------------------------------------------------------------------- /core/src/main/java/com/github/drinkjava2/jdialects/annotation/jdia/LastModifiedBy.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 | * use this file except in compliance with the License. You may obtain a copy of 6 | * the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by 7 | * applicable law or agreed to in writing, software distributed under the 8 | * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS 9 | * OF ANY KIND, either express or implied. See the License for the specific 10 | * language governing permissions and limitations under the License. 11 | */ 12 | package com.github.drinkjava2.jdialects.annotation.jdia; 13 | 14 | import static java.lang.annotation.ElementType.FIELD; 15 | import static java.lang.annotation.RetentionPolicy.RUNTIME; 16 | 17 | import java.lang.annotation.Retention; 18 | import java.lang.annotation.Target; 19 | 20 | /** 21 | * Mark a LastModifiedBy column, explained by ORM tool 22 | * 23 | * @author Yong Zhu 24 | * @since 4.0.2 25 | */ 26 | @Target({ FIELD }) 27 | @Retention(RUNTIME) 28 | 29 | public @interface LastModifiedBy { 30 | } 31 | -------------------------------------------------------------------------------- /core/src/main/java/com/github/drinkjava2/jdialects/annotation/jdia/PKey.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 | * use this file except in compliance with the License. You may obtain a copy of 6 | * the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by 7 | * applicable law or agreed to in writing, software distributed under the 8 | * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS 9 | * OF ANY KIND, either express or implied. See the License for the specific 10 | * language governing permissions and limitations under the License. 11 | */ 12 | package com.github.drinkjava2.jdialects.annotation.jdia; 13 | 14 | import java.lang.annotation.Target; 15 | 16 | import com.github.drinkjava2.jdialects.annotation.jpa.Column; 17 | import com.github.drinkjava2.jdialects.annotation.jpa.GeneratedValue; 18 | 19 | import java.lang.annotation.Retention; 20 | import static java.lang.annotation.ElementType.FIELD; 21 | import static java.lang.annotation.ElementType.METHOD; 22 | import static java.lang.annotation.RetentionPolicy.RUNTIME; 23 | 24 | /** 25 | * Specifies the primary key of an entity. 26 | * The field or property to which the PKey annotation is applied 27 | * should be one of the following types: any Java primitive type; 28 | * any primitive wrapper type; 29 | * String; 30 | * java.util.Date; 31 | * java.sql.Date; 32 | * java.math.BigDecimal; 33 | * java.math.BigInteger. 34 | * 35 | *

The mapped column for the primary key of the entity is assumed 36 | * to be the primary key of the primary table. If no Column annotation 37 | * is specified, the primary key column name is assumed to be the name 38 | * of the primary key property or field. 39 | * 40 | *

41 |  *   Example:
42 |  *
43 |  *   @Pkey
44 |  *   public Long getId() { return id; }
45 |  * 
46 | * 47 | * @see Column 48 | * @see GeneratedValue 49 | * 50 | */ 51 | @Target({METHOD, FIELD}) 52 | @Retention(RUNTIME) 53 | 54 | public @interface PKey {} 55 | -------------------------------------------------------------------------------- /core/src/main/java/com/github/drinkjava2/jdialects/annotation/jdia/ShardDatabase.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 | * use this file except in compliance with the License. You may obtain a copy of 6 | * the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by 7 | * applicable law or agreed to in writing, software distributed under the 8 | * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS 9 | * OF ANY KIND, either express or implied. See the License for the specific 10 | * language governing permissions and limitations under the License. 11 | */ 12 | package com.github.drinkjava2.jdialects.annotation.jdia; 13 | 14 | import static java.lang.annotation.ElementType.FIELD; 15 | import static java.lang.annotation.RetentionPolicy.RUNTIME; 16 | import java.lang.annotation.Retention; 17 | import java.lang.annotation.Target; 18 | 19 | /** 20 | * To mark this column is a ShardDatabase column, one table only allow 1 column 21 | * be marked as ShardDatabase column 22 | * 23 | * @author Yong Zhu 24 | * @since 1.0.5 25 | */ 26 | 27 | @Target(FIELD) 28 | @Retention(RUNTIME) 29 | public @interface ShardDatabase { 30 | String[] value(); 31 | } 32 | -------------------------------------------------------------------------------- /core/src/main/java/com/github/drinkjava2/jdialects/annotation/jdia/ShardTable.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 | * use this file except in compliance with the License. You may obtain a copy of 6 | * the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by 7 | * applicable law or agreed to in writing, software distributed under the 8 | * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS 9 | * OF ANY KIND, either express or implied. See the License for the specific 10 | * language governing permissions and limitations under the License. 11 | */ 12 | package com.github.drinkjava2.jdialects.annotation.jdia; 13 | 14 | import static java.lang.annotation.ElementType.FIELD; 15 | import static java.lang.annotation.RetentionPolicy.RUNTIME; 16 | import java.lang.annotation.Retention; 17 | import java.lang.annotation.Target; 18 | 19 | /** 20 | * To mark this column is a shardTable column, one table only allow 1 column be 21 | * marked as shardiTable column 22 | * 23 | * @author Yong Zhu 24 | * @since 1.0.5 25 | */ 26 | 27 | @Target(FIELD) 28 | @Retention(RUNTIME) 29 | public @interface ShardTable { 30 | String[] value(); 31 | } 32 | -------------------------------------------------------------------------------- /core/src/main/java/com/github/drinkjava2/jdialects/annotation/jdia/SingleFKey.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 | * use this file except in compliance with the License. You may obtain a copy of 6 | * the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by 7 | * applicable law or agreed to in writing, software distributed under the 8 | * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS 9 | * OF ANY KIND, either express or implied. See the License for the specific 10 | * language governing permissions and limitations under the License. 11 | */ 12 | package com.github.drinkjava2.jdialects.annotation.jdia; 13 | 14 | import static java.lang.annotation.RetentionPolicy.RUNTIME; 15 | import static java.lang.annotation.ElementType.FIELD; 16 | 17 | import java.lang.annotation.Retention; 18 | import java.lang.annotation.Target; 19 | 20 | 21 | /** 22 | * A shortcut annotation of FKey, only for one column 23 | */ 24 | @Target(FIELD) 25 | @Retention(RUNTIME) 26 | public @interface SingleFKey { 27 | /** 28 | * (Optional) The name of the foreign key. 29 | */ 30 | String name() default ""; 31 | 32 | /** 33 | * Referenced table name and columns, first is table name, followed by column 34 | * names, like "table1, col1, col2..." 35 | */ 36 | String[] refs() default {}; 37 | 38 | /** 39 | * if ddl set to false, will not output DDL when call TableModelUtils's 40 | * entity2Model() and oneEntity2Model methods 41 | */ 42 | boolean ddl() default true; 43 | } 44 | -------------------------------------------------------------------------------- /core/src/main/java/com/github/drinkjava2/jdialects/annotation/jdia/SingleIndex.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 | * use this file except in compliance with the License. You may obtain a copy of 6 | * the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by 7 | * applicable law or agreed to in writing, software distributed under the 8 | * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS 9 | * OF ANY KIND, either express or implied. See the License for the specific 10 | * language governing permissions and limitations under the License. 11 | */ 12 | package com.github.drinkjava2.jdialects.annotation.jdia; 13 | 14 | import static java.lang.annotation.ElementType.FIELD; 15 | import static java.lang.annotation.RetentionPolicy.RUNTIME; 16 | import java.lang.annotation.Retention; 17 | import java.lang.annotation.Target; 18 | 19 | /** 20 | * A shortcut annotation of Index, only for one column 21 | */ 22 | @Target(FIELD) 23 | @Retention(RUNTIME) 24 | public @interface SingleIndex { 25 | 26 | /** 27 | * (Optional) The name of the index; defaults to a provider-generated name. 28 | */ 29 | String name() default ""; 30 | } 31 | -------------------------------------------------------------------------------- /core/src/main/java/com/github/drinkjava2/jdialects/annotation/jdia/SingleUnique.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 | * use this file except in compliance with the License. You may obtain a copy of 6 | * the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by 7 | * applicable law or agreed to in writing, software distributed under the 8 | * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS 9 | * OF ANY KIND, either express or implied. See the License for the specific 10 | * language governing permissions and limitations under the License. 11 | */ 12 | package com.github.drinkjava2.jdialects.annotation.jdia; 13 | 14 | import static java.lang.annotation.ElementType.FIELD; 15 | import static java.lang.annotation.RetentionPolicy.RUNTIME; 16 | import java.lang.annotation.Retention; 17 | import java.lang.annotation.Target; 18 | 19 | 20 | /** 21 | * A shortcut annotation of Unique, only for one column 22 | * 23 | * @author Yong Zhu 24 | * @since 1.0.5 25 | */ 26 | 27 | @Target(FIELD) 28 | @Retention(RUNTIME) 29 | public @interface SingleUnique { 30 | 31 | /** 32 | * (Optional) The name of the index; defaults to a provider-generated name. 33 | */ 34 | String name() default ""; 35 | } 36 | -------------------------------------------------------------------------------- /core/src/main/java/com/github/drinkjava2/jdialects/annotation/jdia/Snowflake.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 | * use this file except in compliance with the License. You may obtain a copy of 6 | * the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by 7 | * applicable law or agreed to in writing, software distributed under the 8 | * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS 9 | * OF ANY KIND, either express or implied. See the License for the specific 10 | * language governing permissions and limitations under the License. 11 | */ 12 | package com.github.drinkjava2.jdialects.annotation.jdia; 13 | 14 | import static java.lang.annotation.RetentionPolicy.RUNTIME; 15 | import static java.lang.annotation.ElementType.FIELD; 16 | 17 | import java.lang.annotation.Retention; 18 | import java.lang.annotation.Target; 19 | 20 | /** 21 | * Mark a Long type column value build by using SnowFlake algorithm from twitter 22 | *
23 | * 24 | * In jDialects SnowFlake algorithm source code originated from: 25 | * https://github.com/downgoon/snowflake 26 | * 27 | * The SnowFlake algorithm follows below basic rule:
28 | * 1 bit const=0
29 | * 41 bits Timestamp based on machine
30 | * 10 bits Confighured by user, used as machine ID, an example is assign 5 bits 31 | * for dataCenterID + 5 bits for workerID
32 | * 12 bits Sequence number
33 | * 34 | * 35 | */ 36 | @Target(FIELD) 37 | @Retention(RUNTIME) 38 | public @interface Snowflake { 39 | } 40 | -------------------------------------------------------------------------------- /core/src/main/java/com/github/drinkjava2/jdialects/annotation/jdia/TimeStampId.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 | * use this file except in compliance with the License. You may obtain a copy of 6 | * the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by 7 | * applicable law or agreed to in writing, software distributed under the 8 | * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS 9 | * OF ANY KIND, either express or implied. See the License for the specific 10 | * language governing permissions and limitations under the License. 11 | */ 12 | package com.github.drinkjava2.jdialects.annotation.jdia; 13 | 14 | import static java.lang.annotation.RetentionPolicy.RUNTIME; 15 | import static java.lang.annotation.ElementType.FIELD; 16 | 17 | import java.lang.annotation.Retention; 18 | import java.lang.annotation.Target; 19 | 20 | /** 21 | * A shortcut annotation equal to @GenertedValue(strategy=GenerationType.TIMESTAMP_ID) 22 | */ 23 | @Target(FIELD) 24 | @Retention(RUNTIME) 25 | public @interface TimeStampId { 26 | } 27 | -------------------------------------------------------------------------------- /core/src/main/java/com/github/drinkjava2/jdialects/annotation/jdia/UUID.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 | * use this file except in compliance with the License. You may obtain a copy of 6 | * the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by 7 | * applicable law or agreed to in writing, software distributed under the 8 | * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS 9 | * OF ANY KIND, either express or implied. See the License for the specific 10 | * language governing permissions and limitations under the License. 11 | */ 12 | package com.github.drinkjava2.jdialects.annotation.jdia; 13 | 14 | import static java.lang.annotation.RetentionPolicy.RUNTIME; 15 | import static java.lang.annotation.ElementType.FIELD; 16 | 17 | import java.lang.annotation.Retention; 18 | import java.lang.annotation.Target; 19 | 20 | /** 21 | * A shortcut annotation equal to @GenertedValue(strategy=GenerationType.UUID) 22 | */ 23 | @Target(FIELD) 24 | @Retention(RUNTIME) 25 | public @interface UUID { 26 | } 27 | -------------------------------------------------------------------------------- /core/src/main/java/com/github/drinkjava2/jdialects/annotation/jdia/UUID25.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 | * use this file except in compliance with the License. You may obtain a copy of 6 | * the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by 7 | * applicable law or agreed to in writing, software distributed under the 8 | * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS 9 | * OF ANY KIND, either express or implied. See the License for the specific 10 | * language governing permissions and limitations under the License. 11 | */ 12 | package com.github.drinkjava2.jdialects.annotation.jdia; 13 | 14 | import static java.lang.annotation.RetentionPolicy.RUNTIME; 15 | import static java.lang.annotation.ElementType.FIELD; 16 | 17 | import java.lang.annotation.Retention; 18 | import java.lang.annotation.Target; 19 | 20 | /** 21 | * A shortcut annotation equal to @GenertedValue(strategy=GenerationType.UUID25) 22 | */ 23 | @Target(FIELD) 24 | @Retention(RUNTIME) 25 | public @interface UUID25 { 26 | } 27 | -------------------------------------------------------------------------------- /core/src/main/java/com/github/drinkjava2/jdialects/annotation/jdia/UUID26.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 | * use this file except in compliance with the License. You may obtain a copy of 6 | * the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by 7 | * applicable law or agreed to in writing, software distributed under the 8 | * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS 9 | * OF ANY KIND, either express or implied. See the License for the specific 10 | * language governing permissions and limitations under the License. 11 | */ 12 | package com.github.drinkjava2.jdialects.annotation.jdia; 13 | 14 | import static java.lang.annotation.RetentionPolicy.RUNTIME; 15 | import static java.lang.annotation.ElementType.FIELD; 16 | 17 | import java.lang.annotation.Retention; 18 | import java.lang.annotation.Target; 19 | 20 | /** 21 | * A shortcut annotation equal to @GenertedValue(strategy=GenerationType.UUID26) 22 | */ 23 | @Target(FIELD) 24 | @Retention(RUNTIME) 25 | public @interface UUID26 { 26 | } 27 | -------------------------------------------------------------------------------- /core/src/main/java/com/github/drinkjava2/jdialects/annotation/jdia/UUID32.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 | * use this file except in compliance with the License. You may obtain a copy of 6 | * the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by 7 | * applicable law or agreed to in writing, software distributed under the 8 | * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS 9 | * OF ANY KIND, either express or implied. See the License for the specific 10 | * language governing permissions and limitations under the License. 11 | */ 12 | package com.github.drinkjava2.jdialects.annotation.jdia; 13 | 14 | import static java.lang.annotation.RetentionPolicy.RUNTIME; 15 | import static java.lang.annotation.ElementType.FIELD; 16 | 17 | import java.lang.annotation.Retention; 18 | import java.lang.annotation.Target; 19 | 20 | /** 21 | * A shortcut annotation equal to @GenertedValue(strategy=GenerationType.UUID32) 22 | */ 23 | @Target(FIELD) 24 | @Retention(RUNTIME) 25 | public @interface UUID32 { 26 | } 27 | -------------------------------------------------------------------------------- /core/src/main/java/com/github/drinkjava2/jdialects/annotation/jdia/UUID36.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 | * use this file except in compliance with the License. You may obtain a copy of 6 | * the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by 7 | * applicable law or agreed to in writing, software distributed under the 8 | * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS 9 | * OF ANY KIND, either express or implied. See the License for the specific 10 | * language governing permissions and limitations under the License. 11 | */ 12 | package com.github.drinkjava2.jdialects.annotation.jdia; 13 | 14 | import static java.lang.annotation.RetentionPolicy.RUNTIME; 15 | import static java.lang.annotation.ElementType.FIELD; 16 | 17 | import java.lang.annotation.Retention; 18 | import java.lang.annotation.Target; 19 | 20 | /** 21 | * A shortcut annotation equal to @GenertedValue(strategy=GenerationType.UUID36) 22 | */ 23 | @Target(FIELD) 24 | @Retention(RUNTIME) 25 | public @interface UUID36 { 26 | } 27 | -------------------------------------------------------------------------------- /core/src/main/java/com/github/drinkjava2/jdialects/annotation/jdia/UUIDAny.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 | * use this file except in compliance with the License. You may obtain a copy of 6 | * the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by 7 | * applicable law or agreed to in writing, software distributed under the 8 | * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS 9 | * OF ANY KIND, either express or implied. See the License for the specific 10 | * language governing permissions and limitations under the License. 11 | */ 12 | package com.github.drinkjava2.jdialects.annotation.jdia; 13 | 14 | import static java.lang.annotation.ElementType.FIELD; 15 | import static java.lang.annotation.ElementType.TYPE; 16 | import static java.lang.annotation.RetentionPolicy.RUNTIME; 17 | 18 | import java.lang.annotation.Retention; 19 | import java.lang.annotation.Target; 20 | 21 | import com.github.drinkjava2.jdialects.annotation.jpa.GeneratedValue; 22 | 23 | /** 24 | * Defines a primary key generator that may be referenced by name when 25 | * a generator element is specified for the {@link GeneratedValue} 26 | * annotation. A UUIDAny generator may be specified on the entity 27 | * class or on the primary key field or property. The scope of the 28 | * generator name is global to the persistence unit (across all 29 | * generator types). 30 | * 31 | *
32 |  *   Example:
33 |  *
34 |  *   @UUIDAny(name="uuid_100", length=100)
35 |  * 
36 | * 37 | * @since Java Persistence 1.0 38 | */ 39 | @Target({TYPE, FIELD}) 40 | @Retention(RUNTIME) 41 | public @interface UUIDAny { 42 | /** 43 | * (Required) A unique generator name that can be referenced by one or more 44 | * classes to be the generator for primary key values. 45 | */ 46 | String name(); 47 | 48 | /** 49 | * The length of the UUID 50 | */ 51 | int length() default 20; 52 | } 53 | -------------------------------------------------------------------------------- /core/src/main/java/com/github/drinkjava2/jdialects/annotation/jdia/UpdateTimestamp.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 | * use this file except in compliance with the License. You may obtain a copy of 6 | * the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by 7 | * applicable law or agreed to in writing, software distributed under the 8 | * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS 9 | * OF ANY KIND, either express or implied. See the License for the specific 10 | * language governing permissions and limitations under the License. 11 | */ 12 | package com.github.drinkjava2.jdialects.annotation.jdia; 13 | 14 | import static java.lang.annotation.ElementType.FIELD; 15 | import static java.lang.annotation.RetentionPolicy.RUNTIME; 16 | 17 | import java.lang.annotation.Retention; 18 | import java.lang.annotation.Target; 19 | 20 | /** 21 | * Mark a UpdateTimestamp column, explained by ORM tool 22 | * 23 | * @author Yong Zhu 24 | * @since 4.0.2 25 | */ 26 | @Target({ FIELD }) 27 | @Retention(RUNTIME) 28 | 29 | public @interface UpdateTimestamp { 30 | } 31 | -------------------------------------------------------------------------------- /core/src/main/java/com/github/drinkjava2/jdialects/annotation/jpa/Column.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 | * use this file except in compliance with the License. You may obtain a copy of 6 | * the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by 7 | * applicable law or agreed to in writing, software distributed under the 8 | * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS 9 | * OF ANY KIND, either express or implied. See the License for the specific 10 | * language governing permissions and limitations under the License. 11 | */ 12 | package com.github.drinkjava2.jdialects.annotation.jpa; 13 | 14 | import java.lang.annotation.Target; 15 | import java.lang.annotation.Retention; 16 | import static java.lang.annotation.ElementType.METHOD; 17 | import static java.lang.annotation.ElementType.FIELD; 18 | import static java.lang.annotation.RetentionPolicy.RUNTIME; 19 | 20 | /** 21 | * Specifies the mapped column for a persistent property or field. 22 | * If no Column annotation is specified, the default values apply. 23 | * 24 | *
 25 |  *    Example 1:
 26 |  *
 27 |  *    @Column(name="DESC", nullable=false, length=512)
 28 |  *    public String getDescription() { return description; }
 29 |  *
 30 |  *    Example 2:
 31 |  *
 32 |  *    @Column(name="DESC",
 33 |  *            columnDefinition="CLOB NOT NULL",
 34 |  *            table="EMP_DETAIL")
 35 |  *    @Lob
 36 |  *    public String getDescription() { return description; }
 37 |  *
 38 |  *    Example 3:
 39 |  *
 40 |  *    @Column(name="ORDER_COST", updatable=false, precision=12, scale=2)
 41 |  *    public BigDecimal getCost() { return cost; }
 42 |  *
 43 |  * 
44 | * 45 | * 46 | * @since Java Persistence 1.0 47 | */ 48 | @Target({METHOD, FIELD}) 49 | @Retention(RUNTIME) 50 | public @interface Column { 51 | 52 | /** 53 | * (Optional) The name of the column. Defaults to 54 | * the property or field name. 55 | */ 56 | String name() default ""; 57 | 58 | /** 59 | * (Optional) Whether the column is a unique key. This is a 60 | * shortcut for the UniqueConstraint annotation at the table 61 | * level and is useful for when the unique key constraint 62 | * corresponds to only a single column. This constraint applies 63 | * in addition to any constraint entailed by primary key mapping and 64 | * to constraints specified at the table level. 65 | */ 66 | boolean unique() default false; 67 | 68 | /** 69 | * (Optional) Whether the database column is nullable. 70 | */ 71 | boolean nullable() default true; 72 | 73 | /** 74 | * (Optional) Whether the column is included in SQL INSERT 75 | * statements generated by the persistence provider. 76 | */ 77 | boolean insertable() default true; 78 | 79 | /** 80 | * (Optional) Whether the column is included in SQL UPDATE 81 | * statements generated by the persistence provider. 82 | */ 83 | boolean updatable() default true; 84 | 85 | /** 86 | * (Optional) The SQL fragment that is used when 87 | * generating the DDL for the column. 88 | *

Defaults to the generated SQL to create a 89 | * column of the inferred type. 90 | */ 91 | String columnDefinition() default ""; 92 | 93 | /** 94 | * (Optional) The name of the table that contains the column. 95 | * If absent the column is assumed to be in the primary table. 96 | */ 97 | String table() default ""; 98 | 99 | /** 100 | * (Optional) The column length. (Applies only if a 101 | * string-valued column is used.) 102 | */ 103 | int length() default 255; 104 | 105 | /** 106 | * (Optional) The precision for a decimal (exact numeric) 107 | * column. (Applies only if a decimal column is used.) 108 | * Value must be set by developer if used when generating 109 | * the DDL for the column. 110 | */ 111 | int precision() default 0; 112 | 113 | /** 114 | * (Optional) The scale for a decimal (exact numeric) column. 115 | * (Applies only if a decimal column is used.) 116 | */ 117 | int scale() default 0; 118 | } 119 | -------------------------------------------------------------------------------- /core/src/main/java/com/github/drinkjava2/jdialects/annotation/jpa/Convert.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 | * use this file except in compliance with the License. You may obtain a copy of 6 | * the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by 7 | * applicable law or agreed to in writing, software distributed under the 8 | * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS 9 | * OF ANY KIND, either express or implied. See the License for the specific 10 | * language governing permissions and limitations under the License. 11 | */ 12 | package com.github.drinkjava2.jdialects.annotation.jpa; 13 | 14 | import static java.lang.annotation.ElementType.FIELD; 15 | import static java.lang.annotation.RetentionPolicy.RUNTIME; 16 | 17 | import java.lang.annotation.Retention; 18 | import java.lang.annotation.Target; 19 | 20 | /** 21 | * The value of this Convert annotation point to a class, the implementation of 22 | * this class will used to convert field value to database column value, and 23 | * column value to database, for example: 24 | * 25 | *

26 |  * @Convert(XxxDateConverter.class)
27 |  * private Date date;// Tell ORM Tool use XxxDateConverter to do the convert
28 |  * 
29 |  * @Convert(XxxEnumTranslator.class)
30 |  * private Enum some1;// Tell ORM Tool use XxxEnumTranslator to do the convert
31 |  * 
32 |  * @Convert()
33 |  * private Enum some2; // Tell ORM Tool decide how to do the convert by ORM tool itself
34 |  * 
35 |  * 
36 | */ 37 | @Target(FIELD) 38 | @Retention(RUNTIME) 39 | public @interface Convert { 40 | Class value() default void.class; 41 | 42 | /** 43 | * Specifies the converter to be applied. A value for this element must be 44 | * specified if multiple converters would otherwise apply. 45 | */ 46 | Class converter() default void.class;// equal to value, for compatible to JPA only 47 | 48 | /** 49 | * The attributeName element must be specified unless the 50 | * Convert annotation is on an attribute of basic type or on an 51 | * element collection of basic type. In these cases, the 52 | * attributeName element must not be specified. 53 | */ 54 | String attributeName() default ""; // ignored, for compatible to JPA only 55 | 56 | /** 57 | * Used to disable an auto-apply or inherited converter. If disableConversion is 58 | * true, the converter element should not be specified. 59 | */ 60 | boolean disableConversion() default false; // for compatible to JPA only 61 | } -------------------------------------------------------------------------------- /core/src/main/java/com/github/drinkjava2/jdialects/annotation/jpa/Entity.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 | * use this file except in compliance with the License. You may obtain a copy of 6 | * the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by 7 | * applicable law or agreed to in writing, software distributed under the 8 | * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS 9 | * OF ANY KIND, either express or implied. See the License for the specific 10 | * language governing permissions and limitations under the License. 11 | */ 12 | package com.github.drinkjava2.jdialects.annotation.jpa; 13 | 14 | import java.lang.annotation.Target; 15 | import java.lang.annotation.Retention; 16 | import java.lang.annotation.Documented; 17 | import static java.lang.annotation.ElementType.TYPE; 18 | import static java.lang.annotation.RetentionPolicy.RUNTIME; 19 | 20 | /** 21 | * Specifies that the class is an entity. This annotation is applied to the 22 | * entity class. 23 | * 24 | * @since Java Persistence 1.0 25 | */ 26 | @Documented 27 | @Target(TYPE) 28 | @Retention(RUNTIME) 29 | public @interface Entity { 30 | 31 | /** 32 | * (Optional) The entity name. Defaults to the unqualified 33 | * name of the entity class. This name is used to refer to the 34 | * entity in queries. The name must not be a reserved literal 35 | * in the Java Persistence query language. 36 | */ 37 | String name() default ""; 38 | } 39 | -------------------------------------------------------------------------------- /core/src/main/java/com/github/drinkjava2/jdialects/annotation/jpa/EnumType.java: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * Copyright (c) 2008 - 2013 Oracle Corporation. All rights reserved. 3 | * 4 | * This program and the accompanying materials are made available under the 5 | * terms of the Eclipse Public License v1.0 and Eclipse Distribution License v. 1.0 6 | * which accompanies this distribution. 7 | * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html 8 | * and the Eclipse Distribution License is available at 9 | * http://www.eclipse.org/org/documents/edl-v10.php. 10 | * 11 | * Contributors: 12 | * Linda DeMichiel - Java Persistence 2.1 13 | * Linda DeMichiel - Java Persistence 2.0 14 | * 15 | ******************************************************************************/ 16 | package com.github.drinkjava2.jdialects.annotation.jpa; 17 | 18 | /** 19 | * Defines mapping for enumerated types. The constants of this 20 | * enumerated type specify how a persistent property or 21 | * field of an enumerated type should be persisted. 22 | * 23 | * @since Java Persistence 1.0 24 | */ 25 | public enum EnumType { 26 | /** Persist enumerated type property or field as an integer. */ 27 | ORDINAL, 28 | 29 | /** Persist enumerated type property or field as a string. */ 30 | STRING 31 | } 32 | -------------------------------------------------------------------------------- /core/src/main/java/com/github/drinkjava2/jdialects/annotation/jpa/Enumerated.java: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * Copyright (c) 2008 - 2014 Oracle Corporation. All rights reserved. 3 | * 4 | * This program and the accompanying materials are made available under the 5 | * terms of the Eclipse Public License v1.0 and Eclipse Distribution License v. 1.0 6 | * which accompanies this distribution. 7 | * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html 8 | * and the Eclipse Distribution License is available at 9 | * http://www.eclipse.org/org/documents/edl-v10.php. 10 | * 11 | * Contributors: 12 | * Linda DeMichiel - Java Persistence 2.1 13 | * Linda DeMichiel - Java Persistence 2.0 14 | * 15 | ******************************************************************************/ 16 | package com.github.drinkjava2.jdialects.annotation.jpa; 17 | 18 | import static java.lang.annotation.ElementType.FIELD; 19 | import static java.lang.annotation.RetentionPolicy.RUNTIME; 20 | 21 | import java.lang.annotation.Retention; 22 | import java.lang.annotation.Target; 23 | 24 | /** 25 | * Specifies that a persistent property or field should be persisted as a 26 | * enumerated type. The Enumerated annotation may be used in 27 | * conjunction with the Basic annotation, or in conjunction with 28 | * the ElementCollection annotation when the element collection 29 | * value is of basic type. If the enumerated type is not specified or the 30 | * Enumerated annotation is not used, the EnumType 31 | * value is assumed to be ORDINAL. 32 | * 33 | *
34 |  *   Example:
35 |  *
36 |  *   public enum EmployeeStatus {FULL_TIME, PART_TIME, CONTRACT}
37 |  *
38 |  *   public enum SalaryRate {JUNIOR, SENIOR, MANAGER, EXECUTIVE}
39 |  *
40 |  *   @Entity public class Employee {
41 |  *       public EmployeeStatus getStatus() {...}
42 |  *       ...
43 |  *       @Enumerated(STRING)
44 |  *       public SalaryRate getPayScale() {...}
45 |  *       ...
46 |  *   }
47 |  * 
48 | * 49 | * @see Basic 50 | * @see ElementCollection 51 | * 52 | * @since Java Persistence 1.0 53 | */ 54 | @Target({ FIELD }) 55 | @Retention(RUNTIME) 56 | public @interface Enumerated { 57 | 58 | /** (Optional) The type used in mapping an enum type. */ 59 | EnumType value() default EnumType.ORDINAL; 60 | } 61 | -------------------------------------------------------------------------------- /core/src/main/java/com/github/drinkjava2/jdialects/annotation/jpa/GeneratedValue.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 | * use this file except in compliance with the License. You may obtain a copy of 6 | * the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by 7 | * applicable law or agreed to in writing, software distributed under the 8 | * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS 9 | * OF ANY KIND, either express or implied. See the License for the specific 10 | * language governing permissions and limitations under the License. 11 | */ 12 | package com.github.drinkjava2.jdialects.annotation.jpa; 13 | 14 | import static com.github.drinkjava2.jdialects.annotation.jpa.GenerationType.AUTO; 15 | import static java.lang.annotation.ElementType.FIELD; 16 | import static java.lang.annotation.ElementType.METHOD; 17 | import static java.lang.annotation.RetentionPolicy.RUNTIME; 18 | 19 | import java.lang.annotation.Retention; 20 | import java.lang.annotation.Target; 21 | 22 | /** 23 | * Provides for the specification of generation strategies for the 24 | * values of primary keys. 25 | * 26 | *

The GeneratedValue annotation 27 | * may be applied to a primary key property or field of an entity or 28 | * mapped superclass in conjunction with the {@link Id} annotation. 29 | * The use of the GeneratedValue annotation is only 30 | * required to be supported for simple primary keys. Use of the 31 | * GeneratedValue annotation is not supported for derived 32 | * primary keys. 33 | * 34 | *

35 |  *
36 |  *     Example 1:
37 |  *
38 |  *     @Id
39 |  *     @GeneratedValue(strategy=SEQUENCE, generator="CUST_SEQ")
40 |  *     @Column(name="CUST_ID")
41 |  *     public Long getId() { return id; }
42 |  *
43 |  *     Example 2:
44 |  *
45 |  *     @Id
46 |  *     @GeneratedValue(strategy=TABLE, generator="CUST_GEN")
47 |  *     @Column(name="CUST_ID")
48 |  *     Long id;
49 |  * 
50 | * 51 | * @see Id 52 | * @see TableGenerator 53 | * @see SequenceGenerator 54 | * 55 | * @since Java Persistence 1.0 56 | */ 57 | @Target({METHOD, FIELD}) 58 | @Retention(RUNTIME) 59 | 60 | public @interface GeneratedValue { 61 | 62 | /** 63 | * (Optional) The primary key generation strategy 64 | * that the persistence provider must use to 65 | * generate the annotated entity primary key. 66 | */ 67 | GenerationType strategy() default AUTO; 68 | 69 | /** 70 | * (Optional) The name of the primary key generator 71 | * to use as specified in the {@link SequenceGenerator} 72 | * or {@link TableGenerator} annotation. 73 | *

Defaults to the id generator supplied by persistence provider. 74 | */ 75 | String generator() default ""; 76 | } 77 | -------------------------------------------------------------------------------- /core/src/main/java/com/github/drinkjava2/jdialects/annotation/jpa/GenerationType.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 | * use this file except in compliance with the License. You may obtain a copy of 6 | * the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by 7 | * applicable law or agreed to in writing, software distributed under the 8 | * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS 9 | * OF ANY KIND, either express or implied. See the License for the specific 10 | * language governing permissions and limitations under the License. 11 | */ 12 | package com.github.drinkjava2.jdialects.annotation.jpa; 13 | 14 | /** 15 | * Defines the types of primary key generation strategies. 16 | * 17 | * @see GeneratedValue 18 | * 19 | * @since Java Persistence 1.0 20 | */ 21 | public enum GenerationType { 22 | 23 | /** 24 | * Indicates that the persistence provider must assign primary keys for the 25 | * entity using an underlying database table to ensure uniqueness. 26 | */ 27 | TABLE, 28 | 29 | /** 30 | * Indicates that the persistence provider must assign primary keys for the 31 | * entity using a database sequence. 32 | */ 33 | SEQUENCE, 34 | 35 | /** 36 | * Indicates that the persistence provider must assign primary keys for the 37 | * entity using a database identity column. 38 | */ 39 | IDENTITY, 40 | 41 | /** 42 | * Indicates that the persistence provider should pick an appropriate 43 | * strategy for the particular database. The AUTO generation 44 | * strategy may expect a database resource to exist, or it may attempt to 45 | * create one. A vendor may provide documentation on how to create such 46 | * resources in the event that it does not support schema generation or 47 | * cannot create the schema resource at runtime. 48 | */ 49 | AUTO, 50 | 51 | /** A 32 character length UUID */ 52 | UUID, 53 | 54 | /** A 25 character length UUID */ 55 | UUID25, 56 | 57 | /** A 26 character length UUID */ 58 | UUID26, 59 | 60 | /** A 32 character length UUID */ 61 | UUID32, 62 | 63 | /** A 36 character length UUID */ 64 | UUID36, 65 | 66 | /** A Any length UUID */ 67 | UUID_ANY, 68 | 69 | /** A sorted UUID */ 70 | SORTED_UUID, 71 | 72 | /** A TimeStamp */ 73 | TIMESTAMP, 74 | 75 | /** A Snowflake ID */ 76 | SNOWFLAKE, 77 | 78 | /** Unknow or Customized IdGenerators */ 79 | OTHER 80 | } 81 | -------------------------------------------------------------------------------- /core/src/main/java/com/github/drinkjava2/jdialects/annotation/jpa/Id.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 | * use this file except in compliance with the License. You may obtain a copy of 6 | * the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by 7 | * applicable law or agreed to in writing, software distributed under the 8 | * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS 9 | * OF ANY KIND, either express or implied. See the License for the specific 10 | * language governing permissions and limitations under the License. 11 | */ 12 | package com.github.drinkjava2.jdialects.annotation.jpa; 13 | 14 | import java.lang.annotation.Target; 15 | import java.lang.annotation.Retention; 16 | import static java.lang.annotation.ElementType.FIELD; 17 | import static java.lang.annotation.ElementType.METHOD; 18 | import static java.lang.annotation.RetentionPolicy.RUNTIME; 19 | 20 | /** 21 | * Specifies the primary key of an entity. 22 | * The field or property to which the Id annotation is applied 23 | * should be one of the following types: any Java primitive type; 24 | * any primitive wrapper type; 25 | * String; 26 | * java.util.Date; 27 | * java.sql.Date; 28 | * java.math.BigDecimal; 29 | * java.math.BigInteger. 30 | * 31 | *

The mapped column for the primary key of the entity is assumed 32 | * to be the primary key of the primary table. If no Column annotation 33 | * is specified, the primary key column name is assumed to be the name 34 | * of the primary key property or field. 35 | * 36 | *

37 |  *   Example:
38 |  *
39 |  *   @Id
40 |  *   public Long getId() { return id; }
41 |  * 
42 | * 43 | * @see Column 44 | * @see GeneratedValue 45 | * 46 | * @since Java Persistence 1.0 47 | */ 48 | @Target({METHOD, FIELD}) 49 | @Retention(RUNTIME) 50 | 51 | public @interface Id {} 52 | -------------------------------------------------------------------------------- /core/src/main/java/com/github/drinkjava2/jdialects/annotation/jpa/Index.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 | * use this file except in compliance with the License. You may obtain a copy of 6 | * the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by 7 | * applicable law or agreed to in writing, software distributed under the 8 | * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS 9 | * OF ANY KIND, either express or implied. See the License for the specific 10 | * language governing permissions and limitations under the License. 11 | */ 12 | package com.github.drinkjava2.jdialects.annotation.jpa; 13 | 14 | import static java.lang.annotation.RetentionPolicy.RUNTIME; 15 | import java.lang.annotation.Retention; 16 | import java.lang.annotation.Target; 17 | 18 | /** 19 | * Used in schema generation to specify creation of an index. 20 | *

21 | * Note that it is not necessary to specify an index for a primary key, 22 | * as the primary key index will be created automatically. 23 | * 24 | *

25 | * The syntax of the columnList element is a 26 | * column_list, as follows: 27 | * 28 | *

29 |  *    column::= index_column [,index_column]*
30 |  *    index_column::= column_name [ASC | DESC]
31 |  * 
32 | * 33 | *

If ASC or DESC is not specified, 34 | * ASC (ascending order) is assumed. 35 | * 36 | * @see Table 37 | * @see SecondaryTable 38 | * @see CollectionTable 39 | * @see JoinTable 40 | * @see TableIdGeneratorTest 41 | * 42 | * @since Java Persistence 2.1 43 | * 44 | */ 45 | @Target({}) 46 | @Retention(RUNTIME) 47 | public @interface Index { 48 | 49 | /** 50 | * (Optional) The name of the index; defaults to a provider-generated name. 51 | */ 52 | String name() default ""; 53 | 54 | /** 55 | * (Required) The names of the columns to be included in the index, 56 | * in order. 57 | */ 58 | String columnList(); 59 | 60 | /** 61 | * (Optional) Whether the index is unique. 62 | */ 63 | boolean unique() default false; 64 | 65 | } 66 | -------------------------------------------------------------------------------- /core/src/main/java/com/github/drinkjava2/jdialects/annotation/jpa/SequenceGenerator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 | * use this file except in compliance with the License. You may obtain a copy of 6 | * the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by 7 | * applicable law or agreed to in writing, software distributed under the 8 | * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS 9 | * OF ANY KIND, either express or implied. See the License for the specific 10 | * language governing permissions and limitations under the License. 11 | */ 12 | package com.github.drinkjava2.jdialects.annotation.jpa; 13 | 14 | import static java.lang.annotation.ElementType.FIELD; 15 | import static java.lang.annotation.ElementType.TYPE; 16 | import static java.lang.annotation.RetentionPolicy.RUNTIME; 17 | 18 | import java.lang.annotation.Retention; 19 | import java.lang.annotation.Target; 20 | 21 | /** 22 | * Defines a primary key generator that may be referenced by name when 23 | * a generator element is specified for the {@link GeneratedValue} 24 | * annotation. A sequence generator may be specified on the entity 25 | * class or on the primary key field or property. The scope of the 26 | * generator name is global to the persistence unit (across all 27 | * generator types). 28 | * 29 | *

30 |  *   Example:
31 |  *
32 |  *   @SequenceGenerator(name="EMP_SEQ", allocationSize=25)
33 |  * 
34 | * 35 | * @since Java Persistence 1.0 36 | */ 37 | @Target({TYPE, FIELD}) 38 | @Retention(RUNTIME) 39 | public @interface SequenceGenerator { 40 | 41 | /** 42 | * (Required) A unique generator name that can be referenced 43 | * by one or more classes to be the generator for primary key 44 | * values. 45 | */ 46 | String name(); 47 | 48 | /** 49 | * (Optional) The name of the database sequence object from 50 | * which to obtain primary key values. 51 | *

Defaults to a provider-chosen value. 52 | */ 53 | String sequenceName() default ""; 54 | 55 | /** (Optional) The catalog of the sequence generator. 56 | * 57 | * @since Java Persistence 2.0 58 | */ 59 | String catalog() default ""; 60 | 61 | /** (Optional) The schema of the sequence generator. 62 | * 63 | * @since Java Persistence 2.0 64 | */ 65 | String schema() default ""; 66 | 67 | /** 68 | * (Optional) The value from which the sequence object 69 | * is to start generating. 70 | */ 71 | int initialValue() default 1; 72 | 73 | /** 74 | * (Optional) The amount to increment by when allocating 75 | * sequence numbers from the sequence. 76 | */ 77 | int allocationSize() default 50; 78 | } 79 | -------------------------------------------------------------------------------- /core/src/main/java/com/github/drinkjava2/jdialects/annotation/jpa/Table.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 | * use this file except in compliance with the License. You may obtain a copy of 6 | * the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by 7 | * applicable law or agreed to in writing, software distributed under the 8 | * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS 9 | * OF ANY KIND, either express or implied. See the License for the specific 10 | * language governing permissions and limitations under the License. 11 | */ 12 | package com.github.drinkjava2.jdialects.annotation.jpa; 13 | 14 | import static java.lang.annotation.ElementType.TYPE; 15 | import static java.lang.annotation.RetentionPolicy.RUNTIME; 16 | 17 | import java.lang.annotation.Inherited; 18 | import java.lang.annotation.Retention; 19 | import java.lang.annotation.Target; 20 | 21 | /** 22 | * Specifies the primary table for the annotated entity. Additional 23 | * tables may be specified using {@link SecondaryTable} or {@link 24 | * SecondaryTables} annotation. 25 | * 26 | *

If no Table annotation is specified for an entity 27 | * class, the default values apply. 28 | * 29 | *

30 |  *    Example:
31 |  *
32 |  *    @Entity
33 |  *    @Table(name="CUST", schema="RECORDS")
34 |  *    public class Customer { ... }
35 |  * 
36 | * 37 | * @since Java Persistence 1.0 38 | */ 39 | @Inherited 40 | @Target(TYPE) 41 | @Retention(RUNTIME) 42 | public @interface Table { 43 | 44 | /** 45 | * (Optional) The name of the table. 46 | *

Defaults to the entity name. 47 | */ 48 | String name() default ""; 49 | 50 | /** (Optional) The catalog of the table. 51 | *

Defaults to the default catalog. 52 | */ 53 | String catalog() default ""; 54 | 55 | /** (Optional) The schema of the table. 56 | *

Defaults to the default schema for user. 57 | */ 58 | String schema() default ""; 59 | 60 | /** 61 | * (Optional) Unique constraints that are to be placed on 62 | * the table. These are only used if table generation is in 63 | * effect. These constraints apply in addition to any constraints 64 | * specified by the Column and JoinColumn 65 | * annotations and constraints entailed by primary key mappings. 66 | *

Defaults to no additional constraints. 67 | */ 68 | UniqueConstraint[] uniqueConstraints() default {}; 69 | 70 | /** 71 | * (Optional) Indexes for the table. These are only used if 72 | * table generation is in effect. Note that it is not necessary 73 | * to specify an index for a primary key, as the primary key 74 | * index will be created automatically. 75 | * 76 | * @since Java Persistence 2.1 77 | */ 78 | Index[] indexes() default {}; 79 | } 80 | -------------------------------------------------------------------------------- /core/src/main/java/com/github/drinkjava2/jdialects/annotation/jpa/TableGenerator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 | * use this file except in compliance with the License. You may obtain a copy of 6 | * the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by 7 | * applicable law or agreed to in writing, software distributed under the 8 | * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS 9 | * OF ANY KIND, either express or implied. See the License for the specific 10 | * language governing permissions and limitations under the License. 11 | */ 12 | package com.github.drinkjava2.jdialects.annotation.jpa; 13 | 14 | import static java.lang.annotation.ElementType.FIELD; 15 | import static java.lang.annotation.ElementType.TYPE; 16 | import static java.lang.annotation.RetentionPolicy.RUNTIME; 17 | 18 | import java.lang.annotation.Retention; 19 | import java.lang.annotation.Target; 20 | 21 | /** 22 | * Defines a primary key generator that may be 23 | * referenced by name when a generator element is specified for 24 | * the {@link GeneratedValue} annotation. A table generator 25 | * may be specified on the entity class or on the primary key 26 | * field or property. The scope of the generator name is global 27 | * to the persistence unit (across all generator types). 28 | * 29 | *

 30 |  *    Example 1:
 31 |  *    
 32 |  *    @Entity public class Employee {
 33 |  *        ...
 34 |  *        @TableGenerator(
 35 |  *            name="empGen", 
 36 |  *            table="ID_GEN", 
 37 |  *            pkColumnName="GEN_KEY", 
 38 |  *            valueColumnName="GEN_VALUE", 
 39 |  *            pkColumnValue="EMP_ID", 
 40 |  *            allocationSize=1)
 41 |  *        @Id
 42 |  *        @GeneratedValue(strategy=TABLE, generator="empGen")
 43 |  *        int id;
 44 |  *        ...
 45 |  *    }
 46 |  *    
 47 |  *    Example 2:
 48 |  *    
 49 |  *    @Entity public class Address {
 50 |  *        ...
 51 |  *        @TableGenerator(
 52 |  *            name="addressGen", 
 53 |  *            table="ID_GEN", 
 54 |  *            pkColumnName="GEN_KEY", 
 55 |  *            valueColumnName="GEN_VALUE", 
 56 |  *            pkColumnValue="ADDR_ID")
 57 |  *        @Id
 58 |  *        @GeneratedValue(strategy=TABLE, generator="addressGen")
 59 |  *        int id;
 60 |  *        ...
 61 |  *    }
 62 |  * 
63 | * 64 | * @see GeneratedValue 65 | * 66 | * @since Java Persistence 1.0 67 | */ 68 | @Target({TYPE, FIELD}) 69 | @Retention(RUNTIME) 70 | public @interface TableGenerator { 71 | 72 | /** 73 | * (Required) A unique generator name that can be referenced 74 | * by one or more classes to be the generator for id values. 75 | */ 76 | String name(); 77 | 78 | /** 79 | * (Optional) Name of table that stores the generated id values. 80 | *

Defaults to a name chosen by persistence provider. 81 | */ 82 | String table() default ""; 83 | 84 | /** (Optional) The catalog of the table. 85 | *

Defaults to the default catalog. 86 | */ 87 | String catalog() default ""; 88 | 89 | /** (Optional) The schema of the table. 90 | *

Defaults to the default schema for user. 91 | */ 92 | String schema() default ""; 93 | 94 | /** 95 | * (Optional) Name of the primary key column in the table. 96 | *

Defaults to a provider-chosen name. 97 | */ 98 | String pkColumnName() default ""; 99 | 100 | /** 101 | * (Optional) Name of the column that stores the last value generated. 102 | *

Defaults to a provider-chosen name. 103 | */ 104 | String valueColumnName() default ""; 105 | 106 | /** 107 | * (Optional) The primary key value in the generator table 108 | * that distinguishes this set of generated values from others 109 | * that may be stored in the table. 110 | *

Defaults to a provider-chosen value to store in the 111 | * primary key column of the generator table 112 | */ 113 | String pkColumnValue() default ""; 114 | 115 | /** 116 | * (Optional) The initial value to be used to initialize the column 117 | * that stores the last value generated. 118 | */ 119 | int initialValue() default 0; 120 | 121 | /** 122 | * (Optional) The amount to increment by when allocating id 123 | * numbers from the generator. 124 | */ 125 | int allocationSize() default 50; 126 | 127 | /** 128 | * (Optional) Unique constraints that are to be placed on the 129 | * table. These are only used if table generation is in effect. 130 | * These constraints apply in addition to primary key constraints. 131 | *

Defaults to no additional constraints. 132 | */ 133 | UniqueConstraint[] uniqueConstraints() default {}; 134 | 135 | /** 136 | * (Optional) Indexes for the table. These are only used if 137 | * table generation is in effect. Note that it is not necessary 138 | * to specify an index for a primary key, as the primary key 139 | * index will be created automatically. 140 | * 141 | * @since Java Persistence 2.1 142 | */ 143 | Index[] indexes() default {}; 144 | } 145 | -------------------------------------------------------------------------------- /core/src/main/java/com/github/drinkjava2/jdialects/annotation/jpa/Temporal.java: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * Copyright (c) 2008 - 2013 Oracle Corporation. All rights reserved. 3 | * 4 | * This program and the accompanying materials are made available under the 5 | * terms of the Eclipse Public License v1.0 and Eclipse Distribution License v. 1.0 6 | * which accompanies this distribution. 7 | * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html 8 | * and the Eclipse Distribution License is available at 9 | * http://www.eclipse.org/org/documents/edl-v10.php. 10 | * 11 | * Contributors: 12 | * Linda DeMichiel - Java Persistence 2.1 13 | * Linda DeMichiel - Java Persistence 2.0 14 | * 15 | ******************************************************************************/ 16 | package com.github.drinkjava2.jdialects.annotation.jpa; 17 | 18 | import java.lang.annotation.Target; 19 | import java.lang.annotation.Retention; 20 | import static java.lang.annotation.ElementType.FIELD; 21 | import static java.lang.annotation.ElementType.METHOD; 22 | import static java.lang.annotation.RetentionPolicy.RUNTIME; 23 | 24 | /** 25 | * This annotation must be specified for persistent fields 26 | * or properties of type java.util.Date and 27 | * java.util.Calendar. It may only be specified for fields 28 | * or properties of these types. 29 | * 30 | *

The Temporal annotation may be used in 31 | * conjunction with the {@link Basic} annotation, the {@link Id} 32 | * annotation, or the {@link ElementCollection} annotation (when 33 | * the element collection value is of such a temporal type. 34 | * 35 | *

36 |  *     Example:
37 |  * 
38 |  *     @Temporal(DATE)
39 |  *     protected java.util.Date endDate;
40 |  * 
41 | * 42 | * @since Java Persistence 1.0 43 | */ 44 | @Target({METHOD, FIELD}) 45 | @Retention(RUNTIME) 46 | public @interface Temporal { 47 | 48 | /** The type used in mapping java.util.Date or java.util.Calendar. */ 49 | TemporalType value(); 50 | } 51 | -------------------------------------------------------------------------------- /core/src/main/java/com/github/drinkjava2/jdialects/annotation/jpa/TemporalType.java: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * Copyright (c) 2008 - 2013 Oracle Corporation. All rights reserved. 3 | * 4 | * This program and the accompanying materials are made available under the 5 | * terms of the Eclipse Public License v1.0 and Eclipse Distribution License v. 1.0 6 | * which accompanies this distribution. 7 | * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html 8 | * and the Eclipse Distribution License is available at 9 | * http://www.eclipse.org/org/documents/edl-v10.php. 10 | * 11 | * Contributors: 12 | * Linda DeMichiel - Java Persistence 2.1 13 | * Linda DeMichiel - Java Persistence 2.0 14 | * 15 | ******************************************************************************/ 16 | package com.github.drinkjava2.jdialects.annotation.jpa; 17 | 18 | /** 19 | * Type used to indicate a specific mapping of java.util.Date 20 | * or java.util.Calendar. 21 | * 22 | * @since Java Persistence 1.0 23 | */ 24 | public enum TemporalType { 25 | 26 | /** Map as java.sql.Date */ 27 | DATE, 28 | 29 | /** Map as java.sql.Time */ 30 | TIME, 31 | 32 | /** Map as java.sql.Timestamp */ 33 | TIMESTAMP 34 | } 35 | -------------------------------------------------------------------------------- /core/src/main/java/com/github/drinkjava2/jdialects/annotation/jpa/Transient.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 | * use this file except in compliance with the License. You may obtain a copy of 6 | * the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by 7 | * applicable law or agreed to in writing, software distributed under the 8 | * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS 9 | * OF ANY KIND, either express or implied. See the License for the specific 10 | * language governing permissions and limitations under the License. 11 | */ 12 | package com.github.drinkjava2.jdialects.annotation.jpa; 13 | 14 | import java.lang.annotation.Target; 15 | import java.lang.annotation.Retention; 16 | import static java.lang.annotation.ElementType.METHOD; 17 | import static java.lang.annotation.ElementType.FIELD; 18 | import static java.lang.annotation.RetentionPolicy.RUNTIME; 19 | 20 | /** 21 | * Specifies that the property or field is not persistent. It is used 22 | * to annotate a property or field of an entity class, mapped 23 | * superclass, or embeddable class. 24 | * 25 | *
26 |  *    Example:
27 |  *
28 |  *    @Entity
29 |  *    public class Employee {
30 |  *        @Id int id;
31 |  *        @Transient User currentUser;
32 |  *        ...
33 |  *    }
34 |  * 
35 | * 36 | * @since Java Persistence 1.0 37 | */ 38 | @Target({METHOD, FIELD}) 39 | @Retention(RUNTIME) 40 | 41 | public @interface Transient {} 42 | -------------------------------------------------------------------------------- /core/src/main/java/com/github/drinkjava2/jdialects/annotation/jpa/UniqueConstraint.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 | * use this file except in compliance with the License. You may obtain a copy of 6 | * the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by 7 | * applicable law or agreed to in writing, software distributed under the 8 | * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS 9 | * OF ANY KIND, either express or implied. See the License for the specific 10 | * language governing permissions and limitations under the License. 11 | */ 12 | package com.github.drinkjava2.jdialects.annotation.jpa; 13 | 14 | import java.lang.annotation.Target; 15 | import java.lang.annotation.Retention; 16 | import static java.lang.annotation.RetentionPolicy.RUNTIME; 17 | 18 | /** 19 | * Specifies that a unique constraint is to be included in 20 | * the generated DDL for a primary or secondary table. 21 | * 22 | *
23 |  *    Example:
24 |  *    @Entity
25 |  *    @Table(
26 |  *        name="EMPLOYEE", 
27 |  *        uniqueConstraints=
28 |  *            @UniqueConstraint(columnNames={"EMP_ID", "EMP_NAME"})
29 |  *    )
30 |  *    public class Employee { ... }
31 |  * 
32 | * 33 | * @since Java Persistence 1.0 34 | */ 35 | @Target({}) 36 | @Retention(RUNTIME) 37 | public @interface UniqueConstraint { 38 | 39 | /** (Optional) Constraint name. A provider-chosen name will be chosen 40 | * if a name is not specified. 41 | * 42 | * @since Java Persistence 2.0 43 | */ 44 | String name() default ""; 45 | 46 | /** (Required) An array of the column names that make up the constraint. */ 47 | String[] columnNames(); 48 | } 49 | -------------------------------------------------------------------------------- /core/src/main/java/com/github/drinkjava2/jdialects/annotation/jpa/Version.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 | * use this file except in compliance with the License. You may obtain a copy of 6 | * the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by 7 | * applicable law or agreed to in writing, software distributed under the 8 | * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS 9 | * OF ANY KIND, either express or implied. See the License for the specific 10 | * language governing permissions and limitations under the License. 11 | */ 12 | package com.github.drinkjava2.jdialects.annotation.jpa; 13 | 14 | import java.lang.annotation.Target; 15 | import java.lang.annotation.Retention; 16 | import static java.lang.annotation.ElementType.METHOD; 17 | import static java.lang.annotation.ElementType.FIELD; 18 | import static java.lang.annotation.RetentionPolicy.RUNTIME; 19 | 20 | /** 21 | * Specifies the version field or property of an entity class that serves as its 22 | * optimistic lock value. The version is used to ensure integrity when 23 | * performing the merge operation and for optimistic concurrency control. 24 | * 25 | *

26 | * Only a single Version property or field should be used per 27 | * class; applications that use more than one Version property or 28 | * field will not be portable. 29 | * 30 | *

31 | * The Version property should be mapped to the primary table for 32 | * the entity class; applications that map the Version property to 33 | * a table other than the primary table will not be portable. 34 | * 35 | *

36 | * The following types are supported for version properties: int, 37 | * Integer, short, Short, 38 | * long, Long 39 | * 40 | *

41 |  *    Example:
42 |  *
43 |  *    @Version
44 |  *    @Column(name="OPTLOCK")
45 |  *    protected int getVersionNum() { return versionNum; }
46 |  * 
47 | * 48 | */ 49 | @Target({ METHOD, FIELD }) 50 | @Retention(RUNTIME) 51 | public @interface Version { 52 | } -------------------------------------------------------------------------------- /core/src/main/java/com/github/drinkjava2/jdialects/converter/BasicJavaToJdbcConverter.java: -------------------------------------------------------------------------------- 1 | package com.github.drinkjava2.jdialects.converter; 2 | 3 | import java.math.BigInteger; 4 | import java.util.Calendar; 5 | import java.util.Date; 6 | 7 | import com.github.drinkjava2.jdialects.Java8DateUtils; 8 | 9 | public class BasicJavaToJdbcConverter implements JavaToJdbcConverter { 10 | public static final BasicJavaToJdbcConverter instance = new BasicJavaToJdbcConverter(); 11 | 12 | @Override 13 | public Object convert(Object value) { 14 | if (value == null) 15 | return value; 16 | Class vType = value.getClass(); 17 | if (java.util.Date.class == vType) 18 | return new java.sql.Date(((Date) value).getTime()); 19 | if (Character.class == vType) 20 | return ((Character) value).toString(); 21 | if (BigInteger.class == vType) 22 | return ((BigInteger) value).longValue(); // 2020.10.1 change from int to long 23 | if (Boolean.class == vType) 24 | return ((Boolean) value).booleanValue(); 25 | else if (Calendar.class.isAssignableFrom(vType)) 26 | return new java.sql.Date(((Calendar) value).getTime().getTime()); 27 | /*- JAVA8_BEGIN */ 28 | else if (java.time.temporal.Temporal.class.isAssignableFrom(vType)) { 29 | if (java.time.LocalDate.class == vType) 30 | return Java8DateUtils.localDate2SqlDate((java.time.LocalDate) value); 31 | else if (java.time.LocalTime.class == vType) 32 | return Java8DateUtils.localTime2SqlTime((java.time.LocalTime) value); 33 | else if (java.time.OffsetTime.class == vType) 34 | return Java8DateUtils.offsetTime2SqlTime((java.time.OffsetTime) value); 35 | else if (java.time.Instant.class == vType) 36 | return Java8DateUtils.instant2SqlTimestamp((java.time.Instant) value); 37 | else if (java.time.LocalDateTime.class == vType) 38 | return Java8DateUtils.localDateTime2SqlTimestamp((java.time.LocalDateTime) value); 39 | else if (java.time.OffsetDateTime.class == vType) 40 | return Java8DateUtils.offsetDateTime2SqlTimestamp((java.time.OffsetDateTime) value); 41 | else if (java.time.ZonedDateTime.class == vType) 42 | return Java8DateUtils.zonedDateTime2SqlTimestamp((java.time.ZonedDateTime) value); 43 | } 44 | /* JAVA8_END */ 45 | return value; 46 | } 47 | } -------------------------------------------------------------------------------- /core/src/main/java/com/github/drinkjava2/jdialects/converter/BasicJdbcToJavaConverter.java: -------------------------------------------------------------------------------- 1 | package com.github.drinkjava2.jdialects.converter; 2 | 3 | public class BasicJdbcToJavaConverter implements JdbcToJavaConverter { 4 | public static final BasicJdbcToJavaConverter instance = new BasicJdbcToJavaConverter(); 5 | 6 | @Override 7 | public Object convert(Object value) { 8 | return value; 9 | } 10 | } -------------------------------------------------------------------------------- /core/src/main/java/com/github/drinkjava2/jdialects/converter/JavaConverter.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2016 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 | * use this file except in compliance with the License. You may obtain a copy of 6 | * the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 | * License for the specific language governing permissions and limitations under 14 | * the License. 15 | */ 16 | package com.github.drinkjava2.jdialects.converter; 17 | 18 | /** 19 | * JdbcTypeConverter, used to conver jdbc value to java, or java value to jdbc 20 | * parameter for SQL 21 | * 22 | * @author yongz 23 | * @since 5.0.0 24 | * 25 | */ 26 | public interface JavaConverter { 27 | 28 | /** 29 | * Convert java value to targetType java value 30 | * @param value 31 | * @param targetType 32 | * @return new value 33 | */ 34 | public Object convert(Object value, Class targetType); 35 | 36 | } -------------------------------------------------------------------------------- /core/src/main/java/com/github/drinkjava2/jdialects/converter/JavaToJdbcConverter.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2016 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 | * use this file except in compliance with the License. You may obtain a copy of 6 | * the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 | * License for the specific language governing permissions and limitations under 14 | * the License. 15 | */ 16 | package com.github.drinkjava2.jdialects.converter; 17 | 18 | /** 19 | * JavaToJdbcConverter used to convert Java value to JDBC value 20 | * 21 | * @author yongz 22 | * @since 5.0.0 23 | * 24 | */ 25 | public interface JavaToJdbcConverter { 26 | 27 | /** Convert Java value to JDBC value */ 28 | public Object convert(Object value); 29 | 30 | } -------------------------------------------------------------------------------- /core/src/main/java/com/github/drinkjava2/jdialects/converter/JdbcToJavaConverter.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2016 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 | * use this file except in compliance with the License. You may obtain a copy of 6 | * the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 | * License for the specific language governing permissions and limitations under 14 | * the License. 15 | */ 16 | package com.github.drinkjava2.jdialects.converter; 17 | 18 | /** 19 | * JdbcToJavaConverter used to convert jdbc value to java value 20 | * 21 | * @author yongz 22 | * @since 5.0.0 23 | * 24 | */ 25 | public interface JdbcToJavaConverter { 26 | 27 | /** Convert jdbc value to java value */ 28 | public Object convert(Object value); 29 | 30 | } -------------------------------------------------------------------------------- /core/src/main/java/com/github/drinkjava2/jdialects/id/AutoIdGenerator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 | * use this file except in compliance with the License. You may obtain a copy of 6 | * the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by 7 | * applicable law or agreed to in writing, software distributed under the 8 | * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS 9 | * OF ANY KIND, either express or implied. See the License for the specific 10 | * language governing permissions and limitations under the License. 11 | */ 12 | package com.github.drinkjava2.jdialects.id; 13 | 14 | import java.sql.Connection; 15 | 16 | import com.github.drinkjava2.jdialects.Dialect; 17 | import com.github.drinkjava2.jdialects.Type; 18 | import com.github.drinkjava2.jdialects.annotation.jpa.GenerationType; 19 | 20 | /** 21 | * AutoGenerator will depends database's id generator mechanism like MySql's 22 | * Identity, Oracle's Sequence... 23 | * 24 | * @author Yong Zhu 25 | * @version 1.0.0 26 | * @since 1.0.0 27 | */ 28 | public class AutoIdGenerator implements IdGenerator { 29 | private static final String JDIALECTS_AUTOID_NAME = "jdia_autoid"; 30 | private static final String JDIALECTS_AUTOID_TABLE = "jdia_table_autoid"; 31 | private static final String JDIALECTS_AUTOID_SEQUENCE = "jdia_seq_autoid"; 32 | 33 | public static final AutoIdGenerator INSTANCE = new AutoIdGenerator(); 34 | 35 | public static final TableIdGenerator TABLE_AUTOID_INSTANCE = new TableIdGenerator(JDIALECTS_AUTOID_NAME, 36 | JDIALECTS_AUTOID_TABLE, "idcolumn", "valuecolumn", "next_val", 1, 50); 37 | 38 | public static final SequenceIdGenerator SEQ_AUTOID_INSTANCE = new SequenceIdGenerator(JDIALECTS_AUTOID_NAME, 39 | JDIALECTS_AUTOID_SEQUENCE, 1, 1); 40 | 41 | @Override 42 | public GenerationType getGenerationType() { 43 | return GenerationType.AUTO; 44 | } 45 | 46 | @Override 47 | public String getIdGenName() { 48 | return JDIALECTS_AUTOID_NAME; 49 | } 50 | 51 | @Override 52 | public IdGenerator newCopy() { 53 | return INSTANCE; 54 | } 55 | 56 | @Override 57 | public Boolean dependOnAutoIdGenerator() { 58 | return true; 59 | } 60 | 61 | @Override 62 | public Object getNextID(Connection con, Dialect dialect, Type dataType) { 63 | if (dialect.ddlFeatures.supportBasicOrPooledSequence()) 64 | return SEQ_AUTOID_INSTANCE.getNextID(con, dialect, dataType); 65 | else 66 | return TABLE_AUTOID_INSTANCE.getNextID(con, dialect, dataType); 67 | } 68 | 69 | /** 70 | * If dialect support sequence, return a SequenceIdGenerator, otherwise return a 71 | * TableIdGenerator 72 | */ 73 | public IdGenerator getSequenceOrTableIdGenerator(Dialect dialect) { 74 | if (dialect.ddlFeatures.supportBasicOrPooledSequence()) 75 | return SEQ_AUTOID_INSTANCE; 76 | else 77 | return TABLE_AUTOID_INSTANCE; 78 | } 79 | } 80 | -------------------------------------------------------------------------------- /core/src/main/java/com/github/drinkjava2/jdialects/id/IdGenerator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 | * use this file except in compliance with the License. You may obtain a copy of 6 | * the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by 7 | * applicable law or agreed to in writing, software distributed under the 8 | * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS 9 | * OF ANY KIND, either express or implied. See the License for the specific 10 | * language governing permissions and limitations under the License. 11 | */ 12 | package com.github.drinkjava2.jdialects.id; 13 | 14 | import java.sql.Connection; 15 | 16 | import com.github.drinkjava2.jdialects.Dialect; 17 | import com.github.drinkjava2.jdialects.Type; 18 | import com.github.drinkjava2.jdialects.annotation.jpa.GenerationType; 19 | 20 | /** 21 | * Interface for all ID generators 22 | * 23 | * @author Yong Zhu 24 | * @version 1.0.0 25 | * @since 1.0 26 | */ 27 | 28 | public interface IdGenerator {// NOSONAR 29 | /** 30 | * Get the next ID generated by implementation of IdGenerator 31 | * 32 | * @param Connection 33 | * Connection which used get next ID, note this connection will not be closed in method call 34 | * @param dialect 35 | * The Dialect 36 | * @param dataType 37 | * The {@link Type} of a column 38 | * @return id generated by IdGenerator 39 | */ 40 | public Object getNextID(Connection con, Dialect dialect, Type dataType); 41 | 42 | /** Return GenerationType */ 43 | public GenerationType getGenerationType(); 44 | 45 | /** Return a unique Id Generator name in this TableModel */ 46 | public String getIdGenName(); 47 | 48 | /** Return a newCopy (Deep Clone) instance */ 49 | public IdGenerator newCopy(); 50 | 51 | /** 52 | * If depends on AutoIdGenerator, for example SortedUUIDGenerator depends on 53 | * AutoIdGenerator to create start numbers 54 | */ 55 | public Boolean dependOnAutoIdGenerator(); 56 | 57 | } 58 | -------------------------------------------------------------------------------- /core/src/main/java/com/github/drinkjava2/jdialects/id/IdentityIdGenerator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 | * use this file except in compliance with the License. You may obtain a copy of 6 | * the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by 7 | * applicable law or agreed to in writing, software distributed under the 8 | * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS 9 | * OF ANY KIND, either express or implied. See the License for the specific 10 | * language governing permissions and limitations under the License. 11 | */ 12 | package com.github.drinkjava2.jdialects.id; 13 | 14 | import java.sql.Connection; 15 | 16 | import com.github.drinkjava2.jdialects.DDLFeatures; 17 | import com.github.drinkjava2.jdialects.Dialect; 18 | import com.github.drinkjava2.jdialects.DialectException; 19 | import com.github.drinkjava2.jdialects.JdbcUtil; 20 | import com.github.drinkjava2.jdialects.StrUtils; 21 | import com.github.drinkjava2.jdialects.Type; 22 | import com.github.drinkjava2.jdialects.annotation.jpa.GenerationType; 23 | 24 | /** 25 | * Define an Identity type generator, supported by MySQL, SQL Server, DB2, 26 | * Derby, Sybase, PostgreSQL 27 | * 28 | * 29 | * @author Yong Zhu 30 | * @version 1.0.0 31 | * @since 1.0.0 32 | */ 33 | public class IdentityIdGenerator implements IdGenerator { 34 | private String table; 35 | 36 | private String column; 37 | 38 | public IdentityIdGenerator(String table, String column) { 39 | this.table = table; 40 | this.column = column; 41 | } 42 | 43 | @Override 44 | public GenerationType getGenerationType() { 45 | return GenerationType.IDENTITY; 46 | } 47 | 48 | @Override 49 | public String getIdGenName() { 50 | return "IDENTITY"; 51 | } 52 | 53 | @Override 54 | public Boolean dependOnAutoIdGenerator() { 55 | return false; 56 | } 57 | 58 | @Override 59 | public Object getNextID(Connection con, Dialect dialect, Type dataType) { 60 | if (!Boolean.TRUE.equals(dialect.ddlFeatures.getSupportsIdentityColumns())) 61 | throw new DialectException("Dialect '" + dialect + "' does not support identity type"); 62 | String sql = null; 63 | if (Type.BIGINT.equals(dataType)) 64 | sql = dialect.ddlFeatures.getIdentitySelectStringBigINT(); 65 | else 66 | sql = dialect.ddlFeatures.getIdentitySelectString(); 67 | if (StrUtils.isEmpty(sql) || DDLFeatures.NOT_SUPPORT.equals(sql)) 68 | throw new DialectException("Dialect '" + dialect + "' does not support identity type"); 69 | sql=StrUtils.replaceFirst(sql, "_table__col", new StringBuilder(table).append("_").append(column).toString()); 70 | return JdbcUtil.qryOneObject(con, sql); 71 | } 72 | 73 | @Override 74 | public IdGenerator newCopy() { 75 | return new IdentityIdGenerator(table, column); 76 | } 77 | 78 | // getter & setter============== 79 | public String getTable() { 80 | return table; 81 | } 82 | 83 | public void setTable(String table) { 84 | this.table = table; 85 | } 86 | 87 | public String getColumn() { 88 | return column; 89 | } 90 | 91 | public void setColumn(String column) { 92 | this.column = column; 93 | } 94 | } 95 | -------------------------------------------------------------------------------- /core/src/main/java/com/github/drinkjava2/jdialects/id/SequenceIdGenerator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 | * use this file except in compliance with the License. You may obtain a copy of 6 | * the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by 7 | * applicable law or agreed to in writing, software distributed under the 8 | * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS 9 | * OF ANY KIND, either express or implied. See the License for the specific 10 | * language governing permissions and limitations under the License. 11 | */ 12 | package com.github.drinkjava2.jdialects.id; 13 | 14 | import java.sql.Connection; 15 | 16 | import com.github.drinkjava2.jdialects.Dialect; 17 | import com.github.drinkjava2.jdialects.DialectException; 18 | import com.github.drinkjava2.jdialects.JdbcUtil; 19 | import com.github.drinkjava2.jdialects.StrUtils; 20 | import com.github.drinkjava2.jdialects.Type; 21 | import com.github.drinkjava2.jdialects.annotation.jpa.GenerationType; 22 | 23 | /** 24 | * The platform-independent SequenceGen model, similar like JPA 25 | * 26 | * 27 | * 28 | * @author Yong Zhu 29 | * @since 1.0.0 30 | */ 31 | public class SequenceIdGenerator implements IdGenerator { 32 | 33 | /** 34 | * A unique generator name that can be referenced by one or more classes to 35 | * be the generator for primary key values. 36 | */ 37 | private String name; 38 | 39 | /** 40 | * The name of the sequence in database 41 | */ 42 | private String sequenceName; 43 | 44 | /** 45 | * The value from which the sequence is to start generating. 46 | */ 47 | private Integer initialValue = 0; 48 | 49 | /** 50 | * The amount to allocationSize by when allocating sequence numbers from the 51 | * sequence, in Oracle this is identical to "INCREMENT BY", for JPA and ORM 52 | * tools this usually value is 50 53 | */ 54 | private Integer allocationSize = 1; 55 | 56 | public SequenceIdGenerator() { 57 | // default constructor 58 | } 59 | 60 | public SequenceIdGenerator(String name, String sequenceName, Integer initialValue, Integer allocationSize) { 61 | this.name = name; 62 | this.sequenceName = sequenceName; 63 | this.initialValue = initialValue; 64 | this.allocationSize = allocationSize; 65 | } 66 | 67 | @Override 68 | public Object getNextID(Connection con, Dialect dialect, Type dataType) { 69 | DialectException.assureNotEmpty(sequenceName, "sequenceName can not be empty"); 70 | String sequenctSQL = dialect.ddlFeatures.getSequenceNextValString(); 71 | sequenctSQL = StrUtils.replace(sequenctSQL, "_SEQNAME", sequenceName); 72 | return JdbcUtil.qryOneObject(con, sequenctSQL); 73 | } 74 | 75 | @Override 76 | public GenerationType getGenerationType() { 77 | return GenerationType.SEQUENCE; 78 | } 79 | 80 | @Override 81 | public String getIdGenName() { 82 | return name; 83 | } 84 | 85 | @Override 86 | public IdGenerator newCopy() { 87 | return new SequenceIdGenerator(name,sequenceName,initialValue,allocationSize); 88 | } 89 | 90 | @Override 91 | public Boolean dependOnAutoIdGenerator() { 92 | return false; 93 | } 94 | 95 | // getter & setter============== 96 | public String getName() {//NOSONAR 97 | return name; 98 | } 99 | 100 | public void setName(String name) { 101 | this.name = name; 102 | } 103 | 104 | public String getSequenceName() { 105 | return sequenceName; 106 | } 107 | 108 | public void setSequenceName(String sequenceName) { 109 | this.sequenceName = sequenceName; 110 | } 111 | 112 | public Integer getInitialValue() { 113 | return initialValue; 114 | } 115 | 116 | public void setInitialValue(Integer initialValue) { 117 | this.initialValue = initialValue; 118 | } 119 | 120 | public Integer getAllocationSize() { 121 | return allocationSize; 122 | } 123 | 124 | public void setAllocationSize(Integer allocationSize) { 125 | this.allocationSize = allocationSize; 126 | } 127 | 128 | } 129 | -------------------------------------------------------------------------------- /core/src/main/java/com/github/drinkjava2/jdialects/id/SnowflakeGenerator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 | * use this file except in compliance with the License. You may obtain a copy of 6 | * the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by 7 | * applicable law or agreed to in writing, software distributed under the 8 | * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS 9 | * OF ANY KIND, either express or implied. See the License for the specific 10 | * language governing permissions and limitations under the License. 11 | */ 12 | package com.github.drinkjava2.jdialects.id; 13 | 14 | import java.sql.Connection; 15 | 16 | import com.github.drinkjava2.jdialects.Dialect; 17 | import com.github.drinkjava2.jdialects.DialectException; 18 | import com.github.drinkjava2.jdialects.Type; 19 | import com.github.drinkjava2.jdialects.annotation.jpa.GenerationType; 20 | 21 | /** 22 | * SnowflakeGenerator is a special generator, only mark this column is a 23 | * snowflake type column, but getNextID() method does not work, because 24 | * snowflake value should generated by outside program, it depends on real 25 | * machine setting, in jDialects there is a SnowflakeCreator tool for these 26 | * outside program to use 27 | */ 28 | @SuppressWarnings("all") 29 | public class SnowflakeGenerator implements IdGenerator { 30 | public final static SnowflakeGenerator INSTANCE = new SnowflakeGenerator(); 31 | 32 | @Override 33 | public Object getNextID(Connection con, Dialect dialect, Type dataType) { 34 | throw new DialectException("Snowflake type column value should generated by outside program."); 35 | } 36 | 37 | @Override 38 | public GenerationType getGenerationType() { 39 | return GenerationType.SNOWFLAKE; 40 | } 41 | 42 | @Override 43 | public String getIdGenName() { 44 | return "SNOWFLAKE"; 45 | } 46 | 47 | @Override 48 | public IdGenerator newCopy() { 49 | return INSTANCE; 50 | } 51 | 52 | @Override 53 | public Boolean dependOnAutoIdGenerator() { 54 | return false; 55 | } 56 | 57 | } -------------------------------------------------------------------------------- /core/src/main/java/com/github/drinkjava2/jdialects/id/SortedUUIDGenerator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 | * use this file except in compliance with the License. You may obtain a copy of 6 | * the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by 7 | * applicable law or agreed to in writing, software distributed under the 8 | * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS 9 | * OF ANY KIND, either express or implied. See the License for the specific 10 | * language governing permissions and limitations under the License. 11 | */ 12 | package com.github.drinkjava2.jdialects.id; 13 | 14 | import java.sql.Connection; 15 | 16 | import com.github.drinkjava2.jdialects.Dialect; 17 | import com.github.drinkjava2.jdialects.DialectException; 18 | import com.github.drinkjava2.jdialects.Type; 19 | import com.github.drinkjava2.jdialects.annotation.jpa.GenerationType; 20 | 21 | /** 22 | * Generate a Sorted UUID, total length = sortedLength + uuidLength,
23 | * for example, SortedUUIDGenerator(5,20):
24 | * 10001NmpQHeGLy8eozSSq2p1B
25 | * 10002DLIGkILFISKJF23KLSDF
26 | * 10003LVBIFI35LDFJIA31KDSF
27 | * 28 | * @author Yong Zhu 29 | * @since 1.0.6 30 | */ 31 | public class SortedUUIDGenerator implements IdGenerator { 32 | private String name; 33 | private int sortedLength; 34 | private int uuidLength; 35 | 36 | public SortedUUIDGenerator(String name, int sortedLength, int uuidLength) { 37 | this.name = name; 38 | this.sortedLength = sortedLength; 39 | this.uuidLength = uuidLength; 40 | } 41 | 42 | @Override 43 | public GenerationType getGenerationType() { 44 | return GenerationType.SORTED_UUID; 45 | } 46 | 47 | @Override 48 | public String getIdGenName() { 49 | return name; 50 | } 51 | 52 | @Override 53 | public Boolean dependOnAutoIdGenerator() { 54 | return true; 55 | } 56 | 57 | @Override 58 | public Object getNextID(Connection con, Dialect dialect, Type dataType) { 59 | String s = "" + AutoIdGenerator.INSTANCE.getNextID(con, dialect, dataType); 60 | if (s.length() > (sortedLength - 1)) 61 | throw new DialectException("SortedLength should set bigger than auto generated ID length"); 62 | StringBuilder sb = new StringBuilder("1"); 63 | for (int i = 1; i < sortedLength - s.length(); i++) 64 | sb.append("0"); 65 | sb.append(s); 66 | sb.append(UUIDAnyGenerator.getAnyLengthRadix36UUID(uuidLength)); 67 | return sb.toString(); 68 | } 69 | 70 | @Override 71 | public IdGenerator newCopy() { 72 | return new SortedUUIDGenerator(name, sortedLength, uuidLength); 73 | } 74 | 75 | public String getName() { 76 | return name; 77 | } 78 | 79 | public int getSortedLength() { 80 | return sortedLength; 81 | } 82 | 83 | public int getUuidLength() { 84 | return uuidLength; 85 | } 86 | 87 | } 88 | -------------------------------------------------------------------------------- /core/src/main/java/com/github/drinkjava2/jdialects/id/TableIdGenerator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 | * use this file except in compliance with the License. You may obtain a copy of 6 | * the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by 7 | * applicable law or agreed to in writing, software distributed under the 8 | * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS 9 | * OF ANY KIND, either express or implied. See the License for the specific 10 | * language governing permissions and limitations under the License. 11 | */ 12 | package com.github.drinkjava2.jdialects.id; 13 | 14 | import java.sql.Connection; 15 | 16 | import com.github.drinkjava2.jdialects.Dialect; 17 | import com.github.drinkjava2.jdialects.JdbcUtil; 18 | import com.github.drinkjava2.jdialects.Type; 19 | import com.github.drinkjava2.jdialects.annotation.jpa.GenerationType; 20 | 21 | /** 22 | * The platform-independent table model 23 | * 24 | * @author Yong Zhu 25 | * @since 1.0.2 26 | */ 27 | public class TableIdGenerator implements IdGenerator { 28 | 29 | /** 30 | * A unique generator name that can be referenced by one or more classes to be 31 | * the generator for id values. 32 | */ 33 | private String name; 34 | 35 | /** 36 | * Table name in database 37 | */ 38 | private String table; 39 | 40 | /** 41 | * Name of the primary key column in the table. 42 | */ 43 | private String pkColumnName = ""; 44 | 45 | /** 46 | * Name of the column that stores the last value generated. 47 | */ 48 | private String valueColumnName = ""; 49 | 50 | /** 51 | * The primary key value in the generator table that distinguishes this set of 52 | * generated values from others that may be stored in the table. 53 | */ 54 | private String pkColumnValue = ""; 55 | 56 | /** 57 | * The initial value to be used when allocating id numbers from the generator. 58 | */ 59 | private Integer initialValue = 0; 60 | 61 | /** 62 | * The amount to increment by when allocating id numbers from the generator. 63 | */ 64 | private Integer allocationSize = 50; 65 | 66 | public TableIdGenerator() { 67 | super(); 68 | } 69 | 70 | public TableIdGenerator(String name, String table, String pkColumnName, String valueColumnName, 71 | String pkColumnValue, Integer initialValue, Integer allocationSize) { 72 | this.name = name; 73 | this.table = table; 74 | this.pkColumnName = pkColumnName; 75 | this.valueColumnName = valueColumnName; 76 | this.pkColumnValue = pkColumnValue; 77 | this.initialValue = initialValue; 78 | this.allocationSize = allocationSize; 79 | } 80 | 81 | @Override 82 | public IdGenerator newCopy() { 83 | return new TableIdGenerator(name, table, pkColumnName, valueColumnName, pkColumnValue, initialValue, 84 | allocationSize); 85 | } 86 | 87 | @Override 88 | public GenerationType getGenerationType() { 89 | return GenerationType.TIMESTAMP; 90 | } 91 | 92 | @Override 93 | public String getIdGenName() { 94 | return name; 95 | } 96 | 97 | @Override 98 | public Boolean dependOnAutoIdGenerator() { 99 | return false; 100 | } 101 | 102 | /** 103 | * Get the next Table Generator ID 104 | */ 105 | @Override 106 | public Object getNextID(Connection con, Dialect dialect, Type dataType) { 107 | int countOfRec = ((Number) JdbcUtil // NOSONAR 108 | .qryOneObject(con, "select count(*) from " + table + " where " + pkColumnName + "=?", pkColumnValue)) 109 | .intValue(); 110 | if (countOfRec == 0) { 111 | JdbcUtil.execute(con, "insert into " + table + "( " + pkColumnName + "," + valueColumnName + " ) values(?,?)", 112 | pkColumnValue, initialValue); 113 | return initialValue; 114 | } else { 115 | JdbcUtil.execute(con, "update " + table + " set " + valueColumnName + "=" + valueColumnName + "+" + allocationSize 116 | + " where " + pkColumnName + " =?", pkColumnValue); 117 | 118 | int last = ((Number) JdbcUtil.qryOneObject(con, // NOSONAR 119 | "select " + valueColumnName + " from " + table + " where " + pkColumnName + "=?", pkColumnValue)) 120 | .intValue(); 121 | return last; 122 | } 123 | } 124 | 125 | // getter && setter===================== 126 | public String getName() { 127 | return name; 128 | } 129 | 130 | public void setName(String name) { 131 | this.name = name; 132 | } 133 | 134 | public String getTable() { 135 | return table; 136 | } 137 | 138 | public void setTable(String table) { 139 | this.table = table; 140 | } 141 | 142 | public String getPkColumnName() { 143 | return pkColumnName; 144 | } 145 | 146 | public void setPkColumnName(String pkColumnName) { 147 | this.pkColumnName = pkColumnName; 148 | } 149 | 150 | public String getValueColumnName() { 151 | return valueColumnName; 152 | } 153 | 154 | public void setValueColumnName(String valueColumnName) { 155 | this.valueColumnName = valueColumnName; 156 | } 157 | 158 | public String getPkColumnValue() { 159 | return pkColumnValue; 160 | } 161 | 162 | public void setPkColumnValue(String pkColumnValue) { 163 | this.pkColumnValue = pkColumnValue; 164 | } 165 | 166 | public Integer getInitialValue() { 167 | return initialValue; 168 | } 169 | 170 | public void setInitialValue(Integer initialValue) { 171 | this.initialValue = initialValue; 172 | } 173 | 174 | public Integer getAllocationSize() { 175 | return allocationSize; 176 | } 177 | 178 | public void setAllocationSize(Integer allocationSize) { 179 | this.allocationSize = allocationSize; 180 | } 181 | } 182 | -------------------------------------------------------------------------------- /core/src/main/java/com/github/drinkjava2/jdialects/id/TimeStampIdGenerator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 | * use this file except in compliance with the License. You may obtain a copy of 6 | * the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by 7 | * applicable law or agreed to in writing, software distributed under the 8 | * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS 9 | * OF ANY KIND, either express or implied. See the License for the specific 10 | * language governing permissions and limitations under the License. 11 | */ 12 | package com.github.drinkjava2.jdialects.id; 13 | 14 | import java.sql.Connection; 15 | 16 | import com.github.drinkjava2.jdialects.Dialect; 17 | import com.github.drinkjava2.jdialects.Type; 18 | import com.github.drinkjava2.jdialects.annotation.jpa.GenerationType; 19 | 20 | /** 21 | * This TimeStampGenerator return a long type value based on computer's current 22 | * time 23 | * 24 | * @author Yong Zhu 25 | * @version 1.0.0 26 | * @since 1.0.0 27 | */ 28 | public class TimeStampIdGenerator implements IdGenerator { 29 | public static final TimeStampIdGenerator INSTANCE = new TimeStampIdGenerator(); 30 | private static long count = 1; 31 | 32 | @Override 33 | public GenerationType getGenerationType() { 34 | return GenerationType.TIMESTAMP; 35 | } 36 | 37 | @Override 38 | public String getIdGenName() { 39 | return "TimeStampId"; 40 | } 41 | 42 | @Override 43 | public Object getNextID(Connection con, Dialect dialect, Type dataType) { 44 | return getNextID(); 45 | } 46 | 47 | public static synchronized Object getNextID() { 48 | if (count > 999999) 49 | count = 1; 50 | return System.currentTimeMillis() * 1000000 + count++; 51 | } 52 | 53 | @Override 54 | public Boolean dependOnAutoIdGenerator() { 55 | return false; 56 | } 57 | 58 | @Override 59 | public IdGenerator newCopy() { 60 | return INSTANCE; 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /core/src/main/java/com/github/drinkjava2/jdialects/id/UUID25Generator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 | * use this file except in compliance with the License. You may obtain a copy of 6 | * the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by 7 | * applicable law or agreed to in writing, software distributed under the 8 | * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS 9 | * OF ANY KIND, either express or implied. See the License for the specific 10 | * language governing permissions and limitations under the License. 11 | */ 12 | package com.github.drinkjava2.jdialects.id; 13 | 14 | import java.math.BigInteger; 15 | import java.sql.Connection; 16 | import java.util.UUID; 17 | 18 | import com.github.drinkjava2.jdialects.Dialect; 19 | import com.github.drinkjava2.jdialects.StrUtils; 20 | import com.github.drinkjava2.jdialects.Type; 21 | import com.github.drinkjava2.jdialects.annotation.jpa.GenerationType; 22 | 23 | /** 24 | * Compress JDK UUID to 25 letters based on radix 36, use 0-9 a-z characters, 25 | * example: pbicz3grgu0zk3ipe1yur03h7 26 | * 27 | * @author Yong Zhu 28 | * @version 1.0.0 29 | * @since 1.0.0 30 | */ 31 | public class UUID25Generator implements IdGenerator { 32 | public static final UUID25Generator INSTANCE = new UUID25Generator(); 33 | 34 | @Override 35 | public GenerationType getGenerationType() { 36 | return GenerationType.UUID25; 37 | } 38 | 39 | @Override 40 | public String getIdGenName() { 41 | return "UUID25"; 42 | } 43 | 44 | @Override 45 | public Object getNextID(Connection con, Dialect dialect, Type dataType) { 46 | return getUUID25(); 47 | } 48 | 49 | @Override 50 | public Boolean dependOnAutoIdGenerator() { 51 | return false; 52 | } 53 | 54 | @Override 55 | public IdGenerator newCopy() { 56 | return INSTANCE; 57 | } 58 | 59 | public static String getUUID25() { 60 | String uuidHex = UUID.randomUUID().toString().replace("-", ""); 61 | BigInteger b = new BigInteger(uuidHex, 16); 62 | String s = b.toString(36); 63 | while (s.length() < 25) 64 | s = s + StrUtils.getRandomChar(); // NOSONAR 65 | return s; 66 | } 67 | 68 | } 69 | -------------------------------------------------------------------------------- /core/src/main/java/com/github/drinkjava2/jdialects/id/UUID26Generator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 | * use this file except in compliance with the License. You may obtain a copy of 6 | * the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by 7 | * applicable law or agreed to in writing, software distributed under the 8 | * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS 9 | * OF ANY KIND, either express or implied. See the License for the specific 10 | * language governing permissions and limitations under the License. 11 | */ 12 | package com.github.drinkjava2.jdialects.id; 13 | 14 | import java.math.BigInteger; 15 | import java.sql.Connection; 16 | import java.util.UUID; 17 | 18 | import com.github.drinkjava2.jdialects.Dialect; 19 | import com.github.drinkjava2.jdialects.StrUtils; 20 | import com.github.drinkjava2.jdialects.Type; 21 | import com.github.drinkjava2.jdialects.annotation.jpa.GenerationType; 22 | 23 | /** 24 | * Compress JDK UUID to 26 letters based on radix 36, use 0-9 a-z characters, 25 | * example: pbicz3grgu0zk3ipe1yur03h7a 26 | * 27 | * @author Yong Zhu 28 | * @version 1.0.0 29 | * @since 1.0.0 30 | */ 31 | public class UUID26Generator implements IdGenerator { 32 | public static final UUID26Generator INSTANCE = new UUID26Generator(); 33 | 34 | @Override 35 | public GenerationType getGenerationType() { 36 | return GenerationType.UUID26; 37 | } 38 | 39 | @Override 40 | public String getIdGenName() { 41 | return "UUID26"; 42 | } 43 | 44 | @Override 45 | public Object getNextID(Connection con, Dialect dialect, Type dataType) { 46 | return getUUID26(); 47 | } 48 | 49 | @Override 50 | public Boolean dependOnAutoIdGenerator() { 51 | return false; 52 | } 53 | 54 | @Override 55 | public IdGenerator newCopy() { 56 | return INSTANCE; 57 | } 58 | 59 | public static String getUUID26() { 60 | String uuidHex = UUID.randomUUID().toString().replace("-", ""); 61 | BigInteger b = new BigInteger(uuidHex, 16); 62 | String s = b.toString(36); 63 | while (s.length() < 26) 64 | s = s + StrUtils.getRandomChar(); //NOSONAR 65 | return s; 66 | } 67 | 68 | } 69 | -------------------------------------------------------------------------------- /core/src/main/java/com/github/drinkjava2/jdialects/id/UUID32Generator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 | * use this file except in compliance with the License. You may obtain a copy of 6 | * the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by 7 | * applicable law or agreed to in writing, software distributed under the 8 | * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS 9 | * OF ANY KIND, either express or implied. See the License for the specific 10 | * language governing permissions and limitations under the License. 11 | */ 12 | package com.github.drinkjava2.jdialects.id; 13 | 14 | import java.sql.Connection; 15 | import java.util.UUID; 16 | 17 | import com.github.drinkjava2.jdialects.Dialect; 18 | import com.github.drinkjava2.jdialects.Type; 19 | import com.github.drinkjava2.jdialects.annotation.jpa.GenerationType; 20 | 21 | /** 22 | * Generate a JDK 32 letters random UUID based on Base16 encoding, example: 23 | * bca5414e9b1b4bdfa257125e05428b92 24 | * 25 | * @author Yong Zhu 26 | * @version 1.0.0 27 | * @since 1.0.0 28 | */ 29 | public class UUID32Generator implements IdGenerator { 30 | public static final UUID32Generator INSTANCE = new UUID32Generator(); 31 | 32 | @Override 33 | public GenerationType getGenerationType() { 34 | return GenerationType.UUID32; 35 | } 36 | 37 | @Override 38 | public String getIdGenName() { 39 | return "UUID32"; 40 | } 41 | 42 | @Override 43 | public Object getNextID(Connection con, Dialect dialect, Type dataType) { 44 | return getUUID32(); 45 | } 46 | 47 | public static String getUUID32() { 48 | return UUID.randomUUID().toString().replace("-", ""); 49 | } 50 | 51 | @Override 52 | public Boolean dependOnAutoIdGenerator() { 53 | return false; 54 | } 55 | 56 | @Override 57 | public IdGenerator newCopy() { 58 | return INSTANCE; 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /core/src/main/java/com/github/drinkjava2/jdialects/id/UUID36Generator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 | * use this file except in compliance with the License. You may obtain a copy of 6 | * the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by 7 | * applicable law or agreed to in writing, software distributed under the 8 | * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS 9 | * OF ANY KIND, either express or implied. See the License for the specific 10 | * language governing permissions and limitations under the License. 11 | */ 12 | package com.github.drinkjava2.jdialects.id; 13 | 14 | import java.sql.Connection; 15 | import java.util.UUID; 16 | 17 | import com.github.drinkjava2.jdialects.Dialect; 18 | import com.github.drinkjava2.jdialects.Type; 19 | import com.github.drinkjava2.jdialects.annotation.jpa.GenerationType; 20 | 21 | /** 22 | * Generate a JDK 36 letters random UUID generated by 23 | * UUID.randomUUID().toString(), for example:
24 | * d3ad36c0-c6c2-495c-a414-b9cc4a0a7a93 25 | * 26 | * @author Yong Zhu 27 | * @version 1.0.0 28 | * @since 1.0.0 29 | */ 30 | public class UUID36Generator implements IdGenerator { 31 | public static final UUID36Generator INSTANCE = new UUID36Generator(); 32 | 33 | @Override 34 | public GenerationType getGenerationType() { 35 | return GenerationType.UUID36; 36 | } 37 | 38 | @Override 39 | public String getIdGenName() { 40 | return "UUID36"; 41 | } 42 | 43 | @Override 44 | public Object getNextID(Connection con, Dialect dialect, Type dataType) { 45 | return UUID.randomUUID().toString(); 46 | } 47 | 48 | @Override 49 | public Boolean dependOnAutoIdGenerator() { 50 | return false; 51 | } 52 | 53 | @Override 54 | public IdGenerator newCopy() { 55 | return INSTANCE; 56 | } 57 | 58 | } 59 | -------------------------------------------------------------------------------- /core/src/main/java/com/github/drinkjava2/jdialects/id/UUIDAnyGenerator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 | * use this file except in compliance with the License. You may obtain a copy of 6 | * the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by 7 | * applicable law or agreed to in writing, software distributed under the 8 | * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS 9 | * OF ANY KIND, either express or implied. See the License for the specific 10 | * language governing permissions and limitations under the License. 11 | */ 12 | package com.github.drinkjava2.jdialects.id; 13 | 14 | import java.security.SecureRandom; 15 | import java.sql.Connection; 16 | 17 | import com.github.drinkjava2.jdialects.Dialect; 18 | import com.github.drinkjava2.jdialects.Type; 19 | import com.github.drinkjava2.jdialects.annotation.jpa.GenerationType; 20 | 21 | /** 22 | * Generate any length UUID String based on radix 36, use 0-9 a-z characters 23 | *
24 | * Default length is 20; 25 | * 26 | * @author Yong Zhu 27 | * @version 1.0.0 28 | * @since 1.0.0 29 | */ 30 | public class UUIDAnyGenerator implements IdGenerator { 31 | 32 | private static final SecureRandom random = new SecureRandom(); 33 | private static final char[] ALPHABET = "0123456789abcdefghijklmnopqrstuvwxyz".toCharArray(); 34 | 35 | private String name; 36 | private Integer length = 36; 37 | 38 | public UUIDAnyGenerator() { 39 | // default constructor 40 | } 41 | 42 | /** 43 | * Build a give length UUID Generator 44 | */ 45 | public UUIDAnyGenerator(String name, Integer length) { 46 | this.name = name; 47 | this.length = length; 48 | } 49 | 50 | @Override 51 | public GenerationType getGenerationType() { 52 | return GenerationType.UUID_ANY; 53 | } 54 | 55 | @Override 56 | public String getIdGenName() { 57 | return name; 58 | } 59 | 60 | @Override 61 | public Object getNextID(Connection con, Dialect dialect, Type dataType) { 62 | return getAnyLengthRadix36UUID(length); 63 | } 64 | 65 | @Override 66 | public Boolean dependOnAutoIdGenerator() { 67 | return false; 68 | } 69 | 70 | @Override 71 | public IdGenerator newCopy() { 72 | return new UUIDAnyGenerator(name, length); 73 | } 74 | 75 | public static String getAnyLengthRadix36UUID(Integer length) { 76 | StringBuilder sb = new StringBuilder(); 77 | for (int i = 0; i < length; i++) { 78 | sb.append(ALPHABET[random.nextInt(32)]); 79 | } 80 | return sb.toString(); 81 | } 82 | 83 | // getter & setter 84 | public Integer getLength() { 85 | return length; 86 | } 87 | 88 | public void setLength(Integer length) { 89 | this.length = length; 90 | } 91 | 92 | } 93 | -------------------------------------------------------------------------------- /core/src/main/java/com/github/drinkjava2/jdialects/id/UUIDGenerator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 | * use this file except in compliance with the License. You may obtain a copy of 6 | * the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by 7 | * applicable law or agreed to in writing, software distributed under the 8 | * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS 9 | * OF ANY KIND, either express or implied. See the License for the specific 10 | * language governing permissions and limitations under the License. 11 | */ 12 | package com.github.drinkjava2.jdialects.id; 13 | 14 | import java.sql.Connection; 15 | import java.util.UUID; 16 | 17 | import com.github.drinkjava2.jdialects.Dialect; 18 | import com.github.drinkjava2.jdialects.Type; 19 | import com.github.drinkjava2.jdialects.annotation.jpa.GenerationType; 20 | 21 | /** 22 | * Generate a JDK 32 letters random UUID based on Base16 encoding, example: 23 | * bca5414e9b1b4bdfa257125e05428b92 24 | * 25 | * @author Yong Zhu 26 | * @version 1.0.0 27 | * @since 1.0.0 28 | */ 29 | public class UUIDGenerator implements IdGenerator { 30 | public static final UUIDGenerator INSTANCE = new UUIDGenerator(); 31 | 32 | @Override 33 | public GenerationType getGenerationType() { 34 | return GenerationType.UUID; 35 | } 36 | 37 | @Override 38 | public String getIdGenName() { 39 | return "UUID"; 40 | } 41 | 42 | @Override 43 | public Object getNextID(Connection con, Dialect dialect, Type dataType) { 44 | return getUUID(); 45 | } 46 | 47 | public static String getUUID() { 48 | return UUID.randomUUID().toString().replace("-", ""); 49 | } 50 | 51 | @Override 52 | public Boolean dependOnAutoIdGenerator() { 53 | return false; 54 | } 55 | 56 | @Override 57 | public IdGenerator newCopy() { 58 | return INSTANCE; 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /core/src/main/java/com/github/drinkjava2/jdialects/model/FKeyModel.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 | * use this file except in compliance with the License. You may obtain a copy of 6 | * the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by 7 | * applicable law or agreed to in writing, software distributed under the 8 | * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS 9 | * OF ANY KIND, either express or implied. See the License for the specific 10 | * language governing permissions and limitations under the License. 11 | */ 12 | package com.github.drinkjava2.jdialects.model; 13 | 14 | import java.util.ArrayList; 15 | import java.util.List; 16 | 17 | import com.github.drinkjava2.jdialects.ArrayUtils; 18 | import com.github.drinkjava2.jdialects.DialectException; 19 | 20 | /** 21 | * The platform-independent Foreign Key Constraint model 22 | * 23 | *
 24 |  * Usage:
 25 |  * Table t=new Table('Customer');
 26 |  *    ...
 27 |  *  t.fkey("column1").ref("refTable", "refColumn1")
 28 |  *  
 29 |  *  or compound foreign key: *  
 30 |  *  t.fkey("column1","column2").ref("refTable", "refColumn1", "refColumn2");
 31 |  * 
 32 |  * 
33 | * 34 | * @author Yong Zhu 35 | * @since 1.0.2 36 | */ 37 | public class FKeyModel { 38 | private String fkeyName; 39 | private String tableName; 40 | private List columnNames = new ArrayList(); 41 | private String fkeyTail; 42 | private TableModel tableModel; // belong to which tableModel 43 | 44 | /* 45 | * if ddl set to false, will not create DDL when call TableModelUtils's 46 | * entity2Model() and oneEntity2Model methods 47 | */ 48 | private Boolean ddl = true; 49 | 50 | /** format: "reftable, refcol1, refcol2..." */ 51 | private String[] refTableAndColumns; 52 | 53 | public FKeyModel() { 54 | // default constructor 55 | } 56 | 57 | public FKeyModel(String fkeyName) { 58 | this.fkeyName = fkeyName; 59 | } 60 | 61 | public FKeyModel newCopy() { 62 | FKeyModel result = new FKeyModel(); 63 | result.fkeyName = fkeyName; 64 | result.tableName = tableName; 65 | result.fkeyTail = fkeyTail; 66 | result.refTableAndColumns = refTableAndColumns; 67 | result.ddl = ddl; 68 | for (String colName : columnNames) { 69 | result.columnNames.add(colName); 70 | } 71 | return result; 72 | } 73 | 74 | public FKeyModel columns(String... columnNames) { 75 | checkReadOnly(); 76 | this.columnNames = ArrayUtils.strArrayToList(columnNames); 77 | return this; 78 | } 79 | 80 | public FKeyModel fkeyName(String fkeyName) { 81 | checkReadOnly(); 82 | this.fkeyName = fkeyName; 83 | return this; 84 | } 85 | 86 | public FKeyModel ddl(Boolean ddl) { 87 | checkReadOnly(); 88 | this.ddl = ddl; 89 | return this; 90 | } 91 | 92 | public FKeyModel refs(String... refTableAndColumns) { 93 | checkReadOnly(); 94 | this.refTableAndColumns = refTableAndColumns; 95 | return this; 96 | } 97 | 98 | /** Add a tail String at the end of Foreign key DDL */ 99 | public FKeyModel fkeyTail(String fkeyTail) { 100 | checkReadOnly(); 101 | this.fkeyTail = fkeyTail; 102 | return this; 103 | } 104 | 105 | public void checkReadOnly() { 106 | if (tableModel != null && tableModel.getReadOnly()) 107 | throw new DialectException( 108 | "TableModel '" + tableModel.getTableName() + "' is readOnly, can not be modified."); 109 | } 110 | 111 | // getter & setter===== 112 | public String getTableName() { 113 | return tableName; 114 | } 115 | 116 | public void setTableName(String tableName) { 117 | checkReadOnly(); 118 | this.tableName = tableName; 119 | } 120 | 121 | public List getColumnNames() { 122 | return columnNames; 123 | } 124 | 125 | public void setColumnNames(List columnNames) { 126 | checkReadOnly(); 127 | this.columnNames = columnNames; 128 | } 129 | 130 | public String[] getRefTableAndColumns() { 131 | return refTableAndColumns; 132 | } 133 | 134 | public void setRefTableAndColumns(String[] refTableAndColumns) { 135 | checkReadOnly(); 136 | this.refTableAndColumns = refTableAndColumns; 137 | } 138 | 139 | public String getFkeyName() { 140 | return fkeyName; 141 | } 142 | 143 | public void setFkeyName(String fkeyName) { 144 | checkReadOnly(); 145 | this.fkeyName = fkeyName; 146 | } 147 | 148 | public String getFkeyTail() { 149 | return fkeyTail; 150 | } 151 | 152 | public void setFkeyTail(String tail) { 153 | checkReadOnly(); 154 | this.fkeyTail = tail; 155 | } 156 | 157 | public Boolean getDdl() { 158 | return ddl; 159 | } 160 | 161 | public void setDdl(Boolean ddl) { 162 | checkReadOnly(); 163 | this.ddl = ddl; 164 | } 165 | 166 | public TableModel getTableModel() { 167 | return tableModel; 168 | } 169 | 170 | public void setTableModel(TableModel tableModel) { 171 | checkReadOnly(); 172 | this.tableModel = tableModel; 173 | } 174 | 175 | } 176 | -------------------------------------------------------------------------------- /core/src/main/java/com/github/drinkjava2/jdialects/model/IndexModel.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 | * use this file except in compliance with the License. You may obtain a copy of 6 | * the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by 7 | * applicable law or agreed to in writing, software distributed under the 8 | * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS 9 | * OF ANY KIND, either express or implied. See the License for the specific 10 | * language governing permissions and limitations under the License. 11 | */ 12 | package com.github.drinkjava2.jdialects.model; 13 | 14 | import com.github.drinkjava2.jdialects.DialectException; 15 | 16 | /** 17 | * The platform-independent Index model 18 | * 19 | *
 20 |  * Usage:
 21 |  * Table t=new Table('Customer');
 22 |  * ...  
 23 |  *  t.index().forColumn("column1");
 24 |  *  or
 25 |  *  t.index("indexName").forColumn("column1","column2");
 26 |  * 
 27 |  * 
28 | * 29 | * @author Yong Zhu 30 | * @since 1.0.5 31 | */ 32 | public class IndexModel { 33 | /** (Optional) The names of the index */ 34 | private String name; 35 | 36 | /** The names of the columns to be included in the index */ 37 | private String[] columnList; 38 | 39 | /** Whether the index is unique. */ 40 | private Boolean unique = false; 41 | 42 | private TableModel tableModel; // belong to which tableModel 43 | 44 | public IndexModel() { 45 | 46 | } 47 | 48 | public IndexModel(String name) { 49 | this.name = name; 50 | } 51 | 52 | public IndexModel newCopy() { 53 | IndexModel result = new IndexModel(); 54 | result.name = name; 55 | result.columnList = columnList; 56 | result.unique = unique; 57 | return result; 58 | } 59 | 60 | public IndexModel columns(String... columns) { 61 | checkReadOnly(); 62 | this.columnList = columns; 63 | return this; 64 | } 65 | 66 | public IndexModel unique() { 67 | checkReadOnly(); 68 | this.unique = true; 69 | return this; 70 | } 71 | 72 | public void checkReadOnly() { 73 | if (tableModel != null && tableModel.getReadOnly()) 74 | throw new DialectException( 75 | "TableModel '" + tableModel.getTableName() + "' is readOnly, can not be modified."); 76 | } 77 | 78 | // getter & setter ========= 79 | public String getName() { 80 | return name; 81 | } 82 | 83 | public void setName(String name) { 84 | checkReadOnly(); 85 | this.name = name; 86 | } 87 | 88 | public String[] getColumnList() { 89 | return columnList; 90 | } 91 | 92 | public void setColumnList(String[] columnList) { 93 | checkReadOnly(); 94 | this.columnList = columnList; 95 | } 96 | 97 | public Boolean getUnique() { 98 | return unique; 99 | } 100 | 101 | public void setUnique(Boolean unique) { 102 | checkReadOnly(); 103 | this.unique = unique; 104 | } 105 | 106 | public TableModel getTableModel() { 107 | return tableModel; 108 | } 109 | 110 | public void setTableModel(TableModel tableModel) { 111 | checkReadOnly(); 112 | this.tableModel = tableModel; 113 | } 114 | 115 | } 116 | -------------------------------------------------------------------------------- /core/src/main/java/com/github/drinkjava2/jdialects/model/UniqueModel.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 | * use this file except in compliance with the License. You may obtain a copy of 6 | * the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by 7 | * applicable law or agreed to in writing, software distributed under the 8 | * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS 9 | * OF ANY KIND, either express or implied. See the License for the specific 10 | * language governing permissions and limitations under the License. 11 | */ 12 | package com.github.drinkjava2.jdialects.model; 13 | 14 | import com.github.drinkjava2.jdialects.DialectException; 15 | 16 | /** 17 | * The platform-independent Unique Constraint model 18 | * 19 | *
20 |  * Usage:
21 |  * Table t=new Table('Customer');
22 |  * ...  
23 |  *  t.index().forColumn("column1");
24 |  *  or
25 |  *  t.index("indexName").forColumn("column1","column2");
26 |  * 
27 |  * 
28 | * 29 | * @author Yong Zhu 30 | * @since 1.0.5 31 | */ 32 | public class UniqueModel { 33 | private String name; 34 | private String[] columnList; 35 | private TableModel tableModel; // belong to which tableModel 36 | 37 | public UniqueModel() { 38 | 39 | } 40 | 41 | public UniqueModel(String name) { 42 | this.name = name; 43 | } 44 | 45 | public UniqueModel newCopy() { 46 | UniqueModel result = new UniqueModel(); 47 | result.name = name; 48 | result.columnList = columnList; 49 | return result; 50 | } 51 | 52 | public UniqueModel columns(String... columns) { 53 | checkReadOnly(); 54 | this.columnList = columns; 55 | return this; 56 | } 57 | 58 | public void checkReadOnly() { 59 | if (tableModel != null && tableModel.getReadOnly()) 60 | throw new DialectException( 61 | "TableModel '" + tableModel.getTableName() + "' is readOnly, can not be modified."); 62 | } 63 | 64 | // getter & setter ========= 65 | public String getName() { 66 | return name; 67 | } 68 | 69 | public void setName(String name) { 70 | checkReadOnly(); 71 | this.name = name; 72 | } 73 | 74 | public String[] getColumnList() { 75 | return columnList; 76 | } 77 | 78 | public void setColumnList(String[] columnList) { 79 | checkReadOnly(); 80 | this.columnList = columnList; 81 | } 82 | 83 | public TableModel getTableModel() { 84 | return tableModel; 85 | } 86 | 87 | public void setTableModel(TableModel tableModel) { 88 | checkReadOnly(); 89 | this.tableModel = tableModel; 90 | } 91 | 92 | } 93 | -------------------------------------------------------------------------------- /core/src/main/java/com/github/drinkjava2/jdialects/springsrc/PackageInfo.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2002-2017 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 | * use this file except in compliance with the License. You may obtain a copy of 6 | * the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 | * License for the specific language governing permissions and limitations under 14 | * the License. 15 | */ 16 | 17 | package com.github.drinkjava2.jdialects.springsrc; 18 | 19 | /** 20 | * All .java files in this folder are copied from Spring4.3.9.Release, no any 21 | * change except the package name 22 | * 23 | * @author Yong Zhu 24 | * @since 1.7.0 25 | */ 26 | public abstract class PackageInfo { 27 | } 28 | -------------------------------------------------------------------------------- /core/src/main/java/com/github/drinkjava2/jlogs/ConsoleLog.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 | * use this file except in compliance with the License. You may obtain a copy of 6 | * the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 | * License for the specific language governing permissions and limitations under 14 | * the License. 15 | */ 16 | package com.github.drinkjava2.jlogs; 17 | 18 | import java.io.PrintWriter; 19 | import java.io.StringWriter; 20 | 21 | /** 22 | * ConsoleLog only output to console 23 | * 24 | * @author Yong Zhu 25 | * @since 1.7.0 26 | */ 27 | @SuppressWarnings("all") 28 | public class ConsoleLog implements Log { 29 | 30 | public static final int OFF = 5; 31 | public static final int ERROR = 4; 32 | public static final int WARNING = 3; 33 | public static final int INFO = 2; 34 | public static final int DEBUG = 1; 35 | 36 | Class clazz; 37 | private static int logLevel = INFO; 38 | private static boolean logHead = false; 39 | 40 | public ConsoleLog(Class clazz) { 41 | this.clazz = clazz; 42 | } 43 | 44 | public static int getLogLevel() { 45 | return logLevel; 46 | } 47 | 48 | public static void setLogLevel(int logLevel) { 49 | ConsoleLog.logLevel = logLevel; 50 | } 51 | 52 | public static boolean isLogHead() { 53 | return logHead; 54 | } 55 | 56 | public static void setLogHead(boolean logHead) { 57 | ConsoleLog.logHead = logHead; 58 | } 59 | 60 | private String logheadStr(String method) { 61 | return logHead ? new StringBuilder(clazz.getSimpleName()).append(" ").append(method).append(": ").toString() 62 | : ""; 63 | } 64 | 65 | @Override 66 | public void info(String msg) { 67 | if (logLevel <= INFO) 68 | System.out.println(logheadStr("info") + msg); 69 | } 70 | 71 | @Override 72 | public void warn(String msg) { 73 | if (logLevel <= WARNING) 74 | System.out.println(logheadStr("warning") + msg); 75 | } 76 | 77 | @Override 78 | public void warn(String msg, Throwable t) { 79 | if (logLevel <= WARNING) 80 | System.out.println(logheadStr("warning") + msg + getStackTrace(t)); 81 | } 82 | 83 | @Override 84 | public void error(String msg) { 85 | if (logLevel <= ERROR) 86 | System.out.println(logheadStr("error") + msg); 87 | } 88 | 89 | @Override 90 | public void error(String msg, Throwable t) { 91 | if (logLevel <= WARNING) 92 | System.out.println(logheadStr("error") + msg + getStackTrace(t)); 93 | } 94 | 95 | @Override 96 | public void debug(String msg) { 97 | if (logLevel <= DEBUG) 98 | System.out.println(logheadStr("info") + msg); 99 | } 100 | 101 | public static String getStackTrace(Throwable t) { 102 | StringWriter sw = new StringWriter(); 103 | PrintWriter pw = new PrintWriter(sw); 104 | try { 105 | t.printStackTrace(pw); 106 | return sw.toString(); 107 | } finally { 108 | pw.close(); 109 | } 110 | } 111 | } 112 | -------------------------------------------------------------------------------- /core/src/main/java/com/github/drinkjava2/jlogs/EmptyLog.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 | * use this file except in compliance with the License. You may obtain a copy of 6 | * the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 | * License for the specific language governing permissions and limitations under 14 | * the License. 15 | */ 16 | package com.github.drinkjava2.jlogs; 17 | 18 | /** 19 | * EmptyLog is an empty implementation of Log 20 | * 21 | * @author Yong Zhu 22 | * @since 1.7.0 23 | */ 24 | @SuppressWarnings("all") 25 | public class EmptyLog implements Log { 26 | 27 | public EmptyLog(Class clazz) { 28 | } 29 | 30 | @Override 31 | public void info(String msg) { 32 | } 33 | 34 | @Override 35 | public void warn(String msg) { 36 | } 37 | 38 | @Override 39 | public void warn(String msg, Throwable t) { 40 | } 41 | 42 | @Override 43 | public void error(String msg) { 44 | 45 | } 46 | 47 | @Override 48 | public void error(String msg, Throwable t) { 49 | } 50 | 51 | @Override 52 | public void debug(String msg) { 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /core/src/main/java/com/github/drinkjava2/jlogs/Log.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 | * use this file except in compliance with the License. You may obtain a copy of 6 | * the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 | * License for the specific language governing permissions and limitations under 14 | * the License. 15 | */ 16 | package com.github.drinkjava2.jlogs; 17 | 18 | /** 19 | * JLog used for inside of jSqlBox project, if a "jlogs.properties" file if 20 | * found on class root folder (main/resources), will try load the designated 21 | * JLog implementation, otherwise use default ConsoleLog as logger
22 | * 23 | * An example of "jlogs.properties":
24 | * log=com.github.drinkjava2.jlogs.SimpleSLF4JLog 25 | * 26 | * Another full example of using SLF4j+LogBack in production environment please 27 | * see jsqlbox-jbooox demo project 28 | * 29 | * @author Yong Zhu 30 | * @since 2.0.5 31 | */ 32 | public interface Log { 33 | 34 | public void info(String msg); 35 | 36 | public void warn(String msg); 37 | 38 | public void warn(String msg, Throwable t); 39 | 40 | public void error(String msg); 41 | 42 | public void error(String msg, Throwable t); 43 | 44 | public void debug(String msg); 45 | 46 | } 47 | -------------------------------------------------------------------------------- /core/src/main/java/com/github/drinkjava2/jlogs/LogFactory.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 | * use this file except in compliance with the License. You may obtain a copy of 6 | * the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 | * License for the specific language governing permissions and limitations under 14 | * the License. 15 | */ 16 | package com.github.drinkjava2.jlogs; 17 | 18 | import java.io.IOException; 19 | import java.io.InputStream; 20 | import java.lang.reflect.Constructor; 21 | import java.util.Properties; 22 | 23 | /** 24 | * This LogFactory is designed for get a jLog implement used inside of jSqlBox 25 | * project. 26 | * 27 | * Usage: Log log= LogFactory.getLog(Xxx.class); 28 | * 29 | * JLog used for inside of jSqlBox project, if a "jlogs.properties" file if 30 | * found on class root folder (main/resources), will try load the designated 31 | * JLog implementation, otherwise use default ConsoleLog as logger
32 | * 33 | * An example of "jlogs.properties":
34 | * log=com.github.drinkjava2.jlogs.SimpleSLF4JLog 35 | * 36 | * @author Yong Zhu 37 | * @since 1.7.0 38 | */ 39 | public abstract class LogFactory {// NOSONAR 40 | 41 | private static boolean printed = false; 42 | private static Class dbProLogClass = null; 43 | 44 | /** 45 | * Find jlogs.properties configuration, if not found or jlogs.properties is 46 | * empty, use default ConsoleLog 47 | */ 48 | public static Log getLog(Class clazz) { 49 | if (dbProLogClass == void.class) 50 | return new ConsoleLog(clazz); 51 | 52 | if (dbProLogClass != null) 53 | try { 54 | Constructor constr = dbProLogClass.getConstructor(Class.class); 55 | return (Log) constr.newInstance(clazz); 56 | } catch (Exception e) { 57 | if (!printed()) 58 | System.err.println("Can not load log class: " + dbProLogClass // NOSONAR 59 | + ", will use ConsoleLog JLog logger. \r\n" + e.getMessage()); 60 | dbProLogClass = void.class; 61 | return new ConsoleLog(clazz); 62 | } 63 | 64 | InputStream is = Log.class.getClassLoader().getResourceAsStream("jlogs.properties"); 65 | if (is == null) { 66 | if (!printed()) 67 | System.out.println("Not found jlogs.properties for jSqlBox, will use ConsoleLog as default logger.");// NOSONAR 68 | dbProLogClass = void.class; 69 | return new ConsoleLog(clazz); 70 | } 71 | 72 | Properties prop = new Properties(); 73 | String className = ""; 74 | try { 75 | prop.load(is); 76 | className = prop.getProperty("log"); 77 | dbProLogClass = Class.forName(className); 78 | if (!printed()) 79 | System.out.print("jlog.properties found, will use " + className + " as JLog logger."); // NOSONAR 80 | return getLog(clazz); 81 | } catch (Exception e) { 82 | if (!printed()) 83 | System.err.println("No or wrong jlog.properties file: " + className // NOSONAR 84 | + ", will use ConsoleLog as JLog logger. \r\n" + e.getMessage()); 85 | dbProLogClass = void.class; 86 | return new ConsoleLog(clazz); 87 | } finally { 88 | try { 89 | is.close(); 90 | } catch (IOException e) { 91 | // Do nothing 92 | } 93 | } 94 | } 95 | 96 | private static boolean printed() { 97 | boolean old = printed; 98 | printed = true; 99 | return old; 100 | } 101 | 102 | } 103 | -------------------------------------------------------------------------------- /core/src/main/java/com/github/drinkjava2/jlogs/SimpleSLF4JLog.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 | * use this file except in compliance with the License. You may obtain a copy of 6 | * the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 | * License for the specific language governing permissions and limitations under 14 | * the License. 15 | */ 16 | package com.github.drinkjava2.jlogs; 17 | 18 | import java.lang.reflect.Method; 19 | 20 | /** 21 | * SimpleSLF4JLog use SLF4J Logger, to use it, need put a file 22 | * “jlogs.properties” in main/resources or test/resources folder with below 23 | * line:
24 | * log=com.github.drinkjava2.jlogs.SLF4JLog 25 | * 26 | * And of cause, related SLF4J dependencies or logback-classic dependency which 27 | * depends on SLF4J need added in pom.xml 28 | * 29 | * Note: this SimpleSLF4JLog is based on method reflection, not recommended to 30 | * use in envirements which effectiveness is critical, for that a non-reflection 31 | * logger needed, please see the example SLF4JLog.java in jsqlbox-jbooox project 32 | * 33 | * @author Yong Zhu 34 | * @since 2.0.5 35 | */ 36 | public class SimpleSLF4JLog implements Log { 37 | private Object logger; 38 | private Method info; 39 | private Method debug; 40 | private Method warn; 41 | private Method warnExp; 42 | private Method error; 43 | private Method errorExp; 44 | 45 | public SimpleSLF4JLog(Class targetClass) { 46 | try { 47 | Class logFactoryClass = Class.forName("org.slf4j.LoggerFactory"); 48 | Method method = logFactoryClass.getMethod("getLogger", Class.class); 49 | logger = method.invoke(logFactoryClass, targetClass); 50 | debug = logger.getClass().getMethod("debug", String.class); 51 | info = logger.getClass().getMethod("info", String.class); 52 | warn = logger.getClass().getMethod("warn", String.class); 53 | error = logger.getClass().getMethod("error", String.class); 54 | warnExp = logger.getClass().getMethod("warn", String.class, Throwable.class); 55 | errorExp = logger.getClass().getMethod("error", String.class, Throwable.class); 56 | } catch (Exception e) { 57 | throw new RuntimeException(e);//NOSONAR 58 | } 59 | } 60 | 61 | public void call(Method method, String msg) { 62 | try { 63 | method.invoke(logger, msg); 64 | } catch (Exception e) { 65 | // do nothing 66 | } 67 | } 68 | 69 | @Override 70 | public void info(String msg) { 71 | call(info, msg); 72 | } 73 | 74 | @Override 75 | public void warn(String msg) { 76 | call(warn, msg); 77 | } 78 | 79 | @Override 80 | public void debug(String msg) { 81 | call(debug, msg); 82 | } 83 | 84 | @Override 85 | public void warn(String msg, Throwable t) { 86 | try { 87 | warnExp.invoke(logger, msg, t); 88 | } catch (Exception e) { 89 | // do nothing 90 | } 91 | } 92 | 93 | @Override 94 | public void error(String msg) { 95 | call(error, msg); 96 | } 97 | 98 | @Override 99 | public void error(String msg, Throwable t) { 100 | try { 101 | errorExp.invoke(logger, msg, t); 102 | } catch (Exception e) { 103 | // do nothing 104 | } 105 | } 106 | 107 | } 108 | -------------------------------------------------------------------------------- /core/src/test/java/Readme.java: -------------------------------------------------------------------------------- 1 | /** 2 | * As jDialects is a child project of jSqlBox, unit test is inside of jSqlBox, 3 | * not at here 4 | * 5 | * @author Yong Zhu 6 | * @since 1.7.0.1 7 | */ 8 | 9 | public interface Readme { 10 | } 11 | -------------------------------------------------------------------------------- /core/src/test/resources/common-logging.properties: -------------------------------------------------------------------------------- 1 | #if commons log jar found in class path, will use it, otherwise will use JDK logger. 2 | 3 | #if found commons log jar, this configuration file will effect 4 | 5 | #Below line tell commons log to load Log4j 6 | org.apache.commons.logging.Log=org.apache.commons.logging.impl.Log4JLogger 7 | -------------------------------------------------------------------------------- /core/src/test/resources/log4j.properties: -------------------------------------------------------------------------------- 1 | log4j.rootLogger=INFO, stdout 2 | log4j.appender.stdout=org.apache.log4j.ConsoleAppender 3 | log4j.appender.stdout.Target=System.out 4 | log4j.appender.stdout.layout=org.apache.log4j.PatternLayout 5 | #log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{20}:%L-%m%n 6 | log4j.appender.stdout.layout.ConversionPattern=%m%n 7 | -------------------------------------------------------------------------------- /demo/demo-jsqlbox/maven_clean.bat: -------------------------------------------------------------------------------- 1 | mvn clean -------------------------------------------------------------------------------- /demo/demo-jsqlbox/maven_eclipse_clean.bat: -------------------------------------------------------------------------------- 1 | mvn eclipse:clean -------------------------------------------------------------------------------- /demo/demo-jsqlbox/maven_eclipse_eclipse.bat: -------------------------------------------------------------------------------- 1 | call mvn eclipse:eclipse 2 | pause -------------------------------------------------------------------------------- /demo/demo-jsqlbox/maven_test.bat: -------------------------------------------------------------------------------- 1 | call mvn clean test 2 | pause -------------------------------------------------------------------------------- /demo/demo-jsqlbox/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | com.github.drinkjava2 5 | demo-jsqlbox 6 | 1.0 7 | jar 8 | 9 | demo-jsqlbox 10 | jDialects demo for use in jSqlBox 11 | 12 | 13 | UTF-8 14 | UTF-8 15 | UTF-8 16 | 17 | 18 | 19 | 20 | 21 | 22 | com.github.drinkjava2 23 | jsqlbox 24 | 5.0.5.jre8 25 | test 26 | 27 | 28 | 29 | junit 30 | junit 31 | 4.13.1 32 | test 33 | 34 | 35 | 36 | 37 | org.slf4j 38 | slf4j-api 39 | 1.7.25 40 | test 41 | 42 | 43 | org.slf4j 44 | slf4j-log4j12 45 | 1.7.25 46 | test 47 | 48 | 49 | 50 | 51 | com.zaxxer 52 | HikariCP-java6 53 | 2.3.13 54 | test 55 | 56 | 57 | 58 | 59 | com.h2database 60 | h2 61 | 2.1.210 62 | test 63 | 64 | 65 | 66 | 67 | mysql 68 | mysql-connector-java 69 | 8.0.28 70 | test 71 | 72 | 73 | 74 | 75 | com.microsoft.sqlserver 76 | mssql-jdbc 77 | 6.1.0.jre8 78 | test 79 | 80 | 81 | 82 | 83 | 84 | 85 | -------------------------------------------------------------------------------- /demo/demo-jsqlbox/src/test/java/com/github/drinkjava2/demo/TestDemo.java: -------------------------------------------------------------------------------- 1 | package com.github.drinkjava2.demo; 2 | 3 | import java.util.List; 4 | import java.util.Map; 5 | 6 | import org.junit.Assert; 7 | import org.junit.Test; 8 | 9 | import com.github.drinkjava2.jdialects.annotation.jdia.UUID25; 10 | import com.github.drinkjava2.jdialects.annotation.jpa.Id; 11 | import com.github.drinkjava2.jdialects.annotation.jpa.Table; 12 | import com.github.drinkjava2.jsqlbox.ActiveRecord; 13 | import com.github.drinkjava2.jsqlbox.SqlBoxContext; 14 | import com.zaxxer.hikari.HikariDataSource; 15 | 16 | public class TestDemo { 17 | 18 | @Table(name = "users") 19 | public static class User extends ActiveRecord { 20 | @UUID25 21 | @Id 22 | private String id; 23 | 24 | private String firstName; 25 | 26 | private String lastName; 27 | 28 | private Integer age; 29 | 30 | public String getId() { 31 | return id; 32 | } 33 | 34 | public void setId(String id) { 35 | this.id = id; 36 | } 37 | 38 | public String getFirstName() { 39 | return firstName; 40 | } 41 | 42 | public void setFirstName(String firstName) { 43 | this.firstName = firstName; 44 | } 45 | 46 | public String getLastName() { 47 | return lastName; 48 | } 49 | 50 | public void setLastName(String lastName) { 51 | this.lastName = lastName; 52 | } 53 | 54 | public Integer getAge() { 55 | return age; 56 | } 57 | 58 | public void setAge(Integer age) { 59 | this.age = age; 60 | } 61 | } 62 | 63 | @Test 64 | public void doTest() { 65 | HikariDataSource ds = new HikariDataSource();// DataSource 66 | 67 | // H2 is a memory database 68 | ds.setDriverClassName("org.h2.Driver"); 69 | ds.setJdbcUrl("jdbc:h2:mem:DBName;MODE=MYSQL;DB_CLOSE_DELAY=-1;TRACE_LEVEL_SYSTEM_OUT=0"); 70 | ds.setUsername("sa"); 71 | ds.setPassword(""); 72 | 73 | // MySQL 74 | // ds.setDriverClassName("com.mysql.jdbc.Driver"); 75 | // ds.setJdbcUrl("jdbc:mysql://127.0.0.1:3306/test?rewriteBatchedStatements=true&useSSL=false"); 76 | // ds.setUsername("root"); 77 | // ds.setPassword("root888"); 78 | 79 | // MS-SqlServer 80 | // ds.setDriverClassName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); 81 | // ds.setJdbcUrl("jdbc:sqlserver://localhost:1433;databaseName=test"); 82 | // ds.setUsername("sa"); 83 | // ds.setPassword("root888"); 84 | 85 | // ORACLE 86 | // ds.setDriverClassName("oracle.jdbc.OracleDriver"); 87 | // ds.setJdbcUrl("jdbc:oracle:thin:@127.0.0.1:1521:XE"); 88 | // ds.setUsername("root"); 89 | // ds.setPassword("root888"); 90 | SqlBoxContext.setGlobalNextAllowShowSql(true); 91 | SqlBoxContext ctx = new SqlBoxContext(ds); 92 | SqlBoxContext.setGlobalSqlBoxContext(ctx); 93 | 94 | String[] ddlArray = ctx.toDropAndCreateDDL(User.class); 95 | for (String ddl : ddlArray) 96 | ctx.quiteExecute(ddl); 97 | 98 | for (int i = 1; i <= 100; i++) { 99 | User u = new User(); 100 | u.setFirstName("Foo" + i); 101 | u.setLastName("Bar" + i); 102 | u.setAge(i); 103 | u.insert(); 104 | } 105 | Assert.assertEquals(100, ctx.nQueryForLongValue("select count(*) from users")); 106 | 107 | List> users = ctx.nQueryForMapList( 108 | ctx.pagin(2, 10, "select concat(firstName, ' ', lastName) as USERNAME, age from users where age>?"), 109 | 50); 110 | 111 | Assert.assertEquals(10, users.size()); 112 | 113 | for (Map map : users) 114 | System.out.println("UserName=" + map.get("USERNAME") + ", age=" + map.get("AGE")); 115 | 116 | ds.close(); 117 | } 118 | } 119 | -------------------------------------------------------------------------------- /demo/demo-jsqlbox/src/test/resources/common-logging.properties: -------------------------------------------------------------------------------- 1 | #if commons log jar found in class path, will use it, otherwise will use JDK logger. 2 | 3 | #if found commons log jar, this configuration file will effect 4 | 5 | #Below line tell commons log to load Log4j 6 | org.apache.commons.logging.Log=org.apache.commons.logging.impl.Log4JLogger 7 | -------------------------------------------------------------------------------- /demo/demo-jsqlbox/src/test/resources/log4j.properties: -------------------------------------------------------------------------------- 1 | log4j.rootLogger=INFO, stdout 2 | log4j.appender.stdout=org.apache.log4j.ConsoleAppender 3 | log4j.appender.stdout.Target=System.out 4 | log4j.appender.stdout.layout=org.apache.log4j.PatternLayout 5 | log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{20}:%L - %m%n 6 | 7 | log4j.additivity.com.github.drinkjava2=false 8 | log4j.logger.com.github.drinkjava2=info, stdout2 9 | log4j.appender.stdout2=org.apache.log4j.ConsoleAppender 10 | log4j.appender.stdout2.Target=System.out 11 | log4j.appender.stdout2.layout=org.apache.log4j.PatternLayout 12 | log4j.appender.stdout2.layout.ConversionPattern=%m%n -------------------------------------------------------------------------------- /demo/demo-mybatis/maven_clean.bat: -------------------------------------------------------------------------------- 1 | mvn clean -------------------------------------------------------------------------------- /demo/demo-mybatis/maven_eclipse_clean.bat: -------------------------------------------------------------------------------- 1 | mvn eclipse:clean -------------------------------------------------------------------------------- /demo/demo-mybatis/maven_eclipse_eclipse.bat: -------------------------------------------------------------------------------- 1 | call mvn eclipse:eclipse 2 | pause -------------------------------------------------------------------------------- /demo/demo-mybatis/maven_test.bat: -------------------------------------------------------------------------------- 1 | call mvn clean test 2 | pause -------------------------------------------------------------------------------- /demo/demo-mybatis/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | com.github.drinkjava2 5 | demo-mybatis 6 | 1.0 7 | jar 8 | 9 | demo-mybatis 10 | jDialects demo for use in mybatis, this demo need JDK8+ 11 | 12 | 13 | UTF-8 14 | UTF-8 15 | UTF-8 16 | 17 | 18 | 19 | 20 | junit 21 | junit 22 | 4.13.1 23 | test 24 | 25 | 26 | 28 | 29 | com.github.drinkjava2 30 | jsqlbox 31 | 5.0.5.jre8 32 | test 33 | 34 | 35 | 36 | 37 | 38 | org.mybatis 39 | mybatis 40 | 3.5.6 41 | test 42 | 43 | 44 | 45 | 46 | 47 | com.zaxxer 48 | HikariCP-java6 49 | 2.3.13 50 | test 51 | 52 | 53 | 54 | 55 | com.h2database 56 | h2 57 | 2.1.210 58 | test 59 | 60 | 61 | 62 | 63 | mysql 64 | mysql-connector-java 65 | 8.0.28 66 | test 67 | 68 | 69 | 70 | 71 | com.microsoft.sqlserver 72 | mssql-jdbc 73 | 6.1.0.jre8 74 | test 75 | 76 | 77 | 78 | 84 | 85 | 86 | 92 | 93 | 94 | 95 | 96 | 97 | -------------------------------------------------------------------------------- /demo/demo-mybatis/src/test/resources/common-logging.properties: -------------------------------------------------------------------------------- 1 | #if commons log jar found in class path, will use it, otherwise will use JDK logger. 2 | 3 | #if found commons log jar, this configuration file will effect 4 | 5 | #Below line tell commons log to load Log4j 6 | org.apache.commons.logging.Log=org.apache.commons.logging.impl.Log4JLogger 7 | -------------------------------------------------------------------------------- /demo/demo-mybatis/src/test/resources/log4j.properties: -------------------------------------------------------------------------------- 1 | log4j.rootLogger=INFO, stdout 2 | log4j.appender.stdout=org.apache.log4j.ConsoleAppender 3 | log4j.appender.stdout.Target=System.out 4 | log4j.appender.stdout.layout=org.apache.log4j.PatternLayout 5 | log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{20}:%L - %m%n 6 | 7 | log4j.additivity.com.github.drinkjava2=false 8 | log4j.logger.com.github.drinkjava2=info, stdout2 9 | log4j.appender.stdout2=org.apache.log4j.ConsoleAppender 10 | log4j.appender.stdout2.Target=System.out 11 | log4j.appender.stdout2.layout=org.apache.log4j.PatternLayout 12 | log4j.appender.stdout2.layout.ConversionPattern=%m%n -------------------------------------------------------------------------------- /demo/demo-pure-jdbc/maven_clean.bat: -------------------------------------------------------------------------------- 1 | mvn clean -------------------------------------------------------------------------------- /demo/demo-pure-jdbc/maven_eclipse_clean.bat: -------------------------------------------------------------------------------- 1 | mvn eclipse:clean -------------------------------------------------------------------------------- /demo/demo-pure-jdbc/maven_eclipse_eclipse.bat: -------------------------------------------------------------------------------- 1 | call mvn eclipse:eclipse 2 | pause -------------------------------------------------------------------------------- /demo/demo-pure-jdbc/maven_test.bat: -------------------------------------------------------------------------------- 1 | call mvn clean test 2 | pause -------------------------------------------------------------------------------- /demo/demo-pure-jdbc/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | com.github.drinkjava2 5 | demo-pure-jdbc 6 | 1.0 7 | jar 8 | 9 | demo-pure-jdbc 10 | jDialects demo for use in pure JDBC 11 | 12 | 13 | UTF-8 14 | UTF-8 15 | UTF-8 16 | 17 | 18 | 19 | 20 | com.github.drinkjava2 21 | jdialects 22 | 5.0.5.jre8 23 | test 24 | 25 | 26 | 27 | junit 28 | junit 29 | 4.13.1 30 | test 31 | 32 | 33 | 34 | 35 | com.zaxxer 36 | HikariCP-java6 37 | 2.3.13 38 | test 39 | 40 | 41 | 42 | 43 | com.h2database 44 | h2 45 | 2.1.210 46 | test 47 | 48 | 49 | 50 | 51 | mysql 52 | mysql-connector-java 53 | 8.0.28 54 | test 55 | 56 | 57 | 58 | 59 | com.microsoft.sqlserver 60 | mssql-jdbc 61 | 6.1.0.jre8 62 | test 63 | 64 | 65 | 66 | 67 | 68 | 69 | -------------------------------------------------------------------------------- /demo/demo-pure-jdbc/src/test/java/com/github/drinkjava2/demo/TestDemo.java: -------------------------------------------------------------------------------- 1 | package com.github.drinkjava2.demo; 2 | 3 | import java.sql.Connection; 4 | import java.sql.PreparedStatement; 5 | import java.sql.ResultSet; 6 | import java.sql.ResultSetMetaData; 7 | import java.sql.SQLException; 8 | import java.util.ArrayList; 9 | import java.util.LinkedHashMap; 10 | import java.util.List; 11 | import java.util.Map; 12 | 13 | import org.junit.Assert; 14 | import org.junit.Test; 15 | 16 | import com.github.drinkjava2.jdialects.Dialect; 17 | import com.github.drinkjava2.jdialects.model.TableModel; 18 | import com.zaxxer.hikari.HikariDataSource; 19 | 20 | public class TestDemo { 21 | 22 | @SuppressWarnings("unchecked") 23 | public static T queryForObject(Connection conn, String sql, Object... params) { 24 | ResultSet rs = null; 25 | PreparedStatement pst = null; 26 | try { 27 | int i = 1; 28 | pst = conn.prepareStatement(sql);// NOSONAR 29 | for (Object obj : params) 30 | pst.setObject(i++, obj); 31 | rs = pst.executeQuery(); 32 | if (rs.next()) 33 | return (T) rs.getObject(1); 34 | else 35 | return null; 36 | } catch (SQLException e) { 37 | e.printStackTrace(); 38 | return null; 39 | } finally { 40 | if (rs != null) 41 | try { 42 | rs.close(); 43 | } catch (SQLException e) { 44 | e.printStackTrace(); 45 | } 46 | if (pst != null) 47 | try { 48 | pst.close(); 49 | } catch (SQLException e) { 50 | e.printStackTrace(); 51 | } 52 | } 53 | } 54 | 55 | public static List> queryForMapList(Connection conn, String sql, Object... params) { 56 | ResultSet rs = null; 57 | PreparedStatement pst = null; 58 | List> list = new ArrayList>(); 59 | try { 60 | int i = 1; 61 | pst = conn.prepareStatement(sql);// NOSONAR 62 | for (Object obj : params) 63 | pst.setObject(i++, obj); 64 | rs = pst.executeQuery(); 65 | ResultSetMetaData md = rs.getMetaData(); 66 | int columnCount = md.getColumnCount(); 67 | while (rs.next()) { 68 | LinkedHashMap rowData = new LinkedHashMap(); 69 | for (int j = 1; j <= columnCount; j++) 70 | rowData.put(md.getColumnName(j).toUpperCase(), rs.getObject(j)); 71 | list.add(rowData); 72 | } 73 | return list; 74 | } catch (SQLException e) { 75 | e.printStackTrace(); 76 | return null; 77 | } finally { 78 | if (rs != null) 79 | try { 80 | rs.close(); 81 | } catch (SQLException e) { 82 | e.printStackTrace(); 83 | } 84 | if (pst != null) 85 | try { 86 | pst.close(); 87 | } catch (SQLException e) { 88 | e.printStackTrace(); 89 | } 90 | } 91 | } 92 | 93 | public int execute(Connection conn, String sql, Object... params) throws SQLException { 94 | PreparedStatement pst = null; 95 | try { 96 | int i = 1; 97 | pst = conn.prepareStatement(sql);// NOSONAR 98 | for (Object obj : params) 99 | pst.setObject(i++, obj); 100 | pst.execute(); 101 | return 1; 102 | } catch (SQLException e) { 103 | e.printStackTrace(); 104 | return 0; 105 | } finally { 106 | if (pst != null) 107 | try { 108 | pst.close(); 109 | } catch (SQLException e) { 110 | e.printStackTrace(); 111 | } 112 | } 113 | } 114 | 115 | @Test 116 | public void doTest() { 117 | HikariDataSource ds = new HikariDataSource();// DataSource 118 | 119 | // H2 is a memory database 120 | ds.setDriverClassName("org.h2.Driver"); 121 | ds.setJdbcUrl("jdbc:h2:mem:DBName;MODE=MYSQL;DB_CLOSE_DELAY=-1;TRACE_LEVEL_SYSTEM_OUT=0"); 122 | ds.setUsername("sa"); 123 | ds.setPassword(""); 124 | 125 | // MySQL 126 | // ds.setDriverClassName("com.mysql.jdbc.Driver"); 127 | // ds.setJdbcUrl("jdbc:mysql://127.0.0.1:3306/test?rewriteBatchedStatements=true&useSSL=false"); 128 | // ds.setUsername("root"); 129 | // ds.setPassword("root888"); 130 | 131 | // MS-SqlServer 132 | // ds.setDriverClassName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); 133 | // ds.setJdbcUrl("jdbc:sqlserver://localhost:1433;databaseName=test"); 134 | // ds.setUsername("sa"); 135 | // ds.setPassword("root888"); 136 | 137 | // ORACLE 138 | // ds.setDriverClassName("oracle.jdbc.OracleDriver"); 139 | // ds.setJdbcUrl("jdbc:oracle:thin:@127.0.0.1:1521:XE"); 140 | // ds.setUsername("root"); 141 | // ds.setPassword("root888"); 142 | 143 | Dialect dialect = Dialect.guessDialect(ds); 144 | //Dialect.setAllowShowDialectLog(true); 145 | 146 | Connection conn = null; 147 | try { 148 | conn = ds.getConnection(); 149 | 150 | TableModel t = new TableModel("users"); 151 | t.column("firstName").VARCHAR(20).pkey(); 152 | t.column("lastName").VARCHAR(20).pkey(); 153 | t.column("age").INTEGER(); 154 | 155 | String[] ddlArray = dialect.toDropAndCreateDDL(t); 156 | for (String ddl : ddlArray) 157 | try { 158 | execute(conn, ddl); 159 | } catch (Exception e) { 160 | } 161 | 162 | for (int i = 1; i <= 100; i++) 163 | execute(conn, "insert into users (firstName, lastName, age) values(?,?,?)", "Foo" + i, "Bar" + i, i); 164 | 165 | Assert.assertEquals(100L, ((Number) queryForObject(conn, "select count(*) from users")).longValue()); 166 | 167 | List> users = queryForMapList(conn, dialect.paginAndTrans(2, 10, 168 | "select concat(firstName, ' ', lastName) as UserName, age from users where age>?"), 50); 169 | 170 | Assert.assertEquals(10, users.size()); 171 | 172 | for (Map map : users) 173 | System.out.println("UserName=" + map.get("USERNAME") + ", age=" + map.get("AGE")); 174 | 175 | } catch (SQLException e) { 176 | e.printStackTrace(); 177 | } finally { 178 | try { 179 | if (conn != null) 180 | conn.close(); 181 | } catch (SQLException e) { 182 | e.printStackTrace(); 183 | } 184 | } 185 | 186 | ds.close(); 187 | } 188 | } 189 | -------------------------------------------------------------------------------- /demo/demo-pure-jdbc/src/test/resources/common-logging.properties: -------------------------------------------------------------------------------- 1 | #if commons log jar found in class path, will use it, otherwise will use JDK logger. 2 | 3 | #if found commons log jar, this configuration file will effect 4 | 5 | #Below line tell commons log to load Log4j 6 | org.apache.commons.logging.Log=org.apache.commons.logging.impl.Log4JLogger 7 | -------------------------------------------------------------------------------- /demo/demo-pure-jdbc/src/test/resources/log4j.properties: -------------------------------------------------------------------------------- 1 | log4j.rootLogger=INFO, stdout 2 | log4j.appender.stdout=org.apache.log4j.ConsoleAppender 3 | log4j.appender.stdout.Target=System.out 4 | log4j.appender.stdout.layout=org.apache.log4j.PatternLayout 5 | log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{20}:%L - %m%n 6 | 7 | log4j.additivity.com.github.drinkjava2=false 8 | log4j.logger.com.github.drinkjava2=info, stdout2 9 | log4j.appender.stdout2=org.apache.log4j.ConsoleAppender 10 | log4j.appender.stdout2.Target=System.out 11 | log4j.appender.stdout2.layout=org.apache.log4j.PatternLayout 12 | log4j.appender.stdout2.layout.ConversionPattern=%m%n -------------------------------------------------------------------------------- /jdialects-logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/drinkjava2/jDialects/e08cc354c2b3be6f4a710a8c3659d8c0b2609914/jdialects-logo.png --------------------------------------------------------------------------------