Skip to content

Commit ec62a24

Browse files
authored
Merge pull request #266 from FirebirdSQL/251-non-numeric-column-default-values-are-not-quoted
Issue#251: fix & refactoring to preserve quotes in default value column
2 parents 0417922 + c5baff9 commit ec62a24

File tree

2 files changed

+42
-40
lines changed

2 files changed

+42
-40
lines changed

IscDbc/IscColumnsResultSet.cpp

Lines changed: 1 addition & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -266,43 +266,6 @@ bool IscColumnsResultSet::nextFetch()
266266
return true;
267267
}
268268

269-
bool IscColumnsResultSet::getDefSource (int indexIn, int indexTarget)
270-
{
271-
if ( sqlda->isNull (indexIn) )
272-
{
273-
sqlda->updateVarying (indexTarget, "NULL");
274-
return false;
275-
}
276-
277-
//XSQLVAR *var = sqlda->Var(indexIn);
278-
auto * var = sqlda->Var( indexIn );
279-
char buffer[1024];
280-
char * beg = buffer + 7; // sizeof("default")
281-
char * end;
282-
int lenRead;
283-
284-
blob.directOpenBlob ((char*)var->sqldata);
285-
blob.directFetchBlob (buffer, 1024, lenRead);
286-
blob.directCloseBlob();
287-
288-
end = buffer + lenRead;
289-
290-
while ( *++beg == ' ' );
291-
while ( *end == ' ' ) end--;
292-
293-
if ( *beg == '\'' && *(beg + 1) != '\'' )
294-
{
295-
++beg;
296-
--end;
297-
}
298-
299-
*end = '\0';
300-
301-
sqlda->updateVarying (indexTarget, beg);
302-
303-
return true;
304-
}
305-
306269
void IscColumnsResultSet::setCharLen (int charLenInd,
307270
int fldLenInd,
308271
IscSqlType &sqlType)
@@ -415,8 +378,7 @@ void IscColumnsResultSet::adjustResults (IscSqlType &sqlType)
415378
sqlda->updateShort (11, nullable);
416379

417380
// default source
418-
if (!getDefSource (26, 13))
419-
getDefSource (20, 13);
381+
setFieldDefault();
420382

421383
switch (sqlType.type)
422384
{

IscDbc/IscColumnsResultSet.h

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,54 @@ class IscColumnsResultSet : public IscMetaDataResultSet
4141
IscColumnsResultSet(IscDatabaseMetaData *metaData);
4242
void initResultSet(IscStatement *stmt);
4343
private:
44-
virtual bool getDefSource (int indexIn, int indexTarget);
4544
virtual void setCharLen (int charLenInd, int fldLenInd, IscSqlType &sqlType);
4645
virtual void checkQuotes (IscSqlType &sqlType, JString stringVal);
4746
virtual void adjustResults (IscSqlType &sqlType);
4847

4948
IscBlob blob;
5049
CAttrArray arrAttr;
5150
IscSqlType sqlType;
51+
52+
static constexpr int COLUMN_DEFAULT_TARGET = 13;
53+
static constexpr int COLUMN_DEFAULT_SRC[] = { 26, 20 };
54+
static constexpr char DEFAULT_SIGNATURE[] = "DEFAULT";
55+
56+
inline void setFieldDefault(bool removeQuotes = false)
57+
{
58+
sqlda->updateVarying(COLUMN_DEFAULT_TARGET, "NULL");
59+
60+
for (auto src_fld : COLUMN_DEFAULT_SRC)
61+
{
62+
if (!sqlda->isNull(src_fld))
63+
{
64+
auto* var = sqlda->Var(src_fld);
65+
char buffer[1024];
66+
int lenRead;
67+
68+
blob.directOpenBlob((char*)var->sqldata);
69+
blob.directFetchBlob(buffer, sizeof(buffer) - 1, lenRead);
70+
blob.directCloseBlob();
71+
72+
const char* first = buffer + (strncasecmp(buffer, DEFAULT_SIGNATURE, lenRead) ? 0 : sizeof(DEFAULT_SIGNATURE) - 1);
73+
char* last = buffer + lenRead - 1;
74+
75+
while (*first == ' ') ++first;
76+
while (last > first && *last == ' ') --last;
77+
78+
if (removeQuotes && *first == '\'' && last > first)
79+
{
80+
++first;
81+
if (*last == '\'')
82+
--last;
83+
}
84+
85+
*(last + 1) = '\0';
86+
87+
sqlda->updateVarying(COLUMN_DEFAULT_TARGET, first);
88+
break;
89+
}
90+
}
91+
}
5292
};
5393

5494
}; // end namespace IscDbcLibrary

0 commit comments

Comments
 (0)