Skip to content

Commit 5e03a59

Browse files
committed
[Table Editor] Added 'Generate insert query' to the context menu
1 parent d3f050e commit 5e03a59

File tree

11 files changed

+45
-14
lines changed

11 files changed

+45
-14
lines changed

AvaloniaStyles/Controls/FastTableView/ColorsDark.axaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,6 @@
1919
<Pen Thickness="1" Brush="#0FFFFFFF" x:Key="FastTableView.ButtonBackgroundPen" />
2020
<Pen Thickness="1" Brush="#15FFFFFF" x:Key="FastTableView.ButtonBackgroundHoverPen" />
2121
<Pen Thickness="1" Brush="#08FFFFFF" x:Key="FastTableView.ButtonBackgroundPressedPen" />
22+
<Pen Thickness="1" Brush="#10FFFFFF" x:Key="FastTableView.ButtonBackgroundDisabledPen" />
2223
</Styles.Resources>
2324
</Styles>

AvaloniaStyles/Controls/FastTableView/ColorsLight.axaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,6 @@
1919
<Pen Thickness="1" Brush="White" x:Key="FastTableView.ButtonBackgroundPen" />
2020
<Pen Thickness="1" Brush="#F5F5F5" x:Key="FastTableView.ButtonBackgroundHoverPen" />
2121
<Pen Thickness="1" Brush="#D7D7D7" x:Key="FastTableView.ButtonBackgroundPressedPen" />
22+
<Pen Thickness="1" Brush="#D2D2D2" x:Key="FastTableView.ButtonBackgroundDisabledPen" />
2223
</Styles.Resources>
2324
</Styles>

WDE.DatabaseEditors.Avalonia/Views/SingleRow/CustomCellDrawer.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,11 @@ public class CustomCellDrawer : CustomCellDrawerInteractorBase, ICustomCellDrawe
2727
private static IPen ButtonBackgroundPen = new Pen(new SolidColorBrush(Colors.White), 0);
2828
private static IPen ButtonBackgroundHoverPen = new Pen(new SolidColorBrush(Color.FromRgb(245, 245, 245)), 0);
2929
private static IPen ButtonBackgroundPressedPen = new Pen(new SolidColorBrush(Color.FromRgb(215, 215, 215)), 0);
30-
30+
private static IPen ButtonBackgroundDisabledPen = new Pen(new SolidColorBrush(Color.FromRgb(170, 170, 170)), 0);
31+
3132
private Point mouseCursor;
3233
private bool leftPressed;
3334

34-
35-
3635
static CustomCellDrawer()
3736
{
3837
ModifiedCellPen.GetResource("FastTableView.ModifiedCellPen", ModifiedCellPen, out ModifiedCellPen);
@@ -42,6 +41,7 @@ static CustomCellDrawer()
4241
ButtonBackgroundPen.GetResource("FastTableView.ButtonBackgroundPen", ButtonBackgroundPen, out ButtonBackgroundPen);
4342
ButtonBackgroundHoverPen.GetResource("FastTableView.ButtonBackgroundHoverPen", ButtonBackgroundHoverPen, out ButtonBackgroundHoverPen);
4443
ButtonBackgroundPressedPen.GetResource("FastTableView.ButtonBackgroundPressedPen", ButtonBackgroundPressedPen, out ButtonBackgroundPressedPen);
44+
ButtonBackgroundDisabledPen.GetResource("FastTableView.ButtonBackgroundDisabledPen", ButtonBackgroundDisabledPen, out ButtonBackgroundDisabledPen);
4545
}
4646

4747
public void DrawRow(DrawingContext context, ITableRow r, Rect rect)
@@ -65,13 +65,13 @@ public bool UpdateCursor(Point point, bool leftPressed)
6565
return true;
6666
}
6767

