Example #1
0
        public Declare ReadDeclare()
        {
            SkipComments();
            Declare dec = new Declare();

            Next();
            var exp = ReadExpression();

            if (exp is Variable vari1)
            {
                dec.VarName = vari1.Name;
            }
            else
            {
                ex("declare语法错误。");
            }
            exp = ReadExpression();
            SqlDataTyoe t = new SqlDataTyoe();

            if (exp is Variable vari)
            {
                t.DataType = SqlDataTyoe.GetDataType(vari.Name);
            }
            else if (exp is Function func)
            {
                t.DataType = SqlDataTyoe.GetDataType(func.Name);
                var paras = func.Arguments?.GetList();
                if (paras != null && paras.Count > 0)
                {
                    try
                    {
                        t.Length1 = int.Parse(((Constant)paras[0]).Content);
                        if (paras.Count > 1)
                        {
                            t.Length2 = int.Parse(((Constant)paras[1]).Content);
                            if (paras.Count > 2)
                            {
                                ex($"类型[{func.Name}]的长度/精度参数错误。");
                            }
                        }
                    }
                    catch (Exception)
                    {
                        ex($"类型[{func.Name}]的长度/精度参数错误。");
                    }
                }
            }
            dec.DataType = t;

            return(dec);
        }
Example #2
0
        public override string GetDataType(SqlDataTyoe type)
        {
            string t = "";

            switch (type.DataType)
            {
            case DataType.Char:
                t = "char";
                break;

            case DataType.VarChar:
                t = "varchar";
                break;

            case DataType.Text:
                t = "text";
                break;

            case DataType.Int:
                t = "int";
                break;

            case DataType.Long:
                t = "bigint";
                break;

            case DataType.Decimal:
                t = "decimal";
                break;

            case DataType.Boolean:
                t = "bit";
                break;

            case DataType.Binary:
                t = "binary";
                break;

            case DataType.DateTime:
                t = "datetime";
                break;

            case DataType.Time:
                t = "time";
                break;

            default:
                throw new NotSupportedException("不支持的类型:" + type.DataType.ToString());
            }
            string getlength(int len)
            {
                if (len >= 0)
                {
                    return(len.ToString());;
                }
                else
                {//为负表示使用最大值
                    return("max");
                }
            }

            if (type.Length1.HasValue && type.Length2.HasValue)
            {
                return($"{t}({getlength(type.Length1.Value)},{getlength(type.Length2.Value)})");
            }
            else if (type.Length1.HasValue)
            {
                return($"{t}({getlength(type.Length1.Value)})");
            }
            else
            {
                return(t);
            }
        }
Example #3
0
 /// <summary>
 /// 在派生类中实现,获取通用数据类型在目标数据库中的实现
 /// </summary>
 /// <param name="t"></param>
 /// <returns></returns>
 public abstract string GetDataType(SqlDataTyoe t);