re_cache.cc 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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 || table->type != kTableName)
  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)
  96. {
  97. log4cplus_debug("type: %d", table->type);
  98. if(table->type == kTableName && table->schema)
  99. {
  100. return std::string(table->schema);
  101. }
  102. }
  103. }
  104. else if(t == kStmtInsert)
  105. {
  106. const InsertStatement* stmt = (const InsertStatement*)(sql_ast->getStatement(0));
  107. if(stmt && stmt->schema)
  108. return std::string(stmt->schema);
  109. }
  110. else if(t == kStmtUpdate)
  111. {
  112. const UpdateStatement* stmt = (const UpdateStatement*)(sql_ast->getStatement(0));
  113. TableRef* table = stmt->table;
  114. if(table)
  115. {
  116. if(table->type == kTableName && table->schema)
  117. return std::string(table->schema);
  118. }
  119. }
  120. else if(t == kStmtDelete)
  121. {
  122. const DeleteStatement* stmt = (const DeleteStatement*)(sql_ast->getStatement(0));
  123. if(stmt && stmt->schema)
  124. {
  125. return std::string(stmt->schema);
  126. }
  127. }
  128. return "";
  129. }
  130. std::string get_table_name(SQLParserResult* sql_ast)
  131. {
  132. StatementType t = sql_ast->getStatement(0)->type();
  133. if(t == kStmtSelect)
  134. {
  135. const SelectStatement* stmt = (const SelectStatement*)(sql_ast->getStatement(0));
  136. TableRef* table = stmt->fromTable;
  137. if(table)
  138. {
  139. log4cplus_debug("type: %d", table->type);
  140. if(table->type == kTableName && table->name)
  141. {
  142. return std::string(table->name);
  143. }
  144. }
  145. }
  146. else if(t == kStmtInsert)
  147. {
  148. const InsertStatement* stmt = (const InsertStatement*)(sql_ast->getStatement(0));
  149. if(stmt && stmt->tableName)
  150. return std::string(stmt->tableName);
  151. }
  152. else if(t == kStmtUpdate)
  153. {
  154. const UpdateStatement* stmt = (const UpdateStatement*)(sql_ast->getStatement(0));
  155. TableRef* table = stmt->table;
  156. if(table)
  157. {
  158. if(table->type == kTableName && table->name)
  159. return std::string(table->name);
  160. }
  161. }
  162. else if(t == kStmtDelete)
  163. {
  164. const DeleteStatement* stmt = (const DeleteStatement*)(sql_ast->getStatement(0));
  165. if(stmt && stmt->tableName)
  166. {
  167. return std::string(stmt->tableName);
  168. }
  169. }
  170. return "";
  171. }
  172. bool is_dtc_adapt_type(SQLParserResult* sql_ast)
  173. {
  174. StatementType t = sql_ast->getStatement(0)->type();
  175. if(t == kStmtSelect || t == kStmtInsert || t == kStmtUpdate || t == kStmtDelete)
  176. return true;
  177. else
  178. return false;
  179. }
  180. int check_dtc_key(hsql::Expr* rule, std::string key)
  181. {
  182. int count = 0;
  183. if(!rule)
  184. return 0;
  185. if(rule->isType(kExprOperator) && rule->opType == kOpAnd)
  186. {
  187. count += check_dtc_key(rule->expr, key);
  188. count += check_dtc_key(rule->expr2, key);
  189. return count;
  190. }
  191. else if(rule->isType(kExprOperator) && rule->opType == kOpEquals)
  192. {
  193. log4cplus_debug("key string: %s %s", std::string(rule->expr->getName()).c_str(), key.c_str());
  194. if(strcasecmp(std::string(rule->expr->getName()).c_str(), key.c_str()) == 0)
  195. return 1;
  196. }
  197. return 0;
  198. }
  199. bool re_is_cache_sql(SQLParserResult* sql_ast, std::string key)
  200. {
  201. if(sql_ast->size() > 1)
  202. return false;
  203. if(!is_dtc_adapt_type(sql_ast))
  204. return false;
  205. StatementType type = sql_ast->getStatement(0)->type();
  206. if(type == kStmtSelect)
  207. {
  208. const SelectStatement* stmt = (const SelectStatement*)(sql_ast->getStatement(0));
  209. Expr* where = stmt->whereClause;
  210. if(!where)
  211. return false;
  212. if(check_dtc_key(where, key) == 1)
  213. return true;
  214. }
  215. else if(type == kStmtDelete)
  216. {
  217. const DeleteStatement* stmt = (const DeleteStatement*)(sql_ast->getStatement(0));
  218. Expr* where = stmt->expr;
  219. if(!where)
  220. return false;
  221. if(check_dtc_key(where, key) == 1)
  222. return true;
  223. }
  224. else if(type == kStmtUpdate)
  225. {
  226. const UpdateStatement* stmt = (const UpdateStatement*)(sql_ast->getStatement(0));
  227. Expr* where = stmt->where;
  228. if(!where)
  229. return false;
  230. if(check_dtc_key(where, key) == 1)
  231. return true;
  232. }
  233. else if(type == kStmtInsert)
  234. {
  235. const InsertStatement* stmt = (const InsertStatement*)(sql_ast->getStatement(0));
  236. if(stmt->type != kInsertValues)
  237. return false;
  238. if(stmt->columns == NULL && stmt->values->size() > 0)
  239. {
  240. return true;
  241. }
  242. for(int i = 0; i < stmt->columns->size(); i++)
  243. {
  244. if(strcasecmp(std::string(stmt->columns->at(i)).c_str(), key.c_str()) == 0)
  245. return true;
  246. }
  247. }
  248. else
  249. {
  250. return false;
  251. }
  252. return false;
  253. }