Skip to content

Commit

Permalink
Creating unique index names for PostgreSql (#113)
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastienros committed Jun 11, 2018
1 parent 8c30e7c commit f6b6428
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 5 deletions.
5 changes: 5 additions & 0 deletions src/YesSql.Abstractions/ISqlDialect.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ public interface ISqlDialect
string PrimaryKeyString { get; }
string NullColumnString { get; }
bool SupportsUnique { get; }

/// <summary>
/// Returns whether the index names must be prefixed or not.
/// </summary>
bool PrefixIndex { get; }
bool HasDataTypeInIdentityColumn { get; }
bool SupportsIdentityColumns { get; }
string IdentityColumnString { get; }
Expand Down
2 changes: 1 addition & 1 deletion src/YesSql.Core/Sql/BaseComandInterpreter.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
Expand Down
19 changes: 17 additions & 2 deletions src/YesSql.Core/Sql/Schema/AlterTableCommand.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
using System;
using System;
using System.Data;

namespace YesSql.Sql.Schema
{
public class AlterTableCommand : SchemaCommand, IAlterTableCommand
{
public AlterTableCommand(string name)
private readonly ISqlDialect _dialect;
private readonly string _tablePrefix;

public AlterTableCommand(string name, ISqlDialect dialect, string tablePrefix)
: base(name, SchemaCommandType.AlterTable)
{
_dialect = dialect;
_tablePrefix = tablePrefix;
}

public void AddColumn(string columnName, DbType dbType, Action<IAddColumnCommand> column = null)
Expand Down Expand Up @@ -43,12 +48,22 @@ public void AlterColumn(string columnName, Action<IAlterColumnCommand> column =

public void CreateIndex(string indexName, params string[] columnNames)
{
if (_dialect.PrefixIndex)
{
indexName = _tablePrefix + indexName;
}

var command = new AddIndexCommand(Name, indexName, columnNames);
TableCommands.Add(command);
}

public void DropIndex(string indexName)
{
if (_dialect.PrefixIndex)
{
indexName = _tablePrefix + indexName;
}

var command = new DropIndexCommand(Name, indexName);
TableCommands.Add(command);
}
Expand Down
4 changes: 2 additions & 2 deletions src/YesSql.Core/Sql/SchemaBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.Data;
using Dapper;
Expand Down Expand Up @@ -185,7 +185,7 @@ public SchemaBuilder AlterTable(string name, Action<AlterTableCommand> table)
{
try
{
var alterTable = new AlterTableCommand(Prefix(name));
var alterTable = new AlterTableCommand(Prefix(name), _dialect, _tablePrefix);
table(alterTable);
Execute(_builder.CreateSql(alterTable));
}
Expand Down
2 changes: 2 additions & 0 deletions src/YesSql.Provider.Common/BaseDialect.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@ public virtual string GetDropTableString(string name)

public virtual string DefaultValuesInsert => "DEFAULT VALUES";

public virtual bool PrefixIndex => false;

protected virtual string Quote(string value)
{
return SingleQuoteString + value.Replace(SingleQuoteString, DoubleSingleQuoteString) + SingleQuoteString;
Expand Down
1 change: 1 addition & 0 deletions src/YesSql.Provider.PostgreSql/PostgreSqlDialect.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ public PostgreSqlDialect()
public override string IdentitySelectString => "RETURNING ";
public override string IdentityColumnString => "SERIAL PRIMARY KEY";
public override bool SupportsIfExistsBeforeTableName => true;
public override bool PrefixIndex => true;

public override string GetTypeName(DbType dbType, int? length, byte precision, byte scale)
{
Expand Down

0 comments on commit f6b6428

Please sign in to comment.