|  | CYQ.Data componentsCYQ.Data support multi-database application  [Txt,Xml,Access, MSSQL, Oracle,SQLite,MySql], help easily and quickly to develop your project | 
CYQ.Data 文本数据库解析SQL语法
| Principles |
										
										|
										
										| | #TopicOwner | 
| 昨晚突然心血来潮,想用直接执行SQL语句来操作文件数据库。 对于CYQ.Data V5版本的数据库,基本上基于MDataTable的实现,以前有解析过where条件那块语法,基本实现了简单的where条件查询和过滤。 在使用上,基本上基于MAction的封装和CodeFirst的实体方式,所以并不涉及到Select、Update、Delete语句。 为此,MProc这个执行Sql或存储过程的功能,在文本数据库里就用不了。 由于秋色园的错误日志,直接使用了文本数据库,所以,想通过sql语句批量删除点日志,所以就想到了要支持下基本的SQL语法。 要支持SQL语法支持,就得对SQL语句进行字符串解析,分拆出来各种关键字,然后结合原有增删改查机制进行处理了: 处理机制,基本如下: 1:对SQL语句,按空格分格出数组。 2:对数组进行循环,遍历并标记出所有的关键字。 3:根据关键字,分拆:Select、Update、Delete、Insert、From、Where、Set、Into及表名,还有字段名和字段值及*,count(*),Distinct等关键字。 4:根据分析出来的条件,结合原有逻辑组合处理了。 基本上就上面四个步骤了,目前除了Insert,其它的基本上完成,贴代码了:  void FormatSqlText(string sqlText)         {             string[] items = sqlText.Split(' ');             foreach (string item in items)             {                 switch (item.ToLower())                 {                     case "select":                         IsSelect = true;                         break;                     case "update":                         IsUpdate = true;                         break;                     case "delete":                         IsDelete = true;                         break;                     case "from":                         IsFrom = true;                         break;                     case "count(*)":                         IsGetCount = true;                         break;                     case "where":                         whereSql = sqlText.Substring(sqlText.IndexOf(item) + item.Length + 1);                         //该结束语句了。                         return;                     case "top":                         if (IsSelect && !IsFrom)                         {                             IsTopN = true;                         }                         break;                     case "distinct":                         if (IsSelect && !IsFrom)                         {                             IsDistinct = true;                         }                         break;                     case "set":                         if (IsUpdate && !string.IsNullOrEmpty(tableName) && fieldItems.Count == 0)                         {                             int start = sqlText.IndexOf(item) + item.Length;                             int end = sqlText.ToLower().IndexOf("where");                             string itemText = sqlText.Substring(start, end == -1 ? sqlText.Length - start : end - start);                             int quoteCount = 0, commaIndex = 0;                             for (int i = 0; i < itemText.Length; i++)                             {                                 if (i == itemText.Length - 1)                                 {                                     string keyValue = itemText.Substring(commaIndex).Trim();                                     if (!fieldItems.Contains(keyValue))                                     {                                         fieldItems.Add(keyValue);                                     }                                 }                                 else                                 {                                     switch (itemText[i])                                     {                                         case '\'':                                             quoteCount++;                                             break;                                         case ',':                                             if (quoteCount % 2 == 0)//双数,则允许分隔。                                             {                                                 string keyValue = itemText.Substring(commaIndex, i - commaIndex).Trim();                                                 if (!fieldItems.Contains(keyValue))                                                 {                                                     fieldItems.Add(keyValue);                                                 }                                                 commaIndex = i + 1;                                             }                                             break;                                     }                                 }                             }                         }                         break;                     default:                         if (IsTopN && topN == -1)                         {                             int.TryParse(item, out topN);//查询TopN                         }                         else if ((IsFrom || IsUpdate) && string.IsNullOrEmpty(tableName))                         {                             tableName = item;//获取表名。                         }                         else if (IsSelect && !IsFrom)//提取查询的中间条件。                         {                             #region Select语法解析                             string temp = item.Trim(',');                             switch (temp.ToLower())                             {                                 case "*":                                 case "count(*)":                                 case "top":                                 case "distinct":                                     break;                                 default:                                     if (IsTopN && topN.ToString() == temp)                                     {                                         break;                                     }                                     if (!fieldItems.Contains(temp))                                     {                                         fieldItems.Add(temp.ToLower());                                     }                                     break;                             }                             #endregion                         }                         break;                 }             }         }  | |
Post Comment
Bulletin
Article Search
Categories
- Platform for dynamic (20)
- Feedback (9)
- Guide (33)
- Principles (19)
- Project-Case (8)
- Business & Buy (2)
- Technology exchange (45)
New Article
- CYQ.Data Components Getting Started Guide [Part 5]-[MProc Execute Stored Procedures or SQL]
- CYQ.Data Components Getting Started Guide [Part 4]-[MAction Insert Delete Update]
- CYQ.Data Components Getting Started Guide [Part 3]-[MAction Get And Set Value]
- CYQ.Data Components Getting Started Guide [Part 2]-[MAction Data Query- Fill And Select]
- CYQ.Data Components Getting Started Guide [Part 1]
New Comment
- When some one searches for his necessary thing, therefore he/she wishes to be available that in detail, so that thing is maintained over here.
- This is my first time pay a quick visit at here and i am in fact happy to read everthing at alone place.
- I truly appreciate this blog article.Really thank you! Cool.
- please pay a visit to the web sites we follow, like this one particular, as it represents our picks in the web
- Really enjoyed this post.Really thank you!
- Really enjoyed this article.Really looking forward to read more. Great.
- poker bonuses What are the norms of copyright of web content? How as it different from Patent?
- Wow! Thank you! I permanently needed to write on my blog something like that. Can I implement a fragment of your post to my site?
- This website was how do I say it? Relevant!! Finally I ave found something that helped me. Cheers!
- I was reading through some of your content on this internet site and I believe this web site is very informative ! Continue posting.

