Skip to content

Commit cc93afb

Browse files
committed
Implemented Unicode support in Command class
1 parent 0948f42 commit cc93afb

File tree

5 files changed

+46
-63
lines changed

5 files changed

+46
-63
lines changed

redis-desktop-manager/include/redis/Command.h

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,15 @@ class Command
2222
int getDbIndex() const;
2323

2424
/** @see http://redis.io/topics/protocol for more info **/
25-
QString getFormattedString() const;
25+
QByteArray getByteRepresentation() const;
26+
static QByteArray getByteRepresentation(const QStringList&);
27+
static QByteArray getByteRepresentation(const QString&);
2628

2729
QString getRawString() const;
2830

2931
QObject * getOwner();
3032
void setOwner(QObject *);
3133

32-
static QString getFormatted(const QStringList&);
33-
34-
static QString getFormatted(const QString&);
35-
3634
bool hasCallback();
3735

3836
QString getCallbackName();

redis-desktop-manager/source/redis/Command.cpp

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,6 @@ int Command::getDbIndex() const
5959
return dbIndex;
6060
}
6161

62-
QString Command::getFormattedString() const
63-
{
64-
return Command::getFormatted(commandWithArguments);
65-
}
66-
6762
QString Command::getRawString() const
6863
{
6964
return commandWithArguments.join(' ');
@@ -84,31 +79,33 @@ void Command::setOwner(QObject * o)
8479
owner = o;
8580
}
8681

87-
QString Command::getFormatted(const QString& command)
82+
QByteArray Command::getByteRepresentation(const QString& command)
8883
{
89-
QStringList parts = command.split(" ", QString::SkipEmptyParts, Qt::CaseInsensitive);
84+
QStringList parts = command.split(" ", QString::SkipEmptyParts, Qt::CaseInsensitive);
9085

91-
QString formattedCmd("*");
92-
formattedCmd.append(QString("%1\r\n").arg(parts.length()));
93-
94-
for (QString part : parts) {
95-
formattedCmd.append(QString("$%1\r\n%2\r\n")
96-
.arg(QString("%1").arg(part.length()), part));
97-
}
98-
99-
return formattedCmd;
86+
return Command::getByteRepresentation(parts);
10087
}
10188

102-
QString Command::getFormatted(const QStringList& command)
89+
QByteArray Command::getByteRepresentation(const QStringList& command)
10390
{
104-
QString formattedCmd("*");
105-
formattedCmd.append(QString("%1\r\n").arg(command.length()));
91+
QByteArray result;
92+
result.append(QString("*%1\r\n").arg(command.length()));
93+
94+
QByteArray partArray;
10695

10796
for (QString part : command) {
108-
formattedCmd.append(QString("$%1\r\n%2\r\n")
109-
.arg(QString("%1").arg(part.length()), part));
97+
partArray = part.toUtf8();
98+
result.append("$");
99+
result.append(QString::number(partArray.size()));
100+
result.append("\r\n");
101+
result.append(partArray);
102+
result.append("\r\n");
110103
}
111104

112-
return formattedCmd;
105+
return result;
113106
}
114107

108+
QByteArray Command::getByteRepresentation() const
109+
{
110+
return Command::getByteRepresentation(commandWithArguments);
111+
}

redis-desktop-manager/source/redis/RedisConnection.cpp

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -68,15 +68,11 @@ QVariant RedisConnection::execute(QString command)
6868
if (command.isEmpty()) {
6969
return QVariant();
7070
}
71-
72-
QString formattedCommand = Command::getFormatted(command);
73-
74-
/*
75-
* Send command
76-
*/
77-
QTextStream out(socket);
78-
out << formattedCommand;
79-
out.flush();
71+
72+
// Send command
73+
QByteArray cmd = Command::getByteRepresentation(command);
74+
socket->write(cmd);
75+
socket->flush();
8076

8177
if (!socket->waitForReadyRead(config.executeTimeout)) {
8278

@@ -130,16 +126,12 @@ void RedisConnection::runCommand(const Command &command)
130126

131127
if (command.isEmpty()) {
132128
return sendResponse();
133-
}
134-
135-
QString formattedCommand = command.getFormattedString();
129+
}
136130

137-
/*
138-
* Send command
139-
*/
140-
QTextStream out(socket);
141-
out << formattedCommand;
142-
out.flush();
131+
// Send command
132+
QByteArray cmd = command.getByteRepresentation();
133+
socket->write(cmd);
134+
socket->flush();
143135
}
144136

145137
void RedisConnection::readyRead()

redis-desktop-manager/source/redis/RedisConnectionOverSsh.cpp

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -206,12 +206,10 @@ QVariant RedisConnectionOverSsh::execute(QString command)
206206
return QVariant();
207207
}
208208

209-
QString formattedCommand = Command::getFormatted(command);
210-
211209
/*
212210
* Send command
213211
*/
214-
QByteArray byteArray = formattedCommand.toUtf8();
212+
QByteArray byteArray = Command::getByteRepresentation(command);
215213
const char* cString = byteArray.constData();
216214

217215
socket->write(cString, byteArray.size());
@@ -283,14 +281,12 @@ void RedisConnectionOverSsh::runCommand(const Command &command)
283281

284282
if (command.isEmpty()) {
285283
return sendResponse();
286-
}
287-
288-
QString formattedCommand = command.getFormattedString();
284+
}
289285

290286
/*
291287
* Send command
292288
*/
293-
QByteArray byteArray = formattedCommand.toUtf8();
289+
QByteArray byteArray = command.getByteRepresentation();
294290
const char* cString = byteArray.constData();
295291

296292
socket->write(cString, byteArray.size());

tests/source/test_command.cpp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,17 @@
22
#include "Command.h"
33

44
#include <QTest>
5-
6-
void TestCommand::prepareCommand()
7-
{
8-
//given
9-
//Command class
10-
11-
//when
12-
QVariant actualResult = Command::getFormatted("EXISTS testkey:test");
13-
14-
//then
15-
QCOMPARE(actualResult, QVariant("*2\r\n$6\r\nEXISTS\r\n$12\r\ntestkey:test\r\n"));
5+
6+
void TestCommand::prepareCommand()
7+
{
8+
//given
9+
//Command class
10+
11+
//when
12+
QByteArray actualResult = Command::getByteRepresentation("EXISTS testkey:test");
13+
14+
//then
15+
QCOMPARE(actualResult, QByteArray("*2\r\n$6\r\nEXISTS\r\n$12\r\ntestkey:test\r\n"));
1616
}
1717

1818

0 commit comments

Comments
 (0)