Skip to content
This repository has been archived by the owner on Apr 4, 2024. It is now read-only.

Use STRICT tables #1

Closed
jirislaby opened this issue Mar 22, 2023 · 4 comments
Closed

Use STRICT tables #1

jirislaby opened this issue Mar 22, 2023 · 4 comments

Comments

@jirislaby
Copy link

jirislaby commented Mar 22, 2023

if (asprintf (&sql, "CREATE TABLE IF NOT EXISTS Lastlog(Name TEXT PRIMARY KEY, Time INT, TTY TEXT, RemoteHost TEXT);"

Append STRICT at the end of CREATE TABLE. This might save you a lot of pain -- the types are really what they are supposed to be. That means you have to switch from INT to INTEGER too.

Should be TIME updated by a TRIGGER (unlikely as you set the time from stat AFAICS) and have a default value of CURRENT_TIMESTAMP?

Also I would expect NOT NULL in some columns. I suppose, UNIQUE is only the name and that is by default via PRIMARY KEY.

@thkukuk
Copy link
Owner

thkukuk commented Mar 22, 2023

Ok, I understand the "STRICT" and added that, but I don't understand your comments about "NOT NULL", "UNIQUE" and "PRIMARY". I just followed the sqlite tutorial/introduction, first time I use sqlite.
NAME is the unique key and never empty, all other fields could be theoretically empty (TTY and RemoteHost for sure, not sure about TIME, but I think the some utilities clear that in some cases, too).

About "TIME": no, time should not be automatically set or updated.

thkukuk added a commit that referenced this issue Mar 22, 2023
@jirislaby
Copy link
Author

Ok, everything is fine then.

@jirislaby
Copy link
Author

jirislaby commented Mar 22, 2023

Actually thinking about it more and looking into the code. You save '' as empty tty and rhost, for example:

lastlog2/lib/lastlog2.c

Lines 184 to 186 in be609c4

"REPLACE INTO Lastlog VALUES('%s', %llu, '%s', '%s');",
user, (long long int)ll_time, tty ? tty : "",
rhost ? rhost : "") < 0)

You never use NULL values AFAICS. The question is if you want/expect NULL values in the columns in the database. They behave sort of differently than empty ('') values. If not, I would do: tty TEXT NOT NULL DEFAULT '' and similar.

@jirislaby jirislaby reopened this Mar 22, 2023
@thkukuk
Copy link
Owner

thkukuk commented Mar 22, 2023

I'm using NULL values, it was only because of the asprintf that this was mapped to empty strings.
I hope the code is now fine regarding sqlite3.
Thanks for the hints!

@thkukuk thkukuk closed this as completed Mar 22, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants