Skip to content

Added SQLite header parsing functionality and associated tests - #249

Merged
SRombauts merged 6 commits into
SRombauts:masterfrom
ptrks:add_header_info
Dec 30, 2019
Merged

Added SQLite header parsing functionality and associated tests#249
SRombauts merged 6 commits into
SRombauts:masterfrom
ptrks:add_header_info

Conversation

@ptrks

@ptrks ptrks commented Dec 30, 2019

Copy link
Copy Markdown
Contributor

Added header parsing functionality via a getHeaderInfo() function. This function reads the first 100 bytes of a SQLite database file and attempts to reconstruct byte groups into the specific header fields within a Header object. Also added tests to demonstrate updating header values via PRAGMA and verifying updated / default header values.

@ptrks

ptrks commented Dec 30, 2019

Copy link
Copy Markdown
Contributor Author

@SRombauts tell me what you think of this implementation. In the meantime I am looking into getting the tests to pass but still have access to fixed width integer datatypes.

@SRombauts

Copy link
Copy Markdown
Owner

About the compile error, you can use plain old C type (unsigned char/short/int). Alternatively you could apply your pull request to the slqitcpp-3.x branch that is c++11

@ptrks

ptrks commented Dec 30, 2019

Copy link
Copy Markdown
Contributor Author

@SRombauts I am going to go ahead and replace with plain C types for now so it can be available in master. I'll also open a PR with fixed width support for the C+11 branch. Thanks!

@coveralls

coveralls commented Dec 30, 2019

Copy link
Copy Markdown

Coverage Status

Coverage remained the same at 100.0% when pulling fe14559 on patrick--:add_header_info into 54c7a18 on SRombauts:master.

@SRombauts

Copy link
Copy Markdown
Owner

I left a bunch of comments, but it looks good overall!

@SRombauts

Copy link
Copy Markdown
Owner

Also don't bother to create a different version for c++11, I don't want that we go an rewrite all just for the sake of it

@SRombauts

SRombauts commented Dec 30, 2019

Copy link
Copy Markdown
Owner

And finally, I have just reached 100% unit test code coverage (for what it's worth) so you should test all cases of error

https://coveralls.io/builds/27840818/source?filename=src/Database.cpp

Basically you have to test also with no filename, no file, and wrong file header

@ptrks

ptrks commented Dec 30, 2019

Copy link
Copy Markdown
Contributor Author

@SRombauts okay I will get those tests added.

@ptrks

ptrks commented Dec 30, 2019

Copy link
Copy Markdown
Contributor Author

@SRombauts you mentioned leaving some comments on the code? I might be blind, but I'm not seeing those anywhere.

Comment thread src/Database.cpp Outdated
return h;
}

const SQLite::Exception exception("Could not open database, the aFilename parameter was empty.");

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you perhaps put this exception at the start of the function like I did yesterday on a cleanup commit? It would avoid the return being inside the scope.

Comment thread src/Database.cpp Outdated
if (fileBuffer.is_open())
{
fileBuffer.seekg(0, std::ios::beg);
fileBuffer.read(reinterpret_cast<char*>(&buf[0]), 100);

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be better to avoid these cast; perhaps have one additional "pointer of char" variable pointing to the buf.

unsigned char buf[100];
char* pBuf = reinterpret_cast<char*>(&buf[0]);

Comment thread src/Database.cpp Outdated
}

// If the "magic string" can't be found then header is invalid, corrupt or unreadable
if (!strncmp(reinterpret_cast<const char*>(h.headerStr), "SQLite format 3", 15) == 0)

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similarly here, headerStr could simply be a char[] variable directly on the header structure

Comment thread src/Database.cpp Outdated
// If the "magic string" can't be found then header is invalid, corrupt or unreadable
if (!strncmp(reinterpret_cast<const char*>(h.headerStr), "SQLite format 3", 15) == 0)
{
const SQLite::Exception exception("Invalid SQLite header");

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or encrypted database

@SRombauts

Copy link
Copy Markdown
Owner

Sorry, my bad, I didn't know this new review system needed a submit when finished

…of function calls and cleared up invalid header exception message
@SRombauts SRombauts self-assigned this Dec 30, 2019
@SRombauts
SRombauts merged commit b5c0a08 into SRombauts:master Dec 30, 2019
@SRombauts

Copy link
Copy Markdown
Owner

That's very good, thanks a lot!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants