Example #1
0
        public int SetMagicConnection(string methodName, ref string csharpCode, string appRoot)
        {
            string          conParttern = @"Models\s*.\s*([_a-zA-Z0-9]+)\s*.\s*(Open|Close)Connection\s*\(\s*\)\s*;";
            Regex           reg         = new Regex(conParttern);
            MatchCollection mac         = reg.Matches(csharpCode);
            string          dataName    = null;

            NFinal.DB.ConnectionString connectionString = null;
            string fileName = null;

            VTemplate.Engine.TemplateDocument doc = null;
            string conCode           = null;
            int    relative_position = 0;

            for (int i = 0; i < mac.Count; i++)
            {
                dataName = mac[i].Groups[1].Value;
                //查找出sql语句中用的是哪个数据库
                connectionString = GetConnectionString(dataName);
                if (mac[i].Groups[2].Value == "Open")
                {
                    fileName = appRoot +
                               "NFinal\\SqlTemplate\\" +
                               connectionString.type.ToString() +
                               "\\Open.txt";
                    doc = new VTemplate.Engine.TemplateDocument(
                        fileName, System.Text.Encoding.UTF8);
                    doc.SetValue("functionName", methodName);
                    doc.SetValue("dbName", dataName);
                    conCode            = doc.GetRenderText();
                    relative_position += Replace(ref csharpCode, mac[i].Index + relative_position, mac[i].Length, conCode);
                }
                else
                {
                    fileName = appRoot +
                               "NFinal\\SqlTemplate\\" +
                               connectionString.type.ToString() +
                               "\\Close.txt";
                    doc = new VTemplate.Engine.TemplateDocument(
                        fileName, System.Text.Encoding.UTF8);
                    doc.SetValue("functionName", methodName);
                    doc.SetValue("dbName", dataName);
                    conCode            = doc.GetRenderText();
                    relative_position += Replace(ref csharpCode, mac[i].Index + relative_position, mac[i].Length, conCode);
                }
            }
            return(relative_position);
        }
Example #2
0
        /// <summary>
        /// 数据库函数替换类
        /// </summary>
        /// <param name="methodName">函数名</param>
        /// <param name="dbFunctionData">函数信息</param>
        /// <param name="appRoot">网站根目录</param>
        /// <returns></returns>
        public string SetMagicStruct(string methodName, DbFunctionData dbFunctionData, List <NFinal.Compile.StructField> structFieldList, string appRoot)
        {
            string result = null;

            if (dbFunctionData.functionName == "QueryAll" || dbFunctionData.functionName == "QueryRow" ||
                dbFunctionData.functionName == "Page" || dbFunctionData.functionName == "QueryRandom" ||
                dbFunctionData.functionName == "QueryTop")
            {
                NFinal.DB.ConnectionString connectionString = null;
                connectionString = GetConnectionString(dbFunctionData.connectionName);
                string type     = connectionString.type.ToString();
                string fileName = appRoot + "NFinal\\SqlTemplate\\" +
                                  type +
                                  "\\Struct.txt";
                VTemplate.Engine.TemplateDocument doc = new VTemplate.Engine.TemplateDocument(fileName, System.Text.Encoding.UTF8);
                doc.SetValue("functionName", methodName);
                doc.SetValue("varName", dbFunctionData.varName);
                doc.SetValue("dbType", type);
                doc.SetValue("fields", dbFunctionData.fields);
                doc.SetValue("structFields", structFieldList);
                result = doc.GetRenderText();
            }
            return(result);
        }
