@@ -248,8 +248,8 @@ public SQLParsedResult parse(final Map<Object, Object> jdbcParams) {
248248 final SQLParsedResult result = new SQLParsedResult () {
249249
250250 @ Override
251- public void checkIfCrossPreparedStatement (Map <Object , Object > jdbcParam )
252- throws CrossPreparedStatementException {
251+ public void checkIfCrossPreparedStatement (final Map <Object , Object > jdbcParams )
252+ throws CrossPreparedStatementException {
253253 for (Map .Entry <TableWrapper , String > entry : convertedTables .entrySet ()) {
254254 TableWrapper tab = entry .getKey ();
255255 route1 (tab , jdbcParams , entry .getValue (), this .getSql ());
@@ -322,18 +322,11 @@ private ShardRouteInfo route1(TableWrapper tab, Map<Object, Object> jdbcParams,
322322 + sql + "]" );
323323 }
324324 routeInfo = getRouteInfo (tab , sdValue );
325- String next = routeInfo .toString ();
325+ String newRoutedFulltableName = routeInfo .toString ();
326326 if (routedFullTableName == null ) {
327- routedFullTableName = next ;
327+ routedFullTableName = newRoutedFulltableName ;
328328 } else {
329- if (!routedFullTableName .equals (next )) {
330- throw new AmbiguousRouteResultException ("In sql[" + sql + "], table:'"
331- + tab .getOriginalConfig ().toString ()
332- + "' has multiple routing results["
333- + routedFullTableName + "," + next
334- + "]. Jdbc parameter is "
335- + DDRJSONUtils .toJSONString (jdbcParams ));
336- }
329+ verifyRoutedFullTableName (tab , jdbcParams , routedFullTableName , routedSql , newRoutedFulltableName );
337330 }
338331 } else {// range
339332 RangeParam rangeParam = (RangeParam ) sqlParam ;
@@ -356,7 +349,10 @@ private ShardRouteInfo route1(TableWrapper tab, Map<Object, Object> jdbcParams,
356349 if (end .isJdbcParamType ()) {
357350 Number number = (Number ) jdbcParams .get (end .getValue ());
358351 if (number == null ) {
359-
352+ throw new IllegalSQLParameterException ("Jdbc parameter can't be null. Jdbc parameter key is "
353+ + end .getValue () + ", jdbc parameter is "
354+ + DDRJSONUtils .toJSONString (jdbcParams )
355+ + " and sql is [" + sql + "]" );
360356 }
361357 e0 = number .longValue ();
362358 } else {
@@ -368,32 +364,35 @@ private ShardRouteInfo route1(TableWrapper tab, Map<Object, Object> jdbcParams,
368364 if (routedFullTableName == null ) {
369365 routedFullTableName = next ;
370366 } else {
371- if (!routedFullTableName .equals (next )) {
372- if (routedSql != null ) {
373- throw new CrossPreparedStatementException ("Sql[" + sql + "] has been routed to ["
374- + routedSql + "] and table:'"
375- + tab .getOriginalConfig ().toString ()
376- + "' has been route to '" + routedFullTableName
377- + "'. But current jdbc parameter:"
378- + DDRJSONUtils .toJSONString (jdbcParams )
379- + " require route to " + next
380- + DDRJSONUtils .toJSONString (jdbcParams ));
381- } else {
382- throw new AmbiguousRouteResultException ("In sql[" + sql + "], table:'"
383- + tab .getOriginalConfig ().toString ()
384- + "' has multiple routing results["
385- + routedFullTableName + "," + next
386- + "]. Jdbc parameter is "
387- + DDRJSONUtils .toJSONString (jdbcParams ));
388- }
389- }
367+ verifyRoutedFullTableName (tab , jdbcParams , routedFullTableName , routedSql , next );
390368 }
391369
392370 }
393371 }
394372 return routeInfo ;
395373 }
396374
375+ private void verifyRoutedFullTableName (TableWrapper tab , Map <Object , Object > jdbcParams ,
376+ String routedFullTableName , String routedSql , String newRoutedFulltableName ) {
377+ if (!routedFullTableName .equals (newRoutedFulltableName )) {
378+ if (routedSql != null ) {
379+ throw new CrossPreparedStatementException ("Sql[" + sql + "] has been routed to [" + routedSql
380+ + "] and table:'" + tab .getOriginalConfig ().toString ()
381+ + "' has been route to '" + routedFullTableName
382+ + "'. But current jdbc parameter:"
383+ + DDRJSONUtils .toJSONString (jdbcParams )
384+ + " require route to " + newRoutedFulltableName
385+ + DDRJSONUtils .toJSONString (jdbcParams ));
386+ } else {
387+ throw new AmbiguousRouteResultException ("In sql[" + sql + "], table:'"
388+ + tab .getOriginalConfig ().toString ()
389+ + "' has multiple routing results[" + routedFullTableName + ","
390+ + newRoutedFulltableName + "]. Jdbc parameter is "
391+ + DDRJSONUtils .toJSONString (jdbcParams ));
392+ }
393+ }
394+ }
395+
397396 private void afterVisitBaseStatement () {
398397 FrameContext context = this .getStack ().pop ();
399398 for (Map .Entry <String , TableWrapper > entry : context .entrySet ()) {
@@ -523,6 +522,7 @@ private TableWrapper getTableFromContext(Column col) {
523522 */
524523 private void addRoutedTableIntoContext (TableWrapper table , ShardRouteConfig routeConfig , boolean appendAlias ) {
525524 FrameContext frameContext = this .getStack ().peek ();
525+ String scName = table .getSchemaName ();
526526 String tbName = table .getName ();
527527 String tbAliasName = tbName ;
528528 if (table .getAlias () != null && table .getAlias ().getName () != null ) {
@@ -533,18 +533,17 @@ private void addRoutedTableIntoContext(TableWrapper table, ShardRouteConfig rout
533533 }
534534 }
535535 String sdKey = DDRStringUtils .toLowerCase (routeConfig .getSdKey ());// sdKey可以为null,当为null时需要通过context路由
536- if (table . getSchemaName () != null ) {
536+ if (scName != null ) {
537537 StringBuilder sb = new StringBuilder ();
538- sb .append (DDRStringUtils .toLowerCase (table . getSchemaName () ));
538+ sb .append (DDRStringUtils .toLowerCase (scName ));
539539 sb .append ('.' );
540540 sb .append (DDRStringUtils .toLowerCase (tbAliasName ));
541541 sb .append ('.' );
542542 sb .append (sdKey );
543543 String key = sb .toString ();
544544 putIntoContext (frameContext , key , table );
545- putIntoContext (frameContext , key .substring (table .getSchemaName ().length () + 1 ), table );
546- putIntoContext (frameContext , key .substring (table .getSchemaName ().length () + 2 + tbAliasName .length ()),
547- table );
545+ putIntoContext (frameContext , key .substring (scName .length () + 1 ), table );
546+ putIntoContext (frameContext , key .substring (scName .length () + 2 + tbAliasName .length ()), table );
548547 } else {
549548 StringBuilder sb = new StringBuilder ();
550549 sb .append (DDRStringUtils .toLowerCase (tbAliasName ));
@@ -556,7 +555,7 @@ private void addRoutedTableIntoContext(TableWrapper table, ShardRouteConfig rout
556555 }
557556 }
558557
559- private Object getRouteValue (Column column , Expression obj ) {
558+ protected Object getRouteValue (Column column , Expression obj ) {
560559 if (obj == null ) {
561560 return null ;
562561 }
@@ -637,9 +636,7 @@ public void visit(Between between) {
637636 s1 = e1 ;
638637 e1 = temp ;
639638 }
640- for (long l = s1 ; l <= e1 ; l ++) {
641- routeTable (tab , column , l );
642- }
639+ routeTable (tab , column , new RangeShardValue (s1 , e1 ));
643640 }
644641 }
645642
0 commit comments