/** * 根据解析结果设置数据源与真实表名称 * * @param sqlStruct * @param sqlInfo * @throws SQLException */ private void parsePartition(SQLStruct sqlStruct, SQLInfo sqlInfo) throws SQLException { DALCustomInfo dalCustomInfo = DALCurrentStatus.getCustomInfo(); DALFactory dalFactory = DALFactory.getDefault(); ParsedTableInfo parsedTableInfo = new ParsedTableInfo(); boolean hasParser = true; if (sqlStruct.isCanParse()) { if (dalCustomInfo == null) { ConnectionStatus connectionStatus = new ConnectionStatus(); connectionStatus.setAutoCommit(this.dalConnection.getAutoCommit()); connectionStatus.setReadOnly(this.dalConnection.isReadOnly()); PartitionParser parser; PartitionTableInfo partitionTableInfo = null; for (String table : sqlStruct.getTableNames()) { parser = dalFactory.getPartitionParserFactory().getParser(table); if (parser == null) { hasParser = false; } else { // 存在解析器时,进行解析 partitionTableInfo = parser.parse(table, sqlInfo, connectionStatus); if (partitionTableInfo == null) { throw new DALRunTimeException( "partitionTableInfo return from " + parser.getClass().getName() + " can not be null : " + table); } parsedTableInfo.setRealTable(table, partitionTableInfo.getRealTable()); } } // 如果不需要解析路由,就设置默认数据源 if (partitionTableInfo == null) { // DALCurrentStatus.setDefaultDsKey(); } // 设置解析后的数据源 else { DALCurrentStatus.setDsKey(partitionTableInfo.getDsName()); } } // 通过代码指定数据源与表名称,进行设定 else { for (String table : sqlStruct.getTableNames()) { parsedTableInfo.setRealTable(table, dalCustomInfo.getRealTable(table)); } DALCurrentStatus.setDsKey(dalCustomInfo.getDsKey()); } // 在有进行解析的条件下,获得解析后指定表的sql语句 if (hasParser) { this.sql = dalFactory.getSqlAnalyzer().outPutSQL(sql, sqlStruct, sqlInfo, parsedTableInfo); } } else { // 当不需要进行解析时,什么也不做,直接使用上一次使用的解析结果 } }
/** * 初始化真正的PreparedStatement,对当前对象的操作全部都设置到真正的PreparedStatement * * @throws SQLException */ private void prepare() throws SQLException { DALFactory dalFactory = DALFactory.getDefault(); List<Object> values = dalParameters.getValues(); Map<String, Object> context = new HashMap<String, Object>(); SQLStruct sqlStruct = dalFactory.getSqlAnalyzer().parse(sql, context); SQLInfo sqlInfo = null; if (sqlStruct.isCanParse()) { sqlInfo = dalFactory .getSqlAnalyzer() .analyse(sql, sqlStruct, values.toArray(new Object[values.size()]), context); } this.parsePartition(sqlStruct, sqlInfo); this.initRealPreparedStatement(); if (this.maxFieldSize != 0) { ps.setMaxFieldSize(maxFieldSize); } if (this.maxRows != 0) { ps.setMaxRows(maxRows); } if (!this.escapeProcessing) { ps.setEscapeProcessing(escapeProcessing); } if (this.queryTimeout != 0) { ps.setQueryTimeout(queryTimeout); } if (this.cursorName != null) { ps.setCursorName(cursorName); } if (this.fetchDirection != 0) { ps.setFetchDirection(fetchDirection); } if (this.fetchSize != 0) { ps.setFetchSize(fetchSize); } if (!this.poolable) { ps.setPoolable(poolable); } this.dalParameters.initRealPreparedStatement(ps); }