@@ -96,6 +96,7 @@ public ClassInfo generateSelectSqlBySQLPraser(ParamInfo paramInfo) throws Except
9696 if (!CollectionUtils .isEmpty (tableNameList )) {
9797 String tableName = tableNameList .get (0 ).trim ();
9898 classInfo .setTableName (tableName );
99+ classInfo .setOriginTableName (tableName );
99100 String className = StringUtilsPlus .upperCaseFirst (StringUtilsPlus .underlineToCamelCase (tableName ));
100101 if (className .contains ("_" )) {
101102 className = className .replaceAll ("_" , "" );
@@ -121,21 +122,15 @@ public ClassInfo generateSelectSqlBySQLPraser(ParamInfo paramInfo) throws Except
121122 fieldName =fieldName .contains ("." )?fieldName .substring (fieldName .indexOf ("." )+1 ):fieldName ;
122123 //转换前
123124 fieldInfo .setColumnName (fieldName );
124- switch ((String ) paramInfo .getOptions ().get ("nameCaseType" )) {
125- case ParamInfo .NAME_CASE_TYPE .CAMEL_CASE :
125+ fieldName = switch ((String ) paramInfo .getOptions ().get ("nameCaseType" )) {
126+ case ParamInfo .NAME_CASE_TYPE .CAMEL_CASE ->
126127 // 2024-1-27 L&J 适配任意(maybe)原始风格转小写驼峰
127- fieldName = StringUtilsPlus .toLowerCamel (aliasName );
128- break ;
129- case ParamInfo .NAME_CASE_TYPE .UNDER_SCORE_CASE :
130- fieldName = StringUtilsPlus .toUnderline (aliasName , false );
131- break ;
132- case ParamInfo .NAME_CASE_TYPE .UPPER_UNDER_SCORE_CASE :
133- fieldName = StringUtilsPlus .toUnderline (aliasName .toUpperCase (), true );
134- break ;
135- default :
136- fieldName = aliasName ;
137- break ;
138- }
128+ StringUtilsPlus .toLowerCamel (aliasName );
129+ case ParamInfo .NAME_CASE_TYPE .UNDER_SCORE_CASE -> StringUtilsPlus .toUnderline (aliasName , false );
130+ case ParamInfo .NAME_CASE_TYPE .UPPER_UNDER_SCORE_CASE ->
131+ StringUtilsPlus .toUnderline (aliasName .toUpperCase (), true );
132+ default -> aliasName ;
133+ };
139134 //转换后
140135 fieldInfo .setFieldName (fieldName );
141136
@@ -157,62 +152,57 @@ public ClassInfo generateSelectSqlBySQLPraser(ParamInfo paramInfo) throws Except
157152 @ Override
158153 public ClassInfo generateCreateSqlBySQLPraser (ParamInfo paramInfo ) throws Exception {
159154 ClassInfo classInfo = new ClassInfo ();
160- Statement statement = CCJSqlParserUtil .parse (paramInfo .getTableSql ());
161- CCJSqlParserManager parserManager = new CCJSqlParserManager ();
162- statement = parserManager .parse (new StringReader (paramInfo .getTableSql ()));
163- TablesNamesFinder tablesNamesFinder = new TablesNamesFinder (); // 创建表名发现者对象
164- List <String > tableNameList = tablesNamesFinder .getTableList (statement ); // 获取到表名列表
165- //一般这里应该只解析到一个表名,除非多个表名,取第一个
166- if (!CollectionUtils .isEmpty (tableNameList )) {
167- String tableName = tableNameList .get (0 ).trim ();
168- classInfo .setTableName (tableName );
169- String className = StringUtilsPlus .upperCaseFirst (StringUtilsPlus .underlineToCamelCase (tableName ));
170- if (className .contains ("_" )) {
171- className = className .replaceAll ("_" , "" );
172- }
173- classInfo .setClassName (className );
174- classInfo .setClassComment (paramInfo .getTableSql ());
155+ Statement statement = null ;
156+ try {
157+ statement = CCJSqlParserUtil .parse (paramInfo .getTableSql ().trim ());
158+ }catch (Exception e ) {
159+ e .printStackTrace ();
160+ throw new SqlException ("SQL语法错误:" +e .getMessage ());
175161 }
176- //解析查询元素
177- Select select = null ;
178- select = (Select ) CCJSqlParserUtil .parse (paramInfo .getTableSql ());
179- PlainSelect plainSelect = (PlainSelect ) select .getSelectBody ();
180- List <SelectItem <?>> selectItems = plainSelect .getSelectItems ();
181162
182- // field List
163+ // 确保是CREATE TABLE语句
164+ if (!(statement instanceof CreateTable createTable )) {
165+ throw new SqlException ("检测到SQL语句不是DLL CREATE TABLE语句" );
166+ }
167+
168+ // 提取表名
169+ String tableName = createTable .getTable ().getName ();
170+ classInfo .setTableName (tableName );
171+ String className = StringUtilsPlus .upperCaseFirst (StringUtilsPlus .underlineToCamelCase (tableName ));
172+ if (className .contains ("_" )) {
173+ className = className .replaceAll ("_" , "" );
174+ }
175+ classInfo .setClassName (className );
176+ classInfo .setOriginTableName (tableName );
177+ classInfo .setClassComment (paramInfo .getTableSql ());
178+
179+ // 提取字段信息
183180 List <FieldInfo > fieldList = new ArrayList <FieldInfo >();
184- selectItems .forEach (t ->{
185- FieldInfo fieldInfo = new FieldInfo ();
186- String fieldName = ((Column )t .getExpression ()).getColumnName ();
187- String aliasName = t .getAlias () != null ? t .getAlias ().getName () : ((Column )t .getExpression ()).getColumnName ();
188- //存储原始字段名
189- fieldInfo .setFieldComment (aliasName );fieldInfo .setColumnName (aliasName );
190- //处理字段名是t.xxx的情况
191- fieldName =fieldName .contains ("." )?fieldName .substring (fieldName .indexOf ("." )+1 ):fieldName ;
192- //转换前
193- fieldInfo .setColumnName (fieldName );
194- switch ((String ) paramInfo .getOptions ().get ("nameCaseType" )) {
195- case ParamInfo .NAME_CASE_TYPE .CAMEL_CASE :
196- // 2024-1-27 L&J 适配任意(maybe)原始风格转小写驼峰
197- fieldName = StringUtilsPlus .toLowerCamel (aliasName );
198- break ;
199- case ParamInfo .NAME_CASE_TYPE .UNDER_SCORE_CASE :
200- fieldName = StringUtilsPlus .toUnderline (aliasName , false );
201- break ;
202- case ParamInfo .NAME_CASE_TYPE .UPPER_UNDER_SCORE_CASE :
203- fieldName = StringUtilsPlus .toUnderline (aliasName .toUpperCase (), true );
204- break ;
205- default :
206- fieldName = aliasName ;
207- break ;
181+ List <ColumnDefinition > columnDefinitions = createTable .getColumnDefinitions ();
182+
183+ if (columnDefinitions != null ) {
184+ for (ColumnDefinition columnDefinition : columnDefinitions ) {
185+ FieldInfo fieldInfo = new FieldInfo ();
186+ String columnName = columnDefinition .getColumnName ();
187+ fieldInfo .setColumnName (columnName );
188+ fieldInfo .setFieldComment (columnDefinition .toString ());
189+
190+ // 根据命名规则转换字段名
191+ String fieldName = switch ((String ) paramInfo .getOptions ().get ("nameCaseType" )) {
192+ case ParamInfo .NAME_CASE_TYPE .CAMEL_CASE -> StringUtilsPlus .toLowerCamel (columnName );
193+ case ParamInfo .NAME_CASE_TYPE .UNDER_SCORE_CASE -> StringUtilsPlus .toUnderline (columnName , false );
194+ case ParamInfo .NAME_CASE_TYPE .UPPER_UNDER_SCORE_CASE ->
195+ StringUtilsPlus .toUnderline (columnName .toUpperCase (), true );
196+ default -> columnName ;
197+ };
198+ fieldInfo .setFieldName (fieldName );
199+
200+ // 设置字段类型为String(因为无法准确推测类型)
201+ fieldInfo .setFieldClass ("String" );
202+ fieldList .add (fieldInfo );
208203 }
209- //转换后
210- fieldInfo .setFieldName (fieldName );
211-
212- //无法推测类型,所有都set为String
213- fieldInfo .setFieldClass ("String" );
214- fieldList .add (fieldInfo );
215- });
204+ }
205+
216206 classInfo .setFieldList (fieldList );
217207 log .info ("classInfo:{}" , JSON .toJSONString (classInfo ));
218208 return classInfo ;
0 commit comments