internal static string CreateObjectsScript(StoreItemCollection itemCollection) { DdlBuilder builder = new DdlBuilder(); foreach (EntityContainer container in itemCollection.GetItems <EntityContainer>()) { var entitySets = container.BaseEntitySets.OfType <EntitySet>().OrderBy(s => s.Name); var schemas = new HashSet <string>(entitySets.Select(s => GetSchemaName(s))); foreach (string schema in schemas.OrderBy(s => s)) { // don't bother creating default schema if (schema != "dbo") { builder.AppendCreateSchema(schema); } } foreach (EntitySet entitySet in container.BaseEntitySets.OfType <EntitySet>().OrderBy(s => s.Name)) { builder.AppendCreateTable(entitySet); } foreach (AssociationSet associationSet in container.BaseEntitySets.OfType <AssociationSet>().OrderBy(s => s.Name)) { builder.AppendCreateForeignKeys(associationSet); } } return(builder.GetCommandText()); }
protected override void DbDeleteDatabase(DbConnection connection, int?commandTimeout, StoreItemCollection storeItemCollection) { if (connection == null) { throw new ArgumentNullException("connection must not be null"); } if (storeItemCollection == null) { throw new ArgumentNullException("storeItemCollection must not be null"); } SampleConnection sampleConnection = connection as SampleConnection; if (sampleConnection == null) { throw new ArgumentException("connection must be a valid SampleConnection"); } string databaseName = GetDatabaseName(sampleConnection); string dropDatabaseScript = DdlBuilder.DropDatabaseScript(databaseName); // clear the connection pool in case someone is holding on to the database sampleConnection.ClearPool(); UsingMasterConnection(sampleConnection, (conn) => { CreateCommand(conn, dropDatabaseScript, commandTimeout).ExecuteNonQuery(); }); }
protected override bool DbDatabaseExists(DbConnection connection, int?commandTimeout, StoreItemCollection storeItemCollection) { if (connection == null) { throw new ArgumentNullException("connection must not be null"); } if (storeItemCollection == null) { throw new ArgumentNullException("storeItemCollection must not be null"); } SampleConnection sampleConnection = connection as SampleConnection; if (sampleConnection == null) { throw new ArgumentException("connection must be a valid SampleConnection"); } string databaseName = GetDatabaseName(sampleConnection); bool exists = false; UsingMasterConnection(sampleConnection, conn => { StoreVersion storeVersion = StoreVersionUtils.GetStoreVersion(conn); string databaseExistsScript = DdlBuilder.CreateDatabaseExistsScript(databaseName); int result = (int)CreateCommand(conn, databaseExistsScript, commandTimeout).ExecuteScalar(); exists = (result == 1); }); return(exists); }
internal static string DropDatabaseScript(string databaseName) { var builder = new DdlBuilder(); builder.AppendSql("drop database "); builder.AppendIdentifier(databaseName); return(builder.stringBuilder.ToString()); }
internal static string CreateDatabaseExistsScript(string databaseName) { var builder = new DdlBuilder(); builder.AppendSql("SELECT Count(*) FROM "); builder.AppendSql("sys.databases"); builder.AppendSql(" WHERE [name]="); builder.AppendStringLiteral(databaseName); return(builder.stringBuilder.ToString()); }
protected override string DbCreateDatabaseScript(string providerManifestToken, StoreItemCollection storeItemCollection) { if (providerManifestToken == null) { throw new ArgumentNullException("providerManifestToken must not be null"); } if (storeItemCollection == null) { throw new ArgumentNullException("storeItemCollection must not be null"); } return(DdlBuilder.CreateObjectsScript(storeItemCollection)); }
protected override void DbCreateDatabase(DbConnection connection, int?commandTimeout, StoreItemCollection storeItemCollection) { if (connection == null) { throw new ArgumentNullException("connection must not be null"); } if (storeItemCollection == null) { throw new ArgumentNullException("storeItemCollection must not be null"); } SampleConnection sampleConnection = connection as SampleConnection; if (sampleConnection == null) { throw new ArgumentException("The connection is not of type 'SampleConnection'."); } string databaseName = GetDatabaseName(sampleConnection); if (string.IsNullOrEmpty(databaseName)) { throw new InvalidOperationException("Initial Catalog is missing from the connection string"); } string dataFileName, logFileName; GetDatabaseFileNames(sampleConnection, out dataFileName, out logFileName); string createDatabaseScript = DdlBuilder.CreateDatabaseScript(databaseName, dataFileName, logFileName); string createObjectsScript = DdlBuilder.CreateObjectsScript(storeItemCollection); UsingMasterConnection(sampleConnection, conn => { // create database CreateCommand(conn, createDatabaseScript, commandTimeout).ExecuteNonQuery(); }); // Clear connection pool for the database connection since after the 'create database' call, a previously // invalid connection may now be valid. sampleConnection.ClearPool(); UsingConnection(sampleConnection, conn => { // create database objects CreateCommand(conn, createObjectsScript, commandTimeout).ExecuteNonQuery(); }); }
internal static string CreateDatabaseScript(string databaseName, string dataFileName, string logFileName) { var builder = new DdlBuilder(); builder.AppendSql("create database "); builder.AppendIdentifier(databaseName); if (null != dataFileName) { builder.AppendSql(" on primary "); builder.AppendFileName(dataFileName); builder.AppendSql(" log on "); builder.AppendFileName(logFileName); } return(builder.stringBuilder.ToString()); }
private void AppendCreateSchema(string schema) { AppendSql("if (schema_id("); AppendStringLiteral(schema); AppendSql(") is null) exec("); // need to create a sub-command and escape it as a string literal as well... DdlBuilder schemaBuilder = new DdlBuilder(); schemaBuilder.AppendSql("create schema "); schemaBuilder.AppendIdentifier(schema); AppendStringLiteral(schemaBuilder.stringBuilder.ToString()); AppendSql(");"); AppendNewLine(); }