Skip to content

Commit

Permalink
Added support for deleting tables from Ceen.Database
Browse files Browse the repository at this point in the history
  • Loading branch information
kenkendk committed May 15, 2020
1 parent a3eb59c commit 291d573
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 0 deletions.
7 changes: 7 additions & 0 deletions Ceen.Database/DatabaseDialectBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,13 @@ public virtual TableMapping GetTypeMap(Type type)
/// <param name="ifNotExists">Only create table if it does not exist</param>
public abstract string CreateTableSql(Type recordtype, bool ifNotExists = true);

/// <summary>
/// Returns a delete-table sql statement
/// </summary>
/// <param name="recordtype">The datatype to delete from the table.</param>
/// <param name="ifExists">Only delete table if it exists</param>
public abstract string DeleteTableSql(Type recordtype, bool ifExists = true);

/// <summary>
/// Creates a command that checks if a table exists
/// </summary>
Expand Down
17 changes: 17 additions & 0 deletions Ceen.Database/DatabaseDialectSQLite.cs
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,23 @@ public override string CreateTableSql(Type recordtype, bool ifNotExists = true)
return sql;
}

/// <summary>
/// Returns a delete-table sql statement
/// </summary>
/// <param name="recordtype">The datatype to delete from the table.</param>
/// <param name="ifExists">Only delete table if it exists</param>
public override string DeleteTableSql(Type recordtype, bool ifExists = true)
{
var mapping = GetTypeMap(recordtype);

return string.Format(
@"DROP TABLE{0} {1}",
ifExists ? " IF EXISTS" : "",
QuoteName(mapping.Name)
);
}


/// <summary>
/// Creates a command that checks if a table exists
/// </summary>
Expand Down
25 changes: 25 additions & 0 deletions Ceen.Database/DatabaseHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,31 @@ public static void CreateTable(this IDbConnection connection, Type type, bool if
}
}

/// <summary>
/// Deletes the table for the given type
/// </summary>
/// <typeparam name="T">The type of the table to delete.</typeparam>
/// <param name="connection">The connection to use</param>
public static void DeleteTable<T>(this IDbConnection connection)
{
DeleteTable(connection, typeof(T));
}

/// <summary>
/// Deletes a table for the given type
/// </summary>
/// <param name="type">The type of the table to delete.</param>
/// <param name="connection">The connection to use</param>
/// <param name="ifExists">Only delete table if it exists</param>
public static void DeleteTable(this IDbConnection connection, Type type, bool ifExists = true)
{
var dialect = GetDialect(connection);
var mapping = dialect.GetTypeMap(type);

var sql = dialect.DeleteTableSql(type);
using (var cmd = connection.CreateCommand(sql))
cmd.ExecuteNonQuery();
}

/// <summary>
/// Checks if the table for the given type exists
Expand Down
7 changes: 7 additions & 0 deletions Ceen.Database/IDatabaseDialect.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,13 @@ public interface IDatabaseDialect
/// <param name="ifNotExists">Only create table if it does not exist</param>
string CreateTableSql(Type recordtype, bool ifNotExists = true);

/// <summary>
/// Returns a delete-table sql statement
/// </summary>
/// <param name="recordtype">The datatype to delete from the table.</param>
/// <param name="ifExists">Only delete table if it exists</param>
string DeleteTableSql(Type recordtype, bool ifExists = true);

/// <summary>
/// Creates a command that checks if a table exists
/// </summary>
Expand Down

0 comments on commit 291d573

Please sign in to comment.