aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Tango.CodeGeneration/EntityDTOCodeFile.cs
blob: eb97dcd9097c4531c457d5c192ca4326ea85d4ed (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Tango.CodeGeneration
{
    /// <summary>
    /// Represents a database entity DTO code file.
    /// </summary>
    /// <seealso cref="Tango.CodeGeneration.Class" />
    public class EntityDTOCodeFile : Class
    {
        public String ObservableType { get; set; }

        public String InheritedType { get; set; }
    }
}
*/ .highlight .vc { color: #336699 } /* Name.Variable.Class */ .highlight .vg { color: #dd7700 } /* Name.Variable.Global */ .highlight .vi { color: #3333bb } /* Name.Variable.Instance */ .highlight .vm { color: #336699 } /* Name.Variable.Magic */ .highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Tango.Core;
using Tango.DAL.Remote.DB;
using Tango.Settings;
using Tango.Synchronization;
using Tango.Synchronization.Conversion;

namespace Tango.SQLiteGenerator.CLI
{
    public enum Config
    {
        Default,
        CopyAll,
    }

    class Program
    {
        static void Main(string[] args)
        {
            Console.Title = "Tango SQLite Generator";

#if DEBUG
            args = new string[]
            {
                    Path.GetFullPath( "..\\..\\..\\..\\DB\\Tango.db"),
                    "CopyAll"
            };
#endif

            String sqlitePath = args[0];
            Config conf = args.Length > 1 ? (Config)Enum.Parse(typeof(Config), args[1]) : Config.Default;

            sqlitePath = Path.GetFullPath(sqlitePath);

            Console.WriteLine("Generating " + sqlitePath + "...");

            bool completed = false;

            String connectionString = SettingsManager.Default.GetOrCreate<CoreSettings>().DataSource.ToConnection().ConnectionString;

            Console.WriteLine("Connecting to " + connectionString + "...");

            connectionString = String.Format("Data Source={0};Initial Catalog=Tango;Integrated Security=SSPI;", connectionString);

            List<SYNC_CONFIGURATIONS> sync_configurations = new List<SYNC_CONFIGURATIONS>();

            using (RemoteDB db = new RemoteDB(SettingsManager.Default.GetOrCreate<CoreSettings>().DataSource))
            {
                sync_configurations = db.SYNC_CONFIGURATIONS.ToList();
            }

            SqlConversionHandler handler = new SqlConversionHandler(delegate (bool done, bool success, int percent, string msg)
            {
                Console.WriteLine(String.Format("{0}  [%{1}]", msg, percent));

                if (done)
                {
                    if (success)
                    {
                        Console.WriteLine("SQLite database has been generated successfully on " + sqlitePath);
                        Environment.Exit(0);
                    }
                    else
                    {
                        Console.WriteLine("SQLite database generation failed!");
                        Environment.Exit(-1);
                    }

                    completed = true;
                }
            });

            SqlTableSelectionHandler selectionHandler = new SqlTableSelectionHandler(delegate (List<TableSchema> schema)
            {
                List<TableSchema> updated = schema.Where(x => x.TableName.ToLower() != "sysdiagrams").ToList();

                if (conf == Config.Default)
                {
                    updated
                    .Where(table => sync_configurations.Where(config => (SyncConfiguration)config.SYNC_TYPE == SyncConfiguration.OverwriteLocal).ToList()
                    .Exists(config => config.TABLE_NAME == table.TableName)).ToList().ForEach(x => x.CopyData = true);
                }
                else if (conf == Config.CopyAll)
                {
                    updated.ForEach(x => x.CopyData = true);
                }

                return updated;
            });

            SqlServerToSQLiteConverter converter = new SqlServerToSQLiteConverter();
            converter.GenerateForeignKeys = true;
            converter.GenerateIndexes = true;

            converter.ConvertSqlServerToSQLiteDatabase(connectionString, sqlitePath, null, handler, selectionHandler, null, false, false);

            while (!completed)
            {
                Console.ReadLine();
            }
        }
    }
}