re_cache.cc 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. #include "re_cache.h"
  2. #include "re_load.h"
  3. #include "log.h"
  4. using namespace hsql;
  5. #define SPECIFIC_L1_SCHEMA "L1"
  6. #define SPECIFIC_L2_SCHEMA "L2"
  7. #define SPECIFIC_L3_SCHEMA "L3"
  8. int is_single_talbe()
  9. {
  10. return 0;
  11. }
  12. bool is_select(SQLParserResult* sql_ast)
  13. {
  14. if(sql_ast->getStatement(0)->type() == kStmtSelect)
  15. return true;
  16. return false;
  17. }
  18. int get_select_fields_num(SQLParserResult* sql_ast)
  19. {
  20. const SelectStatement* stmt = (const SelectStatement*)(sql_ast->getStatement(0));
  21. return stmt->selectList->size();
  22. }
  23. int is_select_count(const SelectStatement* stmt, int index)
  24. {
  25. //TODO
  26. return 0;
  27. }
  28. int get_where_condition_num(SQLParserResult* sql_ast)
  29. {
  30. const SelectStatement* stmt = (const SelectStatement*)(sql_ast->getStatement(0));
  31. Expr* where = stmt->whereClause;
  32. if(!where)
  33. return 0;
  34. get_rule_condition_num(where);
  35. return 0;
  36. }
  37. bool is_where_key(SQLParserResult* sql_ast, std::string keyname)
  38. {
  39. const SelectStatement* stmt = (const SelectStatement*)(sql_ast->getStatement(0));
  40. Expr* where = stmt->whereClause;
  41. if(!where)
  42. return false;
  43. if(where->isType(kExprOperator) && where->opType == kOpEquals)
  44. {
  45. if(where->expr->getName() == keyname)
  46. return true;
  47. }
  48. return false;
  49. }
  50. bool is_where_and(SQLParserResult* sql_ast)
  51. {
  52. const SelectStatement* stmt = (const SelectStatement*)(sql_ast->getStatement(0));
  53. Expr* where = stmt->whereClause;
  54. if(!where)
  55. return false;
  56. return false;
  57. }
  58. bool is_complex_keyword(SQLParserResult* sql_ast)
  59. {
  60. const SelectStatement* stmt = (const SelectStatement*)(sql_ast->getStatement(0));
  61. const TableRef* table = stmt->fromTable;
  62. if(stmt->order || stmt->groupBy || table->join)
  63. return true;
  64. return false;
  65. }
  66. bool is_complex_sql(SQLParserResult* sql_ast)
  67. {
  68. const SelectStatement* stmt = (const SelectStatement*)(sql_ast->getStatement(0));
  69. Expr* where = stmt->whereClause;
  70. if(!where)
  71. return false;
  72. Expr* cond1 = where->expr;
  73. if(cond1)
  74. {
  75. if((cond1->opType >= kOpNot && cond1->opType <= kOpExists) ||
  76. (cond1->opType >= kOpCase && cond1->opType <= kOpCaret))
  77. return true;
  78. }
  79. Expr* cond2 = where->expr2;
  80. if(cond2)
  81. {
  82. if((cond2->opType >= kOpNot && cond2->opType <= kOpExists) ||
  83. (cond2->opType >= kOpCase && cond2->opType <= kOpCaret))
  84. return true;
  85. }
  86. return false;
  87. }
  88. std::string get_schema(SQLParserResult* sql_ast)
  89. {
  90. StatementType t = sql_ast->getStatement(0)->type();
  91. if(t == kStmtSelect)
  92. {
  93. const SelectStatement* stmt = (const SelectStatement*)(sql_ast->getStatement(0));
  94. TableRef* table = stmt->fromTable;
  95. if(table->hasSchema())
  96. {
  97. return std::string(table->schema);
  98. }
  99. }
  100. else if(t == kStmtInsert)
  101. {
  102. const InsertStatement* stmt = (const InsertStatement*)(sql_ast->getStatement(0));
  103. if(stmt->schema)
  104. {
  105. return std::string(stmt->schema);
  106. }
  107. }
  108. else if(t == kStmtUpdate)
  109. {
  110. const UpdateStatement* stmt = (const UpdateStatement*)(sql_ast->getStatement(0));
  111. TableRef* table = stmt->table;
  112. if(table->hasSchema())
  113. {
  114. return std::string(table->schema);
  115. }
  116. }
  117. else if(t == kStmtDelete)
  118. {
  119. const DeleteStatement* stmt = (const DeleteStatement*)(sql_ast->getStatement(0));
  120. if(stmt->schema)
  121. {
  122. return std::string(stmt->schema);
  123. }
  124. }
  125. return "";
  126. }
  127. std::string get_table_name(SQLParserResult* sql_ast)
  128. {
  129. StatementType t = sql_ast->getStatement(0)->type();
  130. if(t == kStmtSelect)
  131. {
  132. const SelectStatement* stmt = (const SelectStatement*)(sql_ast->getStatement(0));
  133. TableRef* table = stmt->fromTable;
  134. if(table)
  135. {
  136. return std::string(table->getName());
  137. }
  138. }
  139. else if(t == kStmtInsert)
  140. {
  141. const InsertStatement* stmt = (const InsertStatement*)(sql_ast->getStatement(0));
  142. return std::string(stmt->tableName);
  143. }
  144. else if(t == kStmtUpdate)
  145. {
  146. const UpdateStatement* stmt = (const UpdateStatement*)(sql_ast->getStatement(0));
  147. TableRef* table = stmt->table;
  148. if(table)
  149. {
  150. return std::string(table->getName());
  151. }
  152. }
  153. else if(t == kStmtDelete)
  154. {
  155. const DeleteStatement* stmt = (const DeleteStatement*)(sql_ast->getStatement(0));
  156. if(stmt)
  157. {
  158. return std::string(stmt->tableName);
  159. }
  160. }
  161. return "";
  162. }
  163. bool is_dtc_adapt_type(SQLParserResult* sql_ast)
  164. {
  165. StatementType t = sql_ast->getStatement(0)->type();
  166. if(t == kStmtSelect || t == kStmtInsert || t == kStmtUpdate || t == kStmtDelete)
  167. return true;
  168. else
  169. return false;
  170. }
  171. int check_dtc_key(hsql::Expr* rule, std::string key)
  172. {
  173. int count = 0;
  174. if(!rule)
  175. return 0;
  176. if(rule->isType(kExprOperator) && rule->opType == kOpAnd)
  177. {
  178. count += check_dtc_key(rule->expr, key);
  179. count += check_dtc_key(rule->expr2, key);
  180. return count;
  181. }
  182. else if(rule->isType(kExprOperator) && rule->opType == kOpEquals)
  183. {
  184. if(rule->expr->getName() == key)
  185. return 1;
  186. }
  187. return 0;
  188. }
  189. bool re_is_cache_sql(SQLParserResult* sql_ast, std::string key)
  190. {
  191. if(sql_ast->size() > 1)
  192. return false;
  193. if(!is_dtc_adapt_type(sql_ast))
  194. return false;
  195. std::string schema = get_schema(sql_ast);
  196. if(schema == std::string(SPECIFIC_L1_SCHEMA))
  197. return true;
  198. else if(schema == std::string(SPECIFIC_L2_SCHEMA))
  199. return false;
  200. StatementType type = sql_ast->getStatement(0)->type();
  201. if(type == kStmtSelect)
  202. {
  203. const SelectStatement* stmt = (const SelectStatement*)(sql_ast->getStatement(0));
  204. Expr* where = stmt->whereClause;
  205. if(!where)
  206. return false;
  207. if(check_dtc_key(where, key) == 1)
  208. return true;
  209. }
  210. else if(type == kStmtDelete)
  211. {
  212. const DeleteStatement* stmt = (const DeleteStatement*)(sql_ast->getStatement(0));
  213. Expr* where = stmt->expr;
  214. if(!where)
  215. return false;
  216. if(check_dtc_key(where, key) == 1)
  217. return true;
  218. }
  219. else if(type == kStmtUpdate)
  220. {
  221. const UpdateStatement* stmt = (const UpdateStatement*)(sql_ast->getStatement(0));
  222. Expr* where = stmt->where;
  223. if(!where)
  224. return false;
  225. if(check_dtc_key(where, key) == 1)
  226. return true;
  227. }
  228. else if(type == kStmtInsert)
  229. {
  230. const InsertStatement* stmt = (const InsertStatement*)(sql_ast->getStatement(0));
  231. if(stmt->type != kInsertValues)
  232. return false;
  233. if(stmt->columns->size() == 0)
  234. return false;
  235. for(int i = 0; i < stmt->columns->size(); i++)
  236. {
  237. if(std::string(stmt->columns->at(i)) == key)
  238. return true;
  239. }
  240. }
  241. else
  242. {
  243. return false;
  244. }
  245. return false;
  246. }