diff --git a/minisqlquery-master/src/MiniSqlQuery.Core/DbModel/DbModelColumn.cs b/minisqlquery-master/src/MiniSqlQuery.Core/DbModel/DbModelColumn.cs
new file mode 100644
index 0000000..843a2a0
--- /dev/null
+++ b/minisqlquery-master/src/MiniSqlQuery.Core/DbModel/DbModelColumn.cs
@@ -0,0 +1,89 @@
+#region License
+
+// Copyright 2005-2019 Paul Kohler (https://github.com/paulkohler/minisqlquery). All rights reserved.
+// This source code is made available under the terms of the GNU Lesser General Public License v3.0
+// https://github.com/paulkohler/minisqlquery/blob/master/LICENSE
+#endregion
+
+using System.Diagnostics;
+
+namespace MiniSqlQuery.Core.DbModel
+{
+ /// <summary>The db model column.</summary>
+ [DebuggerDisplay("DbModelColumn: {Name} (DbModelType.Summary: {DbModelType.Summary}, Nullable: {Nullable}, IsKey: {IsKey})")]
+ public class DbModelColumn : DbModelObjectBase
+ {
+ /// <summary>Initializes a new instance of the <see cref="DbModelColumn"/> class.</summary>
+ public DbModelColumn()
+ {
+ Nullable = true;
+ DbType = new DbModelType("varchar", 50);
+ ObjectType = ObjectTypes.Column;
+ }
+
+ /// <summary>Gets or sets DbType.</summary>
+ /// <value>The db type.</value>
+ public virtual DbModelType DbType { get; set; }
+
+ /// <summary>Gets or sets ForeignKeyReference.</summary>
+ /// <value>The foreign key reference.</value>
+ public virtual DbModelForeignKeyReference ForeignKeyReference { get; set; }
+
+ /// <summary>Gets FullName.</summary>
+ /// <value>The full name.</value>
+ public override string FullName
+ {
+ get { return Name; }
+ }
+
+ /// <summary>Gets a value indicating whether HasFK.</summary>
+ /// <value>The has fk.</value>
+ public virtual bool HasFK
+ {
+ get { return ForeignKeyReference != null; }
+ }
+
+ /// <summary>Gets or sets a value indicating whether IsAutoIncrement.</summary>
+ /// <value>The is auto increment.</value>
+ public virtual bool IsAutoIncrement { get; set; }
+
+ /// <summary>Gets or sets a value indicating whether IsIdentity.</summary>
+ /// <value>The is identity.</value>
+ public virtual bool IsIdentity { get; set; }
+
+ /// <summary>Gets or sets a value indicating whether IsKey.</summary>
+ /// <value>The is key.</value>
+ public virtual bool IsKey { get; set; }
+
+ /// <summary>Gets or sets a value indicating whether IsReadOnly.</summary>
+ /// <value>The is read only.</value>
+ public virtual bool IsReadOnly { get; set; }
+
+ /// <summary>
+ /// Gets or sets a value indicating whether this column is a concurrency field, such as a timestamp.
+ /// </summary>
+ /// <value>
+ /// <c>true</c> if this instance is row version, or concurrency field; otherwise, <c>false</c>.
+ /// </value>
+ public virtual bool IsRowVersion { get; set; }
+
+ /// <summary>Gets or sets a value indicating whether IsUnique.</summary>
+ /// <value>The is unique.</value>
+ public virtual bool IsUnique { get; set; }
+
+ /// <summary>Gets a value indicating whether IsWritable.</summary>
+ /// <value>The is writable.</value>
+ public virtual bool IsWritable
+ {
+ get { return !IsReadOnly; }
+ }
+
+ /// <summary>Gets or sets a value indicating whether Nullable.</summary>
+ /// <value>The nullable.</value>
+ public virtual bool Nullable { get; set; }
+
+ /// <summary>Gets the parent table of the column.</summary>
+ /// <value>The parent table instance.</value>
+ public virtual DbModelTable ParentTable { get; internal set; }
+ }
+}
\ No newline at end of file
diff --git a/minisqlquery-master/src/MiniSqlQuery.Core/DbModel/DbModelColumnCollection.cs b/minisqlquery-master/src/MiniSqlQuery.Core/DbModel/DbModelColumnCollection.cs
new file mode 100644
index 0000000..dc8eebc
--- /dev/null
+++ b/minisqlquery-master/src/MiniSqlQuery.Core/DbModel/DbModelColumnCollection.cs
@@ -0,0 +1,15 @@
+#region License
+// Copyright 2005-2009 Paul Kohler (http://pksoftware.net/MiniSqlQuery/). All rights reserved.
+// This source code is made available under the terms of the Microsoft Public License (Ms-PL)
+// http://minisqlquery.codeplex.com/license
+#endregion
+using System;
+using System.Collections.Generic;
+
+namespace MiniSqlQuery.Core.DbModel
+{
+ [Obsolete("Just use List - easier for filtering etc")]
+ public class DbModelColumnCollection : List<DbModelColumn>
+ {
+ }
+}
\ No newline at end of file
diff --git a/minisqlquery-master/src/MiniSqlQuery.Core/DbModel/DbModelConstraint.cs b/minisqlquery-master/src/MiniSqlQuery.Core/DbModel/DbModelConstraint.cs
new file mode 100644
index 0000000..ad2f12a
--- /dev/null
+++ b/minisqlquery-master/src/MiniSqlQuery.Core/DbModel/DbModelConstraint.cs
@@ -0,0 +1,54 @@
+#region License
+
+// Copyright 2005-2019 Paul Kohler (https://github.com/paulkohler/minisqlquery). All rights reserved.
+// This source code is made available under the terms of the GNU Lesser General Public License v3.0
+// https://github.com/paulkohler/minisqlquery/blob/master/LICENSE
+#endregion
+
+
+namespace MiniSqlQuery.Core.DbModel
+{
+ /// <summary>The db model constraint.</summary>
+ public class DbModelConstraint
+ {
+ /// <summary>Gets or sets ColumnName.</summary>
+ /// <value>The column name.</value>
+ public string ColumnName { get; set; }
+
+ /// <summary>Gets or sets ConstraintName.</summary>
+ /// <value>The constraint name.</value>
+ public string ConstraintName { get; set; }
+
+ /// <summary>Gets or sets ConstraintTableName.</summary>
+ /// <value>The constraint table name.</value>
+ public string ConstraintTableName { get; set; }
+
+ /// <summary>Gets or sets ConstraintTableSchema.</summary>
+ /// <value>The constraint table schema.</value>
+ public string ConstraintTableSchema { get; set; }
+
+ /// <summary>Gets or sets DeleteRule.</summary>
+ /// <value>The delete rule.</value>
+ public string DeleteRule { get; set; }
+
+ /// <summary>Gets or sets UniqueColumnName.</summary>
+ /// <value>The unique column name.</value>
+ public string UniqueColumnName { get; set; }
+
+ /// <summary>Gets or sets UniqueConstraintName.</summary>
+ /// <value>The unique constraint name.</value>
+ public string UniqueConstraintName { get; set; }
+
+ /// <summary>Gets or sets UniqueConstraintTableName.</summary>
+ /// <value>The unique constraint table name.</value>
+ public string UniqueConstraintTableName { get; set; }
+
+ /// <summary>Gets or sets UniqueConstraintTableSchema.</summary>
+ /// <value>The unique constraint table schema.</value>
+ public string UniqueConstraintTableSchema { get; set; }
+
+ /// <summary>Gets or sets UpdateRule.</summary>
+ /// <value>The update rule.</value>
+ public string UpdateRule { get; set; }
+ }
+}
\ No newline at end of file
diff --git a/minisqlquery-master/src/MiniSqlQuery.Core/DbModel/DbModelDependencyWalker.cs b/minisqlquery-master/src/MiniSqlQuery.Core/DbModel/DbModelDependencyWalker.cs
new file mode 100644
index 0000000..7851f1e
--- /dev/null
+++ b/minisqlquery-master/src/MiniSqlQuery.Core/DbModel/DbModelDependencyWalker.cs
@@ -0,0 +1,94 @@
+#region License
+
+// Copyright 2005-2019 Paul Kohler (https://github.com/paulkohler/minisqlquery). All rights reserved.
+// This source code is made available under the terms of the GNU Lesser General Public License v3.0
+// https://github.com/paulkohler/minisqlquery/blob/master/LICENSE
+#endregion
+
+using System;
+using System.Collections.Generic;
+
+namespace MiniSqlQuery.Core.DbModel
+{
+ /// <summary>Examins a <see cref="DbModelInstance"/> providing sort methods.</summary>
+ public class DbModelDependencyWalker
+ {
+ /// <summary>The _model.</summary>
+ private readonly DbModelInstance _model;
+
+ /// <summary>Initializes a new instance of the <see cref="DbModelDependencyWalker"/> class.</summary>
+ /// <param name="model">The database model instance.</param>
+ public DbModelDependencyWalker(DbModelInstance model)
+ {
+ if (model == null)
+ {
+ throw new ArgumentNullException("model");
+ }
+
+ _model = model;
+ }
+
+ /// <summary>Sorts the tables by checking the foreign key references recursivly building a list of tables in order.</summary>
+ /// <returns>An array of tables in dependency order.</returns>
+ public DbModelTable[] SortTablesByForeignKeyReferences()
+ {
+ List<DbModelTable> tables = new List<DbModelTable>();
+
+ // add tables with no FKs
+ foreach (DbModelTable table in _model.Tables)
+ {
+ if (table.ForeignKeyColumns.Count == 0)
+ {
+ tables.Add(table);
+ }
+ }
+
+ foreach (DbModelTable table in _model.Tables)
+ {
+ ProcessForeignKeyReferences(1, tables, table);
+ }
+
+ return tables.ToArray();
+ }
+
+ /// <summary>The process foreign key references.</summary>
+ /// <param name="level">The level.</param>
+ /// <param name="tablesList">The tables list.</param>
+ /// <param name="table">The table.</param>
+ /// <exception cref="InvalidOperationException"></exception>
+ private void ProcessForeignKeyReferences(int level, List<DbModelTable> tablesList, DbModelTable table)
+ {
+ if (tablesList.Contains(table))
+ {
+ return;
+ }
+
+ // recursive insurance ;-)
+ level++;
+ if (level > 1000)
+ {
+ throw new InvalidOperationException(string.Format("FK processor exceeded recursive level of 1000 at '{0}'.", table.Name));
+ }
+
+ // if there are FK refs, add the refered tables first
+ if (table.ForeignKeyColumns.Count > 0)
+ {
+ // if the table is not already in the list....
+ foreach (DbModelColumn fkColumn in table.ForeignKeyColumns)
+ {
+ // ensure its not a self referencing table
+ if (fkColumn.ForeignKeyReference.ReferenceTable != table)
+ {
+ ProcessForeignKeyReferences(++level, tablesList, fkColumn.ForeignKeyReference.ReferenceTable);
+ }
+ }
+
+ // now add the table if not in the list yet
+ if (!tablesList.Contains(table))
+ {
+ tablesList.Add(table);
+ }
+ }
+ }
+ }
+}
\ No newline at end of file
diff --git a/minisqlquery-master/src/MiniSqlQuery.Core/DbModel/DbModelForeignKeyReference.cs b/minisqlquery-master/src/MiniSqlQuery.Core/DbModel/DbModelForeignKeyReference.cs
new file mode 100644
index 0000000..ad6c910
--- /dev/null
+++ b/minisqlquery-master/src/MiniSqlQuery.Core/DbModel/DbModelForeignKeyReference.cs
@@ -0,0 +1,54 @@
+#region License
+
+// Copyright 2005-2019 Paul Kohler (https://github.com/paulkohler/minisqlquery). All rights reserved.
+// This source code is made available under the terms of the GNU Lesser General Public License v3.0
+// https://github.com/paulkohler/minisqlquery/blob/master/LICENSE
+#endregion
+
+
+namespace MiniSqlQuery.Core.DbModel
+{
+ /// <summary>The db model foreign key reference.</summary>
+ public class DbModelForeignKeyReference
+ {
+ /// <summary>Initializes a new instance of the <see cref="DbModelForeignKeyReference"/> class.</summary>
+ public DbModelForeignKeyReference()
+ {
+ }
+
+ /// <summary>Initializes a new instance of the <see cref="DbModelForeignKeyReference"/> class.</summary>
+ /// <param name="owningColumn">The owning column.</param>
+ /// <param name="fkTable">The fk table.</param>
+ /// <param name="fkColumn">The fk column.</param>
+ public DbModelForeignKeyReference(DbModelColumn owningColumn, DbModelTable fkTable, DbModelColumn fkColumn)
+ {
+ OwningColumn = owningColumn;
+ ReferenceTable = fkTable;
+ ReferenceColumn = fkColumn;
+ }
+
+ /// <summary>Gets or sets ConstraintName.</summary>
+ /// <value>The constraint name.</value>
+ public virtual string ConstraintName { get; set; }
+
+ /// <summary>Gets or sets DeleteRule.</summary>
+ /// <value>The delete rule.</value>
+ public string DeleteRule { get; set; }
+
+ /// <summary>Gets or sets OwningColumn.</summary>
+ /// <value>The owning column.</value>
+ public DbModelColumn OwningColumn { get; set; }
+
+ /// <summary>Gets or sets ReferenceColumn.</summary>
+ /// <value>The reference column.</value>
+ public DbModelColumn ReferenceColumn { get; set; }
+
+ /// <summary>Gets or sets ReferenceTable.</summary>
+ /// <value>The reference table.</value>
+ public DbModelTable ReferenceTable { get; set; }
+
+ /// <summary>Gets or sets UpdateRule.</summary>
+ /// <value>The update rule.</value>
+ public string UpdateRule { get; set; }
+ }
+}
\ No newline at end of file
diff --git a/minisqlquery-master/src/MiniSqlQuery.Core/DbModel/DbModelInstance.cs b/minisqlquery-master/src/MiniSqlQuery.Core/DbModel/DbModelInstance.cs
new file mode 100644
index 0000000..5776f44
--- /dev/null
+++ b/minisqlquery-master/src/MiniSqlQuery.Core/DbModel/DbModelInstance.cs
@@ -0,0 +1,101 @@
+#region License
+
+// Copyright 2005-2019 Paul Kohler (https://github.com/paulkohler/minisqlquery). All rights reserved.
+// This source code is made available under the terms of the GNU Lesser General Public License v3.0
+// https://github.com/paulkohler/minisqlquery/blob/master/LICENSE
+#endregion
+
+using System;
+using System.Collections.Generic;
+
+namespace MiniSqlQuery.Core.DbModel
+{
+ /// <summary>The db model instance.</summary>
+ public class DbModelInstance
+ {
+ /// <summary>The _tables.</summary>
+ private readonly List<DbModelTable> _tables;
+
+ /// <summary>The _views.</summary>
+ private readonly List<DbModelView> _views;
+
+ /// <summary>Initializes a new instance of the <see cref="DbModelInstance"/> class.</summary>
+ public DbModelInstance()
+ {
+ _tables = new List<DbModelTable>();
+ _views = new List<DbModelView>();
+ }
+
+ /// <summary>Gets or sets ConnectionString.</summary>
+ /// <value>The connection string.</value>
+ public string ConnectionString { get; set; }
+
+ /// <summary>Gets or sets ProviderName.</summary>
+ /// <value>The provider name.</value>
+ public string ProviderName { get; set; }
+
+ /// <summary>Gets Tables.</summary>
+ /// <value>The tables.</value>
+ public virtual ICollection<DbModelTable> Tables
+ {
+ get { return _tables; }
+ }
+
+ /// <summary>Gets or sets Types.</summary>
+ /// <value>The types.</value>
+ public Dictionary<string, DbModelType> Types { get; set; }
+
+ /// <summary>Gets Views.</summary>
+ /// <value>The views.</value>
+ public virtual ICollection<DbModelView> Views
+ {
+ get { return _views; }
+ }
+
+ /// <summary>The add.</summary>
+ /// <param name="table">The table.</param>
+ public virtual void Add(DbModelTable table)
+ {
+ table.ParentDb = this;
+ _tables.Add(table);
+ }
+
+ /// <summary>The add.</summary>
+ /// <param name="view">The view.</param>
+ public virtual void Add(DbModelView view)
+ {
+ view.ParentDb = this;
+ _views.Add(view);
+ }
+
+ /// <summary>The find table.</summary>
+ /// <param name="tableName">The table name.</param>
+ /// <returns></returns>
+ public virtual DbModelTable FindTable(string tableName)
+ {
+ return _tables.Find(table => table.FullName.Equals(tableName, StringComparison.InvariantCultureIgnoreCase));
+ }
+
+ /// <summary>The find table or view.</summary>
+ /// <param name="name">The name.</param>
+ /// <returns></returns>
+ public virtual DbModelTable FindTableOrView(string name)
+ {
+ var obj = _tables.Find(table => table.FullName.Equals(name, StringComparison.InvariantCultureIgnoreCase));
+ if (obj == null)
+ {
+ obj = _views.Find(view => view.FullName.Equals(name, StringComparison.InvariantCultureIgnoreCase));
+ }
+
+ return obj;
+ }
+
+ /// <summary>The find view.</summary>
+ /// <param name="viewName">The view name.</param>
+ /// <returns></returns>
+ public virtual DbModelTable FindView(string viewName)
+ {
+ return _views.Find(view => view.FullName.Equals(viewName, StringComparison.InvariantCultureIgnoreCase));
+ }
+ }
+}
\ No newline at end of file
diff --git a/minisqlquery-master/src/MiniSqlQuery.Core/DbModel/DbModelObjectBase.cs b/minisqlquery-master/src/MiniSqlQuery.Core/DbModel/DbModelObjectBase.cs
new file mode 100644
index 0000000..348fd8f
--- /dev/null
+++ b/minisqlquery-master/src/MiniSqlQuery.Core/DbModel/DbModelObjectBase.cs
@@ -0,0 +1,46 @@
+#region License
+
+// Copyright 2005-2019 Paul Kohler (https://github.com/paulkohler/minisqlquery). All rights reserved.
+// This source code is made available under the terms of the GNU Lesser General Public License v3.0
+// https://github.com/paulkohler/minisqlquery/blob/master/LICENSE
+
+#endregion
+
+
+namespace MiniSqlQuery.Core.DbModel
+{
+ /// <summary>The db model object base.</summary>
+ public class DbModelObjectBase : IDbModelNamedObject
+ {
+ private string _fullName;
+
+ /// <summary>
+ /// Gets the full name of the object which may include the <see cref="IDbModelNamedObject.Schema"/> for example.
+ /// </summary>
+ /// <value>The full name.</value>
+ public virtual string FullName
+ {
+ get
+ {
+ if (_fullName == null)
+ {
+ _fullName = Utility.RenderSafeSchemaObjectName(Schema, Name);
+ }
+
+ return _fullName;
+ }
+ }
+
+ /// <summary>Gets or sets name of the database object.</summary>
+ /// <value>The name of the object.</value>
+ public virtual string Name { get; set; }
+
+ /// <summary>Gets or sets ObjectType.</summary>
+ /// <value>The object type.</value>
+ public virtual string ObjectType { get; set; }
+
+ /// <summary>Gets or sets Schema.</summary>
+ /// <value>The schema.</value>
+ public virtual string Schema { get; set; }
+ }
+}
\ No newline at end of file
diff --git a/minisqlquery-master/src/MiniSqlQuery.Core/DbModel/DbModelTable.cs b/minisqlquery-master/src/MiniSqlQuery.Core/DbModel/DbModelTable.cs
new file mode 100644
index 0000000..c5e7820
--- /dev/null
+++ b/minisqlquery-master/src/MiniSqlQuery.Core/DbModel/DbModelTable.cs
@@ -0,0 +1,66 @@
+#region License
+
+// Copyright 2005-2019 Paul Kohler (https://github.com/paulkohler/minisqlquery). All rights reserved.
+// This source code is made available under the terms of the GNU Lesser General Public License v3.0
+// https://github.com/paulkohler/minisqlquery/blob/master/LICENSE
+#endregion
+
+using System.Collections.Generic;
+using System.Diagnostics;
+
+namespace MiniSqlQuery.Core.DbModel
+{
+ /// <summary>The db model table.</summary>
+ [DebuggerDisplay("DbModelTable: {FullName} (Columns: {Columns.Count}, PKs: {PrimaryKeyColumns.Count}, FKs: {ForeignKeyColumns.Count})")]
+ public class DbModelTable : DbModelObjectBase
+ {
+ /// <summary>Initializes a new instance of the <see cref="DbModelTable"/> class.</summary>
+ public DbModelTable()
+ {
+ Columns = new List<DbModelColumn>();
+ Constraints = new List<DbModelConstraint>();
+ ObjectType = ObjectTypes.Table;
+ }
+
+ /// <summary>Gets the Columns for this table.</summary>
+ /// <value>The columns.</value>
+ public virtual List<DbModelColumn> Columns { get; internal set; }
+
+ /// <summary>Gets Constraints.</summary>
+ /// <value>The constraints.</value>
+ public virtual List<DbModelConstraint> Constraints { get; private set; }
+
+ /// <summary>Gets ForeignKeyColumns.</summary>
+ /// <value>The foreign key columns.</value>
+ public virtual List<DbModelColumn> ForeignKeyColumns
+ {
+ get { return Columns.FindAll(c => c.ForeignKeyReference != null); }
+ }
+
+ /// <summary>Gets NonKeyColumns.</summary>
+ /// <value>The non key columns.</value>
+ public virtual List<DbModelColumn> NonKeyColumns
+ {
+ get { return Columns.FindAll(c => !c.IsKey && c.ForeignKeyReference == null); }
+ }
+
+ /// <summary>Gets a reference to the parent database instance.</summary>
+ /// <value>The parent model instance object.</value>
+ public virtual DbModelInstance ParentDb { get; internal set; }
+
+ /// <summary>Gets PrimaryKeyColumns.</summary>
+ /// <value>The primary key columns.</value>
+ public virtual List<DbModelColumn> PrimaryKeyColumns
+ {
+ get { return Columns.FindAll(c => c.IsKey); }
+ }
+
+ /// <summary>Adds the <paramref name="column"/> to the list of <see cref="Columns"/> assigning the <see cref="DbModelColumn.ParentTable"/>.</summary>
+ /// <param name="column">The new column.</param>
+ public virtual void Add(DbModelColumn column)
+ {
+ column.ParentTable = this;
+ Columns.Add(column);
+ }
+ }
+}
\ No newline at end of file
diff --git a/minisqlquery-master/src/MiniSqlQuery.Core/DbModel/DbModelType.cs b/minisqlquery-master/src/MiniSqlQuery.Core/DbModel/DbModelType.cs
new file mode 100644
index 0000000..261c15b
--- /dev/null
+++ b/minisqlquery-master/src/MiniSqlQuery.Core/DbModel/DbModelType.cs
@@ -0,0 +1,212 @@
+#region License
+
+// Copyright 2005-2019 Paul Kohler (https://github.com/paulkohler/minisqlquery). All rights reserved.
+// This source code is made available under the terms of the GNU Lesser General Public License v3.0
+// https://github.com/paulkohler/minisqlquery/blob/master/LICENSE
+
+#endregion
+
+using System;
+using System.Collections.Generic;
+using System.Diagnostics;
+
+namespace MiniSqlQuery.Core.DbModel
+{
+ /// <summary>Describes a database type.</summary>
+ [DebuggerDisplay("DbModelType: {Name} [{Summary,nq}]")]
+ public class DbModelType : DbModelObjectBase
+ {
+ /// <summary>Initializes a new instance of the <see cref="DbModelType"/> class.</summary>
+ /// <param name="name">The name of the type, e.g. "varchar".</param>
+ /// <param name="length">The length of the type, e.g. 10.</param>
+ public DbModelType(string name, int length)
+ {
+ Name = name;
+ Length = length;
+ }
+
+ /// <summary>Gets or sets CreateFormat.</summary>
+ /// <value>The create format.</value>
+ public virtual string CreateFormat { get; set; }
+
+ /// <summary>Gets or sets CreateParameters.</summary>
+ /// <value>The create parameters.</value>
+ public virtual string CreateParameters { get; set; }
+
+ /// <summary>Gets or sets Length.</summary>
+ /// <value>The length.</value>
+ public virtual int Length { get; set; }
+
+ /// <summary>Gets or sets LiteralPrefix.</summary>
+ /// <value>The literal prefix.</value>
+ public virtual string LiteralPrefix { get; set; }
+
+ /// <summary>Gets or sets LiteralSuffix.</summary>
+ /// <value>The literal suffix.</value>
+ public virtual string LiteralSuffix { get; set; }
+
+ /// <summary>Gets or sets Precision.</summary>
+ /// <value>The precision.</value>
+ public virtual int Precision { get; set; }
+
+ /// <summary>Gets or sets ProviderDbType.</summary>
+ /// <value>The provider db type.</value>
+ public virtual string ProviderDbType { get; set; }
+
+ /// <summary>Gets or sets Scale.</summary>
+ /// <value>The scale.</value>
+ public virtual int Scale { get; set; }
+
+ /// <summary>
+ /// Gets the summary of the SQL type using the <see cref="CreateFormat"/> if applicable.
+ /// </summary>
+ /// <value>The summary.</value>
+ public virtual string Summary
+ {
+ get
+ {
+ if (!string.IsNullOrEmpty(CreateFormat))
+ {
+ if (CreateFormat.Contains("{1}") && (Precision != -1 && Scale != -1))
+ {
+ return string.Format(CreateFormat, Precision, Scale);
+ }
+
+ if (CreateFormat.Contains("{0}") && !CreateFormat.Contains("{1}") && (Length != -1))
+ {
+ return string.Format(CreateFormat, Length);
+ }
+
+ if (CreateFormat.Contains("{0}"))
+ {
+ // err...
+ return Name;
+ }
+
+ return CreateFormat;
+ }
+
+ return Name;
+ }
+ }
+
+ /// <summary>Gets or sets SystemType.</summary>
+ /// <value>The system type.</value>
+ public virtual Type SystemType { get; set; }
+
+ /// <summary>Gets or sets Value.</summary>
+ /// <value>The value.</value>
+ public virtual object Value { get; set; }
+
+ // public static DbModelType Create(string name, int length, int precision, int scale, string systemTypeName)
+ // {
+ // return Create(null, name, length, precision, scale, systemTypeName);
+ // }
+
+ /// <summary>Creates an instance of <see cref="DbModelType"/> defined by the parameers.</summary>
+ /// <param name="dbTypes">The db types list, if the <paramref name="name"/> is in the list it is used as a base copy of the type.</param>
+ /// <param name="name">The name of the type, e.g. "int", "nvarchar" etc.</param>
+ /// <param name="length">The length of the type.</param>
+ /// <param name="precision">The precision.</param>
+ /// <param name="scale">The scale.</param>
+ /// <param name="systemTypeName">Name of the system type, e.g. "System.String".</param>
+ /// <returns>An instance of <see cref="DbModelType"/> defined by the parameers</returns>
+ public static DbModelType Create(Dictionary<string, DbModelType> dbTypes, string name, int length, int precision, int scale, string systemTypeName)
+ {
+ DbModelType baseType;
+
+ string key = name.ToLower();
+ if (dbTypes != null && dbTypes.ContainsKey(key))
+ {
+ // the type should be here, this is used as a baseline for new instances
+ baseType = dbTypes[key].Copy();
+ baseType.Length = length;
+ }
+ else
+ {
+ baseType = new DbModelType(name, length);
+ baseType.SystemType = Type.GetType(systemTypeName);
+ }
+
+ baseType.Precision = precision;
+ baseType.Scale = scale;
+
+ return baseType;
+ }
+
+ /// <summary>Copies this instance.</summary>
+ /// <returns>A copy of this instance.</returns>
+ public DbModelType Copy()
+ {
+ DbModelType copy = new DbModelType(Name, Length)
+ {
+ CreateFormat = CreateFormat,
+ CreateParameters = CreateParameters,
+ LiteralPrefix = LiteralPrefix,
+ LiteralSuffix = LiteralSuffix,
+ Precision = Precision,
+ Scale = Scale,
+ SystemType = SystemType,
+ };
+ return copy;
+ }
+
+ /// <summary>Renders this <see cref="DbModelType"/> as basic DDL base on the contents of <see cref="Value"/> (assumes nullable).</summary>
+ /// <returns>An SQL string representing the <see cref="Value"/> acording to the database type (assumes nullable).</returns>
+ public string ToDDLValue()
+ {
+ return ToDDLValue(true);
+ }
+
+ /// <summary>Renders this <see cref="DbModelType"/> as basic DDL base on the contents of <see cref="Value"/>.</summary>
+ /// <param name="nullable">A boolean value indicating the nullability of the column.</param>
+ /// <returns>An SQL string representing the <see cref="Value"/> acording to the database type.</returns>
+ /// <remarks>If a column is "not null" and the <see cref="Value"/> is null, in the furure this method will attampt to return a default value
+ /// rather than throwing an exception etc.</remarks>
+ public string ToDDLValue(bool nullable)
+ {
+ if (Value == null || Value == DBNull.Value)
+ {
+ if (nullable)
+ {
+ return "null";
+ }
+
+ // not supposed to be nullable but the Value is. render a default
+ switch (SystemType.FullName)
+ {
+ case "System.String":
+ return string.Concat(LiteralPrefix, LiteralSuffix);
+ case "System.DateTime":
+ return "'?'";
+ case "System.Guid":
+ return string.Concat("'", Guid.Empty, "'");
+ default:
+ return "0"; // take a punt
+ }
+ }
+
+ if (SystemType == typeof(string))
+ {
+ return string.Concat(LiteralPrefix, ((string)Value).Replace("'", "''"), LiteralSuffix);
+ }
+
+ if (SystemType == typeof(DateTime))
+ {
+ return string.Format("{0}{1:yyyy-MM-dd HH:mm:ss}{2}", LiteralPrefix, Value, LiteralSuffix);
+ }
+
+ if (SystemType == typeof(bool) && Name.Equals("bit", StringComparison.InvariantCultureIgnoreCase))
+ {
+ return ((bool)Value) ? "1" : "0";
+ }
+
+ if (SystemType == typeof(byte[]))
+ {
+ return "null /* not supported yet */ ";
+ }
+
+ return string.Concat(LiteralPrefix, Value, LiteralSuffix);
+ }
+ }
+}
\ No newline at end of file
diff --git a/minisqlquery-master/src/MiniSqlQuery.Core/DbModel/DbModelView.cs b/minisqlquery-master/src/MiniSqlQuery.Core/DbModel/DbModelView.cs
new file mode 100644
index 0000000..8438833
--- /dev/null
+++ b/minisqlquery-master/src/MiniSqlQuery.Core/DbModel/DbModelView.cs
@@ -0,0 +1,20 @@
+#region License
+
+// Copyright 2005-2019 Paul Kohler (https://github.com/paulkohler/minisqlquery). All rights reserved.
+// This source code is made available under the terms of the GNU Lesser General Public License v3.0
+// https://github.com/paulkohler/minisqlquery/blob/master/LICENSE
+#endregion
+
+
+namespace MiniSqlQuery.Core.DbModel
+{
+ /// <summary>The db model view.</summary>
+ public class DbModelView : DbModelTable
+ {
+ /// <summary>Initializes a new instance of the <see cref="DbModelView"/> class.</summary>
+ public DbModelView()
+ {
+ ObjectType = ObjectTypes.View;
+ }
+ }
+}
\ No newline at end of file
diff --git a/minisqlquery-master/src/MiniSqlQuery.Core/DbModel/GenericSchemaService.cs b/minisqlquery-master/src/MiniSqlQuery.Core/DbModel/GenericSchemaService.cs
new file mode 100644
index 0000000..a8a374a
--- /dev/null
+++ b/minisqlquery-master/src/MiniSqlQuery.Core/DbModel/GenericSchemaService.cs
@@ -0,0 +1,359 @@
+#region License
+
+// Copyright 2005-2019 Paul Kohler (https://github.com/paulkohler/minisqlquery). All rights reserved.
+// This source code is made available under the terms of the GNU Lesser General Public License v3.0
+// https://github.com/paulkohler/minisqlquery/blob/master/LICENSE
+
+#endregion
+
+using System;
+using System.Collections.Generic;
+using System.Data;
+using System.Data.Common;
+using System.Diagnostics;
+
+namespace MiniSqlQuery.Core.DbModel
+{
+ /// <summary>
+ /// The generic schema service.
+ /// </summary>
+ public class GenericSchemaService : IDatabaseSchemaService
+ {
+ /// <summary>The _connection.</summary>
+ private string _connection;
+
+ /// <summary>Gets or sets a value indicating whether ForeignKeyInformationAvailable.</summary>
+ /// <value>The foreign key information available.</value>
+ public bool ForeignKeyInformationAvailable { get; set; }
+
+ /// <summary>Gets or sets ProviderName.</summary>
+ /// <value>The provider name.</value>
+ public string ProviderName { get; set; }
+
+ /// <summary>Gets a database object model that represents the items defined by the <paramref name="connection"/>.</summary>
+ /// <param name="connection">The connection string.</param>
+ /// <returns>An instance of <see cref="DbModelInstance"/> describing the database.</returns>
+ public virtual DbModelInstance GetDbObjectModel(string connection)
+ {
+
+ _connection = connection;
+
+ DbModelInstance model = new DbModelInstance();
+ DbProviderFactory factory = DbProviderFactories.GetFactory(ProviderName);
+
+ try
+ {
+ using (DbConnection dbConn = factory.CreateConnection())
+ {
+ dbConn.ConnectionString = connection;
+ dbConn.Open();
+
+ DataTable tables = dbConn.GetSchema("Tables");
+ Dictionary<string, DbModelType> dbTypes = GetDbTypes(dbConn);
+ model.Types = dbTypes;
+ model.ProviderName = ProviderName;
+ model.ConnectionString = _connection;
+
+ if (tables == null)
+ {
+ return model;
+ }
+
+ DataView tablesDV = new DataView(tables, "TABLE_TYPE='TABLE' OR TABLE_TYPE='BASE TABLE'", "TABLE_SCHEMA, TABLE_NAME", DataViewRowState.CurrentRows);
+
+ foreach (DataRowView row in tablesDV)
+ {
+ string schemaName = SafeGetString(row.Row, "TABLE_SCHEMA");
+ string tableName = SafeGetString(row.Row, "TABLE_NAME");
+
+ DbModelTable dbTable = new DbModelTable { Schema = schemaName, Name = tableName };
+ model.Add(dbTable);
+
+ DataTable schemaTableKeyInfo = GetTableKeyInfo(dbConn, schemaName, tableName);
+ GetColumnsForTable(dbTable, schemaTableKeyInfo, dbTypes);
+ }
+
+ DataView viewsDV = new DataView(tables, "TABLE_TYPE='VIEW'", "TABLE_SCHEMA, TABLE_NAME", DataViewRowState.CurrentRows);
+ foreach (DataRowView row in viewsDV)
+ {
+ string schemaName = SafeGetString(row.Row, "TABLE_SCHEMA");
+ string tableName = SafeGetString(row.Row, "TABLE_NAME");
+
+ DbModelView dbTable = new DbModelView { Schema = schemaName, Name = tableName };
+ model.Add(dbTable);
+
+ DataTable schemaTableKeyInfo = GetTableKeyInfo(dbConn, schemaName, tableName);
+ GetColumnsForTable(dbTable, schemaTableKeyInfo, dbTypes);
+ }
+
+ // build FK relationships
+ if (model.Tables != null)
+ {
+ foreach (DbModelTable table in model.Tables)
+ {
+ GetForeignKeyReferencesForTable(dbConn, table);
+ ProcessForeignKeyReferencesForTable(dbConn, table);
+ }
+ }
+
+ // build FK relationships
+ if (model.Views != null)
+ {
+ foreach (DbModelView view in model.Views)
+ {
+ GetForeignKeyReferencesForTable(dbConn, view);
+ ProcessForeignKeyReferencesForTable(dbConn, view);
+ }
+ }
+ }
+ }
+ catch (Exception)
+ {
+ // catch all for providers that are not implementing the schema info.
+ return model;
+ }
+
+ return model;
+ }
+
+ /// <summary>The get db types.</summary>
+ /// <param name="connection">The connection.</param>
+ /// <returns>A dictionary of named <see cref="DbModelType"/> objects supported by the database.</returns>
+ /// <exception cref="ArgumentNullException">If the <paramref name="connection"/> is null.</exception>
+ public virtual Dictionary<string, DbModelType> GetDbTypes(DbConnection connection)
+ {
+ if (connection == null)
+ {
+ throw new ArgumentNullException("connection");
+ }
+
+ Dictionary<string, DbModelType> dbTypes = new Dictionary<string, DbModelType>();
+
+ DataTable dataTypes = connection.GetSchema("DataTypes");
+
+ foreach (DataRow row in dataTypes.Rows)
+ {
+ string typeName = SafeGetString(row, "TypeName");
+ int columnSize = SafeGetInt(row, "ColumnSize");
+ DbModelType dbType = new DbModelType(typeName, columnSize);
+
+ // Some providers may double up on schema info, check first:
+ if (!dbTypes.ContainsKey(typeName.ToLower()))
+ {
+ dbTypes.Add(typeName.ToLower(), dbType);
+
+ dbType.CreateFormat = SafeGetString(row, "CreateFormat");
+ dbType.CreateParameters = SafeGetString(row, "CreateParameters");
+ dbType.LiteralPrefix = SafeGetString(row, "LiteralPrefix");
+ dbType.LiteralSuffix = SafeGetString(row, "LiteralSuffix");
+ dbType.SystemType = Type.GetType(SafeGetString(row, "DataType"));
+ dbType.ProviderDbType = SafeGetString(row, "ProviderDbType");
+ }
+ }
+
+ return dbTypes;
+ }
+
+ /// <returns>The get description of the database.</returns>
+ public string GetDescription()
+ {
+ return "todo";
+ }
+
+ /// <summary>The get columns for table.</summary>
+ /// <param name="dbTable">The db table.</param>
+ /// <param name="schemaTableKeyInfo">The schema table key info.</param>
+ /// <param name="dbTypes">The db types.</param>
+ protected virtual void GetColumnsForTable(DbModelTable dbTable, DataTable schemaTableKeyInfo, Dictionary<string, DbModelType> dbTypes)
+ {
+ if (schemaTableKeyInfo == null)
+ {
+ return;
+ }
+
+ foreach (DataRow columnRow in schemaTableKeyInfo.Rows)
+ {
+ if (SafeGetBool(columnRow, "IsHidden"))
+ {
+ continue;
+ }
+
+ string columnName = SafeGetString(columnRow, "ColumnName");
+ string dataType = GetDataTypeNameForColumn(dbTable, schemaTableKeyInfo, columnRow);
+
+ // note - need a better work around for columns missing the data type info (e.g. access)
+ if (string.IsNullOrEmpty(dataType))
+ {
+ // try using the "ProviderDbType" to match
+ string providerDbType = SafeGetString(columnRow, "ProviderType");
+ foreach (var type in dbTypes.Values)
+ {
+ if (type.ProviderDbType == providerDbType)
+ {
+ dataType = type.Name;
+ break;
+ }
+ }
+ }
+
+ DbModelType dbType = DbModelType.Create(
+ dbTypes,
+ dataType,
+ SafeGetInt(columnRow, "ColumnSize"),
+ SafeGetInt(columnRow, "Precision"),
+ SafeGetInt(columnRow, "Scale"),
+ SafeGetString(columnRow, "DataType"));
+
+ // todo - FK info
+ DbModelColumn dbColumn = new DbModelColumn
+ {
+ Name = columnName,
+ // Name = MakeSqlFriendly(columnName),
+ Nullable = SafeGetBool(columnRow, "AllowDBNull"),
+ IsKey = SafeGetBool(columnRow, "IsKey"),
+ IsUnique = SafeGetBool(columnRow, "IsUnique"),
+ IsRowVersion = SafeGetBool(columnRow, "IsRowVersion"),
+ IsIdentity = SafeGetBool(columnRow, "IsIdentity"),
+ IsAutoIncrement = SafeGetBool(columnRow, "IsAutoIncrement"),
+ IsReadOnly = SafeGetBool(columnRow, "IsReadOnly"),
+ DbType = dbType,
+ };
+ dbTable.Add(dbColumn);
+ }
+ }
+
+ /// <summary>The get data type name for column.</summary>
+ /// <param name="dbTable">The db table.</param>
+ /// <param name="schemaTableKeyInfo">The schema table key info.</param>
+ /// <param name="columnRow">The column row.</param>
+ /// <returns>The get data type name for column.</returns>
+ protected virtual string GetDataTypeNameForColumn(DbModelTable dbTable, DataTable schemaTableKeyInfo, DataRow columnRow)
+ {
+ return SafeGetString(columnRow, "DataTypeName");
+ }
+
+ /// <summary>The get foreign key references for table.</summary>
+ /// <param name="dbConn">The db conn.</param>
+ /// <param name="dbTable">The db table.</param>
+ protected virtual void GetForeignKeyReferencesForTable(DbConnection dbConn, DbModelTable dbTable)
+ {
+ // foreach (DbModelColumn column in dbTable.Columns)
+ // {
+ // // KF info for DB's varies widley, needs to be implemented by derived class
+ // column.ForeignKeyReference = DbModelForeignKeyReference.NullForeignKeyReference;
+ // }
+ }
+
+ /// <summary>The get table key information.</summary>
+ /// <param name="dbConn">The database connection.</param>
+ /// <param name="schema">The schema.</param>
+ /// <param name="name">The name of the table.</param>
+ /// <returns>A <see cref="DataTable"/> describing the tables columns and key information.</returns>
+ protected virtual DataTable GetTableKeyInfo(DbConnection dbConn, string schema, string name)
+ {
+ DataTable schemaTableKeyInfo = null;
+ try
+ {
+ using (DbCommand command = dbConn.CreateCommand())
+ {
+ string tableName = Utility.RenderSafeSchemaObjectName(schema, name);
+ command.CommandText = "SELECT * FROM " + MakeSqlFriendly(tableName);
+ using (DbDataReader reader = command.ExecuteReader(CommandBehavior.SchemaOnly | CommandBehavior.KeyInfo))
+ {
+ schemaTableKeyInfo = reader.GetSchemaTable();
+ }
+ }
+ }
+ catch (Exception exp)
+ {
+ // Generic catch all - not all provoders stick to the DbException base:
+ Debug.WriteLine(GetType().FullName + " ERROR: " + exp.Message);
+ }
+
+ return schemaTableKeyInfo;
+ }
+
+ /// <summary>The make sql friendly.</summary>
+ /// <param name="name">The name.</param>
+ /// <returns>The make sql friendly.</returns>
+ protected virtual string MakeSqlFriendly(string name)
+ {
+ return Utility.MakeSqlFriendly(name);
+ }
+
+ /// <summary>The process foreign key references for table.</summary>
+ /// <param name="dbConn">The db conn.</param>
+ /// <param name="dbTable">The db table.</param>
+ protected virtual void ProcessForeignKeyReferencesForTable(DbConnection dbConn, DbModelTable dbTable)
+ {
+ }
+
+
+ /// <summary>The safe get bool.</summary>
+ /// <param name="row">The row.</param>
+ /// <param name="columnName">The column name.</param>
+ /// <returns>The safe get bool.</returns>
+ protected bool SafeGetBool(DataRow row, string columnName)
+ {
+ if (row.Table.Columns.Contains(columnName) && !row.IsNull(columnName))
+ {
+ string value = row[columnName].ToString();
+ switch (value.ToLower())
+ {
+ case "no":
+ case "false":
+ return false;
+
+ case "yes":
+ case "true":
+ return true;
+ }
+ }
+
+ return false;
+ }
+
+ /// <summary>The safe get int.</summary>
+ /// <param name="row">The row.</param>
+ /// <param name="columnName">The column name.</param>
+ /// <returns>The safe get int.</returns>
+ protected virtual int SafeGetInt(DataRow row, string columnName)
+ {
+ try
+ {
+ int result = -1;
+
+ if (row.Table.Columns.Contains(columnName) && !row.IsNull(columnName))
+ {
+ result = Convert.ToInt32(row[columnName]);
+ }
+
+ return result;
+ }
+ catch (OverflowException)
+ {
+ // In Oracle Maximum size for column is larger than Max Int32, instead of changing return value, just coerce on Max.Int32.
+
+ return Int32.MaxValue;
+
+ }
+
+ }
+
+ /// <summary>The safe get string.</summary>
+ /// <param name="row">The row.</param>
+ /// <param name="columnName">The column name.</param>
+ /// <returns>The safe get string.</returns>
+ protected string SafeGetString(DataRow row, string columnName)
+ {
+ string result = string.Empty;
+
+ if (row.Table.Columns.Contains(columnName) && !row.IsNull(columnName))
+ {
+ result = row[columnName].ToString();
+ }
+
+ return result;
+ }
+ }
+}
\ No newline at end of file
diff --git a/minisqlquery-master/src/MiniSqlQuery.Core/DbModel/ISqlWriter.cs b/minisqlquery-master/src/MiniSqlQuery.Core/DbModel/ISqlWriter.cs
new file mode 100644
index 0000000..fcb11da
--- /dev/null
+++ b/minisqlquery-master/src/MiniSqlQuery.Core/DbModel/ISqlWriter.cs
@@ -0,0 +1,67 @@
+#region License
+
+// Copyright 2005-2019 Paul Kohler (https://github.com/paulkohler/minisqlquery). All rights reserved.
+// This source code is made available under the terms of the GNU Lesser General Public License v3.0
+// https://github.com/paulkohler/minisqlquery/blob/master/LICENSE
+
+#endregion
+
+using System.IO;
+
+namespace MiniSqlQuery.Core.DbModel
+{
+ /// <summary>An SQL Writer interface.</summary>
+ public interface ISqlWriter
+ {
+ /// <summary>Gets or sets a value indicating whether IncludeComments.</summary>
+ /// <value>The include comments.</value>
+ bool IncludeComments { get; set; }
+
+ /// <summary>
+ /// Gets or sets a value indicating whether to include read-only columns in the export SQL.
+ /// </summary>
+ /// <value>
+ /// <c>true</c> if including read-only columns in the export; otherwise, <c>false</c>.
+ /// </value>
+ bool IncludeReadOnlyColumnsInExport { get; set; }
+
+ /// <summary>Gets or sets a value indicating whether InsertLineBreaksBetweenColumns.</summary>
+ /// <value>The insert line breaks between columns.</value>
+ bool InsertLineBreaksBetweenColumns { get; set; }
+
+ /// <summary>The write create.</summary>
+ /// <param name="writer">The writer.</param>
+ /// <param name="column">The column.</param>
+ void WriteCreate(TextWriter writer, DbModelColumn column);
+
+ /// <summary>The write delete.</summary>
+ /// <param name="writer">The writer.</param>
+ /// <param name="tableOrView">The table or view.</param>
+ void WriteDelete(TextWriter writer, DbModelTable tableOrView);
+
+ /// <summary>The write insert.</summary>
+ /// <param name="writer">The writer.</param>
+ /// <param name="tableOrView">The table or view.</param>
+ void WriteInsert(TextWriter writer, DbModelTable tableOrView);
+
+ /// <summary>The write select.</summary>
+ /// <param name="writer">The writer.</param>
+ /// <param name="tableOrView">The table or view.</param>
+ void WriteSelect(TextWriter writer, DbModelTable tableOrView);
+
+ /// <summary>The write select count.</summary>
+ /// <param name="writer">The writer.</param>
+ /// <param name="tableOrView">The table or view.</param>
+ void WriteSelectCount(TextWriter writer, DbModelTable tableOrView);
+
+ /// <summary>The write summary.</summary>
+ /// <param name="writer">The writer.</param>
+ /// <param name="column">The column.</param>
+ void WriteSummary(TextWriter writer, DbModelColumn column);
+
+ /// <summary>The write update.</summary>
+ /// <param name="writer">The writer.</param>
+ /// <param name="tableOrView">The table or view.</param>
+ void WriteUpdate(TextWriter writer, DbModelTable tableOrView);
+ }
+}
\ No newline at end of file
diff --git a/minisqlquery-master/src/MiniSqlQuery.Core/DbModel/OleDbSchemaService.cs b/minisqlquery-master/src/MiniSqlQuery.Core/DbModel/OleDbSchemaService.cs
new file mode 100644
index 0000000..9054018
--- /dev/null
+++ b/minisqlquery-master/src/MiniSqlQuery.Core/DbModel/OleDbSchemaService.cs
@@ -0,0 +1,43 @@
+#region License
+
+// Copyright 2005-2019 Paul Kohler (https://github.com/paulkohler/minisqlquery). All rights reserved.
+// This source code is made available under the terms of the GNU Lesser General Public License v3.0
+// https://github.com/paulkohler/minisqlquery/blob/master/LICENSE
+#endregion
+
+using System.Collections.Generic;
+using System.Data.Common;
+
+namespace MiniSqlQuery.Core.DbModel
+{
+ /// <summary>The ole db schema service.</summary>
+ public class OleDbSchemaService : GenericSchemaService
+ {
+ /// <summary>The get db types.</summary>
+ /// <param name="connection">The connection.</param>
+ /// <returns></returns>
+ public override Dictionary<string, DbModelType> GetDbTypes(DbConnection connection)
+ {
+ var types = base.GetDbTypes(connection);
+
+ foreach (var dbType in types.Values)
+ {
+ if (dbType.CreateFormat.Length == 0)
+ {
+ // probably MS Access
+ switch (dbType.Name)
+ {
+ case "VarChar":
+ dbType.CreateFormat = "VarChar({0})";
+ break;
+ case "VarBinary":
+ dbType.CreateFormat = "VarBinary({0})";
+ break;
+ }
+ }
+ }
+
+ return types;
+ }
+ }
+}
\ No newline at end of file
diff --git a/minisqlquery-master/src/MiniSqlQuery.Core/DbModel/OracleSchemaService.cs b/minisqlquery-master/src/MiniSqlQuery.Core/DbModel/OracleSchemaService.cs
new file mode 100644
index 0000000..74f9472
--- /dev/null
+++ b/minisqlquery-master/src/MiniSqlQuery.Core/DbModel/OracleSchemaService.cs
@@ -0,0 +1,101 @@
+using System;
+using System.Collections.Generic;
+using System.Data;
+using System.Data.Common;
+
+namespace MiniSqlQuery.Core.DbModel
+{
+ public class OracleSchemaService : GenericSchemaService
+ {
+
+ protected override int SafeGetInt(DataRow row, string columnName)
+ {
+ try
+ {
+ return base.SafeGetInt(row, columnName);
+ }
+ catch (OverflowException)
+ {
+ // Coerce to Max.Int32
+ return Int32.MaxValue;
+ }
+ }
+
+ public override DbModelInstance GetDbObjectModel(string connection)
+ {
+
+ //_connection = connection;
+
+ DbModelInstance model = new DbModelInstance();
+ DbProviderFactory factory = DbProviderFactories.GetFactory(ProviderName);
+
+ using (DbConnection dbConn = factory.CreateConnection())
+ {
+ dbConn.ConnectionString = connection;
+ dbConn.Open();
+
+ DataTable tables = dbConn.GetSchema("Tables");
+ Dictionary<string, DbModelType> dbTypes = GetDbTypes(dbConn);
+ model.Types = dbTypes;
+ model.ProviderName = ProviderName;
+ model.ConnectionString = connection;
+
+ if (tables == null)
+ {
+ return model;
+ }
+
+ DataView tablesDV = new DataView(tables, "", "OWNER, TABLE_NAME", DataViewRowState.CurrentRows);
+
+ foreach (DataRowView row in tablesDV)
+ {
+ string schemaName = SafeGetString(row.Row, "OWNER");
+ string tableName = SafeGetString(row.Row, "TABLE_NAME");
+
+ DbModelTable dbTable = new DbModelTable { Schema = schemaName, Name = tableName };
+ model.Add(dbTable);
+
+ DataTable schemaTableKeyInfo = GetTableKeyInfo(dbConn, schemaName, tableName);
+ GetColumnsForTable(dbTable, schemaTableKeyInfo, dbTypes);
+ }
+
+ DataTable views = dbConn.GetSchema("Views");
+ DataView viewsDV = new DataView(views, "", "OWNER, VIEW_NAME", DataViewRowState.CurrentRows);
+ foreach (DataRowView row in viewsDV)
+ {
+ string schemaName = SafeGetString(row.Row, "OWNER");
+ string tableName = SafeGetString(row.Row, "VIEW_NAME");
+
+ DbModelView dbTable = new DbModelView { Schema = schemaName, Name = tableName };
+ model.Add(dbTable);
+
+ DataTable schemaTableKeyInfo = GetTableKeyInfo(dbConn, schemaName, tableName);
+ GetColumnsForTable(dbTable, schemaTableKeyInfo, dbTypes);
+ }
+
+ // build FK relationships
+ if (model.Tables != null)
+ {
+ foreach (DbModelTable table in model.Tables)
+ {
+ GetForeignKeyReferencesForTable(dbConn, table);
+ ProcessForeignKeyReferencesForTable(dbConn, table);
+ }
+ }
+
+ // build FK relationships
+ if (model.Views != null)
+ {
+ foreach (DbModelView view in model.Views)
+ {
+ GetForeignKeyReferencesForTable(dbConn, view);
+ ProcessForeignKeyReferencesForTable(dbConn, view);
+ }
+ }
+ }
+
+ return model;
+ }
+
+ }
+}
diff --git a/minisqlquery-master/src/MiniSqlQuery.Core/DbModel/SqlCeSchemaService.cs b/minisqlquery-master/src/MiniSqlQuery.Core/DbModel/SqlCeSchemaService.cs
new file mode 100644
index 0000000..65fec8a
--- /dev/null
+++ b/minisqlquery-master/src/MiniSqlQuery.Core/DbModel/SqlCeSchemaService.cs
@@ -0,0 +1,323 @@
+#region License
+
+// Copyright 2005-2019 Paul Kohler (https://github.com/paulkohler/minisqlquery). All rights reserved.
+// This source code is made available under the terms of the GNU Lesser General Public License v3.0
+// https://github.com/paulkohler/minisqlquery/blob/master/LICENSE
+#endregion
+
+using System;
+using System.Collections.Generic;
+using System.Data;
+using System.Data.Common;
+
+namespace MiniSqlQuery.Core.DbModel
+{
+ /// <summary>SQL Compact Edition schema service.
+ /// Made possible with contributions form ExportSQLCE project.
+ /// http://exportsqlce.codeplex.com/
+ /// http://erikej.blogspot.com/</summary>
+ public class SqlCeSchemaService : GenericSchemaService
+ {
+ /// <summary>The ma x_ binar y_ colum n_ size.</summary>
+ internal static readonly int MAX_BINARY_COLUMN_SIZE = 8000;
+
+ /// <summary>The ma x_ imag e_ colum n_ size.</summary>
+ internal static readonly int MAX_IMAGE_COLUMN_SIZE = 1073741823;
+
+ /// <summary>The ma x_ ncha r_ colum n_ size.</summary>
+ internal static readonly int MAX_NCHAR_COLUMN_SIZE = 4000;
+
+ /// <summary>The ma x_ ntex t_ colum n_ size.</summary>
+ internal static readonly int MAX_NTEXT_COLUMN_SIZE = 536870911;
+
+ /// <summary>The _connection.</summary>
+ private string _connection;
+
+ /// <summary>The get db object model.</summary>
+ /// <param name="connection">The connection.</param>
+ /// <returns></returns>
+ public override DbModelInstance GetDbObjectModel(string connection)
+ {
+ _connection = connection;
+
+ DbModelInstance model = new DbModelInstance();
+ DbProviderFactory factory = DbProviderFactories.GetFactory(ProviderName);
+
+ using (DbConnection dbConn = factory.CreateConnection())
+ {
+ dbConn.ConnectionString = connection;
+ dbConn.Open();
+
+ QueryTableNames(dbConn, model);
+ Dictionary<string, DbModelType> dbTypes = GetDbTypes(dbConn);
+ model.Types = dbTypes;
+ model.ProviderName = ProviderName;
+ model.ConnectionString = _connection;
+
+ // build all table info
+ foreach (DbModelTable table in model.Tables)
+ {
+ DataTable schemaTableKeyInfo = GetTableKeyInfo(dbConn, null, table.Name);
+ GetColumnsForTable(table, schemaTableKeyInfo, dbTypes);
+ }
+
+ // build FK relationships
+ foreach (DbModelTable table in model.Tables)
+ {
+ GetForeignKeyReferencesForTable(dbConn, table);
+ ProcessForeignKeyReferencesForTable(dbConn, table);
+ }
+
+ return model;
+ }
+ }
+
+ /// <summary>Gets the db types for the SQL CE provider.</summary>
+ /// <param name="connection">The connection (not required).</param>
+ /// <returns></returns>
+ public override Dictionary<string, DbModelType> GetDbTypes(DbConnection connection)
+ {
+ Dictionary<string, DbModelType> dbTypes = new Dictionary<string, DbModelType>();
+ string dataTypesSql = "SELECT * FROM information_schema.provider_types";
+ using (var cmd = connection.CreateCommand())
+ {
+ cmd.CommandText = dataTypesSql;
+ cmd.CommandType = CommandType.Text;
+ using (var reader = cmd.ExecuteReader())
+ {
+ while (reader.Read())
+ {
+ string typeName = (string)reader["TYPE_NAME"];
+ int columnSize = Convert.ToInt32(reader["COLUMN_SIZE"]);
+ DbModelType dbType = new DbModelType(typeName, columnSize);
+
+ dbType.CreateParameters = Convert.ToString(reader["CREATE_PARAMS"]);
+ dbType.LiteralPrefix = Convert.ToString(reader["LITERAL_PREFIX"]);
+ dbType.LiteralSuffix = Convert.ToString(reader["LITERAL_SUFFIX"]);
+ dbType.ProviderDbType = Convert.ToString(reader["DATA_TYPE"]);
+
+ FixCreateFormat(dbType);
+ FixMaxLengths(dbType);
+ AssignSystemTypes(dbType);
+
+ dbTypes.Add(typeName, dbType);
+ }
+ }
+ }
+
+ return dbTypes;
+ }
+
+ /// <summary>The get data type name for column.</summary>
+ /// <param name="dbTable">The db table.</param>
+ /// <param name="schemaTableKeyInfo">The schema table key info.</param>
+ /// <param name="columnRow">The column row.</param>
+ /// <returns>The get data type name for column.</returns>
+ protected override string GetDataTypeNameForColumn(DbModelTable dbTable, DataTable schemaTableKeyInfo, DataRow columnRow)
+ {
+ return SafeGetString(columnRow, "ProviderType");
+ }
+
+ /// <summary>The get foreign key references for table.</summary>
+ /// <param name="dbConn">The db conn.</param>
+ /// <param name="dbTable">The db table.</param>
+ protected override void GetForeignKeyReferencesForTable(DbConnection dbConn, DbModelTable dbTable)
+ {
+ ForeignKeyInformationAvailable = true;
+ try
+ {
+ using (var cmd = dbConn.CreateCommand())
+ {
+ cmd.CommandText =
+ string.Format(
+ @"SELECT
+ KCU1.TABLE_NAME AS FK_TABLE_NAME,
+ KCU1.CONSTRAINT_NAME AS FK_CONSTRAINT_NAME,
+ KCU1.COLUMN_NAME AS FK_COLUMN_NAME,
+ KCU2.TABLE_NAME AS UQ_TABLE_NAME,
+ KCU2.CONSTRAINT_NAME AS UQ_CONSTRAINT_NAME,
+ KCU2.COLUMN_NAME AS UQ_COLUMN_NAME,
+ RC.UPDATE_RULE,
+ RC.DELETE_RULE,
+ KCU2.ORDINAL_POSITION AS UQ_ORDINAL_POSITION,
+ KCU1.ORDINAL_POSITION AS FK_ORDINAL_POSITION
+FROM
+ INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS RC
+ JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE KCU1 ON KCU1.CONSTRAINT_NAME = RC.CONSTRAINT_NAME
+ JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE KCU2 ON KCU2.CONSTRAINT_NAME = RC.UNIQUE_CONSTRAINT_NAME AND KCU2.ORDINAL_POSITION = KCU1.ORDINAL_POSITION AND KCU2.TABLE_NAME = RC.UNIQUE_CONSTRAINT_TABLE_NAME
+WHERE KCU1.TABLE_NAME = '{0}'
+ORDER BY
+ FK_TABLE_NAME,
+ FK_CONSTRAINT_NAME,
+ FK_ORDINAL_POSITION
+",
+ dbTable.Name);
+ cmd.CommandType = CommandType.Text;
+ using (var dr = cmd.ExecuteReader())
+ {
+ while (dr.Read())
+ {
+ dbTable.Constraints.Add(new DbModelConstraint
+ {
+ ConstraintTableName = dr.GetString(0),
+ ConstraintName = dr.GetString(1),
+ ColumnName = dr.GetString(2),
+ UniqueConstraintTableName = dr.GetString(3),
+ UniqueConstraintName = dr.GetString(4),
+ UniqueColumnName = dr.GetString(5),
+ UpdateRule = dr.GetString(6),
+ DeleteRule = dr.GetString(7)
+ });
+ }
+ }
+ }
+ }
+ catch (DbException)
+ {
+ ForeignKeyInformationAvailable = false;
+ }
+ }
+
+ /// <summary>The process foreign key references for table.</summary>
+ /// <param name="dbConn">The db conn.</param>
+ /// <param name="dbTable">The db table.</param>
+ protected override void ProcessForeignKeyReferencesForTable(DbConnection dbConn, DbModelTable dbTable)
+ {
+ // todo - check GetGroupForeingKeys
+ foreach (DbModelConstraint constraint in dbTable.Constraints)
+ {
+ var column = dbTable.Columns.Find(c => c.Name == constraint.ColumnName);
+ var refTable = dbTable.ParentDb.FindTable(
+ Utility.RenderSafeSchemaObjectName(constraint.UniqueConstraintTableSchema, constraint.UniqueConstraintTableName));
+ var refColumn = refTable.Columns.Find(c => c.Name == constraint.UniqueColumnName);
+ DbModelForeignKeyReference fk = new DbModelForeignKeyReference(column, refTable, refColumn);
+ fk.UpdateRule = constraint.UpdateRule;
+ fk.DeleteRule = constraint.DeleteRule;
+ column.ForeignKeyReference = fk;
+ }
+ }
+
+ /// <summary>The assign system types.</summary>
+ /// <param name="dbType">The db type.</param>
+ private void AssignSystemTypes(DbModelType dbType)
+ {
+ switch (dbType.Name.ToLower())
+ {
+ case "smallint":
+ dbType.SystemType = typeof(byte);
+ break;
+
+ case "int":
+ dbType.SystemType = typeof(int);
+ break;
+
+ case "tinyint":
+ dbType.SystemType = typeof(byte);
+ break;
+
+ case "bigint":
+ dbType.SystemType = typeof(long);
+ break;
+
+ case "float":
+ dbType.SystemType = typeof(double); // yes, float is double ;-)
+ break;
+
+ case "numeric":
+ case "money":
+ case "real":
+ dbType.SystemType = typeof(decimal);
+ break;
+
+ case "bit":
+ dbType.SystemType = typeof(bool);
+ break;
+
+ case "uniqueidentifier":
+ dbType.SystemType = typeof(Guid);
+ break;
+
+ case "nvarchar":
+ case "nchar":
+ case "ntext":
+ dbType.SystemType = typeof(string);
+ break;
+
+ case "datetime":
+ dbType.SystemType = typeof(DateTime);
+ break;
+
+ case "varbinary":
+ case "binary":
+ case "image":
+ case "rowversion":
+ dbType.SystemType = typeof(byte[]);
+ break;
+ }
+ }
+
+ /// <summary>The fix create format.</summary>
+ /// <param name="dbType">The db type.</param>
+ private void FixCreateFormat(DbModelType dbType)
+ {
+ switch (dbType.Name.ToLower())
+ {
+ case "nchar":
+ case "nvarchar":
+ case "binary":
+ case "varbinary":
+ dbType.CreateFormat = dbType.Name.ToLower() + "({0})";
+ break;
+
+ case "decimal":
+ case "numeric":
+ dbType.CreateFormat = dbType.Name.ToLower() + "({0}, {1})";
+ break;
+ }
+ }
+
+ /// <summary>The fix max lengths.</summary>
+ /// <param name="dbType">The db type.</param>
+ private void FixMaxLengths(DbModelType dbType)
+ {
+ switch (dbType.Name.ToLower())
+ {
+ case "nchar":
+ case "nvarchar":
+ dbType.Length = MAX_NCHAR_COLUMN_SIZE;
+ break;
+ case "ntext":
+ dbType.Length = MAX_NTEXT_COLUMN_SIZE;
+ break;
+ case "binary":
+ case "varbinary":
+ dbType.Length = MAX_BINARY_COLUMN_SIZE;
+ break;
+ case "image":
+ dbType.Length = MAX_IMAGE_COLUMN_SIZE;
+ break;
+ }
+ }
+
+ /// <summary>The query table names.</summary>
+ /// <param name="dbConn">The db conn.</param>
+ /// <param name="model">The model.</param>
+ private void QueryTableNames(DbConnection dbConn, DbModelInstance model)
+ {
+ using (var cmd = dbConn.CreateCommand())
+ {
+ cmd.CommandText = "SELECT table_name FROM information_schema.tables WHERE TABLE_TYPE = N'TABLE'";
+ cmd.CommandType = CommandType.Text;
+ using (var reader = cmd.ExecuteReader())
+ {
+ while (reader.Read())
+ {
+ DbModelTable table = new DbModelTable();
+ table.Name = (string)reader["table_name"];
+ model.Add(table);
+ }
+ }
+ }
+ }
+ }
+}
\ No newline at end of file
diff --git a/minisqlquery-master/src/MiniSqlQuery.Core/DbModel/SqlClientSchemaService.cs b/minisqlquery-master/src/MiniSqlQuery.Core/DbModel/SqlClientSchemaService.cs
new file mode 100644
index 0000000..13a98ac
--- /dev/null
+++ b/minisqlquery-master/src/MiniSqlQuery.Core/DbModel/SqlClientSchemaService.cs
@@ -0,0 +1,105 @@
+#region License
+
+// Copyright 2005-2019 Paul Kohler (https://github.com/paulkohler/minisqlquery). All rights reserved.
+// This source code is made available under the terms of the GNU Lesser General Public License v3.0
+// https://github.com/paulkohler/minisqlquery/blob/master/LICENSE
+#endregion
+
+using System.Collections.Generic;
+using System.Data;
+using System.Data.Common;
+using System.Data.SqlClient;
+
+namespace MiniSqlQuery.Core.DbModel
+{
+ /// <summary>The sql client schema service.</summary>
+ public class SqlClientSchemaService : GenericSchemaService
+ {
+ /// <summary>The get db types.</summary>
+ /// <param name="connection">The connection.</param>
+ /// <returns></returns>
+ public override Dictionary<string, DbModelType> GetDbTypes(DbConnection connection)
+ {
+ var types = base.GetDbTypes(connection);
+
+ var date = types["datetime"];
+ date.LiteralPrefix = "'";
+ date.LiteralSuffix = "'";
+
+ return types;
+ }
+
+ /// <summary>The get foreign key references for table.</summary>
+ /// <param name="dbConn">The db conn.</param>
+ /// <param name="dbTable">The db table.</param>
+ protected override void GetForeignKeyReferencesForTable(DbConnection dbConn, DbModelTable dbTable)
+ {
+ ForeignKeyInformationAvailable = true;
+ try
+ {
+ using (var cmd = dbConn.CreateCommand())
+ {
+ cmd.CommandText = string.Format(
+ @"SELECT
+ OBJECT_SCHEMA_NAME(f.parent_object_id) AS TableSchemaName,
+ OBJECT_NAME(f.parent_object_id) AS TableName,
+ COL_NAME(fc.parent_object_id, fc.parent_column_id) AS ColumnName,
+ f.name AS ForeignKeyName,
+ OBJECT_SCHEMA_NAME(f.referenced_object_id) AS ReferenceTableSchemaName,
+ OBJECT_NAME(f.referenced_object_id) AS ReferenceTableName,
+ COL_NAME(fc.referenced_object_id, fc.referenced_column_id) AS ReferenceColumnName,
+ f.update_referential_action_desc,
+ f.delete_referential_action_desc
+FROM
+ sys.foreign_keys AS f INNER JOIN sys.foreign_key_columns AS fc
+ ON f.OBJECT_ID = fc.constraint_object_id
+WHERE OBJECT_SCHEMA_NAME(f.parent_object_id) = '{0}' AND OBJECT_NAME(f.parent_object_id) = '{1}'
+",
+ dbTable.Schema, dbTable.Name);
+ cmd.CommandType = CommandType.Text;
+ using (var dr = cmd.ExecuteReader())
+ {
+ while (dr.Read())
+ {
+ dbTable.Constraints.Add(new DbModelConstraint
+ {
+ ConstraintTableSchema = (string)dr["TableSchemaName"],
+ ConstraintTableName = (string)dr["TableName"],
+ ColumnName = (string)dr["ColumnName"],
+ ConstraintName = (string)dr["ForeignKeyName"],
+ UniqueConstraintTableSchema = (string)dr["ReferenceTableSchemaName"],
+ UniqueConstraintTableName = (string)dr["ReferenceTableName"],
+ UniqueColumnName = (string)dr["ReferenceColumnName"],
+ UpdateRule = (string)dr["update_referential_action_desc"],
+ DeleteRule = (string)dr["delete_referential_action_desc"],
+ });
+ }
+ }
+ }
+ }
+ catch (SqlException)
+ {
+ ForeignKeyInformationAvailable = false;
+ }
+ }
+
+ /// <summary>The process foreign key references for table.</summary>
+ /// <param name="dbConn">The db conn.</param>
+ /// <param name="dbTable">The db table.</param>
+ protected override void ProcessForeignKeyReferencesForTable(DbConnection dbConn, DbModelTable dbTable)
+ {
+ // todo - check GetGroupForeingKeys
+ foreach (DbModelConstraint constraint in dbTable.Constraints)
+ {
+ var column = dbTable.Columns.Find(c => c.Name == constraint.ColumnName);
+ var refTable = dbTable.ParentDb.FindTable(
+ Utility.RenderSafeSchemaObjectName(constraint.UniqueConstraintTableSchema, constraint.UniqueConstraintTableName));
+ var refColumn = refTable.Columns.Find(c => c.Name == constraint.UniqueColumnName);
+ DbModelForeignKeyReference fk = new DbModelForeignKeyReference(column, refTable, refColumn);
+ fk.UpdateRule = constraint.UpdateRule;
+ fk.DeleteRule = constraint.DeleteRule;
+ column.ForeignKeyReference = fk;
+ }
+ }
+ }
+}
\ No newline at end of file
diff --git a/minisqlquery-master/src/MiniSqlQuery.Core/DbModel/SqlWriter.cs b/minisqlquery-master/src/MiniSqlQuery.Core/DbModel/SqlWriter.cs
new file mode 100644
index 0000000..9b2be5f
--- /dev/null
+++ b/minisqlquery-master/src/MiniSqlQuery.Core/DbModel/SqlWriter.cs
@@ -0,0 +1,261 @@
+#region License
+
+// Copyright 2005-2019 Paul Kohler (https://github.com/paulkohler/minisqlquery). All rights reserved.
+// This source code is made available under the terms of the GNU Lesser General Public License v3.0
+// https://github.com/paulkohler/minisqlquery/blob/master/LICENSE
+#endregion
+
+using System.Collections.Generic;
+using System.IO;
+
+namespace MiniSqlQuery.Core.DbModel
+{
+ /// <summary>The sql writer.</summary>
+ public class SqlWriter : ISqlWriter
+ {
+ /// <summary>Initializes a new instance of the <see cref="SqlWriter"/> class.</summary>
+ public SqlWriter()
+ {
+ // todo - format options?
+ IncludeComments = true;
+ InsertLineBreaksBetweenColumns = true;
+ }
+
+ /// <summary>Gets or sets a value indicating whether IncludeComments.</summary>
+ /// <value>The include comments.</value>
+ public bool IncludeComments { get; set; }
+
+ /// <summary>Gets or sets a value indicating whether InsertLineBreaksBetweenColumns.</summary>
+ /// <value>The insert line breaks between columns.</value>
+ public bool InsertLineBreaksBetweenColumns { get; set; }
+
+ /// <summary>
+ /// Gets or sets a value indicating whether to include read-only columns in the export SQL.
+ /// </summary>
+ /// <value>
+ /// <c>true</c> if including read-only columns in the export; otherwise, <c>false</c>.
+ /// </value>
+ public bool IncludeReadOnlyColumnsInExport { get; set; }
+
+ /// <summary>The write create.</summary>
+ /// <param name="writer">The writer.</param>
+ /// <param name="column">The column.</param>
+ public virtual void WriteCreate(TextWriter writer, DbModelColumn column)
+ {
+ writer.Write("{0} {1} ", MakeSqlFriendly(column.Name), column.DbType.Summary);
+
+ if (!column.Nullable)
+ {
+ writer.Write("not ");
+ }
+
+ writer.Write("null");
+ }
+
+ /// <summary>The write delete.</summary>
+ /// <param name="writer">The writer.</param>
+ /// <param name="tableOrView">The table or view.</param>
+ public virtual void WriteDelete(TextWriter writer, DbModelTable tableOrView)
+ {
+ writer.WriteLine("DELETE FROM");
+ writer.Write("\t");
+ writer.WriteLine(MakeSqlFriendly(tableOrView.FullName));
+ writer.WriteLine("WHERE");
+
+ for (int i = 0; i < tableOrView.PrimaryKeyColumns.Count; i++)
+ {
+ var column = tableOrView.PrimaryKeyColumns[i];
+ writer.Write("\t{0} = ", MakeSqlFriendly(column.Name));
+ if (i < tableOrView.PrimaryKeyColumns.Count - 1)
+ {
+ writer.Write(" /*value:{0}*/ AND", column.Name);
+ writer.WriteLine();
+ }
+ else
+ {
+ writer.Write("/*value:{0}*/", column.Name);
+ }
+ }
+
+ writer.WriteLine();
+ }
+
+ /// <summary>The write insert.</summary>
+ /// <param name="writer">The writer.</param>
+ /// <param name="tableOrView">The table or view.</param>
+ public virtual void WriteInsert(TextWriter writer, DbModelTable tableOrView)
+ {
+ writer.Write("INSERT INTO ");
+ writer.Write(MakeSqlFriendly(tableOrView.FullName));
+ if (InsertLineBreaksBetweenColumns)
+ {
+ writer.WriteLine();
+ writer.Write("\t");
+ }
+
+ writer.Write("(");
+
+ // get all columns that are "writable" including PKs that are not auto generated (unless specified)
+ List<DbModelColumn> writableColumns = null;
+ if (IncludeReadOnlyColumnsInExport)
+ {
+ writableColumns = tableOrView.Columns;
+ }
+ else
+ {
+ writableColumns = tableOrView.Columns.FindAll(c => c.IsWritable);
+ }
+
+ for (int i = 0; i < writableColumns.Count; i++)
+ {
+ var column = writableColumns[i];
+ writer.Write(MakeSqlFriendly(column.Name));
+ if (i < writableColumns.Count - 1)
+ {
+ if (InsertLineBreaksBetweenColumns)
+ {
+ writer.WriteLine(",");
+ writer.Write("\t");
+ }
+ else
+ {
+ writer.Write(", ");
+ }
+ }
+ }
+
+ writer.WriteLine(")");
+ writer.Write("VALUES");
+ if (InsertLineBreaksBetweenColumns)
+ {
+ writer.WriteLine();
+ writer.Write("\t");
+ }
+
+ writer.Write("(");
+
+ for (int i = 0; i < writableColumns.Count; i++)
+ {
+ var column = writableColumns[i];
+ writer.Write(column.DbType.ToDDLValue(column.Nullable));
+ if (IncludeComments)
+ {
+ writer.Write(" /*{0},{1}*/", column.Name, column.DbType.Summary);
+ }
+
+ if (i < writableColumns.Count - 1)
+ {
+ if (InsertLineBreaksBetweenColumns)
+ {
+ writer.WriteLine(",");
+ writer.Write("\t");
+ }
+ else
+ {
+ writer.Write(", ");
+ }
+ }
+ }
+
+ writer.WriteLine(")");
+ }
+
+ /// <summary>The write select.</summary>
+ /// <param name="writer">The writer.</param>
+ /// <param name="tableOrView">The table or view.</param>
+ public virtual void WriteSelect(TextWriter writer, DbModelTable tableOrView)
+ {
+ writer.Write("SELECT");
+ writer.WriteLine();
+ for (int i = 0; i < tableOrView.Columns.Count; i++)
+ {
+ writer.Write("\t");
+ writer.Write(MakeSqlFriendly(tableOrView.Columns[i].Name));
+ if (i < tableOrView.Columns.Count - 1)
+ {
+ writer.Write(",");
+ writer.WriteLine();
+ }
+ }
+
+ writer.WriteLine();
+ writer.Write("FROM {0}", MakeSqlFriendly(tableOrView.FullName));
+ writer.WriteLine();
+ }
+
+ /// <summary>The write select count.</summary>
+ /// <param name="writer">The writer.</param>
+ /// <param name="tableOrView">The table or view.</param>
+ public virtual void WriteSelectCount(TextWriter writer, DbModelTable tableOrView)
+ {
+ writer.Write("SELECT COUNT(*) FROM {0}", MakeSqlFriendly(tableOrView.FullName));
+ writer.WriteLine();
+ }
+
+ /// <summary>The write summary.</summary>
+ /// <param name="writer">The writer.</param>
+ /// <param name="column">The column.</param>
+ public void WriteSummary(TextWriter writer, DbModelColumn column)
+ {
+ writer.Write("{0} ({1} ", MakeSqlFriendly(column.Name), column.DbType.Summary);
+
+ if (!column.Nullable)
+ {
+ writer.Write("not ");
+ }
+
+ writer.Write("null)");
+ }
+
+ /// <summary>The write update.</summary>
+ /// <param name="writer">The writer.</param>
+ /// <param name="tableOrView">The table or view.</param>
+ public virtual void WriteUpdate(TextWriter writer, DbModelTable tableOrView)
+ {
+ writer.Write("UPDATE ");
+ writer.WriteLine(MakeSqlFriendly(tableOrView.FullName));
+ writer.WriteLine("SET");
+
+ // get all columns that are "writable" excluding keys that are not auto generated
+ var writableColumns = tableOrView.Columns.FindAll(c => c.IsWritable && !c.IsKey);
+ for (int i = 0; i < writableColumns.Count; i++)
+ {
+ var column = writableColumns[i];
+ writer.Write("\t{0} = {1}", MakeSqlFriendly(column.Name), column.DbType.ToDDLValue(column.Nullable));
+ if (i < writableColumns.Count - 1)
+ {
+ writer.Write(",");
+ writer.WriteLine();
+ }
+ }
+
+ writer.WriteLine();
+ writer.WriteLine("WHERE");
+
+ for (int i = 0; i < tableOrView.PrimaryKeyColumns.Count; i++)
+ {
+ var column = tableOrView.PrimaryKeyColumns[i];
+ writer.Write("\t{0} = ", MakeSqlFriendly(column.Name));
+ if (i < tableOrView.PrimaryKeyColumns.Count - 1)
+ {
+ writer.Write(" /*value:{0},{1}*/ AND", column.Name, column.DbType.Summary);
+ writer.WriteLine();
+ }
+ else
+ {
+ writer.Write("/*value:{0},{1}*/", column.Name, column.DbType.Summary);
+ }
+ }
+
+ writer.WriteLine();
+ }
+
+ /// <summary>The make the sql friendly, e.g. "[TableName]".</summary>
+ /// <param name="name">The name of the object.</param>
+ /// <returns>The make sql friendly name.</returns>
+ protected string MakeSqlFriendly(string name)
+ {
+ return Utility.MakeSqlFriendly(name);
+ }
+ }
+}
\ No newline at end of file