/// <summary> /// Armazena os valores do objeto /// </summary> internal void Insert(QObjectBase pObjectBase) { bool first = true; StringBuilder sbQuery = new StringBuilder(); StringBuilder sbValues = new StringBuilder(); sbQuery.AppendLine(string.Format("INSERT INTO {0}.dbo.{1} ", _Project.GetName().Name, pObjectBase.GetType().Name)); sbQuery.AppendLine(" ("); sbValues.Append(" VALUES ("); foreach (PropertyInfo proper in pObjectBase.GetType().GetProperties()) { if (proper.GetCustomAttributes(typeof(NonPersistentAttribute), false).Count() == 0 && !(proper.PropertyType.IsGenericType && proper.PropertyType.GetGenericTypeDefinition().Equals(typeof(QCollection<>)))) { if (first) { sbQuery.AppendLine(proper.Name); sbValues.AppendLine(); if(proper.PropertyType == typeof(string) || proper.PropertyType == typeof(char) || proper.PropertyType == typeof(bool)) sbValues.Append(String.Format("'{0}'", proper.GetValue(pObjectBase, null))); else if (proper.PropertyType == typeof(Guid) || proper.PropertyType.IsSubclassOf(typeof(QObjectBase))) sbValues.Append(String.Format("CONVERT( uniqueidentifier, '{0}')", proper.GetValue(pObjectBase, null))); else sbValues.Append(proper.GetValue(pObjectBase, null)); first = false; } else { sbQuery.Append(", "); sbQuery.AppendLine(proper.Name); if (proper.PropertyType == typeof(string) || proper.PropertyType == typeof(char) || proper.PropertyType == typeof(bool)) { sbValues.Append(", "); sbValues.Append(String.Format("'{0}'", proper.GetValue(pObjectBase, null))); } else if (proper.PropertyType == typeof(Guid) || proper.PropertyType.IsSubclassOf(typeof(QObjectBase))) { sbValues.Append(", "); sbValues.Append(String.Format("CONVERT( uniqueidentifier, '{0}')", proper.GetValue(pObjectBase, null))); } else { sbValues.Append(", "); sbValues.Append(proper.GetValue(pObjectBase, null)); } } } } sbQuery.AppendLine(" )"); sbValues.AppendLine(" )"); sbQuery.AppendLine(sbValues.ToString()); // using (var command = _Connect.CreateCommand()) { command.CommandText = sbQuery.ToString(); command.ExecuteNonQuery(); } }
/// <summary> /// Atualiza os valores do objeto /// </summary> internal void Update(QObjectBase pObjectBase) { bool first = true; StringBuilder sbQuery = new StringBuilder(); sbQuery.AppendLine(string.Format("UPDATE {0}.dbo.{1} ", _Project.GetName().Name, pObjectBase.GetType().Name)); sbQuery.AppendLine(" SET "); foreach (PropertyInfo proper in pObjectBase.GetType().GetProperties()) { if (proper.GetCustomAttributes(typeof(NonPersistentAttribute), false).Count() == 0 && !proper.PropertyType.Equals(typeof(Guid))) { if (first) { sbQuery.AppendLine(proper.Name); sbQuery.Append(" = "); if (proper.PropertyType == typeof(string) || proper.PropertyType == typeof(char)) sbQuery.Append(String.Format("'{0}'", proper.GetValue(pObjectBase, null))); else sbQuery.Append(proper.GetValue(pObjectBase, null)); first = false; } else { sbQuery.Append(","); sbQuery.Append(proper.Name); sbQuery.Append(" = "); if (proper.PropertyType == typeof(string) || proper.PropertyType == typeof(char)) sbQuery.Append(String.Format("'{0}'", proper.GetValue(pObjectBase, null))); else sbQuery.Append(proper.GetValue(pObjectBase, null)); } sbQuery.AppendLine(); } } sbQuery.AppendLine(string.Format(" WHERE OCOD = '{0}'", pObjectBase.OCod)); // Executa a query using (var command = _Connect.CreateCommand()) { command.CommandText = sbQuery.ToString(); command.ExecuteNonQuery(); } }
/// <summary> /// Remove os objetos pesistidos /// </summary> /// <param name="qObjectBase">objeto removido</param> internal void Delete(QObjectBase pObjectBase) { StringBuilder sbQuery = new StringBuilder(); sbQuery.AppendLine(string.Format("DELETE FROM {0}.dbo.{1}", _Project.GetName().Name, pObjectBase.GetType().Name)); sbQuery.AppendLine(string.Format("WHERE OCod = '{0}'", pObjectBase.OCod)); // Cria uma objeto para execcutar a query using (var command = this._Connect.CreateCommand()) { command.CommandText = sbQuery.ToString(); command.ExecuteNonQuery(); } }