68-
private void DrawButton(DrawingContext context, Rect rect, string text, int margin)
68+
private void DrawButton(DrawingContext context, Rect rect, string text, int margin, bool enabled = true)
6969
{
7070
rect = rect.Deflate(margin);
7171

7272
bool isOver = rect.Contains(mouseCursor);
7373

74-
context.DrawRectangle((isOver ? (leftPressed ? ButtonBackgroundPressedPen : ButtonBackgroundHoverPen) : ButtonBackgroundPen).Brush, ButtonBorderPen, rect, 4, 4);
74+
context.DrawRectangle((!enabled ? ButtonBackgroundDisabledPen : isOver ? (leftPressed ? ButtonBackgroundPressedPen : ButtonBackgroundHoverPen) : ButtonBackgroundPen).Brush, ButtonBorderPen, rect, 4, 4);
7575

7676
var state = context.PushClip(rect);
7777
var ft = new FormattedText
@@ -92,7 +92,7 @@ public bool Draw(DrawingContext context, ref Rect rect, ITableCell c)
9292

9393
if (cell.ActionCommand != null)
9494
{
95-
DrawButton(context, rect, cell.ActionLabel, 3);
95+
DrawButton(context, rect, cell.ActionLabel, 3, cell.ActionCommand.CanExecute(null));
9696
return true;
9797
}
9898

WDE.DatabaseEditors.Avalonia/Views/SingleRow/CustomCellInteractor.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,10 @@ public bool SpawnEditorFor(string? initialText, Visual parent, Rect rect, ITable
3030
if (c is not SingleRecordDatabaseCellViewModel cell)
3131
return false;
3232

33-
if (cell.ActionCommand != null && cell.ActionCommand.CanExecute(null))
33+
if (cell.ActionCommand != null)
3434
{
35-
cell.ActionCommand.Execute(null);
35+
if (cell.ActionCommand.CanExecute(null))
36+
cell.ActionCommand.Execute(null);
3637
return true;
3738
}
3839

WDE.DatabaseEditors.Avalonia/Views/SingleRow/SingleRowDbTableEditorView.axaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,8 @@
180180
<MenuItem Header="-"></MenuItem>
181181
<MenuItem Header="Delete row" Command="{CompiledBinding DeleteRowSelectedCommand}" />
182182
<MenuItem Header="Duplicate row" Command="{CompiledBinding DuplicateSelectedCommand}"/>
183+
<MenuItem Header="-"></MenuItem>
184+
<MenuItem Header="Generate insert query" Command="{CompiledBinding GenerateInsertQueryForSelectedCommand}"/>
183185
</MenuFlyout>
184186
</fastTableView:VeryFastTableView.ContextFlyout>
185187
</fastTableView:VeryFastTableView>

WDE.DatabaseEditors/Loaders/DatabaseTableDataProvider.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,8 @@ public async Task<long> GetCount(string definitionId, string? customWhere, IEnum
120120
try
121121
{
122122
var result = await sqlExecutor.ExecuteSelectSql(sql);
123+
if (result.Count == 0)
124+
return 0;
123125
return Convert.ToInt64(result[0]["num"].Item2);
124126
}
125127
catch (IMySqlExecutor.CannotConnectToDatabaseException)

WDE.DatabaseEditors/QueryGenerators/IQueryGenerator.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ namespace WDE.DatabaseEditors.QueryGenerators
88
{
99
public interface IQueryGenerator
1010
{
11+
public IQuery GenerateInsertQuery(IReadOnlyList<DatabaseKey> keys, IDatabaseTableData tableData);
1112
public IQuery GenerateQuery(IReadOnlyList<DatabaseKey> keys, IReadOnlyList<DatabaseKey>? deletedKeys, IDatabaseTableData tableData);
1213
public IQuery GenerateUpdateFieldQuery(DatabaseTableDefinitionJson table, DatabaseEntity entity, IDatabaseField field);
1314
public IQuery GenerateDeleteQuery(DatabaseTableDefinitionJson table, DatabaseEntity entity);

WDE.DatabaseEditors/QueryGenerators/QueryGenerator.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,10 @@ private static void GeneratePrimaryKeyDeletion(DatabaseTableDefinitionJson defin
101101
{
102102
table = query.Table(foreign.TableName);
103103
if (keys.Count > 1 && keys[0].Count == 1)
104-
table.WhereIn(foreign.ForeignKeys[0], keys.Select(k => k[0])).Delete();
104+
{
105+
foreach (var chunk in keys.Select(k => k[0]).Chunk(128))
106+
table.WhereIn(foreign.ForeignKeys[0], chunk).Delete();
107+
}
105108
else
106109
{
107110
foreach (var key in keys)
@@ -112,7 +115,10 @@ private static void GeneratePrimaryKeyDeletion(DatabaseTableDefinitionJson defin
112115

113116
table = query.Table(definition.TableName);
114117
if (keys.Count > 1 && keys[0].Count == 1)
115-
table.WhereIn(definition.TablePrimaryKeyColumnName, keys.Select(k => k[0])).Delete();
118+
{
119+
foreach (var chunk in keys.Select(k => k[0]).Chunk(128))
120+
table.WhereIn(definition.TablePrimaryKeyColumnName, chunk).Delete();
121+
}
116122
else
117123
{
118124
foreach (var key in keys)
@@ -180,7 +186,7 @@ public int Compare(DatabaseEntity? x, DatabaseEntity? y)
180186
}
181187
}
182188

183-
private IQuery GenerateInsertQuery(IReadOnlyList<DatabaseKey> keys, IDatabaseTableData tableData)
189+
public IQuery GenerateInsertQuery(IReadOnlyList<DatabaseKey> keys, IDatabaseTableData tableData)
184190
{
185191
if (keys.Count == 0)
186192
return Queries.Empty();

WDE.DatabaseEditors/ViewModels/RowPickerViewModel.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,11 @@ public JustQuerySolutionItem(string query)
151151
{
152152
Query = query;
153153
}
154+
155+
public JustQuerySolutionItem(IQuery query)
156+
{
157+
Query = query.QueryString;
158+
}
154159
}
155160

156161
[AutoRegister]

WDE.DatabaseEditors/ViewModels/SingleRow/SingleRowDbTableEditorViewModel.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
using WDE.Common;
1818
using WDE.Common.Database;
1919
using WDE.Common.Disposables;
20+
using WDE.Common.Events;
2021
using WDE.Common.History;
2122
using WDE.Common.Managers;
2223
using WDE.Common.Parameters;
@@ -161,6 +162,7 @@ public bool SplitVertical
161162

162163
public AsyncAutoCommand DeleteRowSelectedCommand { get; }
163164
public DelegateCommand SetNullSelectedCommand { get; }
165+
public DelegateCommand GenerateInsertQueryForSelectedCommand { get; }
164166
public AsyncAutoCommand DuplicateSelectedCommand { get; }
165167
public AsyncAutoCommand RevertSelectedCommand { get; }
166168

@@ -257,7 +259,16 @@ public SingleRowDbTableEditorViewModel(DatabaseTableSolutionItem solutionItem,
257259
ForceInsertEntity(duplicate, FocusedRowIndex + 1);
258260
}, () => FocusedRow != null);
259261
AutoDispose(this.ToObservable(() => FocusedRow).Subscribe(_ => DuplicateSelectedCommand.RaiseCanExecuteChanged()));
260-
262+
263+
GenerateInsertQueryForSelectedCommand = new DelegateCommand(() =>
264+
{
265+
var selectedKeys = MultiSelection.All().Select(index => Entities[index].GenerateKey(TableDefinition)).ToList();
266+
var tableData = new DatabaseTableData(TableDefinition, Entities);
267+
var query = queryGenerator.GenerateInsertQuery(selectedKeys, tableData);
268+
var item = new MetaSolutionSQL(new JustQuerySolutionItem(query));
269+
eventAggregator.GetEvent<EventRequestOpenItem>().Publish(item);
270+
}, () => !MultiSelection.Empty).ObservesProperty(() => FocusedRow);
271+
261272
foreach (var key in solutionItem.Entries.Select(x => x.Key))
262273
keys.Add(key);
263274

0 commit comments

Comments
 (0)