当前位置:网站首页>Join table query select generation
Join table query select generation
2022-07-01 00:40:00 【Woo, Allen】
Even the table query select Generate
result
select
plan.*,
time_slot.*,
program.*
from plan, plan_time_slot,
time_slot, time_slot_program,
program
where plan.id = plan_time_slot.plan_id
and plan_time_slot. time_slot_id = time_slot.id
and time_slot.id = time_slot_program.time_slot_id
and time_slot_program. program_id = program.id ;
package com.gm.wj.util;
import com.gm.wj.entity.Notice;
import lombok.Data;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
@Data
public class CodeGen {
// public static void main(String[] args) {
List<String> tableNames=new ArrayList<>();
List<String> tableNames= Arrays.asList("plan","time_slot","program");
int frontIdx=0;
int linkIdx=1;
int endIdx=2;
String whereCode=" where ";
for (int i = 0; i < tableNames.size(); i++) {
String frontTable = tableNames.get(frontIdx);
String linkTable = tableNames.get(linkIdx);
String backTable = tableNames.get(endIdx);
String linkTableName=frontTable+"_"+backTable;
whereCode+="#frontTable#.id = #linkTableName#.#frontTable#_id ";
whereCode+="#linkTableName#. #endTable#_id = #endTable#.id ";
}
// List<String> tableNames= Arrays.asList("plan","time_slot","program");
// String whereCode = makeWhereCode(tableNames);
// String selectCode = makeSelectCode(tableNames);
// String fromCode = makeFromCode(tableNames);
// System.out.println(selectCode+fromCode+whereCode);
// }
static String makeFromCode( List<String> tableNames){
// List<String> tableNames= Arrays.asList("plan","time_slot","program");
int frontIdx=0;
// int linkIdx=1;
int backIdx=1;
// int endIdx=2;
// String whereCode=" where ";
// 0 1 2
StringBuilder whereCode= new StringBuilder(" from ");
String firstTable = tableNames.get(0);
whereCode.append(firstTable).append(", ");
for (int i = 0; i < tableNames.size(); i++) {
String frontTable = tableNames.get(frontIdx);
// String linkTable = tableNames.get(backIdx);
String backTable = tableNames.get(backIdx);
// String backTable = tableNames.get(endIdx);
String linkTableName=frontTable+"_"+backTable;
// whereCode.append(" #frontTable#,\n#linkTableName#,\n#backTable#,"
whereCode.append(" #linkTableName#,\n#backTable#,"
.replace("#frontTable#", frontTable)
.replace("#backTable#", backTable)
.replace("#linkTableName#", linkTableName))
;
frontIdx++;
backIdx++;
if(backIdx>=tableNames.size()){
break;
}
}
// whereCode.rela
whereCode = new StringBuilder(whereCode.substring(0, whereCode.length() - 1));
whereCode.append("\n");
// whereCode= whereCode.replaceFirst("and","where");
// System.out.println(whereCode);
return whereCode.toString();
}
static String makeSelectCode( List<String> tableNames){
// List<String> tableNames= Arrays.asList("plan","time_slot","program");
int frontIdx=0;
// int linkIdx=1;
int backIdx=1;
// int endIdx=2;
// String whereCode=" where ";
StringBuilder whereCode= new StringBuilder(" select \n");
String firstTable = tableNames.get(0);
whereCode.append(firstTable+".*").append(",\n ");
List<String>codes=new ArrayList<>();
for (int i = 0; i < tableNames.size(); i++) {
String frontTable = tableNames.get(frontIdx);
// String linkTable = tableNames.get(backIdx);
String backTable = tableNames.get(backIdx);
// String backTable = tableNames.get(endIdx);
String linkTableName=frontTable+"_"+backTable;
// whereCode.append(" #frontTable#.*, \n #backTable#.*,"
codes.add(backTable+".*");
// whereCode.append(" #backTable#.*,"
// .replace("#frontTable#", frontTable)
// .replace("#backTable#", backTable)
// .replace("#linkTableName#", linkTableName))
// ;
frontIdx++;
backIdx++;
if(backIdx>=tableNames.size()){
break;
}
}
// System.out.println(codes);
String tables = String.join(",\n", codes);
whereCode.append(tables);
// whereCode = new StringBuilder(whereCode.substring(0, whereCode.length() - 1));
whereCode.append("\n");
// System.out.println(whereCode);
return whereCode.toString();
}
static String makeWhereCode( List<String> tableNames){
// List<String> tableNames= Arrays.asList("plan","time_slot","program");
int frontIdx=0;
// int linkIdx=1;
int backIdx=1;
// int endIdx=2;
// String whereCode=" where ";
StringBuilder whereCode= new StringBuilder(" ");
for (int i = 0; i < tableNames.size(); i++) {
String frontTable = tableNames.get(frontIdx);
// String linkTable = tableNames.get(backIdx);
String backTable = tableNames.get(backIdx);
// String backTable = tableNames.get(endIdx);
String linkTableName=frontTable+"_"+backTable;
whereCode.append(" \n and #frontTable#.id = #linkTableName#.#frontTable#_id \n and #linkTableName#. #backTable#_id = #backTable#.id "
.replace("#frontTable#", frontTable)
.replace("#backTable#", backTable)
.replace("#linkTableName#", linkTableName))
;
frontIdx++;
backIdx++;
if(backIdx>=tableNames.size()){
break;
}
}
whereCode = new StringBuilder(whereCode.toString().replaceFirst("and", "where"));
// System.out.println(whereCode);
return whereCode.toString();
}
public static void main(String[] args) {
// Notice notice = new Notice();
// String gen = gen(notice);
// System.out.println(gen);
// CodeGen codeGen = new CodeGen();
// codeGen.genAxios(notice);
codeGen.axiosGetCode.so
// System.out.println(codeGen.axiosPostCode);
List<String> tableNames= Arrays.asList("plan","time_slot","program");
// String whereCode = makeWhereCode(tableNames);
// String selectCode = makeSelectCode(tableNames);
// String fromCode = makeFromCode(tableNames);
// System.out.println(selectCode+fromCode+whereCode);
String linkedTableQuery = genLinkedTableQuery(tableNames);
System.out.println(linkedTableQuery);
}
static String genLinkedTableQuery( List<String> tableNames){
// List<String> tableNames= Arrays.asList("plan","time_slot","program");
String whereCode = makeWhereCode(tableNames);
String selectCode = makeSelectCode(tableNames);
String fromCode = makeFromCode(tableNames);
// System.out.println(selectCode+fromCode+whereCode);
return selectCode+fromCode+whereCode+";";
}
String axiosGetCode;
String axiosPostCode;
public void genAxios(Object object) {
String tableName = getTableName(object);
String jsonAllFiled = ObjectToJson.Object2JsonAllFiled(object);
String axiosGetCodeTpl = "axios.get( common.baseURL + \"/#tableName#\").then((resp) => {\n" +
" if (resp && resp.code === 200) {\n" +
" let data=resp.data.data\n" +
" this.#tableName#=data\n"+
" }\n" +
" });";
String axiosPostCodeTpl = "axios.post( common.baseURL + \"/#tableName#\",#dataForm#).then((resp) => {\n" +
" if (resp && resp.code === 200) {\n" +
" let data=resp.data.data\n" +
" this.#tableName#=data\n"+
" }\n" +
" });";
axiosGetCode = axiosGetCodeTpl.replace("#tableName#", tableName);
axiosPostCode = axiosPostCodeTpl.replace("#tableName#", tableName)
.replace("#dataForm#", jsonAllFiled);
}
public static String gen(Object object) {
String tableName = getTableName(object);
String jsonAllFiled = ObjectToJson.Object2JsonAllFiled(object);
String axiosCode = "axios.get( common.baseURL + \"/#tableName#\").then((resp) => {\n" +
" if (resp && resp.code === 200) {\n" +
" let data=resp.data.data" +
" }\n" +
" });";
String axiosPostCode = "axios.post( common.baseURL + \"/#tableName#\",#dataForm#).then((resp) => {\n" +
" if (resp && resp.code === 200) {\n" +
" let data=resp.data.data" +
" }\n" +
" });";
String replace = axiosCode.replace("#tableName#", tableName);
String replace1 = axiosPostCode.replace("#tableName#", tableName)
.replace("#dataForm#", jsonAllFiled);
return replace;
}
// public static <T> String getTableName(T obj) {
// String tableName = obj.getClass().getSimpleName();
// tableName = StringUtils.underscoreNameLower(tableName);
// return tableName;
// }
public static String getTableName(Object obj) {
String tableName = obj.getClass().getSimpleName();
tableName = StringUtils.underscoreNameLower(tableName);
return tableName;
}
}
边栏推荐
- Using Excel to quickly generate SQL statements
- What should I do without 50W bride price
- SAP ui5 beginner tutorial 19 - SAP ui5 data types and complex data binding
- What SQL statements are supported for data filtering
- NE555波形发生器手把手教程之NE555内部结构(一)
- Kubernetes ---- pod configuration container start command
- 2022-2028 global encrypted external hard disk industry research and trend analysis report
- IBL预计算的疑问终于解开了
- 2022-2028 global 3D printing ASA consumables industry research and trend analysis report
- C # generates PPK files in putty format (supports passphrase)
猜你喜欢

