@@ -209,14 +209,14 @@ static Regex[] GetRegSqlWhereDateTimes(string columnName, string quoteParameterN
209209 cn = columnName . Replace ( "[" , "\\ [" ) . Replace ( "]" , "\\ ]" ) . Replace ( "." , "\\ ." ) ;
210210 return new [ ]
211211 {
212- new Regex ( $@ "({ cn } \s*(<|<=|>|>=|=|between)\s*)(datetime|cdate|to_date)\(('[^']+')\)", RegexOptions . IgnoreCase ) ,
213- new Regex ( $@ "({ cn } \s*(<|<=|>|>=|=|between)(\s*))to_timestamp\(('[^']+')\s*,\s*'YYYY-MM-DD[^']+'\)", RegexOptions . IgnoreCase ) ,
214- new Regex ( $@ "({ cn } \s*(<|<=|>|>=|=|between)(\s*))cast\(('[^']+') as (datetime|timestamp)\)", RegexOptions . IgnoreCase ) ,
215- new Regex ( $@ "({ cn } \s*(<|<=|>|>=|=|between)(\s*))('[^']+')::(datetime|timestamp)", RegexOptions . IgnoreCase ) ,
216- new Regex ( $@ "({ cn } \s*(between)\s+'[^']+'\s+and\s+)(datetime|cdate|to_date)\(('[^']+')\)", RegexOptions . IgnoreCase ) ,
217- new Regex ( $@ "({ cn } \s*(between)\s+'[^']+'\s+and(\s+))to_timestamp\(('[^']+')\s*,\s*'YYYY-MM-DD[^']+'\)", RegexOptions . IgnoreCase ) ,
218- new Regex ( $@ "({ cn } \s*(between)\s+'[^']+'\s+and(\s+))cast\(('[^']+') as (datetime|timestamp)\)", RegexOptions . IgnoreCase ) ,
219- new Regex ( $@ "({ cn } \s*(between)\s+'[^']+'\s+and(\s+))('[^']+')::(datetime|timestamp)", RegexOptions . IgnoreCase ) ,
212+ // new Regex($@"({cn}\s*(<|<=|>|>=|=|between)\s*)(datetime|cdate|to_date)\(('[^']+')\)", RegexOptions.IgnoreCase),
213+ // new Regex($@"({cn}\s*(<|<=|>|>=|=|between)(\s*))to_timestamp\(('[^']+')\s*,\s*'YYYY-MM-DD[^']+'\)", RegexOptions.IgnoreCase),
214+ // new Regex($@"({cn}\s*(<|<=|>|>=|=|between)(\s*))cast\(('[^']+') as (datetime|timestamp)\)", RegexOptions.IgnoreCase),
215+ // new Regex($@"({cn}\s*(<|<=|>|>=|=|between)(\s*))('[^']+')::(datetime|timestamp)", RegexOptions.IgnoreCase),
216+ // new Regex($@"({cn}\s*(between)\s+'[^']+'\s+and\s+)(datetime|cdate|to_date)\(('[^']+')\)", RegexOptions.IgnoreCase),
217+ // new Regex($@"({cn}\s*(between)\s+'[^']+'\s+and(\s+))to_timestamp\(('[^']+')\s*,\s*'YYYY-MM-DD[^']+'\)", RegexOptions.IgnoreCase),
218+ // new Regex($@"({cn}\s*(between)\s+'[^']+'\s+and(\s+))cast\(('[^']+') as (datetime|timestamp)\)", RegexOptions.IgnoreCase),
219+ // new Regex($@"({cn}\s*(between)\s+'[^']+'\s+and(\s+))('[^']+')::(datetime|timestamp)", RegexOptions.IgnoreCase),
220220
221221 new Regex ( $@ "({ cn } \s*(<|<=|>|>=|=)\s*)(datetime|cdate|to_date)\(({ quoteParameterName } [\w_]+)\)", RegexOptions . IgnoreCase ) ,
222222 new Regex ( $@ "({ cn } \s*(<|<=|>|>=|=)(\s*))to_timestamp\(({ quoteParameterName } [\w_]+)\s*,\s*'YYYY-MM-DD[^']+'\)", RegexOptions . IgnoreCase ) ,
@@ -228,13 +228,13 @@ static Regex[] GetRegSqlWhereDateTimes(string columnName, string quoteParameterN
228228 new Regex ( $@ "({ cn } \s*(between)\s+{ quoteParameterName } [\w_]+\s+and(\s+))({ quoteParameterName } [^w_]+)::(datetime|timestamp)", RegexOptions . IgnoreCase ) ,
229229
230230
231- new Regex ( $@ "{ cn } \s*between\s*'([^']+)'\s*and\s*'([^']+)'", RegexOptions . IgnoreCase ) ,
231+ new Regex ( $@ "{ cn } \s*between\s*'([^']+)'\s*and\s*'([^']+)'", RegexOptions . IgnoreCase ) , //预留暂时不用
232232 new Regex ( $@ "{ cn } \s*between\s*{ quoteParameterName } ([\w_]+)\s*and\s*{ quoteParameterName } ([\w_]+)", RegexOptions . IgnoreCase ) ,
233233
234- new Regex ( $@ "{ cn } \s*(<|<=|>|>=)\s*'([^']+)'\s*and\s*{ cn } \s*(<|<=|>|>=)\s*'([^']+)'", RegexOptions . IgnoreCase ) ,
234+ new Regex ( $@ "{ cn } \s*(<|<=|>|>=)\s*'([^']+)'\s*and\s*{ cn } \s*(<|<=|>|>=)\s*'([^']+)'", RegexOptions . IgnoreCase ) , //预留暂时不用
235235 new Regex ( $@ "{ cn } \s*(<|<=|>|>=)\s*{ quoteParameterName } ([\w_]+)\s*and\s*{ cn } \s*(<|<=|>|>=)\s*{ quoteParameterName } ([\w_]+)", RegexOptions . IgnoreCase ) ,
236236
237- new Regex ( $@ "{ cn } \s*(<|<=|>|>=)\s*'([^']+)'", RegexOptions . IgnoreCase ) ,
237+ new Regex ( $@ "{ cn } \s*(<|<=|>|>=)\s*'([^']+)'", RegexOptions . IgnoreCase ) , //预留暂时不用
238238 new Regex ( $@ "{ cn } \s*(<|<=|>|>=)\s*{ quoteParameterName } ([\w_]+)", RegexOptions . IgnoreCase ) ,
239239 } ;
240240 } ) ;
@@ -256,44 +256,53 @@ static Regex[] GetRegSqlWhereDateTimes(string columnName, string quoteParameterN
256256 public string [ ] GetTableNamesBySqlWhere ( string sqlWhere , List < DbParameter > dbParams , SelectTableInfo tb , CommonUtils commonUtils )
257257 {
258258 if ( string . IsNullOrWhiteSpace ( sqlWhere ) ) return AllTables ;
259+ var dictParams = new Dictionary < string , string > ( ) ;
260+ var newSqlWhere = Utils . ReplaceSqlConstString ( sqlWhere , dictParams ) ;
261+ var tsqlWhere = Utils . ParseSqlWhereLevel1 ( sqlWhere ) ;
262+
259263 var quoteParameterName = commonUtils . QuoteParamterName ( "" ) ;
260264 var quoteParameterNameCharArray = quoteParameterName . ToCharArray ( ) ;
261265 var columnName = commonUtils . QuoteSqlName ( tb . Table . AsTableColumn . Attribute . Name ) ;
262266 var regs = GetRegSqlWhereDateTimes ( $ "{ ( string . IsNullOrWhiteSpace ( tb . Alias ) ? "" : $ "{ tb . Alias } .") } { commonUtils . QuoteSqlName ( tb . Table . AsTableColumn . Attribute . Name ) } ", quoteParameterName ) ;
263- for ( var a = 0 ; a < 16 ; a ++ ) sqlWhere = regs [ a ] . Replace ( sqlWhere , "$1$4" ) ;
267+ for ( var a = 0 ; a < 8 ; a ++ ) newSqlWhere = regs [ a ] . Replace ( newSqlWhere , "$1$4" ) ;
264268
265- var m = regs [ 16 ] . Match ( sqlWhere ) ;
266- if ( m . Success ) return GetTableNamesByColumnValueRange ( m . Groups [ 1 ] . Value , m . Groups [ 2 ] . Value ) ;
267- m = m = regs [ 18 ] . Match ( sqlWhere ) ;
268- if ( m . Success ) return LocalGetTables ( m . Groups [ 1 ] . Value , m . Groups [ 3 ] . Value , ParseColumnValue ( m . Groups [ 2 ] . Value ) , ParseColumnValue ( m . Groups [ 4 ] . Value ) ) ;
269- m = regs [ 20 ] . Match ( sqlWhere ) ;
270- if ( m . Success ) return LocalGetTables2 ( m . Groups [ 1 ] . Value , ParseColumnValue ( m . Groups [ 2 ] . Value ) ) ;
269+ // var m = regs[8 ].Match(newSqlWhere );
270+ // if (m.Success) return GetTableNamesByColumnValueRange(m.Groups[1].Value, m.Groups[2].Value);
271+ // m = m = regs[10 ].Match(newSqlWhere );
272+ // if (m.Success) return LocalGetTables(m.Groups[1].Value, m.Groups[3].Value, ParseColumnValue(m.Groups[2].Value), ParseColumnValue(m.Groups[4].Value));
273+ // m = regs[12 ].Match(newSqlWhere );
274+ // if (m.Success) return LocalGetTables2(m.Groups[1].Value, ParseColumnValue(m.Groups[2].Value));
271275
272- m = m = regs [ 17 ] . Match ( sqlWhere ) ;
276+ var m = regs [ 9 ] . Match ( newSqlWhere ) ;
273277 if ( m . Success )
274278 {
275- var val1 = dbParams . Where ( a => a . ParameterName . Trim ( quoteParameterNameCharArray ) == m . Groups [ 2 ] . Value ) . FirstOrDefault ( ) ;
276- var val2 = dbParams . Where ( a => a . ParameterName . Trim ( quoteParameterNameCharArray ) == m . Groups [ 4 ] . Value ) . FirstOrDefault ( ) ;
279+ var val1 = LocalGetParamValue ( m . Groups [ 1 ] . Value ) ;
280+ var val2 = LocalGetParamValue ( m . Groups [ 2 ] . Value ) ;
277281 if ( val1 == null || val2 == null ) throw new Exception ( $ "未能解析分表字段值 { sqlWhere } ") ;
278282 return GetTableNamesByColumnValueRange ( val1 , val2 ) ;
279283 }
280- m = regs [ 19 ] . Match ( sqlWhere ) ;
284+ m = regs [ 11 ] . Match ( newSqlWhere ) ;
281285 if ( m . Success )
282286 {
283- var val1 = dbParams . Where ( a => a . ParameterName . Trim ( quoteParameterNameCharArray ) == m . Groups [ 2 ] . Value ) . FirstOrDefault ( ) ;
284- var val2 = dbParams . Where ( a => a . ParameterName . Trim ( quoteParameterNameCharArray ) == m . Groups [ 4 ] . Value ) . FirstOrDefault ( ) ;
287+ var val1 = LocalGetParamValue ( m . Groups [ 2 ] . Value ) ;
288+ var val2 = LocalGetParamValue ( m . Groups [ 4 ] . Value ) ;
285289 if ( val1 == null || val2 == null ) throw new Exception ( $ "未能解析分表字段值 { sqlWhere } ") ;
286290 return LocalGetTables ( m . Groups [ 1 ] . Value , m . Groups [ 3 ] . Value , ParseColumnValue ( val1 ) , ParseColumnValue ( val2 ) ) ;
287291 }
288- m = regs [ 21 ] . Match ( sqlWhere ) ;
292+ m = regs [ 13 ] . Match ( newSqlWhere ) ;
289293 if ( m . Success )
290294 {
291- var val1 = dbParams . Where ( a => a . ParameterName . Trim ( quoteParameterNameCharArray ) == m . Groups [ 2 ] . Value ) . FirstOrDefault ( ) ;
295+ var val1 = LocalGetParamValue ( m . Groups [ 2 ] . Value ) ;
292296 if ( val1 == null ) throw new Exception ( $ "未能解析分表字段值 { sqlWhere } ") ;
293297 return LocalGetTables2 ( m . Groups [ 1 ] . Value , ParseColumnValue ( val1 ) ) ;
294298 }
295299 return AllTables ;
296300
301+ object LocalGetParamValue ( string paramName )
302+ {
303+ if ( dictParams . TryGetValue ( quoteParameterName + paramName , out var trydictVal ) ) return trydictVal ;
304+ return dbParams . Where ( a => a . ParameterName . Trim ( quoteParameterNameCharArray ) == m . Groups [ 2 ] . Value ) . FirstOrDefault ( ) ? . Value ;
305+ }
297306 string [ ] LocalGetTables ( string opt1 , string opt2 , DateTime val1 , DateTime val2 )
298307 {
299308 switch ( opt1 )
@@ -317,7 +326,7 @@ string[] LocalGetTables(string opt1, string opt2, DateTime val1, DateTime val2)
317326 break ;
318327 case ">" :
319328 case ">=" :
320- if ( opt1 == ">" ) val1 = val1 . AddSeconds ( 1 ) ;
329+ if ( opt1 == ">" ) val1 = val1 . AddSeconds ( 1 ) ;
321330 switch ( opt2 )
322331 {
323332 case "<" :
0 commit comments