Beispiel #1
0
        public async Task <IdentityResult> CreateAsync(AppUser user, CancellationToken cancellationToken)
        {
            ThrowIfDisposed();
            if (user == null)
            {
                throw new ArgumentNullException(nameof(user));
            }
            string sql = $"INSERT INTO {_configuration.UserTableDataKey.Item1}({_userIdPropertyInfo.Name}, ";

            foreach (var propertyInfo in _userPropertyInfos)
            {
                sql += $"{propertyInfo.Name}, ";
            }
            var idValue = _userIdPropertyInfo.GetValue(user);

            if (idValue == null)
            {
                throw new InvalidDbModelCastException();
            }
            sql = sql.Substring(0, sql.Length - 2) + $") VALUES ('{idValue}', ";
            foreach (var propertyInfo in _userPropertyInfos)
            {
                var propValue = propertyInfo.GetValue(user);
                if (propValue == null && !_configuration.NullableFields.Contains(propertyInfo.Name))
                {
                    throw new InvalidDbModelCastException();
                }
                sql += $"'{propValue}', ";
            }
            sql = sql.Substring(0, sql.Length - 2) + ")";
            await _connection.Execute(sql);

            return(IdentityResult.Success);
        }
Beispiel #2
0
        public void ClearAllData(IDapperConnection connection, int?commandTimeout = null)
        {
            if (!(connection.Dialect is ISchemaQueryDialect dialect))
            {
                throw new ArgumentException($"The dialect '{connection.Dialect.Name}' does not support querying the schema and can therefore not be used");
            }

            var ignoredTables = new HashSet <string>(this.IgnoredTables ?? Enumerable.Empty <string>());

            var tables    = connection.Query <AllTablesQueryResult>(dialect.MakeGetAllTablesStatement());
            var relations = connection.Query <TableRelationsQueryResult>(dialect.MakeGetAllRelationsStatement());

            var schemaRelations = new SchemaRelations();

            foreach (var table in tables)
            {
                if (!ignoredTables.Contains(table.Name))
                {
                    schemaRelations.AddTable(table.Name);
                }
            }

            foreach (var relation in relations)
            {
                schemaRelations.AddRelationship(relation.ReferencedTable, relation.ReferencingTable, relation.ReferencingColumn, relation.RelationIsOptional);
            }

            var commands = schemaRelations.GetClearDataCommands();

            foreach (var command in commands)
            {
                switch (command)
                {
                case ClearTableCommand c:
                {
                    var tableSchema = new TableSchema(c.TableName, ImmutableArray <ColumnSchema> .Empty);

                    var sql = dialect.MakeDeleteRangeStatement(tableSchema, null);
                    connection.Execute(sql, commandTimeout: commandTimeout);
                    break;
                }

                case NullColumnCommand c:
                {
                    var sql = dialect.MakeSetColumnNullStatement(c.TableName, c.ColumnName);
                    connection.Execute(sql, commandTimeout: commandTimeout);
                    break;
                }

                default:
                    throw new InvalidOperationException("Unknown sql command: " + command?.GetType());
                }
            }
        }
Beispiel #3
0
 private static void AddOrdersToCustomer(IDapperConnection dapperConnection, int customerId)
 {
     dapperConnection.Execute(BuildInsertOrdersAgainstCustomerSql(customerId));
     dapperConnection.Execute(BuildInsertOrdersAgainstCustomerSql(customerId));
     dapperConnection.Execute(BuildInsertOrdersAgainstCustomerSql(customerId));
     dapperConnection.Execute(BuildInsertOrdersAgainstCustomerSql(customerId));
     dapperConnection.Execute(BuildInsertOrdersAgainstCustomerSql(customerId));
     dapperConnection.Execute(BuildInsertOrdersAgainstCustomerSql(customerId));
     dapperConnection.Execute(BuildInsertOrdersAgainstCustomerSql(customerId));
     dapperConnection.Execute(BuildInsertOrdersAgainstCustomerSql(customerId));
     dapperConnection.Execute(BuildInsertOrdersAgainstCustomerSql(customerId));
     dapperConnection.Execute(BuildInsertOrdersAgainstCustomerSql(customerId));
 }