2012-10-12 12:37:55 -02:00
using System ;
2012-11-27 15:07:53 -01:00
using Umbraco.Core.Logging ;
2012-10-14 12:56:02 -02:00
using Umbraco.Core.Persistence.Migrations.Initial ;
2012-10-19 13:20:57 -02:00
using Umbraco.Core.Persistence.SqlSyntax ;
using Umbraco.Core.Persistence.SqlSyntax.ModelDefinitions ;
2012-10-12 12:37:55 -02:00
namespace Umbraco.Core.Persistence
{
public static class PetaPocoExtensions
{
2012-10-25 18:38:23 -02:00
internal delegate void CreateTableEventHandler ( string tableName , Database db , TableCreationEventArgs e ) ;
internal static event CreateTableEventHandler NewTable ;
2012-10-12 12:37:55 -02:00
public static void CreateTable < T > ( this Database db )
where T : new ( )
{
var tableType = typeof ( T ) ;
CreateTable ( db , false , tableType ) ;
}
public static void CreateTable < T > ( this Database db , bool overwrite )
where T : new ( )
{
var tableType = typeof ( T ) ;
CreateTable ( db , overwrite , tableType ) ;
}
public static void CreateTable ( this Database db , bool overwrite , Type modelType )
{
2012-12-07 14:52:54 -01:00
//TODO The line below should be refactored to use 'Umbraco.Core.Persistence.DatabaseModelDefinitions.DefinitionFactory.GetTableDefinition(modelType)'
//But first the sql syntax provider should be updated/refactored to format sql statements using the 'new' definitions from the DatabaseModelDefinitions-namespace.
2012-10-19 13:20:57 -02:00
var tableDefinition = DefinitionFactory . GetTableDefinition ( modelType ) ;
var tableName = tableDefinition . TableName ;
2012-10-12 12:37:55 -02:00
2012-10-19 13:20:57 -02:00
string createSql = SyntaxConfig . SqlSyntaxProvider . ToCreateTableStatement ( tableDefinition ) ;
string createPrimaryKeySql = SyntaxConfig . SqlSyntaxProvider . ToCreatePrimaryKeyStatement ( tableDefinition ) ;
var foreignSql = SyntaxConfig . SqlSyntaxProvider . ToCreateForeignKeyStatements ( tableDefinition ) ;
var indexSql = SyntaxConfig . SqlSyntaxProvider . ToCreateIndexStatements ( tableDefinition ) ;
2012-10-18 11:49:44 -02:00
2012-10-12 12:37:55 -02:00
var tableExist = db . TableExist ( tableName ) ;
2012-10-19 13:20:57 -02:00
if ( overwrite & & tableExist )
2012-10-12 12:37:55 -02:00
{
db . DropTable ( tableName ) ;
}
2012-10-19 13:20:57 -02:00
if ( ! tableExist )
2012-10-12 12:37:55 -02:00
{
2012-12-05 12:37:00 -01:00
using ( var transaction = db . GetTransaction ( ) )
2012-10-25 18:38:23 -02:00
{
2012-12-05 12:37:00 -01:00
//Execute the Create Table sql
int created = db . Execute ( new Sql ( createSql ) ) ;
2012-12-04 08:16:10 -01:00
2012-12-05 12:37:00 -01:00
//If any statements exists for the primary key execute them here
if ( ! string . IsNullOrEmpty ( createPrimaryKeySql ) )
db . Execute ( new Sql ( createPrimaryKeySql ) ) ;
2012-12-05 08:39:31 -01:00
2012-12-05 12:37:00 -01:00
//Fires the NewTable event, which is used internally to insert base data before adding constrants to the schema
if ( NewTable ! = null )
{
var e = new TableCreationEventArgs ( ) ;
2012-12-05 14:18:08 -01:00
//Turn on identity insert if db provider is not mysql
2012-12-14 08:06:32 +05:00
if ( ApplicationContext . Current . DatabaseContext . ProviderName . Contains ( "MySql" ) = = false & & tableDefinition . IsIdentity )
2012-12-05 14:18:08 -01:00
db . Execute ( new Sql ( string . Format ( "SET IDENTITY_INSERT {0} ON " , SyntaxConfig . SqlSyntaxProvider . GetQuotedTableName ( tableName ) ) ) ) ;
//Call the NewTable-event to trigger the insert of base/default data
2012-12-05 12:37:00 -01:00
NewTable ( tableName , db , e ) ;
2012-12-05 14:18:08 -01:00
//Turn off identity insert if db provider is not mysql
2012-12-14 08:06:32 +05:00
if ( ApplicationContext . Current . DatabaseContext . ProviderName . Contains ( "MySql" ) = = false & & tableDefinition . IsIdentity )
2012-12-05 14:18:08 -01:00
db . Execute ( new Sql ( string . Format ( "SET IDENTITY_INSERT {0} OFF;" , SyntaxConfig . SqlSyntaxProvider . GetQuotedTableName ( tableName ) ) ) ) ;
2012-12-05 12:37:00 -01:00
}
2012-10-30 15:03:58 -01:00
2012-12-05 12:37:00 -01:00
//Loop through foreignkey statements and execute sql
foreach ( var sql in foreignSql )
{
int createdFk = db . Execute ( new Sql ( sql ) ) ;
}
//Loop through index statements and execute sql
foreach ( var sql in indexSql )
2012-10-30 15:03:58 -01:00
{
2012-12-05 12:37:00 -01:00
int createdIndex = db . Execute ( new Sql ( sql ) ) ;
2012-10-30 15:03:58 -01:00
}
2012-12-05 12:37:00 -01:00
//Specific to Sql Ce - look for changes to Identity Seed
2012-12-14 08:06:32 +05:00
if ( ApplicationContext . Current . DatabaseContext . ProviderName . Contains ( "SqlServerCe" ) )
2012-12-05 12:37:00 -01:00
{
var seedSql = SyntaxConfig . SqlSyntaxProvider . ToAlterIdentitySeedStatements ( tableDefinition ) ;
foreach ( var sql in seedSql )
{
int createdSeed = db . Execute ( new Sql ( sql ) ) ;
}
}
transaction . Complete ( ) ;
2012-10-30 15:03:58 -01:00
}
2012-10-12 12:37:55 -02:00
}
2012-11-27 15:07:53 -01:00
LogHelper . Info < Database > ( string . Format ( "New table '{0}' was created" , tableName ) ) ;
2012-10-12 12:37:55 -02:00
}
public static void DropTable < T > ( this Database db )
where T : new ( )
{
Type type = typeof ( T ) ;
var tableNameAttribute = type . FirstAttribute < TableNameAttribute > ( ) ;
if ( tableNameAttribute = = null )
throw new Exception (
string . Format (
"The Type '{0}' does not contain a TableNameAttribute, which is used to find the name of the table to drop. The operation could not be completed." ,
type . Name ) ) ;
string tableName = tableNameAttribute . Value ;
DropTable ( db , tableName ) ;
}
public static void DropTable ( this Database db , string tableName )
{
2012-10-19 13:20:57 -02:00
var sql = new Sql ( string . Format ( "DROP TABLE {0}" , SyntaxConfig . SqlSyntaxProvider . GetQuotedTableName ( tableName ) ) ) ;
2012-10-12 12:37:55 -02:00
db . Execute ( sql ) ;
}
public static bool TableExist ( this Database db , string tableName )
{
2012-10-19 13:20:57 -02:00
return SyntaxConfig . SqlSyntaxProvider . DoesTableExist ( db , tableName ) ;
2012-10-12 12:37:55 -02:00
}
2012-10-14 12:56:02 -02:00
2012-11-29 12:40:48 -01:00
public static void CreateDatabaseSchema ( this Database db )
2012-10-14 12:56:02 -02:00
{
2012-10-25 18:38:23 -02:00
NewTable + = PetaPocoExtensions_NewTable ;
2012-11-27 15:07:53 -01:00
LogHelper . Info < Database > ( "Initializing database schema creation" ) ;
2012-12-04 08:16:10 -01:00
var creation = new DatabaseSchemaCreation ( db ) ;
2012-10-23 07:30:00 -02:00
creation . InitializeDatabaseSchema ( ) ;
2012-11-01 15:06:35 -01:00
NewTable - = PetaPocoExtensions_NewTable ;
2012-10-14 12:56:02 -02:00
}
2012-10-24 12:38:13 -02:00
2012-10-29 09:49:31 -01:00
private static void PetaPocoExtensions_NewTable ( string tableName , Database db , TableCreationEventArgs e )
2012-10-24 12:38:13 -02:00
{
var baseDataCreation = new BaseDataCreation ( db ) ;
2012-10-25 18:38:23 -02:00
baseDataCreation . InitializeBaseData ( tableName ) ;
2012-10-24 12:38:13 -02:00
}
2012-10-12 12:37:55 -02:00
}
2012-10-25 18:38:23 -02:00
internal class TableCreationEventArgs : System . ComponentModel . CancelEventArgs { }
2012-10-12 12:37:55 -02:00
}