2020-05-28 13:28:51 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Configuration;
|
|
|
|
|
using System.Data;
|
|
|
|
|
using System.Data.Common;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Xml.Linq;
|
|
|
|
|
using System.Xml.XPath;
|
2020-08-27 14:01:37 +00:00
|
|
|
|
|
2020-05-28 13:28:51 +00:00
|
|
|
|
using ASC.Common;
|
|
|
|
|
using ASC.Common.Logging;
|
2021-04-16 13:26:26 +00:00
|
|
|
|
using ASC.Common.Utils;
|
2021-08-12 15:18:01 +00:00
|
|
|
|
using ASC.Core.Common.EF;
|
|
|
|
|
using ASC.Core.Common.EF.Context;
|
2020-08-27 14:01:37 +00:00
|
|
|
|
|
2020-05-28 13:28:51 +00:00
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
|
using Microsoft.Extensions.Options;
|
|
|
|
|
|
|
|
|
|
namespace ASC.Data.Backup
|
2020-10-28 20:02:03 +00:00
|
|
|
|
{
|
|
|
|
|
[Scope]
|
2020-05-28 13:28:51 +00:00
|
|
|
|
public class DbHelper : IDisposable
|
|
|
|
|
{
|
|
|
|
|
private readonly DbProviderFactory factory;
|
|
|
|
|
private readonly DbConnection connect;
|
|
|
|
|
private readonly DbCommandBuilder builder;
|
|
|
|
|
private readonly DataTable columns;
|
|
|
|
|
private readonly bool mysql;
|
|
|
|
|
private readonly IDictionary<string, string> whereExceptions = new Dictionary<string, string>();
|
2021-12-28 13:08:11 +00:00
|
|
|
|
private readonly ILog log;
|
2021-08-12 15:18:01 +00:00
|
|
|
|
private readonly TenantDbContext tenantDbContext;
|
|
|
|
|
private readonly CoreDbContext coreDbContext;
|
2020-05-28 13:28:51 +00:00
|
|
|
|
|
2021-12-28 13:08:11 +00:00
|
|
|
|
public DbHelper(IOptionsMonitor<ILog> options, ConnectionStringSettings connectionString, TenantDbContext tenantDbContext, CoreDbContext coreDbContext)
|
2020-05-28 13:28:51 +00:00
|
|
|
|
{
|
2021-12-28 13:08:11 +00:00
|
|
|
|
log = options.CurrentValue;
|
2021-08-12 15:18:01 +00:00
|
|
|
|
this.tenantDbContext = tenantDbContext;
|
|
|
|
|
this.coreDbContext = coreDbContext;
|
2020-05-28 13:28:51 +00:00
|
|
|
|
var file = connectionString.ElementInformation.Source;
|
|
|
|
|
if ("web.connections.config".Equals(Path.GetFileName(file), StringComparison.InvariantCultureIgnoreCase))
|
|
|
|
|
{
|
2021-04-16 13:26:26 +00:00
|
|
|
|
file = CrossPlatform.PathCombine(Path.GetDirectoryName(file), "Web.config");
|
2020-05-28 13:28:51 +00:00
|
|
|
|
}
|
|
|
|
|
var xconfig = XDocument.Load(file);
|
|
|
|
|
var provider = xconfig.XPathSelectElement("/configuration/system.data/DbProviderFactories/add[@invariant='" + connectionString.ProviderName + "']");
|
|
|
|
|
factory = (DbProviderFactory)Activator.CreateInstance(Type.GetType(provider.Attribute("type").Value, true));
|
|
|
|
|
builder = factory.CreateCommandBuilder();
|
|
|
|
|
connect = factory.CreateConnection();
|
|
|
|
|
connect.ConnectionString = connectionString.ConnectionString;
|
|
|
|
|
connect.Open();
|
|
|
|
|
|
2022-01-12 15:42:03 +00:00
|
|
|
|
mysql = connectionString.ProviderName.Contains("mysql", StringComparison.OrdinalIgnoreCase);
|
2020-05-28 13:28:51 +00:00
|
|
|
|
if (mysql)
|
|
|
|
|
{
|
2020-10-12 19:39:23 +00:00
|
|
|
|
CreateCommand("set @@session.sql_mode = concat(@@session.sql_mode, ',NO_AUTO_VALUE_ON_ZERO')").ExecuteNonQuery();
|
2020-05-28 13:28:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
columns = connect.GetSchema("Columns");
|
|
|
|
|
|
|
|
|
|
whereExceptions["calendar_calendar_item"] = " where calendar_id in (select id from calendar_calendars where tenant = {0}) ";
|
|
|
|
|
whereExceptions["calendar_calendar_user"] = " where calendar_id in (select id from calendar_calendars where tenant = {0}) ";
|
|
|
|
|
whereExceptions["calendar_event_item"] = " inner join calendar_events on calendar_event_item.event_id = calendar_events.id where calendar_events.tenant = {0} ";
|
|
|
|
|
whereExceptions["calendar_event_user"] = " inner join calendar_events on calendar_event_user.event_id = calendar_events.id where calendar_events.tenant = {0} ";
|
|
|
|
|
whereExceptions["crm_entity_contact"] = " inner join crm_contact on crm_entity_contact.contact_id = crm_contact.id where crm_contact.tenant_id = {0} ";
|
|
|
|
|
whereExceptions["crm_entity_tag"] = " inner join crm_tag on crm_entity_tag.tag_id = crm_tag.id where crm_tag.tenant_id = {0} ";
|
|
|
|
|
whereExceptions["files_folder_tree"] = " inner join files_folder on folder_id = id where tenant_id = {0} ";
|
|
|
|
|
whereExceptions["forum_answer_variant"] = " where answer_id in (select id from forum_answer where tenantid = {0})";
|
|
|
|
|
whereExceptions["forum_topic_tag"] = " where topic_id in (select id from forum_topic where tenantid = {0})";
|
|
|
|
|
whereExceptions["forum_variant"] = " where question_id in (select id from forum_question where tenantid = {0})";
|
|
|
|
|
whereExceptions["projects_project_participant"] = " inner join projects_projects on projects_project_participant.project_id = projects_projects.id where projects_projects.tenant_id = {0} ";
|
|
|
|
|
whereExceptions["projects_following_project_participant"] = " inner join projects_projects on projects_following_project_participant.project_id = projects_projects.id where projects_projects.tenant_id = {0} ";
|
|
|
|
|
whereExceptions["projects_project_tag"] = " inner join projects_projects on projects_project_tag.project_id = projects_projects.id where projects_projects.tenant_id = {0} ";
|
|
|
|
|
whereExceptions["tenants_tenants"] = " where id = {0}";
|
|
|
|
|
whereExceptions["core_acl"] = " where tenant = {0} or tenant = -1";
|
|
|
|
|
whereExceptions["core_subscription"] = " where tenant = {0} or tenant = -1";
|
|
|
|
|
whereExceptions["core_subscriptionmethod"] = " where tenant = {0} or tenant = -1";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<string> GetTables()
|
|
|
|
|
{
|
|
|
|
|
var allowTables = new List<string>
|
|
|
|
|
{
|
|
|
|
|
"blogs_",
|
|
|
|
|
"bookmarking_",
|
|
|
|
|
"calendar_",
|
|
|
|
|
"core_",
|
|
|
|
|
"crm_",
|
|
|
|
|
"events_",
|
|
|
|
|
"files_",
|
|
|
|
|
"forum_",
|
|
|
|
|
"photo_",
|
|
|
|
|
"projects_",
|
|
|
|
|
"tenants_",
|
|
|
|
|
"webstudio_",
|
|
|
|
|
"wiki_",
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var disallowTables = new List<string>
|
|
|
|
|
{
|
|
|
|
|
"core_settings",
|
|
|
|
|
"webstudio_uservisit",
|
|
|
|
|
"webstudio_useractivity",
|
|
|
|
|
"tenants_forbiden",
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
IEnumerable<string> tables;
|
|
|
|
|
|
|
|
|
|
if (mysql)
|
|
|
|
|
{
|
|
|
|
|
tables = ExecuteList(CreateCommand("show tables"));
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
tables = connect
|
|
|
|
|
.GetSchema("Tables")
|
|
|
|
|
.Select(@"TABLE_TYPE <> 'SYSTEM_TABLE'")
|
2022-01-21 13:46:30 +00:00
|
|
|
|
.Select(row => (string)row["TABLE_NAME"]);
|
2020-05-28 13:28:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return tables
|
|
|
|
|
.Where(t => allowTables.Any(a => t.StartsWith(a)) && !disallowTables.Any(d => t.StartsWith(d)))
|
|
|
|
|
.ToList();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public DataTable GetTable(string table, int tenant)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var dataTable = new DataTable(table);
|
|
|
|
|
var adapter = factory.CreateDataAdapter();
|
|
|
|
|
adapter.SelectCommand = CreateCommand("select " + Quote(table) + ".* from " + Quote(table) + GetWhere(table, tenant));
|
|
|
|
|
|
|
|
|
|
log.Debug(adapter.SelectCommand.CommandText);
|
|
|
|
|
|
2020-10-12 19:39:23 +00:00
|
|
|
|
adapter.Fill(dataTable);
|
2020-05-28 13:28:51 +00:00
|
|
|
|
return dataTable;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception error)
|
|
|
|
|
{
|
|
|
|
|
log.ErrorFormat("Table {0}: {1}", table, error);
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void SetTable(DataTable table)
|
|
|
|
|
{
|
2020-09-29 10:28:03 +00:00
|
|
|
|
using var tx = connect.BeginTransaction();
|
|
|
|
|
try
|
2020-05-28 13:28:51 +00:00
|
|
|
|
{
|
2020-09-29 10:28:03 +00:00
|
|
|
|
if ("tenants_tenants".Equals(table.TableName, StringComparison.InvariantCultureIgnoreCase))
|
2020-05-28 13:28:51 +00:00
|
|
|
|
{
|
2020-09-29 10:28:03 +00:00
|
|
|
|
// remove last tenant
|
2021-08-12 15:18:01 +00:00
|
|
|
|
var tenant = tenantDbContext.Tenants.LastOrDefault();
|
2020-09-29 10:28:03 +00:00
|
|
|
|
if (tenant != null)
|
2020-05-28 13:28:51 +00:00
|
|
|
|
{
|
2021-08-12 15:18:01 +00:00
|
|
|
|
tenantDbContext.Tenants.Remove(tenant);
|
|
|
|
|
tenantDbContext.SaveChanges();
|
2020-09-29 10:28:03 +00:00
|
|
|
|
}
|
|
|
|
|
/* var tenantid = CreateCommand("select id from tenants_tenants order by id desc limit 1").ExecuteScalar();
|
|
|
|
|
CreateCommand("delete from tenants_tenants where id = " + tenantid).ExecuteNonQuery();*/
|
|
|
|
|
if (table.Columns.Contains("mappeddomain"))
|
|
|
|
|
{
|
|
|
|
|
foreach (var r in table.Rows.Cast<DataRow>())
|
2020-05-28 13:28:51 +00:00
|
|
|
|
{
|
2020-09-29 10:28:03 +00:00
|
|
|
|
r[table.Columns["mappeddomain"]] = null;
|
|
|
|
|
if (table.Columns.Contains("id"))
|
2020-05-28 13:28:51 +00:00
|
|
|
|
{
|
2021-08-12 15:18:01 +00:00
|
|
|
|
var tariff = coreDbContext.Tariffs.FirstOrDefault(t => t.Tenant == tenant.Id);
|
2020-09-29 10:28:03 +00:00
|
|
|
|
tariff.Tenant = (int)r[table.Columns["id"]];
|
|
|
|
|
// CreateCommand("update tenants_tariff set tenant = " + r[table.Columns["id"]] + " where tenant = " + tenantid).ExecuteNonQuery();
|
2021-08-12 15:18:01 +00:00
|
|
|
|
coreDbContext.Entry(tariff).State = EntityState.Modified;
|
|
|
|
|
coreDbContext.SaveChanges();
|
2020-05-28 13:28:51 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-09-29 10:28:03 +00:00
|
|
|
|
}
|
2020-05-28 13:28:51 +00:00
|
|
|
|
|
2020-09-29 10:28:03 +00:00
|
|
|
|
var sql = new StringBuilder("replace into " + Quote(table.TableName) + "(");
|
2020-05-28 13:28:51 +00:00
|
|
|
|
|
2020-09-29 10:28:03 +00:00
|
|
|
|
var tableColumns = GetColumnsFrom(table.TableName)
|
|
|
|
|
.Intersect(table.Columns.Cast<DataColumn>().Select(c => c.ColumnName), StringComparer.InvariantCultureIgnoreCase)
|
|
|
|
|
.ToList();
|
2020-05-28 13:28:51 +00:00
|
|
|
|
|
2022-01-14 13:12:37 +00:00
|
|
|
|
tableColumns.ForEach(column => sql.Append($"{Quote(column)}, "));
|
2020-10-12 19:39:23 +00:00
|
|
|
|
sql.Replace(", ", ") values (", sql.Length - 2, 2);
|
2020-05-28 13:28:51 +00:00
|
|
|
|
|
2020-09-29 10:28:03 +00:00
|
|
|
|
var insert = connect.CreateCommand();
|
|
|
|
|
tableColumns.ForEach(column =>
|
|
|
|
|
{
|
2022-01-14 13:12:37 +00:00
|
|
|
|
sql.Append($"@{column}, ");
|
2020-09-29 10:28:03 +00:00
|
|
|
|
var p = insert.CreateParameter();
|
|
|
|
|
p.ParameterName = "@" + column;
|
2020-10-12 19:39:23 +00:00
|
|
|
|
insert.Parameters.Add(p);
|
2020-09-29 10:28:03 +00:00
|
|
|
|
});
|
2020-10-12 19:39:23 +00:00
|
|
|
|
sql.Replace(", ", ")", sql.Length - 2, 2);
|
2020-09-29 10:28:03 +00:00
|
|
|
|
insert.CommandText = sql.ToString();
|
|
|
|
|
|
|
|
|
|
foreach (var r in table.Rows.Cast<DataRow>())
|
|
|
|
|
{
|
|
|
|
|
foreach (var c in tableColumns)
|
2020-05-28 13:28:51 +00:00
|
|
|
|
{
|
2020-09-29 10:28:03 +00:00
|
|
|
|
((IDbDataParameter)insert.Parameters["@" + c]).Value = r[c];
|
2020-05-28 13:28:51 +00:00
|
|
|
|
}
|
2020-10-12 19:39:23 +00:00
|
|
|
|
insert.ExecuteNonQuery();
|
2020-05-28 13:28:51 +00:00
|
|
|
|
}
|
2020-09-29 10:28:03 +00:00
|
|
|
|
|
|
|
|
|
tx.Commit();
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
log.ErrorFormat("Table {0}: {1}", table, e);
|
2020-05-28 13:28:51 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
|
|
|
|
builder.Dispose();
|
|
|
|
|
connect.Dispose();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public DbCommand CreateCommand(string sql)
|
|
|
|
|
{
|
|
|
|
|
var command = connect.CreateCommand();
|
|
|
|
|
command.CommandText = sql;
|
|
|
|
|
return command;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<string> ExecuteList(DbCommand command)
|
|
|
|
|
{
|
2020-05-29 14:50:39 +00:00
|
|
|
|
var list = new List<string>();
|
2020-05-28 13:28:51 +00:00
|
|
|
|
using (var result = command.ExecuteReader())
|
|
|
|
|
{
|
|
|
|
|
while (result.Read())
|
|
|
|
|
{
|
|
|
|
|
list.Add(result.GetString(0));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return list;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private string Quote(string identifier)
|
|
|
|
|
{
|
|
|
|
|
return identifier;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private IEnumerable<string> GetColumnsFrom(string table)
|
|
|
|
|
{
|
|
|
|
|
if (mysql)
|
|
|
|
|
{
|
|
|
|
|
return ExecuteList(CreateCommand("show columns from " + Quote(table)));
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2022-01-14 13:12:37 +00:00
|
|
|
|
return columns.Select($"TABLE_NAME = '{table}'")
|
2020-05-28 13:28:51 +00:00
|
|
|
|
.Select(r => r["COLUMN_NAME"].ToString());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private string GetWhere(string tableName, int tenant)
|
|
|
|
|
{
|
|
|
|
|
if (tenant == -1) return string.Empty;
|
|
|
|
|
|
2022-01-19 08:59:14 +00:00
|
|
|
|
if (whereExceptions.TryGetValue(tableName.ToLower(), out var exc))
|
2020-05-28 13:28:51 +00:00
|
|
|
|
{
|
2022-01-19 08:59:14 +00:00
|
|
|
|
return string.Format(exc, tenant);
|
2020-05-28 13:28:51 +00:00
|
|
|
|
}
|
2022-01-12 15:42:03 +00:00
|
|
|
|
var tenantColumn = GetColumnsFrom(tableName).FirstOrDefault(c => c.StartsWith("tenant", StringComparison.OrdinalIgnoreCase));
|
2020-05-28 13:28:51 +00:00
|
|
|
|
return tenantColumn != null ?
|
|
|
|
|
" where " + Quote(tenantColumn) + " = " + tenant :
|
|
|
|
|
" where 1 = 0";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|