This commit is contained in:
Дмитрий Блохин 2024-05-21 22:20:17 +04:00
parent 9494d07ff5
commit 26e699c393
8 changed files with 557 additions and 484 deletions

View File

@ -7,6 +7,8 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="MongoDB.Bson" Version="2.25.0" />
<PackageReference Include="MongoDB.Driver" Version="2.25.0" />
<PackageReference Include="Npgsql" Version="8.0.3" /> <PackageReference Include="Npgsql" Version="8.0.3" />
</ItemGroup> </ItemGroup>

View File

@ -0,0 +1,244 @@
using Commentbase;
using MongoDB.Driver;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.ConstrainedExecution;
using System.Text;
using System.Threading.Tasks;
namespace Database
{
public class ImplementsMongoDB : Abstracts
{
private IMongoDatabase _database;
private IMongoCollection<Album> _albumCollection;
private IMongoCollection<Location> _locationCollection;
private IMongoCollection<Author> _authorCollection;
private IMongoCollection<Photo> _photoCollection;
private IMongoCollection<Comment> _commentCollection;
private IMongoCollection<Sequence> _sequenceCollection;
public ImplementsMongoDB()
{
var client = new MongoClient("mongodb://localhost:27017");
_database = client.GetDatabase("SUBD");
_albumCollection = _database.GetCollection<Album>("Album");
_locationCollection = _database.GetCollection<Location>("Location");
_authorCollection = _database.GetCollection<Author>("Author");
_photoCollection = _database.GetCollection<Photo>("Photo");
_commentCollection = _database.GetCollection<Comment>("Comment");
_sequenceCollection = _database.GetCollection<Sequence>("sequence");
}
//Album
public override void CreateAlbum(Album album)
{
if (album.Id == 0)
{
Sequence sequence = _sequenceCollection.Find(seq => seq.Id == "Album").FirstOrDefault();
sequence.Count++;
album.Id = sequence.Count;
_sequenceCollection.ReplaceOne(seq => seq.Id == "Album", sequence);
}
_albumCollection.InsertOne(album);
}
public override List<Album> GetAlbums()
{
return _albumCollection.Find(_ => true).ToList();
}
public override Album GetAlbum(int id)
{
return _albumCollection.Find(album => album.Id == id).FirstOrDefault();
}
public override void UpdateAlbum(Album album)
{
_albumCollection.ReplaceOne(c => c.Id == album.Id, album);
}
public override void DeleteAlbum(int id)
{
_albumCollection.DeleteOne(album => album.Id == id);
}
public override Album GetAlbum(string title)
{
return _albumCollection.Find(album => album.Title == title).FirstOrDefault();
}
public override void DeleteAllAlbums()
{
_albumCollection.DeleteManyAsync(Builders<Album>.Filter.Empty);
}
// Location
public override void CreateLocation(Location location)
{
if (location.Id == 0)
{
Sequence sequence = _sequenceCollection.Find(seq => seq.Id == "Location").FirstOrDefault();
sequence.Count++;
location.Id = sequence.Count;
_sequenceCollection.ReplaceOne(seq => seq.Id == "Location", sequence);
}
_locationCollection.InsertOne(location);
}
public override List<Location> GetLocations()
{
return _locationCollection.Find(_ => true).ToList();
}
public override Location GetLocation(int id)
{
return _locationCollection.Find(location => location.Id == id).FirstOrDefault();
}
public override void UpdateLocation(Location location)
{
_locationCollection.ReplaceOne(c => c.Id == location.Id, location);
}
public override void DeleteLocation(int id)
{
_locationCollection.DeleteOne(location => location.Id == id);
}
public override Location GetLocation(string name)
{
return _locationCollection.Find(location => location.Name == name).FirstOrDefault();
}
public override void DeleteAllLocations()
{
_locationCollection.DeleteManyAsync(Builders<Location>.Filter.Empty);
}
// Author
public override void CreateAuthor(Author author)
{
if (author.Id == 0)
{
Sequence sequence = _sequenceCollection.Find(seq => seq.Id == "Author").FirstOrDefault();
sequence.Count++;
author.Id = sequence.Count;
_sequenceCollection.ReplaceOne(seq => seq.Id == "Author", sequence);
}
_authorCollection.InsertOne(author);
}
public override List<Author> GetAuthors()
{
return _authorCollection.Find(_ => true).ToList();
}
public override Author GetAuthor(int id)
{
return _authorCollection.Find(author => author.Id == id).FirstOrDefault();
}
public override void UpdateAuthor(Author author)
{
_authorCollection.ReplaceOne(r => r.Id == author.Id, author);
}
public override void DeleteAuthor(int id)
{
_authorCollection.DeleteOne(author => author.Id == id);
}
public override Author GetAuthor(string name)
{
return _authorCollection.Find(author => author.Name == name).FirstOrDefault();
}
public override void DeleteAllAuthors()
{
_authorCollection.DeleteManyAsync(Builders<Author>.Filter.Empty);
}
// Photo
public override void CreatePhoto(Photo photo)
{
if (photo.Id == 0)
{
Sequence sequence = _sequenceCollection.Find(seq => seq.Id == "Photo").FirstOrDefault();
sequence.Count++;
photo.Id = sequence.Count;
_sequenceCollection.ReplaceOne(seq => seq.Id == "Photo", sequence);
}
_photoCollection.InsertOne(photo);
}
public override List<Photo> GetPhotos()
{
return _photoCollection.Find(_ => true).ToList();
}
public override Photo GetPhoto(int id)
{
return _photoCollection.Find(photo => photo.Id == id).FirstOrDefault();
}
public override void UpdatePhoto(Photo photo)
{
_photoCollection.ReplaceOne(b => b.Id == photo.Id, photo);
}
public override void DeletePhoto(int id)
{
_photoCollection.DeleteOne(photo => photo.Id == id);
}
public override Photo GetPhoto(int AlbumId, int LocationId, int AuthorId)
{
return _photoCollection.Find(photo => (photo.AlbumId == AlbumId && photo.LocationId == LocationId && photo.AuthorId == AuthorId)).FirstOrDefault();
}
public override void DeleteAllPhotos()
{
_photoCollection.DeleteManyAsync(Builders<Photo>.Filter.Empty);
}
// Comment
public override void CreateComment(Comment comment)
{
if (comment.Id == 0)
{
Sequence sequence = _sequenceCollection.Find(seq => seq.Id == "Comment").FirstOrDefault();
sequence.Count++;
comment.Id = sequence.Count;
_sequenceCollection.ReplaceOne(seq => seq.Id == "Comment", sequence);
}
_commentCollection.InsertOne(comment);
}
public override List<Comment> GetComments()
{
return _commentCollection.Find(_ => true).ToList();
}
public override Comment GetComment(int id)
{
return _commentCollection.Find(comment => comment.Id == id).FirstOrDefault();
}
public override void UpdateComment(Comment comment)
{
_commentCollection.ReplaceOne(b => b.Id == comment.Id, comment);
}
public override void DeleteComment(int id)
{
_commentCollection.DeleteOne(comment => comment.Id == id);
}
public override Comment GetCommentPhoto(int PhotoId)
{
return _commentCollection.Find(comment => comment.PhotoId == PhotoId).FirstOrDefault();
}
public override void DeleteAllComments()
{
_commentCollection.DeleteManyAsync(Builders<Comment>.Filter.Empty);
}
public void UpdateSequence(Sequence sequence)
{
_sequenceCollection.ReplaceOne(seq => seq.Id == sequence.Id, sequence);
}
}
}

