2007-08-10 16:19:57 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
2009-04-13 16:29:41 +00:00
|
|
|
* vim: sw=2 ts=2 et lcs=trail\:.,tab\:>~ :
|
2012-05-21 11:12:37 +00:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2007-08-10 16:19:57 +00:00
|
|
|
|
2013-12-09 02:52:54 +00:00
|
|
|
#include "mozilla/ArrayUtils.h"
|
2011-10-11 05:50:08 +00:00
|
|
|
|
2009-04-20 14:59:52 +00:00
|
|
|
#include "mozStorageSQLFunctions.h"
|
2007-08-10 16:19:57 +00:00
|
|
|
#include "nsUnicharUtils.h"
|
2013-01-15 12:22:03 +00:00
|
|
|
#include <algorithm>
|
2007-08-10 16:19:57 +00:00
|
|
|
|
2009-04-20 14:59:52 +00:00
|
|
|
namespace mozilla {
|
|
|
|
namespace storage {
|
2007-08-10 16:19:57 +00:00
|
|
|
|
2009-04-20 14:59:52 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
//// Local Helper Functions
|
2007-08-10 16:19:57 +00:00
|
|
|
|
2009-04-20 14:59:52 +00:00
|
|
|
namespace {
|
2007-08-10 16:19:57 +00:00
|
|
|
|
2009-04-20 14:59:52 +00:00
|
|
|
/**
|
|
|
|
* Performs the LIKE comparison of a string against a pattern. For more detail
|
|
|
|
* see http://www.sqlite.org/lang_expr.html#like.
|
|
|
|
*
|
|
|
|
* @param aPatternItr
|
|
|
|
* An iterator at the start of the pattern to check for.
|
|
|
|
* @param aPatternEnd
|
|
|
|
* An iterator at the end of the pattern to check for.
|
|
|
|
* @param aStringItr
|
|
|
|
* An iterator at the start of the string to check for the pattern.
|
|
|
|
* @param aStringEnd
|
|
|
|
* An iterator at the end of the string to check for the pattern.
|
|
|
|
* @param aEscapeChar
|
|
|
|
* The character to use for escaping symbols in the pattern.
|
2009-07-24 15:39:34 +00:00
|
|
|
* @return 1 if the pattern is found, 0 otherwise.
|
2009-04-20 14:59:52 +00:00
|
|
|
*/
|
|
|
|
int
|
2007-08-10 16:19:57 +00:00
|
|
|
likeCompare(nsAString::const_iterator aPatternItr,
|
|
|
|
nsAString::const_iterator aPatternEnd,
|
|
|
|
nsAString::const_iterator aStringItr,
|
|
|
|
nsAString::const_iterator aStringEnd,
|
2014-01-04 15:02:17 +00:00
|
|
|
char16_t aEscapeChar)
|
2007-08-10 16:19:57 +00:00
|
|
|
{
|
2014-01-04 15:02:17 +00:00
|
|
|
const char16_t MATCH_ALL('%');
|
|
|
|
const char16_t MATCH_ONE('_');
|
2007-08-10 16:19:57 +00:00
|
|
|
|
2011-09-29 06:19:26 +00:00
|
|
|
bool lastWasEscape = false;
|
2007-08-10 16:19:57 +00:00
|
|
|
while (aPatternItr != aPatternEnd) {
|
|
|
|
/**
|
|
|
|
* What we do in here is take a look at each character from the input
|
|
|
|
* pattern, and do something with it. There are 4 possibilities:
|
|
|
|
* 1) character is an un-escaped match-all character
|
|
|
|
* 2) character is an un-escaped match-one character
|
|
|
|
* 3) character is an un-escaped escape character
|
|
|
|
* 4) character is not any of the above
|
|
|
|
*/
|
|
|
|
if (!lastWasEscape && *aPatternItr == MATCH_ALL) {
|
|
|
|
// CASE 1
|
|
|
|
/**
|
|
|
|
* Now we need to skip any MATCH_ALL or MATCH_ONE characters that follow a
|
|
|
|
* MATCH_ALL character. For each MATCH_ONE character, skip one character
|
|
|
|
* in the pattern string.
|
|
|
|
*/
|
|
|
|
while (*aPatternItr == MATCH_ALL || *aPatternItr == MATCH_ONE) {
|
|
|
|
if (*aPatternItr == MATCH_ONE) {
|
|
|
|
// If we've hit the end of the string we are testing, no match
|
|
|
|
if (aStringItr == aStringEnd)
|
|
|
|
return 0;
|
|
|
|
aStringItr++;
|
|
|
|
}
|
|
|
|
aPatternItr++;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we've hit the end of the pattern string, match
|
|
|
|
if (aPatternItr == aPatternEnd)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
while (aStringItr != aStringEnd) {
|
2009-04-20 14:59:52 +00:00
|
|
|
if (likeCompare(aPatternItr, aPatternEnd, aStringItr, aStringEnd,
|
|
|
|
aEscapeChar)) {
|
2007-08-10 16:19:57 +00:00
|
|
|
// we've hit a match, so indicate this
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
aStringItr++;
|
|
|
|
}
|
|
|
|
|
|
|
|
// No match
|
|
|
|
return 0;
|
2009-04-20 14:59:52 +00:00
|
|
|
}
|
|
|
|
else if (!lastWasEscape && *aPatternItr == MATCH_ONE) {
|
2007-08-10 16:19:57 +00:00
|
|
|
// CASE 2
|
|
|
|
if (aStringItr == aStringEnd) {
|
|
|
|
// If we've hit the end of the string we are testing, no match
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
aStringItr++;
|
2011-10-17 14:59:28 +00:00
|
|
|
lastWasEscape = false;
|
2009-04-20 14:59:52 +00:00
|
|
|
}
|
|
|
|
else if (!lastWasEscape && *aPatternItr == aEscapeChar) {
|
2007-08-10 16:19:57 +00:00
|
|
|
// CASE 3
|
2011-10-17 14:59:28 +00:00
|
|
|
lastWasEscape = true;
|
2009-04-20 14:59:52 +00:00
|
|
|
}
|
|
|
|
else {
|
2007-08-10 16:19:57 +00:00
|
|
|
// CASE 4
|
2009-04-20 14:59:52 +00:00
|
|
|
if (::ToUpperCase(*aStringItr) != ::ToUpperCase(*aPatternItr)) {
|
2007-08-10 16:19:57 +00:00
|
|
|
// If we've hit a point where the strings don't match, there is no match
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
aStringItr++;
|
2011-10-17 14:59:28 +00:00
|
|
|
lastWasEscape = false;
|
2007-08-10 16:19:57 +00:00
|
|
|
}
|
2009-04-20 14:59:52 +00:00
|
|
|
|
2007-08-10 16:19:57 +00:00
|
|
|
aPatternItr++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return aStringItr == aStringEnd;
|
|
|
|
}
|
|
|
|
|
2009-07-24 15:39:34 +00:00
|
|
|
/**
|
|
|
|
* Compute the Levenshtein Edit Distance between two strings.
|
|
|
|
*
|
|
|
|
* @param aStringS
|
|
|
|
* a string
|
|
|
|
* @param aStringT
|
|
|
|
* another string
|
|
|
|
* @param _result
|
|
|
|
* an outparam that will receive the edit distance between the arguments
|
|
|
|
* @return a Sqlite result code, e.g. SQLITE_OK, SQLITE_NOMEM, etc.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
levenshteinDistance(const nsAString &aStringS,
|
|
|
|
const nsAString &aStringT,
|
|
|
|
int *_result)
|
|
|
|
{
|
|
|
|
// Set the result to a non-sensical value in case we encounter an error.
|
|
|
|
*_result = -1;
|
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
const uint32_t sLen = aStringS.Length();
|
|
|
|
const uint32_t tLen = aStringT.Length();
|
2009-07-24 15:39:34 +00:00
|
|
|
|
|
|
|
if (sLen == 0) {
|
|
|
|
*_result = tLen;
|
|
|
|
return SQLITE_OK;
|
|
|
|
}
|
|
|
|
if (tLen == 0) {
|
|
|
|
*_result = sLen;
|
|
|
|
return SQLITE_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Notionally, Levenshtein Distance is computed in a matrix. If we
|
|
|
|
// assume s = "span" and t = "spam", the matrix would look like this:
|
|
|
|
// s -->
|
|
|
|
// t s p a n
|
|
|
|
// | 0 1 2 3 4
|
|
|
|
// V s 1 * * * *
|
|
|
|
// p 2 * * * *
|
|
|
|
// a 3 * * * *
|
|
|
|
// m 4 * * * *
|
|
|
|
//
|
|
|
|
// Note that the row width is sLen + 1 and the column height is tLen + 1,
|
|
|
|
// where sLen is the length of the string "s" and tLen is the length of "t".
|
|
|
|
// The first row and the first column are initialized as shown, and
|
|
|
|
// the algorithm computes the remaining cells row-by-row, and
|
|
|
|
// left-to-right within each row. The computation only requires that
|
|
|
|
// we be able to see the current row and the previous one.
|
|
|
|
|
2015-10-28 01:22:52 +00:00
|
|
|
// Allocate memory for two rows.
|
2016-02-02 15:36:30 +00:00
|
|
|
AutoTArray<int, nsAutoString::kDefaultStorageSize> row1;
|
|
|
|
AutoTArray<int, nsAutoString::kDefaultStorageSize> row2;
|
2009-07-24 15:39:34 +00:00
|
|
|
|
|
|
|
// Declare the raw pointers that will actually be used to access the memory.
|
2015-10-28 01:22:52 +00:00
|
|
|
int *prevRow = row1.AppendElements(sLen + 1);
|
|
|
|
int *currRow = row2.AppendElements(sLen + 1);
|
2009-07-24 15:39:34 +00:00
|
|
|
|
|
|
|
// Initialize the first row.
|
2012-08-22 15:56:38 +00:00
|
|
|
for (uint32_t i = 0; i <= sLen; i++)
|
2009-07-24 15:39:34 +00:00
|
|
|
prevRow[i] = i;
|
|
|
|
|
2014-01-04 15:02:17 +00:00
|
|
|
const char16_t *s = aStringS.BeginReading();
|
|
|
|
const char16_t *t = aStringT.BeginReading();
|
2009-07-24 15:39:34 +00:00
|
|
|
|
|
|
|
// Compute the empty cells in the "matrix" row-by-row, starting with
|
|
|
|
// the second row.
|
2012-08-22 15:56:38 +00:00
|
|
|
for (uint32_t ti = 1; ti <= tLen; ti++) {
|
2009-07-24 15:39:34 +00:00
|
|
|
|
|
|
|
// Initialize the first cell in this row.
|
|
|
|
currRow[0] = ti;
|
|
|
|
|
|
|
|
// Get the character from "t" that corresponds to this row.
|
2014-01-04 15:02:17 +00:00
|
|
|
const char16_t tch = t[ti - 1];
|
2009-07-24 15:39:34 +00:00
|
|
|
|
|
|
|
// Compute the remaining cells in this row, left-to-right,
|
|
|
|
// starting at the second column (and first character of "s").
|
2012-08-22 15:56:38 +00:00
|
|
|
for (uint32_t si = 1; si <= sLen; si++) {
|
2009-07-24 15:39:34 +00:00
|
|
|
|
|
|
|
// Get the character from "s" that corresponds to this column,
|
|
|
|
// compare it to the t-character, and compute the "cost".
|
2014-01-04 15:02:17 +00:00
|
|
|
const char16_t sch = s[si - 1];
|
2009-07-24 15:39:34 +00:00
|
|
|
int cost = (sch == tch) ? 0 : 1;
|
|
|
|
|
|
|
|
// ............ We want to calculate the value of cell "d" from
|
|
|
|
// ...ab....... the previously calculated (or initialized) cells
|
|
|
|
// ...cd....... "a", "b", and "c", where d = min(a', b', c').
|
|
|
|
// ............
|
|
|
|
int aPrime = prevRow[si - 1] + cost;
|
|
|
|
int bPrime = prevRow[si] + 1;
|
|
|
|
int cPrime = currRow[si - 1] + 1;
|
2013-01-15 12:22:03 +00:00
|
|
|
currRow[si] = std::min(aPrime, std::min(bPrime, cPrime));
|
2009-07-24 15:39:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Advance to the next row. The current row becomes the previous
|
|
|
|
// row and we recycle the old previous row as the new current row.
|
|
|
|
// We don't need to re-initialize the new current row since we will
|
|
|
|
// rewrite all of its cells anyway.
|
|
|
|
int *oldPrevRow = prevRow;
|
|
|
|
prevRow = currRow;
|
|
|
|
currRow = oldPrevRow;
|
|
|
|
}
|
|
|
|
|
|
|
|
// The final result is the value of the last cell in the last row.
|
|
|
|
// Note that that's now in the "previous" row, since we just swapped them.
|
|
|
|
*_result = prevRow[sLen];
|
|
|
|
return SQLITE_OK;
|
|
|
|
}
|
|
|
|
|
2011-10-11 05:50:08 +00:00
|
|
|
// This struct is used only by registerFunctions below, but ISO C++98 forbids
|
|
|
|
// instantiating a template dependent on a locally-defined type. Boo-urns!
|
|
|
|
struct Functions {
|
|
|
|
const char *zName;
|
|
|
|
int nArg;
|
|
|
|
int enc;
|
|
|
|
void *pContext;
|
|
|
|
void (*xFunc)(::sqlite3_context*, int, sqlite3_value**);
|
|
|
|
};
|
|
|
|
|
2015-07-13 15:25:42 +00:00
|
|
|
} // namespace
|
2009-04-20 14:59:52 +00:00
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
//// Exposed Functions
|
|
|
|
|
|
|
|
int
|
|
|
|
registerFunctions(sqlite3 *aDB)
|
|
|
|
{
|
2009-07-24 15:39:34 +00:00
|
|
|
Functions functions[] = {
|
|
|
|
{"lower",
|
|
|
|
1,
|
|
|
|
SQLITE_UTF16,
|
|
|
|
0,
|
|
|
|
caseFunction},
|
|
|
|
{"lower",
|
|
|
|
1,
|
|
|
|
SQLITE_UTF8,
|
|
|
|
0,
|
|
|
|
caseFunction},
|
|
|
|
{"upper",
|
|
|
|
1,
|
|
|
|
SQLITE_UTF16,
|
|
|
|
(void*)1,
|
|
|
|
caseFunction},
|
|
|
|
{"upper",
|
|
|
|
1,
|
|
|
|
SQLITE_UTF8,
|
|
|
|
(void*)1,
|
|
|
|
caseFunction},
|
|
|
|
|
|
|
|
{"like",
|
|
|
|
2,
|
|
|
|
SQLITE_UTF16,
|
|
|
|
0,
|
|
|
|
likeFunction},
|
|
|
|
{"like",
|
|
|
|
2,
|
|
|
|
SQLITE_UTF8,
|
|
|
|
0,
|
|
|
|
likeFunction},
|
|
|
|
{"like",
|
|
|
|
3,
|
|
|
|
SQLITE_UTF16,
|
|
|
|
0,
|
|
|
|
likeFunction},
|
|
|
|
{"like",
|
|
|
|
3,
|
|
|
|
SQLITE_UTF8,
|
|
|
|
0,
|
|
|
|
likeFunction},
|
|
|
|
|
|
|
|
{"levenshteinDistance",
|
|
|
|
2,
|
|
|
|
SQLITE_UTF16,
|
|
|
|
0,
|
|
|
|
levenshteinDistanceFunction},
|
|
|
|
{"levenshteinDistance",
|
|
|
|
2,
|
|
|
|
SQLITE_UTF8,
|
|
|
|
0,
|
|
|
|
levenshteinDistanceFunction},
|
2009-04-20 14:59:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
int rv = SQLITE_OK;
|
2011-10-11 05:50:08 +00:00
|
|
|
for (size_t i = 0; SQLITE_OK == rv && i < ArrayLength(functions); ++i) {
|
2009-04-20 14:59:52 +00:00
|
|
|
struct Functions *p = &functions[i];
|
|
|
|
rv = ::sqlite3_create_function(aDB, p->zName, p->nArg, p->enc, p->pContext,
|
2013-07-31 15:44:58 +00:00
|
|
|
p->xFunc, nullptr, nullptr);
|
2009-04-20 14:59:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
//// SQL Functions
|
|
|
|
|
|
|
|
void
|
|
|
|
caseFunction(sqlite3_context *aCtx,
|
|
|
|
int aArgc,
|
|
|
|
sqlite3_value **aArgv)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(1 == aArgc, "Invalid number of arguments!");
|
|
|
|
|
2014-01-04 15:02:17 +00:00
|
|
|
nsAutoString data(static_cast<const char16_t *>(::sqlite3_value_text16(aArgv[0])));
|
2011-09-29 06:19:26 +00:00
|
|
|
bool toUpper = ::sqlite3_user_data(aCtx) ? true : false;
|
2009-04-20 14:59:52 +00:00
|
|
|
|
|
|
|
if (toUpper)
|
|
|
|
::ToUpperCase(data);
|
|
|
|
else
|
|
|
|
::ToLowerCase(data);
|
|
|
|
|
|
|
|
// Set the result.
|
|
|
|
::sqlite3_result_text16(aCtx, data.get(), -1, SQLITE_TRANSIENT);
|
|
|
|
}
|
|
|
|
|
2007-08-10 16:19:57 +00:00
|
|
|
/**
|
|
|
|
* This implements the like() SQL function. This is used by the LIKE operator.
|
|
|
|
* The SQL statement 'A LIKE B' is implemented as 'like(B, A)', and if there is
|
|
|
|
* an escape character, say E, it is implemented as 'like(B, A, E)'.
|
|
|
|
*/
|
|
|
|
void
|
2009-04-20 14:59:52 +00:00
|
|
|
likeFunction(sqlite3_context *aCtx,
|
|
|
|
int aArgc,
|
|
|
|
sqlite3_value **aArgv)
|
2007-08-10 16:19:57 +00:00
|
|
|
{
|
|
|
|
NS_ASSERTION(2 == aArgc || 3 == aArgc, "Invalid number of arguments!");
|
|
|
|
|
2009-04-20 14:59:52 +00:00
|
|
|
if (::sqlite3_value_bytes(aArgv[0]) > SQLITE_MAX_LIKE_PATTERN_LENGTH) {
|
|
|
|
::sqlite3_result_error(aCtx, "LIKE or GLOB pattern too complex",
|
|
|
|
SQLITE_TOOBIG);
|
2007-08-10 16:19:57 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-04-20 14:59:52 +00:00
|
|
|
if (!::sqlite3_value_text16(aArgv[0]) || !::sqlite3_value_text16(aArgv[1]))
|
2007-08-28 04:43:10 +00:00
|
|
|
return;
|
|
|
|
|
2014-01-04 15:02:17 +00:00
|
|
|
nsDependentString A(static_cast<const char16_t *>(::sqlite3_value_text16(aArgv[1])));
|
|
|
|
nsDependentString B(static_cast<const char16_t *>(::sqlite3_value_text16(aArgv[0])));
|
2007-08-10 18:33:14 +00:00
|
|
|
NS_ASSERTION(!B.IsEmpty(), "LIKE string must not be null!");
|
2007-08-10 16:19:57 +00:00
|
|
|
|
2014-01-04 15:02:17 +00:00
|
|
|
char16_t E = 0;
|
2007-08-10 18:33:14 +00:00
|
|
|
if (3 == aArgc)
|
2014-01-04 15:02:17 +00:00
|
|
|
E = static_cast<const char16_t *>(::sqlite3_value_text16(aArgv[2]))[0];
|
2007-08-10 16:19:57 +00:00
|
|
|
|
|
|
|
nsAString::const_iterator itrString, endString;
|
|
|
|
A.BeginReading(itrString);
|
|
|
|
A.EndReading(endString);
|
|
|
|
nsAString::const_iterator itrPattern, endPattern;
|
|
|
|
B.BeginReading(itrPattern);
|
|
|
|
B.EndReading(endPattern);
|
2009-04-20 14:59:52 +00:00
|
|
|
::sqlite3_result_int(aCtx, likeCompare(itrPattern, endPattern, itrString,
|
|
|
|
endString, E));
|
2007-08-10 16:19:57 +00:00
|
|
|
}
|
|
|
|
|
2009-07-24 15:39:34 +00:00
|
|
|
void levenshteinDistanceFunction(sqlite3_context *aCtx,
|
|
|
|
int aArgc,
|
|
|
|
sqlite3_value **aArgv)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(2 == aArgc, "Invalid number of arguments!");
|
|
|
|
|
|
|
|
// If either argument is a SQL NULL, then return SQL NULL.
|
|
|
|
if (::sqlite3_value_type(aArgv[0]) == SQLITE_NULL ||
|
|
|
|
::sqlite3_value_type(aArgv[1]) == SQLITE_NULL) {
|
|
|
|
::sqlite3_result_null(aCtx);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-01-04 15:02:17 +00:00
|
|
|
int aLen = ::sqlite3_value_bytes16(aArgv[0]) / sizeof(char16_t);
|
|
|
|
const char16_t *a = static_cast<const char16_t *>(::sqlite3_value_text16(aArgv[0]));
|
2009-07-24 15:39:34 +00:00
|
|
|
|
2014-01-04 15:02:17 +00:00
|
|
|
int bLen = ::sqlite3_value_bytes16(aArgv[1]) / sizeof(char16_t);
|
|
|
|
const char16_t *b = static_cast<const char16_t *>(::sqlite3_value_text16(aArgv[1]));
|
2009-07-24 15:39:34 +00:00
|
|
|
|
|
|
|
// Compute the Levenshtein Distance, and return the result (or error).
|
|
|
|
int distance = -1;
|
|
|
|
const nsDependentString A(a, aLen);
|
|
|
|
const nsDependentString B(b, bLen);
|
|
|
|
int status = levenshteinDistance(A, B, &distance);
|
|
|
|
if (status == SQLITE_OK) {
|
|
|
|
::sqlite3_result_int(aCtx, distance);
|
|
|
|
}
|
|
|
|
else if (status == SQLITE_NOMEM) {
|
|
|
|
::sqlite3_result_error_nomem(aCtx);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
::sqlite3_result_error(aCtx, "User function returned error code", -1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-04-20 14:59:52 +00:00
|
|
|
} // namespace storage
|
|
|
|
} // namespace mozilla
|