/// <summary> /// </summary> /// <param name="model"></param> /// <param name="cls"></param> /// <param name="bscls"></param> /// <param name="xml"></param> /// <returns></returns> public override SqlObject Setup(PersistentModel model, PersistentClass cls, IBSharpClass bscls, XElement xml){ model = model ?? cls.Model; base.Setup(model, cls, bscls, xml); string xname = xml.Name.LocalName; IsProcedure = xname == "void" || (xname == "function" && string.IsNullOrWhiteSpace(xml.Attr("returns"))); if (!IsProcedure){ if (xname.EndsWith("__STAR__")){ var tp = xname.Substring(0, xname.Length - 8); ReturnType = GetTableType(model, tp,cls); } else{ string dtype = xname; if (dtype == "function"){ dtype = xml.Attr("returns"); } if (Table.DataTypeMap.ContainsKey(dtype)){ ReturnType = Table.DataTypeMap[dtype]; } else{ ReturnType = new DataType{IsNative = true, SqlText = dtype}; } } } if (xml.GetSmartValue("sql-method").ToBool()){ var sqlm = SqlMethodOptions.IsMethod; var opts = xml.GetSmartValue("sql-method"); if (!string.IsNullOrWhiteSpace(opts)){ sqlm |= opts.To<SqlMethodOptions>(); } SqlMethod = sqlm; } int i = 0; foreach (XAttribute a in xml.Attributes()){ string name = a.Name.LocalName.Unescape(EscapingType.XmlName); string val = a.Value; if (name.StartsWith("@")){ string[] namepair = name.Substring(1).Split('-'); string argname = namepair[0]; SqlFunctionArgument arg; if (!Arguments.ContainsKey(argname)){ Arguments[argname] = new SqlFunctionArgument{Name = argname, DataType = Table.DataTypeMap["string"], Index = i++}; } arg = Arguments[argname]; if (namepair.Length == 1){ //only type determine if (Table.DataTypeMap.ContainsKey(val)){ arg.DataType = Table.DataTypeMap[val]; } else{ arg.DataType = GetIdRefType(model,val,argname,cls); } } else{ bool hastype = false; bool hasdefault = false; for (int j = 1; j < namepair.Length; j++){ string part = namepair[j]; if (part == "default"){ hasdefault = true; } else if (part == "out"){ arg.IsOutput = true; } else if (Table.DataTypeMap.ContainsKey(part)){ arg.DataType = Table.DataTypeMap[part]; hastype = true; } else{ Model.RegisterError(new BSharpError{Message = "unknown arg part " + part, Xml = xml, Class = Table.TargetClass}); } } if ((hastype || hasdefault) && !string.IsNullOrWhiteSpace(val)){ arg.DefaultValue = new DefaultValue{DefaultValueType = DbDefaultValueType.Native, Value = val}; } } } } return this; }
/// <summary> /// </summary> /// <param name="model"></param> /// <param name="cls"></param> /// <param name="bscls"></param> /// <param name="xml"></param> /// <returns></returns> public override SqlObject Setup(PersistentModel model, PersistentClass cls, IBSharpClass bscls, XElement xml) { model = model ?? cls.Model; base.Setup(model, cls, bscls, xml); string xname = xml.Name.LocalName; IsProcedure = xname == "void" || (xname == "function" && string.IsNullOrWhiteSpace(xml.Attr("returns"))); if (!IsProcedure) { if (xname.EndsWith("__STAR__")) { var tp = xname.Substring(0, xname.Length - 8); ReturnType = GetTableType(model, tp, cls); } else { string dtype = xname; if (dtype == "function") { dtype = xml.Attr("returns"); } if (Table.DataTypeMap.ContainsKey(dtype)) { ReturnType = Table.DataTypeMap[dtype]; } else { ReturnType = new DataType { IsNative = true, SqlText = dtype }; } } } if (xml.GetSmartValue("sql-method").ToBool()) { var sqlm = SqlMethodOptions.IsMethod; var opts = xml.GetSmartValue("sql-method"); if (!string.IsNullOrWhiteSpace(opts)) { sqlm |= opts.To <SqlMethodOptions>(); } SqlMethod = sqlm; } int i = 0; foreach (XAttribute a in xml.Attributes()) { string name = a.Name.LocalName.Unescape(EscapingType.XmlName); string val = a.Value; if (name.StartsWith("@")) { string[] namepair = name.Substring(1).Split('-'); string argname = namepair[0]; SqlFunctionArgument arg; if (!Arguments.ContainsKey(argname)) { Arguments[argname] = new SqlFunctionArgument { Name = argname, DataType = Table.DataTypeMap["string"], Index = i++ }; } arg = Arguments[argname]; if (namepair.Length == 1) { //only type determine if (Table.DataTypeMap.ContainsKey(val)) { arg.DataType = Table.DataTypeMap[val]; } else { arg.DataType = GetIdRefType(model, val, argname, cls); } } else { bool hastype = false; bool hasdefault = false; for (int j = 1; j < namepair.Length; j++) { string part = namepair[j]; if (part == "default") { hasdefault = true; } else if (part == "out") { arg.IsOutput = true; } else if (Table.DataTypeMap.ContainsKey(part)) { arg.DataType = Table.DataTypeMap[part]; hastype = true; } else { Model.RegisterError(new BSharpError { Message = "unknown arg part " + part, Xml = xml, Class = Table.TargetClass }); } } if ((hastype || hasdefault) && !string.IsNullOrWhiteSpace(val)) { arg.DefaultValue = new DefaultValue { DefaultValueType = DbDefaultValueType.Native, Value = val }; } } } } return(this); }