159 lines
6.8 KiB
C#
159 lines
6.8 KiB
C#
using MongoDB.Bson;
|
|
using MongoDB.Driver.Core.Configuration;
|
|
using MongoDB.Driver;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using CarRentMongoDB.Models;
|
|
using Npgsql;
|
|
|
|
namespace CarRentMongoDB
|
|
{
|
|
public static class PostgreSQLToMongoDB
|
|
{
|
|
public static void Start()
|
|
{
|
|
// настройки подключения к PostgreSQL
|
|
string connectionString = "Host=192.168.56.101;Username=postgres;Password=123;Database=SUBDLab8";
|
|
using var conn = new NpgsqlConnection(connectionString);
|
|
conn.Open();
|
|
// настройки подключения к MongoDB
|
|
var client = new MongoClient("mongodb://localhost:27017");
|
|
var database = client.GetDatabase("lab7SUBD");
|
|
var collectionCars = database.GetCollection<BsonDocument>("Cars");
|
|
var collectionClients = database.GetCollection<BsonDocument>("Clients");
|
|
var collectionBranches = database.GetCollection<BsonDocument>("Branches");
|
|
var collectionRentals = database.GetCollection<BsonDocument>("Rentals");
|
|
|
|
// запрос на получение данных из PostgreSQL
|
|
using var commandBranches = new NpgsqlCommand("SELECT * FROM \"Branches\"", conn);
|
|
using var readerBranches = commandBranches.ExecuteReader();
|
|
|
|
// цикл по результатам запроса и сохранение данных в MongoDB
|
|
while (readerBranches.Read())
|
|
{
|
|
var document = new BsonDocument();
|
|
for (int i = 0; i < readerBranches.FieldCount; i++)
|
|
{
|
|
string fieldName = readerBranches.GetName(i);
|
|
object value = readerBranches.GetValue(i);
|
|
document.Add(fieldName, BsonValue.Create(value));
|
|
}
|
|
collectionBranches.InsertOne((document));
|
|
}
|
|
readerBranches.Close();
|
|
commandBranches.Cancel();
|
|
// запрос на получение данных из PostgreSQL
|
|
using var commandClient = new NpgsqlCommand("SELECT * FROM \"Clients\"", conn);
|
|
using var readerClient = commandClient.ExecuteReader();
|
|
|
|
// цикл по результатам запроса и сохранение данных в MongoDB
|
|
while (readerClient.Read())
|
|
{
|
|
var document = new BsonDocument();
|
|
for (int i = 0; i < readerClient.FieldCount; i++)
|
|
{
|
|
string fieldName = readerClient.GetName(i);
|
|
object value = readerClient.GetValue(i);
|
|
document.Add(fieldName, BsonValue.Create(value));
|
|
}
|
|
collectionClients.InsertOne((document));
|
|
}
|
|
readerClient.Close();
|
|
commandBranches.Cancel();
|
|
// запрос на получение данных из PostgreSQL
|
|
using var commandCars = new NpgsqlCommand("SELECT * FROM \"Cars\"", conn);
|
|
using var readerCars = commandCars.ExecuteReader();
|
|
|
|
// цикл по результатам запроса и сохранение данных в MongoDB
|
|
while (readerCars.Read())
|
|
{
|
|
var document = new BsonDocument();
|
|
for (int i = 0; i < readerCars.FieldCount; i++)
|
|
{
|
|
string fieldName = readerCars.GetName(i);
|
|
if (fieldName == "BranchId") fieldName = "branch_id";
|
|
object value = readerCars.GetValue(i);
|
|
if (value == null) continue;
|
|
document.Add(fieldName, BsonValue.Create(value));
|
|
}
|
|
collectionCars.InsertOne((document));
|
|
}
|
|
readerCars.Close();
|
|
commandBranches.Cancel();
|
|
// запрос на получение данных из PostgreSQL
|
|
using var commandRentals = new NpgsqlCommand("SELECT * FROM \"Rentals\"", conn);
|
|
using var readerRentals = commandRentals.ExecuteReader();
|
|
|
|
// цикл по результатам запроса и сохранение данных в MongoDB
|
|
while (readerRentals.Read())
|
|
{
|
|
var document = new BsonDocument();
|
|
for (int i = 0; i < readerRentals.FieldCount; i++)
|
|
{
|
|
string fieldName = readerRentals.GetName(i);
|
|
if (fieldName == "CarId") fieldName = "car_id";
|
|
if (fieldName == "ClientId") fieldName = "client_id";
|
|
object value = readerRentals.GetValue(i);
|
|
if (value == DBNull.Value || value.Equals(string.Empty)) continue;
|
|
document.Add(fieldName, BsonValue.Create(value));
|
|
}
|
|
collectionRentals.InsertOne((document));
|
|
}
|
|
readerRentals.Close();
|
|
commandBranches.Cancel();
|
|
conn.Close();
|
|
|
|
var cars = database.GetCollection<BsonDocument>("Cars").Find(Builders<BsonDocument>.Filter.Empty)
|
|
.ToList();
|
|
var clients = database.GetCollection<BsonDocument>("Clients").Find(Builders<BsonDocument>.Filter.Empty)
|
|
.ToList();
|
|
var branches = database.GetCollection<BsonDocument>("Branches").Find(Builders<BsonDocument>.Filter.Empty)
|
|
.ToList();
|
|
var rentals = database.GetCollection<BsonDocument>("Rentals").Find(Builders<BsonDocument>.Filter.Empty)
|
|
.ToList();
|
|
|
|
cars.ForEach(c =>
|
|
{
|
|
// Меняем int Id из Postgres, на ObjectId в связанных документах (Автомобиль - Филиал)
|
|
int tempId = c.GetValue("branch_id").ToInt32();
|
|
var branchFilter = Builders<BsonDocument>.Filter.Eq("Id", tempId);
|
|
var carBranchId = collectionBranches.Find(branchFilter).FirstOrDefaultAsync().Result.GetValue("_id").ToString();
|
|
|
|
var filter = Builders<BsonDocument>.Filter.Eq("_id", c.GetValue("_id"));
|
|
var update = Builders<BsonDocument>.Update.Set("branch_id", carBranchId);
|
|
var result = collectionCars.UpdateOneAsync(filter, update);
|
|
});
|
|
|
|
rentals.ForEach(c =>
|
|
{
|
|
// Меняем int Id из Postgres, на ObjectId в связанных документах (Договор аренды - Клиент)
|
|
int tempClientId = c.GetValue("client_id").ToInt32();
|
|
var clientFilter = Builders<BsonDocument>.Filter.Eq("Id", tempClientId);
|
|
var rentalClientId = collectionClients.Find(clientFilter).FirstOrDefaultAsync().Result.GetValue("_id").ToString();
|
|
|
|
var filter = Builders<BsonDocument>.Filter.Eq("_id", c.GetValue("_id"));
|
|
var update = Builders<BsonDocument>.Update.Set("client_id", rentalClientId);
|
|
var result = collectionRentals.UpdateOneAsync(filter, update);
|
|
// Меняем int Id из Postgres, на ObjectId в связанных документах (Договор аренды - Автомобиль)
|
|
int tempCarId = c.GetValue("car_id").ToInt32();
|
|
var carFilter = Builders<BsonDocument>.Filter.Eq("Id", tempCarId);
|
|
var rentalCarId = collectionCars.Find(carFilter).FirstOrDefaultAsync().Result.GetValue("_id").ToString();
|
|
|
|
filter = Builders<BsonDocument>.Filter.Eq("_id", c.GetValue("_id"));
|
|
update = Builders<BsonDocument>.Update.Set("car_id", rentalCarId);
|
|
result = collectionRentals.UpdateOneAsync(filter, update);
|
|
});
|
|
|
|
var update = Builders<BsonDocument>.Update.Unset("Id");
|
|
collectionClients.UpdateManyAsync(new BsonDocument(), update);
|
|
collectionBranches.UpdateManyAsync(new BsonDocument(), update);
|
|
collectionCars.UpdateManyAsync(new BsonDocument(), update);
|
|
collectionRentals.UpdateManyAsync(new BsonDocument(), update);
|
|
}
|
|
}
|
|
}
|
|
|