SQLExpression.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978
  1. /*##############################################################################
  2. HPCC SYSTEMS software Copyright (C) 2014 HPCC Systems.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. ############################################################################## */
  13. #include "SQLExpression.hpp"
  14. /***********************SQLFieldsExpression START**************************************************************************/
  15. SQLFieldsExpression::SQLFieldsExpression(bool allfiles)
  16. {
  17. this->all = allfiles;
  18. isExpanded = false;
  19. }
  20. SQLFieldsExpression::SQLFieldsExpression(const char * table)
  21. {
  22. this->all = false;
  23. this->table.set(table);
  24. isExpanded = false;
  25. }
  26. SQLFieldsExpression::~SQLFieldsExpression()
  27. {
  28. #ifdef _DEBUG
  29. fprintf(stderr, "\ndestroying SQLFieldsExression.\n");
  30. #endif
  31. }
  32. void SQLFieldsExpression::toString(StringBuffer & targetstr, bool fullOutput)
  33. {
  34. if (table.length() > 0)
  35. targetstr.append(table.str()).append(".");
  36. targetstr.append("*");
  37. }
  38. /***********************SQLFieldsExpression END**************************************************************************/
  39. /***********************SQLFieldValueExpression START**************************************************************************/
  40. SQLLogicType SQLFieldValueExpression::getLogicType()
  41. {
  42. const char * type = field.getColumnType();
  43. if (strnicmp(type,"STRING",6)==0)
  44. return String_LogicType;
  45. else if (strnicmp(type,"QSTRING",7)==0)
  46. return QSstring_LogicType;
  47. else if (strnicmp(type,"UNICODE",7)==0)
  48. return Unicode_LogicType;
  49. else if (strnicmp(type,"VARUNICODE",10)==0)
  50. return Unicode_LogicType;
  51. else if (strnicmp(type,"VARSTRING",9)==0)
  52. return String_LogicType;
  53. else if (strnicmp(type,"BOOLEAN",7)==0)
  54. return Bool_LogicType;
  55. else if (strnicmp(type,"UNSIGNED",8)==0)
  56. return Integer_LogicType;
  57. else if (strnicmp(type,"REAL",4)==0)
  58. return Decimal_LogicType;
  59. else if (strnicmp(type,"DECIMAL",7)==0)
  60. return Decimal_LogicType;
  61. else
  62. return Unknown_LogicType;
  63. }
  64. void SQLFieldValueExpression::toECLStringTranslateSource(
  65. StringBuffer & eclStr,
  66. IProperties * map,
  67. bool ignoreMisTranslations,
  68. bool forHaving,
  69. bool funcParam,
  70. bool countFuncParam)
  71. {
  72. StringBuffer translation;
  73. map->getProp(getParentTableName(),translation);
  74. if (translation.length() == 0 && getParentTableName() != NULL)
  75. return;
  76. if ( translation.length() > 0)
  77. {
  78. if (funcParam && forHaving && translation.length() > 0)
  79. {
  80. eclStr.append(" ROWS ( ");
  81. eclStr.append(translation);
  82. eclStr.append(" ) ");
  83. if (!countFuncParam)
  84. {
  85. eclStr.append(", ");
  86. eclStr.append(getName());
  87. }
  88. }
  89. else
  90. {
  91. eclStr.append(translation);
  92. eclStr.append(".");
  93. eclStr.append(getName());
  94. }
  95. }
  96. }
  97. SQLFieldValueExpression::SQLFieldValueExpression(const char * parentTableName, const char * fieldName)
  98. {
  99. field.setName(fieldName);
  100. field.setParenttable(parentTableName);
  101. }
  102. SQLFieldValueExpression::SQLFieldValueExpression()
  103. {
  104. field.setName("");
  105. field.setParenttable("");
  106. field.setAlias("");
  107. }
  108. const char * SQLFieldValueExpression::getParentTableName()
  109. {
  110. return field.getParenttable();
  111. }
  112. void SQLFieldValueExpression::setParentTableName(const char * parentTableName)
  113. {
  114. field.setParenttable(parentTableName);
  115. }
  116. const char * SQLFieldValueExpression::getName()
  117. {
  118. return field.getName();
  119. }
  120. const char * SQLFieldValueExpression::getNameOrAlias()
  121. {
  122. return field.getColumnNameOrAlias();
  123. }
  124. void SQLFieldValueExpression::setName(const char * fieldName)
  125. {
  126. field.setName(fieldName);
  127. }
  128. bool SQLFieldValueExpression::containsKey(const char * colname)
  129. {
  130. return field.isFieldNameOrAalias(colname);
  131. }
  132. void SQLFieldValueExpression::toString(StringBuffer & targetstr, bool fullOutput)
  133. {
  134. field.toString(targetstr, fullOutput);
  135. }
  136. const char * SQLFieldValueExpression::getAlias()
  137. {
  138. return field.getAlias();
  139. }
  140. void SQLFieldValueExpression::setAlias(const char * alias)
  141. {
  142. if (alias)
  143. field.setAlias(alias);
  144. }
  145. bool SQLFieldValueExpression::isAscending()
  146. {
  147. return field.isAscending();
  148. }
  149. void SQLFieldValueExpression::setAscending(bool ascending)
  150. {
  151. field.setAscending(ascending);
  152. }
  153. bool SQLFieldValueExpression::isAliasOrName(const char * possiblenameoralias)
  154. {
  155. return field.isFieldNameOrAalias(possiblenameoralias);
  156. }
  157. void SQLFieldValueExpression::setECLType(const char * type)
  158. {
  159. field.setColumnType(type);
  160. }
  161. const char * SQLFieldValueExpression::getECLType()
  162. {
  163. return field.getColumnType();
  164. }
  165. SQLFieldValueExpression::~SQLFieldValueExpression()
  166. {
  167. #ifdef _DEBUG
  168. fprintf(stderr, "\ndestroying SQLFieldValueExpression.\n");
  169. #endif
  170. }
  171. /***********************SQLFieldValueExpression END**************************************************************************/
  172. /***********************SQLUnaryExpression START**************************************************************************/
  173. SQLUnaryExpression::SQLUnaryExpression()
  174. {}
  175. SQLUnaryExpression::~SQLUnaryExpression()
  176. {
  177. #ifdef _DEBUG
  178. fprintf(stderr, "\ndestroying SQLUnaryExpression.\n");
  179. #endif
  180. operand1->Release();
  181. }
  182. void SQLUnaryExpression::getExpressionFromColumnName(const char * colname, StringBuffer & str)
  183. {
  184. switch (op)
  185. {
  186. case ISNOTNULL:
  187. str.append(" TRUE ");
  188. break;
  189. case ISNULL:
  190. str.append(" FALSE ");
  191. break;
  192. case NOT_SYM:
  193. case NEGATION:
  194. str.append(" NOT ");
  195. operand1->getExpressionFromColumnName(colname, str);
  196. break;
  197. default:
  198. break;
  199. }
  200. }
  201. SQLUnaryExpression::SQLUnaryExpression(ISQLExpression* operand1, int opname)
  202. {
  203. this->operand1 = operand1;
  204. this->op = opname;
  205. }
  206. bool SQLUnaryExpression::containsKey(const char* colname)
  207. {
  208. return operand1->containsKey(colname);
  209. }
  210. bool SQLUnaryExpression::containsEqualityCondition(IProperties * map, const char * first, const char * second)
  211. {
  212. return operand1->containsEqualityCondition(map, first, second);
  213. }
  214. void SQLUnaryExpression::toString(StringBuffer & targetstr, bool fullOutput)
  215. {
  216. switch (op)
  217. {
  218. case ISNOTNULL:
  219. targetstr.append(" TRUE ");
  220. break;
  221. case ISNULL:
  222. targetstr.append(" FALSE ");
  223. break;
  224. case NOT_SYM:
  225. case NEGATION:
  226. targetstr.append(" NOT ");
  227. operand1->toString(targetstr, fullOutput);
  228. break;
  229. default:
  230. operand1->toString(targetstr, fullOutput);
  231. break;
  232. }
  233. }
  234. void SQLUnaryExpression::toECLStringTranslateSource(
  235. StringBuffer & eclStr,
  236. IProperties * map,
  237. bool ignoreMisTranslations,
  238. bool forHaving,
  239. bool funcParam,
  240. bool countFuncParam)
  241. {
  242. switch (op)
  243. {
  244. case ISNOTNULL:
  245. eclStr.append(" TRUE ");
  246. break;
  247. case ISNULL:
  248. eclStr.append(" FALSE ");
  249. break;
  250. case NOT_SYM:
  251. case NEGATION:
  252. eclStr.append(" NOT ");
  253. operand1->toECLStringTranslateSource(eclStr, map, ignoreMisTranslations, forHaving, funcParam, countFuncParam);
  254. break;
  255. default:
  256. operand1->toECLStringTranslateSource(eclStr, map, ignoreMisTranslations, forHaving, funcParam, countFuncParam);
  257. break;
  258. }
  259. }
  260. /***********************SQLUnaryExpression END**************************************************************************/
  261. /***********************SQLParenthesisExpression START**************************************************************************/
  262. SQLParenthesisExpression::~SQLParenthesisExpression()
  263. {
  264. #ifdef _DEBUG
  265. fprintf(stderr, "\ndestroying SQLParenthesisExpression.\n");
  266. #endif
  267. innerexpression->Release();
  268. }
  269. SQLParenthesisExpression::SQLParenthesisExpression(ISQLExpression* exp)
  270. {
  271. this->innerexpression = exp;
  272. }
  273. bool SQLParenthesisExpression::containsKey(const char* colname)
  274. {
  275. return this->containsKey(colname);
  276. }
  277. void SQLParenthesisExpression::toString(StringBuffer & targetstr, bool fullOutput)
  278. {
  279. targetstr.append("( ");
  280. innerexpression->toString(targetstr, fullOutput);
  281. targetstr.append(" )");
  282. }
  283. ISQLExpression* SQLParenthesisExpression::getInnerExp()
  284. {
  285. return innerexpression;
  286. }
  287. /***********************SQLParenthesisExpression END**************************************************************************/
  288. /***********************SQLValueExpression START**************************************************************************/
  289. int SQLValueExpression::setParameterizedNames(int currentindex)
  290. {
  291. if (hasPlaceHolder())
  292. {
  293. placeHolderName.setf("%sPlaceHolder%d", getPlaceHolderType(), currentindex);
  294. return ++currentindex;
  295. }
  296. else
  297. return currentindex;
  298. }
  299. void SQLValueExpression::toECLStringTranslateSource(
  300. StringBuffer & eclStr,
  301. IProperties * map,
  302. bool ignoreMisTranslations,
  303. bool forHaving,
  304. bool funcParam,
  305. bool countFuncParam)
  306. {
  307. if (hasPlaceHolder())
  308. eclStr.append(placeHolderName.str());
  309. else
  310. eclStr.append(value.str());
  311. }
  312. SQLValueExpression::SQLValueExpression()
  313. {
  314. type = -1;
  315. value = "";
  316. field.setName("");
  317. field.setParenttable("");
  318. field.setAlias("");
  319. isAWildCardPattern = false;
  320. }
  321. SQLValueExpression::~SQLValueExpression()
  322. {
  323. #ifdef _DEBUG
  324. fprintf(stderr, "\ndestroying SQLValueExpression.\n");
  325. #endif
  326. }
  327. SQLValueExpression::SQLValueExpression(int type, const char * value)
  328. {
  329. this->type = type;
  330. this->value = value;
  331. isAWildCardPattern = false;
  332. }
  333. void SQLValueExpression::trimTextQuotes()
  334. {
  335. if (this->type == TEXT_STRING)
  336. {
  337. if (this->value.charAt(0) == '\'' && this->value.charAt(value.length()-1) == '\'')
  338. {
  339. this->value.remove(this->value.length()-1, 1);
  340. this->value.remove(0, 1);
  341. }
  342. }
  343. }
  344. void SQLValueExpression::toString(StringBuffer & targetstr, bool fullOutput)
  345. {
  346. if (hasPlaceHolder())
  347. targetstr.append(placeHolderName.str());
  348. else
  349. targetstr.append(value.str());
  350. }
  351. const char * SQLValueExpression::getName()
  352. {
  353. return field.getName();
  354. }
  355. const char * SQLValueExpression::getNameOrAlias()
  356. {
  357. return field.getColumnNameOrAlias();
  358. }
  359. void SQLValueExpression::setName(const char * fieldName)
  360. {
  361. field.setName(fieldName);
  362. }
  363. const char * SQLValueExpression::getAlias()
  364. {
  365. return field.getAlias();
  366. }
  367. void SQLValueExpression::setAlias(const char * alias)
  368. {
  369. if (alias)
  370. field.setAlias(alias);
  371. }
  372. void SQLValueExpression::setECLType(const char * type)
  373. {
  374. field.setColumnType(type);
  375. }
  376. const char * SQLValueExpression::getECLType()
  377. {
  378. return field.getColumnType();
  379. }
  380. /***********************SQLValueExpression END**************************************************************************/
  381. /***********************SQLBinaryExpression Start**************************************************************************/
  382. int SQLBinaryExpression::setParameterizedNames(int currentindex)
  383. {
  384. int op1index = operand1->setParameterizedNames(currentindex);
  385. return operand2->setParameterizedNames(op1index);
  386. }
  387. bool SQLBinaryExpression::containsEqualityCondition(ISQLExpression* operand, IProperties * map, const char * first, const char * second)
  388. {
  389. switch (operand->getExpType())
  390. {
  391. case Unary_ExpressionType:
  392. {
  393. SQLUnaryExpression * unary = dynamic_cast<SQLUnaryExpression *>(operand);
  394. return unary->containsEqualityCondition(map, first, second);
  395. }
  396. case Binary_ExpressionType:
  397. {
  398. SQLBinaryExpression * binary = dynamic_cast<SQLBinaryExpression *>(operand);
  399. return binary->isEqualityCondition(map, first, second);
  400. }
  401. case Parenthesis_ExpressionType:
  402. {
  403. SQLParenthesisExpression * paren = dynamic_cast<SQLParenthesisExpression *>(operand);
  404. return containsEqualityCondition(paren, map, first, second);
  405. }
  406. default:
  407. return false;
  408. }
  409. }
  410. bool SQLBinaryExpression::containsEqualityCondition(IProperties * map, const char * first, const char * second)
  411. {
  412. if (isEqualityCondition(map, first, second))
  413. {
  414. return true;
  415. }
  416. else
  417. {
  418. bool operand1Hasequality = containsEqualityCondition(operand1, map, first, second);
  419. bool operand2Hasequality = containsEqualityCondition(operand2, map, first, second);
  420. if (op == OR_SYM)
  421. return (operand1Hasequality && operand2Hasequality);
  422. else
  423. return (operand1Hasequality || operand2Hasequality);
  424. }
  425. }
  426. bool SQLBinaryExpression::isEqualityCondition(IProperties * map, const char * first, const char * second)
  427. {
  428. StringBuffer operand1Translate;
  429. StringBuffer operand2Translate;
  430. if (operand1->getExpType() == FieldValue_ExpressionType)
  431. {
  432. SQLFieldValueExpression * op1field = dynamic_cast<SQLFieldValueExpression *>(operand1);
  433. map->getProp(op1field->getParentTableName(), operand1Translate);
  434. }
  435. if (operand2->getExpType() == FieldValue_ExpressionType)
  436. {
  437. SQLFieldValueExpression * op2field = dynamic_cast<SQLFieldValueExpression *>(operand2);
  438. map->getProp(op2field->getParentTableName(), operand2Translate);
  439. }
  440. if (operand1Translate.length()<=0 || operand2Translate.length() <= 0)
  441. return false;
  442. return (
  443. strcmp(operand1Translate.str(), operand2Translate.str()) != 0 &&
  444. (
  445. (strcmp(operand1Translate.str(), first)==0 || strcmp(operand2Translate.str(), first)==0)
  446. &&
  447. (strcmp(operand1Translate.str(), second)==0 || strcmp(operand2Translate.str(), second)==0)
  448. )
  449. );
  450. }
  451. void SQLBinaryExpression::toECLStringTranslateSource(
  452. StringBuffer & eclStr,
  453. IProperties * map,
  454. bool ignoreMisTranslations,
  455. bool forHaving,
  456. bool funcParam,
  457. bool countFuncParam)
  458. {
  459. StringBuffer translation1;
  460. StringBuffer translation2;
  461. operand1->toECLStringTranslateSource(translation1, map, ignoreMisTranslations, forHaving, false, false);
  462. operand2->toECLStringTranslateSource(translation2, map, ignoreMisTranslations, forHaving, false, false);
  463. if (translation1.length()>0 && translation2.length()>0)
  464. {
  465. if ( op == LIKE_SYM || op == NOT_LIKE)
  466. {
  467. eclStr.append(getOpStr());
  468. eclStr.append("( ");
  469. eclStr.append(translation1);
  470. eclStr.append(" , ");
  471. eclStr.append(translation2);
  472. eclStr.append(" , true )");
  473. }
  474. else
  475. {
  476. eclStr.append(translation1);
  477. eclStr.append(getOpStr());
  478. eclStr.append(translation2);
  479. }
  480. }
  481. else if (translation1.length() == 0 && translation2.length() == 0)
  482. {
  483. return;
  484. }
  485. else if (ignoreMisTranslations)
  486. {
  487. /*
  488. * If operand1 or operand2 could not be translated using the translation map,
  489. * and ignoreMisTranslations = true, we're going to attempt to return an valid
  490. * ECL translation of this binary expression. IF the binary expression is of type
  491. * OR | AND, we can substitute the mistranslated operand with the appropriate boolean value
  492. * to complete the expression with out changing the gist of the expression.
  493. *
  494. * This is typically done when converting an SQL 'WHERE' clause or 'ON' clause to ECL to
  495. * be used in an ECL JOIN function. In any one particular ECL Join funtion only two datasets
  496. * are joined, therefore not all portions of the SQL logic clause might be relevant.
  497. *
  498. */
  499. if (op == OR_SYM || op == AND_SYM)
  500. {
  501. StringBuffer convert( op == OR_SYM ? "FALSE" : "TRUE");
  502. if (translation1.length()>0)
  503. {
  504. WARNLOG("Operand 1 of binary expression could not be translated.");
  505. eclStr.append(translation1);
  506. eclStr.append(getOpStr());
  507. eclStr.append(convert);
  508. }
  509. else
  510. {
  511. WARNLOG("Operand 2 of binary expression could not be translated.");
  512. eclStr.append(convert);
  513. eclStr.append(getOpStr());
  514. eclStr.append(translation2);
  515. }
  516. }
  517. else
  518. {
  519. WARNLOG("Binary expression could not be translated.");
  520. return;
  521. }
  522. }
  523. else
  524. {
  525. WARNLOG("Binary expression could not be translated.");
  526. return;
  527. }
  528. }
  529. SQLBinaryExpression::SQLBinaryExpression(int op,ISQLExpression* operand1,ISQLExpression* operand2)
  530. {
  531. this->operand1 = operand1;
  532. this->op = op;
  533. this->operand2 = operand2;
  534. }
  535. SQLBinaryExpression::~SQLBinaryExpression()
  536. {
  537. #ifdef _DEBUG
  538. fprintf(stderr, "\ndestroying SQLBinaryExpression.\n");
  539. #endif
  540. if (operand1)
  541. operand1->Release();
  542. if (operand2)
  543. operand2->Release();
  544. }
  545. bool SQLBinaryExpression::containsKey(const char * colname)
  546. {
  547. return operand1->containsKey(colname) || operand2->containsKey(colname);
  548. }
  549. void SQLBinaryExpression::toString(StringBuffer & targetstr, bool fullOutput)
  550. {
  551. if ( op == LIKE_SYM || op == NOT_LIKE)
  552. {
  553. targetstr.append(getOpStr());
  554. targetstr.append("( ");
  555. operand1->toString(targetstr, fullOutput);
  556. targetstr.append(" , ");
  557. operand2->toString(targetstr, fullOutput);
  558. targetstr.append(" , true )");
  559. }
  560. else
  561. {
  562. operand1->toString(targetstr, fullOutput);
  563. targetstr.append(getOpStr());
  564. operand2->toString(targetstr, fullOutput);
  565. }
  566. }
  567. const char * SQLBinaryExpression::getOpStr()
  568. {
  569. switch (op)
  570. {
  571. case AND_SYM:
  572. return " AND ";
  573. case DIVIDE:
  574. return " / ";
  575. case EQ_SYM:
  576. return " = ";
  577. case GTH:
  578. return " > ";
  579. case GET:
  580. return " >= ";
  581. case LTH:
  582. return " < ";
  583. case LET:
  584. return " <= ";
  585. case MINUS:
  586. return " - ";
  587. case MOD:
  588. return " % ";
  589. case ASTERISK:
  590. return " * ";
  591. case NOT_EQ:
  592. return " != ";
  593. case OR_SYM:
  594. return " OR ";
  595. case PLUS:
  596. return " + ";
  597. case IN_SYM:
  598. return " IN ";
  599. case NOT_IN:
  600. return " NOT IN ";
  601. case NOT_LIKE:
  602. return " NOT STD.Str.WildMatch";
  603. case LIKE_SYM:
  604. return " STD.Str.WildMatch";
  605. default:
  606. return " ";
  607. }
  608. }
  609. int SQLBinaryExpression::getExpressionsCount()
  610. {
  611. return operand1->getExpressionsCount() + operand2->getExpressionsCount();
  612. }
  613. /***********************SQLBinaryExpression END**************************************************************************/
  614. /***********************SQLParameterPlaceHolderExpression START**********************************************************/
  615. const char * SQLParameterPlaceHolderExpression::PARAMPREFIX = "PARAM";
  616. int SQLParameterPlaceHolderExpression::setParameterizedNames(int currentindex)
  617. {
  618. value.set(PARAMPREFIX);
  619. value.append(currentindex);
  620. return ++currentindex;
  621. }
  622. SQLParameterPlaceHolderExpression::SQLParameterPlaceHolderExpression()
  623. {
  624. }
  625. SQLParameterPlaceHolderExpression::~SQLParameterPlaceHolderExpression()
  626. {
  627. #ifdef _DEBUG
  628. fprintf(stderr, "\ndestroying SQLParameterPlaceHolderExpression.\n");
  629. #endif
  630. }
  631. void SQLParameterPlaceHolderExpression::toString(StringBuffer & targetstr, bool fullOutput)
  632. {
  633. targetstr.append(value.str());
  634. }
  635. /***********************SQLParameterPlaceHolderExpression END********************************************************/
  636. /***********************SQLFunctionExpression START**********************************************************/
  637. int SQLFunctionExpression::setParameterizedNames(int currentindex)
  638. {
  639. int paramundex = currentindex;
  640. ForEachItemIn(paramidx , params)
  641. {
  642. ISQLExpression & param = params.item(paramidx);
  643. paramundex = param.setParameterizedNames(currentindex);
  644. }
  645. return paramundex;
  646. }
  647. void SQLFunctionExpression::toECLStringTranslateSource(
  648. StringBuffer & eclStr,
  649. IProperties * map,
  650. bool ignoreMisTranslations,
  651. bool forHaving,
  652. bool funcParam,
  653. bool countFuncParam)
  654. {
  655. eclStr.append(function.eclFunctionName);
  656. eclStr.append("( ");
  657. ForEachItemIn(paramidx , params)
  658. {
  659. ISQLExpression & param = params.item(paramidx);
  660. StringBuffer translation;
  661. param.toECLStringTranslateSource(translation, map, ignoreMisTranslations, forHaving, true, strncmp(function.name,"COUNT", 5)==0);
  662. if (!ignoreMisTranslations && translation.length()<=0)
  663. return;
  664. if ( paramidx != 0 )
  665. eclStr.append(", ");
  666. eclStr.append(translation);
  667. }
  668. eclStr.append(" )");
  669. }
  670. SQLFunctionExpression::SQLFunctionExpression(const char* funcname)
  671. {
  672. setFunction(funcname);
  673. this->alias = "";
  674. distinct = false;
  675. }
  676. SQLFunctionExpression::SQLFunctionExpression(const char* funcname, const IArrayOf<ISQLExpression> &params)
  677. {
  678. setFunction(funcname);
  679. this->params = params;
  680. this->alias = "";
  681. distinct = false;
  682. }
  683. const char * SQLFunctionExpression::getAlias()
  684. {
  685. return alias.str();
  686. }
  687. void SQLFunctionExpression::setAlias(const char * something)
  688. {
  689. this->alias.append(something);
  690. }
  691. SQLFunctionExpression::~SQLFunctionExpression()
  692. {
  693. #ifdef _DEBUG
  694. fprintf(stderr, "Leaving SQLFunctionExpression");
  695. #endif
  696. params.kill(false);
  697. }
  698. void SQLFunctionExpression::getParamsString(StringBuffer & targetstr, bool fullOutput)
  699. {
  700. int paramcount = params.length();
  701. for (int i = 0; i < paramcount; i++)
  702. {
  703. if ( i != 0 )
  704. targetstr.append(", ");
  705. ISQLExpression & exp = params.item(i);
  706. exp.toString(targetstr, fullOutput);
  707. }
  708. }
  709. void SQLFunctionExpression::toString(StringBuffer & targetstr, bool fullOutput)
  710. {
  711. targetstr.append(function.eclFunctionName);
  712. targetstr.append(" ( ");
  713. getParamsString(targetstr, fullOutput);
  714. targetstr.append(" )");
  715. }
  716. bool SQLFunctionExpression::containsKey(const char * colname)
  717. {
  718. ForEachItemIn(Idx, params)
  719. {
  720. if (params.item(Idx).containsKey(colname))
  721. return true;
  722. }
  723. return false;
  724. }
  725. int SQLFunctionExpression::getExpressionsCount()
  726. {
  727. int count = 0;
  728. ForEachItemIn(paramidx , params)
  729. {
  730. count += params.item(paramidx).getExpressionsCount();
  731. }
  732. return count;
  733. }
  734. const char * SQLFunctionExpression::getNameOrAlias()
  735. {
  736. if (alias.length() > 0)
  737. return getAlias();
  738. else
  739. return getName();
  740. }
  741. /***********************SQLFunctionExpression END********************************************************/
  742. /************************SQLListExpression Start *************************************************************************/
  743. SQLListExpression::SQLListExpression(){}
  744. SQLListExpression::~SQLListExpression()
  745. {
  746. #ifdef _DEBUG
  747. fprintf(stderr, "\ndestroying SQLListExpression.\n");
  748. #endif
  749. entries.kill(false);
  750. }
  751. int SQLListExpression::getExpressionsCount()
  752. {
  753. int count = 0;
  754. ForEachItemIn(paramidx , entries)
  755. {
  756. count += entries.item(paramidx).getExpressionsCount();
  757. }
  758. return count;
  759. }
  760. void SQLListExpression::getExpressionFromColumnName(const char * colname, StringBuffer & str)
  761. {
  762. StringBuffer paramlist;
  763. StringBuffer paramresult;
  764. ForEachItemIn(idx, entries)
  765. {
  766. entries.item(idx).getExpressionFromColumnName(colname, paramresult.clear());
  767. if (paramresult.length() == 0)
  768. return;
  769. if ( idx > 0 )
  770. paramlist.append(", ");
  771. paramlist.append(paramresult);
  772. }
  773. if (paramlist.length()>0)
  774. str.appendf(" [ %s ] ", paramlist.str());
  775. }
  776. void SQLListExpression::getUniqueExpressionColumnNames(StringArray & uniquenames)
  777. {
  778. ForEachItemIn(paramidx , entries)
  779. {
  780. ISQLExpression & param = entries.item(paramidx);
  781. param.getUniqueExpressionColumnNames(uniquenames);
  782. }
  783. }
  784. void SQLListExpression::eclDeclarePlaceHolders(StringBuffer & eclstr, int op, int sibtype)
  785. {
  786. ForEachItemIn(paramidx , entries)
  787. {
  788. ISQLExpression & param = entries.item(paramidx);
  789. param.eclDeclarePlaceHolders(eclstr, op, sibtype);
  790. }
  791. }
  792. SQLLogicType SQLListExpression::getLogicType()
  793. {
  794. return Bool_LogicType;
  795. }
  796. int SQLListExpression::setParameterizedNames(int currentindex)
  797. {
  798. int paramundex = currentindex;
  799. ForEachItemIn(paramidx , entries)
  800. {
  801. ISQLExpression & param = entries.item(paramidx);
  802. paramundex = param.setParameterizedNames(currentindex);
  803. }
  804. return paramundex;
  805. }
  806. void SQLListExpression::toECLStringTranslateSource(
  807. StringBuffer & eclStr,
  808. IProperties * map,
  809. bool ignoreMisTranslations,
  810. bool forHaving,
  811. bool funcParam,
  812. bool countFuncParam)
  813. {
  814. eclStr.append("[ ");
  815. ForEachItemIn(paramidx , entries)
  816. {
  817. ISQLExpression & param = entries.item(paramidx);
  818. StringBuffer translation;
  819. param.toECLStringTranslateSource(translation, map, ignoreMisTranslations, forHaving, funcParam, countFuncParam);
  820. if (!ignoreMisTranslations && translation.length()<=0)
  821. return;
  822. if ( paramidx != 0 )
  823. eclStr.append(", ");
  824. eclStr.append(translation);
  825. }
  826. eclStr.append(" ]");
  827. }
  828. bool SQLListExpression::containsKey(const char* colname)
  829. {
  830. ForEachItemIn(Idx, entries)
  831. {
  832. if (entries.item(Idx).containsKey(colname))
  833. return true;
  834. }
  835. return false;
  836. }
  837. void SQLListExpression::toString(StringBuffer & targetstr, bool fullOutput)
  838. {
  839. targetstr.append(" [ ");
  840. int entrycount = entries.length();
  841. ForEachItemIn(Idx, entries)
  842. {
  843. if ( Idx != 0 )
  844. targetstr.append(", ");
  845. ISQLExpression & exp = entries.item(Idx);
  846. exp.toString(targetstr, fullOutput);
  847. }
  848. targetstr.append(" ] ");
  849. }
  850. void SQLListExpression::appendEntry(ISQLExpression * entry)
  851. {
  852. entries.append(*entry);
  853. }
  854. /************************SQLListExpression end *************************************************************************/