Esempio n. 1
0
 /// <summary>
 /// Atualiza os dados to tipo <see cref="IORM{T}"/> que e passado no parâmetro
 /// </summary>
 /// <param name="item">objeto de classe que implementa e interface <see cref="IORM{T}"/></param>
 /// <returns>retorna um valor numérico indicando o resultado da operação</returns>
 public int Update(IORM <T> item)
 {
     return(ConnectionException <int> .AsyncCheck(() => {
         using (var connection = OpenDB()) {
             connection.Open();
             DataBaseLog?.Invoke(this, new ConnectionEventArgs <T>($"atualização de dados: {item.GetThis()}"));
             if (UpdateString == null)
             {
                 UpdateString = item.GetThis().QueryUpdate();
             }
             return connection.Execute(UpdateString, item);
         }
     }));
 }
Esempio n. 2
0
 /// <summary>
 /// Adiciona um elemento do tipo <see cref="IORM{T}"/> no banco de dados
 /// </summary>
 /// <param name="item">objeto de classe que implementa e interface <see cref="IORM{T}"/></param>
 /// <returns>retorna um valor numérico indicando o resultado da operação</returns>
 public int?Add(IORM <T> item)
 {
     return(ConnectionException <int> .AsyncCheck(() => {
         using (var connection = OpenDB()) {
             connection.Open();
             DataBaseLog?.Invoke(this, new ConnectionEventArgs <T>($"inserção do registro: {item.GetThis()}"));
             if (AddString == null)
             {
                 AddString = item.GetThis().QueryAdd();
             }
             return connection.Execute(AddString, item);
         }
     }));
 }
Esempio n. 3
0
 /// <summary>
 /// Verifica se ja existe um tabela do tipo referenciado no banco de dadps
 /// </summary>
 /// <returns>The table.</returns>
 private int CheckTable()
 {
     return(ConnectionException <int> .AsyncCheck(() => {
         using (var connection = OpenDB()) {
             connection.Open();
             try {
                 return connection.Execute($"SELECT *FROM {Activator.CreateInstance<T>().GetType().Name}");
             } catch (Exception) {
                 var instance = Activator.CreateInstance <T>();
                 return connection.Execute(instance.QueryCreateTable());
             }
         }
     }));
 }
Esempio n. 4
0
 /// <summary>
 /// Implementa a verificacao de eventos
 /// </summary>
 private void InitializeLogs()
 {
     this.DataBaseLog += (sender, args) => {
         ConnectionException <int> .AsyncCheck(() => {
             using (var connection = OpenDB()) {
                 connection.Open();
                 string sql = "create table if not exists logs (" +
                              "Id serial unique primary key not null," +
                              "Tabela varchar(128) not null," +
                              "Descricao varchar(128) not null," +
                              "Horario timestamp not null)";
                 connection.Execute(sql);
                 return(connection.Execute("insert into logs(Tabela, Descricao, Horario) values (@Tabela, @Descricao, @Horario)", args));
             }
         });
     };
 }
Esempio n. 5
0
 /// <summary>
 /// Remove um elemento do tipo <see cref="IORM{T}"/> do banco de dados
 /// </summary>
 /// <param name="Id">número de identificação</param>
 /// <returns>retorna um valor numérico indicando o resultado da operação</returns>
 public int Remove(int Id)
 {
     if (!(Activator.CreateInstance <T>() is IORM <T>))
     {
         throw new ArgumentException("class not implement interface IBean");
     }
     return(ConnectionException <int> .AsyncCheck(() => {
         using (var connection = OpenDB()) {
             connection.Open();
             DataBaseLog?.Invoke(this, new ConnectionEventArgs <T>($"remoção do registro: {Id}"));
             if (RemoveString == null)
             {
                 RemoveString = Activator.CreateInstance <T>().QueryRemove();
             }
             return connection.Execute(RemoveString, new { id = Id });
         }
     }));
 }
Esempio n. 6
0
 /// <summary>
 /// Pesquisa um elemento do tipo <see cref="IORM{T}"/> no banco de dados com base em seu Id
 /// </summary>
 /// <param name="Id">número de indentificação</param>
 /// <returns>retorna um tipo <see cref="IORM{T}"/></returns>
 public T FindById(int Id)
 {
     return(ConnectionException <T> .AsyncCheck((Func <T>)(() => {
         if (!(Activator.CreateInstance <T>() is IORM <T>))
         {
             throw new ArgumentException("class not implement interface IBean");
         }
         using (var connection = OpenDB()) {
             connection.Open();
             DataBaseLog?.Invoke(this, new ConnectionEventArgs <T>($"registro pesquisado: {Id}"));
             if (FindByIdString == null)
             {
                 FindByIdString = Activator.CreateInstance <T>().QueryFindById();
             }
             return ReflectionConvert(connection.Query(FindByIdString, new { id = Id }).FirstOrDefault());
         }
     })));
 }
Esempio n. 7
0
 /// <summary>
 /// Seleciona todos elementos do tipo <see cref="IORM{T}"/> no banco de dados
 /// </summary>
 /// <returns>retorna um lista de elementos <see cref="IORM{T}"/></returns>
 public List <T> FindAll()
 {
     return(ConnectionException <T> .AsyncCheck(() => {
         if (!(Activator.CreateInstance <T>() is IORM <T>))
         {
             throw new ArgumentException("class not implement interface IBean");
         }
         using (var connection = OpenDB()) {
             connection.Open();
             DataBaseLog?.Invoke(this, new ConnectionEventArgs <T>("listagem de dados"));
             if (FindAllString == null)
             {
                 FindAllString = Activator.CreateInstance <T>().QueryFindAll();
             }
             return ReflectionConvertList(connection.Query(FindAllString));
         }
     }));
 }