100 lines
4.1 KiB
C#
100 lines
4.1 KiB
C#
using DbMigrate;
|
|
using System;
|
|
using System.ComponentModel;
|
|
using System.IO;
|
|
using System.Text.RegularExpressions;
|
|
using System.Threading.Tasks;
|
|
using System.Windows.Forms;
|
|
|
|
namespace DbMigrateTester.Forms.UserControls {
|
|
public partial class DatabasePanel : DevExpress.XtraEditors.XtraUserControl {
|
|
|
|
public string DbFilename {
|
|
get { return txtDbFilename.Text; }
|
|
set {
|
|
txtDbFilename.Text = value;
|
|
if (string.IsNullOrEmpty(value)) {
|
|
enableDbFilename();
|
|
} else {
|
|
disableDbFilename();
|
|
}
|
|
}
|
|
}
|
|
|
|
public string Sql => rtfSql.Text;
|
|
|
|
public SqlDatabase SqlDb { get; private set; }
|
|
|
|
[Browsable(true)]
|
|
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
|
|
public string FileLabel {
|
|
get { return layoutControlItem1.Text; }
|
|
set { layoutControlItem1.Text = value; }
|
|
}
|
|
|
|
public DatabasePanel() {
|
|
InitializeComponent();
|
|
}
|
|
|
|
private async void DbFilename_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e) {
|
|
try {
|
|
if (e.Button.Kind == DevExpress.XtraEditors.Controls.ButtonPredefines.Ellipsis) {
|
|
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 = "Select Database File"
|
|
};
|
|
if (dlg.ShowDialog() == DialogResult.OK) {
|
|
await LoadDatabase(dlg.FileName);
|
|
}
|
|
} else if (e.Button.Kind == DevExpress.XtraEditors.Controls.ButtonPredefines.Clear) {
|
|
txtDbFilename.Text = "";
|
|
rtfSql.Text = "";
|
|
treeList1.DataSource = null;
|
|
enableDbFilename();
|
|
}
|
|
} catch (Exception ex) {
|
|
Console.WriteLine(ex.ToString());
|
|
}
|
|
}
|
|
|
|
private void enableDbFilename(bool enabled = true) {
|
|
txtDbFilename.ReadOnly = !enabled;
|
|
txtDbFilename.Properties.Buttons[0].Visible = enabled;
|
|
txtDbFilename.Properties.Buttons[1].Visible = !enabled;
|
|
}
|
|
|
|
private void disableDbFilename() {
|
|
enableDbFilename(false);
|
|
}
|
|
|
|
public async Task LoadDatabase(string filename) {
|
|
DbFilename = filename;
|
|
disableDbFilename();
|
|
|
|
SqlDatabase db = new SqlDatabase();
|
|
if (Path.GetExtension(filename).ToLower() == ".sql" || Path.GetExtension(filename).ToLower() == ".txt") {
|
|
// Load data from SQL file
|
|
string sql = File.ReadAllText(filename);
|
|
MatchCollection mc = Regex.Matches(sql, "^(CREATE TABLE( IF NOT EXISTS)? (\\w*).*\\);)", RegexOptions.Multiline | RegexOptions.Singleline);
|
|
db.LoadSql(sql);
|
|
rtfSql.Text = db.SqlScript;
|
|
} else if (Path.GetExtension(filename).ToLower() == ".sqlite" || Path.GetExtension(filename).ToLower() == ".db") {
|
|
// Load data from SQLite database file
|
|
string sql = await db.BuildSql("Data Source=" + filename + "; Version=3;");
|
|
rtfSql.Text = sql;
|
|
} else {
|
|
MessageBox.Show("Unsupported database file type: " + Path.GetExtension(filename), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
}
|
|
|
|
if (db.Tables != null && db.Tables.Count > 0) {
|
|
treeList1.Columns.Clear();
|
|
treeList1.Columns.Add(new DevExpress.XtraTreeList.Columns.TreeListColumn() { Caption = "Table Name", Visible = true, FieldName = "TableName" });
|
|
treeList1.DataSource = db.Tables;
|
|
}
|
|
SqlDb = db;
|
|
}
|
|
}
|
|
}
|