Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ Easy to use desktop SQL widget
![alt text](https://github.com/MoDevby/SQL-Widget/blob/master/Sql%20Widget/ScreenShots/1.JPG)
![alt text](https://github.com/MoDevby/SQL-Widget/blob/master/Sql%20Widget/ScreenShots/2.JPG)
![alt text](https://github.com/MoDevby/SQL-Widget/blob/master/Sql%20Widget/ScreenShots/3.JPG)
![alt text](https://github.com/MoDevby/SQL-Widget/blob/master/Sql%20Widget/ScreenShots/4.JPG)
![alt text](https://github.com/MoDevby/SQL-Widget/blob/master/Sql%20Widget/ScreenShots/5.JPG)
![alt text](https://github.com/MoDevby/SQL-Widget/blob/master/Sql%20Widget/ScreenShots/6.JPG)

### This Project is using the following open source libraries:
* [MaterialDesignInXamlToolkit](https://github.com/MaterialDesignInXAML/MaterialDesignInXamlToolkit) under the MIT license.
Expand Down
18 changes: 0 additions & 18 deletions Sql Widget/Entities/ConditionElement.cs

This file was deleted.

4 changes: 0 additions & 4 deletions Sql Widget/Help.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@
* F5: Execute.
* Ctrl+Tab: Change the Tab.

## Select Tab:
* Ctrl+Right: Add selected fields to the select clause.
* Ctrl+Left: Remove selected fields from the select clause.

## Result Page:
* F5: Re-Execute.

Expand Down
19 changes: 0 additions & 19 deletions Sql Widget/Helper/Enums.cs

This file was deleted.

29 changes: 0 additions & 29 deletions Sql Widget/Models/OperatorsModel.cs

This file was deleted.

Binary file modified Sql Widget/ScreenShots/0.JPG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified Sql Widget/ScreenShots/1.JPG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified Sql Widget/ScreenShots/2.JPG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified Sql Widget/ScreenShots/3.JPG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Sql Widget/ScreenShots/4.JPG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Sql Widget/ScreenShots/5.JPG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Sql Widget/ScreenShots/6.JPG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
17 changes: 0 additions & 17 deletions Sql Widget/Sql Widget.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -86,21 +86,12 @@
<Compile Include="Views\Controls\HistoryControl.xaml.cs">
<DependentUpon>HistoryControl.xaml</DependentUpon>
</Compile>
<Compile Include="Views\Controls\SelectControl.xaml.cs">
<DependentUpon>SelectControl.xaml</DependentUpon>
</Compile>
<Compile Include="Views\Controls\ServerControl.xaml.cs">
<DependentUpon>ServerControl.xaml</DependentUpon>
</Compile>
<Compile Include="Views\Controls\WhereControl.xaml.cs">
<DependentUpon>WhereControl.xaml</DependentUpon>
</Compile>
<Compile Include="Helper\ButtonsCommand.cs" />
<Compile Include="Entities\ConditionElement.cs" />
<Compile Include="Helper\Converters.cs" />
<Compile Include="Helper\Enums.cs" />
<Compile Include="Entities\HistoryItem.cs" />
<Compile Include="Models\OperatorsModel.cs" />
<Compile Include="Entities\TableColumn.cs" />
<Compile Include="Models\DBModel.cs" />
<Compile Include="Models\QueryModel.cs" />
Expand All @@ -123,18 +114,10 @@
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="Views\Controls\SelectControl.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="Views\Controls\ServerControl.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="Views\Controls\WhereControl.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="Views\MainWindow.xaml">
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
Expand Down
181 changes: 15 additions & 166 deletions Sql Widget/ViewModels/MainWindowVM.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ class MainWindowVM : INotifyPropertyChanged
#pragma warning disable 0067
public event PropertyChangedEventHandler PropertyChanged;
#pragma warning restore 0067
private string _selectedDB;
private string _selectedTable;
private DBModel _dbModel = new DBModel();

#region Properties
Expand Down Expand Up @@ -47,43 +45,16 @@ public bool UseWinAuth
public string UserName { get; set; }
#endregion
#region DB
public string SelectedDB { get; set; }
public List<string> DBsList { get; set; } = new List<string>();
public List<string> TablesList { get; set; } = new List<string>();
public string SelectedDB
{
get { return _selectedDB; }
set
{
_selectedDB = value;
AsyncUpdateTables();
SelectedTable = null;
}
}
public string SelectedTable
{
get { return _selectedTable; }
set { _selectedTable = value; IntiateFields(); }
}
public bool SelectTablesIsEnabled => TablesList.Any();
public List<TableColumn> Fields { get; set; } = new List<TableColumn>();
public bool ComponentsEnabled => Fields.Count() > 1;
#endregion
#region Query
public string QueryContent { get; set; }
public string ExecutableQuery { get; set; }
public bool QueryEnabled => ValidConnection && !string.IsNullOrWhiteSpace(SelectedDB);
private bool IsValidExecutableQuery => !string.IsNullOrWhiteSpace(SelectedDB) && !string.IsNullOrWhiteSpace(ExecutableQuery);
#endregion
#region Select
public List<TableColumn> VisibleFields => Fields.Where(x => !SelectedFields.Contains(x)).ToList();
public List<TableColumn> SelectedFields { get; set; } = new List<TableColumn>();
private bool IsValidSelect => SelectedFields.Any();
#endregion
#region Where
public List<TableColumn> WhereFields { get { return Fields.Where(a => a.Name != "*").ToList(); } }
public List<ConditionElement> Conditions { get; set; } = new List<ConditionElement> { new ConditionElement() };
public Dictionary<CompareOperators, string> CompareOperators { get; set; }
public List<RelationOperators> RelationOperators { get; set; }
public bool CanExecute => ValidConnection && !string.IsNullOrWhiteSpace(SelectedDB) &&
(!string.IsNullOrWhiteSpace(QueryContent) || SelectedTab.Header.ToString() != "Query");
public bool IsValidExecutableQuery => ValidConnection &&
!string.IsNullOrWhiteSpace(SelectedDB) && !string.IsNullOrWhiteSpace(ExecutableQuery);
#endregion
#region History
public List<HistoryItem> HistoryItems { get; set; } = new List<HistoryItem>();
Expand All @@ -93,32 +64,21 @@ public string SelectedTable
#region Methods
public MainWindowVM()
{
CompareOperators = OperatorsModel.GetCompareOperators();
RelationOperators = OperatorsModel.GetReleationsOperators();

VerfiyServer();
}

private async void IntiateFields()
{
SelectedFields = new List<TableColumn>();
Conditions = new List<ConditionElement> { new ConditionElement() };
Fields = new List<TableColumn> { new TableColumn { Name = "*", Position = 0, Type = "*" } }
.Concat(await TableColumnsModel.GetTableColumns(SelectedDB, SelectedTable)).ToList();
}

private async void VerfiyServer()
{
ServerName = Properties.Settings.Default.Server;
UserName = Properties.Settings.Default.UserName;

ServerMessage = "Connecting....";

if (await _dbModel.CheckConnection())
{
ValidConnection = true;
ServerMessage = "Connected!";
AsyncLoadDBs();
IntiateFields();
}
else
{
Expand All @@ -129,6 +89,8 @@ private async void VerfiyServer()

private async void AsyncLoadDBs()
{
SelectedDB = null;
_dbModel.InvalidateCache();
try
{
DBsList = await _dbModel.GetAllDBs();
Expand All @@ -141,59 +103,6 @@ private async void AsyncLoadDBs()
}
}

private async void AsyncUpdateTables()
{
if (!string.IsNullOrWhiteSpace(_selectedDB))
try
{
TablesList = await TablesModel.GetAllTables(SelectedDB);
return;
}
catch (Exception)
{
ServerMessage = $"Error, Couldn't Load The Tables for {SelectedDB}!";
ValidConnection = false;
}
TablesList = new List<string>();
}

private string GetSelectQuery()
{
var Query = string.Empty;
if (IsValidSelect)
{
var fields = string.Join(", ", SelectedFields.Select(x => x.Name));
Query = $"SELECT {fields}{Environment.NewLine}FROM {SelectedTable}";

var whereConditions = Conditions.Where(x => x.SelectedField != "" && x.SelectedOerator != 0);
Query += ConstructWhereClause(whereConditions);
}
return Query;
}

private string ConstructWhereClause(IEnumerable<ConditionElement> whereConditions)
{
if (!whereConditions.Any()) return null;
var where = Environment.NewLine + "Where ";
var index = 0;
whereConditions.ToList().ForEach(condition =>
{
where += condition.SelectedField + CompareOperators.First(x => x.Key == condition.SelectedOerator).Value;
where += IsStringType(condition.SelectedField) && condition.Value != "null"
? string.IsNullOrEmpty(condition.Value) ? "" : $"'{condition.Value}'"
: string.IsNullOrWhiteSpace(condition.Value) ? "null" : condition.Value;
if (index < whereConditions.Count() - 1)
where += condition.NextLineOperator == 0 ? $" {RelationOperators.First()} " : $" {condition.NextLineOperator} ";
index++;
});
return where;
}
private bool IsStringType(string fieldName)
{
var type = Fields.FirstOrDefault(x => x.Name == fieldName).Type;
return type.Contains("char") || type.Contains("uniqueidentifier") || type.Contains("date");
}

private async void AddToHistory(string db, string query, ResultVM resultVm)
{
var historyItem = new HistoryItem
Expand Down Expand Up @@ -237,24 +146,13 @@ public ICommand SaveServerCommand
Properties.Settings.Default.UserName = UserName;
Properties.Settings.Default.Password = (obj as PasswordBox).Password;
Properties.Settings.Default.Save();
ResetCacheCommand.Execute("");
VerfiyServer();
});
}
}
#endregion
#region DB Bar
public ICommand ResetCacheCommand
{
get
{
return new ButtonsCommand((object obj) =>
{
SelectedDB = null;
_dbModel.InvalidateCache();
VerfiyServer();
});
}
}

public ICommand HelpCommand
{
get
Expand Down Expand Up @@ -285,53 +183,11 @@ public ICommand SelectDBCommand
{
return new ButtonsCommand((object obj) =>
{
var db = (string)obj;
SelectedDB = db;
});
}
}
#endregion
#region Select
public ICommand AddFieldCommand
{
get
{
return new ButtonsCommand((object obj) =>
{
var list = (ListBox)obj;
foreach (TableColumn item in list.SelectedItems)
{
SelectedFields.Add(item);
}
SelectedFields = new List<TableColumn>(SelectedFields);
});
}
}
public ICommand RemoveFieldCommand
{
get
{
return new ButtonsCommand((object obj) =>
{
var list = (ListBox)obj;
foreach (TableColumn item in list.SelectedItems)
{
SelectedFields.Remove(item);
}
SelectedFields = new List<TableColumn>(SelectedFields);
});
}
}
#endregion
#region Where
public ICommand AddConditionCommand
{
get
{
return new ButtonsCommand((object obj) =>
{
Conditions.Add(new ConditionElement());
Conditions = new List<ConditionElement>(Conditions);
var comboBox = (ComboBox)obj;

if (!DBsList.Contains(comboBox.Text))
comboBox.Text = "";
SelectedDB = comboBox.Text;
});
}
}
Expand All @@ -348,9 +204,6 @@ public ICommand ExecuteCommand
case "Query":
ExecutableQuery = QueryContent;
break;
case "SELECT":
ExecutableQuery = GetSelectQuery();
break;
case "History":
ExecutableQuery = string.Join(";", HistoryItems.Where(x => x.Selected).Select(x => x.Query));
break;
Expand Down Expand Up @@ -381,10 +234,6 @@ public ICommand ClearCommand
case "Query":
QueryContent = string.Empty;
break;
case "SELECT":
SelectedFields = new List<TableColumn>();
Conditions = new List<ConditionElement> { new ConditionElement() };
break;
case "History":
HistoryItems.ForEach(x => x.Selected = false);
HistoryItems = new List<HistoryItem>(HistoryItems);
Expand Down
Loading