Skip to content

Commit

Permalink
Merge pull request #10262 from rouault/fix_qgis_57823
Browse files Browse the repository at this point in the history
SQLite/GPKG: detect UNIQUE constraints expressed as a ', CONSTRAINT name UNIQUE (column_name)' at the end of CREATE TABLE
  • Loading branch information
rouault authored Jun 22, 2024
2 parents 46988b5 + e67a9db commit 5525a21
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 4 deletions.
10 changes: 9 additions & 1 deletion autotest/ogr/ogr_sqlite.py
Original file line number Diff line number Diff line change
Expand Up @@ -3314,7 +3314,7 @@ def test_ogr_sqlite_unique(tmp_vsimem):
# and indexes
# Note: leave create table in a single line because of regex spaces testing
sql = (
'CREATE TABLE IF NOT EXISTS "test2" ( "fid" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,\n"field_default" TEXT, "field_no_unique" TEXT DEFAULT \'UNIQUE\',"field_unique" TEXT UNIQUE,`field unique2` TEXT UNIQUE,field_unique3 TEXT UNIQUE, FIELD_UNIQUE_INDEX TEXT, `field unique index2`, "field_unique_index3" TEXT, NOT_UNIQUE TEXT);',
'CREATE TABLE IF NOT EXISTS "test2" ( "fid" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,\n"field_default" TEXT, "field_no_unique" TEXT DEFAULT \'UNIQUE\',"field_unique" TEXT UNIQUE,`field unique2` TEXT UNIQUE,field_unique3 TEXT UNIQUE, FIELD_UNIQUE_INDEX TEXT, `field unique index2`, "field_unique_index3" TEXT, NOT_UNIQUE TEXT,field4 TEXT,field5 TEXT,field6 TEXT,CONSTRAINT ignored_constraint CHECK (fid >= 0),CONSTRAINT field5_6_uniq UNIQUE (field5, field6), CONSTRAINT field4_uniq UNIQUE (field4));',
"CREATE UNIQUE INDEX test2_unique_idx ON test2(field_unique_index);", # field_unique_index in lowercase whereas in uppercase in CREATE TABLE statement
"CREATE UNIQUE INDEX test2_unique_idx2 ON test2(`field unique index2`);",
'CREATE UNIQUE INDEX test2_unique_idx3 ON test2("field_unique_index3");',
Expand Down Expand Up @@ -3365,6 +3365,14 @@ def test_ogr_sqlite_unique(tmp_vsimem):
fldDef = layerDefinition.GetFieldDefn(8)
assert not fldDef.IsUnique()

# Constraint given by CONSTRAINT field4_uniq UNIQUE (field4)
fldDef = layerDefinition.GetFieldDefn(layerDefinition.GetFieldIndex("field4"))
assert fldDef.IsUnique()

# Constraint given by CONSTRAINT field5_6_uniq UNIQUE (field5, field6) ==> ignored
fldDef = layerDefinition.GetFieldDefn(layerDefinition.GetFieldIndex("field5"))
assert not fldDef.IsUnique()

ds = None


Expand Down
105 changes: 102 additions & 3 deletions ogr/ogrsf_frmts/sqlite/ogrsqliteutility.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -424,14 +424,14 @@ std::set<std::string> SQLGetUniqueFieldUCConstraints(
}
}
}
else if (osStr[pos] == ',')
else if (osStr[pos] == ',' || osStr[pos] == '(' || osStr[pos] == ')')
{
osToken = ',';
osToken = osStr[pos];
pos++;
}
else
{
size_t pos2 = osStr.find_first_of(" \t\n\r,", pos);
size_t pos2 = osStr.find_first_of(") \t\n\r,", pos);
if (pos2 == std::string::npos)
osToken = osStr.substr(pos);
else
Expand All @@ -453,8 +453,18 @@ std::set<std::string> SQLGetUniqueFieldUCConstraints(
tableDefinition =
tableDefinition.substr(nPosStart + 1, nPosEnd - nPosStart - 1);
size_t pos = 0;
bool bHasConstraint = false;
while (true)
{
size_t posBackup = pos;
if (EQUAL(
GetNextToken(tableDefinition, posBackup, true).c_str(),
"CONSTRAINT"))
{
bHasConstraint = true;
break;
}

const std::string osColName =
GetNextToken(tableDefinition, pos, false);
if (osColName.empty())
Expand All @@ -473,6 +483,95 @@ std::set<std::string> SQLGetUniqueFieldUCConstraints(
}
}
}

// Process https://www.sqlite.org/syntax/table-constraint.html
if (bHasConstraint)
{
while (true)
{
if (!EQUAL(GetNextToken(tableDefinition, pos, true).c_str(),
"CONSTRAINT"))
{
break;
}

const std::string osConstraintName =
GetNextToken(tableDefinition, pos, false);
if (osConstraintName.empty())
{
break;
}

const std::string osConstraintType =
GetNextToken(tableDefinition, pos, true);
if (osConstraintType.empty())
{
break;
}

if (EQUAL(osConstraintType.c_str(), "UNIQUE"))
{
std::string osToken =
GetNextToken(tableDefinition, pos, true);
if (osToken != "(")
break;

const std::string osColName =
GetNextToken(tableDefinition, pos, false);
osToken = GetNextToken(tableDefinition, pos, true);
if (osToken == ")")
{
// Only takes into account single column unique constraint
uniqueFieldsUC.insert(
CPLString(osColName).toupper());
}
else
{
// Skip tokens until ')'
if (!osToken.empty())
{
do
{
osToken = GetNextToken(tableDefinition, pos,
true);
} while (!osToken.empty() && osToken != ")");
}
if (osToken.empty())
break;
}
osToken = GetNextToken(tableDefinition, pos, true);
if (osToken != ",")
break;
}
else
{
// Skip ignored constraint types by looking for the
// next "," token, that is not inside parenthesis.
int nCountParenthesis = 0;
std::string osToken;
while (true)
{
osToken = GetNextToken(tableDefinition, pos, true);
if (osToken.empty())
break;
if (nCountParenthesis == 0 && osToken == ",")
break;
else if (osToken == "(")
nCountParenthesis++;
else if (osToken == ")")
{
nCountParenthesis--;
if (nCountParenthesis < 0)
{
break;
}
}
}
if (!(nCountParenthesis == 0 && osToken == ","))
break;
}
}
}
}
}

Expand Down

0 comments on commit 5525a21

Please sign in to comment.