SQLExpression.cpp 26 KB

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