108 lines
5.0 KiB
C#
108 lines
5.0 KiB
C#
using DbTools;
|
|
using System.Data.SQLite;
|
|
using System.IO;
|
|
using System.Windows.Forms;
|
|
|
|
namespace DbToolsTester.Forms {
|
|
public partial class MainForm : DevExpress.XtraEditors.XtraForm {
|
|
public static MainForm Instance { get; private set; }
|
|
public string DatabaseName { get; set; }
|
|
public string Database1File { get; set; }
|
|
public string Database2File { get; set; }
|
|
|
|
public MainForm() {
|
|
InitializeComponent();
|
|
Instance = this;
|
|
}
|
|
|
|
private void XtraTabControl1_CustomHeaderButtonClick(object sender, DevExpress.XtraTab.ViewInfo.CustomHeaderButtonEventArgs e) {
|
|
string tag = e.Button.Tag?.ToString();
|
|
if (tag == "open") {
|
|
OpenForm frm = new OpenForm();
|
|
if (frm.ShowDialog(this) == DialogResult.OK) {
|
|
// Databases are set in OpenForm
|
|
SetDatabases(frm.Db1Path, frm.Db2Path);
|
|
}
|
|
} else if (tag == "clear") {
|
|
Database1File = null;
|
|
Database2File = null;
|
|
|
|
db1Sql.Clear();
|
|
db2Sql.Clear();
|
|
deltaSql.Clear();
|
|
|
|
Text = "DbSync Tester";
|
|
}
|
|
}
|
|
|
|
public void SetDatabases(string db1File, string db2File) {
|
|
Database1File = db1File;
|
|
Database2File = db2File;
|
|
DatabaseName = Path.GetFileNameWithoutExtension(Database1File);
|
|
Text += " [ Production DB = " + Path.GetFileName(Database1File) + "; Schema DB = " + Path.GetFileName(Database2File) + " ]";
|
|
}
|
|
|
|
private string getDatabaseFile(string title = "Select Database File...") {
|
|
OpenFileDialog dlg = new OpenFileDialog() {
|
|
Filter = "Database Files (*.sql;*.sqlite;*.db)|*.sql;*.sqlite;*.db|SQL Files (*.sql)|*.sql|Sqlite Files (*.sqlite;*.db)|*.sqlite;*.db|Text Files (*.txt)|*.txt|All Files (*.*)|*.*",
|
|
CheckFileExists = true,
|
|
Multiselect = false,
|
|
Title = title
|
|
};
|
|
if (dlg.ShowDialog() == DialogResult.OK) {
|
|
return dlg.FileName;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
private async void ToolbarItemClicked(object sender, DevExpress.XtraBars.ItemClickEventArgs e) {
|
|
if (e.Item.Equals(bbiDelta)) {
|
|
deltaSql.Clear();
|
|
|
|
SQLiteConnection db1 = new SQLiteConnection("Data Source=" + Database1File + ";Version=3;");
|
|
SQLiteConnection db2 = new SQLiteConnection("Data Source=" + Database2File + ";Version=3;");
|
|
|
|
SqlBuilder builder = new SqlBuilder();
|
|
string[] missingTables = builder.GetMissingTables(db1, db2);
|
|
foreach (string table in missingTables) {
|
|
string sql = await builder.GetTableCreateSqlAsync(db2, table);
|
|
deltaSql.AppendText(sql + "\r\n");
|
|
}
|
|
|
|
//Delta delta = new Delta();
|
|
//deltaSql.Text = delta.BuildDelta(db1, db2, btsRemoveUnusedTables.Checked, btsRemoveUnusedColumns.Checked);
|
|
//db1Sql.Text = delta.Db1Sql;
|
|
//db2Sql.Text = delta.Db2Sql;
|
|
} else if (e.Item.Equals(bbiApplyBoth)) {
|
|
if (string.IsNullOrEmpty(deltaSql.Text)) {
|
|
MessageBox.Show("No delta SQL to apply.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
return;
|
|
}
|
|
|
|
using (var db1 = new SQLiteConnection("Data Source=" + Database1File + ";Version=3;")) {
|
|
db1.Open();
|
|
Delta delta = new Delta();
|
|
bool success = delta.ApplyDelta(db1, deltaSql.Text);
|
|
if (success) {
|
|
MessageBox.Show("Delta applied successfully.", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
|
} else {
|
|
MessageBox.Show("Failed to apply delta." + (delta.LastError != null ? "\r\n\r\n" + delta.LastError.Message : ""), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
}
|
|
}
|
|
} else if (e.Item.Equals(bbiBuildTableCreate)) {
|
|
TableChooserForm frm = new TableChooserForm();
|
|
if (frm.ShowDialog(this) == DialogResult.OK) {
|
|
using (var cn = new SQLiteConnection("Data Source=" + (frm.SelectedDatabase == Path.GetFileName(Database1File) ? Database1File : Database2File) + ";Version=3;")) {
|
|
SqlBuilder builder = new SqlBuilder();
|
|
string sql = await builder.GetTableCreateSqlAsync(cn, frm.SelectedTable);
|
|
if (!string.IsNullOrEmpty(sql)) {
|
|
deltaSql.Text = sql;
|
|
} else {
|
|
MessageBox.Show("Failed to get CREATE TABLE SQL for table " + frm.SelectedTable, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
} |