View File

@ -1,5 +1,6 @@
using Commentbase; using Commentbase;
using Npgsql; using Npgsql;
using System.Collections.Generic;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
@ -8,7 +9,7 @@ using System.Threading.Tasks;
namespace Database namespace Database
{ {
public class Implements : Abstracts public class ImplementsPostgres : Abstracts
{ {
private NpgsqlConnection GetConnect() private NpgsqlConnection GetConnect()
{ {
@ -523,5 +524,39 @@ namespace Database
using var cmd = new NpgsqlCommand($"DELETE FROM Comment", conn); using var cmd = new NpgsqlCommand($"DELETE FROM Comment", conn);
cmd.ExecuteNonQuery(); cmd.ExecuteNonQuery();
} }
private Sequence GetSequence(string nameInPg, string nameInApp)
{
using var conn = GetConnect();
conn.Open();
using var cmd = new NpgsqlCommand("SELECT sequencename, last_value " +
"FROM pg_sequences " +
"WHERE sequencename = '" + nameInPg + "_id_seq'", conn);
using var reader = cmd.ExecuteReader();
if (reader.Read())
{
return new Sequence
{
Id = nameInApp,
Count = reader.GetInt32(1)
};
}
else
return new Sequence
{
Id = nameInApp,
Count = 0
};
}
public List<Sequence> GetSequences()
{
return new List<Sequence>{
GetSequence("album", "Album"),
GetSequence("location", "Location"),
GetSequence("author", "Author"),
GetSequence("photo", "Photo"),
GetSequence("comment", "Comment")
};
}
} }
} }

View File

@ -1,4 +1,6 @@
using System; using MongoDB.Bson;
using MongoDB.Bson.Serialization.Attributes;
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
@ -8,18 +10,21 @@ namespace Database
{ {
public class Album public class Album
{ {
[BsonRepresentation(BsonType.Int32)]
public int Id { get; set; } public int Id { get; set; }
public string Title { get; set; } public string Title { get; set; }
public string Description { get; set; } public string Description { get; set; }
} }
public class Location public class Location
{ {
[BsonRepresentation(BsonType.Int32)]
public int Id { get; set; } public int Id { get; set; }
public string Name { get; set; } public string Name { get; set; }
public string ShortName { get; set; } public string ShortName { get; set; }
} }
public class Author public class Author
{ {
[BsonRepresentation(BsonType.Int32)]
public int Id { get; set; } public int Id { get; set; }
public string Name { get; set; } public string Name { get; set; }
public string PhoneNum { get; set; } public string PhoneNum { get; set; }
@ -27,6 +32,7 @@ namespace Database
} }
public class Photo public class Photo
{ {
[BsonRepresentation(BsonType.Int32)]
public int Id { get; set; } public int Id { get; set; }
public string Title { get; set; } public string Title { get; set; }
public string Description { get; set; } public string Description { get; set; }
@ -39,9 +45,16 @@ namespace Database
} }
public class Comment public class Comment
{ {
[BsonRepresentation(BsonType.Int32)]
public int Id { get; set; } public int Id { get; set; }
public DateTime PostDate { get; set; } public DateTime PostDate { get; set; }
public string Content { get; set; } public string Content { get; set; }
public int PhotoId { get; set; } public int PhotoId { get; set; }
} }
public class Sequence
{
[BsonRepresentation(BsonType.String)]
public string Id { get; set; }
public int Count { get; set; }
}
} }

