-
Notifications
You must be signed in to change notification settings - Fork 1
History/database helper #4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jayjay-w
wants to merge
11
commits into
master
Choose a base branch
from
history/database_helper
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
2822dc6
Add DatabaseConstants class
jayjay-w f2bc751
Add DatabaseManager class
jayjay-w 64df940
Enable logging of chats
jayjay-w 85bc818
Add SQL Code for getting contact list
jayjay-w 9896325
Prefix sql query strings with sql_*
jayjay-w fa621a1
Add TODO comment for a pending task
jayjay-w ad711be
Whitespace Fix
jayjay-w 2de590f
Rename ChatHistoryHandler to ChatHistoryPlugin
jayjay-w 0e9ec03
Use DatabaseConstants for query column names
jayjay-w a8e9dfc
Fix line lengths
jayjay-w ea69c93
Catch database errors
jayjay-w File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| #include "chathistoryplugin.h" | ||
| #include "databasemanager.h" | ||
| #include "kopetemessage.h" | ||
| #include "kopetechatsessionmanager.h" | ||
| #include <kaboutdata.h> | ||
| #include <kgenericfactory.h> | ||
| #include "kapplication.h" | ||
|
|
||
| ChatHistoryPlugin *ChatHistoryPlugin::mInstance = 0; | ||
|
|
||
| K_PLUGIN_FACTORY(ChatHistoryPluginFactory, registerPlugin<ChatHistoryPlugin>();) | ||
| K_EXPORT_PLUGIN(ChatHistoryPluginFactory( "kopete_history" )) | ||
|
|
||
| ChatHistoryPlugin::ChatHistoryPlugin(QObject *parent, const QVariantList &) | ||
| : Kopete::Plugin(ChatHistoryPluginFactory::componentData(), parent) | ||
| { | ||
| //Initialize the database. | ||
| //TODO: Implement other DB Systems (MySQL, PostgreSQL etc) | ||
| DatabaseManager::instance()->initDatabase(DatabaseManager::SQLITE); | ||
| connect (Kopete::ChatSessionManager::self(), SIGNAL(aboutToDisplay(Kopete::Message&)), | ||
| this, SLOT(logMessage(Kopete::Message&))); | ||
| } | ||
|
|
||
| ChatHistoryPlugin::~ChatHistoryPlugin() | ||
| { | ||
|
|
||
| } | ||
|
|
||
| ChatHistoryPlugin *ChatHistoryPlugin::instance() | ||
| { | ||
| if (!mInstance) { | ||
| mInstance = new ChatHistoryPlugin(0, QVariantList()); | ||
| } | ||
|
|
||
| return mInstance; | ||
| } | ||
|
|
||
| void ChatHistoryPlugin::logMessage(Kopete::Message &message) | ||
| { | ||
| DatabaseManager::instance()->insertMessage(message); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| #ifndef CHATHISTORYPLUGIN_H | ||
| #define CHATHISTORYPLUGIN_H | ||
|
|
||
| #include <QVariantList> | ||
| #include "kopeteplugin.h" | ||
| #include <QObject> | ||
|
|
||
| namespace Kopete { | ||
| class Message; | ||
| } | ||
|
|
||
| /** | ||
| * The ChatHistoryPlugin class is used to handle all history connected activities, and | ||
| * then call the respective class (logger, searcher etc). | ||
| */ | ||
| class ChatHistoryPlugin : public Kopete::Plugin | ||
| { | ||
| Q_OBJECT | ||
| public: | ||
| /** | ||
| * Constructs a new ChatHistoryPlugin class instance. There should only be one | ||
| * instance for every instance of Kopete running. | ||
| */ | ||
| explicit ChatHistoryPlugin(QObject *parent, const QVariantList &); | ||
|
|
||
| ~ChatHistoryPlugin(); | ||
| static ChatHistoryPlugin *instance(); | ||
|
|
||
| public slots: | ||
| /** | ||
| * Insert a new chat message to the database. | ||
| * @param message The message to be logged. The message details to be stored | ||
| * in the database will be extracted from here. | ||
| */ | ||
| void logMessage(Kopete::Message &message); | ||
| private: | ||
| static ChatHistoryPlugin *mInstance; | ||
| }; | ||
|
|
||
| #endif // CHATHISTORYPLUGIN_H |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,140 @@ | ||
| #include "databaseconstants.h" | ||
|
|
||
| QString DatabaseConstants::columnId() | ||
| { | ||
| return "id"; | ||
| } | ||
|
|
||
| QString DatabaseConstants::columnTimeStamp() | ||
| { | ||
| return "timestamp"; | ||
| } | ||
|
|
||
| QString DatabaseConstants::columnMessage() | ||
| { | ||
| return "message"; | ||
| } | ||
|
|
||
| QString DatabaseConstants::columnProtocol() | ||
| { | ||
| return "protocol"; | ||
| } | ||
|
|
||
| QString DatabaseConstants::columnAccount() | ||
| { | ||
| return "account"; | ||
| } | ||
|
|
||
| QString DatabaseConstants::columnDirection() | ||
| { | ||
| return "direction"; | ||
| } | ||
|
|
||
| QString DatabaseConstants::columnImportance() | ||
| { | ||
| return "importance"; | ||
| } | ||
|
|
||
| QString DatabaseConstants::columnContact() | ||
| { | ||
| return "contact"; | ||
| } | ||
|
|
||
| QString DatabaseConstants::columnSubject() | ||
| { | ||
| return "subject"; | ||
| } | ||
|
|
||
| QString DatabaseConstants::columnSession() | ||
| { | ||
| return "session"; | ||
| } | ||
|
|
||
| QString DatabaseConstants::columnSessionName() | ||
| { | ||
| return "session_name"; | ||
| } | ||
|
|
||
| QString DatabaseConstants::columnFrom() | ||
| { | ||
| return "from"; | ||
| } | ||
|
|
||
| QString DatabaseConstants::columnFromName() | ||
| { | ||
| return "from_name"; | ||
| } | ||
|
|
||
| QString DatabaseConstants::columnTo() | ||
| { | ||
| return "to"; | ||
| } | ||
|
|
||
| QString DatabaseConstants::columnToName() | ||
| { | ||
| return "to_name"; | ||
| } | ||
|
|
||
| QString DatabaseConstants::columnState() | ||
| { | ||
| return "state"; | ||
| } | ||
|
|
||
| QString DatabaseConstants::columnType() | ||
| { | ||
| return "type"; | ||
| } | ||
|
|
||
| QString DatabaseConstants::columnIsGroup() | ||
| { | ||
| return "is_group"; | ||
| } | ||
|
|
||
| QString DatabaseConstants::sql_prepareForMessageInsert() | ||
| { | ||
| return "INSERT INTO `messages` (`" + columnTimeStamp() + "`, `" + columnMessage() | ||
| + "`, `" + columnAccount() + "`, `" + columnProtocol() + "`, `" | ||
| + columnDirection() + "`, `" + columnImportance() + "`, `" | ||
| + columnContact() + "`, `" + columnSubject() + "`, " | ||
| " `" + columnSession() + "`, `" + columnSessionName() + "`, `" | ||
| + columnFrom() + "`, `" + columnFromName() + "`, `" + columnTo() | ||
| + "`, `" + columnToName() + "`, `" + columnState() + "`, `" | ||
| + columnType() + "`, `" + columnIsGroup() + "`) " | ||
| " VALUES (:" + columnTimeStamp() + ", :" + columnMessage() + ", :" | ||
| + columnAccount() + ", :" + columnProtocol() + ", :" | ||
| + columnDirection() + ", :" + columnImportance() + ", :" | ||
| + columnContact() + ", :" + columnSubject() + ", :" | ||
| + columnSession() + ", :" + columnSessionName() + ", " | ||
| " :" + columnFrom() + ", :" + columnFromName() + ", :" | ||
| + columnTo() + ", :" + columnToName() + ", :" | ||
| + columnState() + ", :" + columnType() + ", :" | ||
| + columnIsGroup() + ")"; | ||
| } | ||
|
|
||
| QString DatabaseConstants::sql_createMessagesTable() | ||
| { | ||
| return "CREATE TABLE IF NOT EXISTS \"messages\" (" | ||
| "\"" + columnId() + "\" Integer Primary Key Autoincrement Not Null, " | ||
| "\"" + columnTimeStamp() + "\" Integer, " | ||
| "\"" + columnMessage() + "\" Text, " | ||
| "\"" + columnProtocol() + "\" Text Not Null, " | ||
| "\"" + columnAccount() + "\" Text Not Null, " | ||
| "\"" + columnDirection() + "\" Integer Not Null, " | ||
| "\"" + columnImportance() + "\" Integer, " | ||
| "\"" + columnContact() + "\" Text, " | ||
| "\"" + columnSubject() + "\" Text, " | ||
| "\"" + columnSession() + "\" Text, " | ||
| "\"" + columnSessionName() + "\" Text, " | ||
| "\"" + columnFrom() + "\" Text, " | ||
| "\"" + columnFromName() + "\" Text, " | ||
| "\"" + columnTo() + "\" Text, " | ||
| "\"" + columnToName() + "\" Text, " | ||
| "\"" + columnState() + "\" Integer, " | ||
| "\"" + columnType() + "\" Integer, " | ||
| "\"" + columnIsGroup() + "\" Integer Default'0') "; | ||
| } | ||
|
|
||
| QString DatabaseConstants::sql_getContactList(QString accountId) | ||
| { | ||
| return "SELECT DISTINCT contact, protocol FROM messages WHERE account = '" + accountId + "'"; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| #ifndef DATABASECONSTANTS_H | ||
| #define DATABASECONSTANTS_H | ||
|
|
||
| #include <QObject> | ||
|
|
||
| /** | ||
| * The DatabaseConstants class is used to provide static representations | ||
| * of the various strings used by the database in the history plugin. | ||
| */ | ||
| class DatabaseConstants : public QObject | ||
| { | ||
| Q_OBJECT | ||
| public: | ||
| explicit DatabaseConstants(QObject *parent = 0); | ||
| /** | ||
| * These are the names of the various database columns | ||
| */ | ||
| static QString columnId(); | ||
| static QString columnTimeStamp(); | ||
| static QString columnMessage(); | ||
| static QString columnProtocol(); | ||
| static QString columnAccount(); | ||
| static QString columnDirection(); | ||
| static QString columnImportance(); | ||
| static QString columnContact(); | ||
| static QString columnSubject(); | ||
| static QString columnSession(); | ||
| static QString columnSessionName(); | ||
| static QString columnFrom(); | ||
| static QString columnFromName(); | ||
| static QString columnTo(); | ||
| static QString columnToName(); | ||
| static QString columnState(); | ||
| static QString columnType(); | ||
| static QString columnIsGroup(); | ||
|
|
||
| /** | ||
| * Query string for inserting a new message | ||
| */ | ||
| static QString sql_prepareForMessageInsert(); | ||
|
|
||
| /** | ||
| * Query string for creating the messsages table | ||
| */ | ||
| static QString sql_createMessagesTable(); | ||
|
|
||
| /** | ||
| * Query string to retrieve the contacts in an account who have chat history. | ||
| * @param accountId The account to search for. | ||
| * @return | ||
| */ | ||
| static QString sql_getContactList(QString accountId = QString()); | ||
| }; | ||
|
|
||
| #endif // DATABASECONSTANTS_H |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there any other way of using database statements, other than generating string queries ourselves ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am working on seeing if there is another way to do this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about we write for ourselves a really Simple Query builder ? we can either use a
Builder Patternor anything else of your choice.