2020-07-07 18:28:50 +00:00
|
|
|
#include "sql_string_helpers.h"
|
|
|
|
|
|
|
|
#include <base/system.h>
|
2016-05-09 21:35:23 +00:00
|
|
|
#include <cmath>
|
|
|
|
#include <cstring>
|
|
|
|
|
2020-10-27 17:57:14 +00:00
|
|
|
void sqlstr::FuzzyString(char *pString, int Size)
|
2016-05-09 21:35:23 +00:00
|
|
|
{
|
2020-10-27 17:57:14 +00:00
|
|
|
char *pNewString = new char[Size * 4 - 1];
|
|
|
|
int OutPos = 0;
|
2016-05-09 21:35:23 +00:00
|
|
|
|
2020-10-27 17:57:14 +00:00
|
|
|
for(int i = 0; i < Size; i++)
|
2016-05-09 21:35:23 +00:00
|
|
|
{
|
|
|
|
if(!pString[i])
|
|
|
|
break;
|
|
|
|
|
2020-10-27 17:57:14 +00:00
|
|
|
pNewString[OutPos++] = pString[i];
|
2020-09-26 19:41:58 +00:00
|
|
|
if(pString[i] != '\\' && str_utf8_isstart(pString[i + 1]))
|
2020-10-27 17:57:14 +00:00
|
|
|
pNewString[OutPos++] = '%';
|
2016-05-09 21:35:23 +00:00
|
|
|
}
|
|
|
|
|
2020-10-27 17:57:14 +00:00
|
|
|
pNewString[OutPos] = '\0';
|
|
|
|
str_copy(pString, pNewString, Size);
|
|
|
|
delete[] pNewString;
|
2016-05-09 21:35:23 +00:00
|
|
|
}
|
|
|
|
|
2020-07-04 17:53:27 +00:00
|
|
|
int sqlstr::EscapeLike(char *pDst, const char *pSrc, int DstSize)
|
|
|
|
{
|
|
|
|
int Pos = 0;
|
|
|
|
int DstPos = 0;
|
|
|
|
while(DstPos + 2 < DstSize)
|
|
|
|
{
|
|
|
|
if(pSrc[Pos] == '\0')
|
|
|
|
break;
|
|
|
|
if(pSrc[Pos] == '\\' || pSrc[Pos] == '%' || pSrc[Pos] == '_' || pSrc[Pos] == '[')
|
|
|
|
pDst[DstPos++] = '\\';
|
|
|
|
pDst[DstPos++] = pSrc[Pos++];
|
|
|
|
}
|
|
|
|
pDst[DstPos++] = '\0';
|
|
|
|
return DstPos;
|
|
|
|
}
|
|
|
|
|
2020-10-12 16:31:55 +00:00
|
|
|
void sqlstr::AgoTimeToString(int AgoTime, char *pAgoString, int Size)
|
2016-05-09 21:35:23 +00:00
|
|
|
{
|
2021-12-25 00:32:58 +00:00
|
|
|
if(AgoTime <= 0)
|
|
|
|
{
|
|
|
|
str_copy(pAgoString, "moments", Size);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-05-09 21:35:23 +00:00
|
|
|
char aBuf[20];
|
2017-03-21 10:24:44 +00:00
|
|
|
int aTimes[7] =
|
2020-09-26 19:41:58 +00:00
|
|
|
{
|
|
|
|
60 * 60 * 24 * 365,
|
|
|
|
60 * 60 * 24 * 30,
|
2016-05-09 21:35:23 +00:00
|
|
|
60 * 60 * 24 * 7,
|
2020-09-26 19:41:58 +00:00
|
|
|
60 * 60 * 24,
|
|
|
|
60 * 60,
|
|
|
|
60,
|
|
|
|
1};
|
2017-03-21 10:24:44 +00:00
|
|
|
char aaNames[7][6] =
|
2020-09-26 19:41:58 +00:00
|
|
|
{
|
2016-05-09 21:35:23 +00:00
|
|
|
"year",
|
|
|
|
"month",
|
|
|
|
"week",
|
|
|
|
"day",
|
|
|
|
"hour",
|
|
|
|
"min",
|
2020-09-26 19:41:58 +00:00
|
|
|
"sec"};
|
2016-05-09 21:35:23 +00:00
|
|
|
|
2017-03-21 10:24:44 +00:00
|
|
|
int Seconds = 0;
|
|
|
|
char aName[6];
|
|
|
|
int Count = 0;
|
2016-05-09 21:35:23 +00:00
|
|
|
int i = 0;
|
|
|
|
|
|
|
|
// finding biggest match
|
2017-03-21 10:24:44 +00:00
|
|
|
for(i = 0; i < 7; i++)
|
2016-05-09 21:35:23 +00:00
|
|
|
{
|
2017-03-21 10:24:44 +00:00
|
|
|
Seconds = aTimes[i];
|
2020-10-12 16:31:55 +00:00
|
|
|
str_copy(aName, aaNames[i], sizeof(aName));
|
2016-05-09 21:35:23 +00:00
|
|
|
|
2020-11-05 10:34:20 +00:00
|
|
|
Count = std::floor((float)AgoTime / (float)Seconds);
|
2017-03-21 10:24:44 +00:00
|
|
|
if(Count != 0)
|
2016-05-09 21:35:23 +00:00
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-21 10:24:44 +00:00
|
|
|
if(Count == 1)
|
2016-05-09 21:35:23 +00:00
|
|
|
{
|
2020-09-26 19:41:58 +00:00
|
|
|
str_format(aBuf, sizeof(aBuf), "%d %s", 1, aName);
|
2016-05-09 21:35:23 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-09-26 19:41:58 +00:00
|
|
|
str_format(aBuf, sizeof(aBuf), "%d %ss", Count, aName);
|
2016-05-09 21:35:23 +00:00
|
|
|
}
|
2020-10-12 16:31:55 +00:00
|
|
|
str_append(pAgoString, aBuf, Size);
|
2016-05-09 21:35:23 +00:00
|
|
|
|
2020-09-26 19:41:58 +00:00
|
|
|
if(i + 1 < 7)
|
2016-05-09 21:35:23 +00:00
|
|
|
{
|
|
|
|
// getting second piece now
|
2020-09-26 19:41:58 +00:00
|
|
|
int Seconds2 = aTimes[i + 1];
|
2017-03-21 10:24:44 +00:00
|
|
|
char aName2[6];
|
2020-10-12 16:31:55 +00:00
|
|
|
str_copy(aName2, aaNames[i + 1], sizeof(aName2));
|
2016-05-09 21:35:23 +00:00
|
|
|
|
|
|
|
// add second piece if it's greater than 0
|
2020-11-05 10:34:20 +00:00
|
|
|
int Count2 = std::floor((float)(AgoTime - (Seconds * Count)) / (float)Seconds2);
|
2016-05-09 21:35:23 +00:00
|
|
|
|
2020-09-26 19:41:58 +00:00
|
|
|
if(Count2 != 0)
|
2016-05-09 21:35:23 +00:00
|
|
|
{
|
2017-03-21 10:24:44 +00:00
|
|
|
if(Count2 == 1)
|
2016-05-09 21:35:23 +00:00
|
|
|
{
|
2020-09-26 19:41:58 +00:00
|
|
|
str_format(aBuf, sizeof(aBuf), " and %d %s", 1, aName2);
|
2016-05-09 21:35:23 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-09-26 19:41:58 +00:00
|
|
|
str_format(aBuf, sizeof(aBuf), " and %d %ss", Count2, aName2);
|
2016-05-09 21:35:23 +00:00
|
|
|
}
|
2020-10-12 16:31:55 +00:00
|
|
|
str_append(pAgoString, aBuf, Size);
|
2016-05-09 21:35:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|