コード例 #1
0
        public static IDb Create(Common.DbConfigDatabaseInfo dbConfig, Common.DbExecuteSqlEvent dbExecuteSqlEvent)
        {
            IDb db = Create(dbConfig);

            if (db != null && dbExecuteSqlEvent != null)
            {
                db.OnDbExecuteSqlEvent += dbExecuteSqlEvent;
            }
            return(db);
        }
コード例 #2
0
 public SingleTableGeneratedEventArgs(Common.DbConfigDatabaseInfo db, Common.DbDataTableInfo table, Config generatorConfig, string entityClassFullName, string entityClassPath, int tableCount, int tableNum)
 {
     this.db                  = db;
     this.table               = table;
     this.generatorConfig     = generatorConfig;
     this.entityClassFullName = entityClassFullName;
     this.entityClassPath     = entityClassPath;
     this.tableCount          = tableCount;
     this.tableNum            = tableNum;
 }
コード例 #3
0
        public static IDb Create(Common.DbConfigDatabaseInfo dbConfig)
        {
            IDb db = null;

            switch (dbConfig.dbType)
            {
            case "SqlServer":
                db = new SqlServer.Db(dbConfig);
                break;

            case "MySql":
                db = new MySql.Db(dbConfig);
                break;

            case "Sqlite":
                db = new Sqlite.Db(dbConfig);
                break;
            }
            return(db);
        }
