All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.docshare.orm.mysql.MySQLDelegate Maven / Gradle / Ivy

Go to download

An efficient, fast, convenient, easy to learn, easy to use MVC framework and ORM framework. It is seamless compatible with JSTL and supports FreeMarker. It can run independently, and it can also be applied to traditional Java Web projects. It is an efficient, fast, convenient, easy to learn and easy to use MVC framework and ORM framework. It is seamless compatible with JSTL and supports FreeMarker. It can be run on its own, or it can be applied to traditional Java Web projects

There is a newer version: 2023.06.19
Show newest version
package org.docshare.orm.mysql;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.docshare.log.Log;
import org.docshare.mvc.except.MVCException;
import org.docshare.orm.ArrayTool;
import org.docshare.orm.ColumnDesc;
import org.docshare.orm.DBHelper;
import org.docshare.orm.DBTool;
import org.docshare.orm.IDBDelegate;
import org.docshare.orm.Model;
import org.docshare.orm.SQLConstains;


public class MySQLDelegate implements IDBDelegate {
	public Map c_to_remarks;
	@Override
	public ResultSet resultById(String tname,String column,Object id) throws SQLException {
		//String.format("select * from `%s` where `%s` = ? limit 0,1",tname,column)
		StringBuilder sBuffer =new StringBuilder();
		sBuffer.append("select * from `");
		sBuffer.append(tname);
		sBuffer.append("` where `");
		sBuffer.append(column);
		sBuffer.append("` = ? limit 0,1");
		//"select * from `"+tname+"` where `"+column+"` = ? limit 0,1" 
		ResultSet rs = DBHelper.getIns().getPrepareRS(sBuffer.toString(),id);
		return rs;
	}

	@Override
	public int save(DBTool tool,Model m,String key,boolean forceInsert ){
		if(m == null){
			Log.e("can not save a null object");
			return 0;
		}
		Object id = m.get(key);
		String sql = "";
		
		ArrayList plist = new ArrayList(8); //参数列表
		if(forceInsert || m.isCreated || id == null || (id instanceof Integer && (Integer)id <= 0 ) ){
			//This is an insert
			StringBuilder ks=new StringBuilder();
			
			StringBuilder vs2= new StringBuilder();
			boolean first=true;
			for(String k: m.keySet()){
				if(k.equals(key)){ //这里不再跳过主键字段
					//continue;    //不跳过主键字段了
				}
				Object v = m.get(k);
				if(v == null ){
					continue;
				}
				if(!first){
					ks.append(',');
					vs2.append(',');
				}
				//  ks += "`"+k+"`"; 被优化
				ks.append('`');
				ks.append(k);
				ks.append('`');
				
				String type = tool.getColumnTypeName(k);
				ArrayTool.valueWrapper(null, v,type);
				vs2.append('?');
				plist.add(v);
				first = false;
			}
			//sql = String.format("insert into `%s`(%s) values(%s)", m.getTableName(),ks.toString(),vs2.toString());
			StringBuilder sqlb = new StringBuilder();
			sqlb.append("insert into `");
			sqlb.append(m.getTableName());
			sqlb.append("`(");
			sqlb.append(ks);
			sqlb.append(") values(");
			sqlb.append(vs2);
			sqlb.append(")");
			sql = sqlb.toString();
		}else{
			int csize = m.changeColumns().size();
			if(csize == 0){
				Log.i("no change data for update "+sql);
				return 0;
			}

			ArrayList sa=new ArrayList(csize);
			for(String k: m.changeColumns()){
				if(k == key)continue;
				Object v = m.get(k);
				if(v == null ){
					continue;
				}
				//String type = tool.getColumnTypeName(k);
				//ArrayTool.valueWrapper(k, m.get(k),type);
				//sa.add(s);
				sa.add("`"+k+"`=?");
				plist.add(m.get(k));
			}
			String ss = ArrayTool.join(",", sa);
			//sql=String.format("update `%s` set %s where %s", m.getTableName(),ss,ArrayTool.valueWrapper(key, id,tool.getColumnTypeName(key)) );
			StringBuilder sqlb = new StringBuilder();
			sqlb.append("update `");
			sqlb.append(m.getTableName());
			sqlb.append("` set ");
			sqlb.append(ss);
			sqlb.append(" where ");
			sqlb.append(ArrayTool.valueWrapper(key, id,tool.getColumnTypeName(key)));
			sql = sqlb.toString();
		}
		Log.d("DBTool run sql: "+sql+"  params=["+ArrayTool.joinWithLengthLimit(",", plist,20)+"]");
		Object[] objs = plist.toArray();
		DBHelper helper = DBHelper.getIns("mysql");
		
		int d = helper.updateWithArray(sql,objs);
		Log.d("return "+d);
		if(d != 0 &&(id == null|| forceInsert)){
			id = helper.getLastId();			
			m.put(key, id);
		}
		return d;//helper.getLastId();
	}
	
