diff options
Diffstat (limited to 'dbaccess/source/filter/hsqldb/createparser.cxx')
-rw-r--r-- | dbaccess/source/filter/hsqldb/createparser.cxx | 96 |
1 files changed, 55 insertions, 41 deletions
diff --git a/dbaccess/source/filter/hsqldb/createparser.cxx b/dbaccess/source/filter/hsqldb/createparser.cxx index 9aad116535fb..360741ce0b28 100644 --- a/dbaccess/source/filter/hsqldb/createparser.cxx +++ b/dbaccess/source/filter/hsqldb/createparser.cxx @@ -19,6 +19,7 @@ #include <comphelper/string.hxx> #include <sal/log.hxx> +#include <o3tl/string_view.hxx> #include "createparser.hxx" #include "utils.hxx" #include <com/sun/star/sdbc/DataType.hpp> @@ -30,16 +31,17 @@ namespace { /// Returns substring of sSql from the first occurrence of '(' until the /// last occurrence of ')' (excluding the parenthesis) -OUString lcl_getColumnPart(const OUString& sSql) +std::u16string_view lcl_getColumnPart(std::u16string_view sSql) { - sal_Int32 nBeginIndex = sSql.indexOf("(") + 1; - if (nBeginIndex < 0) + size_t nBeginIndex = sSql.find('('); + if (nBeginIndex == std::u16string_view::npos) { SAL_WARN("dbaccess", "No column definitions found"); - return OUString(); + return std::u16string_view(); } - sal_Int32 nCount = sSql.lastIndexOf(")") - nBeginIndex; - return sSql.copy(nBeginIndex, nCount); + sal_Int32 nCount = sSql.rfind(')') - nBeginIndex - 1; + auto sPart = sSql.substr(nBeginIndex + 1, nCount); + return sPart; } /// Constructs a vector of strings that represents the definitions of each @@ -47,7 +49,7 @@ OUString lcl_getColumnPart(const OUString& sSql) /// /// @param sColumnPart part of the create statement inside the parenthesis /// containing the column definitions -std::vector<OUString> lcl_splitColumnPart(const OUString& sColumnPart) +std::vector<OUString> lcl_splitColumnPart(std::u16string_view sColumnPart) { std::vector<OUString> sParts = string::split(sColumnPart, sal_Unicode(u',')); std::vector<OUString> sReturn; @@ -67,10 +69,11 @@ std::vector<OUString> lcl_splitColumnPart(const OUString& sColumnPart) return sReturn; } -sal_Int32 lcl_getAutoIncrementDefault(const OUString& sColumnDef) +sal_Int32 lcl_getAutoIncrementDefault(std::u16string_view sColumnDef) { // TODO what if there are more spaces? - if (sColumnDef.indexOf("GENERATED BY DEFAULT AS IDENTITY") > 0) + size_t nPos = sColumnDef.find(u"GENERATED BY DEFAULT AS IDENTITY"); + if (nPos != std::u16string_view::npos && nPos > 0) { // TODO parse starting sequence stated by "START WITH" return 0; @@ -78,24 +81,34 @@ sal_Int32 lcl_getAutoIncrementDefault(const OUString& sColumnDef) return -1; } -OUString lcl_getDefaultValue(const OUString& sColumnDef) +std::u16string_view lcl_getDefaultValue(std::u16string_view sColumnDef) { - constexpr char DEFAULT_KW[] = "DEFAULT"; - auto nDefPos = sColumnDef.indexOf(DEFAULT_KW); - if (nDefPos > 0 && lcl_getAutoIncrementDefault(sColumnDef) < 0) + constexpr std::u16string_view DEFAULT_KW = u"DEFAULT"; + size_t nDefPos = sColumnDef.find(DEFAULT_KW); + if (nDefPos > 0 && nDefPos != std::u16string_view::npos + && lcl_getAutoIncrementDefault(sColumnDef) < 0) { - const OUString& fromDefault = sColumnDef.copy(nDefPos + sizeof(DEFAULT_KW)).trim(); + std::u16string_view fromDefault + = o3tl::trim(sColumnDef.substr(nDefPos + DEFAULT_KW.size())); // next word is the value - auto nNextSpace = fromDefault.indexOf(" "); - return nNextSpace > 0 ? fromDefault.copy(0, fromDefault.indexOf(" ")) : fromDefault; + size_t nNextSpace = fromDefault.find(' '); + return (nNextSpace > 0 && nNextSpace != std::u16string_view::npos) + ? fromDefault.substr(0, nNextSpace) + : fromDefault; } - return OUString{}; + return std::u16string_view(); } -bool lcl_isNullable(const OUString& sColumnDef) { return sColumnDef.indexOf("NOT NULL") < 0; } +bool lcl_isNullable(std::u16string_view sColumnDef) +{ + return sColumnDef.find(u"NOT NULL") == std::u16string_view::npos; +} -bool lcl_isPrimaryKey(const OUString& sColumnDef) { return sColumnDef.indexOf("PRIMARY KEY") >= 0; } +bool lcl_isPrimaryKey(std::u16string_view sColumnDef) +{ + return sColumnDef.find(u"PRIMARY KEY") != std::u16string_view::npos; +} sal_Int32 lcl_getDataTypeFromHsql(std::u16string_view sTypeName) { @@ -163,15 +176,15 @@ struct ColumnTypeParts * Separates full type descriptions (e.g. NUMERIC(5,4)) to type name (NUMERIC) and * parameters (5,4) */ -ColumnTypeParts lcl_getColumnTypeParts(const OUString& sFullTypeName) +ColumnTypeParts lcl_getColumnTypeParts(std::u16string_view sFullTypeName) { ColumnTypeParts parts; - auto nParenPos = sFullTypeName.indexOf("("); - if (nParenPos > 0) + auto nParenPos = sFullTypeName.find('('); + if (nParenPos > 0 && nParenPos != std::u16string_view::npos) { - parts.typeName = sFullTypeName.copy(0, nParenPos).trim(); - OUString sParamStr - = sFullTypeName.copy(nParenPos + 1, sFullTypeName.indexOf(")") - nParenPos - 1); + parts.typeName = o3tl::trim(sFullTypeName.substr(0, nParenPos)); + std::u16string_view sParamStr + = sFullTypeName.substr(nParenPos + 1, sFullTypeName.find(')') - nParenPos - 1); auto sParams = string::split(sParamStr, sal_Unicode(u',')); for (const auto& sParam : sParams) { @@ -180,7 +193,7 @@ ColumnTypeParts lcl_getColumnTypeParts(const OUString& sFullTypeName) } else { - parts.typeName = sFullTypeName.trim(); + parts.typeName = o3tl::trim(sFullTypeName); lcl_addDefaultParameters(parts.params, lcl_getDataTypeFromHsql(parts.typeName)); } return parts; @@ -192,13 +205,13 @@ namespace dbahsql { CreateStmtParser::CreateStmtParser() {} -void CreateStmtParser::parsePrimaryKeys(const OUString& sPrimaryPart) +void CreateStmtParser::parsePrimaryKeys(std::u16string_view sPrimaryPart) { - sal_Int32 nParenPos = sPrimaryPart.indexOf("("); - if (nParenPos > 0) + size_t nParenPos = sPrimaryPart.find('('); + if (nParenPos > 0 && nParenPos != std::u16string_view::npos) { - OUString sParamStr - = sPrimaryPart.copy(nParenPos + 1, sPrimaryPart.lastIndexOf(")") - nParenPos - 1); + std::u16string_view sParamStr + = sPrimaryPart.substr(nParenPos + 1, sPrimaryPart.rfind(')') - nParenPos - 1); auto sParams = string::split(sParamStr, sal_Unicode(u',')); for (const auto& sParam : sParams) { @@ -207,7 +220,7 @@ void CreateStmtParser::parsePrimaryKeys(const OUString& sPrimaryPart) } } -void CreateStmtParser::parseColumnPart(const OUString& sColumnPart) +void CreateStmtParser::parseColumnPart(std::u16string_view sColumnPart) { auto sColumns = lcl_splitColumnPart(sColumnPart); for (const OUString& sColumn : sColumns) @@ -232,15 +245,15 @@ void CreateStmtParser::parseColumnPart(const OUString& sColumnPart) = bIsQuoteUsedForColumnName ? sColumn.indexOf("\"", 1) + 1 : sColumn.indexOf(" "); OUString rColumnName = sColumn.copy(0, nEndColumnName); - const OUString& sFromTypeName = sColumn.copy(nEndColumnName).trim(); + const OUString sFromTypeName(o3tl::trim(sColumn.subView(nEndColumnName))); // Now let's manage the column type // search next space to get the whole type name // eg: INTEGER, VARCHAR(10), DECIMAL(6,3) auto nNextSpace = sFromTypeName.indexOf(" "); - OUString sFullTypeName; + std::u16string_view sFullTypeName; if (nNextSpace > 0) - sFullTypeName = sFromTypeName.copy(0, nNextSpace); + sFullTypeName = sFromTypeName.subView(0, nNextSpace); // perhaps column type corresponds to the last info here else sFullTypeName = sFromTypeName; @@ -253,29 +266,30 @@ void CreateStmtParser::parseColumnPart(const OUString& sColumnPart) if (isPrimaryKey) m_PrimaryKeys.push_back(rColumnName); - const OUString sColumnWithoutName = sColumn.copy(sColumn.indexOf(typeParts.typeName)); + const std::u16string_view sColumnWithoutName + = sColumn.subView(sColumn.indexOf(typeParts.typeName)); ColumnDefinition aColDef(rColumnName, lcl_getDataTypeFromHsql(typeParts.typeName), - typeParts.params, isPrimaryKey, + std::move(typeParts.params), isPrimaryKey, lcl_getAutoIncrementDefault(sColumnWithoutName), lcl_isNullable(sColumnWithoutName), bCaseInsensitive, - lcl_getDefaultValue(sColumnWithoutName)); + OUString(lcl_getDefaultValue(sColumnWithoutName))); m_aColumns.push_back(aColDef); } } -void CreateStmtParser::parse(const OUString& sSql) +void CreateStmtParser::parse(std::u16string_view sSql) { // TODO Foreign keys - if (!sSql.startsWith("CREATE")) + if (!o3tl::starts_with(sSql, u"CREATE")) { SAL_WARN("dbaccess", "Not a create statement"); return; } m_sTableName = utils::getTableNameFromStmt(sSql); - OUString sColumnPart = lcl_getColumnPart(sSql); + std::u16string_view sColumnPart = lcl_getColumnPart(sSql); parseColumnPart(sColumnPart); } |