Академический Документы
Профессиональный Документы
Культура Документы
Introduction
Generate sequential GUIDs in C++ using the Qt framework. This is a rewrite of the existing C# article here (http://www.codeproject.com
/Articles/388157/GUIDs-as-fast-primary-keys-under-multiple-database). That article contains an excellent description of why you would
want sequential GUIDs.
Background
Databases insert sequential information quickly. When you insert information out of order it requires the database to rebuild indexes
which can be time consuming. Get the advantages of sequential numbering for you database records while getting the benefit of unique
ids that a GUID gives.
#ifndef SEQUENTIALGUID_H
#define SEQUENTIALGUID_H
#include <QObject>
#include <QUuid>
#include <QByteArray>
#include <QDateTime>
#include <QDataStream>
#include <QTime>
enum SequentialGUIDType {
SequentialAsString = 0,
SequentialAsBinary = 1,
SequentialAtEnd = 2
};
signals:
public slots:
private:
1 of 5 18/04/17 11:52
Sequential GUIDs in C++ with Qt - CodeProject https://www.codeproject.com/Tips/594304/Sequen...
#endif // SEQUENTIALGUID_H
#include "sequentialguid.h"
SequentialGUID::SequentialGUID(QObject *parent) :
QObject(parent)
{
}
void SequentialGUID::InitRand() {
if (!rand_init) {
QTime t = QTime::currentTime();
qsrand((uint) t.msec());
rand_init = true;
}
}
QByteArray tsBytes;
// Use the correct endian so we get a proper sequence (e.g. counting up digits
// on the right so it sorts properly in the database)
#if Q_BYTE_ORDER == Q_LITTLE_ENDIAN
tsBytes = timestampBytesRev;
#else
tsBytes = timestampBytes;
#endif
QByteArray guidBytes;
switch (guid_type) {
2 of 5 18/04/17 11:52
Sequential GUIDs in C++ with Qt - CodeProject https://www.codeproject.com/Tips/594304/Sequen...
case SequentialAsString:
case SequentialAsBinary:
guidBytes.append(tsBytes);
guidBytes.append(randBytes);
break;
case SequentialAtEnd:
guidBytes.append(randBytes);
guidBytes.append(tsBytes);
break;
}
Points of Interest
Random numbers are NOT generated using a cryptographically secure method. For my particular case, this wasn't important. The
original article used the .Net framework System.Security.Cryptography.RNGCryptoServiceProvider(); to do so. I didn't
know the code to do so in C++ off the top of my head.
Rule of thumb us to use SequentialAtEnd for MSSql servers, and the others for most other database systems (look under "Using The
Code" section in original article for a description).
History
5/17/2013 - Initial posting of code
License
This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)
Share
3 of 5 18/04/17 11:52
Sequential GUIDs in C++ with Qt - CodeProject https://www.codeproject.com/Tips/594304/Sequen...
EMAIL TWITTER
We specialize in custom software development for your business. With 10+ years of experience,
you can be be sure that you are getting the best code that money can buy.
Search Comments Go
General News Suggestion Question Bug Answer Joke Praise Rant Admin
4 of 5 18/04/17 11:52
Sequential GUIDs in C++ with Qt - CodeProject https://www.codeproject.com/Tips/594304/Sequen...
Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.
Permalink | Advertise | Privacy | Terms of Use | Mobile Layout: fixed | Article Copyright 2013 by computermagic
Web02 | 2.8.170308.1 | Last Updated 17 May 2013 Seleccionar idioma fluid Everything else Copyright CodeProject, 1999-2017
5 of 5 18/04/17 11:52