	@Override
	public int delete(String tname,String key,Object id){
		//String sql = String.format("delete from `%s` where `%s` = ?", tname,key);
		StringBuilder sBuffer =new StringBuilder();
		sBuffer.append("delete from `");
		sBuffer.append(tname).append("` where `").append(key).append("` = ?");
		String sql = sBuffer.toString();
		Log.d("DBTool run sql: "+ sql +" ,param  = "+id);
		return DBHelper.getIns().update(sql,id);
	}

//	public ResultSet runSQL(List cons,SQLConstains limit_con,SQLConstains order_con,DBTool tool,String tbName){
//		return runSQL(cons,limit_con,order_con, tool, tbName,"*");
//	}

	@Override
	public long size(List cons, DBTool tool, String tbName) {
		ResultSet rs;
		try {
			rs = runSQL(cons, null,null,tool, tbName,"count(*) as CT");
			if(rs!=null){
				if(rs.next()){
					long id = rs.getLong("CT");
					rs.close();
					return id;
				}
				rs.close();
			}else{
				
			}
		} catch (SQLException e) {
			//e.printStackTrace();
			throw new MVCException("size: get size of query error", e);
		}
		return 0;
	}
	@Override

	public ResultSet runSQL(List cons,SQLConstains order_con,SQLConstains limit_con,DBTool tool,String tbName,String prefix){

		StringBuilder sb=new StringBuilder();
		sb.append("select ").append(prefix).append(" from `").append(tbName).append("` ");
		final String[] fh = {"","=",">","<",">=","<=","<>"};
		ArrayList params = new ArrayList();
		if(cons.size()>0){
			sb.append("where ");
			boolean first= true;

			for(SQLConstains c: cons){
				if(!first){ 
					sb.append(" and ");
				}
				first = false;
				
				if(c.type cons,SQLConstains limit_con,SQLConstains order_con,DBTool tool,String tbName,String prefix){
//		if(tbName == null) return null ;//参数检查,表名不能为空
//		
//		ArrayList sa = new ArrayList();
//		ArrayList params = new ArrayList();
//		final String[] fh = {"","=",">","<",">=","<=","<>"};
//		SQLConstains limitc=null;
//		SQLConstains orderc=null;
//		String w;
//		StringBuilder sb=new StringBuilder();
//		for(SQLConstains c: cons){
//			if(c.type columnOfRs(String sql,ResultSet rs) {
		return DBHelper.getIns().columeOfRs(sql,rs);
	}

	@Override
	public Map listColumn(String tname,boolean useCache) {
		return DBHelper.getIns().listColumn(tname,useCache);
	}

	@Override
	public String keyColumn(String tname) {
		return DBHelper.getIns().keyColumn(tname);
	}

	@Override
	public void beginTransaction() {
		DBHelper.getIns().beginTransation();
	}

	@Override
	public void commit() {
		DBHelper.getIns().commit();
	}

	@Override
	public void rollback() {
		DBHelper.getIns().rollback();
	}



}