|
|
@ -6,6 +6,7 @@ using Shared;
|
|
|
|
using Shared.Mapper;
|
|
|
|
using Shared.Mapper;
|
|
|
|
using System.Text;
|
|
|
|
using System.Text;
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
|
|
|
|
using Microsoft.EntityFrameworkCore.Metadata.Internal;
|
|
|
|
|
|
|
|
|
|
|
|
namespace API.Service;
|
|
|
|
namespace API.Service;
|
|
|
|
|
|
|
|
|
|
|
@ -21,49 +22,150 @@ public class QueryDataServiceApi : IQueryService<QueryDto>
|
|
|
|
return new QueryDto { Result = "Le nom de la base de données est requis." };
|
|
|
|
return new QueryDto { Result = "Le nom de la base de données est requis." };
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
using (NpgsqlConnection connection = new NpgsqlConnection(connectionString))
|
|
|
|
try
|
|
|
|
{
|
|
|
|
{
|
|
|
|
connection.Open();
|
|
|
|
using (NpgsqlConnection connection = new NpgsqlConnection(connectionString))
|
|
|
|
using (NpgsqlCommand command = new NpgsqlCommand(query, connection))
|
|
|
|
|
|
|
|
{
|
|
|
|
{
|
|
|
|
using (NpgsqlDataReader reader = command.ExecuteReader())
|
|
|
|
connection.Open();
|
|
|
|
|
|
|
|
using (NpgsqlCommand command = new NpgsqlCommand(query, connection))
|
|
|
|
{
|
|
|
|
{
|
|
|
|
List<Dictionary<string, object>> resultList = new List<Dictionary<string, object>>();
|
|
|
|
using (NpgsqlDataReader reader = command.ExecuteReader())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
List<string> columnNames = new List<string>();
|
|
|
|
|
|
|
|
for (int i = 0; i < reader.FieldCount; i++)
|
|
|
|
|
|
|
|
{
|
|
|
|
{
|
|
|
|
columnNames.Add(reader.GetName(i));
|
|
|
|
List<Dictionary<string, object>> resultList = new List<Dictionary<string, object>>();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
while (reader.Read())
|
|
|
|
|
|
|
|
{
|
|
|
|
List<string> columnNames = new List<string>();
|
|
|
|
Dictionary<string, object> row = new Dictionary<string, object>();
|
|
|
|
|
|
|
|
for (int i = 0; i < reader.FieldCount; i++)
|
|
|
|
for (int i = 0; i < reader.FieldCount; i++)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
row[columnNames[i]] = reader[i].ToString();
|
|
|
|
columnNames.Add(reader.GetName(i));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
resultList.Add(row);
|
|
|
|
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);
|
|
|
|
string resultJson = JsonConvert.SerializeObject(resultList);
|
|
|
|
|
|
|
|
|
|
|
|
QueryDto queryDto = new QueryDto { Result = resultJson };
|
|
|
|
QueryDto queryDto = new QueryDto { Result = resultJson };
|
|
|
|
return queryDto;
|
|
|
|
return queryDto;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
return new QueryDto { Result = ex.Message };
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public IEnumerable<string> GetTables(string database)
|
|
|
|
public QueryDto GetTables(string database)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
throw new NotImplementedException();
|
|
|
|
string connectionString =
|
|
|
|
|
|
|
|
$"Host=localhost;Username=admin;Password=motdepasse;Database={database}";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
using (NpgsqlConnection connection = new NpgsqlConnection(connectionString))
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
connection.Open();
|
|
|
|
|
|
|
|
using (NpgsqlCommand command = new NpgsqlCommand())
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
command.Connection = connection;
|
|
|
|
|
|
|
|
// Donner et exclure les bonnes permissions au rôle en question
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// GRANT SELECT ON TABLE information_schema.tables TO votre_utilisateur;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// GRANT SELECT ON TABLE information_schema.columns TO votre_utilisateur;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// REVOKE ALL ON SCHEMA information_schema FROM PUBLIC;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
command.CommandText =
|
|
|
|
|
|
|
|
"SELECT table_name FROM information_schema.tables WHERE table_schema = 'public';";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
using (NpgsqlDataReader reader = command.ExecuteReader())
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
List<string> tableNames = new List<string>();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
while (reader.Read())
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
tableNames.Add(reader["table_name"].ToString());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Dictionary<string, string> tablesDict = new Dictionary<string, string>();
|
|
|
|
|
|
|
|
foreach (string tableName in tableNames)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
tablesDict[tableName] = tableName;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
string tablesJson = JsonConvert.SerializeObject(tablesDict);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
QueryDto queryDto = new QueryDto { Result = tablesJson };
|
|
|
|
|
|
|
|
return queryDto;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
return new QueryDto { Result = ex.Message };
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public IEnumerable<string> GetColumns(string database, string table)
|
|
|
|
public QueryDto GetColumns(string database, string table)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
throw new NotImplementedException();
|
|
|
|
string connectionString =
|
|
|
|
|
|
|
|
$"Host=localhost;Username=admin;Password=motdepasse;Database={database}";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
using (NpgsqlConnection connection = new NpgsqlConnection(connectionString))
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
connection.Open();
|
|
|
|
|
|
|
|
using (NpgsqlCommand command = new NpgsqlCommand())
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
command.Connection = connection;
|
|
|
|
|
|
|
|
// Donner et exclure les bonnes permissions au rôle en question
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//GRANT SELECT ON TABLE information_schema.tables TO votre_utilisateur;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//GRANT SELECT ON TABLE information_schema.columns TO votre_utilisateur;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//REVOKE ALL ON SCHEMA information_schema FROM PUBLIC;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
command.CommandText =
|
|
|
|
|
|
|
|
$"SELECT column_name FROM information_schema.columns WHERE table_name = '{table}';";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Console.WriteLine(command.CommandText);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
using (NpgsqlDataReader reader = command.ExecuteReader())
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
List<string> columnsNames = new List<string>();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
while (reader.Read())
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
columnsNames.Add(reader["column_name"].ToString());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Dictionary<string, string> columnsDict = new Dictionary<string, string>();
|
|
|
|
|
|
|
|
foreach (string colName in columnsNames)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
columnsDict[colName] = colName;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
string tablesJson = JsonConvert.SerializeObject(columnsDict);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Console.WriteLine(tablesJson);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
QueryDto queryDto = new QueryDto { Result = tablesJson };
|
|
|
|
|
|
|
|
return queryDto;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|