Beispiel #1
0
        private void ExportDataContractClass(int version, TableNameData tnd)
        {
            DataTable dt          = tnd.Data;
            bool      allowDbNull = cmd.Has("NULL");

            string[] keys = cmd.Columns;

            DataColumn[] pk = dt.PrimaryKey;
            if (pk == null || pk.Length == 0)
            {
                pk = dt.PrimaryKeys(keys);

                if (pk.Length == 0)
                {
                    dt.PrimaryKey = new DataColumn[] { dt.Columns[0] };
                    cout.WriteLine($"no primary key found on Table: \"{dt.TableName}\"");
                }

                dt.PrimaryKey = pk;
            }


            TheClassBuilder gen = null;

            if (version == 0)
            {
                gen = new DataContractClassBuilder(cmd, tnd.Name, dt, allowDbNull);
            }
            else if (version == 1)
            {
                gen = new DataContract1ClassBuilder(cmd, tnd.Name, dt, allowDbNull);
            }
            else
            {
                gen = new DataContract2ClassBuilder(cmd, tnd.Name, dt, allowDbNull);
            }

            if (gen != null)
            {
                string path = cmd.OutputPath(ConfigKey._GENERATOR_DC_PATH, $"{ConfigurationEnvironment.MyDocuments}\\dc");
                string ns   = cmd.GetValue("ns", ConfigKey._GENERATOR_DC_NS, "Sys.DataModel.DataContract");
                string mtd  = cmd.GetValue("method");

                gen.SetNamespace(ns);
                gen.SetClassName(dt.TableName);
                gen.SetMethod(mtd);
                string file = gen.WriteFile(path);
                cout.WriteLine("code generated on {0}", file);
            }

            TableSchemaCache.Clear();
        }
Beispiel #2
0
        public void ExportDataContract(Command cmd, int version)
        {
            DataTable dt = null;
            if (SqlShell.LastResult is DataTable)
            {
                dt = SqlShell.LastResult as DataTable;
            }

            if (dt == null)
            {
                if (tname != null)
                {
                    dt = new SqlCmd(tname.Provider, $"SELECT TOP 1 * FROM {tname.FormalName}").FillDataTable();
                }
                else
                {
                    stdio.ErrorFormat("data table cannot find, use command type or select first");
                    return;
                }
            }

            string path = cmd.GetValue("out") ?? cfg.GetValue<string>("dc.path", $"{Configuration.MyDocuments}\\dc");
            string ns = cmd.GetValue("ns") ?? cfg.GetValue<string>("dc.ns", "Sys.DataModel.DataContracts");
            string clss = cmd.GetValue("class") ?? cfg.GetValue<string>("dc.class", "DataContract");
            string mtd = cmd.GetValue("method");
            string[] keys = cmd.Columns;

            if (version == 1)
            {
                var builder = new DataContractClassBuilder(ns, cmd, dt)
                {
                    cname = clss,
                    mtd = mtd,
                    keys = keys
                };

                string file = builder.WriteFile(path);
                stdio.WriteLine("code generated on {0}", file);
            }
            else
            {
                var builder = new DataContract2ClassBuilder(ns, cmd, dt)
                {
                    cname = clss,
                    mtd = mtd
                };

                string file = builder.WriteFile(path);
                stdio.WriteLine("code generated on {0}", file);
            }
        }