public string QuerySelectEntity(TEntity obj) { Dictionary <string, string> lista = new Dictionary <string, string>(); FieldsAttribute flAttribute = null; TablesAttribute tbAttribute = null; foreach (PropertyInfo info in obj.GetType().GetProperties()) { foreach (object field in info.GetCustomAttributes(true)) { if (field is FieldsAttribute) { flAttribute = field as FieldsAttribute; lista.Add("U_" + flAttribute.Nome, flAttribute.Descricao); } } } string campos = ""; int controle = 1; foreach (KeyValuePair <string, string> objetos in lista) { if (controle == 1) { campos = objetos.Key + " as '" + objetos.Value + "'"; } else { campos = campos + "," + objetos.Key + " as '" + objetos.Value + "'"; } controle++; } string nomeTabela = ""; foreach (object customAttribute in obj.GetType().GetCustomAttributes(true)) { if (customAttribute is TablesAttribute) { tbAttribute = customAttribute as TablesAttribute; nomeTabela = tbAttribute.Nome; } } return(string.Format("select Code, Name, {0} from [@{1}]", campos, nomeTabela)); }
protected TableBase() { TableId = new List <string>(); TablesAttribute attribute = null; FieldsAttribute flAttribute = null; UdoAttribute udoAttribute = null; UdoChildAttribute udoChildAttribute = null; ValidValuesAttribute valoresValidos = null; foreach (object obj2 in base.GetType().GetCustomAttributes(false)) { #region Atributo Tabelas if (obj2 is TablesAttribute) { attribute = obj2 as TablesAttribute; if (!string.IsNullOrEmpty(attribute.Nome)) { Tabelas tb = new Tabelas(); tb.NomeTabela = attribute.Nome; tb.DescricaoTabela = attribute.Descricao; tb.TipoTabelaSap = attribute.Tipo; tb.Campos = new List <Campos>(); tb.Ttabela = attribute.TabelaSistema ? Tipos.TipoTabela.Sap : Tipos.TipoTabela.Usuario; foreach (PropertyInfo info in this.GetType().GetProperties()) { List <ValoresValidos> vlrs = new List <ValoresValidos>(); Campos cp = new Campos(); foreach (object field in info.GetCustomAttributes(true)) { if (field is FieldsAttribute) { flAttribute = field as FieldsAttribute; RelationalReader.verificaTipos(cp, info, flAttribute, tb.NomeTabela); } if (field is ValidValuesAttribute) { valoresValidos = field as ValidValuesAttribute; vlrs.Add(new ValoresValidos() { Descricao = valoresValidos.Descricao, Valor = valoresValidos.Valor }); } } if (!string.IsNullOrEmpty(cp.NomeCampo)) { if (vlrs.Count > 0) { cp.ValoresValidos = vlrs; } tb.Campos.Add(cp); } } B1AppDomain.RegisterTable(this, tb); } } #endregion #region Atributo Udo if (obj2 is UdoAttribute) { udoAttribute = obj2 as UdoAttribute; if (!string.IsNullOrEmpty(udoAttribute.Code)) { Udo ud = new Udo(); ud.TableName = udoAttribute.TableName; ud.Name = udoAttribute.Name; ud.Code = udoAttribute.Code; ud.Cancel = udoAttribute.Cancel; ud.Close = udoAttribute.Close; ud.CreateDefaultForm = udoAttribute.CreateDefaultForm; ud.Delete = udoAttribute.Delete; ud.Find = udoAttribute.Find; ud.YearTransfer = udoAttribute.YearTransfer; ud.ManageSeries = udoAttribute.ManageSeries; ud.ObjectType = udoAttribute.ObjectType; ud.Formulario = udoAttribute.Formulario; ud.EnableEnhancedform = udoAttribute.EnableEnhancedform; ud.RebuildEnhancedForm = udoAttribute.RebuildEnhancedForm; B1AppDomain.RegisterUdo(this, ud); } } #endregion #region Atributo UdoFilhos if (obj2 is UdoChildAttribute) { udoChildAttribute = obj2 as UdoChildAttribute; if (!string.IsNullOrEmpty(udoChildAttribute.TableName)) { UdoFilhos udf = new UdoFilhos(); udf.TableName = udoChildAttribute.TableName; udf.TabelaPai = udoChildAttribute.TabelaPai; B1AppDomain.RegisterUdoChild(this, udf); } } #endregion } if (attribute == null) { B1Exception.writeLog("Falha ao indexar Tabela. Por favor checar os atributos informados"); } }
//Configura tipo SAP baseado no tipo modelo. internal static void verificaTipos(Campos campo, PropertyInfo info, FieldsAttribute attribute, string nomeTabela) { #region Dados dos Atributos campo.DescricaoCampo = attribute.Descricao; campo.Mandatory = attribute.Obrigatorio ? BoYesNoEnum.tYES : BoYesNoEnum.tNO; campo.NomeCampo = attribute.Nome; campo.NomeTabela = nomeTabela; campo.UdoReferencia = attribute.LinkUdo; #endregion if (info.PropertyType == typeof(System.String)) { switch (attribute.SubTipo) { case BoFldSubTypes.st_None: if (attribute.Tamanho > 254) { campo.TipoCampo = BoFieldTypes.db_Memo; campo.SubTipos = BoFldSubTypes.st_None; } else { campo.TipoCampo = BoFieldTypes.db_Alpha; campo.SubTipos = BoFldSubTypes.st_None; campo.Tamanho = attribute.Tamanho; } break; case BoFldSubTypes.st_Address: campo.TipoCampo = BoFieldTypes.db_Alpha; campo.SubTipos = BoFldSubTypes.st_Address; break; case BoFldSubTypes.st_Phone: campo.TipoCampo = BoFieldTypes.db_Alpha; campo.SubTipos = BoFldSubTypes.st_Phone; break; case BoFldSubTypes.st_Image: campo.TipoCampo = BoFieldTypes.db_Alpha; campo.SubTipos = BoFldSubTypes.st_Image; break; default: B1Exception.writeLog(string.Format(mensagemErro, info.PropertyType.ToString(), attribute.Nome, attribute.TypeId)); break; } } else if (info.PropertyType == typeof(System.Int32) || info.PropertyType == typeof(System.Int16) || info.PropertyType == typeof(System.Int64)) { switch (attribute.SubTipo) { case BoFldSubTypes.st_None: campo.TipoCampo = BoFieldTypes.db_Numeric; campo.SubTipos = BoFldSubTypes.st_None; campo.Tamanho = attribute.Tamanho; break; case BoFldSubTypes.st_Time: campo.TipoCampo = BoFieldTypes.db_Date; campo.SubTipos = BoFldSubTypes.st_Time; break; default: B1Exception.writeLog(string.Format(mensagemErro, info.PropertyType.ToString(), attribute.Nome, attribute.TypeId)); break; } } else if (info.PropertyType == typeof(System.DateTime)) { campo.TipoCampo = BoFieldTypes.db_Date; campo.SubTipos = BoFldSubTypes.st_None; } else if (info.PropertyType == typeof(System.Double)) { switch (attribute.SubTipo) { case BoFldSubTypes.st_None: campo.TipoCampo = BoFieldTypes.db_Float; campo.SubTipos = BoFldSubTypes.st_Sum; break; case BoFldSubTypes.st_Percentage: campo.TipoCampo = BoFieldTypes.db_Float; campo.SubTipos = BoFldSubTypes.st_Percentage; break; case BoFldSubTypes.st_Measurement: campo.TipoCampo = BoFieldTypes.db_Float; campo.SubTipos = BoFldSubTypes.st_Measurement; break; case BoFldSubTypes.st_Price: campo.TipoCampo = BoFieldTypes.db_Float; campo.SubTipos = BoFldSubTypes.st_Price; break; case BoFldSubTypes.st_Quantity: campo.TipoCampo = BoFieldTypes.db_Float; campo.SubTipos = BoFldSubTypes.st_Quantity; break; case BoFldSubTypes.st_Rate: campo.TipoCampo = BoFieldTypes.db_Float; campo.SubTipos = BoFldSubTypes.st_Rate; break; case BoFldSubTypes.st_Sum: campo.TipoCampo = BoFieldTypes.db_Float; campo.SubTipos = BoFldSubTypes.st_Sum; break; default: B1Exception.writeLog(string.Format(mensagemErro, info.PropertyType.ToString(), attribute.Nome, attribute.TypeId)); break; } } else if (info.PropertyType == typeof(System.Decimal)) { switch (attribute.SubTipo) { case BoFldSubTypes.st_None: campo.TipoCampo = BoFieldTypes.db_Float; campo.SubTipos = BoFldSubTypes.st_Sum; break; case BoFldSubTypes.st_Percentage: campo.TipoCampo = BoFieldTypes.db_Float; campo.SubTipos = BoFldSubTypes.st_Percentage; break; case BoFldSubTypes.st_Measurement: campo.TipoCampo = BoFieldTypes.db_Float; campo.SubTipos = BoFldSubTypes.st_Measurement; break; case BoFldSubTypes.st_Price: campo.TipoCampo = BoFieldTypes.db_Float; campo.SubTipos = BoFldSubTypes.st_Price; break; case BoFldSubTypes.st_Quantity: campo.TipoCampo = BoFieldTypes.db_Float; campo.SubTipos = BoFldSubTypes.st_Quantity; break; case BoFldSubTypes.st_Rate: campo.TipoCampo = BoFieldTypes.db_Float; campo.SubTipos = BoFldSubTypes.st_Rate; break; case BoFldSubTypes.st_Sum: campo.TipoCampo = BoFieldTypes.db_Float; campo.SubTipos = BoFldSubTypes.st_Sum; break; default: B1Exception.writeLog(string.Format(mensagemErro, info.PropertyType.ToString(), attribute.Nome, attribute.TypeId)); break; } } }
public void Add(TEntity obj) { Dictionary <string, object> lista = new Dictionary <string, object>(); FieldsAttribute flAttribute = null; TablesAttribute tbAttribute = null; foreach (PropertyInfo info in obj.GetType().GetProperties()) { foreach (object field in info.GetCustomAttributes(true)) { if (field is FieldsAttribute) { flAttribute = field as FieldsAttribute; if (info.GetValue(obj) != null) { if (info.GetValue(obj).GetType() == typeof(DateTime)) { lista.Add("U_" + flAttribute.Nome, ((DateTime)info.GetValue(obj)).ToString("yyyyMMdd")); } else { lista.Add("U_" + flAttribute.Nome, info.GetValue(obj)); } } } } } string campos = ""; string valores = ""; int controle = 1; foreach (KeyValuePair <string, object> objetos in lista) { if (controle == 1) { campos = objetos.Key; valores = "'" + objetos.Value + "'"; } else { campos = campos + "," + objetos.Key; valores = valores + "," + "'" + objetos.Value + "'"; } controle++; } string nomeTabela = ""; foreach (object customAttribute in obj.GetType().GetCustomAttributes(true)) { if (customAttribute is TablesAttribute) { tbAttribute = customAttribute as TablesAttribute; nomeTabela = tbAttribute.Nome; } } string key = Guid.NewGuid().ToString().Substring(0, 30); string query = string.Format(@"insert into [@{0}] (Code, Name, {1}) values ('{3}','{3}',{2})", nomeTabela, campos, valores, key); B1AppDomain.RSQuery(query); }
public void Update(TEntity obj) { Dictionary <string, object> lista = new Dictionary <string, object>(); FieldsAttribute flAttribute = null; TablesAttribute tbAttribute = null; string key = ""; foreach (PropertyInfo info in obj.GetType().GetProperties()) { if (info.Name == "Code") { key = info.GetValue(obj).ToString(); } foreach (object field in info.GetCustomAttributes(true)) { if (field is FieldsAttribute) { flAttribute = field as FieldsAttribute; if (info.GetValue(obj) != null) { if (info.GetValue(obj).GetType() == typeof(DateTime)) { lista.Add("U_" + flAttribute.Nome, ((DateTime)info.GetValue(obj)).ToString("yyyyMMdd")); } else { lista.Add("U_" + flAttribute.Nome, info.GetValue(obj)); } } } } } string campos = ""; int controle = 1; foreach (KeyValuePair <string, object> objetos in lista) { if (controle == 1) { campos = objetos.Key + " = '" + objetos.Value + "'"; } else { campos = campos + ", " + objetos.Key + " = '" + objetos.Value + "'"; } controle++; } string nomeTabela = ""; foreach (object customAttribute in obj.GetType().GetCustomAttributes(true)) { if (customAttribute is TablesAttribute) { tbAttribute = customAttribute as TablesAttribute; nomeTabela = tbAttribute.Nome; } } string query = string.Format(@"update [@{0}] set {1} where Code = '{2}'", nomeTabela, campos, key); B1AppDomain.RSQuery(query); }
/// <summary> /// busca todos usando filtros and /// </summary> /// <param name="filtros">campo, valor </param> /// <returns></returns> public List <TEntity> GetAll(Dictionary <string, object> filtros) { if (filtros == null) { return(null); } TEntity obj = new TEntity(); Dictionary <string, object> lista = new Dictionary <string, object>(); FieldsAttribute flAttribute = null; TablesAttribute tbAttribute = null; foreach (PropertyInfo info in obj.GetType().GetProperties()) { foreach (object field in info.GetCustomAttributes(true)) { if (field is FieldsAttribute) { flAttribute = field as FieldsAttribute; lista.Add("U_" + flAttribute.Nome, info.GetValue(obj)); } } } string campos = ""; int controle = 1; foreach (KeyValuePair <string, object> objetos in lista) { if (controle == 1) { campos = objetos.Key; } else { campos = campos + "," + objetos.Key; } controle++; } string nomeTabela = ""; foreach (object customAttribute in obj.GetType().GetCustomAttributes(true)) { if (customAttribute is TablesAttribute) { tbAttribute = customAttribute as TablesAttribute; nomeTabela = tbAttribute.Nome; } } string where = ""; controle = 1; foreach (KeyValuePair <string, object> filtro in filtros) { if (controle == 1) { where = filtro.Key + " = '" + filtro.Value + "'"; } else { where = where + " and " + filtro.Key + " = '" + filtro.Value + "'"; } } string query = string.Format("select Code, Name, {0} from [@{1}] where {2}", campos, nomeTabela, where); var oRs = B1AppDomain.RSQuery(query); List <TEntity> resultado = new List <TEntity>(); while (!oRs.EoF) { TEntity obj2 = new TEntity(); foreach (PropertyInfo info in obj2.GetType().GetProperties()) { foreach (object field in info.GetCustomAttributes(true)) { if (field is FieldsAttribute) { flAttribute = field as FieldsAttribute; info.SetValue(obj2, oRs.Fields.Item("U_" + flAttribute.Nome).Value); } } } resultado.Add(obj2); oRs.MoveNext(); } return(resultado); }
public TEntity GetByEntity(TEntity obj) { Dictionary <string, object> lista = new Dictionary <string, object>(); FieldsAttribute flAttribute = null; TablesAttribute tbAttribute = null; foreach (PropertyInfo info in obj.GetType().GetProperties()) { foreach (object field in info.GetCustomAttributes(true)) { if (field is FieldsAttribute) { flAttribute = field as FieldsAttribute; lista.Add("U_" + flAttribute.Nome, info.GetValue(obj)); } } } string campos = ""; string where = ""; int controle = 1; foreach (KeyValuePair <string, object> objetos in lista) { if (controle == 1) { campos = objetos.Key; where = objetos.Key + " = '" + objetos.Value.ToString() + "'"; } else { campos = campos + "," + objetos.Key; if (objetos.Value != null) { if (objetos.Value.ToString().Length < 254) { if (objetos.Value.GetType() == typeof(DateTime)) { DateTime datavalor = Convert.ToDateTime(objetos.Value); DateTime datavazia = Convert.ToDateTime("01/01/0001 00:00:00"); if (datavalor > datavazia) { where = where + " and " + objetos.Key + " = '" + Convert.ToDateTime(objetos.Value).ToString("yyyyMMdd") + "'"; } } else if (objetos.Value.GetType() == typeof(int)) { if (Convert.ToInt32(objetos.Value) != 0) { where = where + " and " + objetos.Key + " = '" + objetos.Value.ToString() + "'"; } } else { where = where + " and " + objetos.Key + " = '" + objetos.Value.ToString() + "'"; } } } } controle++; } string nomeTabela = ""; foreach (object customAttribute in obj.GetType().GetCustomAttributes(true)) { if (customAttribute is TablesAttribute) { tbAttribute = customAttribute as TablesAttribute; nomeTabela = tbAttribute.Nome; } } string query = string.Format("select Code, Name, {0} from [@{1}] where {2}", campos, nomeTabela, where); var oRs = B1AppDomain.RSQuery(query); foreach (PropertyInfo info in obj.GetType().GetProperties()) { if (info.Name == "Code") { info.SetValue(obj, oRs.Fields.Item("Code").Value); } else if (info.Name == "Name") { info.SetValue(obj, oRs.Fields.Item("Name").Value); } foreach (object field in info.GetCustomAttributes(true)) { if (field is FieldsAttribute) { flAttribute = field as FieldsAttribute; info.SetValue(obj, oRs.Fields.Item("U_" + flAttribute.Nome).Value); } } } return(obj); }