Skip to content

Commit

Permalink
Fix issues with update syntax (#31)
Browse files Browse the repository at this point in the history
  • Loading branch information
EndsOfTheEarth authored Jun 24, 2023
1 parent 058d5c3 commit be994be
Show file tree
Hide file tree
Showing 11 changed files with 894 additions and 218 deletions.
4 changes: 2 additions & 2 deletions Documentation.md
Original file line number Diff line number Diff line change
Expand Up @@ -505,8 +505,8 @@ using(Transaction transaction = new Transaction(DB.Northwind)) {
.Values(values => values
.Set(orderTable.ShipVia, null)
)
.Join(customersTable).On(orderTable.CustomerID == customersTable.CustomerID)
.Where(customersTable.Region.IsNull)
.From(customersTable)
.Where(orderTable.CustomerID == customersTable.CustomerID & customersTable.Region.IsNull)
.Execute(transaction);

transaction.Commit();
Expand Down
32 changes: 12 additions & 20 deletions QueryLite/Databases/PostgreSql/PreparedUpdateQueryGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ string IPreparedUpdateQueryGenerator.GetSql<PARAMETERS, RESULT>(PreparedUpdateTe
sql.Append('.');
}

bool useAlias = template.Joins != null;
bool useAlias = template.FromTables != null;

SqlHelper.AppendEncloseTableName(sql, template.Table);

Expand All @@ -59,34 +59,26 @@ string IPreparedUpdateQueryGenerator.GetSql<PARAMETERS, RESULT>(PreparedUpdateTe
parameters = valuesCollector.Parameters;
}

if(template.Joins != null) {
if(template.FromTables != null) {

sql.Append(" FROM ");

if(!string.IsNullOrWhiteSpace(schemaName)) {
SqlHelper.AppendEncloseSchemaName(sql, schemaName);
sql.Append('.');
}
SqlHelper.AppendEncloseTableName(sql, template.Table);
for(int index = 0; index < template.FromTables.Count; index++) {

foreach(PreparedUpdateJoin<PARAMETERS> join in template.Joins) {
ITable fromTable = template.FromTables[index];

string joinType = join.JoinType switch {
JoinType.Join => " JOIN",
JoinType.LeftJoin => " LEFT JOIN",
_ => throw new Exception($"Unknown join type. Type = {join.JoinType}")
};
sql.Append(' ').Append(joinType).Append(' ');
if(index > 0) {
sql.Append(',');
}

string joinSchemaName = database.SchemaMap(join.Table.SchemaName);
string fromSchemaName = database.SchemaMap(fromTable.SchemaName);

if(!string.IsNullOrWhiteSpace(joinSchemaName)) {
SqlHelper.AppendEncloseSchemaName(sql, joinSchemaName);
if(!string.IsNullOrWhiteSpace(fromSchemaName)) {
SqlHelper.AppendEncloseSchemaName(sql, fromSchemaName);
sql.Append('.');
}
SqlHelper.AppendEncloseTableName(sql, join.Table);
sql.Append(" AS ").Append(join.Table.Alias).Append(" ON ");
join.Condition.GetSql(sql, database, parameters, useAlias: useAlias);
SqlHelper.AppendEncloseTableName(sql, fromTable);
sql.Append(" as ").Append(fromTable.Alias);
}
}

Expand Down
33 changes: 12 additions & 21 deletions QueryLite/Databases/PostgreSql/UpdateQueryGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ string IUpdateQueryGenerator.GetSql<RESULT>(UpdateQueryTemplate template, IDatab
sql.Append('.');
}

bool useAlias = template.Joins != null;
bool useAlias = template.FromTables != null;

SqlHelper.AppendEncloseTableName(sql, template.Table);

Expand Down Expand Up @@ -71,35 +71,26 @@ string IUpdateQueryGenerator.GetSql<RESULT>(UpdateQueryTemplate template, IDatab
parameters = null;
}

if(template.Joins != null) {
if(template.FromTables != null) {

sql.Append(" FROM ");

if(!string.IsNullOrWhiteSpace(schemaName)) {
SqlHelper.AppendEncloseSchemaName(sql, schemaName);
sql.Append('.');
}
SqlHelper.AppendEncloseTableName(sql, template.Table);
sql.Append(' ');
for(int index = 0; index < template.FromTables.Count; index++) {

foreach(IJoin join in template.Joins) {
ITable fromTable = template.FromTables[index];

string joinType = join.JoinType switch {
JoinType.Join => "JOIN",
JoinType.LeftJoin => "LEFT JOIN",
_ => throw new Exception($"Unknown join type. Type = {join.JoinType}")
};
sql.Append(' ').Append(joinType).Append(' ');
if(index > 0) {
sql.Append(',');
}

string joinSchemaName = database.SchemaMap(join.Table.SchemaName);
string fromSchemaName = database.SchemaMap(fromTable.SchemaName);

if(!string.IsNullOrWhiteSpace(joinSchemaName)) {
SqlHelper.AppendEncloseSchemaName(sql, joinSchemaName);
if(!string.IsNullOrWhiteSpace(fromSchemaName)) {
SqlHelper.AppendEncloseSchemaName(sql, fromSchemaName);
sql.Append('.');
}
SqlHelper.AppendEncloseTableName(sql, join.Table);
sql.Append(" AS ").Append(join.Table.Alias).Append(" ON ");
join.Condition.GetSql(sql, database, useAlias: useAlias, parameters);
SqlHelper.AppendEncloseTableName(sql, fromTable);
sql.Append(" as ").Append(fromTable.Alias);
}
}

Expand Down
32 changes: 16 additions & 16 deletions QueryLite/Databases/SqlServer/PreparedUpdateQueryGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ string IPreparedUpdateQueryGenerator.GetSql<PARAMETERS, RESULT>(PreparedUpdateTe

sql.Append("UPDATE ");

bool useAlias = template.Joins != null;
bool useAlias = template.FromTables != null;

string schemaName = database.SchemaMap(template.Table.SchemaName);

Expand Down Expand Up @@ -71,38 +71,38 @@ string IPreparedUpdateQueryGenerator.GetSql<PARAMETERS, RESULT>(PreparedUpdateTe
SqlServerReturningCollectorCache.Release(collector);
}

if(template.Joins != null) {
if(template.FromTables != null) {

sql.Append(" FROM ");

if(!string.IsNullOrWhiteSpace(schemaName)) {
SqlHelper.AppendEncloseSchemaName(sql, schemaName);
sql.Append('.');
}

SqlHelper.AppendEncloseTableName(sql, template.Table);
sql.Append(" as ").Append(template.Table.Alias);

for(int index = 0; index < template.FromTables.Count; index++) {

sql.Append(" AS ").Append(template.Table.Alias);
ITable fromTable = template.FromTables[index];

foreach(PreparedUpdateJoin<PARAMETERS> join in template.Joins) {
if(fromTable == template.Table) {
throw new Exception("The update table must not be included in the 'FROM' table list");
}

sql.Append(join.JoinType switch {
JoinType.Join => " JOIN ",
JoinType.LeftJoin => " LEFT JOIN ",
_ => throw new Exception($"Unknown join type. Type = {join.JoinType}")
});
sql.Append(',');

string joinSchemaName = database.SchemaMap(join.Table.SchemaName);
string fromSchemaName = database.SchemaMap(fromTable.SchemaName);

if(!string.IsNullOrWhiteSpace(joinSchemaName)) {
SqlHelper.AppendEncloseSchemaName(sql, joinSchemaName);
if(!string.IsNullOrWhiteSpace(fromSchemaName)) {
SqlHelper.AppendEncloseSchemaName(sql, fromSchemaName);
sql.Append('.');
}
SqlHelper.AppendEncloseTableName(sql, join.Table);
sql.Append(" AS ").Append(join.Table.Alias).Append(" ON ");
join.Condition.GetSql(sql, database, parameters, useAlias: useAlias);
SqlHelper.AppendEncloseTableName(sql, fromTable);
sql.Append(" as ").Append(fromTable.Alias);
}
}

if(template.WhereCondition != null) {
sql.Append(" WHERE ");
template.WhereCondition.GetSql(sql, database, parameters, useAlias: useAlias);
Expand Down
32 changes: 16 additions & 16 deletions QueryLite/Databases/SqlServer/UpdateQueryGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ string IUpdateQueryGenerator.GetSql<RESULT>(UpdateQueryTemplate template, IDatab

sql.Append("UPDATE ");

bool useAlias = template.Joins != null;
bool useAlias = template.FromTables != null;

string schemaName = database.SchemaMap(template.Table.SchemaName);

Expand Down Expand Up @@ -81,38 +81,38 @@ string IUpdateQueryGenerator.GetSql<RESULT>(UpdateQueryTemplate template, IDatab
SqlServerReturningCollectorCache.Release(collector);
}

if(template.Joins != null) {
if(template.FromTables != null) {

sql.Append(" FROM ");

if(!string.IsNullOrWhiteSpace(schemaName)) {
SqlHelper.AppendEncloseSchemaName(sql, schemaName);
sql.Append('.');
}

SqlHelper.AppendEncloseTableName(sql, template.Table);
sql.Append(" as ").Append(template.Table.Alias);

for(int index = 0; index < template.FromTables.Count; index++) {

sql.Append(" AS ").Append(template.Table.Alias);
ITable fromTable = template.FromTables[index];

foreach(IJoin join in template.Joins) {
if(fromTable == template.Table) {
throw new Exception("The update table must not be included in the 'FROM' table list");
}

sql.Append(join.JoinType switch {
JoinType.Join => " JOIN ",
JoinType.LeftJoin => " LEFT JOIN ",
_ => throw new Exception($"Unknown join type. Type = {join.JoinType}")
});
sql.Append(',');

string joinSchemaName = database.SchemaMap(join.Table.SchemaName);
string fromSchemaName = database.SchemaMap(fromTable.SchemaName);

if(!string.IsNullOrWhiteSpace(joinSchemaName)) {
SqlHelper.AppendEncloseSchemaName(sql, joinSchemaName);
if(!string.IsNullOrWhiteSpace(fromSchemaName)) {
SqlHelper.AppendEncloseSchemaName(sql, fromSchemaName);
sql.Append('.');
}
SqlHelper.AppendEncloseTableName(sql, join.Table);
sql.Append(" AS ").Append(join.Table.Alias).Append(" ON ");
join.Condition.GetSql(sql, database, useAlias: useAlias, parameters);
SqlHelper.AppendEncloseTableName(sql, fromTable);
sql.Append(" as ").Append(fromTable.Alias);
}
}

if(template.WhereCondition != null) {
sql.Append(" WHERE ");
template.WhereCondition.GetSql(sql, database, useAlias: useAlias, parameters);
Expand Down
53 changes: 3 additions & 50 deletions QueryLite/Interfaces/PreparedUpdateInterfaces.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,59 +30,12 @@ namespace QueryLite {

public interface IPreparedUpdateSet<PARAMETERS> {

IPreparedUpdateJoin<PARAMETERS> Values(Action<IPreparedSetValuesCollector<PARAMETERS>> values);
IPreparedUpdateFrom<PARAMETERS> Values(Action<IPreparedSetValuesCollector<PARAMETERS>> values);
}

public interface IPreparedUpdateJoin<PARAMETERS> : IPreparedUpdateWhere<PARAMETERS> {
public interface IPreparedUpdateFrom<PARAMETERS> : IPreparedUpdateWhere<PARAMETERS> {

/// <summary>
/// Join table clause
/// </summary>
/// <param name="table"></param>
/// <returns></returns>
IPreparedUpdateJoinOn<PARAMETERS> Join(ITable table);

/// <summary>
/// Left join table clause
/// </summary>
/// <param name="table"></param>
/// <returns></returns>
IPreparedUpdateJoinOn<PARAMETERS> LeftJoin(ITable table);
}

public interface IPreparedUpdateJoinOn<PARAMETERS> {

// <summary>
/// Join condition
/// </summary>
/// <param name="on"></param>
/// <returns></returns>
IPreparedUpdateJoin<PARAMETERS> On(Func<APreparedCondition<PARAMETERS>, APreparedCondition<PARAMETERS>> on);
}

internal sealed class PreparedUpdateJoin<PARAMETERS> : IPreparedUpdateJoinOn<PARAMETERS> {

public JoinType JoinType { get; private set; }
public ITable Table { get; private set; }

public APreparedCondition<PARAMETERS>? _condition;

public APreparedCondition<PARAMETERS> Condition {
get { return _condition!; }
}

private readonly PreparedUpdateTemplate<PARAMETERS> Template;

internal PreparedUpdateJoin(JoinType joinType, ITable table, PreparedUpdateTemplate<PARAMETERS> template) {
JoinType = joinType;
Table = table;
Template = template;
}

public IPreparedUpdateJoin<PARAMETERS> On(Func<APreparedCondition<PARAMETERS>, APreparedCondition<PARAMETERS>> on) {
_condition = on(new EmptyPreparedCondition<PARAMETERS>());
return Template;
}
public IPreparedUpdateWhere<PARAMETERS> From(ITable table, params ITable[] tables);
}

public interface IPreparedUpdateWhere<PARAMETERS> : IPreparedUpdateSet<PARAMETERS> {
Expand Down
55 changes: 4 additions & 51 deletions QueryLite/Interfaces/UpdateInterfaces.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,62 +29,15 @@ namespace QueryLite {

public interface IUpdateSet {

IUpdateWhere Values(Action<ISetValuesCollector> values);
IUpdateFrom Values(Action<ISetValuesCollector> values);
}

public interface IUpdateJoin : IUpdateWhere {
public interface IUpdateFrom : IUpdateWhere {

/// <summary>
/// Join table clause
/// </summary>
/// <param name="table"></param>
/// <returns></returns>
IUpdateJoinOn Join(ITable table);

/// <summary>
/// Left join table clause
/// </summary>
/// <param name="table"></param>
/// <returns></returns>
IUpdateJoinOn LeftJoin(ITable table);
}

public interface IUpdateJoinOn {

// <summary>
/// Join condition
/// </summary>
/// <param name="on"></param>
/// <returns></returns>
IUpdateJoin On(ICondition on);
}

internal sealed class UpdateJoin : IJoin, IUpdateJoinOn {

public JoinType JoinType { get; private set; }
public ITable Table { get; private set; }

public ICondition? _condition;

public ICondition Condition {
get { return _condition!; }
}

private readonly UpdateQueryTemplate Template;

internal UpdateJoin(JoinType joinType, ITable table, UpdateQueryTemplate tempate) {
JoinType = joinType;
Table = table;
Template = tempate;
}

public IUpdateJoin On(ICondition on) {
_condition = on;
return Template;
}
public IUpdateWhere From(ITable table, params ITable[] tables);
}

public interface IUpdateWhere : IUpdateSet {
public interface IUpdateWhere {

/// <summary>
/// Where clause
Expand Down
Loading

0 comments on commit be994be

Please sign in to comment.