Permalink
Browse files

Creating unique index names for PostgreSql (#113)

  • Loading branch information...
sebastienros committed Jun 11, 2018
1 parent 8c30e7c commit f6b6428177e069dd0ba13a1ed098606a655d3eff
@@ -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; }
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
@@ -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)
@@ -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);
}
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.Data;
using Dapper;
@@ -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));
}
@@ -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;
@@ -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)
{

0 comments on commit f6b6428

Please sign in to comment.