Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create TeradataProvider.cs #403

Open
wants to merge 6 commits into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion PetaPoco/Core/DatabaseProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,8 @@ internal static IProvider Resolve(Type type, bool allowDefault, string connectio
return Singleton<SqlServerDatabaseProvider>.Instance;
if (typeName.StartsWith("FbConnection") || typeName.EndsWith("FirebirdClientFactory"))
return Singleton<FirebirdDbDatabaseProvider>.Instance;
if (typeName.StartsWith("Teradata"))
return Singleton<TeradataDatabaseProvider>.Instance;
if (typeName.IndexOf("OleDb", StringComparison.InvariantCultureIgnoreCase) >= 0
&& (connectionString.IndexOf("Jet.OLEDB", StringComparison.InvariantCultureIgnoreCase) > 0 || connectionString.IndexOf("ACE.OLEDB", StringComparison.InvariantCultureIgnoreCase) > 0))
{
Expand Down Expand Up @@ -248,6 +250,8 @@ internal static IProvider Resolve(string providerName, bool allowDefault, string
if (providerName.IndexOf("Firebird", StringComparison.InvariantCultureIgnoreCase) >= 0 ||
providerName.IndexOf("FbConnection", StringComparison.InvariantCultureIgnoreCase) >= 0)
return Singleton<FirebirdDbDatabaseProvider>.Instance;
if (providerName.IndexOf("Teradata", StringComparison.InvariantCultureIgnoreCase) >= 0)
return Singleton<TeradataDatabaseProvider>.Instance;
if (providerName.IndexOf("OleDb", StringComparison.InvariantCultureIgnoreCase) >= 0
&& (connectionString.IndexOf("Jet.OLEDB", StringComparison.InvariantCultureIgnoreCase) > 0 || connectionString.IndexOf("ACE.OLEDB", StringComparison.InvariantCultureIgnoreCase) > 0))
{
Expand Down Expand Up @@ -280,4 +284,4 @@ internal static DbProviderFactory Unwrap(DbProviderFactory factory)
return unwrapped == null ? factory : Unwrap(unwrapped);
}
}
}
}
3 changes: 2 additions & 1 deletion PetaPoco/PetaPoco.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@
<Compile Include="Providers\SqlServerDatabaseProvider.cs" />
<Compile Include="Providers\SqlServerCEDatabaseProviders.cs" />
<Compile Include="Providers\MySqlDatabaseProvider.cs" />
<Compile Include="Providers\TeradataProvider.cs" />
<Compile Include="IAlterPoco.cs" />
<Compile Include="IDatabase.cs" />
<Compile Include="IExecute.cs" />
Expand Down Expand Up @@ -134,4 +135,4 @@
<Target Name="AfterBuild">
</Target>
-->
</Project>
</Project>
55 changes: 55 additions & 0 deletions PetaPoco/Providers/TeradataProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using System;
using System.Data;
using System.Data.Common;
using PetaPoco.Core;
using PetaPoco.Internal;
using PetaPoco.Utilities;
using System.Text.RegularExpressions;
using System.Linq;

namespace PetaPoco.Providers
{
public class TeradataDatabaseProvider : DatabaseProvider
{
public override DbProviderFactory GetFactory()
{
return DbProviderFactories.GetFactory("Teradata.Client.Provider");
}

private static readonly Regex simpleRegexOrderBy = new Regex(@"\bORDER\s+BY\s+", RegexOptions.RightToLeft | RegexOptions.IgnoreCase | RegexOptions.Multiline | RegexOptions.Singleline | RegexOptions.Compiled);

public override string BuildPageQuery(long skip, long take, SQLParts parts, ref object[] args)
{
var helper = (PagingHelper)PagingUtility;
// when the query does not contain an "order by", it is very slow
if (simpleRegexOrderBy.IsMatch(parts.SqlSelectRemoved))
{
parts.SqlSelectRemoved = helper.RegexOrderBy.Replace(parts.SqlSelectRemoved, "", 1);
}
if (helper.RegexDistinct.IsMatch(parts.SqlSelectRemoved))
{
parts.SqlSelectRemoved = "peta_inner.* FROM (SELECT " + parts.SqlSelectRemoved + ") peta_inner";
}
var sqlPage = string.Format("SELECT * FROM (SELECT ROW_NUMBER() OVER ({0}) peta_rn, {1}) peta_paged WHERE peta_rn > @{2} AND peta_rn <= @{3}", parts.SqlOrderBy ?? "ORDER BY (SELECT NULL)", parts.SqlSelectRemoved, args.Length, args.Length + 1);
args = args.Concat(new object[] { skip, skip + take }).ToArray();
return sqlPage;
}

public override object ExecuteInsert(Database db, System.Data.IDbCommand cmd, string primaryKeyName)
{
return db.ExecuteScalarHelper(cmd);
}

public override string GetExistsSql()
{
return "IF EXISTS (SELECT 1 FROM {0} WHERE {1}) SELECT 1 ELSE SELECT 0";
}

public override string GetInsertOutputClause(string primaryKeyName)
{
return String.Format(" OUTPUT INSERTED.[{0}]", primaryKeyName);
}
}


}