View File

@ -28,279 +28,167 @@
/// </summary> /// </summary>
private void InitializeComponent() private void InitializeComponent()
{ {
button1 = new Button(); menuStrip1 = new MenuStrip();
button2 = new Button(); сущностиToolStripMenuItem = new ToolStripMenuItem();
button3 = new Button(); albumToolStripMenuItem = new ToolStripMenuItem();
button4 = new Button(); locationToolStripMenuItem = new ToolStripMenuItem();
button5 = new Button(); authorToolStripMenuItem = new ToolStripMenuItem();
button6 = new Button(); photoToolStripMenuItem = new ToolStripMenuItem();
button7 = new Button(); commentToolStripMenuItem = new ToolStripMenuItem();
button8 = new Button(); trackBarDB = new TrackBar();
button9 = new Button(); labelMongoDB = new Label();
button10 = new Button(); label1 = new Label();
button11 = new Button(); buttonTransferDataFromPostgresToMongo = new Button();
button12 = new Button(); buttonTest = new Button();
button13 = new Button(); textBoxTest = new TextBox();
button14 = new Button(); menuStrip1.SuspendLayout();
button15 = new Button(); ((System.ComponentModel.ISupportInitialize)trackBarDB).BeginInit();
button16 = new Button();
button17 = new Button();
button18 = new Button();
button19 = new Button();
button20 = new Button();
SuspendLayout(); SuspendLayout();
// //
// button1 // menuStrip1
// //
button1.Location = new Point(41, 32); menuStrip1.ImageScalingSize = new Size(20, 20);
button1.Name = "button1"; menuStrip1.Items.AddRange(new ToolStripItem[] { сущностиToolStripMenuItem });
button1.Size = new Size(137, 29); menuStrip1.Location = new Point(0, 0);
button1.TabIndex = 0; menuStrip1.Name = "menuStrip1";
button1.Text = "Альбом"; menuStrip1.Size = new Size(1135, 28);
button1.UseVisualStyleBackColor = true; menuStrip1.TabIndex = 20;
button1.Click += button1_Click; menuStrip1.Text = "menuStrip1";
// //
// button2 // сущностиToolStripMenuItem
// //
button2.Location = new Point(243, 32); сущностиToolStripMenuItem.DropDownItems.AddRange(new ToolStripItem[] { albumToolStripMenuItem, locationToolStripMenuItem, authorToolStripMenuItem, photoToolStripMenuItem, commentToolStripMenuItem });
button2.Name = "button2"; сущностиToolStripMenuItem.Name = "сущностиToolStripMenuItem";
button2.Size = new Size(137, 29); сущностиToolStripMenuItem.Size = new Size(91, 24);
button2.TabIndex = 1; сущностиToolStripMenuItem.Text = "Сущности";
button2.Text = "Локация";
button2.UseVisualStyleBackColor = true;
button2.Click += button2_Click;
// //
// button3 // albumToolStripMenuItem
// //
button3.Location = new Point(451, 32); albumToolStripMenuItem.Name = "albumToolStripMenuItem";
button3.Name = "button3"; albumToolStripMenuItem.Size = new Size(190, 26);
button3.Size = new Size(137, 29); albumToolStripMenuItem.Text = "Альбом";
button3.TabIndex = 2; albumToolStripMenuItem.Click += albumToolStripMenuItem_Click;
button3.Text = "Автор";
button3.UseVisualStyleBackColor = true;
button3.Click += button3_Click;
// //
// button4 // locationToolStripMenuItem
// //
button4.Location = new Point(675, 32); locationToolStripMenuItem.Name = "locationToolStripMenuItem";
button4.Name = "button4"; locationToolStripMenuItem.Size = new Size(190, 26);
button4.Size = new Size(137, 29); locationToolStripMenuItem.Text = "Локация";
button4.TabIndex = 3; locationToolStripMenuItem.Click += locationToolStripMenuItem_Click;
button4.Text = "Фотография";
button4.UseVisualStyleBackColor = true;
button4.Click += button4_Click;
// //
// button5 // authorToolStripMenuItem
// //
button5.Location = new Point(922, 32); authorToolStripMenuItem.Name = "authorToolStripMenuItem";
button5.Name = "button5"; authorToolStripMenuItem.Size = new Size(190, 26);
button5.Size = new Size(137, 29); authorToolStripMenuItem.Text = "Автор";
button5.TabIndex = 4; authorToolStripMenuItem.Click += authorToolStripMenuItem_Click;
button5.Text = "Комментарий";
button5.UseVisualStyleBackColor = true;
button5.Click += button5_Click;
// //
// button6 // photoToolStripMenuItem
// //
button6.Location = new Point(12, 120); photoToolStripMenuItem.Name = "photoToolStripMenuItem";
button6.Name = "button6"; photoToolStripMenuItem.Size = new Size(190, 26);
button6.Size = new Size(189, 29); photoToolStripMenuItem.Text = "Фотография";
button6.TabIndex = 5; photoToolStripMenuItem.Click += photoToolStripMenuItem_Click;
button6.Text = "Создать 1000 альбомов";
button6.UseVisualStyleBackColor = true;
button6.Click += button6_Click;
// //
// button7 // commentToolStripMenuItem
// //
button7.Location = new Point(216, 120); commentToolStripMenuItem.Name = "commentToolStripMenuItem";
button7.Name = "button7"; commentToolStripMenuItem.Size = new Size(190, 26);
button7.Size = new Size(189, 29); commentToolStripMenuItem.Text = "Комментарий";
button7.TabIndex = 6; commentToolStripMenuItem.Click += commentToolStripMenuItem_Click;
button7.Text = "Создать 1000 локаций";
button7.UseVisualStyleBackColor = true;
button7.Click += button7_Click;
// //
// button8 // trackBarDB
// //
button8.Location = new Point(424, 120); trackBarDB.LargeChange = 1;
button8.Name = "button8"; trackBarDB.Location = new Point(31, 89);
button8.Size = new Size(189, 29); trackBarDB.Maximum = 100;
button8.TabIndex = 7; trackBarDB.Name = "trackBarDB";
button8.Text = "Создать 1000 авторов"; trackBarDB.Orientation = Orientation.Vertical;
button8.UseVisualStyleBackColor = true; trackBarDB.Size = new Size(56, 119);
button8.Click += button8_Click; trackBarDB.TabIndex = 3;
trackBarDB.MouseUp += trackBarDB_MouseUp;
// //
// button9 // labelMongoDB
// //
button9.Location = new Point(650, 120); labelMongoDB.AutoSize = true;
button9.Name = "button9"; labelMongoDB.Location = new Point(93, 89);
button9.Size = new Size(201, 29); labelMongoDB.Name = "labelMongoDB";
button9.TabIndex = 8; labelMongoDB.Size = new Size(77, 20);
button9.Text = "Создать 1000 фотографий"; labelMongoDB.TabIndex = 22;
button9.UseVisualStyleBackColor = true; labelMongoDB.Text = "MongoDB";
button9.Click += button9_Click;
// //
// button10 // label1
// //
button10.Location = new Point(880, 120); label1.AutoSize = true;
button10.Name = "button10"; label1.Location = new Point(93, 188);
button10.Size = new Size(218, 29); label1.Name = "label1";
button10.TabIndex = 9; label1.Size = new Size(64, 20);
button10.Text = "Создать 1000 комментариев"; label1.TabIndex = 23;
button10.UseVisualStyleBackColor = true; label1.Text = "Postgres";
button10.Click += button10_Click;
// //
// button11 // buttonTransferDataFromPostgresToMongo
// //
button11.Location = new Point(12, 187); buttonTransferDataFromPostgresToMongo.Location = new Point(834, 80);
button11.Name = "button11"; buttonTransferDataFromPostgresToMongo.Name = "buttonTransferDataFromPostgresToMongo";
button11.Size = new Size(198, 29); buttonTransferDataFromPostgresToMongo.Size = new Size(245, 128);
button11.TabIndex = 10; buttonTransferDataFromPostgresToMongo.TabIndex = 24;
button11.Text = "Изменить 1000 альбомов"; buttonTransferDataFromPostgresToMongo.Text = "Transfer data from PostgresSQL to MongoDB";
button11.UseVisualStyleBackColor = true; buttonTransferDataFromPostgresToMongo.UseVisualStyleBackColor = true;
button11.Click += button11_Click; buttonTransferDataFromPostgresToMongo.Click += buttonTransferDataFromPostgresToMongo_Click;
// //
// button12 // buttonTest
// //
button12.Location = new Point(216, 187); buttonTest.Location = new Point(439, 318);
button12.Name = "button12"; buttonTest.Name = "buttonTest";
button12.Size = new Size(189, 29); buttonTest.Size = new Size(94, 29);
button12.TabIndex = 11; buttonTest.TabIndex = 25;
button12.Text = "Изменить 1000 локаций"; buttonTest.Text = "test";
button12.UseVisualStyleBackColor = true; buttonTest.UseVisualStyleBackColor = true;
button12.Click += button12_Click; buttonTest.Click += buttonTest_Click;
// //
// button13 // textBoxTest
// //
button13.Location = new Point(424, 187); textBoxTest.Location = new Point(310, 80);
button13.Name = "button13"; textBoxTest.Multiline = true;
button13.Size = new Size(189, 29); textBoxTest.Name = "textBoxTest";
button13.TabIndex = 12; textBoxTest.Size = new Size(377, 128);
button13.Text = "Изменить 1000 авторов"; textBoxTest.TabIndex = 26;
button13.UseVisualStyleBackColor = true;
button13.Click += button13_Click;
//
// button14
//
button14.Location = new Point(639, 187);
button14.Name = "button14";
button14.Size = new Size(224, 29);
button14.TabIndex = 13;
button14.Text = "Изменить 1000 фотографий";
button14.UseVisualStyleBackColor = true;
button14.Click += button14_Click;
//
// button15
//
button15.Location = new Point(869, 187);
button15.Name = "button15";
button15.Size = new Size(239, 29);
button15.TabIndex = 14;
button15.Text = "Изменить 1000 комментариев";
button15.UseVisualStyleBackColor = true;
button15.Click += button15_Click;
//
// button16
//
button16.Location = new Point(12, 257);
button16.Name = "button16";
button16.Size = new Size(198, 29);
button16.TabIndex = 15;
button16.Text = "Удалить 1000 альбомов";
button16.UseVisualStyleBackColor = true;
button16.Click += button16_Click;
//
// button17
//
button17.Location = new Point(216, 257);
button17.Name = "button17";
button17.Size = new Size(189, 29);
button17.TabIndex = 16;
button17.Text = "Удалить 1000 локаций";
button17.UseVisualStyleBackColor = true;
button17.Click += button17_Click;
//
// button18
//
button18.Location = new Point(424, 257);
button18.Name = "button18";
button18.Size = new Size(189, 29);
button18.TabIndex = 17;
button18.Text = "Удалить 1000 авторов";
button18.UseVisualStyleBackColor = true;
button18.Click += button18_Click;
//
// button19
//
button19.Location = new Point(639, 257);
button19.Name = "button19";
button19.Size = new Size(224, 29);
button19.TabIndex = 18;
button19.Text = "Удалить 1000 фотографий";
button19.UseVisualStyleBackColor = true;
button19.Click += button19_Click;
//
// button20
//
button20.Location = new Point(869, 257);
button20.Name = "button20";
button20.Size = new Size(239, 29);
button20.TabIndex = 19;
button20.Text = "Удалить 1000 комментариев";
button20.UseVisualStyleBackColor = true;
button20.Click += button20_Click;
// //
// MainForm // MainForm
// //
AutoScaleDimensions = new SizeF(8F, 20F); AutoScaleDimensions = new SizeF(8F, 20F);
AutoScaleMode = AutoScaleMode.Font; AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(1131, 318); ClientSize = new Size(1135, 436);
Controls.Add(button20); Controls.Add(textBoxTest);
Controls.Add(button19); Controls.Add(buttonTest);
Controls.Add(button18); Controls.Add(buttonTransferDataFromPostgresToMongo);
Controls.Add(button17); Controls.Add(label1);
Controls.Add(button16); Controls.Add(labelMongoDB);
Controls.Add(button15); Controls.Add(trackBarDB);
Controls.Add(button14); Controls.Add(menuStrip1);
Controls.Add(button13); MainMenuStrip = menuStrip1;
Controls.Add(button12);
Controls.Add(button11);
Controls.Add(button10);
Controls.Add(button9);
Controls.Add(button8);
Controls.Add(button7);
Controls.Add(button6);
Controls.Add(button5);
Controls.Add(button4);
Controls.Add(button3);
Controls.Add(button2);
Controls.Add(button1);
Name = "MainForm"; Name = "MainForm";
Text = "MainForm"; Text = "MainForm";
menuStrip1.ResumeLayout(false);
menuStrip1.PerformLayout();
((System.ComponentModel.ISupportInitialize)trackBarDB).EndInit();
ResumeLayout(false); ResumeLayout(false);
PerformLayout();
} }
#endregion #endregion
private MenuStrip menuStrip1;
private Button button1; private ToolStripMenuItem сущностиToolStripMenuItem;
private Button button2; private ToolStripMenuItem albumToolStripMenuItem;
private Button button3; private ToolStripMenuItem locationToolStripMenuItem;
private Button button4; private ToolStripMenuItem authorToolStripMenuItem;
private Button button5; private ToolStripMenuItem photoToolStripMenuItem;
private Button button6; private ToolStripMenuItem commentToolStripMenuItem;
private Button button7; private TrackBar trackBarDB;
private Button button8; private Label labelMongoDB;
private Button button9; private Label label1;
private Button button10; private Button buttonTransferDataFromPostgresToMongo;
private Button button11; private Button buttonTest;
private Button button12; private TextBox textBoxTest;
private Button button13;
private Button button14;
private Button button15;
private Button button16;
private Button button17;
private Button button18;
private Button button19;
private Button button20;
} }
} }

