123 lines
4.5 KiB
C#
123 lines
4.5 KiB
C#
using MongoDB.Bson;
|
|
using MongoDB.Driver;
|
|
using MongoDB.Driver.Core.Configuration;
|
|
using Npgsql;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace SushiBarMongoDB
|
|
{
|
|
public static class PostgresToMongo
|
|
{
|
|
public static void Convert()
|
|
{
|
|
string postgresConnection = "Host=localhost;Username=postgres;Password=shotboll200412;Database=SUBDLab8";
|
|
using var psqlConn = new NpgsqlConnection(postgresConnection);
|
|
psqlConn.Open();
|
|
|
|
var client = new MongoClient("mongodb://localhost:27017");
|
|
var database = client.GetDatabase("lab8SUBD");
|
|
|
|
var buyers = database.GetCollection<BsonDocument>("Buyers");
|
|
var cooks = database.GetCollection<BsonDocument>("Cooks");
|
|
var menus = database.GetCollection<BsonDocument>("Menus");
|
|
var places = database.GetCollection<BsonDocument>("Places");
|
|
var tasks = database.GetCollection<BsonDocument>("Tasks");
|
|
|
|
//Конвертация покупателей
|
|
|
|
using var psqlBuyers = new NpgsqlCommand("SELECT * FROM \"Buyers\"", psqlConn);
|
|
using var readBuyers = psqlBuyers.ExecuteReader();
|
|
|
|
while(readBuyers.Read())
|
|
{
|
|
var document = new BsonDocument();
|
|
for(int i = 0;i<readBuyers.FieldCount;i++)
|
|
{
|
|
document.Add(readBuyers.GetName(i), BsonValue.Create(readBuyers.GetValue(i)));
|
|
}
|
|
buyers.InsertOne(document);
|
|
}
|
|
readBuyers.Close();
|
|
psqlBuyers.Cancel();
|
|
|
|
//Конвертация поваров
|
|
|
|
using var psqlCooks = new NpgsqlCommand("SELECT * FROM \"Cooks\"", psqlConn);
|
|
using var readCooks = psqlCooks.ExecuteReader();
|
|
|
|
while (readCooks.Read())
|
|
{
|
|
var document = new BsonDocument();
|
|
for (int i = 0; i < readCooks.FieldCount; i++)
|
|
{
|
|
document.Add(readCooks.GetName(i), BsonValue.Create(readCooks.GetValue(i)));
|
|
}
|
|
cooks.InsertOne(document);
|
|
}
|
|
readCooks.Close();
|
|
psqlCooks.Cancel();
|
|
|
|
//Конвертация меню
|
|
|
|
using var psqlMenus = new NpgsqlCommand("SELECT * FROM \"Menus\"", psqlConn);
|
|
using var readMenus = psqlMenus.ExecuteReader();
|
|
|
|
while (readMenus.Read())
|
|
{
|
|
var document = new BsonDocument();
|
|
for (int i = 0; i < readMenus.FieldCount; i++)
|
|
{
|
|
document.Add(readMenus.GetName(i), BsonValue.Create(readMenus.GetValue(i)));
|
|
}
|
|
menus.InsertOne(document);
|
|
}
|
|
readMenus.Close();
|
|
psqlMenus.Cancel();
|
|
|
|
//Конвертация столиков
|
|
|
|
using var psqlPlaces = new NpgsqlCommand("SELECT * FROM \"Places\"", psqlConn);
|
|
using var readPlaces = psqlPlaces.ExecuteReader();
|
|
|
|
while (readPlaces.Read())
|
|
{
|
|
var document = new BsonDocument();
|
|
for (int i = 0; i < readPlaces.FieldCount; i++)
|
|
{
|
|
document.Add(readPlaces.GetName(i), BsonValue.Create(readPlaces.GetValue(i)));
|
|
}
|
|
places.InsertOne(document);
|
|
}
|
|
readPlaces.Close();
|
|
psqlPlaces.Cancel();
|
|
|
|
//Конфертация заказов
|
|
|
|
using var psqlTasks = new NpgsqlCommand("SELECT * FROM \"Tasks\"", psqlConn);
|
|
using var readTasks = psqlTasks.ExecuteReader();
|
|
|
|
while (readTasks.Read())
|
|
{
|
|
var document = new BsonDocument();
|
|
for (int i = 0; i < readTasks.FieldCount; i++)
|
|
{
|
|
string fieldName = readTasks.GetName(i);
|
|
if (fieldName == "PlaceId") fieldName = "place_id";
|
|
if (fieldName == "CookId") fieldName = "cook_id";
|
|
if (fieldName == "BuyerId") fieldName = "buyer_id";
|
|
if (fieldName == "MenuIds") fieldName = "menu_ids";
|
|
document.Add(readTasks.GetName(i), BsonValue.Create(readTasks.GetValue(i)));
|
|
}
|
|
tasks.InsertOne(document);
|
|
}
|
|
readTasks.Close();
|
|
psqlTasks.Cancel();
|
|
psqlConn.Close();
|
|
}
|
|
}
|
|
}
|