@@ -2268,11 +2268,19 @@ public static string GetCsName(string name)
22682268 return char . IsLetter ( name , 0 ) ? name : string . Concat ( "_" , name ) ;
22692269 }
22702270
2271- public static string ReplaceSqlConstString ( string sql , Dictionary < string , string > parms )
2271+ public static string ReplaceSqlConstString ( string sql , Dictionary < string , string > parms , string paramPrefix = "@" )
22722272 {
22732273 var nsb = new StringBuilder ( ) ;
22742274 var sidx = 0 ;
22752275 var pidx = 0 ;
2276+ var ptmpPrefix = "" ;
2277+ while ( true )
2278+ {
2279+ pidx ++ ;
2280+ ptmpPrefix = $ "{ paramPrefix } p{ pidx } ";
2281+ if ( sql . Contains ( ptmpPrefix ) == false ) break ;
2282+ }
2283+ pidx = 0 ;
22762284 while ( sidx < sql . Length )
22772285 {
22782286 var chr = sql [ sidx ++ ] ;
@@ -2307,7 +2315,7 @@ public static string ReplaceSqlConstString(string sql, Dictionary<string, string
23072315 while ( true )
23082316 {
23092317 pidx ++ ;
2310- pname = $ "@p { pidx } ";
2318+ pname = $ "{ ptmpPrefix } { pidx } ";
23112319 if ( parms . ContainsKey ( pname ) == false ) break ;
23122320 }
23132321 }
@@ -2326,46 +2334,72 @@ internal static string ParseSqlWhereLevel1(string sql)
23262334 var remidx = sql . IndexOf ( "WHERE " ) ;
23272335 if ( remidx != - 1 ) sql = sql . Substring ( remidx + 6 ) ;
23282336
2329- var sidx = 0 ;
2330- var ltcou = 0 ;
2331- var ltidxStack = new Stack < int > ( ) ;
2332- while ( sidx < sql . Length )
2337+ //sql = Regex.Replace(sql, @"\s*([@:\?][\w_]+)\s*(<|<=|>|>=|=)\s*((\w+)\s*\.)?([\w_]+)");
2338+ return LocalProcessBrackets ( sql ) ;
2339+
2340+
2341+ string LocalProcessBrackets ( string locsql )
23332342 {
2334- var chr = sql [ sidx ++ ] ;
2335- if ( chr == '(' )
2336- {
2337- ltcou ++ ;
2338- ltidxStack . Push ( sidx - 1 ) ;
2339- }
2340- if ( chr == ')' )
2343+ var sidx = 0 ;
2344+ var ltcou = 0 ;
2345+ var ltidxStack = new Stack < int > ( ) ;
2346+ while ( sidx < locsql . Length )
23412347 {
2342- ltcou -- ;
2343- var ltidx = ltidxStack . Pop ( ) ;
2344- if ( ltidx == 0 && sidx == sql . Length - 1 )
2345- break ;
2346- var sqlLeft = ltidx == 0 ? "" : sql . Remove ( ltidx ) ;
2347- var sqlMid = sql . Substring ( ltidx , sidx - ltidx ) ;
2348- var sqlMidNew = "" ;
2349- var sqlRight = sidx == sql . Length - 1 ? "" : sql . Substring ( sidx + 1 ) ;
2350- var mLeft = Regex . Match ( sqlLeft , @" (and|or|not)\s*$" , RegexOptions . IgnoreCase ) ;
2351- if ( mLeft . Success )
2348+ var chr = locsql [ sidx ++ ] ;
2349+ if ( chr == '(' )
2350+ {
2351+ ltcou ++ ;
2352+ ltidxStack . Push ( sidx - 1 ) ;
2353+ }
2354+ if ( chr == ')' )
23522355 {
2353- switch ( mLeft . Groups [ 1 ] . Value )
2356+ ltcou -- ;
2357+ var ltidx = ltidxStack . Pop ( ) ;
2358+ var ltidx2 = ltidx ;
2359+ var sidx2 = sidx ;
2360+ while ( sidx < locsql . Length )
23542361 {
2355- case "and" :
2356- sqlMidNew = sqlMid . Substring ( 1 , sqlMid . Length - 2 ) ;
2357- break ;
2358- case "or" :
2359- break ;
2360- case "not" :
2361- break ;
2362+ var chr2 = locsql [ sidx ] ;
2363+ if ( chr2 == ')' )
2364+ {
2365+ if ( ltidxStack . First ( ) == ltidx - 1 )
2366+ {
2367+ ltidx = ltidxStack . Pop ( ) ;
2368+ sidx ++ ;
2369+ }
2370+ }
2371+ break ;
2372+ }
2373+ if ( ltidx == 0 && sidx == locsql . Length )
2374+ {
2375+ locsql = locsql . Substring ( 1 , sidx - 2 ) ;
2376+ break ;
2377+ }
2378+ var sqlLeft = ltidx == 0 ? "" : locsql . Remove ( ltidx ) ;
2379+ var sqlMid = locsql . Substring ( ltidx , sidx - ltidx ) ;
2380+ var sqlMidNew = sqlMid ;
2381+ var sqlRight = sidx == locsql . Length ? "" : locsql . Substring ( sidx ) ;
2382+ var mLeft = Regex . Match ( sqlLeft , @" (and|or|not)\s*$" , RegexOptions . IgnoreCase ) ;
2383+ if ( mLeft . Success )
2384+ {
2385+ switch ( mLeft . Groups [ 1 ] . Value )
2386+ {
2387+ case "and" :
2388+ sqlMidNew = sqlMid . Substring ( 1 , sqlMid . Length - 2 ) . Trim ( ) ;
2389+ break ;
2390+ case "or" :
2391+ sqlMidNew = "" ;
2392+ break ;
2393+ case "not" :
2394+ break ;
2395+ }
23622396 }
2397+ sidx -= sqlMid . Length - sqlMidNew . Length ;
2398+ locsql = $ "{ sqlLeft } { sqlMidNew } { sqlRight } ";
23632399 }
2364- sidx -= sqlMid . Length - sqlMidNew . Length ;
2365- sql = $ "{ sqlLeft } { sqlMidNew } { sqlRight } ";
23662400 }
2401+ return locsql ;
23672402 }
2368- return sql ;
23692403 }
23702404
23712405 static string ParseSqlWhereLevel12 ( string sql )
0 commit comments