dbms: improvement [#CONV-2546].

This commit is contained in:
Alexey Milovidov 2011-06-15 18:54:18 +00:00
parent 236ee3303e
commit 36517e32a2
4 changed files with 67 additions and 6 deletions

View File

@ -217,6 +217,8 @@ void readEscapedString(String & s, ReadBuffer & buf);
void readQuotedString(String & s, ReadBuffer & buf); void readQuotedString(String & s, ReadBuffer & buf);
void readDoubleQuotedString(String & s, ReadBuffer & buf);
/// в формате YYYY-MM-DD /// в формате YYYY-MM-DD
inline void readDateText(Yandex::DayNum_t & date, ReadBuffer & buf) inline void readDateText(Yandex::DayNum_t & date, ReadBuffer & buf)

View File

@ -93,6 +93,9 @@ inline void writeQuotedString(const String & s, WriteBuffer & buf)
writeChar('\'', buf); writeChar('\'', buf);
} }
/// Совместимо с JSON.
void writeDoubleQuotedString(const String & s, WriteBuffer & buf);
/// в формате YYYY-MM-DD /// в формате YYYY-MM-DD
inline void writeDateText(Yandex::DayNum_t date, WriteBuffer & buf) inline void writeDateText(Yandex::DayNum_t date, WriteBuffer & buf)

View File

@ -58,12 +58,14 @@ void readEscapedString(String & s, ReadBuffer & buf)
} }
} }
void readQuotedString(String & s, ReadBuffer & buf)
template <char quote>
static void readAnyQuotedString(String & s, ReadBuffer & buf)
{ {
s = ""; s = "";
if (buf.eof() || *buf.position() != '\'') if (buf.eof() || *buf.position() != quote)
throw Exception("Cannot parse quoted string: expected opening single quote", throw Exception("Cannot parse quoted string: expected opening quote",
ErrorCodes::CANNOT_PARSE_QUOTED_STRING); ErrorCodes::CANNOT_PARSE_QUOTED_STRING);
++buf.position(); ++buf.position();
@ -71,13 +73,13 @@ void readQuotedString(String & s, ReadBuffer & buf)
{ {
size_t bytes = 0; size_t bytes = 0;
for (; buf.position() + bytes != buf.buffer().end(); ++bytes) for (; buf.position() + bytes != buf.buffer().end(); ++bytes)
if (buf.position()[bytes] == '\\' || buf.position()[bytes] == '\'') if (buf.position()[bytes] == '\\' || buf.position()[bytes] == quote)
break; break;
s.append(buf.position(), bytes); s.append(buf.position(), bytes);
buf.position() += bytes; buf.position() += bytes;
if (*buf.position() == '\'') if (*buf.position() == quote)
{ {
++buf.position(); ++buf.position();
return; return;
@ -93,8 +95,19 @@ void readQuotedString(String & s, ReadBuffer & buf)
} }
} }
throw Exception("Cannot parse quoted string: expected closing single quote", throw Exception("Cannot parse quoted string: expected closing quote",
ErrorCodes::CANNOT_PARSE_QUOTED_STRING); ErrorCodes::CANNOT_PARSE_QUOTED_STRING);
} }
void readQuotedString(String & s, ReadBuffer & buf)
{
readAnyQuotedString<'\''>(s, buf);
}
void readDoubleQuotedString(String & s, ReadBuffer & buf)
{
readAnyQuotedString<'"'>(s, buf);
}
} }

View File

@ -37,6 +37,48 @@ void writeEscapedString(const String & s, WriteBuffer & buf)
writeChar('\\', buf); writeChar('\\', buf);
writeChar('\'', buf); writeChar('\'', buf);
break; break;
case '\\':
writeChar('\\', buf);
writeChar('\\', buf);
break;
default:
writeChar(*it, buf);
}
}
}
void writeDoubleQuotedString(const String & s, WriteBuffer & buf)
{
writeChar('"', buf);
for (String::const_iterator it = s.begin(); it != s.end(); ++it)
{
switch (*it)
{
case '\b':
writeChar('\\', buf);
writeChar('b', buf);
break;
case '\f':
writeChar('\\', buf);
writeChar('f', buf);
break;
case '\n':
writeChar('\\', buf);
writeChar('n', buf);
break;
case '\r':
writeChar('\\', buf);
writeChar('r', buf);
break;
case '\t':
writeChar('\\', buf);
writeChar('t', buf);
break;
case '\0':
writeChar('\\', buf);
writeChar('0', buf);
break;
case '"': case '"':
writeChar('\\', buf); writeChar('\\', buf);
writeChar('"', buf); writeChar('"', buf);
@ -49,6 +91,7 @@ void writeEscapedString(const String & s, WriteBuffer & buf)
writeChar(*it, buf); writeChar(*it, buf);
} }
} }
writeChar('"', buf);
} }
} }