97 lines
3.2 KiB
C++
97 lines
3.2 KiB
C++
#include <cstdlib>
|
|
#include <ctime>
|
|
#include <iostream>
|
|
#include <sqlite3.h>
|
|
#include <string>
|
|
#include <sstream>
|
|
|
|
#include "db_info.h"
|
|
#include "LicenceOperations.h"
|
|
|
|
constexpr int totalLicences = 50'000'000;
|
|
constexpr float offenceRate = 0.2;
|
|
|
|
/// Generate a random string of numbers and letters
|
|
/// ASCII letters are contiguous so juste add a random offset to 'a'.
|
|
std::string createRandomID() {
|
|
char newID[IDLength];
|
|
for (char& idChar : newID) {
|
|
if (rand() % 2) {
|
|
idChar = 'a' + rand() % 26;
|
|
} else {
|
|
idChar = '0' + rand() % 10;
|
|
}
|
|
}
|
|
return newID;
|
|
}
|
|
|
|
int main()
|
|
{
|
|
srand(time(nullptr));
|
|
|
|
sqlite3* db;
|
|
char* errorMessage = nullptr;
|
|
int errorCode;
|
|
|
|
errorCode = sqlite3_open(dbName.c_str(), &db);
|
|
// errorCode = sqlite3_open("TestDB.sqlite3", &db);
|
|
if (errorCode) {
|
|
std::cerr << "Error opening database" << std::endl << sqlite3_errmsg(db) << std::endl;
|
|
sqlite3_close(db);
|
|
return -1;
|
|
}
|
|
|
|
errorCode = sqlite3_exec(db, ("CREATE TABLE IF NOT EXISTS " + pointsTable + "("
|
|
+ keyColumn + " UNSIGNED BIG INT PRIMARY KEY NOT NULL,"
|
|
+ idColumn + " TEXT NOT NULL,"
|
|
+ pointsColumn + " INT NOT NULL,"
|
|
+ countdownColumn + " INT NOT NULL,"
|
|
+ onePointCountdownColumn + " INT NOT NULL,"
|
|
+ tenYearsCountdownColumn + " INT NOT NULL,"
|
|
+ graveOffenceColumn + " INT NOT NULL);").c_str(),
|
|
nullptr, nullptr, &errorMessage);
|
|
|
|
if (errorCode) {
|
|
dbError("Error creating table " + pointsTable, errorMessage);
|
|
sqlite3_close(db);
|
|
return -1;
|
|
}
|
|
|
|
// Batch all inits
|
|
errorCode = sqlite3_exec(db, "BEGIN TRANSACTION;", nullptr, nullptr, &errorMessage);
|
|
if (errorCode) {
|
|
dbError("Error while beginning transaction", errorMessage);
|
|
sqlite3_close(db);
|
|
return -1;
|
|
}
|
|
|
|
std::ios::sync_with_stdio(false);
|
|
std::time_t startTime = std::time(nullptr);
|
|
for (int i = 0; i < totalLicences; i++) {
|
|
const std::string& ID = createRandomID();
|
|
errorCode = addNewLicence(db, ID);
|
|
if (rand() < RAND_MAX * offenceRate) {
|
|
applyOffence(db, ID, rand() % 6 + 1, (offenceClass)(rand() % 5 + 1));
|
|
}
|
|
|
|
if (!(i % 10000)) {
|
|
std::cout << "Created " << i << " licences" << std::endl;
|
|
}
|
|
|
|
if (errorCode) {
|
|
break;
|
|
}
|
|
}
|
|
|
|
errorCode = sqlite3_exec(db, "END TRANSACTION;", nullptr, nullptr, &errorMessage);
|
|
if (errorCode) {
|
|
dbError("Error while closing transaction", errorMessage);
|
|
sqlite3_close(db);
|
|
return -1;
|
|
}
|
|
|
|
std::cout << "Db created successfully !" << std::endl;
|
|
std::cout << totalLicences << " licences created in " << std::time(nullptr) - startTime << std::endl;
|
|
sqlite3_close(db);
|
|
return 0;
|
|
}
|