Skip to content

Implement CreateOrUpdate on IDbProvider #13

@ChaseFlorell

Description

@ChaseFlorell

IDbProvider needs a CreateOrUpdate method so that we don't have to make multiple calls.

SQLIte will be simple

INSERT OR REPLACE INTO Employee (id, name, role) 
  VALUES (1, 'John Foo', 'CEO');

SQL Server will be a little trickier. We could "merge" if it's easy to do in the dialect, or we have to do it the old way with an if exists call.

# merge example
MERGE dbo.ClientData AS [Target] 
USING (SELECT 12345 AS clientId, 'Some' AS data) AS [Source] ON [Target].clientId = [Source].clientId 
WHEN MATCHED THEN UPDATE SET [Target].data = [Source].data, [Target].updatedDateUtc = GetUtcDate() 
WHEN NOT MATCHED THEN INSERT (clientId, data) VALUES ([Source].clientId, [Source].data);

# if exists examlpe
IF NOT EXISTS (SELECT * FROM dbo.Employee WHERE ID = @SomeID)
    INSERT INTO dbo.Employee(Col1, ..., ColN)
    VALUES(Val1, .., ValN)
ELSE
    UPDATE dbo.Employee
    SET Col1 = Val1, Col2 = Val2, ...., ColN = ValN
    WHERE ID = @SomeID

Methods to add to IDbProvider

void CreateOrUpdate<TModel>(TModel model, string identifierName = "Id") where TModel : class, new();
void CreateOrUpdate<TModel>(TModel model, Expression<Func<TModel, object>> identifier) where TModel : class, new();
void CreateOrUpdate<TModel>(TModel model, IDbMapper<TModel> dbMapper, string identifierName = "Id") where TModel : class, new();
void CreateOrUpdate<TModel>(TModel model, IDbMapper<TModel> dbMapper, Expression<Func<TModel, object>> identifier) where TModel : class, new();
Task CreateOrUpdateAsync<TModel>(TModel model, Expression<Func<TModel, object>> identifier) where TModel : class, new();
Task CreateOrUpdateAsync<TModel>(TModel model, string identifierName = "Id") where TModel : class, new();
Task CreateOrUpdateAsync<TModel>(TModel model, IDbMapper<TModel> dbMapper, string identifierName = "Id") where TModel : class, new();
Task CreateOrUpdateAsync<TModel>(TModel model, IDbMapper<TModel> dbMapper, Expression<Func<TModel, object>> identifier) where TModel : class, new();

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions