/// <summary> /// Parse Inline ParameterMap /// </summary> /// <param name="statement"></param> /// <param name="sqlStatement"></param> /// <returns>A new sql command text.</returns> /// <param name="scope"></param> public SqlText ParseInlineParameterMap(IScope scope, IStatement statement, string sqlStatement) { string newSql = sqlStatement; ArrayList mappingList = new ArrayList(); Type parameterClassType = null; if (statement != null) { parameterClassType = statement.ParameterClass; } StringTokenizer parser = new StringTokenizer(sqlStatement, PARAMETER_TOKEN, true); StringBuilder newSqlBuffer = new StringBuilder(); string token = null; string lastToken = null; IEnumerator enumerator = parser.GetEnumerator(); while (enumerator.MoveNext()) { token = (string)enumerator.Current; if (PARAMETER_TOKEN.Equals(lastToken)) { if (PARAMETER_TOKEN.Equals(token)) { newSqlBuffer.Append(PARAMETER_TOKEN); token = null; } else { ParameterProperty mapping = null; if (token.IndexOf(PARAM_DELIM) > -1) { mapping = OldParseMapping(token, parameterClassType, scope); } else { mapping = NewParseMapping(token, parameterClassType, scope); } mappingList.Add(mapping); newSqlBuffer.Append("? "); enumerator.MoveNext(); token = (string)enumerator.Current; if (!PARAMETER_TOKEN.Equals(token)) { throw new DataMapperException("Unterminated inline parameter in mapped statement (" + statement.Id + ")."); } token = null; } } else { if (!PARAMETER_TOKEN.Equals(token)) { newSqlBuffer.Append(token); } } lastToken = token; } newSql = newSqlBuffer.ToString(); ParameterProperty[] mappingArray = (ParameterProperty[]) mappingList.ToArray(typeof(ParameterProperty)); SqlText sqlText = new SqlText(); sqlText.Text = newSql; sqlText.Parameters = mappingArray; return sqlText; }
/// <summary> /// Parse inline parameter with syntax as /// #propertyName,type=string,dbype=Varchar,direction=Input,nullValue=N/A,handler=string# /// </summary> /// <param name="token"></param> /// <param name="parameterClassType"></param> /// <param name="scope"></param> /// <returns></returns> private ParameterProperty NewParseMapping(string token, Type parameterClassType, IScope scope) { ParameterProperty mapping = new ParameterProperty(); StringTokenizer paramParser = new StringTokenizer(token, "=,", false); IEnumerator enumeratorParam = paramParser.GetEnumerator(); enumeratorParam.MoveNext(); mapping.PropertyName = ((string)enumeratorParam.Current).Trim(); while (enumeratorParam.MoveNext()) { string field = (string)enumeratorParam.Current; if (enumeratorParam.MoveNext()) { string value = (string)enumeratorParam.Current; if ("type".Equals(field)) { mapping.CLRType = value; } else if ("dbType".Equals(field)) { mapping.DbType = value; } else if ("direction".Equals(field)) { mapping.DirectionAttribute = value; } else if ("nullValue".Equals(field)) { mapping.NullValue = value; } else if ("handler".Equals(field)) { mapping.CallBackName = value; } else { throw new DataMapperException("Unrecognized parameter mapping field: '" + field + "' in " + token); } } else { throw new DataMapperException("Incorrect inline parameter map format (missmatched name=value pairs): " + token); } } if (mapping.CallBackName.Length >0) { mapping.Initialize( scope, parameterClassType ); } else { ITypeHandler handler = null; if (parameterClassType == null) { handler = scope.DataExchangeFactory.TypeHandlerFactory.GetUnkownTypeHandler(); } else { handler = ResolveTypeHandler( scope.DataExchangeFactory.TypeHandlerFactory, parameterClassType, mapping.PropertyName, mapping.CLRType, mapping.DbType ); } mapping.TypeHandler = handler; mapping.Initialize( scope, parameterClassType ); } return mapping; }
/// <summary> /// Parse inline parameter with syntax as /// #propertyName:dbType:nullValue# /// </summary> /// <param name="token"></param> /// <param name="parameterClassType"></param> /// <param name="scope"></param> /// <returns></returns> private ParameterProperty OldParseMapping(string token, Type parameterClassType, IScope scope) { ParameterProperty mapping = new ParameterProperty(); if (token.IndexOf(PARAM_DELIM) > -1) { StringTokenizer paramParser = new StringTokenizer(token, PARAM_DELIM, true); IEnumerator enumeratorParam = paramParser.GetEnumerator(); int n1 = paramParser.TokenNumber; if (n1 == 3) { enumeratorParam.MoveNext(); string propertyName = ((string)enumeratorParam.Current).Trim(); mapping.PropertyName = propertyName; enumeratorParam.MoveNext(); enumeratorParam.MoveNext(); //ignore ":" string dBType = ((string)enumeratorParam.Current).Trim(); mapping.DbType = dBType; ITypeHandler handler = null; if (parameterClassType == null) { handler = scope.DataExchangeFactory.TypeHandlerFactory.GetUnkownTypeHandler(); } else { handler = ResolveTypeHandler(scope.DataExchangeFactory.TypeHandlerFactory, parameterClassType, propertyName, null, dBType); } mapping.TypeHandler = handler; mapping.Initialize( scope, parameterClassType ); } else if (n1 >= 5) { enumeratorParam.MoveNext(); string propertyName = ((string)enumeratorParam.Current).Trim(); enumeratorParam.MoveNext(); enumeratorParam.MoveNext(); //ignore ":" string dBType = ((string)enumeratorParam.Current).Trim(); enumeratorParam.MoveNext(); enumeratorParam.MoveNext(); //ignore ":" string nullValue = ((string)enumeratorParam.Current).Trim(); while (enumeratorParam.MoveNext()) { nullValue = nullValue + ((string)enumeratorParam.Current).Trim(); } mapping.PropertyName = propertyName; mapping.DbType = dBType; mapping.NullValue = nullValue; ITypeHandler handler = null; if (parameterClassType == null) { handler = scope.DataExchangeFactory.TypeHandlerFactory.GetUnkownTypeHandler(); } else { handler = ResolveTypeHandler(scope.DataExchangeFactory.TypeHandlerFactory, parameterClassType, propertyName, null, dBType); } mapping.TypeHandler = handler; mapping.Initialize( scope, parameterClassType ); } else { throw new ConfigurationException("Incorrect inline parameter map format: " + token); } } else { mapping.PropertyName = token; ITypeHandler handler = null; if (parameterClassType == null) { handler = scope.DataExchangeFactory.TypeHandlerFactory.GetUnkownTypeHandler(); } else { handler = ResolveTypeHandler(scope.DataExchangeFactory.TypeHandlerFactory, parameterClassType, token, null, null); } mapping.TypeHandler = handler; mapping.Initialize( scope, parameterClassType ); } return mapping; }
/// <summary> /// Parse sql command text. /// </summary> private void EvaluateParameterMap() { string delimiter = "?"; string token = null; int index = 0; string sqlParamName = string.Empty; StringTokenizer parser = new StringTokenizer(_commandText, delimiter, true); StringBuilder newCommandTextBuffer = new StringBuilder(); IEnumerator enumerator = parser.GetEnumerator(); while (enumerator.MoveNext()) { token = (string)enumerator.Current; if (delimiter.Equals(token)) // ? { ParameterProperty property = _request.ParameterMap.Properties[index]; IDataParameter dataParameter = null; if (_session.DataSource.DbProvider.UsePositionalParameters) { // TODO Refactor? if (_parameterPrefix.Equals(":")) { // ODP.NET uses positional parameters by default // but uses ":0" or ":1" instead of "?" sqlParamName = ":" + index; } else { // OLEDB/OBDC doesn't support named parameters !!! sqlParamName = "?"; } } else { dataParameter = (IDataParameter) _propertyDbParameterMap[property]; // 5 May 2004 // Need to check UseParameterPrefixInParameter here // since CreateParametersForStatementText now does // a check for UseParameterPrefixInParameter before // creating the parameter name! if (_session.DataSource.DbProvider.UseParameterPrefixInParameter) { // Fix ByteFX.Data.MySqlClient.MySqlParameter // who strip prefix in Parameter Name ?! if (_session.DataSource.DbProvider.Name.IndexOf("ByteFx")>=0) { sqlParamName = _parameterPrefix+dataParameter.ParameterName; } else { sqlParamName = dataParameter.ParameterName; } } else { sqlParamName = _parameterPrefix+dataParameter.ParameterName; } } newCommandTextBuffer.Append(" "); newCommandTextBuffer.Append(sqlParamName); sqlParamName = string.Empty; index ++; } else { newCommandTextBuffer.Append(token); } } _preparedStatement.PreparedSql = newCommandTextBuffer.ToString(); }
/// <summary> /// Parse and tokenize the sql script into multiple statements /// </summary> /// <param name="script">the script to parse</param> private ArrayList ParseScript(string script) { ArrayList statements = new ArrayList(); StringTokenizer parser = new StringTokenizer(script, ";"); IEnumerator enumerator = parser.GetEnumerator(); while (enumerator.MoveNext()) { string statement= ((string)enumerator.Current).Replace("\r\n"," "); statement = statement.Trim(); if (statement != string.Empty) { statements.Add(statement); } } return statements; }
public StringTokenizerEnumerator(StringTokenizer stok) { _stokenizer = stok; }
/// <summary> /// /// </summary> /// <param name="parameterObject"></param> /// <returns></returns> private string ProcessDynamicElements(object parameterObject) { // define which character is seperating fields StringTokenizer parser = new StringTokenizer(_simpleSqlStatement, ELEMENT_TOKEN, true); StringBuilder newSql = new StringBuilder(); string token = null; string lastToken = null; IEnumerator enumerator = parser.GetEnumerator(); while (enumerator.MoveNext()) { token = ((string)enumerator.Current); if (ELEMENT_TOKEN.Equals(lastToken)) { if (ELEMENT_TOKEN.Equals(token)) { newSql.Append(ELEMENT_TOKEN); token = null; } else { object value = null; if (parameterObject != null) { if ( _dataExchangeFactory.TypeHandlerFactory.IsSimpleType( parameterObject.GetType() ) == true) { value = parameterObject; } else { value = ObjectProbe.GetMemberValue(parameterObject, token, _dataExchangeFactory.AccessorFactory); } } if (value != null) { newSql.Append(value.ToString()); } enumerator.MoveNext(); token = ((string)enumerator.Current); if (!ELEMENT_TOKEN.Equals(token)) { throw new DataMapperException("Unterminated dynamic element in sql (" + _simpleSqlStatement + ")."); } token = null; } } else { if (!ELEMENT_TOKEN.Equals(token)) { newSql.Append(token); } } lastToken = token; } return newSql.ToString(); }