Example #3
0
        public void GetDB()
        {
            //获取WebConfig中的连接字符串信息
            string configFileName = appRoot + "Web.config";

            if (File.Exists(configFileName))
            {
                System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
                doc.Load(configFileName);
                System.Xml.XmlNode     root     = doc.DocumentElement.SelectSingleNode("/configuration");
                System.Xml.XmlNodeList nodeList = root.SelectNodes("connectionStrings/add[@connectionString]");
                Frame.ConnectionStrings.Clear();
                if (nodeList.Count > 0)
                {
                    System.Xml.XmlElement ele = null;
                    foreach (System.Xml.XmlNode node in nodeList)
                    {
                        ele = node as System.Xml.XmlElement;
                        var connectionString = new NFinal.DB.ConnectionString();
                        connectionString.name     = ele.Attributes["name"].Value;
                        connectionString.value    = ele.Attributes["connectionString"].Value;
                        connectionString.provider = ele.Attributes["providerName"].Value;

                        if (connectionString.provider.ToLower().IndexOf("mysql") > -1)
                        {
                            connectionString.type = NFinal.DB.DBType.MySql;
                        }
                        else if (connectionString.provider.ToLower().IndexOf("sqlclient") > -1)
                        {
                            connectionString.type = NFinal.DB.DBType.SqlServer;
                        }
                        else if (connectionString.provider.ToLower().IndexOf("sqlite") > -1)
                        {
                            connectionString.type = NFinal.DB.DBType.Sqlite;
                        }
                        else if (connectionString.provider.ToLower().IndexOf("oracle") > -1)
                        {
                            connectionString.type = NFinal.DB.DBType.Oracle;
                        }
                        else
                        {
                            connectionString.type = NFinal.DB.DBType.Unknown;
                        }
                        Frame.ConnectionStrings.Add(connectionString);
                    }
                }
            }
            //读取数据库信息
            NFinal.DB.Coding.DataUtility dataUtility = null;
            if (Frame.ConnectionStrings.Count > 0)
            {
                NFinal.DB.ConnectionString conStr;
                NFinal.DB.Coding.DB.DbStore.Clear();
                for (int i = 0; i < Frame.ConnectionStrings.Count; i++)
                {
                    conStr = Frame.ConnectionStrings[i];
                    if (conStr.type == NFinal.DB.DBType.MySql)
                    {
                        dataUtility = new NFinal.DB.Coding.MySQLDataUtility(conStr.value);
                        dataUtility.GetAllTables(dataUtility.con.Database);
                        NFinal.DB.Coding.DB.DbStore.Add(conStr.name, dataUtility);
                    }
                    else if (conStr.type == NFinal.DB.DBType.Sqlite)
                    {
                        dataUtility = new NFinal.DB.Coding.SQLiteDataUtility(conStr.value);
                        dataUtility.GetAllTables(dataUtility.con.Database);
                        NFinal.DB.Coding.DB.DbStore.Add(conStr.name, dataUtility);
                    }
                    else if (conStr.type == NFinal.DB.DBType.SqlServer)
                    {
                        dataUtility = new NFinal.DB.Coding.SQLDataUtility(conStr.value);
                        dataUtility.GetAllTables(dataUtility.con.Database);
                        NFinal.DB.Coding.DB.DbStore.Add(conStr.name, dataUtility);
                    }
                    else if (conStr.type == NFinal.DB.DBType.Oracle)
                    {
#if NET2
#else
                        dataUtility = new NFinal.DB.Coding.OracleDataUtility(conStr.value);
                        dataUtility.GetAllTables(dataUtility.con.Database);
                        NFinal.DB.Coding.DB.DbStore.Add(conStr.name, dataUtility);
#endif
                    }
                }
            }
        }
