/// <summary> /// 解释 FROM 子句。 /// </summary> /// <param name="DbParameters">用于缓存在解释过程中可能会产生的参数。</param> /// <returns></returns> public override string Parsing(ref List <IDbDataParameter> DbParameters) { FromBlock fb = (FromBlock)this.Description; StringBuilder cBuffer = new StringBuilder(" FROM"); foreach (object item in fb.Content) { if (item is IDescription) { IDescription D = (IDescription)item; D.DescriptionParserAdapter = fb.DescriptionParserAdapter; cBuffer.Append(D.GetParser().Parsing(ref DbParameters)); } else { if (cBuffer[cBuffer.Length - 1] == (char)0x20) { cBuffer.Append(item); } else { cBuffer.AppendFormat(" {0}", item); } } } return(cBuffer.ToString()); }
/// <summary> /// 列的解析默认值设置. /// </summary> /// <param name="definition"></param> /// <param name="DbParameters"></param> /// <returns></returns> private string ParseDefaultValue(DbTableColumnDefinition definition, ref List <IDbDataParameter> DbParameters) { IDescription valueDes = definition.Default as IDescription; if (valueDes == null) { switch (definition.DataType) { case GenericDbType.Boolean: return(string.Format("DEFAULT({0})", Convert.ToBoolean(definition.Default) ? 1 : 0)); case GenericDbType.SmallInt: case GenericDbType.Int: case GenericDbType.BigInt: case GenericDbType.Single: case GenericDbType.Money: case GenericDbType.Double: return(string.Format("DEFAULT({0})", definition.Default)); case GenericDbType.Binary: case GenericDbType.VarBinary: case GenericDbType.Image: throw new NotSupportedException(string.Format("Data type {0} does not support setting default value.", definition.Default)); default: return(string.Format("DEFAULT('{0}')", definition.Default.ToString())); } } valueDes.DescriptionParserAdapter = this.Adapter; return(string.Format("DEFAULT({0})", valueDes.GetParser().Parsing(ref DbParameters))); }
/// <summary> /// Count 函数解释。 /// </summary> /// <param name="D">FunDescription 对象。</param> /// <param name="DbParameters">用于缓存在解释过程中可能会产生的参数。</param> /// <returns></returns> protected virtual string CountParsing(FunDescription D, ref List <IDbDataParameter> DbParameters) { if (D.Parameter is IDescription) { // 参数为表达式或都字段。 IDescription pDes = (IDescription)(D.Parameter); pDes.DescriptionParserAdapter = D.DescriptionParserAdapter; string param_buf = pDes.GetParser().Parsing(ref DbParameters); if (param_buf[0] == (char)0x20) { param_buf = param_buf.Remove(0, 1); } return(string.Format("COUNT({0})", param_buf)); } else if (D.Parameter is string || D.Parameter is char) { string param_v = D.Parameter.ToString(); if (param_v == "*") { // COUNT(*) 解释。 return("COUNT(*)"); } else { throw new Exception("Count 函数使用了不正确的参数。"); } } else { // 其它情况。 IDbDataParameter p = Adapter.CreateDbParameter("uf_count", D.Parameter); AddDbParameter(ref DbParameters, p); return(string.Format("COUNT({0})", p.ParameterName)); } }
/// <summary> /// 解释逻辑运算的操作元素. /// </summary> /// <param name="DbParameters">用于缓存在解释过程中可能会产生的参数。</param> /// <param name="elemObject">左或右操作元素对象.</param> /// <returns></returns> protected string ParseElementObject(ref List <IDbDataParameter> DbParameters, object elemObject) { if (elemObject == null || elemObject == DBNull.Value) { throw new NotSupportedException("Can not use null objects for logical expression."); } IDescription descr = elemObject as IDescription; string paramName = "u_LogicParam"; if (descr == null) { IDbDataParameter p = Adapter.CreateDbParameter(paramName, elemObject); AddDbParameter(ref DbParameters, p); return(string.Format(" {0}", p.ParameterName)); } descr.DescriptionParserAdapter = Adapter; string buffer = descr.GetParser().Parsing(ref DbParameters); if (buffer[0] == (char)0x20) { return(buffer); } else { return(string.Format(" {0}", buffer)); } }
/// <summary> /// 解释逻辑非运算表达式. /// </summary> /// <param name="DbParameters">用于缓存在解释过程中可能会产生的参数。</param> /// <returns></returns> public override string Parsing(ref List <IDbDataParameter> DbParameters) { LogicNotDescription logicNot = (LogicNotDescription)this.Description; List <Type> allowTypes = new List <Type>(new Type[] { typeof(FieldDescription), typeof(ExpDescription), typeof(FunDescription), typeof(GroupDescription) }); IDescription desObject = logicNot.Expression; Type expType = desObject.GetType(); if (!(allowTypes.Contains(expType))) { throw new NotSupportedException(string.Format("Can not use this description object: {0} for logical not expression.", expType.FullName)); } desObject.DescriptionParserAdapter = this.Adapter; string buffer = desObject.GetParser().Parsing(ref DbParameters); if (buffer[0] == (char)0x20) { return(string.Format(" NOT{0}", buffer)); } else { return(string.Format(" NOT {0}", buffer)); } }
/// <summary> /// 解释元素优先级分组. /// </summary> /// <param name="DbParameters">用于缓存在解释过程中可能会产生的参数。</param> /// <returns></returns> public override string Parsing(ref List <IDbDataParameter> DbParameters) { GroupDescription groupDes = (GroupDescription)this.Description; IDescription desObject = groupDes.Content; desObject.DescriptionParserAdapter = this.Adapter; string buffer = desObject.GetParser().Parsing(ref DbParameters); if (buffer[0] == (char)0x20) { return(string.Format(" ({0})", buffer.Remove(0, 1))); } else { return(string.Format(" ({0})", buffer)); } }
/// <summary> /// NotIn 函数解释。 /// </summary> /// <param name="D">FunDescription 对象。</param> /// <param name="DbParameters">用于缓存在解释过程中可能会产生的参数。</param> /// <returns></returns> protected virtual string NotInParsing(FunDescription D, ref List <IDbDataParameter> DbParameters) { object[] values = (object[])D.Parameter; StringBuilder cBuffer = new StringBuilder(); FieldDescription fd = (FieldDescription)values[0]; fd.DescriptionParserAdapter = D.DescriptionParserAdapter; cBuffer.Append(fd.GetParser().Parsing(ref DbParameters)); cBuffer.Append(" NOT IN ("); bool AppendComma = false; // 是否加逗号标识。 for (int i = 1; i < values.Length; ++i) { string buf; if (values[i] is IDescription) { IDescription desObject = (IDescription)values[i]; desObject.DescriptionParserAdapter = D.DescriptionParserAdapter; buf = desObject.GetParser().Parsing(ref DbParameters); if (buf[0] == (char)0x20) { buf = buf.Remove(0, 1); } } else { IDbDataParameter p = Adapter.CreateDbParameter(string.Format("uf_inValue{0}", i), values[i]); AddDbParameter(ref DbParameters, p); buf = p.ParameterName; } if (AppendComma) { cBuffer.AppendFormat(", {0}", buf); } else { cBuffer.Append(buf); AppendComma = true; } } cBuffer.Append(")"); return(cBuffer.ToString()); }
/// <summary> /// 解释 VALUES 子句中的值元素。 /// </summary> /// <param name="field">与该值对应的字段。</param> /// <param name="v">值。</param> /// <param name="DbParameters">用于缓存在解释过程中可能会产生的参数。</param> /// <returns></returns> protected virtual string ParsingValueItem(string field, object v, ref List <IDbDataParameter> DbParameters) { if (v is IDescription) { // 为表达式时。 IDescription D = (IDescription)v; D.DescriptionParserAdapter = this.Description.DescriptionParserAdapter; string buffer = D.GetParser().Parsing(ref DbParameters); if (buffer[0] == (char)0x20) { buffer = buffer.Remove(0, 1); } return(buffer); } else { // 为值时。 IDbDataParameter p = Adapter.CreateDbParameter(string.Format("u_{0}", field), v); AddDbParameter(ref DbParameters, p); return(p.ParameterName); } }
/// <summary> /// Len 函数解释。 /// </summary> /// <param name="D"></param> /// <param name="DbParameters"></param> /// <returns></returns> protected override string LenParsing(FunDescription D, ref List <IDbDataParameter> DbParameters) { if (D.Parameter is IDescription) { IDescription desObject = (IDescription)(D.Parameter); desObject.DescriptionParserAdapter = D.DescriptionParserAdapter; string buf = desObject.GetParser().Parsing(ref DbParameters); if (buf[0] == (char)0x20) { buf = buf.Remove(0, 1); } return(string.Format("LEN({0})", buf)); } else { IDbDataParameter p = Adapter.CreateDbParameter("uf_len_param", D.Parameter); AddDbParameter(ref DbParameters, p); return(string.Format("LEN({0})", p.ParameterName)); } }
/// <summary> /// Sum 函数解释。 /// </summary> /// <param name="D">FunDescription 对象。</param> /// <param name="DbParameters">用于缓存在解释过程中可能会产生的参数。</param> /// <returns></returns> protected virtual string SumParsing(FunDescription D, ref List <IDbDataParameter> DbParameters) { if (D.Parameter is IDescription) { // 参数为表达式或都字段。 IDescription pDes = (IDescription)(D.Parameter); pDes.DescriptionParserAdapter = D.DescriptionParserAdapter; string param_buf = pDes.GetParser().Parsing(ref DbParameters); if (param_buf[0] == (char)0x20) { param_buf = param_buf.Remove(0, 1); } return(string.Format("SUM({0})", param_buf)); } else { // 其它情况。 IDbDataParameter p = Adapter.CreateDbParameter("uf_sum", D.Parameter); AddDbParameter(ref DbParameters, p); return(string.Format("SUM({0})", p.ParameterName)); } }
/// <summary> /// Substring 函数解释。 /// </summary> /// <param name="D"></param> /// <param name="DbParameters"></param> /// <returns></returns> protected override string SubstringParsing(FunDescription D, ref List <IDbDataParameter> DbParameters) { object[] ps = (object[])D.Parameter; if (ps[0] is IDescription) { IDescription desObject = (IDescription)ps[0]; desObject.DescriptionParserAdapter = D.DescriptionParserAdapter; string buf = desObject.GetParser().Parsing(ref DbParameters); if (buf[0] == (char)0x20) { buf = buf.Remove(0, 1); } return(string.Format("SUBSTRING({0}, {1}, {2})", buf, ps[1], ps[2])); } else { IDbDataParameter p = Adapter.CreateDbParameter("uf_substring", ps[0]); AddDbParameter(ref DbParameters, p); return(string.Format("SUBSTRING({0}, {1}, {2})", p.ParameterName, ps[1], ps[2])); } }
/// <summary> /// 解释整个命令,同时创建该命令应有的参数并返回命令文本。 /// </summary> /// <param name="adapter">解释命令所需的适配器。</param> /// <returns></returns> public virtual string Parsing(ParserAdapter adapter) { // 防止多次调用此方法导致参数集合内的参数重复。 // 如果参数重复将会导致读或写库失败。 if (IsParsed) { return(CommandText); } CommandDescription.DescriptionParserAdapter = adapter; if (CommandDescription.DescriptionParserAdapter == null) { throw (new Exception("未初始化解释命令所需要的适配器。")); } _IdentityCommand = adapter.IdentityCommand; ParserBase parser = CommandDescription.GetParser(); CommandText = parser.Parsing(ref _CommandParameters); _IsParsed = true; return(CommandText); }
/// <summary> /// 解释整个表达式信息。 /// </summary> /// <param name="DbParameters">用于缓存在解释过程中可能会产生的参数。</param> /// <returns></returns> public override string Parsing(ref List <IDbDataParameter> DbParameters) { AsElementDecsription elem = (AsElementDecsription)this.Description; if (string.IsNullOrEmpty(elem.AsName)) { throw new Exception("未指定 AS 别名,将无法正确解释 AsElementDecsription 。"); } if (elem.Objective is IDescription) { IDescription Des = (IDescription)elem.Objective; Des.DescriptionParserAdapter = elem.DescriptionParserAdapter; // 将解析适配器继续向下传递。 string buf = Des.GetParser().Parsing(ref DbParameters); return(string.Format("{0} AS {1}{2}{3}", buf, ElemIdentifierL, elem.AsName, ElemIdentifierR)); } else { IDbDataParameter p = Adapter.CreateDbParameter("u_ASVAL", elem.Objective); AddDbParameter(ref DbParameters, p); return(string.Format("{0} AS {1}{2}{3}", p.ParameterName, ElemIdentifierL, elem.AsName, ElemIdentifierR)); } }
/// <summary> /// NotBetweenAnd 函数解释。 /// </summary> /// <param name="D">FunDescription 对象。</param> /// <param name="DbParameters">用于缓存在解释过程中可能会产生的参数。</param> /// <returns></returns> protected virtual string NotBetweenAndParsing(FunDescription D, ref List <IDbDataParameter> DbParameters) { object[] values = (object[])D.Parameter; IDescription desObject = (IDescription)values[0]; desObject.DescriptionParserAdapter = D.DescriptionParserAdapter; string buf = desObject.GetParser().Parsing(ref DbParameters); if (buf[0] == (char)0x20) { buf = buf.Remove(0, 1); } StringBuilder cBuffer = new StringBuilder(buf); IDbDataParameter p1 = Adapter.CreateDbParameter("uf_start", values[1]); AddDbParameter(ref DbParameters, p1); cBuffer.AppendFormat(" NOT BETWEEN {0}", p1.ParameterName); IDbDataParameter p2 = Adapter.CreateDbParameter("uf_end", values[2]); AddDbParameter(ref DbParameters, p2); cBuffer.AppendFormat(" AND {0}", p2.ParameterName); return(cBuffer.ToString()); }
/// <summary> /// 解释整个表达式信息。 /// </summary> /// <param name="DbParameters">用于缓存在解释过程中可能会产生的参数。</param> /// <returns></returns> public override string Parsing(ref List <IDbDataParameter> DbParameters) { ExpDescription expDes = (ExpDescription)this.Description; StringBuilder cBuffer = new StringBuilder(); string paramName = string.Empty; foreach (object item in expDes.ExpElements) { // 如果是命令元素描述对象,则调用它相应的解释器。 if (item is IDescription) { IDescription Des = (IDescription)item; Des.DescriptionParserAdapter = expDes.DescriptionParserAdapter; // 如果是字段描述对象,则先将字段名称记录下来(后面的非描述对象可能需要创建参数,这就需要使用到) FieldDescription fdes = Des as FieldDescription; if (!IsNull(fdes)) { paramName = fdes.FieldName; } string buf = Des.GetParser().Parsing(ref DbParameters); // 判断并补空格。 if (buf[0] == (char)0x20) { cBuffer.Append(buf); } else { cBuffer.AppendFormat(" {0}", buf); } } else if (item is char) // 如果是单字符,则先分析是否是预定义的运算符,如果是则需要进行相应的转换。 { bool createParam = false; string buf = ParsingOperChar((char)item, ref createParam); if (createParam && !string.IsNullOrEmpty(paramName)) { // 当单字段并非预定义的运算符时视为值,并为其创建参数。 IDbDataParameter p = Adapter.CreateDbParameter(string.Format("u_{0}", paramName), buf); AddDbParameter(ref DbParameters, p); cBuffer.AppendFormat(" {0}", p.ParameterName); paramName = string.Empty; // 参数创建完成后应该清除该记录,否则下一次循环就会一直定沿用。 } else { // 单字符为预定义的运算符时。 cBuffer.AppendFormat(" {0}", buf); } } else // 其他类型的处理 { if (item.Equals(string.Empty)) { // 空字符串转换。 cBuffer.AppendFormat(" ''"); } else { if (string.IsNullOrEmpty(paramName)) { // 前面没有检测到字段描述信息,不需要创建参数。 cBuffer.AppendFormat(" {0}", item); } else { // 前面的元素检测到字段信息,需要创建参数。 IDbDataParameter p = Adapter.CreateDbParameter(string.Format("u_{0}", paramName), item); AddDbParameter(ref DbParameters, p); cBuffer.AppendFormat(" {0}", p.ParameterName); paramName = string.Empty; // 参数创建完成后应该清除该记录,否则下一次循环就会一直定沿用。 } } } } return(cBuffer.ToString()); }