теперь при клонировании данных из постгрес, последовательности тоже клонируются

This commit is contained in:
Timourka 2024-05-08 01:49:23 +04:00
parent 41bd027470
commit dea4fd15db
4 changed files with 119 additions and 11 deletions

View File

@ -143,6 +143,19 @@ namespace Forms
implementationMongoDB.AddCar(it);
foreach (var it in listRentals)
implementationMongoDB.AddRental(it);
// забираем информацию о последовательностях
var listSequence = implementationPostgres.GetSequences();
foreach (var it in listSequence)
if (it.Id == "car_model" ||
it.Id == "branch" ||
it.Id == "car" ||
it.Id == "client" ||
it.Id == "rental"
)
implementationMongoDB.UpdateSequence(it);
else
throw new Exception("неправильнй id последовательности");
}
/*

View File

@ -27,6 +27,7 @@ namespace database
_branchCollection = _database.GetCollection<Branch>("branch");
_statusCollection = _database.GetCollection<Status>("status");
_bodyTypeCollection = _database.GetCollection<BodyType>("body_type");
_sequenceCollection = _database.GetCollection<Sequence>("sequence");
}
// CDUD îïåðàöèè äëÿ êàæäîé ñóùíîñòè
@ -37,8 +38,8 @@ namespace database
if (car.Id == 0)
{
Sequence sequence = _sequenceCollection.Find(seq => seq.Id == "car").FirstOrDefault();
sequence.count++;
car.Id = sequence.count;
sequence.Count++;
car.Id = sequence.Count;
_sequenceCollection.ReplaceOne(seq => seq.Id == "car", sequence);
}
_carCollection.InsertOne(car);
@ -70,8 +71,8 @@ namespace database
if (client.Id == 0)
{
Sequence sequence = _sequenceCollection.Find(seq => seq.Id == "client").FirstOrDefault();
sequence.count++;
client.Id = sequence.count;
sequence.Count++;
client.Id = sequence.Count;
_sequenceCollection.ReplaceOne(seq => seq.Id == "client", sequence);
}
_clientCollection.InsertOne(client);
@ -103,8 +104,8 @@ namespace database
if (rental.Id == 0)
{
Sequence sequence = _sequenceCollection.Find(seq => seq.Id == "rental").FirstOrDefault();
sequence.count++;
rental.Id = sequence.count;
sequence.Count++;
rental.Id = sequence.Count;
_sequenceCollection.ReplaceOne(seq => seq.Id == "rental", sequence);
}
_rentalCollection.InsertOne(rental);
@ -136,8 +137,8 @@ namespace database
if (carModel.Id == 0)
{
Sequence sequence = _sequenceCollection.Find(seq => seq.Id == "car_model").FirstOrDefault();
sequence.count++;
carModel.Id = sequence.count;
sequence.Count++;
carModel.Id = sequence.Count;
_sequenceCollection.ReplaceOne(seq => seq.Id == "car_model", sequence);
}
_carModelCollection.InsertOne(carModel);
@ -169,8 +170,8 @@ namespace database
if (branch.Id == 0)
{
Sequence sequence = _sequenceCollection.Find(seq => seq.Id == "branch").FirstOrDefault();
sequence.count++;
branch.Id = sequence.count;
sequence.Count++;
branch.Id = sequence.Count;
_sequenceCollection.ReplaceOne(seq => seq.Id == "branch", sequence);
}
_branchCollection.InsertOne(branch);
@ -247,5 +248,10 @@ namespace database
{
_bodyTypeCollection.DeleteOne(bodyType => bodyType.Id == id);
}
public void UpdateSequence(Sequence sequence)
{
_sequenceCollection.ReplaceOne(seq => seq.Id == sequence.Id, sequence);
}
}
}

View File

@ -1,4 +1,5 @@
using Npgsql;
using System.Collections.Generic;
namespace database
{
@ -497,5 +498,93 @@ namespace database
using var cmd = new NpgsqlCommand($"DELETE FROM body_type WHERE id = {id}", conn);
cmd.ExecuteNonQuery();
}
public List<Sequence> GetSequences()
{
List < Sequence > sequences = new List<Sequence>();
{
using var conn = GetConnection();
conn.Open();
using var cmd = new NpgsqlCommand("SELECT sequencename, last_value " +
"FROM pg_sequences " +
"WHERE sequencename = 'client_id_seq'", conn);
using var reader = cmd.ExecuteReader();
if (reader.Read())
{
sequences.Add(new Sequence
{
Id = "client",
Count = reader.GetInt32(1)
});
}
}
{
using var conn = GetConnection();
conn.Open();
using var cmd = new NpgsqlCommand("SELECT sequencename, last_value " +
"FROM pg_sequences " +
"WHERE sequencename = 'branch_id_seq'", conn);
using var reader = cmd.ExecuteReader();
if (reader.Read())
{
sequences.Add(new Sequence
{
Id = "branch",
Count = reader.GetInt32(1)
});
}
}
{
using var conn = GetConnection();
conn.Open();
using var cmd = new NpgsqlCommand("SELECT sequencename, last_value " +
"FROM pg_sequences " +
"WHERE sequencename = 'carmodel_id_seq'", conn);
using var reader = cmd.ExecuteReader();
if (reader.Read())
{
sequences.Add(new Sequence
{
Id = "car_model",
Count = reader.GetInt32(1)
});
}
}
{
using var conn = GetConnection();
conn.Open();
using var cmd = new NpgsqlCommand("SELECT sequencename, last_value " +
"FROM pg_sequences " +
"WHERE sequencename = 'car_id_seq'", conn);
using var reader = cmd.ExecuteReader();
if (reader.Read())
{
sequences.Add(new Sequence
{
Id = "car",
Count = reader.GetInt32(1)
});
}
}
{
using var conn = GetConnection();
conn.Open();
using var cmd = new NpgsqlCommand("SELECT sequencename, last_value " +
"FROM pg_sequences " +
"WHERE sequencename = 'rental_id_seq'", conn);
using var reader = cmd.ExecuteReader();
if (reader.Read())
{
sequences.Add(new Sequence
{
Id = "rental",
Count = reader.GetInt32(1)
});
}
}
return sequences;
}
}
}

View File

@ -82,6 +82,6 @@ namespace database
{
[BsonRepresentation(BsonType.String)]
public string Id { get; set; }
public int count { get; set; }
public int Count { get; set; }
}
}