Ejemplo n.º 1
0
 private long GetValue(IPaginationValueSegment paginationValueSegment, ParameterContext parameterContext)
 {
     if (paginationValueSegment is IParameterMarkerPaginationValueSegment parameterMarkerPaginationValueSegment)
     {
         if (parameterContext.TryGetParameterValue(parameterMarkerPaginationValueSegment.GetParameterName(), out var parameterValue))
         {
             return(parameterValue is long l ? l : (int)parameterValue);
         }
         else
         {
             throw new ShardingException(
                       $"{nameof(IParameterMarkerPaginationValueSegment)} cant get value that parameter name:[{parameterMarkerPaginationValueSegment.GetParameterName()}]  ");
         }
     }
     else
     {
         return(((INumberLiteralPaginationValueSegment)paginationValueSegment).GetValue());
     }
 }
        /// <summary>
        /// 找到自动生成的键比如自增id
        /// </summary>
        /// <param name="parameterContext"></param>
        /// <param name="insertCommand"></param>
        /// <param name="generateKeyColumnName"></param>
        /// <returns></returns>
        /// <exception cref="ShardingException"></exception>
        private ParserBinder.Segment.Insert.Keygen.GeneratedKeyContext FindGeneratedKey(ParameterContext parameterContext, InsertCommand insertCommand, string generateKeyColumnName)
        {
            ParserBinder.Segment.Insert.Keygen.GeneratedKeyContext result = new ParserBinder.Segment.Insert.Keygen.GeneratedKeyContext(generateKeyColumnName, false);
            foreach (var expression in FindGenerateKeyExpressions(insertCommand, generateKeyColumnName))
            {
                if (expression is ParameterMarkerExpressionSegment parameterMarkerExpressionSegment)
                {
                    var parameterName = parameterMarkerExpressionSegment.GetParameterName();


                    if (!parameterContext.TryGetParameterValue(parameterName, out var value))
                    {
                        throw new ShardingException($"not found parameter name:[{parameterName}] in parameters");
                    }

                    result.GetGeneratedValues().Add((IComparable)value);
                }
                else if (expression is LiteralExpressionSegment literalExpressionSegment)
                {
                    result.GetGeneratedValues().Add((IComparable)literalExpressionSegment.GetLiterals());
                }
            }
            return(result);
        }