Added tablechooser and SqlBuilder class
This commit is contained in:
@@ -55,6 +55,7 @@
|
||||
<Compile Include="Model\Table.cs" />
|
||||
<Compile Include="Model\TableCollection.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
<Compile Include="SqlBuilder.cs" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
||||
</Project>
|
||||
@@ -43,6 +43,7 @@
|
||||
<Reference Include="DevExpress.XtraBars.v23.2, Version=23.2.3.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a, processorArchitecture=MSIL" />
|
||||
<Reference Include="DevExpress.XtraEditors.v23.2, Version=23.2.3.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a, processorArchitecture=MSIL" />
|
||||
<Reference Include="DevExpress.XtraLayout.v23.2, Version=23.2.3.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a, processorArchitecture=MSIL" />
|
||||
<Reference Include="DevExpress.XtraTreeList.v23.2, Version=23.2.3.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a, processorArchitecture=MSIL" />
|
||||
<Reference Include="System" />
|
||||
<Reference Include="System.Core" />
|
||||
<Reference Include="System.Data.SQLite, Version=1.0.119.0, Culture=neutral, PublicKeyToken=db937bc2d44ff139, processorArchitecture=MSIL">
|
||||
@@ -71,6 +72,12 @@
|
||||
<Compile Include="Forms\OpenForm.Designer.cs">
|
||||
<DependentUpon>OpenForm.cs</DependentUpon>
|
||||
</Compile>
|
||||
<Compile Include="Forms\TableChooserForm.cs">
|
||||
<SubType>Form</SubType>
|
||||
</Compile>
|
||||
<Compile Include="Forms\TableChooserForm.Designer.cs">
|
||||
<DependentUpon>TableChooserForm.cs</DependentUpon>
|
||||
</Compile>
|
||||
<Compile Include="Program.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
<EmbeddedResource Include="Forms\MainForm.resx">
|
||||
@@ -79,6 +86,9 @@
|
||||
<EmbeddedResource Include="Forms\OpenForm.resx">
|
||||
<DependentUpon>OpenForm.cs</DependentUpon>
|
||||
</EmbeddedResource>
|
||||
<EmbeddedResource Include="Forms\TableChooserForm.resx">
|
||||
<DependentUpon>TableChooserForm.cs</DependentUpon>
|
||||
</EmbeddedResource>
|
||||
<EmbeddedResource Include="Properties\licenses.licx" />
|
||||
<EmbeddedResource Include="Properties\Resources.resx">
|
||||
<Generator>ResXFileCodeGenerator</Generator>
|
||||
|
||||
85
DbToolsTester/Forms/MainForm.Designer.cs
generated
85
DbToolsTester/Forms/MainForm.Designer.cs
generated
@@ -36,6 +36,9 @@
|
||||
this.bbiApplyBoth = new DevExpress.XtraBars.BarButtonItem();
|
||||
this.bar2 = new DevExpress.XtraBars.Bar();
|
||||
this.bar3 = new DevExpress.XtraBars.Bar();
|
||||
this.bar4 = new DevExpress.XtraBars.Bar();
|
||||
this.btsRemoveUnusedColumns = new DevExpress.XtraBars.BarToggleSwitchItem();
|
||||
this.btsRemoveUnusedTables = new DevExpress.XtraBars.BarToggleSwitchItem();
|
||||
this.barDockControlTop = new DevExpress.XtraBars.BarDockControl();
|
||||
this.barDockControlBottom = new DevExpress.XtraBars.BarDockControl();
|
||||
this.barDockControlLeft = new DevExpress.XtraBars.BarDockControl();
|
||||
@@ -48,8 +51,7 @@
|
||||
this.xtraTabPage3 = new DevExpress.XtraTab.XtraTabPage();
|
||||
this.deltaSql = new System.Windows.Forms.RichTextBox();
|
||||
this.xtraTabPage4 = new DevExpress.XtraTab.XtraTabPage();
|
||||
this.btsRemoveUnusedTables = new DevExpress.XtraBars.BarToggleSwitchItem();
|
||||
this.btsRemoveUnusedColumns = new DevExpress.XtraBars.BarToggleSwitchItem();
|
||||
this.bbiBuildTableCreate = new DevExpress.XtraBars.BarButtonItem();
|
||||
((System.ComponentModel.ISupportInitialize)(this.barManager1)).BeginInit();
|
||||
((System.ComponentModel.ISupportInitialize)(this.XtraTabControl1)).BeginInit();
|
||||
this.XtraTabControl1.SuspendLayout();
|
||||
@@ -63,7 +65,8 @@
|
||||
this.barManager1.Bars.AddRange(new DevExpress.XtraBars.Bar[] {
|
||||
this.bar1,
|
||||
this.bar2,
|
||||
this.bar3});
|
||||
this.bar3,
|
||||
this.bar4});
|
||||
this.barManager1.DockControls.Add(this.barDockControlTop);
|
||||
this.barManager1.DockControls.Add(this.barDockControlBottom);
|
||||
this.barManager1.DockControls.Add(this.barDockControlLeft);
|
||||
@@ -74,9 +77,10 @@
|
||||
this.bbiMigrations,
|
||||
this.bbiApplyBoth,
|
||||
this.btsRemoveUnusedTables,
|
||||
this.btsRemoveUnusedColumns});
|
||||
this.btsRemoveUnusedColumns,
|
||||
this.bbiBuildTableCreate});
|
||||
this.barManager1.MainMenu = this.bar2;
|
||||
this.barManager1.MaxItemId = 6;
|
||||
this.barManager1.MaxItemId = 7;
|
||||
this.barManager1.StatusBar = this.bar3;
|
||||
//
|
||||
// bar1
|
||||
@@ -89,8 +93,7 @@
|
||||
new DevExpress.XtraBars.LinkPersistInfo(this.bbiDelta),
|
||||
new DevExpress.XtraBars.LinkPersistInfo(this.bbiMigrations),
|
||||
new DevExpress.XtraBars.LinkPersistInfo(this.bbiApplyBoth),
|
||||
new DevExpress.XtraBars.LinkPersistInfo(this.btsRemoveUnusedTables, true),
|
||||
new DevExpress.XtraBars.LinkPersistInfo(this.btsRemoveUnusedColumns)});
|
||||
new DevExpress.XtraBars.LinkPersistInfo(this.bbiBuildTableCreate)});
|
||||
this.bar1.Text = "Tools";
|
||||
//
|
||||
// bbiDelta
|
||||
@@ -136,13 +139,36 @@
|
||||
this.bar3.OptionsBar.UseWholeRow = true;
|
||||
this.bar3.Text = "Status bar";
|
||||
//
|
||||
// bar4
|
||||
//
|
||||
this.bar4.BarName = "Custom 5";
|
||||
this.bar4.DockCol = 0;
|
||||
this.bar4.DockRow = 2;
|
||||
this.bar4.DockStyle = DevExpress.XtraBars.BarDockStyle.Top;
|
||||
this.bar4.LinksPersistInfo.AddRange(new DevExpress.XtraBars.LinkPersistInfo[] {
|
||||
new DevExpress.XtraBars.LinkPersistInfo(this.btsRemoveUnusedColumns),
|
||||
new DevExpress.XtraBars.LinkPersistInfo(this.btsRemoveUnusedTables)});
|
||||
this.bar4.Text = "Custom 5";
|
||||
//
|
||||
// btsRemoveUnusedColumns
|
||||
//
|
||||
this.btsRemoveUnusedColumns.Caption = "Remove Unused Columns";
|
||||
this.btsRemoveUnusedColumns.Id = 5;
|
||||
this.btsRemoveUnusedColumns.Name = "btsRemoveUnusedColumns";
|
||||
//
|
||||
// btsRemoveUnusedTables
|
||||
//
|
||||
this.btsRemoveUnusedTables.Caption = "Remove Unused Tables";
|
||||
this.btsRemoveUnusedTables.Id = 4;
|
||||
this.btsRemoveUnusedTables.Name = "btsRemoveUnusedTables";
|
||||
//
|
||||
// barDockControlTop
|
||||
//
|
||||
this.barDockControlTop.CausesValidation = false;
|
||||
this.barDockControlTop.Dock = System.Windows.Forms.DockStyle.Top;
|
||||
this.barDockControlTop.Location = new System.Drawing.Point(0, 0);
|
||||
this.barDockControlTop.Manager = this.barManager1;
|
||||
this.barDockControlTop.Size = new System.Drawing.Size(840, 41);
|
||||
this.barDockControlTop.Size = new System.Drawing.Size(840, 62);
|
||||
//
|
||||
// barDockControlBottom
|
||||
//
|
||||
@@ -156,17 +182,17 @@
|
||||
//
|
||||
this.barDockControlLeft.CausesValidation = false;
|
||||
this.barDockControlLeft.Dock = System.Windows.Forms.DockStyle.Left;
|
||||
this.barDockControlLeft.Location = new System.Drawing.Point(0, 41);
|
||||
this.barDockControlLeft.Location = new System.Drawing.Point(0, 62);
|
||||
this.barDockControlLeft.Manager = this.barManager1;
|
||||
this.barDockControlLeft.Size = new System.Drawing.Size(0, 417);
|
||||
this.barDockControlLeft.Size = new System.Drawing.Size(0, 396);
|
||||
//
|
||||
// barDockControlRight
|
||||
//
|
||||
this.barDockControlRight.CausesValidation = false;
|
||||
this.barDockControlRight.Dock = System.Windows.Forms.DockStyle.Right;
|
||||
this.barDockControlRight.Location = new System.Drawing.Point(840, 41);
|
||||
this.barDockControlRight.Location = new System.Drawing.Point(840, 62);
|
||||
this.barDockControlRight.Manager = this.barManager1;
|
||||
this.barDockControlRight.Size = new System.Drawing.Size(0, 417);
|
||||
this.barDockControlRight.Size = new System.Drawing.Size(0, 396);
|
||||
//
|
||||
// XtraTabControl1
|
||||
//
|
||||
@@ -176,10 +202,10 @@
|
||||
new DevExpress.XtraTab.Buttons.CustomHeaderButton(DevExpress.XtraEditors.Controls.ButtonPredefines.Glyph, "Open Databases", -1, true, true, editorButtonImageOptions1, serializableAppearanceObject1, "", "open", null),
|
||||
new DevExpress.XtraTab.Buttons.CustomHeaderButton(DevExpress.XtraEditors.Controls.ButtonPredefines.Glyph, "Clear All", -1, true, true, editorButtonImageOptions2, serializableAppearanceObject2, "", "clear", null)});
|
||||
this.XtraTabControl1.Dock = System.Windows.Forms.DockStyle.Fill;
|
||||
this.XtraTabControl1.Location = new System.Drawing.Point(0, 41);
|
||||
this.XtraTabControl1.Location = new System.Drawing.Point(0, 62);
|
||||
this.XtraTabControl1.Name = "XtraTabControl1";
|
||||
this.XtraTabControl1.SelectedTabPage = this.xtraTabPage1;
|
||||
this.XtraTabControl1.Size = new System.Drawing.Size(840, 417);
|
||||
this.XtraTabControl1.Size = new System.Drawing.Size(840, 396);
|
||||
this.XtraTabControl1.TabIndex = 4;
|
||||
this.XtraTabControl1.TabPages.AddRange(new DevExpress.XtraTab.XtraTabPage[] {
|
||||
this.xtraTabPage1,
|
||||
@@ -192,7 +218,7 @@
|
||||
//
|
||||
this.xtraTabPage1.Controls.Add(this.db1Sql);
|
||||
this.xtraTabPage1.Name = "xtraTabPage1";
|
||||
this.xtraTabPage1.Size = new System.Drawing.Size(838, 375);
|
||||
this.xtraTabPage1.Size = new System.Drawing.Size(838, 354);
|
||||
this.xtraTabPage1.Text = "Live / Production DB";
|
||||
//
|
||||
// db1Sql
|
||||
@@ -202,7 +228,7 @@
|
||||
this.db1Sql.Location = new System.Drawing.Point(0, 0);
|
||||
this.db1Sql.Name = "db1Sql";
|
||||
this.db1Sql.ReadOnly = true;
|
||||
this.db1Sql.Size = new System.Drawing.Size(838, 375);
|
||||
this.db1Sql.Size = new System.Drawing.Size(838, 354);
|
||||
this.db1Sql.TabIndex = 0;
|
||||
this.db1Sql.Text = "This database is the one that is currently in use by the application. This databa" +
|
||||
"se needs to be updated to fit the most recent schema.";
|
||||
@@ -211,7 +237,7 @@
|
||||
//
|
||||
this.xtraTabPage2.Controls.Add(this.db2Sql);
|
||||
this.xtraTabPage2.Name = "xtraTabPage2";
|
||||
this.xtraTabPage2.Size = new System.Drawing.Size(838, 375);
|
||||
this.xtraTabPage2.Size = new System.Drawing.Size(838, 354);
|
||||
this.xtraTabPage2.Text = "Schema / Updated DB";
|
||||
//
|
||||
// db2Sql
|
||||
@@ -221,7 +247,7 @@
|
||||
this.db2Sql.Location = new System.Drawing.Point(0, 0);
|
||||
this.db2Sql.Name = "db2Sql";
|
||||
this.db2Sql.ReadOnly = true;
|
||||
this.db2Sql.Size = new System.Drawing.Size(838, 375);
|
||||
this.db2Sql.Size = new System.Drawing.Size(838, 354);
|
||||
this.db2Sql.TabIndex = 1;
|
||||
this.db2Sql.Text = resources.GetString("db2Sql.Text");
|
||||
//
|
||||
@@ -229,7 +255,7 @@
|
||||
//
|
||||
this.xtraTabPage3.Controls.Add(this.deltaSql);
|
||||
this.xtraTabPage3.Name = "xtraTabPage3";
|
||||
this.xtraTabPage3.Size = new System.Drawing.Size(838, 375);
|
||||
this.xtraTabPage3.Size = new System.Drawing.Size(838, 354);
|
||||
this.xtraTabPage3.Text = "Delta SQL";
|
||||
//
|
||||
// deltaSql
|
||||
@@ -239,7 +265,7 @@
|
||||
this.deltaSql.Location = new System.Drawing.Point(0, 0);
|
||||
this.deltaSql.Name = "deltaSql";
|
||||
this.deltaSql.ReadOnly = true;
|
||||
this.deltaSql.Size = new System.Drawing.Size(838, 375);
|
||||
this.deltaSql.Size = new System.Drawing.Size(838, 354);
|
||||
this.deltaSql.TabIndex = 1;
|
||||
this.deltaSql.Text = "This SQL script contains all of the SQL commands required to make the production " +
|
||||
"(existing) database the same as the new template database.";
|
||||
@@ -247,20 +273,15 @@
|
||||
// xtraTabPage4
|
||||
//
|
||||
this.xtraTabPage4.Name = "xtraTabPage4";
|
||||
this.xtraTabPage4.Size = new System.Drawing.Size(838, 375);
|
||||
this.xtraTabPage4.Size = new System.Drawing.Size(838, 354);
|
||||
this.xtraTabPage4.Text = "Migrations";
|
||||
//
|
||||
// btsRemoveUnusedTables
|
||||
// bbiBuildTableCreate
|
||||
//
|
||||
this.btsRemoveUnusedTables.Caption = "Remove Unused Tables";
|
||||
this.btsRemoveUnusedTables.Id = 4;
|
||||
this.btsRemoveUnusedTables.Name = "btsRemoveUnusedTables";
|
||||
//
|
||||
// btsRemoveUnusedColumns
|
||||
//
|
||||
this.btsRemoveUnusedColumns.Caption = "Remove Unused Columns";
|
||||
this.btsRemoveUnusedColumns.Id = 5;
|
||||
this.btsRemoveUnusedColumns.Name = "btsRemoveUnusedColumns";
|
||||
this.bbiBuildTableCreate.Caption = "Build Table Create Script";
|
||||
this.bbiBuildTableCreate.Id = 6;
|
||||
this.bbiBuildTableCreate.Name = "bbiBuildTableCreate";
|
||||
this.bbiBuildTableCreate.ItemClick += new DevExpress.XtraBars.ItemClickEventHandler(this.ToolbarItemClicked);
|
||||
//
|
||||
// MainForm
|
||||
//
|
||||
@@ -309,5 +330,7 @@
|
||||
private System.Windows.Forms.RichTextBox deltaSql;
|
||||
private DevExpress.XtraBars.BarToggleSwitchItem btsRemoveUnusedTables;
|
||||
private DevExpress.XtraBars.BarToggleSwitchItem btsRemoveUnusedColumns;
|
||||
private DevExpress.XtraBars.Bar bar4;
|
||||
private DevExpress.XtraBars.BarButtonItem bbiBuildTableCreate;
|
||||
}
|
||||
}
|
||||
@@ -80,6 +80,19 @@ namespace DbToolsTester.Forms {
|
||||
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 = builder.GetTableCreateSql(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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,7 +11,7 @@ namespace DbToolsTester.Forms {
|
||||
}
|
||||
|
||||
private void btnOpen_Click(object sender, EventArgs e) {
|
||||
MainForm.Instance.SetDatabases(Db1File.Text, db2File.Text);
|
||||
//MainForm.Instance.SetDatabases(Db1File.Text, db2File.Text);
|
||||
}
|
||||
|
||||
private void Db1File_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e) {
|
||||
|
||||
93
DbToolsTester/Forms/TableChooserForm.Designer.cs
generated
Normal file
93
DbToolsTester/Forms/TableChooserForm.Designer.cs
generated
Normal file
@@ -0,0 +1,93 @@
|
||||
namespace DbToolsTester.Forms {
|
||||
partial class TableChooserForm {
|
||||
/// <summary>
|
||||
/// Required designer variable.
|
||||
/// </summary>
|
||||
private System.ComponentModel.IContainer components = null;
|
||||
|
||||
/// <summary>
|
||||
/// Clean up any resources being used.
|
||||
/// </summary>
|
||||
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
|
||||
protected override void Dispose(bool disposing) {
|
||||
if (disposing && (components != null)) {
|
||||
components.Dispose();
|
||||
}
|
||||
base.Dispose(disposing);
|
||||
}
|
||||
|
||||
#region Windows Form Designer generated code
|
||||
|
||||
/// <summary>
|
||||
/// Required method for Designer support - do not modify
|
||||
/// the contents of this method with the code editor.
|
||||
/// </summary>
|
||||
private void InitializeComponent() {
|
||||
this.treeList1 = new DevExpress.XtraTreeList.TreeList();
|
||||
this.btnOK = new DevExpress.XtraEditors.SimpleButton();
|
||||
this.btnCancel = new DevExpress.XtraEditors.SimpleButton();
|
||||
((System.ComponentModel.ISupportInitialize)(this.treeList1)).BeginInit();
|
||||
this.SuspendLayout();
|
||||
//
|
||||
// treeList1
|
||||
//
|
||||
this.treeList1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
|
||||
| System.Windows.Forms.AnchorStyles.Left)));
|
||||
this.treeList1.Location = new System.Drawing.Point(0, 0);
|
||||
this.treeList1.Name = "treeList1";
|
||||
this.treeList1.OptionsBehavior.Editable = false;
|
||||
this.treeList1.OptionsBehavior.ReadOnly = true;
|
||||
this.treeList1.OptionsView.ShowColumns = false;
|
||||
this.treeList1.OptionsView.ShowHorzLines = false;
|
||||
this.treeList1.OptionsView.ShowIndicator = false;
|
||||
this.treeList1.OptionsView.ShowVertLines = false;
|
||||
this.treeList1.Size = new System.Drawing.Size(456, 303);
|
||||
this.treeList1.TabIndex = 0;
|
||||
this.treeList1.FocusedNodeChanged += new DevExpress.XtraTreeList.FocusedNodeChangedEventHandler(this.treeList1_FocusedNodeChanged);
|
||||
//
|
||||
// btnOK
|
||||
//
|
||||
this.btnOK.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
|
||||
this.btnOK.DialogResult = System.Windows.Forms.DialogResult.OK;
|
||||
this.btnOK.Location = new System.Drawing.Point(222, 309);
|
||||
this.btnOK.Name = "btnOK";
|
||||
this.btnOK.Size = new System.Drawing.Size(108, 36);
|
||||
this.btnOK.TabIndex = 1;
|
||||
this.btnOK.Text = "&OK";
|
||||
//
|
||||
// btnCancel
|
||||
//
|
||||
this.btnCancel.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
|
||||
this.btnCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
|
||||
this.btnCancel.Location = new System.Drawing.Point(336, 309);
|
||||
this.btnCancel.Name = "btnCancel";
|
||||
this.btnCancel.Size = new System.Drawing.Size(108, 36);
|
||||
this.btnCancel.TabIndex = 2;
|
||||
this.btnCancel.Text = "&Cancel";
|
||||
//
|
||||
// TableChooserForm
|
||||
//
|
||||
this.AcceptButton = this.btnOK;
|
||||
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
|
||||
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
|
||||
this.CancelButton = this.btnCancel;
|
||||
this.ClientSize = new System.Drawing.Size(456, 357);
|
||||
this.Controls.Add(this.btnCancel);
|
||||
this.Controls.Add(this.btnOK);
|
||||
this.Controls.Add(this.treeList1);
|
||||
this.Name = "TableChooserForm";
|
||||
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
|
||||
this.Text = "Choose Table ...";
|
||||
this.Load += new System.EventHandler(this.TableChooserForm_Load);
|
||||
((System.ComponentModel.ISupportInitialize)(this.treeList1)).EndInit();
|
||||
this.ResumeLayout(false);
|
||||
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
private DevExpress.XtraTreeList.TreeList treeList1;
|
||||
private DevExpress.XtraEditors.SimpleButton btnOK;
|
||||
private DevExpress.XtraEditors.SimpleButton btnCancel;
|
||||
}
|
||||
}
|
||||
71
DbToolsTester/Forms/TableChooserForm.cs
Normal file
71
DbToolsTester/Forms/TableChooserForm.cs
Normal file
@@ -0,0 +1,71 @@
|
||||
using DbTools;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.Data.SQLite;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
|
||||
namespace DbToolsTester.Forms {
|
||||
public partial class TableChooserForm : DevExpress.XtraEditors.XtraForm {
|
||||
List<TreeItem> TreeItems = new List<TreeItem>();
|
||||
|
||||
public string SelectedTable { get; private set; }
|
||||
public string SelectedDatabase { get; private set; }
|
||||
|
||||
public TableChooserForm() {
|
||||
InitializeComponent();
|
||||
}
|
||||
|
||||
private int AddTreeItem(string text, int parentId = 0) {
|
||||
TreeItems.Add(new TreeItem(text, TreeItems.Count + 1, parentId));
|
||||
return TreeItems.Count;
|
||||
}
|
||||
|
||||
private void TableChooserForm_Load(object sender, EventArgs e) {
|
||||
SqlBuilder builder = new SqlBuilder();
|
||||
int db1 = AddTreeItem(Path.GetFileName(MainForm.Instance.Database1File));
|
||||
int db2 = AddTreeItem(Path.GetFileName(MainForm.Instance.Database2File));
|
||||
|
||||
using (SQLiteConnection cn = new SQLiteConnection("Data Source=" + MainForm.Instance.Database1File + "; Version=3;")) {
|
||||
string[] tables = builder.GetTableNames(cn).OrderBy(f => f).ToArray();
|
||||
foreach (string table in tables) {
|
||||
AddTreeItem(table, db1);
|
||||
}
|
||||
}
|
||||
|
||||
using (SQLiteConnection cn = new SQLiteConnection("Data Source=" + MainForm.Instance.Database2File + "; Version=3;")) {
|
||||
string[] tables = builder.GetTableNames(cn).OrderBy(f => f).ToArray();
|
||||
foreach (string table in tables) {
|
||||
AddTreeItem(table, db2);
|
||||
}
|
||||
}
|
||||
|
||||
treeList1.DataSource = TreeItems;
|
||||
treeList1.ExpandAll();
|
||||
}
|
||||
|
||||
private void treeList1_FocusedNodeChanged(object sender, DevExpress.XtraTreeList.FocusedNodeChangedEventArgs e) {
|
||||
if (e.Node == null || e.Node.ParentNode == null) {
|
||||
SelectedTable = null;
|
||||
SelectedDatabase = null;
|
||||
return;
|
||||
}
|
||||
SelectedDatabase = e.Node.ParentNode.GetValue(0).ToString();
|
||||
SelectedTable = e.Node.GetValue(0).ToString();
|
||||
}
|
||||
}
|
||||
|
||||
internal class TreeItem {
|
||||
public int ID { get; set; }
|
||||
public int ParentID { get; set; }
|
||||
public string Text { get; set; }
|
||||
|
||||
public TreeItem() { }
|
||||
public TreeItem(string text, int id, int parentId = 0) {
|
||||
ID = id;
|
||||
ParentID = parentId;
|
||||
Text = text;
|
||||
}
|
||||
}
|
||||
}
|
||||
120
DbToolsTester/Forms/TableChooserForm.resx
Normal file
120
DbToolsTester/Forms/TableChooserForm.resx
Normal file
@@ -0,0 +1,120 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<root>
|
||||
<!--
|
||||
Microsoft ResX Schema
|
||||
|
||||
Version 2.0
|
||||
|
||||
The primary goals of this format is to allow a simple XML format
|
||||
that is mostly human readable. The generation and parsing of the
|
||||
various data types are done through the TypeConverter classes
|
||||
associated with the data types.
|
||||
|
||||
Example:
|
||||
|
||||
... ado.net/XML headers & schema ...
|
||||
<resheader name="resmimetype">text/microsoft-resx</resheader>
|
||||
<resheader name="version">2.0</resheader>
|
||||
<resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
|
||||
<resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
|
||||
<data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
|
||||
<data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
|
||||
<data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
|
||||
<value>[base64 mime encoded serialized .NET Framework object]</value>
|
||||
</data>
|
||||
<data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
|
||||
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
|
||||
<comment>This is a comment</comment>
|
||||
</data>
|
||||
|
||||
There are any number of "resheader" rows that contain simple
|
||||
name/value pairs.
|
||||
|
||||
Each data row contains a name, and value. The row also contains a
|
||||
type or mimetype. Type corresponds to a .NET class that support
|
||||
text/value conversion through the TypeConverter architecture.
|
||||
Classes that don't support this are serialized and stored with the
|
||||
mimetype set.
|
||||
|
||||
The mimetype is used for serialized objects, and tells the
|
||||
ResXResourceReader how to depersist the object. This is currently not
|
||||
extensible. For a given mimetype the value must be set accordingly:
|
||||
|
||||
Note - application/x-microsoft.net.object.binary.base64 is the format
|
||||
that the ResXResourceWriter will generate, however the reader can
|
||||
read any of the formats listed below.
|
||||
|
||||
mimetype: application/x-microsoft.net.object.binary.base64
|
||||
value : The object must be serialized with
|
||||
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
|
||||
: and then encoded with base64 encoding.
|
||||
|
||||
mimetype: application/x-microsoft.net.object.soap.base64
|
||||
value : The object must be serialized with
|
||||
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
|
||||
: and then encoded with base64 encoding.
|
||||
|
||||
mimetype: application/x-microsoft.net.object.bytearray.base64
|
||||
value : The object must be serialized into a byte array
|
||||
: using a System.ComponentModel.TypeConverter
|
||||
: and then encoded with base64 encoding.
|
||||
-->
|
||||
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
|
||||
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
|
||||
<xsd:element name="root" msdata:IsDataSet="true">
|
||||
<xsd:complexType>
|
||||
<xsd:choice maxOccurs="unbounded">
|
||||
<xsd:element name="metadata">
|
||||
<xsd:complexType>
|
||||
<xsd:sequence>
|
||||
<xsd:element name="value" type="xsd:string" minOccurs="0" />
|
||||
</xsd:sequence>
|
||||
<xsd:attribute name="name" use="required" type="xsd:string" />
|
||||
<xsd:attribute name="type" type="xsd:string" />
|
||||
<xsd:attribute name="mimetype" type="xsd:string" />
|
||||
<xsd:attribute ref="xml:space" />
|
||||
</xsd:complexType>
|
||||
</xsd:element>
|
||||
<xsd:element name="assembly">
|
||||
<xsd:complexType>
|
||||
<xsd:attribute name="alias" type="xsd:string" />
|
||||
<xsd:attribute name="name" type="xsd:string" />
|
||||
</xsd:complexType>
|
||||
</xsd:element>
|
||||
<xsd:element name="data">
|
||||
<xsd:complexType>
|
||||
<xsd:sequence>
|
||||
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
|
||||
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
|
||||
</xsd:sequence>
|
||||
<xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
|
||||
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
|
||||
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
|
||||
<xsd:attribute ref="xml:space" />
|
||||
</xsd:complexType>
|
||||
</xsd:element>
|
||||
<xsd:element name="resheader">
|
||||
<xsd:complexType>
|
||||
<xsd:sequence>
|
||||
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
|
||||
</xsd:sequence>
|
||||
<xsd:attribute name="name" type="xsd:string" use="required" />
|
||||
</xsd:complexType>
|
||||
</xsd:element>
|
||||
</xsd:choice>
|
||||
</xsd:complexType>
|
||||
</xsd:element>
|
||||
</xsd:schema>
|
||||
<resheader name="resmimetype">
|
||||
<value>text/microsoft-resx</value>
|
||||
</resheader>
|
||||
<resheader name="version">
|
||||
<value>2.0</value>
|
||||
</resheader>
|
||||
<resheader name="reader">
|
||||
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
|
||||
</resheader>
|
||||
<resheader name="writer">
|
||||
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
|
||||
</resheader>
|
||||
</root>
|
||||
@@ -1,3 +1,4 @@
|
||||
DevExpress.XtraEditors.ButtonEdit, DevExpress.XtraEditors.v23.2, Version=23.2.3.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a
|
||||
DevExpress.XtraLayout.LayoutControl, DevExpress.XtraLayout.v23.2, Version=23.2.3.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a
|
||||
DevExpress.XtraTreeList.TreeList, DevExpress.XtraTreeList.v23.2, Version=23.2.3.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a
|
||||
DevExpress.XtraBars.BarManager, DevExpress.XtraBars.v23.2, Version=23.2.3.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a
|
||||
|
||||
84
SqlBuilder.cs
Normal file
84
SqlBuilder.cs
Normal file
@@ -0,0 +1,84 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.Text;
|
||||
|
||||
namespace DbTools {
|
||||
public class SqlBuilder {
|
||||
|
||||
public string[] GetTableNames(IDbConnection cn) {
|
||||
if (cn == null) {
|
||||
throw new ArgumentNullException("cn");
|
||||
}
|
||||
List<string> tables = new List<string>();
|
||||
if (cn.State != ConnectionState.Open) {
|
||||
cn.Open();
|
||||
}
|
||||
using (IDbCommand cmd = cn.CreateCommand()) {
|
||||
cmd.CommandText = "SELECT name FROM sqlite_master WHERE type='table' AND name NOT LIKE 'sqlite_%' ORDER BY name;";
|
||||
using (IDataReader reader = cmd.ExecuteReader()) {
|
||||
while (reader.Read()) {
|
||||
tables.Add(reader.GetString(0));
|
||||
}
|
||||
}
|
||||
}
|
||||
return tables.ToArray();
|
||||
}
|
||||
|
||||
public string GetTableCreateSql(IDbConnection cn, string tableName, string selectStatement = "select * from $table") {
|
||||
if (cn == null) {
|
||||
throw new ArgumentNullException("cn");
|
||||
}
|
||||
if (string.IsNullOrEmpty(tableName)) {
|
||||
throw new ArgumentNullException("tableName");
|
||||
}
|
||||
|
||||
if (cn.State != ConnectionState.Open) {
|
||||
cn.Open();
|
||||
}
|
||||
selectStatement = selectStatement.Replace("$table", tableName);
|
||||
using (IDbCommand cmd = cn.CreateCommand()) {
|
||||
cmd.CommandText = selectStatement;
|
||||
using (IDataReader reader = cmd.ExecuteReader(CommandBehavior.SchemaOnly | CommandBehavior.KeyInfo)) {
|
||||
DataTable schemaTable = reader.GetSchemaTable();
|
||||
if (schemaTable == null) {
|
||||
throw new Exception("Could not get schema for table " + tableName);
|
||||
}
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.AppendLine("CREATE TABLE " + tableName + " (");
|
||||
List<string> pkColumns = new List<string>();
|
||||
foreach (DataRow row in schemaTable.Rows) {
|
||||
string columnName = row["ColumnName"].ToString();
|
||||
string dataType = row["DataTypeName"].ToString();
|
||||
int columnSize = Convert.ToInt32(row["ColumnSize"]);
|
||||
bool allowDBNull = Convert.ToBoolean(row["AllowDBNull"]);
|
||||
bool isKey = Convert.ToBoolean(row["IsKey"]);
|
||||
if (isKey) {
|
||||
pkColumns.Add(columnName);
|
||||
}
|
||||
sb.Append(" " + columnName + " " + dataType);
|
||||
if (dataType.Equals("VARCHAR", StringComparison.OrdinalIgnoreCase) ||
|
||||
dataType.Equals("CHAR", StringComparison.OrdinalIgnoreCase) ||
|
||||
dataType.Equals("NVARCHAR", StringComparison.OrdinalIgnoreCase) ||
|
||||
dataType.Equals("NCHAR", StringComparison.OrdinalIgnoreCase)) {
|
||||
sb.Append("(" + columnSize + ")");
|
||||
}
|
||||
if (!allowDBNull) {
|
||||
sb.Append(" NOT NULL");
|
||||
}
|
||||
sb.AppendLine(",");
|
||||
}
|
||||
if (pkColumns.Count > 0) {
|
||||
sb.AppendLine(" PRIMARY KEY (" + string.Join(", ", pkColumns) + ")");
|
||||
} else {
|
||||
// Remove the last comma
|
||||
sb.Length -= 3;
|
||||
sb.AppendLine();
|
||||
}
|
||||
sb.AppendLine(");");
|
||||
return sb.ToString();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user