コード例 #4
0
        private void Builder(Common.DbConfigDatabaseInfo db, List <Common.DbDataTableInfo> tables, Common.DbConfigDatabaseInfo dbInfo, string entityProjectRootDir, string targetProjectRootDir, string nameSpace, string entityTemplate = "")
        {
            string entityFileSaveDir = System.IO.Path.Combine(entityProjectRootDir, dbInfo.dbKey);
            string dbConfigFilePath  = System.IO.Path.Combine(targetProjectRootDir, "ZeroDbConfig.xml");

            if (!System.IO.Directory.Exists(entityFileSaveDir))
            {
                System.IO.Directory.CreateDirectory(entityFileSaveDir);
            }
            if (!System.IO.Directory.Exists(targetProjectRootDir))
            {
                System.IO.Directory.CreateDirectory(targetProjectRootDir);
            }
            if (!nameSpace.EndsWith("." + dbInfo.dbKey))
            {
                nameSpace += "." + dbInfo.dbKey;
            }
            entityTemplate = string.IsNullOrEmpty(entityTemplate) ? template : entityTemplate;
            if (string.IsNullOrEmpty(entityTemplate))
            {
                throw new Exception("template Is Null Or Empty");
            }
            System.Text.RegularExpressions.Match match = System.Text.RegularExpressions.Regex.Match(
                entityTemplate,
                @"%ColumnRepeatBegin%(?<temp>[\s\S]+)%ColumnRepeatEnd%",
                System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            if (!match.Success)
            {
                throw new Exception("template 缺少 ColumnLoop 片段");
            }
            string ColumnLoopTemplate = match.Groups["temp"].Value;

            if (string.IsNullOrEmpty(ColumnLoopTemplate))
            {
                throw new Exception("template 的 ColumnLoop 片段为空");
            }

            #region -- ZeroDbConfig.xml --
            if (!System.IO.File.Exists(dbConfigFilePath))
            {
                string s = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>" + System.Environment.NewLine
                           + "<zero></zero>";
                System.IO.File.AppendAllText(dbConfigFilePath, s, Encoding.UTF8);
            }
            System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
            doc.Load(dbConfigFilePath);
            System.Xml.XmlNode zeroNode = doc.SelectSingleNode(@"/zero");
            System.Xml.XmlNode nodeDbs  = doc.SelectSingleNode(@"/zero/dbs");
            bool hasNodeDbs             = nodeDbs != null;
            if (!hasNodeDbs)
            {
                nodeDbs = doc.CreateElement("dbs");
            }
            System.Xml.XmlNode nodeDvs = doc.SelectSingleNode(@"/zero/dvs");
            bool hasNodeDvs            = nodeDvs != null;
            if (!hasNodeDvs)
            {
                nodeDvs = doc.CreateElement("dvs");
            }
            System.Xml.XmlNode nodeDb = doc.SelectSingleNode(@"/zero/dbs/db[@dbKey='" + dbInfo.dbKey + "']");
            if (nodeDb == null)
            {
                nodeDb = doc.CreateElement("db");
                System.Xml.XmlAttribute attribute = doc.CreateAttribute("dbKey");
                attribute.Value = dbInfo.dbKey;
                nodeDb.Attributes.Append(attribute);
                attribute       = doc.CreateAttribute("dbConnectionString");
                attribute.Value = dbInfo.dbConnectionString;
                nodeDb.Attributes.Append(attribute);
                attribute       = doc.CreateAttribute("dbType");
                attribute.Value = dbInfo.dbType;
                nodeDb.Attributes.Append(attribute);
                nodeDbs.AppendChild(nodeDb);
            }
            #endregion

            int tableIndex    = 0;
            int myTablesCount = tables.Count;

            #region -- tables foreach --
            foreach (var table in tables)
            {
                string claText      = entityTemplate;
                string tDescription = table.Description;
                if (string.IsNullOrEmpty(tDescription))
                {
                    if (table.IsView)
                    {
                        tDescription = "VIEW:" + table.Name;
                    }
                    else
                    {
                        tDescription = "TABLE:" + table.Name;
                    }
                }
                claText = claText.Replace("%NameSpace%", nameSpace);
                claText = claText.Replace("%TableDescription%", tDescription);
                string className = System.Text.RegularExpressions.Regex.Replace(table.Name, @"^(t|v|tb|view)_", "", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
                className = FirstLetterToUpper(className);
                className = (table.IsView ? "v" : "t") + className;
                claText   = claText.Replace("%ClassName%", className);

                StringBuilder colsText = new StringBuilder();
                foreach (var column in table.Colunms)
                {
                    #region -- code --
                    string colText           = ColumnLoopTemplate;
                    string colDescription    = column.Description;
                    string colDefaultValue   = column.DefaultValue;
                    string colDotNetDataType = column.Type;
                    if (string.IsNullOrEmpty(colDescription))
                    {
                        colDescription = column.Name;
                    }
                    if (column.IsPrimaryKey)
                    {
                        colDescription = "[PrimaryKey]" + colDescription;
                    }
                    if (column.IsIdentity)
                    {
                        colDescription = "[Identity]" + colDescription;
                    }
                    if (string.IsNullOrEmpty(colDefaultValue))
                    {
                        colDefaultValue = "";
                    }
                    if (colDefaultValue.Length > 0)
                    {
                        colDefaultValue = " = " + colDefaultValue;
                    }
                    if (column.IsNullable)
                    {
                        if (colDotNetDataType != "string" &&
                            colDotNetDataType != "byte[]" &&
                            colDotNetDataType != "object" &&
                            colDotNetDataType != "object*" &&
                            colDotNetDataType != "Xml")
                        {
                            colDotNetDataType = colDotNetDataType + "?";
                        }
                    }
                    else
                    {
                        if (colDotNetDataType == "string")
                        {
                            if (colDefaultValue.Length < 1)
                            {
                                colDefaultValue = " = \"\"";
                            }
                        }
                    }

                    colText = colText.Replace("%ColumnDotNetType%", colDotNetDataType);
                    colText = colText.Replace("%ColumnName%", FirstLetterToUpper(column.Name));
                    colText = colText.Replace("%ColumnDotNetDefaultValue%", colDefaultValue);
                    colText = colText.Replace("%ColumnDescription%", colDescription);

                    colsText.Append(colText);

                    #endregion
                }

                claText = System.Text.RegularExpressions.Regex.Replace(
                    claText,
                    @"%ColumnRepeatBegin%(?<temp>[\s\S]+)%ColumnRepeatEnd%",
                    colsText.ToString());

                string filePath = System.IO.Path.Combine(entityFileSaveDir, className + ".cs");
                if (System.IO.File.Exists(filePath))
                {
                    System.IO.File.Delete(filePath);
                }
                System.IO.File.AppendAllText(filePath, claText, Encoding.UTF8);

                #region -- ZeroDbConfig.xml: /zero/dvs/dv --
                string             xpath  = @"/zero/dvs/dv[@entityKey='" + nameSpace + "." + className + "' and @dbKey='" + dbInfo.dbKey + "']";
                System.Xml.XmlNode nodeDv = doc.SelectSingleNode(xpath);
                if (nodeDv == null)
                {
                    nodeDv = doc.CreateElement("dv");
                    System.Xml.XmlAttribute attribute = doc.CreateAttribute("dbKey");
                    attribute.Value = dbInfo.dbKey;
                    nodeDv.Attributes.Append(attribute);

                    attribute       = doc.CreateAttribute("tableName");
                    attribute.Value = tables[tableIndex].Name;
                    nodeDv.Attributes.Append(attribute);

                    attribute       = doc.CreateAttribute("entityKey");
                    attribute.Value = nameSpace + "." + className;
                    nodeDv.Attributes.Append(attribute);

                    nodeDvs.AppendChild(nodeDv);
                }
                #endregion

                tableIndex++;

                if (OnSingleTableGenerated != null)
                {
                    OnSingleTableGenerated(new SingleTableGeneratedEventArgs(db, table, GeneratorConfig, nameSpace + "." + className, filePath, myTablesCount, tableIndex));
                }
            }
            #endregion

            if (!hasNodeDbs)
            {
                zeroNode.AppendChild(nodeDbs);
            }
            if (!hasNodeDvs)
            {
                zeroNode.AppendChild(nodeDvs);
            }
            doc.Save(dbConfigFilePath);
        }
コード例 #5
0
 public Db(Common.DbConfigDatabaseInfo dbConfigDatabaseInfo)
 {
     this.dbConfigDatabaseInfo = dbConfigDatabaseInfo;
     this.dbDataTypeMaping     = new DbDataTypeMaping();
     this.dbSqlBuilder         = new DbSqlBuilder(this);
 }