c# - How to convert ADO.NET SQL Server parametrized query to ORACLE query (in Regex)? -
queries can like:
select * atable acolumn = @acolumn; select * atable acolumn >= @acolumn; select * atable acolumn between @acolumn1 , @acolumn2; //not necessary support
if better option regex available i'm open suggestions. there must method/delegate, adds dbparameter
dbcommand
when match found. way beyond regex skills.
edit: happy restrict =, <, >, >=, <= in way column can 1 time. leaving in, not in , between out of this. more complex queries can made other way (explained in comment).
-m
ok. did basic implementation, if has might idea in basic case. here query written in oracle format , in case of sql-server change : "placeholder" @.
public void addparameters(string query, list<object> valuelist) { int ordernr = 0; string newquery = regex.replace(query, @"(?<operator>=|<=|>=|<|>)\s?(?<placeholder>:)(?<columnname>([a-za-z0-9\-]+))", new matchevaluator( delegate(match match) { var param = this.providerfactory.createparameter(); param.parametername = match.groups["columnname"].value; param.dbtype = getdbtype(valuelist[ordernr]); param.value = valuelist[ordernr++]; parameters.add(param); if (providertype == dbprovidertype.sqlserver) return string.format("{0}@{1}", match.groups["operator"].value, match.groups["columnname"]); return match.value; } )); } private dbtype getdbtype(object value) { if (value int) return dbtype.int32; else if (value double) return dbtype.double; else if (value string) return dbtype.string; else return dbtype.datetime; }
used this:
query = "select * atable columna=:columna , columnb=columnb , (languageid=:languageid or languageid = '*')"; cmd.addparameters(query, new list<object> { tableid, fieldname, languageid });
here cmd own abstraction of dbcommand-class.
Comments
Post a Comment