|
|
|
@ -0,0 +1,69 @@
|
|
|
|
|
using Dto;
|
|
|
|
|
using Entities;
|
|
|
|
|
using Model.OrderCriteria;
|
|
|
|
|
using Npgsql;
|
|
|
|
|
using Shared;
|
|
|
|
|
using Shared.Mapper;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
|
|
|
|
|
|
namespace API.Service;
|
|
|
|
|
|
|
|
|
|
public class QueryDataServiceApi : IQueryService<QueryDto>
|
|
|
|
|
{
|
|
|
|
|
public QueryDto ExecuteQuery(string query, string database)
|
|
|
|
|
{
|
|
|
|
|
string connectionString =
|
|
|
|
|
$"Host=localhost;Username=admin;Password=motdepasse;Database={database}";
|
|
|
|
|
|
|
|
|
|
if (string.IsNullOrEmpty(database))
|
|
|
|
|
{
|
|
|
|
|
return new QueryDto { Result = "Le nom de la base de données est requis." };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
using (NpgsqlConnection connection = new NpgsqlConnection(connectionString))
|
|
|
|
|
{
|
|
|
|
|
connection.Open();
|
|
|
|
|
using (NpgsqlCommand command = new NpgsqlCommand(query, connection))
|
|
|
|
|
{
|
|
|
|
|
using (NpgsqlDataReader reader = command.ExecuteReader())
|
|
|
|
|
{
|
|
|
|
|
List<Dictionary<string, object>> resultList = new List<Dictionary<string, object>>();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
List<string> columnNames = new List<string>();
|
|
|
|
|
for (int i = 0; i < reader.FieldCount; i++)
|
|
|
|
|
{
|
|
|
|
|
columnNames.Add(reader.GetName(i));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
while (reader.Read())
|
|
|
|
|
{
|
|
|
|
|
Dictionary<string, object> row = new Dictionary<string, object>();
|
|
|
|
|
for (int i = 0; i < reader.FieldCount; i++)
|
|
|
|
|
{
|
|
|
|
|
row[columnNames[i]] = reader[i].ToString();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
resultList.Add(row);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
string resultJson = JsonConvert.SerializeObject(resultList);
|
|
|
|
|
|
|
|
|
|
QueryDto queryDto = new QueryDto { Result = resultJson };
|
|
|
|
|
return queryDto;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public IEnumerable<string> GetTables(string database)
|
|
|
|
|
{
|
|
|
|
|
throw new NotImplementedException();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public IEnumerable<string> GetColumns(string database, string table)
|
|
|
|
|
{
|
|
|
|
|
throw new NotImplementedException();
|
|
|
|
|
}
|
|
|
|
|
}
|