public bool Equals(DatabaseName?other) { if (other == null) { return(false); } return(other.Name == Name && object.Equals(Server, other.Server)); }
static string Use(DatabaseName?databaseName, string script) { if (databaseName == null) { return(script); } return("use " + databaseName + "\r\n" + script); }
public ObjectName OnDatabase(DatabaseName?databaseName) { if (databaseName != null && databaseName.IsPostgres != this.IsPostgres) { throw new Exception("Inconsitent IsPostgres"); } return(new ObjectName(new SchemaName(databaseName, Schema !.Name, IsPostgres), Name, IsPostgres)); }
public static SqlPreCommandSimple SP_RENAME(DatabaseName?database, string oldName, string newName, string?objectType) { return(new SqlPreCommandSimple("EXEC {0}SP_RENAME '{1}' , '{2}'{3}".FormatWith( database == null ? null: (new SchemaName(database, "dbo").ToString() + "."), oldName, newName, objectType == null ? null : ", '{0}'".FormatWith(objectType) ))); }
public SchemaName(DatabaseName?database, string name) { if (string.IsNullOrEmpty(name)) { throw new ArgumentNullException(nameof(name)); } this.Name = name; this.database = database; }
public static SqlPreCommand RemoveAllScript(DatabaseName?databaseName) { var schemas = SqlBuilder.SystemSchemas.ToString(a => "'" + a + "'", ", "); return(SqlPreCommand.Combine(Spacing.Double, new SqlPreCommandSimple(Use(databaseName, RemoveAllProceduresScript)), new SqlPreCommandSimple(Use(databaseName, RemoveAllViewsScript).FormatWith(schemas)), new SqlPreCommandSimple(Use(databaseName, RemoveAllConstraintsScript)), Connector.Current.SupportsTemporalTables ? new SqlPreCommandSimple(Use(databaseName, StopSystemVersioning)) : null, new SqlPreCommandSimple(Use(databaseName, RemoveAllTablesScript)), new SqlPreCommandSimple(Use(databaseName, RemoveAllSchemasScript.FormatWith(schemas))) ) !); }
public static SqlPreCommandSimple DropPrimaryKeyConstraint(ObjectName tableName) { DatabaseName?db = tableName.Schema.Database; var tn = tableName.OnDatabase(null); string varName = "PrimaryKey_Constraint_" + tn.Name; string command = @" DECLARE @sql nvarchar(max) SELECT @sql = 'ALTER TABLE {Table} DROP CONSTRAINT [' + kc.name + '];' FROM DB.sys.key_constraints kc WHERE kc.parent_object_id = OBJECT_ID('{FullTable}') EXEC DB.dbo.sp_executesql @sql" .Replace("DB.", db == null ? null : (db.ToString() + ".")) .Replace("@sql", "@" + varName) .Replace("{FullTable}", tableName.ToString()) .Replace("{Table}", tn.ToString()); return(new SqlPreCommandSimple(command)); }
public override void CleanDatabase(DatabaseName?database) { PostgreSqlConnectorScripts.RemoveAllScript(database).ExecuteNonQuery(); }
public static SqlPreCommandSimple RemoveAllScript(DatabaseName?databaseName) { if (databaseName != null) { throw new NotSupportedException(); } return(new SqlPreCommandSimple(@"-- Copyright © 2019 -- mirabilos <*****@*****.**> -- -- Provided that these terms and disclaimer and all copyright notices -- are retained or reproduced in an accompanying document, permission -- is granted to deal in this work without restriction, including un‐ -- limited rights to use, publicly perform, distribute, sell, modify, -- merge, give away, or sublicence. -- -- This work is provided “AS IS” and WITHOUT WARRANTY of any kind, to -- the utmost extent permitted by applicable law, neither express nor -- implied; without malicious intent or gross negligence. In no event -- may a licensor, author or contributor be held liable for indirect, -- direct, other damage, loss, or other issues arising in any way out -- of dealing in the work, even if advised of the possibility of such -- damage or existence of a defect, except proven that it results out -- of said person’s immediate fault when using the work as intended. -- - -- Drop everything from the PostgreSQL database. DO $$ DECLARE r RECORD; BEGIN -- triggers FOR r IN (SELECT pns.nspname, pc.relname, pt.tgname FROM pg_trigger pt, pg_class pc, pg_namespace pns WHERE pns.oid=pc.relnamespace AND pc.oid=pt.tgrelid AND pns.nspname NOT IN ('information_schema', 'pg_catalog', 'pg_toast') AND pt.tgisinternal=false ) LOOP EXECUTE format('DROP TRIGGER %I ON %I.%I;', r.tgname, r.nspname, r.relname); END LOOP; -- constraints #1: foreign key FOR r IN (SELECT pns.nspname, pc.relname, pcon.conname FROM pg_constraint pcon, pg_class pc, pg_namespace pns WHERE pns.oid=pc.relnamespace AND pc.oid=pcon.conrelid AND pns.nspname NOT IN ('information_schema', 'pg_catalog', 'pg_toast') AND pcon.contype='f' ) LOOP EXECUTE format('ALTER TABLE ONLY %I.%I DROP CONSTRAINT %I;', r.nspname, r.relname, r.conname); END LOOP; -- constraints #2: the rest FOR r IN (SELECT pns.nspname, pc.relname, pcon.conname FROM pg_constraint pcon, pg_class pc, pg_namespace pns WHERE pns.oid=pc.relnamespace AND pc.oid=pcon.conrelid AND pns.nspname NOT IN ('information_schema', 'pg_catalog', 'pg_toast') AND pcon.contype<>'f' ) LOOP EXECUTE format('ALTER TABLE ONLY %I.%I DROP CONSTRAINT %I;', r.nspname, r.relname, r.conname); END LOOP; -- indicēs FOR r IN (SELECT pns.nspname, pc.relname FROM pg_class pc, pg_namespace pns WHERE pns.oid=pc.relnamespace AND pns.nspname NOT IN ('information_schema', 'pg_catalog', 'pg_toast') AND pc.relkind='i' ) LOOP EXECUTE format('DROP INDEX %I.%I;', r.nspname, r.relname); END LOOP; -- normal and materialised views FOR r IN (SELECT pns.nspname, pc.relname FROM pg_class pc, pg_namespace pns WHERE pns.oid=pc.relnamespace AND pns.nspname NOT IN ('information_schema', 'pg_catalog', 'pg_toast') AND pc.relname NOT LIKE 'pg_%' AND pc.relkind IN ('v', 'm') ) LOOP EXECUTE format('DROP VIEW %I.%I;', r.nspname, r.relname); END LOOP; -- tables FOR r IN (SELECT pns.nspname, pc.relname FROM pg_class pc, pg_namespace pns WHERE pns.oid=pc.relnamespace AND pns.nspname NOT IN ('information_schema', 'pg_catalog', 'pg_toast') AND pc.relkind='r' ) LOOP EXECUTE format('DROP TABLE %I.%I;', r.nspname, r.relname); END LOOP; -- sequences FOR r IN (SELECT pns.nspname, pc.relname FROM pg_class pc, pg_namespace pns WHERE pns.oid=pc.relnamespace AND pns.nspname NOT IN ('information_schema', 'pg_catalog', 'pg_toast') AND pc.relkind='S' ) LOOP EXECUTE format('DROP SEQUENCE %I.%I;', r.nspname, r.relname); END LOOP; -- extensions (see below), only if necessary FOR r IN (SELECT pns.nspname, pe.extname FROM pg_extension pe, pg_namespace pns WHERE pns.oid=pe.extnamespace AND pns.nspname NOT IN ('information_schema', 'pg_catalog', 'pg_toast') ) LOOP EXECUTE format('DROP EXTENSION %I;', r.extname); END LOOP; -- functions / procedures FOR r IN (SELECT pns.nspname, pp.proname, pp.oid FROM pg_proc pp, pg_namespace pns WHERE pns.oid=pp.pronamespace AND pns.nspname NOT IN ('information_schema', 'pg_catalog', 'pg_toast') ) LOOP EXECUTE format('DROP FUNCTION %I.%I(%s);', r.nspname, r.proname, pg_get_function_identity_arguments(r.oid)); END LOOP; -- nōn-default schemata we own; assume to be run by a not-superuser FOR r IN (SELECT pns.nspname FROM pg_namespace pns, pg_roles pr WHERE pr.oid=pns.nspowner AND pns.nspname NOT IN ('information_schema', 'pg_catalog', 'pg_toast', 'public') AND pr.rolname=current_user ) LOOP EXECUTE format('DROP SCHEMA %I;', r.nspname); END LOOP; -- voilà RAISE NOTICE 'Database cleared!'; END; $$;")); }
public override void CleanDatabase(DatabaseName?databaseName) { SqlConnectorScripts.RemoveAllScript(databaseName).ExecuteLeaves(); SqlConnectorScripts.ShrinkDatabase(DatabaseName()); }
public ObjectName OnDatabase(DatabaseName?databaseName) { return(new ObjectName(new SchemaName(databaseName, Schema.Name), Name)); }
public abstract void CleanDatabase(DatabaseName?database);
internal SchemaName OnDatabase(DatabaseName?database) { return(new SchemaName(database, this.Name, this.IsPostgres)); }
public static List <T> ToListWithInvalidation <T>(this IQueryable <T> simpleQuery, Type type, string exceptionContext, Action <SqlNotificationEventArgs> invalidation) { if (!WithSqlDependency) { throw new InvalidOperationException("ToListWithInvalidation requires SqlDependency"); } ITranslateResult tr; using (ObjectName.OverrideOptions(new ObjectNameOptions { AvoidDatabaseName = true })) tr = ((DbQueryProvider)simpleQuery.Provider).GetRawTranslateResult(simpleQuery.Expression); void onChange(object sender, SqlNotificationEventArgs args) { try { if (args.Type != SqlNotificationType.Change) { throw new InvalidOperationException( "Problems with SqlDependency (Type : {0} Source : {1} Info : {2}) on query: \r\n{3}" .FormatWith(args.Type, args.Source, args.Info, tr.MainCommand.PlainSql())); } if (args.Info == SqlNotificationInfo.PreviousFire) { throw new InvalidOperationException("The same transaction that loaded the data is invalidating it!") { Data = { { "query", tr.MainCommand.PlainSql() } } } } ; if (CacheLogic.LogWriter != null) { CacheLogic.LogWriter.WriteLine("Change ToListWithInvalidations {0} {1}".FormatWith(typeof(T).TypeName()), exceptionContext); } invalidation(args); } catch (Exception e) { e.LogException(c => c.ControllerName = exceptionContext); } } SimpleReader?reader = null; Expression <Func <IProjectionRow, T> > projectorExpression = (Expression <Func <IProjectionRow, T> >)tr.GetMainProjector(); Func <IProjectionRow, T> projector = projectorExpression.Compile(); List <T> list = new List <T>(); CacheLogic.AssertSqlDependencyStarted(); Table table = Schema.Current.Table(type); DatabaseName?db = table.Name.Schema?.Database; SqlServerConnector subConnector = (SqlServerConnector)Connector.Current.ForDatabase(db); if (CacheLogic.LogWriter != null) { CacheLogic.LogWriter.WriteLine("Load ToListWithInvalidations {0} {1}".FormatWith(typeof(T).TypeName()), exceptionContext); } using (new EntityCache()) using (var r = EntityCache.NewRetriever()) { subConnector.ExecuteDataReaderDependency(tr.MainCommand, onChange, StartSqlDependencyAndEnableBrocker, fr => { if (reader == null) { reader = new SimpleReader(fr, r); } list.Add(projector(reader)); }, CommandType.Text); r.CompleteAll(); } return(list); }
public static IDisposable OverrideDatabaseInSysViews(DatabaseName?database) { return(OverrideViewNameReplacer(n => n.Schema.Name == "sys" ? n.OnDatabase(database) : n)); }