/// <summary> /// Gets a random value of the given postgresql type. /// </summary> /// <exception cref="TeamworkConnectionException">Is raised if the given type is not supported.</exception> /// <returns>The random value.</returns> private string GetRandomValue(string type, ISQLFile target) { switch (type) { case "bigint": case "integer": case "smallint": case "decimal": case "numeric": case "int": case "bit": case "varbit": case "bit varying": return("1"); case "bool": case "boolean": return("true"); case "double precision": case "real": case "money": return("1.2"); case "date": case "timestamp": case "timestamp without time zone": case "time": case "time without time zone": case "time with time zone": case "timestamp with time zone": return("now()"); case "char": case "character": return("C"); case "text": case "varchar": case "character varying": return("testtext"); default: throw new TeamworkConnectionException(target, $"Could not create data for file {target.FileName} because the type {type} is not supported!"); } }
private void CreateData(Database database, ISQLFile target, PgSchema schema, PgTable table, Dictionary <PgTable, bool> executedTables) { foreach (var constraint in table.Constraints) { var references = "REFERENCES "; if (constraint.CreationSQL.Contains(references)) { var referenceStart = constraint.CreationSQL.IndexOf(references) + references.Length; var referenceEnd = constraint.CreationSQL.Substring(referenceStart).IndexOf("("); var reference = constraint.CreationSQL.Substring(referenceStart, referenceEnd); if (schema.Tables.Any(t => t.Name.QuoteName() == reference)) { var constraintTable = schema.Tables.Single(t => t.Name.QuoteName() == reference); // check if file was not already executed if (executedTables[constraintTable] == false) { this.CreateData(database, target, schema, constraintTable, executedTables); } } } } var columns = string.Empty; var first = true; foreach (var column in table.Columns) { if (!first) { columns += ", "; } if (column.NullValue) { columns += "DEFAULT"; } else if (column.DefaultValue != null) { columns += column.DefaultValue; } else if (schema.Types.Any(t => t.Name.QuoteName() == column.Type)) { var type = schema.Types.Single(t => t.Name.QuoteName() == column.Type); columns += type.EnumEntries.First(); } else { columns += this.GetRandomValue(column.Type, target); } first = false; } var insertSQL = $"INSERT INTO {table.Name.QuoteName()} VALUES ({columns});"; database.ExecuteCommandNonQuery(insertSQL); executedTables[table] = true; }
/// <summary> /// Creates a new Exception with the given parameters, a user defined message and a /// inner exception which is used for the stack trace. /// </summary> public TeamworkConnectionException([NullGuard.AllowNull] ISQLFile file, string message, NpgsqlException innerException) : base(message, innerException) { this.SqlException = innerException; this.File = file; }
/// <summary> /// Creates a new Exception with the given parameters and a user defined message. /// </summary> public TeamworkConnectionException([NullGuard.AllowNull] ISQLFile file, string message) : base(message) { this.File = file; }