Details

    • Type: Improvement
    • Status: Resolved
    • Priority: Minor
    • Resolution: Fixed
    • Affects Version/s: 1.2.0
    • Fix Version/s: 1.2.0
    • Component/s: Core Framework
    • Labels:
      None

      Description

      NIFI-3481 introduced a DatabaseAdapter implementation for Microsoft SQL Server 2012+, it is not compatible with MS SQL 2008 because of its use of OFFSET and FETCH, e.g.

      This case is to introduce support for earlier versions of SQL Server to the database fetching processors such as QueryDatabaseTable and GenerateTableFetch. It may be the case that the generated SQL is awkward and less-than-ideal (more like Oracle's pagination query), but this will enable those processors to interact with SQL Server versions prior to 2012.

        Issue Links

          Activity

          Hide
          githubbot ASF GitHub Bot added a comment -

          GitHub user mattyb149 opened a pull request:

          https://github.com/apache/nifi/pull/1584

          NIFI-3585: Add DatabaseAdapter impl for MS SQL 2008

          Thank you for submitting a contribution to Apache NiFi.

          In order to streamline the review of the contribution we ask you
          to ensure the following steps have been taken:

              1. For all changes:
          • [x] Is there a JIRA ticket associated with this PR? Is it referenced
            in the commit message?
          • [x] Does your PR title start with NIFI-XXXX where XXXX is the JIRA number you are trying to resolve? Pay particular attention to the hyphen "-" character.
          • [x] Has your PR been rebased against the latest commit within the target branch (typically master)?
          • [x] Is your initial contribution a single, squashed commit?
              1. For code changes:
          • [x] Have you ensured that the full suite of tests is executed via mvn -Pcontrib-check clean install at the root nifi folder?
          • [x] Have you written or updated unit tests to verify your changes?
          • [ ] If adding new dependencies to the code, are these dependencies licensed in a way that is compatible for inclusion under [ASF 2.0](http://www.apache.org/legal/resolved.html#category-a)?
          • [ ] If applicable, have you updated the LICENSE file, including the main LICENSE file under nifi-assembly?
          • [ ] If applicable, have you updated the NOTICE file, including the main NOTICE file found under nifi-assembly?
          • [ ] If adding new Properties, have you added .displayName in addition to .name (programmatic access) for each of the new properties?
              1. For documentation related changes:
          • [x] Have you ensured that format looks appropriate for the output in which it is rendered?
              1. Note:
                Please ensure that once the PR is submitted, you check travis-ci for build issues and submit an update to your PR as soon as possible.

          You can merge this pull request into a Git repository by running:

          $ git pull https://github.com/mattyb149/nifi NIFI-3585

          Alternatively you can review and apply these changes as the patch at:

          https://github.com/apache/nifi/pull/1584.patch

          To close this pull request, make a commit to your master/trunk branch
          with (at least) the following in the commit message:

          This closes #1584


          commit 30efa7234185ee2c059a326e9f4c62d8b6c1cffc
          Author: Matt Burgess <mattyb149@apache.org>
          Date: 2017-03-10T19:16:07Z

          NIFI-3585: Add DatabaseAdapter impl for MS SQL 2008


          Show
          githubbot ASF GitHub Bot added a comment - GitHub user mattyb149 opened a pull request: https://github.com/apache/nifi/pull/1584 NIFI-3585 : Add DatabaseAdapter impl for MS SQL 2008 Thank you for submitting a contribution to Apache NiFi. In order to streamline the review of the contribution we ask you to ensure the following steps have been taken: For all changes: [x] Is there a JIRA ticket associated with this PR? Is it referenced in the commit message? [x] Does your PR title start with NIFI-XXXX where XXXX is the JIRA number you are trying to resolve? Pay particular attention to the hyphen "-" character. [x] Has your PR been rebased against the latest commit within the target branch (typically master)? [x] Is your initial contribution a single, squashed commit? For code changes: [x] Have you ensured that the full suite of tests is executed via mvn -Pcontrib-check clean install at the root nifi folder? [x] Have you written or updated unit tests to verify your changes? [ ] If adding new dependencies to the code, are these dependencies licensed in a way that is compatible for inclusion under [ASF 2.0] ( http://www.apache.org/legal/resolved.html#category-a)? [ ] If applicable, have you updated the LICENSE file, including the main LICENSE file under nifi-assembly? [ ] If applicable, have you updated the NOTICE file, including the main NOTICE file found under nifi-assembly? [ ] If adding new Properties, have you added .displayName in addition to .name (programmatic access) for each of the new properties? For documentation related changes: [x] Have you ensured that format looks appropriate for the output in which it is rendered? Note: Please ensure that once the PR is submitted, you check travis-ci for build issues and submit an update to your PR as soon as possible. You can merge this pull request into a Git repository by running: $ git pull https://github.com/mattyb149/nifi NIFI-3585 Alternatively you can review and apply these changes as the patch at: https://github.com/apache/nifi/pull/1584.patch To close this pull request, make a commit to your master/trunk branch with (at least) the following in the commit message: This closes #1584 commit 30efa7234185ee2c059a326e9f4c62d8b6c1cffc Author: Matt Burgess <mattyb149@apache.org> Date: 2017-03-10T19:16:07Z NIFI-3585 : Add DatabaseAdapter impl for MS SQL 2008
          Hide
          githubbot ASF GitHub Bot added a comment -

          Github user mattyb149 commented on the issue:

          https://github.com/apache/nifi/pull/1584

          @patricker do you mind taking a look at this? I am no MS SQL expert by any means, but I tested GenerateTableFetch and QueryDatabaseTable and they seem ok to me.

          Show
          githubbot ASF GitHub Bot added a comment - Github user mattyb149 commented on the issue: https://github.com/apache/nifi/pull/1584 @patricker do you mind taking a look at this? I am no MS SQL expert by any means, but I tested GenerateTableFetch and QueryDatabaseTable and they seem ok to me.
          Hide
          githubbot ASF GitHub Bot added a comment -

          Github user jfrazee commented on a diff in the pull request:

          https://github.com/apache/nifi/pull/1584#discussion_r105506187

          — Diff: nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/main/java/org/apache/nifi/processors/standard/db/impl/MSSQL2008DatabaseAdapter.java —
          @@ -0,0 +1,89 @@
          +/*
          + * Licensed to the Apache Software Foundation (ASF) under one or more
          + * contributor license agreements. See the NOTICE file distributed with
          + * this work for additional information regarding copyright ownership.
          + * The ASF licenses this file to You under the Apache License, Version 2.0
          + * (the "License"); you may not use this file except in compliance with
          + * the License. You may obtain a copy of the License at
          + *
          + * http://www.apache.org/licenses/LICENSE-2.0
          + *
          + * Unless required by applicable law or agreed to in writing, software
          + * distributed under the License is distributed on an "AS IS" BASIS,
          + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
          + * See the License for the specific language governing permissions and
          + * limitations under the License.
          + */
          +package org.apache.nifi.processors.standard.db.impl;
          +
          +import org.apache.commons.lang3.StringUtils;
          +import org.apache.nifi.processors.standard.db.DatabaseAdapter;
          +
          +/**
          + * A database adapter that generates MS SQL Compatible SQL for version 2008.
          + */
          +public class MSSQL2008DatabaseAdapter implements DatabaseAdapter {
          + @Override
          + public String getName()

          { + return "MS SQL 2008"; + }

          +
          + @Override
          + public String getDescription()

          { + return "Generates MS SQL Compatible SQL for version 2008"; + }

          +
          + @Override
          + public String getSelectStatement(String tableName, String columnNames, String whereClause, String orderByClause, Long limit, Long offset) {
          + if (StringUtils.isEmpty(tableName))

          { + throw new IllegalArgumentException("Table name cannot be null or empty"); + }

          +
          + final StringBuilder query = new StringBuilder("SELECT ");
          +
          + // If this is a limit query and not a paging query then use TOP in MS SQL
          + if (limit != null) {
          +
          + if (offset != null)

          { + query.append("* FROM (SELECT "); + }

          + final long effectiveOffset = (offset == null) ? 0 : offset;
          + if (effectiveOffset + limit > 0)

          { + query.append("TOP "); + query.append(effectiveOffset + limit); + query.append(" "); + }

          + }
          +
          + if (StringUtils.isEmpty(columnNames) || columnNames.trim().equals("*"))

          { + query.append("*"); + }

          else

          { + query.append(columnNames); + }

          +
          + if (limit != null && offset != null) {
          + query.append(", ROW_NUMBER() OVER(ORDER BY ID asc) rnum");
          — End diff –

          @mattyb149 I don't think there's a guarantee that the `ORDBER BY` column is `ID`. You meant for this to be `orderByClause`, right?

          Show
          githubbot ASF GitHub Bot added a comment - Github user jfrazee commented on a diff in the pull request: https://github.com/apache/nifi/pull/1584#discussion_r105506187 — Diff: nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/main/java/org/apache/nifi/processors/standard/db/impl/MSSQL2008DatabaseAdapter.java — @@ -0,0 +1,89 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.nifi.processors.standard.db.impl; + +import org.apache.commons.lang3.StringUtils; +import org.apache.nifi.processors.standard.db.DatabaseAdapter; + +/** + * A database adapter that generates MS SQL Compatible SQL for version 2008. + */ +public class MSSQL2008DatabaseAdapter implements DatabaseAdapter { + @Override + public String getName() { + return "MS SQL 2008"; + } + + @Override + public String getDescription() { + return "Generates MS SQL Compatible SQL for version 2008"; + } + + @Override + public String getSelectStatement(String tableName, String columnNames, String whereClause, String orderByClause, Long limit, Long offset) { + if (StringUtils.isEmpty(tableName)) { + throw new IllegalArgumentException("Table name cannot be null or empty"); + } + + final StringBuilder query = new StringBuilder("SELECT "); + + // If this is a limit query and not a paging query then use TOP in MS SQL + if (limit != null) { + + if (offset != null) { + query.append("* FROM (SELECT "); + } + final long effectiveOffset = (offset == null) ? 0 : offset; + if (effectiveOffset + limit > 0) { + query.append("TOP "); + query.append(effectiveOffset + limit); + query.append(" "); + } + } + + if (StringUtils.isEmpty(columnNames) || columnNames.trim().equals("*")) { + query.append("*"); + } else { + query.append(columnNames); + } + + if (limit != null && offset != null) { + query.append(", ROW_NUMBER() OVER(ORDER BY ID asc) rnum"); — End diff – @mattyb149 I don't think there's a guarantee that the `ORDBER BY` column is `ID`. You meant for this to be `orderByClause`, right?
          Hide
          githubbot ASF GitHub Bot added a comment -

          Github user jfrazee commented on the issue:

          https://github.com/apache/nifi/pull/1584

          @mattyb149 I think I'll be able to say LGTM if this `orderByClause` thing gets fixed. Right now I think it only works on tables where the max value column is ID.

          Show
          githubbot ASF GitHub Bot added a comment - Github user jfrazee commented on the issue: https://github.com/apache/nifi/pull/1584 @mattyb149 I think I'll be able to say LGTM if this `orderByClause` thing gets fixed. Right now I think it only works on tables where the max value column is ID.
          Show
          githubbot ASF GitHub Bot added a comment - Github user jfrazee commented on the issue: https://github.com/apache/nifi/pull/1584 @mattyb149 Ok LGTM now so +1 I tested with [NIFI-3585.xml] ( https://gist.github.com/jfrazee/63fc76f25bc5cdcfaf66d246cf232d19 ) on an Amazon RDS SQL Server instance using the Orders table + OrderID column in the Northwind database [1,2,3] . 1. https://northwinddatabase.codeplex.com 2. http://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/SQLServer.Procedural.Importing.html#SQLServer.Procedural.Importing.Native.Using.Restore 3. https://aws.amazon.com/blogs/database/migrating-microsoft-sql-server-enterprise-workloads-to-amazon-rds-part-1/
          Hide
          jira-bot ASF subversion and git services added a comment -

          Commit 37a1e6d07f9f8b10201c82f8610897f99142bd97 in nifi's branch refs/heads/master from Matt Burgess
          [ https://git-wip-us.apache.org/repos/asf?p=nifi.git;h=37a1e6d ]

          NIFI-3585: Add DatabaseAdapter impl for MS SQL 2008

          This closes #1584

          Signed-off-by: Joey Frazee <jfrazee@apache.org>

          Show
          jira-bot ASF subversion and git services added a comment - Commit 37a1e6d07f9f8b10201c82f8610897f99142bd97 in nifi's branch refs/heads/master from Matt Burgess [ https://git-wip-us.apache.org/repos/asf?p=nifi.git;h=37a1e6d ] NIFI-3585 : Add DatabaseAdapter impl for MS SQL 2008 This closes #1584 Signed-off-by: Joey Frazee <jfrazee@apache.org>
          Hide
          githubbot ASF GitHub Bot added a comment -

          Github user asfgit closed the pull request at:

          https://github.com/apache/nifi/pull/1584

          Show
          githubbot ASF GitHub Bot added a comment - Github user asfgit closed the pull request at: https://github.com/apache/nifi/pull/1584
          Hide
          joewitt Joseph Witt added a comment -

          was merged already

          Show
          joewitt Joseph Witt added a comment - was merged already

            People

            • Assignee:
              mattyb149 Matt Burgess
              Reporter:
              mattyb149 Matt Burgess
            • Votes:
              0 Vote for this issue
              Watchers:
              5 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved:

                Development