MCPcopy Create free account
hub / github.com/OSGeo/gdal / GetLayerNameForSQL

Function GetLayerNameForSQL

apps/test_ogrsf.cpp:1540–1591  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1538/************************************************************************/
1539
1540static const char *GetLayerNameForSQL(GDALDataset *poDS,
1541 const char *pszLayerName)
1542{
1543 /* Only quote if needed. Quoting conventions depend on the driver... */
1544 if (!EQUAL(pszLayerName, "SELECT") && !EQUAL(pszLayerName, "AS") &&
1545 !EQUAL(pszLayerName, "CAST") && !EQUAL(pszLayerName, "FROM") &&
1546 !EQUAL(pszLayerName, "JOIN") && !EQUAL(pszLayerName, "WHERE") &&
1547 !EQUAL(pszLayerName, "ON") && !EQUAL(pszLayerName, "USING") &&
1548 !EQUAL(pszLayerName, "ORDER") && !EQUAL(pszLayerName, "BY") &&
1549 !EQUAL(pszLayerName, "ASC") && !EQUAL(pszLayerName, "DESC") &&
1550 !EQUAL(pszLayerName, "GROUP") && !EQUAL(pszLayerName, "LIMIT") &&
1551 !EQUAL(pszLayerName, "OFFSET"))
1552 {
1553 char ch;
1554 for (int i = 0; (ch = pszLayerName[i]) != 0; i++)
1555 {
1556 if (ch >= '0' && ch <= '9')
1557 {
1558 if (i == 0)
1559 break;
1560 }
1561 else if (!((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')))
1562 break;
1563 }
1564 if (ch == 0)
1565 return pszLayerName;
1566 }
1567
1568 if (EQUAL(poDS->GetDriverName(), "MYSQL"))
1569 return CPLSPrintf("`%s`", pszLayerName);
1570
1571 if (EQUAL(poDS->GetDriverName(), "PostgreSQL") && strchr(pszLayerName, '.'))
1572 {
1573 const char *pszRet = nullptr;
1574 char **papszTokens = CSLTokenizeStringComplex(pszLayerName, ".", 0, 0);
1575 if (CSLCount(papszTokens) == 2)
1576 pszRet =
1577 CPLSPrintf("\"%s\".\"%s\"", papszTokens[0], papszTokens[1]);
1578 else
1579 pszRet = CPLSPrintf("\"%s\"", pszLayerName);
1580 CSLDestroy(papszTokens);
1581 return pszRet;
1582 }
1583
1584 if (EQUAL(poDS->GetDriverName(), "SQLAnywhere"))
1585 return pszLayerName;
1586
1587 if (EQUAL(poDS->GetDriverName(), "DB2ODBC"))
1588 return pszLayerName;
1589
1590 return CPLSPrintf("\"%s\"", pszLayerName);
1591}
1592
1593/************************************************************************/
1594/* TestOGRLayerFeatureCount() */

Callers 2

TestOGRLayerFeatureCountFunction · 0.85
TestLayerSQLFunction · 0.85

Calls 5

CPLSPrintfFunction · 0.85
CSLTokenizeStringComplexFunction · 0.85
CSLCountFunction · 0.85
GetDriverNameMethod · 0.80
EQUALFunction · 0.50

Tested by

no test coverage detected