package util.sqlparse.visitor.dm.test;

import java.util.ArrayList;
import java.util.List;

public class Sql {
   public String with = "";
   public String table = "";
   public String join = "";
   public String[] columns;
   public String[] where;
   public String[] groupBy;
   public String[] having;
   public String[] orderBy;
   public String[] forUpdateTable;

   public static List<Sql> sqls() {
      List<Sql> sqls = new ArrayList();
      sqls.add(query_exp_with());
      return sqls;
   }

   static Sql query_exp_with() {
      Sql a = new Sql();
      a.table = "sys_user a";
      a.columns = new String[]{"user_name,dept_id", "a.user_name as uname,dept_id", "user_name,a.dept_id", "a.user_name,a.dept_id", "*", "a.*"};
      a.where = new String[]{"", "user_name='1'", "a.user_name='1'", "user_name='1' and dept_id=1", "user_name='1' and a.dept_id=1", "user_name='1' and a.dept_id=1"};
      a.groupBy = new String[]{"", "user_name,dept_id", "a.user_name,a.dept_id", "user_name,a.dept_id", "a.user_name,a.dept_id", "", ""};
      a.having = new String[]{"", "count(*)>1", "count(user_name)>1", "", "", ""};
      a.orderBy = new String[]{"user_name", "uname asc", "1 desc", "", "", ""};
      a.forUpdateTable = new String[]{"", "", "", "sys_user", "", "sys_user"};
      return a;
   }

   public static List<String> getSqls() {
      List<String> rets = new ArrayList();

      for(Sql a : sqls()) {
         StringBuilder sb = new StringBuilder();
         sb.append(a.with).append(" SELECT ");
         List<StringBuilder> ex = new ArrayList();
         ex.add(sb);
         ex = append(ex, new String[]{"", "ALL", "DISTINCT", "UNIQUE"});
         List<StringBuilder> ex1 = append(ex, new String[]{""});
         List<StringBuilder> ex2 = append(ex, new String[]{"TOP 1"});
         ex2 = append(ex2, new String[]{"", "PERCENT"});
         ex2 = append(ex2, new String[]{"", "WITH TIES"});
         ex.clear();
         ex.addAll(ex1);
         ex.addAll(ex2);
         int size = a.columns.length;

         for(StringBuilder x : ex) {
            for(int i = 0; i < size; ++i) {
               StringBuilder s = new StringBuilder(x);
               s.append(a.columns[i]).append(" FROM ");
               s.append(a.table).append(" ");
               String where = a.where[i];
               if (where.length() > 0) {
                  s.append(" WHERE ").append(where);
               }

               String group = a.groupBy[i];
               if (group.length() > 0) {
                  s.append(" GROUP BY ").append(group);
               }

               String having = a.having[i];
               if (having.length() > 0) {
                  s.append(" HAVING ").append(having);
               }

               String orderBy = a.orderBy[i];
               if (orderBy.length() > 0) {
                  s.append(" ORDER BY ").append(orderBy);
               }

               rets.add(s.toString());
            }
         }
      }

      return rets;
   }

   static List<StringBuilder> append(List<StringBuilder> sqls, String[] ids) {
      List<StringBuilder> rets = new ArrayList();

      for(StringBuilder sql : sqls) {
         for(String id : ids) {
            StringBuilder sb = new StringBuilder(sql);
            sb.append(" ").append(id).append(" ");
            rets.add(sb);
         }
      }

      return rets;
   }
}