View File

@ -10,299 +10,162 @@ using System.Net;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Windows.Forms; using System.Windows.Forms;
using static System.Windows.Forms.DataFormats;
namespace View namespace View
{ {
public partial class MainForm : Form public partial class MainForm : Form
{ {
private readonly Abstracts db; public MainForm()
public MainForm(Abstracts abstracts)
{ {
InitializeComponent(); InitializeComponent();
db = abstracts;
} }
private void button1_Click(object sender, EventArgs e) private void albumToolStripMenuItem_Click(object sender, EventArgs e)
{ {
var service = Program.ServiceProvider?.GetService(typeof(FormAlbums)); var service = Program.ServiceProvider?.GetService(typeof(FormAlbums));
if (service is FormAlbums form) if (service is FormAlbums form)
{ {
form.Show(); form.ShowDialog();
} }
} }
private void button2_Click(object sender, EventArgs e)
private void locationToolStripMenuItem_Click(object sender, EventArgs e)
{ {
var service = Program.ServiceProvider?.GetService(typeof(FormLocations)); var service = Program.ServiceProvider?.GetService(typeof(FormLocations));
if (service is FormLocations form) if (service is FormLocations form)
{ {
form.Show(); form.ShowDialog();
} }
} }
private void button3_Click(object sender, EventArgs e)
private void authorToolStripMenuItem_Click(object sender, EventArgs e)
{ {
var service = Program.ServiceProvider?.GetService(typeof(FormAuthors)); var service = Program.ServiceProvider?.GetService(typeof(FormAuthors));
if (service is FormAuthors form) if (service is FormAuthors form)
{ {
form.Show(); form.ShowDialog();
} }
} }
private void button4_Click(object sender, EventArgs e)
private void photoToolStripMenuItem_Click(object sender, EventArgs e)
{ {
var service = Program.ServiceProvider?.GetService(typeof(FormPhotos)); var service = Program.ServiceProvider?.GetService(typeof(FormPhotos));
if (service is FormPhotos form) if (service is FormPhotos form)
{ {
form.Show(); form.ShowDialog();
} }
} }
private void button5_Click(object sender, EventArgs e)
private void commentToolStripMenuItem_Click(object sender, EventArgs e)
{ {
var service = Program.ServiceProvider?.GetService(typeof(FormComments)); var service = Program.ServiceProvider?.GetService(typeof(FormComments));
if (service is FormComments form) if (service is FormComments form)
{ {
form.Show(); form.ShowDialog();
} }
} }
private void button6_Click(object sender, EventArgs e) private void buttonTest_Click(object sender, EventArgs e)
{ {
string Title = "Шикарный альбом"; var photo = new Photo()
string Description = "Тут даже нечего сказать";
DateTime start = DateTime.Now;
for (int i = 0; i < 1000; i++)
{ {
db.CreateAlbum(new() Id = 0,
{ Title = "photo",
Title = $"{Title}{i}", Description = "cool photo",
Description = $"{Description}{i}" Privacy = "public",
}); UploadDate = DateTime.Now,
} ImagePath = "Image/312",
DateTime end = DateTime.Now; AlbumId = 1,
MessageBox.Show((end - start).Milliseconds.ToString(), "Время работы", MessageBoxButtons.OK, MessageBoxIcon.Information); LocationId = 1,
AuthorId = 1,
};
ImplementsPostgres bd = new();
DateTime startTime = DateTime.Now;
bd.DeletePhoto(40);
bd.DeletePhoto(41);
bd.DeletePhoto(42);
bd.DeletePhoto(43);
bd.DeletePhoto(44);
DateTime endTime = DateTime.Now;
// Выводим время выполнения запроса в консоль
textBoxTest.Text = $"Время выполнения запроса в постгресе: {(endTime - startTime).TotalMilliseconds} миллисекунд ";
ImplementsMongoDB bdMongodb = new();
startTime = DateTime.Now;
bdMongodb.DeletePhoto(40);
bdMongodb.DeletePhoto(41);
bdMongodb.DeletePhoto(42);
bdMongodb.DeletePhoto(43);
bdMongodb.DeletePhoto(44);
endTime = DateTime.Now;
textBoxTest.Text += $"Время выполнения запроса в монгоДб: {(endTime - startTime).TotalMilliseconds} миллисекунд";
} }
private void button7_Click(object sender, EventArgs e)
private void trackBarDB_MouseUp(object sender, MouseEventArgs e)
{ {
string Name = "Москва, Россия"; if (trackBarDB.Value > 50)
string ShortName = "RUS";
DateTime start = DateTime.Now;
for (int i = 0; i < 1000; i++)
{ {
db.CreateLocation(new() trackBarDB.Value = 100;
{ Program.ChangeDB(false);
Name = $"{Name}{i}",
ShortName = $"{ShortName}{i}"
});
} }
DateTime end = DateTime.Now; else
MessageBox.Show((end - start).Milliseconds.ToString(), "Время работы", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
private void button8_Click(object sender, EventArgs e)
{
string Name = "Эрик Житель";
string PhoneNum = "+79999999999";
string Email = "laba@mail.ru";
DateTime start = DateTime.Now;
for (int i = 0; i < 1000; i++)
{ {
db.CreateAuthor(new() trackBarDB.Value = 0;
{ Program.ChangeDB(true);
Name = $"{Name}{i}",
PhoneNum = $"{PhoneNum}{i}",
Email = $"{Email}{i}"
});
} }
DateTime end = DateTime.Now;
MessageBox.Show((end - start).Milliseconds.ToString(), "Время работы", MessageBoxButtons.OK, MessageBoxIcon.Information);
} }
private void button9_Click(object sender, EventArgs e)
private void buttonTransferDataFromPostgresToMongo_Click(object sender, EventArgs e)
{ {
string Title = "image"; ImplementsMongoDB implementsMongoDB = new();
string Description = "photo";
string Privacy = "Общий доступ"; // очищаем всё
DateTime UploadData = DateTime.Now; foreach (var it in implementsMongoDB.GetAlbums())
string ImagePath = "Image/Ch8cfe8vfevb"; implementsMongoDB.DeleteAlbum(it.Id);
Album albumId = db.GetAlbum("Шикарный альбом0"); foreach (var it in implementsMongoDB.GetLocations())
Location locationId = db.GetLocation("Москва, Россия0"); implementsMongoDB.DeleteLocation(it.Id);
Author authorId = db.GetAuthor("Эрик Житель0"); foreach (var it in implementsMongoDB.GetAuthors())
DateTime start = DateTime.Now; implementsMongoDB.DeleteAuthor(it.Id);
for (int i = 0; i < 1000; i++) foreach (var it in implementsMongoDB.GetPhotos())
{ implementsMongoDB.DeletePhoto(it.Id);
db.CreatePhoto(new() foreach (var it in implementsMongoDB.GetComments())
{ implementsMongoDB.DeleteComment(it.Id);
Title = $"{Title}{i}",
Description = $"{Description}{i}", ImplementsPostgres implementsPostgres = new();
Privacy = $"{Privacy}{i}",
UploadDate = UploadData, // скачиваем из постгреса
ImagePath = $"{ImagePath}{i}", var listAlbums = implementsPostgres.GetAlbums();
AlbumId = albumId.Id + i, var listLocations = implementsPostgres.GetLocations();
LocationId = locationId.Id + i, var listAuthors = implementsPostgres.GetAuthors();
AuthorId = authorId.Id + i var listPhotos = implementsPostgres.GetPhotos();
}); var listComments = implementsPostgres.GetComments();
}
DateTime end = DateTime.Now; // вливаем данные монго дб
MessageBox.Show((end - start).Milliseconds.ToString(), "Время работы", MessageBoxButtons.OK, MessageBoxIcon.Information); foreach (var it in listAlbums)
} implementsMongoDB.CreateAlbum(it);
private void button10_Click(object sender, EventArgs e) foreach (var it in listLocations)
{ implementsMongoDB.CreateLocation(it);
DateTime PostDate = DateTime.Now; foreach (var it in listAuthors)
string Content = "Вай, как круто!"; implementsMongoDB.CreateAuthor(it);
Album albumId = db.GetAlbum("Шикарный альбом0"); foreach (var it in listPhotos)
Location locationId = db.GetLocation("Москва, Россия0"); implementsMongoDB.CreatePhoto(it);
Author authorId = db.GetAuthor("Эрик Житель0"); foreach (var it in listComments)
Photo photoId = db.GetPhoto(albumId.Id, locationId.Id, authorId.Id); implementsMongoDB.CreateComment(it);
DateTime start = DateTime.Now;
for (int i = 0; i < 1000; i++) // забираем информацию о последовательностях
{ var listSequence = implementsPostgres.GetSequences();
db.CreateComment(new() foreach (var it in listSequence)
{ if (it.Id == "Album" ||
PostDate = PostDate, it.Id == "Location" ||
Content = $"{Content}{i}", it.Id == "Author" ||
PhotoId = photoId.Id + i it.Id == "Photo" ||
}); it.Id == "Comment"
} )
DateTime end = DateTime.Now; implementsMongoDB.UpdateSequence(it);
MessageBox.Show((end - start).Milliseconds.ToString(), "Время работы", MessageBoxButtons.OK, MessageBoxIcon.Information); else
} throw new Exception("неправильнй id последовательности");
private void button11_Click(object sender, EventArgs e)
{
Album AlbumId = db.GetAlbum("Шикарный альбом0");
string Title = "Шикарный альбом1";
string Description = "Тут даже нечего сказать опять";
DateTime start = DateTime.Now;
for (int i = 0; i < 1000; i++)
{
db.UpdateAlbum(new()
{
Id = AlbumId.Id + i,
Title = $"{Title}{i}",
Description = $"{Description}{i}"
});
}
DateTime end = DateTime.Now;
MessageBox.Show((end - start).Milliseconds.ToString(), "Время работы", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
private void button12_Click(object sender, EventArgs e)
{
Location locationId = db.GetLocation("Москва, Россия0");
string Name = "Рим, Италия";
string ShortName = "ITA";
DateTime start = DateTime.Now;
for (int i = 0; i < 1000; i++)
{
db.UpdateLocation(new()
{
Id = locationId.Id + i,
Name = $"{Name}{i}",
ShortName = $"{ShortName}{i}"
});
}
DateTime end = DateTime.Now;
MessageBox.Show((end - start).Milliseconds.ToString(), "Время работы", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
private void button13_Click(object sender, EventArgs e)
{
Author authorId = db.GetAuthor("Эрик Житель0");
string Name = "Chel";
string PhoneNum = "+79991112233";
string Email = "laba4@mail.ru";
DateTime start = DateTime.Now;
for (int i = 0; i < 1000; i++)
{
db.UpdateAuthor(new()
{
Id = authorId.Id + i,
Name = $"{Name}{i}",
PhoneNum = $"{PhoneNum}{i}",
Email = $"{Email}{i}"
});
}
DateTime end = DateTime.Now;
MessageBox.Show((end - start).Milliseconds.ToString(), "Время работы", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
private void button14_Click(object sender, EventArgs e)
{
string Title = "image";
string Description = "photo";
string Privacy = "Общий доступ";
DateTime UploadData = DateTime.Now;
string ImagePath = "Image/sndbkKSB23842KJH";
Album albumId = db.GetAlbum("Шикарный альбом0");
Location locationId = db.GetLocation("Москва, Россия0");
Author authorId = db.GetAuthor("Эрик Житель0");
Photo photo = db.GetPhoto(albumId.Id, locationId.Id, authorId.Id);
DateTime start = DateTime.Now;
for (int i = 0; i < 1000; i++)
{
db.UpdatePhoto(new()
{
Id = photo.Id + i,
Title = $"{Title}{i}",
Description = $"{Description}{i}",
Privacy = $"{Privacy}{i}",
UploadDate = UploadData,
ImagePath = $"{ImagePath}{i}",
AlbumId = albumId.Id + i,
LocationId = locationId.Id + i,
AuthorId = authorId.Id + i
});
}
DateTime end = DateTime.Now;
MessageBox.Show((end - start).Milliseconds.ToString(), "Время работы", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
private void button15_Click(object sender, EventArgs e)
{
DateTime PostDate = DateTime.Now;
string Content = "О боже мой!";
Album albumId = db.GetAlbum("Шикарный альбом0");
Location locationId = db.GetLocation("Москва, Россия0");
Author authorId = db.GetAuthor("Эрик Житель0");
Photo photo = db.GetPhoto(albumId.Id, locationId.Id, authorId.Id);
Comment comment = db.GetComment(photo.Id);
DateTime start = DateTime.Now;
for (int i = 0; i < 1000; i++)
{
db.UpdateComment(new()
{
Id = comment.Id + i,
PostDate = PostDate,
Content = $"{Content}{i}",
PhotoId = photo.Id + i
});
}
DateTime end = DateTime.Now;
MessageBox.Show((end - start).Milliseconds.ToString(), "Время работы", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
private void button16_Click(object sender, EventArgs e)
{
DateTime start = DateTime.Now;
db.DeleteAllAlbums();
DateTime end = DateTime.Now;
MessageBox.Show((end - start).Milliseconds.ToString(), "Время работы", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
private void button17_Click(object sender, EventArgs e)
{
DateTime start = DateTime.Now;
db.DeleteAllLocations();
DateTime end = DateTime.Now;
MessageBox.Show((end - start).Milliseconds.ToString(), "Время работы", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
private void button18_Click(object sender, EventArgs e)
{
DateTime start = DateTime.Now;
db.DeleteAllAuthors();
DateTime end = DateTime.Now;
MessageBox.Show((end - start).Milliseconds.ToString(), "Время работы", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
private void button19_Click(object sender, EventArgs e)
{
DateTime start = DateTime.Now;
db.DeleteAllPhotos();
DateTime end = DateTime.Now;
MessageBox.Show((end - start).Milliseconds.ToString(), "Время работы", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
private void button20_Click(object sender, EventArgs e)
{
DateTime start = DateTime.Now;
db.DeleteAllComments();
DateTime end = DateTime.Now;
MessageBox.Show((end - start).Milliseconds.ToString(), "Время работы", MessageBoxButtons.OK, MessageBoxIcon.Information);
} }
} }
} }

View File

@ -117,4 +117,7 @@
<resheader name="writer"> <resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value> <value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader> </resheader>
<metadata name="menuStrip1.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>17, 17</value>
</metadata>
</root> </root>

View File

@ -3,6 +3,7 @@ using Database;
using System; using System;
using Commentbase; using Commentbase;
using Microsoft.VisualBasic; using Microsoft.VisualBasic;
using static System.Windows.Forms.DataFormats;
namespace View namespace View
{ {
@ -20,9 +21,17 @@ namespace View
Application.Run(_serviceProvider.GetRequiredService<MainForm>()); Application.Run(_serviceProvider.GetRequiredService<MainForm>());
} }
private static bool isPostgreSQL = true;
private static void ConfigureServices(ServiceCollection services) private static void ConfigureServices(ServiceCollection services)
{ {
services.AddTransient<Abstracts, Implements>(); if (isPostgreSQL)
{
services.AddTransient<Abstracts, ImplementsPostgres>();
}
else
{
services.AddTransient<Abstracts, ImplementsMongoDB>();
}
services.AddTransient<MainForm>(); services.AddTransient<MainForm>();
services.AddTransient<FormAlbum>(); services.AddTransient<FormAlbum>();
services.AddTransient<FormAlbums>(); services.AddTransient<FormAlbums>();
@ -35,5 +44,21 @@ namespace View
services.AddTransient<FormComment>(); services.AddTransient<FormComment>();
services.AddTransient<FormComments>(); services.AddTransient<FormComments>();
} }
public static void ChangeDB()
{
isPostgreSQL = !isPostgreSQL;
var services = new ServiceCollection();
ConfigureServices(services);
_serviceProvider = services.BuildServiceProvider();
}
public static void ChangeDB(bool newIsPostrgeSQL)
{
if (newIsPostrgeSQL == isPostgreSQL)
return;
isPostgreSQL = newIsPostrgeSQL;
var services = new ServiceCollection();
ConfigureServices(services);
_serviceProvider = services.BuildServiceProvider();
}
} }
} }