20220216 misc buuctf another world WinHex, ASCII conversion flag zip file extraction and repair if you give me three days of brightness zip to rar, Morse code waveform conversion mysterious tornado br

2022-2028 global plant peptone industry research and trend analysis report

When is it appropriate to replace a virtual machine with a virtual machine?

2022-2028 global retro glass industry research and trend analysis report

Cmu15445 (fall 2019) project 1 - buffer pool details

20220215 CTF misc buuctf Xiaoming's safe binwalk analysis DD command separate rar file archpr brute force password cracking

Redis - sentinel mode

Quick start of wechat applet -- project introduction

Wechat official account development (1) introduction to wechat official account

MySQL variables, stored procedures and functions
随机推荐
Makefile notes (Yiwen Institute makefile)
CMU15445 (Fall 2019) 之 Project#1 - Buffer Pool 详解
Can JDBC based on openjdk connect to MySQL?
Line number of Jenkins pipeline script execution exception
leetcode 474. Ones and zeroes (medium)
2022-2028 global rampant travel industry research and trend analysis report
Unit test concept and purpose
Rust controls Dajiang programmable UAV Tello
运动与健康
Quick start of wechat applet -- project introduction
ABAQUS 2022 latest edition - perfect realistic simulation solution
20220215 misc buctf easycap Wireshark tracks TCP flow hidden key (use of WinHex tool)
The programmer's girlfriend gave me a fatigue driving test
Inventory the six second level capabilities of Huawei cloud gaussdb (for redis)
Basic data structure of redis
Oracle临时表详解
Design e-commerce seckill system
Interface documentation system - Yapi
IBL预计算的疑问终于解开了
2022-2028 global 3D printing ASA consumables industry research and trend analysis report