Example #4
0
        /// <summary>
        /// 执行数据库魔法函数
        /// </summary>
        /// <param name="methodName">Controller的函数名</param>
        /// <param name="csharpFileCode">Controller的函数内的代码</param>
        /// <param name="dbFunctionDatas">代码内分析出的魔法函数</param>
        /// <param name="appRoot">项目的根目录</param>
        /// <returns></returns>
        public int SetMagicFunction(string methodName, ref string csharpFileCode, int relative_position, List <DbFunctionData> dbFunctionDatas, string appRoot)
        {
            if (dbFunctionDatas.Count > 0)
            {
                string webCsharpCode = "";
                VTemplate.Engine.TemplateDocument doc = null;
                NFinal.DB.ConnectionString        connectionString = null;
                string fileName = null;

                for (int i = 0; i < dbFunctionDatas.Count; i++)
                {
                    connectionString = GetConnectionString(dbFunctionDatas[i].connectionName);
                    if (connectionString != null)
                    {
                        if (dbFunctionDatas[i].functionName == "QueryAll")
                        {
                            fileName = appRoot +
                                       "NFinal\\SqlTemplate\\" +
                                       connectionString.type.ToString() +
                                       "\\QueryAll.txt";
                            doc = new VTemplate.Engine.TemplateDocument(
                                fileName, System.Text.Encoding.UTF8);
                            doc.SetValue("functionName", methodName);
                            doc.SetValue("varName", dbFunctionDatas[i].varName);
                            doc.SetValue("dbName", dbFunctionDatas[i].connectionName);
                            doc.SetValue("sql", dbFunctionDatas[i].sql);
                            doc.SetValue("fields", dbFunctionDatas[i].fields);
                            doc.SetValue("sqlVarParameters", dbFunctionDatas[i].sqlVarParameters);
                            webCsharpCode      = doc.GetRenderText();
                            relative_position += Replace(ref csharpFileCode,
                                                         relative_position + dbFunctionDatas[i].index,
                                                         dbFunctionDatas[i].length,
                                                         webCsharpCode);
                        }
                        if (dbFunctionDatas[i].functionName == "QueryRow")
                        {
                            fileName = appRoot +
                                       "NFinal\\SqlTemplate\\" +
                                       connectionString.type.ToString() +
                                       "\\QueryRow.txt";
                            doc = new VTemplate.Engine.TemplateDocument(
                                fileName, System.Text.Encoding.UTF8
                                );
                            doc = new VTemplate.Engine.TemplateDocument(
                                fileName, System.Text.Encoding.UTF8);
                            doc.SetValue("functionName", methodName);
                            doc.SetValue("varName", dbFunctionDatas[i].varName);
                            doc.SetValue("dbName", dbFunctionDatas[i].connectionName);
                            doc.SetValue("sql", dbFunctionDatas[i].sql);
                            doc.SetValue("fields", dbFunctionDatas[i].fields);
                            doc.SetValue("sqlVarParameters", dbFunctionDatas[i].sqlVarParameters);
                            webCsharpCode      = doc.GetRenderText();
                            relative_position += Replace(ref csharpFileCode,
                                                         relative_position + dbFunctionDatas[i].index,
                                                         dbFunctionDatas[i].length,
                                                         webCsharpCode);
                        }
                        if (dbFunctionDatas[i].functionName == "Insert")
                        {
                            fileName = appRoot +
                                       "NFinal\\SqlTemplate\\" +
                                       connectionString.type.ToString() +
                                       "\\Insert.txt";
                            doc = new VTemplate.Engine.TemplateDocument(
                                fileName, System.Text.Encoding.UTF8
                                );
                            doc = new VTemplate.Engine.TemplateDocument(
                                fileName, System.Text.Encoding.UTF8);
                            doc.SetValue("functionName", methodName);
                            doc.SetValue("varName", dbFunctionDatas[i].varName);
                            doc.SetValue("dbName", dbFunctionDatas[i].connectionName);
                            doc.SetValue("sql", dbFunctionDatas[i].sql);
                            doc.SetValue("fields", dbFunctionDatas[i].fields);
                            doc.SetValue("sqlVarParameters", dbFunctionDatas[i].sqlVarParameters);
                            webCsharpCode      = doc.GetRenderText();
                            relative_position += Replace(ref csharpFileCode,
                                                         relative_position + dbFunctionDatas[i].index,
                                                         dbFunctionDatas[i].length,
                                                         webCsharpCode);
                        }
                        if (dbFunctionDatas[i].functionName == "Update")
                        {
                            fileName = appRoot +
                                       "NFinal\\SqlTemplate\\" +
                                       connectionString.type.ToString() +
                                       "\\Update.txt";
                            doc = new VTemplate.Engine.TemplateDocument(
                                fileName, System.Text.Encoding.UTF8
                                );
                            doc = new VTemplate.Engine.TemplateDocument(
                                fileName, System.Text.Encoding.UTF8);
                            doc.SetValue("functionName", methodName);
                            doc.SetValue("varName", dbFunctionDatas[i].varName);
                            doc.SetValue("dbName", dbFunctionDatas[i].connectionName);
                            doc.SetValue("sql", dbFunctionDatas[i].sql);
                            doc.SetValue("fields", dbFunctionDatas[i].fields);
                            doc.SetValue("sqlVarParameters", dbFunctionDatas[i].sqlVarParameters);
                            webCsharpCode      = doc.GetRenderText();
                            relative_position += Replace(ref csharpFileCode,
                                                         relative_position + dbFunctionDatas[i].index,
                                                         dbFunctionDatas[i].length,
                                                         webCsharpCode);
                        }
                        if (dbFunctionDatas[i].functionName == "Delete")
                        {
                            fileName = appRoot +
                                       "NFinal\\SqlTemplate\\" +
                                       connectionString.type.ToString() +
                                       "\\Update.txt";
                            doc = new VTemplate.Engine.TemplateDocument(
                                fileName, System.Text.Encoding.UTF8
                                );
                            doc = new VTemplate.Engine.TemplateDocument(
                                fileName, System.Text.Encoding.UTF8);
                            doc.SetValue("functionName", methodName);
                            doc.SetValue("varName", dbFunctionDatas[i].varName);
                            doc.SetValue("dbName", dbFunctionDatas[i].connectionName);
                            doc.SetValue("sql", dbFunctionDatas[i].sql);
                            doc.SetValue("fields", dbFunctionDatas[i].fields);
                            doc.SetValue("sqlVarParameters", dbFunctionDatas[i].sqlVarParameters);
                            webCsharpCode      = doc.GetRenderText();
                            relative_position += Replace(ref csharpFileCode,
                                                         relative_position + dbFunctionDatas[i].index,
                                                         dbFunctionDatas[i].length,
                                                         webCsharpCode);
                        }
                        if (dbFunctionDatas[i].functionName == "QueryObject")
                        {
                            fileName = appRoot +
                                       "NFinal\\SqlTemplate\\" +
                                       connectionString.type.ToString() +
                                       "\\QueryObject.txt";
                            doc = new VTemplate.Engine.TemplateDocument(
                                fileName, System.Text.Encoding.UTF8
                                );

                            doc = new VTemplate.Engine.TemplateDocument(
                                fileName, System.Text.Encoding.UTF8);
                            doc.SetValue("functionName", methodName);
                            doc.SetValue("varName", dbFunctionDatas[i].varName);
                            doc.SetValue("dbName", dbFunctionDatas[i].connectionName);
                            doc.SetValue("sql", dbFunctionDatas[i].sql);
                            doc.SetValue("fields", dbFunctionDatas[i].fields);
                            doc.SetValue("sqlVarParameters", dbFunctionDatas[i].sqlVarParameters);
                            doc.SetValue("convertMethodName", dbFunctionDatas[i].convertMethodName);
                            webCsharpCode      = doc.GetRenderText();
                            relative_position += Replace(ref csharpFileCode,
                                                         relative_position + dbFunctionDatas[i].index,
                                                         dbFunctionDatas[i].length,
                                                         webCsharpCode);
                        }
                        if (dbFunctionDatas[i].functionName == "Page")
                        {
                            fileName = appRoot +
                                       "NFinal\\SqlTemplate\\" +
                                       connectionString.type.ToString() +
                                       "\\Page.txt";
                            doc = new VTemplate.Engine.TemplateDocument(
                                fileName, System.Text.Encoding.UTF8
                                );

                            doc = new VTemplate.Engine.TemplateDocument(
                                fileName, System.Text.Encoding.UTF8);
                            doc.SetValue("functionName", methodName);
                            doc.SetValue("varName", dbFunctionDatas[i].varName);
                            doc.SetValue("dbName", dbFunctionDatas[i].connectionName);
                            doc.SetValue("fields", dbFunctionDatas[i].fields);

                            //分页参数解析
                            PageSqlAnalyse pageStatement = new PageSqlAnalyse(dbFunctionDatas[i].sql, connectionString.type);
                            pageStatement.Parse();
                            doc.SetValue("pageSql", pageStatement.pageSql);
                            doc.SetValue("countSql", pageStatement.countSql);
                            doc.SetValue("pageSizeVarName", dbFunctionDatas[i].parameters[1]);

                            doc.SetValue("sqlVarParameters", dbFunctionDatas[i].sqlVarParameters);
                            doc.SetValue("convertMethodName", dbFunctionDatas[i].convertMethodName);
                            webCsharpCode      = doc.GetRenderText();
                            relative_position += Replace(ref csharpFileCode,
                                                         relative_position + dbFunctionDatas[i].index,
                                                         dbFunctionDatas[i].length,
                                                         webCsharpCode);
                        }
                        if (dbFunctionDatas[i].functionName == "QueryRandom")
                        {
                            fileName = appRoot +
                                       "NFinal\\SqlTemplate\\" +
                                       connectionString.type.ToString() +
                                       "\\QueryRandom.txt";
                            doc = new VTemplate.Engine.TemplateDocument(
                                fileName, System.Text.Encoding.UTF8);
                            doc.SetValue("functionName", methodName);
                            doc.SetValue("varName", dbFunctionDatas[i].varName);
                            doc.SetValue("dbName", dbFunctionDatas[i].connectionName);

                            //select 语句 转为选取某随机行的语句

                            RandomSqlAnalyse random = new RandomSqlAnalyse(dbFunctionDatas[i].sql, connectionString.type);
                            random.Parse();
                            doc.SetValue("topNumber", dbFunctionDatas[i].parameters[1]);

                            doc.SetValue("sql", random.randomSql);
                            doc.SetValue("fields", dbFunctionDatas[i].fields);
                            doc.SetValue("sqlVarParameters", dbFunctionDatas[i].sqlVarParameters);
                            webCsharpCode      = doc.GetRenderText();
                            relative_position += Replace(ref csharpFileCode,
                                                         relative_position + dbFunctionDatas[i].index,
                                                         dbFunctionDatas[i].length,
                                                         webCsharpCode);
                        }
                        if (dbFunctionDatas[i].functionName == "QueryTop")
                        {
                            fileName = appRoot +
                                       "NFinal\\SqlTemplate\\" +
                                       connectionString.type.ToString() +
                                       "\\QueryTop.txt";
                            doc = new VTemplate.Engine.TemplateDocument(
                                fileName, System.Text.Encoding.UTF8);
                            doc.SetValue("functionName", methodName);
                            doc.SetValue("varName", dbFunctionDatas[i].varName);
                            doc.SetValue("dbName", dbFunctionDatas[i].connectionName);

                            //select 语句 转为选取某随机行的语句

                            TopSqlAnalyse top = new TopSqlAnalyse(dbFunctionDatas[i].sql, connectionString.type);
                            top.Parse();
                            doc.SetValue("topNumber", dbFunctionDatas[i].parameters[1]);

                            doc.SetValue("sql", top.topSql);
                            doc.SetValue("fields", dbFunctionDatas[i].fields);
                            doc.SetValue("sqlVarParameters", dbFunctionDatas[i].sqlVarParameters);
                            webCsharpCode      = doc.GetRenderText();
                            relative_position += Replace(ref csharpFileCode,
                                                         relative_position + dbFunctionDatas[i].index,
                                                         dbFunctionDatas[i].length,
                                                         webCsharpCode);
                        }
                    }
                }
            }
            return(relative_position);
        }