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

org.docshare.orm.access.AccessDelegate 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.access;

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;
import org.docshare.util.FileTool;
import org.docshare.util.TextTool;

public class AccessDelegate implements IDBDelegate {
	public Map c_to_remarks;
	@Override
	public ResultSet resultById(String tname,String column,Object id) throws SQLException {
		ResultSet rs = DBHelper.getIns().getPrepareRS(String.format("select * from [%s] where [%s] = ? limit 0,1",tname,column),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(); //参数列表
		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 || v.toString().length() == 0){
					continue;
				}
				if(!first){
					ks.append(',');
					vs2.append(',');
				}
				
				ks.append('[').append(k).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());
		}else{
			ArrayList sa=new ArrayList();
			
			for(String k: m.changeColumns()){
				if(k != null && k.equals(key))continue;
				Object v = m.get(k);
				if(v == null || v.toString().length() == 0){
					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("id")) );
			if(m.changeColumns().size() == 0){
				Log.i("no change data for update "+sql);
				return 0;
			}
		}
		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);
		Log.d("DBTool run sql: "+sql +" ,param  = "+id);
		return DBHelper.getIns().update(sql,id);
	}

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

	@Override
	public long size(List cons, DBTool tool, String tbName) {
		ResultSet rs=null;
		try {
			rs = runSQL(cons,null,null, tool, tbName,"count(*) as CT");
			if(rs.next()){
				long id = rs.getLong("CT");
				rs.close();
				return id;
			}
		} catch (SQLException e) {
			throw new MVCException("get size error", e);
		}finally {
			FileTool.safelyClose(rs);
		}
		return 0;
	}
	public ResultSet runSQL(List cons,SQLConstains orderc,SQLConstains limitc,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;
		for(SQLConstains c: cons){
			if(c.type cons,DBTool tool){
//		return buildSQL(cons, tool,false,null);
//	}
//	public String buildSQL(List cons,DBTool tool,String sqlfrom){
//		return buildSQL(cons, tool,true,sqlfrom);
//	}
	
	@Deprecated
//	public String buildSQL(List cons,DBTool tool,boolean withLimit,String sqlfrom){
//		ArrayList sa = new ArrayList();
//		final String[] fh = {"","=",">","<",">=","<=","<>"};
//		SQLConstains limitc=null;
//		SQLConstains orderc=null;
//		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();
	}

}