zoukankan      html  css  js  c++  java
  • DaoImpl.java

      1 using System;
      2 using System.Collections.Generic;
      3 using System.Linq;
      4 using System.Text;
      5 using MyTech.CodeGenerator.Utils;
      6 
      7 class DaoImpl
      8 {
      9     public static string execute(AppSettings appSettings, List<ColumnInfo> listColumnInfo)
     10     {
     11         string codeStr = "";
     12         codeStr += classHeader(appSettings, listColumnInfo) + "
    ";
     13         codeStr += "
    ";
     14         codeStr += xxxRowMapper(appSettings, listColumnInfo) + "
    ";
     15         codeStr += "
    ";
     16         codeStr += selectByPk(appSettings, listColumnInfo) + "
    ";
     17         codeStr += "
    ";
     18         codeStr += selectAll(appSettings, listColumnInfo) + "
    ";
     19         codeStr += "
    ";
     20         codeStr += selectList(appSettings, listColumnInfo) + "
    ";
     21         codeStr += "
    ";
     22         codeStr += count(appSettings, listColumnInfo) + "
    ";
     23         codeStr += "
    ";
     24         codeStr += selectPaging(appSettings, listColumnInfo) + "
    ";
     25         codeStr += "
    ";
     26         codeStr += insert(appSettings, listColumnInfo) + "
    ";
     27         codeStr += "
    ";
     28         codeStr += insertSelective(appSettings, listColumnInfo) + "
    ";
     29         codeStr += "
    ";
     30         codeStr += update(appSettings, listColumnInfo) + "
    ";
     31         codeStr += "
    ";
     32         codeStr += updateSelective(appSettings, listColumnInfo) + "
    ";
     33         codeStr += "
    ";
     34         codeStr += delete(appSettings, listColumnInfo) + "
    ";
     35         codeStr += "
    ";
     36         codeStr += delete__batch(appSettings, listColumnInfo) + "
    ";
     37 
     38         codeStr += "}";
     39         
     40         //Console.WriteLine("Hello World, from internal!" + listColumnInfo.Count);
     41         //for(int i=0; i<listColumnInfo.Count; i++)
     42         //{
     43         //    codeStr += TypeConverterForJava.SqlType2JavaDeclare(listColumnInfo[i].DataType) + " " + listColumnInfo[i].Name + ";
    ";
     44         //    
     45         //}
     46         
     47         
     48         
     49         return codeStr;
     50     }
     51     
     52     public static string classHeader(AppSettings appSettings, List<ColumnInfo> listColumnInfo)
     53     {
     54         StringBuilder sb = new StringBuilder();
     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         return sb.ToString();
     90     }
     91     
     92     public static string xxxRowMapper(AppSettings appSettings, List<ColumnInfo> listColumnInfo)
     93     {
     94         StringBuilder sb = new StringBuilder();
     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         return sb.ToString();
    108     }
    109     
    110     public static string selectByPk(AppSettings appSettings, List<ColumnInfo> listColumnInfo)
    111     {
    112         StringBuilder sb = new StringBuilder();
    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<" + GetClassName(appSettings, listColumnInfo) + "> 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         return sb.ToString();
    131     }
    132     
    133     public static string selectAll(AppSettings appSettings, List<ColumnInfo> listColumnInfo)
    134     {
    135         StringBuilder sb = new StringBuilder();
    136       
    137         sb.AppendLine("    @Override");
    138         sb.AppendLine("    public List<" + UnderscoreCamelUtil.ToPascalCase(appSettings.ClassPrefix) + "> 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         return sb.ToString();
    147     }
    148         
    149     public static string selectList(AppSettings appSettings, List<ColumnInfo> listColumnInfo)
    150     {
    151         StringBuilder sb = new StringBuilder();
    152       
    153         sb.AppendLine("    public List<" + UnderscoreCamelUtil.ToPascalCase(appSettings.ClassPrefix) + "> 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         return sb.ToString();
    163     }
    164         
    165     public static string count(AppSettings appSettings, List<ColumnInfo> listColumnInfo)
    166     {
    167         StringBuilder sb = new StringBuilder();
    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         return sb.ToString();
    179     }
    180         
    181     public static string selectPaging(AppSettings appSettings, List<ColumnInfo> listColumnInfo)
    182     {
    183         StringBuilder sb = new StringBuilder();
    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<" + UnderscoreCamelUtil.ToPascalCase(appSettings.ClassPrefix) + "> 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         return sb.ToString();
    202     }
    203             
    204     public static string insert(AppSettings appSettings, List<ColumnInfo> listColumnInfo)
    205     {
    206         StringBuilder sb = new StringBuilder();
    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         return sb.ToString();
    253     }
    254     
    255 
    256 
    257     public static string insertSelective(AppSettings appSettings, List<ColumnInfo> listColumnInfo)
    258     {
    259         StringBuilder sb = new StringBuilder();
    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<Object> alParamsValues = new ArrayList<Object>();");
    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         return sb.ToString();
    287     }
    288     
    289     public static string update(AppSettings appSettings, List<ColumnInfo> listColumnInfo)
    290     {
    291         StringBuilder sb = new StringBuilder();
    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         return sb.ToString();
    309     }
    310     public static string updateSelective(AppSettings appSettings, List<ColumnInfo> listColumnInfo)
    311     {
    312         StringBuilder sb = new StringBuilder();
    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<Object> alParamsValues = new ArrayList<Object>();");
    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         return sb.ToString();
    342     }
    343     public static string delete(AppSettings appSettings, List<ColumnInfo> listColumnInfo)
    344     {
    345         StringBuilder sb = new StringBuilder();
    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         return sb.ToString();
    360     }
    361 
    362     public static string delete__batch(AppSettings appSettings, List<ColumnInfo> listColumnInfo)
    363     {
    364         StringBuilder sb = new StringBuilder();
    365       
    366         
    367         sb.AppendLine("    @Override");
    368         sb.AppendLine("    public Integer delete(List<" + UnderscoreCamelUtil.ToPascalCase(appSettings.ClassPrefix) + "> 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         return sb.ToString();
    389     }
    390     
    391     //以下为公共函数
    392     public static string GetClassName(AppSettings appSettings, List<ColumnInfo> listColumnInfo){
    393         return UnderscoreCamelUtil.ToPascalCase(appSettings.ClassPrefix);
    394     }
    395     
    396     public static string GetSelectProcessRow(AppSettings appSettings, List<ColumnInfo> listColumnInfo)
    397     {
    398         string result = "";
    399         string pre = "";
    400         
    401         foreach(ColumnInfo ci in listColumnInfo)
    402         {
    403             result += pre + "model.set" + UnderscoreCamelUtil.ToPascalCase(ci.Name.ToString())
    404                         + "(resultSet.get" + Get_GetJavaTypeFromSqlType(ci) + "("" + ci.Name.ToString() + ""))";
    405             pre = ";
                ";
    406         }
    407         
    408         return result;
    409     }
    410     
    411     public static string GetSelectByPkFunctionSignature(AppSettings appSettings, List<ColumnInfo> listColumnInfo)
    412     {
    413         string result = "";
    414         string pre = "";
    415         
    416         foreach(ColumnInfo ci in listColumnInfo)
    417         {
    418             if(ci.IsPrimaryKey)
    419             {
    420                 result += pre + GetJavaTypeFromSqlType(ci) + " " + UnderscoreCamelUtil.ToCamelCase(ci.Name.ToString());
    421                 pre = ", ";
    422             }
    423         }
    424         
    425         return result;
    426     }
    427     
    428     public static string GetSelectFields(AppSettings appSettings, List<ColumnInfo> listColumnInfo)
    429     {
    430         string result = "";
    431         string where = "";
    432         string pre = "";
    433         string preWhere = "";
    434         string tableName = "";
    435         
    436         foreach(ColumnInfo ci in listColumnInfo)
    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         return result;
    454     }
    455     
    456     public static string GetSelectByPkWhere(AppSettings appSettings, List<ColumnInfo> listColumnInfo)
    457     {
    458         string where = "";
    459         string pre = "";
    460         string preWhere = "";
    461         string tableName = "";
    462         
    463         foreach(ColumnInfo ci in listColumnInfo)
    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, List<ColumnInfo> listColumnInfo)
    479     {
    480         string result = "";
    481         string pre = "";
    482         
    483         foreach(ColumnInfo ci in listColumnInfo)
    484         {
    485             if(ci.IsPrimaryKey)
    486             {
    487                 result += pre + UnderscoreCamelUtil.ToCamelCase(ci.Name.ToString());
    488                 pre = ", ";
    489             }
    490         }
    491         
    492         return result;
    493     }
    494     
    495     public static string GetInsertFields(AppSettings appSettings, List<ColumnInfo> listColumnInfo)
    496     {
    497         string result = "";
    498         string values = "";
    499         string pre = "";
    500         
    501         foreach(ColumnInfo ci in listColumnInfo)
    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         return result;
    517     }
    518     
    519     public static string GetInsertParams(AppSettings appSettings, List<ColumnInfo> listColumnInfo)
    520     {
    521         string result = "";
    522         string pre = "";
    523         
    524         foreach(ColumnInfo ci in listColumnInfo)
    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         return result;
    537     }
    538     
    539     public static string GetInsertParamsReturnPk(AppSettings appSettings, List<ColumnInfo> listColumnInfo)
    540     {
    541         string result = "";
    542         string pre = "";
    543 
    544         int i = 1;
    545         foreach(ColumnInfo ci in listColumnInfo)
    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 = "
                  ";
    555             }
    556         }
    557         
    558         return result;
    559     }
    560 
    561 
    562     public static string GetInsertSelectiveFields(AppSettings appSettings, List<ColumnInfo> columnInfoList)
    563     {
    564         string result = "
    ";
    565         string values = "";
    566         string pre = "";
    567         
    568         foreach(ColumnInfo ci in columnInfoList)
    569         {
    570             if(ci.IsAutoNumber)
    571             {
    572             }
    573             else
    574             {
    575                 result += "        if(model.get" + UnderscoreCamelUtil.ToPascalCase(ci.Name.ToString()) + "() != null){
    ";
    576                 result += "            sbSql.append(pre + "" + ci.Name.ToString() + "");
    ";
    577                 result += "            sbParams.append(pre + "?");
    ";
    578                 result += "            alParamsValues.add(model.get" + UnderscoreCamelUtil.ToPascalCase(ci.Name.ToString()) + "());
    ";
    579                 result += "            pre = ",";
    ";
    580                 result += "        }
    ";
    581                 pre = ", ";
    582             }
    583         }
    584         
    585         return result;
    586     }
    587     
    588 
    589     public static string GetUpdateFields(AppSettings appSettings, List<ColumnInfo> columnInfoList)
    590     {
    591         string result = "";
    592         string where = "";
    593         string pre = "";
    594         string preWhere = "";
    595         
    596         foreach(ColumnInfo ci in columnInfoList)
    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         return result;
    613     }
    614 
    615 
    616 public static string GetUpdateParams(AppSettings appSettings, List<ColumnInfo> columnInfoList)
    617     {
    618         string result = "";
    619         string where = "";
    620         string pre = "";
    621         string preWhere = "";
    622         
    623         foreach(ColumnInfo ci in columnInfoList)
    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         return result;
    638     }
    639     
    640     public static string GetUpdateSelectiveFields(AppSettings appSettings, List<ColumnInfo> columnInfoList)
    641     {
    642         string result = "
    ";
    643         string where = "";
    644         string values = "";
    645         string pre = "";
    646         string preWhere = "";
    647         
    648         foreach(ColumnInfo ci in columnInfoList)
    649         {
    650             if(ci.IsPrimaryKey)
    651             {
    652             }
    653             else
    654             {
    655                 result += "        if(model.get" + UnderscoreCamelUtil.ToPascalCase(ci.Name.ToString()) + "() != null){
    ";
    656                 result += "            sbSql.append(pre + "" + ci.Name.ToString() + "=?");
    ";
    657                 //result += "            sbParams.append(pre + "?");
    ";
    658                 result += "            alParamsValues.add(model.get" + UnderscoreCamelUtil.ToPascalCase(ci.Name.ToString()) + "());
    ";
    659                 result += "            pre = ",";
    ";
    660                 result += "        }
    ";
    661                 pre = ", ";
    662             }
    663         }
    664         
    665         foreach(ColumnInfo ci in columnInfoList)
    666         {
    667             if(ci.IsPrimaryKey)
    668             {
    669                 where += "        if(model.get" + UnderscoreCamelUtil.ToPascalCase(ci.Name.ToString()) + "() != null){
    ";
    670                 //where += "            sbSql.append(pre + "" + pre + ci.Name.ToString() + "=?");
    ";
    671                 where += "            sbParams.append(preWhere + "" + preWhere + ci.Name.ToString() + "=?");
    ";
    672                 where += "            alParamsValues.add(model.get" + UnderscoreCamelUtil.ToPascalCase(ci.Name.ToString()) + "());
    ";
    673                 where += "            preWhere = ",";
    ";
    674                 where += "        }
    ";
    675                 preWhere = ", ";
    676             }
    677             else
    678             {
    679             }
    680         }
    681 
    682         
    683         return result + where;
    684     }
    685     
    686     public static string GetDeleteFunctionSignature(AppSettings appSettings, List<ColumnInfo> columnInfoList)
    687     {
    688         string result = "";
    689         string pre = "";
    690         
    691         foreach(ColumnInfo ci in columnInfoList)
    692         {
    693             if(ci.IsPrimaryKey)
    694             {
    695                 result += pre + GetJavaTypeFromSqlType(ci) + " " + UnderscoreCamelUtil.ToCamelCase(ci.Name.ToString());
    696                 pre = ", ";
    697             }
    698         }
    699         
    700         return result;
    701     }
    702     
    703     public static string GetDeleteFields(AppSettings appSettings, List<ColumnInfo> columnInfoList)
    704     {
    705         string result = "";
    706         string pre = "";
    707         
    708         foreach(ColumnInfo ci in columnInfoList)
    709         {
    710             if(ci.IsPrimaryKey)
    711             {
    712                 result += pre + ci.Name.ToString() + "=?";
    713                 pre = ", ";
    714             }
    715         }
    716         
    717         return result;
    718     }
    719 
    720 
    721     public static string GetDeleteParams(AppSettings appSettings, List<ColumnInfo> columnInfoList)
    722     {
    723         string result = "";
    724         string pre = "";
    725         
    726         foreach(ColumnInfo ci in columnInfoList)
    727         {
    728             if(ci.IsPrimaryKey)
    729             {
    730                 result += pre + UnderscoreCamelUtil.ToCamelCase(ci.Name.ToString());
    731                 pre = ", ";
    732             }
    733         }
    734         
    735         return result;
    736     }
    737     
    738     //preparedStatement.setString(1, ls@UnderscoreCamelUtil.ToPascalCase(@AppSetting.ClassPrefix).get(i).getId());
    739     public static string GetDeleteFieldsValue(AppSettings appSettings, List<ColumnInfo> columnInfoList)
    740     {
    741         string result = "";
    742         string pre = "";
    743         
    744         foreach(ColumnInfo ci in columnInfoList)
    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         return result;
    755     }
    756     
    757     public static string GetImports(List<ColumnInfo> listColumnInfo)
    758     {
    759         string result = "";
    760         bool hasBigDecimal = false;
    761         bool hasDate = false;
    762         foreach(ColumnInfo ci in listColumnInfo)
    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;
    ";
    777         }
    778         if(hasDate)
    779         {
    780             result += "import java.util.Date;";
    781         }
    782 
    783         return result;
    784     }
    785     
    786     public static string HasAUTO_GENERATED_KEYS(List<ColumnInfo> listColumnInfo)
    787     {
    788         string result = "NO";
    789         
    790         foreach(ColumnInfo ci in listColumnInfo)
    791         {
    792             if(ci.IsAutoNumber)
    793             {
    794                 result = "YES";
    795             }
    796         }
    797         
    798         return result;
    799     }
    800     
    801     //更通用的函数
    802     public static string GetJavaTypeFromSqlType(ColumnInfo ci)
    803     {
    804         string result = TypeConverterForJava.SqlType2JavaDeclare(ci.DataType, ci.Precision, ci.Scale);
    805         return result;
    806     }
    807     
    808         
    809     public static string Get_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         return result;
    817     }
    818 }
  • 相关阅读:
    用C#设置系统时间和本地时间
    在ASP.NET中使用Session常见问题集锦 收藏
    C#开源资源大汇总(2)
    漫谈ASP.NET设计中的性能优化问题
    比较著名的.net技术论坛名称(含国外的)
    在ASP.NET 2.0中,一个ASP.NET页面的生命周期
    DataGrid技巧大集合(转载)
    Silverlight经典教程书籍汇总
    Asp.Net细节性问题技巧精萃
    C#开源资源大汇总(1)
  • 原文地址:https://www.cnblogs.com/yasepix/p/12576605.html
Copyright © 2011-2022 走看看