public bool CreateTable <TSchema>(string tableName) where TSchema : IEntityBase { log.Describe($"{nameof(CreateTable)}<{typeof(TSchema).Name}>: {tableName}"); if (string.IsNullOrWhiteSpace(tableName)) { throw new ArgumentNullException(nameof(tableName)); } var type = typeof(TSchema); var columns = ReflectionUtil.GetColumnSchemas(type).ToArray(); var sql = new StringBuilder(512); if (columns.Length == 0) { throw new Exception("No columns defined by " + nameof(PostgreSqlColumnAttribute) + " in class: " + type.Name); } sql.AppendFormat("CREATE TABLE ") .WriteLine(ResolveTableName(tableName)) .WriteLine("("); foreach (var column in columns) { var columnName = '"' + column.Name + '"'; var columnType = column.DataType; var columnNotNull = column.Nullable ? null : " NOT NULL"; sql.WriteTabbedLine(1, $"{columnName} {columnType}{columnNotNull},"); } var pk = columns.SingleOrDefault(k => k.PrimaryKey); if (pk != null) { sql.WriteTabbedLine(1, $"PRIMARY KEY(\"{pk.Name}\")"); } else { sql.TrimEndLineBreaks() .TrimEnd(',') .NewLine(); } sql.WriteLine(")"); return(db.Execute(sql.ToString()) > 0); }
private string resolveTable <TSchema>(string tableName) where TSchema : IEntityBase { tableName = postgreSql.ResolveTableName(tableName); if (!tables.Contains(tableName)) { tables.Add(tableName); if (!postgreSql.TableExists(tableName)) { log.Describe($"Create missing table: {tableName}"); postgreSql.CreateTable <TSchema>(tableName); } } return(tableName); }
private IEnumerable <DefaultItem> getChildrenOfItem(IItemReference itemRef, IEnumerable <Enum> types) { var ttypes = types .Select(context.TypeManager.GetTypeItem) .ToArray(); foreach (var type in ttypes) { var children = provider.QueryItemsByRelation(itemRef.Type.Name, itemRef.Guid, type.Name, CHILDREN_MAPPING_TABLE_NAME) .Select(child => new DefaultItem(child, context)) .ToArray(); foreach (var child in children) { yield return(child); } log.Describe($"Resolved {children.Length} children of type: {type}."); } }
public int Execute(string sql, IEnumerable<object> values) { var parameters = values as object[] ?? values.ToArray(); var stopwatch = new Stopwatch(); stopwatch.Start(); log.Describe(nameof(Execute)); log.Describe(sql, parameters); try { using (var cmd = new NpgsqlCommand()) { cmd.Connection = connection; cmd.CommandText = sql; if (parameters.Length > 0) appendParameters(parameters, cmd); return cmd.ExecuteNonQuery(); } } catch (Exception err) { log.Describe(err); throw err; } finally { log.Describe("Execute complete"); } }