1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.Linq;4 usingSystem.Text;5 usingMyTech.CodeGenerator.Utils;6
7 classDaoImpl8 {9 public static string execute(AppSettings appSettings, ListlistColumnInfo)10 {11 string codeStr = "";12 codeStr += classHeader(appSettings, listColumnInfo) + "\r\n";13 codeStr += "\r\n";14 codeStr += xxxRowMapper(appSettings, listColumnInfo) + "\r\n";15 codeStr += "\r\n";16 codeStr += selectByPk(appSettings, listColumnInfo) + "\r\n";17 codeStr += "\r\n";18 codeStr += selectAll(appSettings, listColumnInfo) + "\r\n";19 codeStr += "\r\n";20 codeStr += selectList(appSettings, listColumnInfo) + "\r\n";21 codeStr += "\r\n";22 codeStr += count(appSettings, listColumnInfo) + "\r\n";23 codeStr += "\r\n";24 codeStr += selectPaging(appSettings, listColumnInfo) + "\r\n";25 codeStr += "\r\n";26 codeStr += insert(appSettings, listColumnInfo) + "\r\n";27 codeStr += "\r\n";28 codeStr += insertSelective(appSettings, listColumnInfo) + "\r\n";29 codeStr += "\r\n";30 codeStr += update(appSettings, listColumnInfo) + "\r\n";31 codeStr += "\r\n";32 codeStr += updateSelective(appSettings, listColumnInfo) + "\r\n";33 codeStr += "\r\n";34 codeStr += delete(appSettings, listColumnInfo) + "\r\n";35 codeStr += "\r\n";36 codeStr += delete__batch(appSettings, listColumnInfo) + "\r\n";37
38 codeStr += "}";39
40 //Console.WriteLine("Hello World, from internal!" + listColumnInfo.Count);41 //for(int i=0; i
45 //}
46
47
48
49 returncodeStr;50 }51
52 public static string classHeader(AppSettings appSettings, ListlistColumnInfo)53 {54 StringBuilder sb = newStringBuilder();55
56 sb.AppendLine("package" + appSettings.NamespaceString + ".dao.impl;");57 sb.AppendLine("");58 sb.AppendLine("import" + appSettings.NamespaceString + ".dao." + UnderscoreCamelUtil.ToPascalCase(appSettings.ClassPrefix)+ "Dao;");59 sb.AppendLine("import" + appSettings.NamespaceString + ".model." + UnderscoreCamelUtil.ToPascalCase(appSettings.ClassPrefix) + ";");60 sb.AppendLine("import com.ydtech.common.web.paging.PageResult;");61 sb.AppendLine("import com.github.drinkjava2.jdialects.Dialect;");62 sb.AppendLine("import org.springframework.beans.factory.annotation.Autowired;");63 sb.AppendLine("import org.springframework.jdbc.core.JdbcTemplate;");64 sb.AppendLine("import org.springframework.jdbc.core.PreparedStatementCreator;");65 sb.AppendLine("import org.springframework.jdbc.core.RowMapper;");66 sb.AppendLine("import org.springframework.jdbc.support.GeneratedKeyHolder;");67 sb.AppendLine("import org.springframework.jdbc.support.KeyHolder;");68 sb.AppendLine("import org.springframework.stereotype.Repository;");69 sb.AppendLine("");70 sb.AppendLine("");71 sb.AppendLine("import javax.sql.DataSource;");72 sb.AppendLine("import java.sql.Connection;");73 sb.AppendLine("import java.sql.PreparedStatement;");74 sb.AppendLine("import java.sql.ResultSet;");75 sb.AppendLine("import java.sql.SQLException;");76 sb.AppendLine("");77 sb.AppendLine("" + GetImports(listColumnInfo) + "");78 sb.AppendLine("import java.util.List;");79 sb.AppendLine("import java.util.ArrayList;");80 sb.AppendLine("");81 sb.AppendLine("");82 sb.AppendLine("@Repository");83 sb.AppendLine("public class" + UnderscoreCamelUtil.ToPascalCase(appSettings.ClassPrefix) + "DaoImpl implements" + UnderscoreCamelUtil.ToPascalCase(appSettings.ClassPrefix) + "Dao{");84 sb.AppendLine("");85 sb.AppendLine("@Autowired");86 sb.AppendLine("public DataSource dataSource;");87
88
89 returnsb.ToString();90 }91
92 public static string xxxRowMapper(AppSettings appSettings, ListlistColumnInfo)93 {94 StringBuilder sb = newStringBuilder();95
96 sb.AppendLine("public static class" + UnderscoreCamelUtil.ToPascalCase(appSettings.ClassPrefix) + "RowMapper implements RowMapper {");97 sb.AppendLine("public" + UnderscoreCamelUtil.ToPascalCase(appSettings.ClassPrefix) + "mapRow(ResultSet resultSet, int rowNum) throws SQLException {");98 sb.AppendLine(" " + UnderscoreCamelUtil.ToPascalCase(appSettings.ClassPrefix) + "model = new" + UnderscoreCamelUtil.ToPascalCase(appSettings.ClassPrefix) + "();");99 sb.AppendLine("");100 sb.AppendLine(" " + GetSelectProcessRow(appSettings, listColumnInfo) + ";");101 sb.AppendLine();102 sb.AppendLine("return model;");103 sb.AppendLine("}");104 sb.AppendLine("}");105 sb.AppendLine(" ");106
107 returnsb.ToString();108 }109
110 public static string selectByPk(AppSettings appSettings, ListlistColumnInfo)111 {112 StringBuilder sb = newStringBuilder();113
114 sb.AppendLine("@Override");115 sb.AppendLine("public" + GetClassName(appSettings, listColumnInfo) + "selectByPk(" + GetSelectByPkFunctionSignature(appSettings, listColumnInfo) + "){");116 sb.AppendLine("String sql = \" select" + GetSelectFields(appSettings, listColumnInfo) + "where" +GetSelectByPkWhere(appSettings, listColumnInfo) + "\" ;");117 sb.AppendLine("");118 sb.AppendLine("JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);");119 sb.AppendLine("");120 sb.AppendLine("List ls = jdbcTemplate.query(sql, new Object[] {" + GetSelectByPkParams(appSettings, listColumnInfo) + "}, new" + GetClassName(appSettings, listColumnInfo) + "RowMapper()))");121 sb.AppendLine("");122 sb.AppendLine("if(ls!=null && ls.size()>0){");123 sb.AppendLine("return ls.get(0);");124 sb.AppendLine("}");125 sb.AppendLine("");126 sb.AppendLine("return null;");127 sb.AppendLine("}");128
129
130 returnsb.ToString();131 }132
133 public static string selectAll(AppSettings appSettings, ListlistColumnInfo)134 {135 StringBuilder sb = newStringBuilder();136
137 sb.AppendLine("@Override");138 sb.AppendLine("public List selectAll( ){");139 sb.AppendLine("String sql = \"select" + GetSelectFields(appSettings,listColumnInfo) + "\";");140 sb.AppendLine("");141 sb.AppendLine("JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);");142 sb.AppendLine("");143 sb.AppendLine("return jdbcTemplate.query(sql, new" + UnderscoreCamelUtil.ToPascalCase(appSettings.ClassPrefix)+ "RowMapper());");144 sb.AppendLine("}");145
146 returnsb.ToString();147 }148
149 public static string selectList(AppSettings appSettings, ListlistColumnInfo)150 {151 StringBuilder sb = newStringBuilder();152
153 sb.AppendLine("public List selectList(String whereCase, Object[] paramValues ){");154 sb.AppendLine("String sql = \"select" + GetSelectFields(appSettings, listColumnInfo) + "\" + whereCase;");155 sb.AppendLine("");156 sb.AppendLine("JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);");157 sb.AppendLine("");158 sb.AppendLine("return jdbcTemplate.query(sql, paramValues, new" + UnderscoreCamelUtil.ToPascalCase(appSettings.ClassPrefix) + "RowMapper());");159 sb.AppendLine("}");160
161
162 returnsb.ToString();163 }164
165 public static string count(AppSettings appSettings, ListlistColumnInfo)166 {167 StringBuilder sb = newStringBuilder();168
169 sb.AppendLine("public Integer count(String whereCase, Object[] paramValues){");170 sb.AppendLine("String sql = \"select count(1) from" + appSettings.ClassPrefix + "t \" + whereCase;");171 sb.AppendLine("");172 sb.AppendLine("JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);");173 sb.AppendLine("");174 sb.AppendLine("return jdbcTemplate.queryForObject(sql, paramValues, Integer.class);");175 sb.AppendLine("}");176
177
178 returnsb.ToString();179 }180
181 public static string selectPaging(AppSettings appSettings, ListlistColumnInfo)182 {183 StringBuilder sb = newStringBuilder();184
185 sb.AppendLine("public PageResult selectPaging(int pageNumber, int pageSize, String whereCase, Object[] paramValues){");186 sb.AppendLine("");187 sb.AppendLine("Dialect dialect = Dialect.guessDialect(dataSource);");188 sb.AppendLine("String sql = dialect.pagin(pageNumber,pageSize, \"select" + GetSelectFields(appSettings, listColumnInfo) + "\" + whereCase);");189 sb.AppendLine("");190 sb.AppendLine("JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);");191 sb.AppendLine("List recordList = jdbcTemplate.query(sql, paramValues, new" + UnderscoreCamelUtil.ToPascalCase(appSettings.ClassPrefix) + "RowMapper());");192 sb.AppendLine("");193 sb.AppendLine("int totalCount = count(whereCase, paramValues);");194 sb.AppendLine("");195 sb.AppendLine("PageResult pageResult = new PageResult(pageNumber, pageSize, totalCount, recordList);");196 sb.AppendLine("");197 sb.AppendLine("return pageResult;");198 sb.AppendLine("}");199
200
201 returnsb.ToString();202 }203
204 public static string insert(AppSettings appSettings, ListlistColumnInfo)205 {206 StringBuilder sb = newStringBuilder();207
208 //sb.AppendLine("@*insert时,如果有自动生成的主键就生成返回该主键的函数;没有自动生成的主键,则返回true,false*@ ");209 //sb.AppendLine("@{");
210 if("NO" ==HasAUTO_GENERATED_KEYS(listColumnInfo)){211 sb.AppendLine("@Override");212 sb.AppendLine("public Integer insert(" + UnderscoreCamelUtil.ToPascalCase(appSettings.ClassPrefix)+"model) {");213 sb.AppendLine("");214 sb.AppendLine("String sql = \"insert into"+appSettings.ClassPrefix+"("+GetInsertFields(appSettings, listColumnInfo)+"\";");215 sb.AppendLine("");216 sb.AppendLine("JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);");217 sb.AppendLine("");218 sb.AppendLine("Object[] params = new Object[] {"+GetInsertParams(appSettings, listColumnInfo)+"};");219 sb.AppendLine("");220 sb.AppendLine("int out = jdbcTemplate.update(sql, params);");221 sb.AppendLine("");222 sb.AppendLine("return out;");223 sb.AppendLine("}");224 }225 sb.AppendLine("");226 if("YES" ==HasAUTO_GENERATED_KEYS(listColumnInfo)){227 sb.AppendLine("");228 sb.AppendLine("@Override");229 sb.AppendLine("public Integer insertReturnPk(" + UnderscoreCamelUtil.ToPascalCase(appSettings.ClassPrefix)+"model) {");230 sb.AppendLine("");231 sb.AppendLine("String sql = \"insert into"+appSettings.ClassPrefix+"("+GetInsertFields(appSettings, listColumnInfo)+"\";");232 sb.AppendLine("");233 sb.AppendLine("JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);");234 sb.AppendLine("");235 sb.AppendLine("KeyHolder keyHolder = new GeneratedKeyHolder();");236 sb.AppendLine("jdbcTemplate.update(new PreparedStatementCreator() {");237 sb.AppendLine("@Override");238 sb.AppendLine("public PreparedStatement createPreparedStatement(Connection conn) throws SQLException {");239 sb.AppendLine("PreparedStatement ps = conn.prepareStatement(sql, PreparedStatement.RETURN_GENERATED_KEYS);");240 sb.AppendLine(" " + GetInsertParamsReturnPk(appSettings,listColumnInfo)+"");241 sb.AppendLine("return ps;");242 sb.AppendLine("}");243 sb.AppendLine("}, keyHolder);");244 sb.AppendLine(" ");245 sb.AppendLine("return keyHolder.getKey().intValue();");246 sb.AppendLine("}");247 sb.AppendLine("}");248
249 }250
251
252 returnsb.ToString();253 }254
255
256
257 public static string insertSelective(AppSettings appSettings, ListlistColumnInfo)258 {259 StringBuilder sb = newStringBuilder();260
261 sb.AppendLine("");262 sb.AppendLine("@Override");263 sb.AppendLine("public Integer insertSelective(" + UnderscoreCamelUtil.ToPascalCase(appSettings.ClassPrefix) + "model) {");264 sb.AppendLine("");265 sb.AppendLine("StringBuffer sbSql = new StringBuffer();");266 sb.AppendLine("StringBuffer sbParams = new StringBuffer();");267 sb.AppendLine("ArrayList alParamsValues = new ArrayList();");268 sb.AppendLine("");269 sb.AppendLine("String pre = \"\";");270 sb.AppendLine(" " +GetInsertSelectiveFields(appSettings, listColumnInfo));271 sb.AppendLine(" ");272 sb.AppendLine("String sql = \"INSERT INTO"+appSettings.ClassPrefix+"(\" + sbSql.toString() + \") VALUES (\" + sbParams.toString() + \")\";");273 sb.AppendLine(" ");274 sb.AppendLine("JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);");275 sb.AppendLine("");276 sb.AppendLine("Object[] params = alParamsValues.toArray();");277 sb.AppendLine("");278 sb.AppendLine("int out = jdbcTemplate.update(sql, params);");279 sb.AppendLine("");280 sb.AppendLine("return out;");281 sb.AppendLine("}");282 sb.AppendLine(" ");283
284
285
286 returnsb.ToString();287 }288
289 public static string update(AppSettings appSettings, ListlistColumnInfo)290 {291 StringBuilder sb = newStringBuilder();292
293
294 sb.AppendLine("@Override");295 sb.AppendLine("public Integer update(" + UnderscoreCamelUtil.ToPascalCase(appSettings.ClassPrefix) + "model) {");296 sb.AppendLine("");297 sb.AppendLine("String sql = \"update"+appSettings.ClassPrefix+"set" + GetUpdateFields(appSettings, listColumnInfo) + "\";");298 sb.AppendLine(" ");299 sb.AppendLine("JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);");300 sb.AppendLine(" ");301 sb.AppendLine("Object[] params = new Object[] {" + GetUpdateParams(appSettings, listColumnInfo) +"};");302 sb.AppendLine("");303 sb.AppendLine("return jdbcTemplate.update(sql, params);");304 sb.AppendLine("}");305 sb.AppendLine(" ");306
307
308 returnsb.ToString();309 }310 public static string updateSelective(AppSettings appSettings, ListlistColumnInfo)311 {312 StringBuilder sb = newStringBuilder();313
314
315 sb.AppendLine("@Override");316 sb.AppendLine("public Integer updateSelective(" + UnderscoreCamelUtil.ToPascalCase(appSettings.ClassPrefix) + "model) {");317 sb.AppendLine("");318 sb.AppendLine("StringBuffer sbSql = new StringBuffer();");319 sb.AppendLine("StringBuffer sbParams = new StringBuffer();");320 sb.AppendLine("ArrayList alParamsValues = new ArrayList();");321 sb.AppendLine("");322 sb.AppendLine("String pre = \"\";");323 sb.AppendLine("String preWhere = \"\";");324 sb.AppendLine(" " +GetUpdateSelectiveFields(appSettings, listColumnInfo) );325 sb.AppendLine(" ");326 sb.AppendLine("if(!sbSql.toString().equalsIgnoreCase(\"\")){");327 sb.AppendLine("String sql = \"update"+appSettings.ClassPrefix+"set \" + sbSql.toString() + \" where \" + sbParams.toString() ;");328 sb.AppendLine(" ");329 sb.AppendLine("JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);");330 sb.AppendLine("");331 sb.AppendLine("Object[] params = alParamsValues.toArray();");332 sb.AppendLine("");333 sb.AppendLine("return jdbcTemplate.update(sql, params);");334 sb.AppendLine("}");335 sb.AppendLine(" ");336 sb.AppendLine("return 0;");337 sb.AppendLine("");338 sb.AppendLine("}");339
340
341 returnsb.ToString();342 }343 public static string delete(AppSettings appSettings, ListlistColumnInfo)344 {345 StringBuilder sb = newStringBuilder();346
347
348 sb.AppendLine("@Override");349 sb.AppendLine("public Integer delete(" + GetDeleteFunctionSignature(appSettings, listColumnInfo) + ") {");350 sb.AppendLine("");351 sb.AppendLine("String sql = \"delete from"+appSettings.ClassPrefix+"where"+GetDeleteFields(appSettings, listColumnInfo)+"\";");352 sb.AppendLine(" ");353 sb.AppendLine("JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);");354 sb.AppendLine("");355 sb.AppendLine("return jdbcTemplate.update(sql," + GetDeleteParams(appSettings, listColumnInfo) + ");");356 sb.AppendLine("}");357
358
359 returnsb.ToString();360 }361
362 public static string delete__batch(AppSettings appSettings, ListlistColumnInfo)363 {364 StringBuilder sb = newStringBuilder();365
366
367 sb.AppendLine("@Override");368 sb.AppendLine("public Integer delete(List ls" + UnderscoreCamelUtil.ToPascalCase(appSettings.ClassPrefix) + "){");369 sb.AppendLine("String sql = \"delete from"+ appSettings.ClassPrefix+"where" + GetDeleteFields(appSettings, listColumnInfo) + "\";");370 sb.AppendLine(" ");371 sb.AppendLine("JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);");372 sb.AppendLine("");373 sb.AppendLine("return jdbcTemplate.batchUpdate(sql, new BatchPreparedStatementSetter() {");374 sb.AppendLine("@Override");375 sb.AppendLine("public void setValues(PreparedStatement preparedStatement, int i) throws SQLException {");376 sb.AppendLine(" " +GetDeleteFieldsValue(appSettings, listColumnInfo));377 sb.AppendLine("}");378 sb.AppendLine(" ");379 sb.AppendLine("@Override");380 sb.AppendLine("public int getBatchSize() {");381 sb.AppendLine("return ls" + UnderscoreCamelUtil.ToPascalCase(appSettings.ClassPrefix)+".size();");382 sb.AppendLine("}");383 sb.AppendLine("}).length;");384 sb.AppendLine("}");385
386
387
388 returnsb.ToString();389 }390
391 //以下为公共函数
392 public static string GetClassName(AppSettings appSettings, ListlistColumnInfo){393 returnUnderscoreCamelUtil.ToPascalCase(appSettings.ClassPrefix);394 }395
396 public static string GetSelectProcessRow(AppSettings appSettings, ListlistColumnInfo)397 {398 string result = "";399 string pre = "";400
401 foreach(ColumnInfo ci inlistColumnInfo)402 {403 result += pre + "model.set" +UnderscoreCamelUtil.ToPascalCase(ci.Name.ToString())404 + "(resultSet.get" + Get_GetJavaTypeFromSqlType(ci) + "(\"" + ci.Name.ToString() + "\"))";405 pre = ";\r\n";406 }407
408 returnresult;409 }410
411 public static string GetSelectByPkFunctionSignature(AppSettings appSettings, ListlistColumnInfo)412 {413 string result = "";414 string pre = "";415
416 foreach(ColumnInfo ci inlistColumnInfo)417 {418 if(ci.IsPrimaryKey)419 {420 result += pre + GetJavaTypeFromSqlType(ci) + " " +UnderscoreCamelUtil.ToCamelCase(ci.Name.ToString());421 pre = ",";422 }423 }424
425 returnresult;426 }427
428 public static string GetSelectFields(AppSettings appSettings, ListlistColumnInfo)429 {430 string result = "";431 string where = "";432 string pre = "";433 string preWhere = "";434 string tableName = "";435
436 foreach(ColumnInfo ci inlistColumnInfo)437 {438 //if(ci.IsPrimaryKey)439 //{440 //where += preWhere + ci.Name.ToString();441 //where += "=?";442 //preWhere = ", ";443 //}
444
445 result += pre + "t." +ci.Name.ToString();446 pre = ",";447
448 tableName=ci.TableName;449 }450
451 result += "from" + tableName + "t";452
453 returnresult;454 }455
456 public static string GetSelectByPkWhere(AppSettings appSettings, ListlistColumnInfo)457 {458 string where = "";459 string pre = "";460 string preWhere = "";461 string tableName = "";462
463 foreach(ColumnInfo ci inlistColumnInfo)464 {465 if(ci.IsPrimaryKey)466 {467 where += preWhere +ci.Name.ToString();468 where += "=?";469 preWhere = ",";470 }471
472 tableName=ci.TableName;473 }474
475 return where;476 }477
478 public static string GetSelectByPkParams(AppSettings appSettings, ListlistColumnInfo)479 {480 string result = "";481 string pre = "";482
483 foreach(ColumnInfo ci inlistColumnInfo)484 {485 if(ci.IsPrimaryKey)486 {487 result += pre +UnderscoreCamelUtil.ToCamelCase(ci.Name.ToString());488 pre = ",";489 }490 }491
492 returnresult;493 }494
495 public static string GetInsertFields(AppSettings appSettings, ListlistColumnInfo)496 {497 string result = "";498 string values = "";499 string pre = "";500
501 foreach(ColumnInfo ci inlistColumnInfo)502 {503 if(ci.IsAutoNumber)504 {505 }506 else
507 {508 result += pre +ci.Name.ToString();509 values += pre + "?";510 pre = ",";511 }512 }513
514 result += ") values (" + values + ")";515
516 returnresult;517 }518
519 public static string GetInsertParams(AppSettings appSettings, ListlistColumnInfo)520 {521 string result = "";522 string pre = "";523
524 foreach(ColumnInfo ci inlistColumnInfo)525 {526 if(ci.IsAutoNumber)527 {528 }529 else
530 {531 result += pre + "model.get" + UnderscoreCamelUtil.ToPascalCase(ci.Name.ToString()) + "()";532 pre = ",";533 }534 }535
536 returnresult;537 }538
539 public static string GetInsertParamsReturnPk(AppSettings appSettings, ListlistColumnInfo)540 {541 string result = "";542 string pre = "";543
544 int i = 1;545 foreach(ColumnInfo ci inlistColumnInfo)546 {547 if(ci.IsAutoNumber)548 {549 }550 else
551 {552 //result += pre + "ps.set" + @GetJavaTypeFromSqlType(ci) + "(" + i++ + ", model.get" + UnderscoreCamelUtil.ToPascalCase(ci.Name.ToString()) + "());";
553 result += pre + "ps.setObject(" + i++ + ", model.get" + UnderscoreCamelUtil.ToPascalCase(ci.Name.ToString()) + "());";554 pre = "\r\n";555 }556 }557
558 returnresult;559 }560
561
562 public static string GetInsertSelectiveFields(AppSettings appSettings, ListcolumnInfoList)563 {564 string result = "\r\n";565 string values = "";566 string pre = "";567
568 foreach(ColumnInfo ci incolumnInfoList)569 {570 if(ci.IsAutoNumber)571 {572 }573 else
574 {575 result += "if(model.get" + UnderscoreCamelUtil.ToPascalCase(ci.Name.ToString()) + "() != null){\r\n";576 result += "sbSql.append(pre + \"" + ci.Name.ToString() + "\");\r\n";577 result += "sbParams.append(pre + \"?\");\r\n";578 result += "alParamsValues.add(model.get" + UnderscoreCamelUtil.ToPascalCase(ci.Name.ToString()) + "());\r\n";579 result += "pre = \",\";\r\n";580 result += "}\r\n";581 pre = ",";582 }583 }584
585 returnresult;586 }587
588
589 public static string GetUpdateFields(AppSettings appSettings, ListcolumnInfoList)590 {591 string result = "";592 string where = "";593 string pre = "";594 string preWhere = "";595
596 foreach(ColumnInfo ci incolumnInfoList)597 {598 if(ci.IsPrimaryKey)599 {600 where += preWhere +ci.Name.ToString();601 where += "=?";602 preWhere = ",";603 }else{604 result += pre +ci.Name.ToString();605 result += "=?";606 pre = ",";607 }608 }609
610 result = result + "where" + where;611
612 returnresult;613 }614
615
616 public static string GetUpdateParams(AppSettings appSettings, ListcolumnInfoList)617 {618 string result = "";619 string where = "";620 string pre = "";621 string preWhere = "";622
623 foreach(ColumnInfo ci incolumnInfoList)624 {625 if(ci.IsPrimaryKey)626 {627 where += preWhere + "model.get" + UnderscoreCamelUtil.ToPascalCase(ci.Name.ToString()) + "()";628 preWhere = ",";629 }else{630 result += pre + "model.get" + UnderscoreCamelUtil.ToPascalCase(ci.Name.ToString()) + "()";631 pre = ",";632 }633 }634
635 result = result + "," + where;636
637 returnresult;638 }639
640 public static string GetUpdateSelectiveFields(AppSettings appSettings, ListcolumnInfoList)641 {642 string result = "\r\n";643 string where = "";644 string values = "";645 string pre = "";646 string preWhere = "";647
648 foreach(ColumnInfo ci incolumnInfoList)649 {650 if(ci.IsPrimaryKey)651 {652 }653 else
654 {655 result += "if(model.get" + UnderscoreCamelUtil.ToPascalCase(ci.Name.ToString()) + "() != null){\r\n";656 result += "sbSql.append(pre + \"" + ci.Name.ToString() + "=?\");\r\n";657 //result += " sbParams.append(pre + \"?\");\r\n";
658 result += "alParamsValues.add(model.get" + UnderscoreCamelUtil.ToPascalCase(ci.Name.ToString()) + "());\r\n";659 result += "pre = \",\";\r\n";660 result += "}\r\n";661 pre = ",";662 }663 }664
665 foreach(ColumnInfo ci incolumnInfoList)666 {667 if(ci.IsPrimaryKey)668 {669 where += "if(model.get" + UnderscoreCamelUtil.ToPascalCase(ci.Name.ToString()) + "() != null){\r\n";670 //where += " sbSql.append(pre + \"" + pre + ci.Name.ToString() + "=?\");\r\n";
671 where += "sbParams.append(preWhere + \"" + preWhere + ci.Name.ToString() + "=?\");\r\n";672 where += "alParamsValues.add(model.get" + UnderscoreCamelUtil.ToPascalCase(ci.Name.ToString()) + "());\r\n";673 where += "preWhere = \",\";\r\n";674 where += "}\r\n";675 preWhere = ",";676 }677 else
678 {679 }680 }681
682
683 return result + where;684 }685
686 public static string GetDeleteFunctionSignature(AppSettings appSettings, ListcolumnInfoList)687 {688 string result = "";689 string pre = "";690
691 foreach(ColumnInfo ci incolumnInfoList)692 {693 if(ci.IsPrimaryKey)694 {695 result += pre + GetJavaTypeFromSqlType(ci) + " " +UnderscoreCamelUtil.ToCamelCase(ci.Name.ToString());696 pre = ",";697 }698 }699
700 returnresult;701 }702
703 public static string GetDeleteFields(AppSettings appSettings, ListcolumnInfoList)704 {705 string result = "";706 string pre = "";707
708 foreach(ColumnInfo ci incolumnInfoList)709 {710 if(ci.IsPrimaryKey)711 {712 result += pre + ci.Name.ToString() + "=?";713 pre = ",";714 }715 }716
717 returnresult;718 }719
720
721 public static string GetDeleteParams(AppSettings appSettings, ListcolumnInfoList)722 {723 string result = "";724 string pre = "";725
726 foreach(ColumnInfo ci incolumnInfoList)727 {728 if(ci.IsPrimaryKey)729 {730 result += pre +UnderscoreCamelUtil.ToCamelCase(ci.Name.ToString());731 pre = ",";732 }733 }734
735 returnresult;736 }737
738 //preparedStatement.setString(1, ls@UnderscoreCamelUtil.ToPascalCase(@AppSetting.ClassPrefix).get(i).getId());
739 public static string GetDeleteFieldsValue(AppSettings appSettings, ListcolumnInfoList)740 {741 string result = "";742 string pre = "";743
744 foreach(ColumnInfo ci incolumnInfoList)745 {746 if(ci.IsPrimaryKey)747 {748 //result += pre + ci.Name.ToString() + "=?";
749 result += pre + "preparedStatement.set" + GetJavaTypeFromSqlType(ci) + "(1, ls" + UnderscoreCamelUtil.ToPascalCase(appSettings.ClassPrefix) + ".get(i).get" + UnderscoreCamelUtil.ToPascalCase(ci.Name.ToString()) + "());";750 pre = ",";751 }752 }753
754 returnresult;755 }756
757 public static string GetImports(ListlistColumnInfo)758 {759 string result = "";760 bool hasBigDecimal = false;761 bool hasDate = false;762 foreach(ColumnInfo ci inlistColumnInfo)763 {764 if("BigDecimal" ==GetJavaTypeFromSqlType(ci))765 {766 hasBigDecimal = true;767 }768 if("Date" ==GetJavaTypeFromSqlType(ci))769 {770 hasDate = true;771 }772 }773
774 if(hasBigDecimal)775 {776 result += "import java.math.BigDecimal;\r\n";777 }778 if(hasDate)779 {780 result += "import java.util.Date;";781 }782
783 returnresult;784 }785
786 public static string HasAUTO_GENERATED_KEYS(ListlistColumnInfo)787 {788 string result = "NO";789
790 foreach(ColumnInfo ci inlistColumnInfo)791 {792 if(ci.IsAutoNumber)793 {794 result = "YES";795 }796 }797
798 returnresult;799 }800
801 //更通用的函数
802 public static stringGetJavaTypeFromSqlType(ColumnInfo ci)803 {804 string result =TypeConverterForJava.SqlType2JavaDeclare(ci.DataType, ci.Precision, ci.Scale);805 returnresult;806 }807
808
809 public static stringGet_GetJavaTypeFromSqlType(ColumnInfo ci)810 {811 string result =TypeConverterForJava.SqlType2JavaDeclare(ci.DataType, ci.Precision, ci.Scale);812 if(result == "Integer")813 {814 return "Int";815 }816 returnresult;817 }818 }