Drizzled Public API Documentation

create.cc
Go to the documentation of this file.
00001 /* Copyright (C) 2000-2003 MySQL AB
00002 
00003    This program is free software; you can redistribute it and/or modify
00004    it under the terms of the GNU General Public License as published by
00005    the Free Software Foundation; version 2 of the License.
00006 
00007    This program is distributed in the hope that it will be useful,
00008    but WITHOUT ANY WARRANTY; without even the implied warranty of
00009    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00010    GNU General Public License for more details.
00011 
00012    You should have received a copy of the GNU General Public License
00013    along with this program; if not, write to the Free Software
00014    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA */
00015 
00023 #include <config.h>
00024 #include <drizzled/item/create.h>
00025 #include <drizzled/item/func.h>
00026 #include <drizzled/error.h>
00027 #include <drizzled/system_variables.h>
00028 
00029 #include <drizzled/function_container.h>
00030 
00031 #include <drizzled/function/str/binary.h>
00032 #include <drizzled/function/str/concat.h>
00033 #include <drizzled/function/str/conv.h>
00034 #include <drizzled/function/str/export_set.h>
00035 #include <drizzled/function/str/load_file.h>
00036 #include <drizzled/function/str/make_set.h>
00037 #include <drizzled/function/str/pad.h>
00038 #include <drizzled/function/str/repeat.h>
00039 #include <drizzled/function/str/str_conv.h>
00040 #include <drizzled/function/str/trim.h>
00041 
00042 #include <drizzled/function/time/date_format.h>
00043 #include <drizzled/function/time/dayname.h>
00044 #include <drizzled/function/time/dayofmonth.h>
00045 #include <drizzled/function/time/dayofyear.h>
00046 #include <drizzled/function/time/from_unixtime.h>
00047 #include <drizzled/function/time/from_days.h>
00048 #include <drizzled/function/time/last_day.h>
00049 #include <drizzled/function/time/makedate.h>
00050 #include <drizzled/function/time/month.h>
00051 #include <drizzled/function/time/period_add.h>
00052 #include <drizzled/function/time/period_diff.h>
00053 #include <drizzled/function/time/to_days.h>
00054 #include <drizzled/function/time/typecast.h>
00055 #include <drizzled/function/time/unix_timestamp.h>
00056 #include <drizzled/function/time/weekday.h>
00057 
00058 #include <drizzled/item/cmpfunc.h>
00059 #include <drizzled/plugin/function.h>
00060 #include <drizzled/session.h>
00061 
00062 /* Function declarations */
00063 
00064 #include <drizzled/function/func.h>
00065 #include <drizzled/function/additive_op.h>
00066 #include <drizzled/function/math/dec.h>
00067 #include <drizzled/function/math/decimal_typecast.h>
00068 #include <drizzled/function/field.h>
00069 #include <drizzled/function/find_in_set.h>
00070 #include <drizzled/function/found_rows.h>
00071 #include <drizzled/function/get_system_var.h>
00072 #include <drizzled/function/math/int_val.h>
00073 #include <drizzled/function/math/integer.h>
00074 #include <drizzled/function/last_insert.h>
00075 #include <drizzled/function/locate.h>
00076 #include <drizzled/function/min_max.h>
00077 #include <drizzled/function/num1.h>
00078 #include <drizzled/function/num_op.h>
00079 #include <drizzled/function/numhybrid.h>
00080 #include <drizzled/function/math/real.h>
00081 #include <drizzled/function/row_count.h>
00082 #include <drizzled/function/set_user_var.h>
00083 #include <drizzled/function/sign.h>
00084 #include <drizzled/function/math/tan.h>
00085 #include <drizzled/function/units.h>
00086 
00087 #include <drizzled/function/cast/boolean.h>
00088 #include <drizzled/function/cast/signed.h>
00089 #include <drizzled/function/cast/time.h>
00090 #include <drizzled/function/cast/unsigned.h>
00091 
00092 using namespace std;
00093 
00094 namespace drizzled {
00095 
00096 /*
00097 =============================================================================
00098   LOCAL DECLARATIONS
00099 =============================================================================
00100 */
00101 
00117 static bool has_named_parameters(List<Item>& params)
00118 {
00119   List<Item>::iterator it(params.begin());
00120   while (Item* param= it++)
00121   {
00122     if (not param->is_autogenerated_name)
00123       return true;
00124   }
00125   return false;
00126 }
00127 
00128 class Create_native_func : public Create_func
00129 {
00130 public:
00131   virtual Item* create(Session* session, str_ref name, List<Item>* item_list)
00132   {
00133     if (item_list && has_named_parameters(*item_list))
00134     {
00135       my_error(ER_WRONG_PARAMETERS_TO_NATIVE_FCT, MYF(0), name.data());
00136       return NULL;
00137     }
00138     return create_native(session, name, item_list);
00139   }
00140 
00148   virtual Item* create_native(Session*, str_ref name, List<Item>*) = 0;
00149 };
00150 
00151 
00156 class Create_func_arg0 : public Create_func
00157 {
00158 public:
00159   virtual Item* create(Session *session, str_ref name, List<Item> *item_list);
00160 
00166   virtual Item* create(Session *session) = 0;
00167 };
00168 
00169 
00174 class Create_func_arg1 : public Create_func
00175 {
00176 public:
00177   virtual Item* create(Session *session, str_ref name, List<Item> *item_list);
00178 
00185   virtual Item* create(Session *session, Item *arg1) = 0;
00186 };
00187 
00188 
00193 class Create_func_arg2 : public Create_func
00194 {
00195 public:
00196   virtual Item* create(Session *session, str_ref name, List<Item> *item_list);
00197 
00205   virtual Item* create(Session *session, Item *arg1, Item *arg2) = 0;
00206 };
00207 
00208 
00213 class Create_func_arg3 : public Create_func
00214 {
00215 public:
00216   virtual Item* create(Session *session, str_ref name, List<Item> *item_list);
00217 
00226   virtual Item* create(Session *session, Item *arg1, Item *arg2, Item *arg3) = 0;
00227 };
00228 
00229 
00234 /*
00235   Concrete functions builders (native functions).
00236   Please keep this list sorted in alphabetical order,
00237   it helps to compare code between versions, and helps with merges conflicts.
00238 */
00239 
00240 
00241 class Create_func_bin : public Create_func_arg1
00242 {
00243 public:
00244   using Create_func_arg1::create;
00245 
00246   virtual Item* create(Session *session, Item *arg1);
00247 
00248   static Create_func_bin s_singleton;
00249 };
00250 
00251 class Create_func_concat : public Create_native_func
00252 {
00253 public:
00254   virtual Item* create_native(Session *session, str_ref name, List<Item> *item_list);
00255 
00256   static Create_func_concat s_singleton;
00257 };
00258 
00259 
00260 class Create_func_concat_ws : public Create_native_func
00261 {
00262 public:
00263   virtual Item* create_native(Session *session, str_ref name, List<Item> *item_list);
00264 
00265   static Create_func_concat_ws s_singleton;
00266 };
00267 
00268 
00269 class Create_func_conv : public Create_func_arg3
00270 {
00271 public:
00272   using Create_func_arg3::create;
00273 
00274   virtual Item* create(Session *session, Item *arg1, Item *arg2, Item *arg3)
00275   {
00276     return new (session->mem) Item_func_conv(arg1, arg2, arg3);
00277   }
00278 
00279   static Create_func_conv s_singleton;
00280 };
00281 
00282 Create_func_conv Create_func_conv::s_singleton;
00283 
00284 class Create_func_cot : public Create_func_arg1
00285 {
00286 public:
00287   using Create_func_arg1::create;
00288 
00289   virtual Item* create(Session *session, Item *arg1);
00290 
00291   static Create_func_cot s_singleton;
00292 };
00293 
00294 class Create_func_date_format : public Create_func_arg2
00295 {
00296 public:
00297   using Create_func_arg2::create;
00298 
00299   virtual Item* create(Session *session, Item *arg1, Item *arg2)
00300   {
00301     return new (session->mem) Item_func_date_format(arg1, arg2, 0);
00302   }
00303 
00304   static Create_func_date_format s_singleton;
00305 };
00306 
00307 Create_func_date_format Create_func_date_format::s_singleton;
00308 
00309 class Create_func_datediff : public Create_func_arg2
00310 {
00311 public:
00312   using Create_func_arg2::create;
00313 
00314   virtual Item* create(Session *session, Item *arg1, Item *arg2);
00315 
00316   static Create_func_datediff s_singleton;
00317 };
00318 
00319 
00320 class Create_func_dayname : public Create_func_arg1
00321 {
00322 public:
00323   using Create_func_arg1::create;
00324 
00325   virtual Item* create(Session *session, Item *arg1)
00326   {
00327     return new (session->mem) Item_func_dayname(arg1);
00328   }
00329 
00330   static Create_func_dayname s_singleton;
00331 };
00332 
00333 Create_func_dayname Create_func_dayname::s_singleton;
00334 
00335 class Create_func_dayofmonth : public Create_func_arg1
00336 {
00337 public:
00338   using Create_func_arg1::create;
00339 
00340   virtual Item* create(Session *session, Item *arg1)
00341   {
00342     return new (session->mem) Item_func_dayofmonth(arg1);
00343   }
00344 
00345   static Create_func_dayofmonth s_singleton;
00346 };
00347 
00348 Create_func_dayofmonth Create_func_dayofmonth::s_singleton;
00349 
00350 class Create_func_dayofweek : public Create_func_arg1
00351 {
00352 public:
00353   using Create_func_arg1::create;
00354 
00355   virtual Item* create(Session *session, Item *arg1)
00356   {
00357     return new (session->mem) Item_func_weekday(arg1, 1);
00358   }
00359 
00360   static Create_func_dayofweek s_singleton;
00361 };
00362 
00363 Create_func_dayofweek Create_func_dayofweek::s_singleton;
00364 
00365 class Create_func_dayofyear : public Create_func_arg1
00366 {
00367 public:
00368   using Create_func_arg1::create;
00369 
00370   virtual Item* create(Session *session, Item *arg1)
00371   {
00372     return new (session->mem) Item_func_dayofyear(arg1);
00373   }
00374 
00375   static Create_func_dayofyear s_singleton;
00376 };
00377 
00378 Create_func_dayofyear Create_func_dayofyear::s_singleton;
00379 
00380 class Create_func_decode : public Create_func_arg2
00381 {
00382 public:
00383   using Create_func_arg2::create;
00384 
00385   virtual Item* create(Session *session, Item *arg1, Item *arg2);
00386 
00387   static Create_func_decode s_singleton;
00388 };
00389 
00390 
00391 class Create_func_degrees : public Create_func_arg1
00392 {
00393 public:
00394   using Create_func_arg1::create;
00395 
00396   virtual Item* create(Session *session, Item *arg1)
00397   {
00398     return new (session->mem) Item_func_units("degrees", arg1, 180/M_PI, 0.0);
00399   }
00400 
00401   static Create_func_degrees s_singleton;
00402 };
00403 
00404 Create_func_degrees Create_func_degrees::s_singleton;
00405 
00406 class Create_func_export_set : public Create_native_func
00407 {
00408 
00409 public:
00410   virtual Item* create_native(Session *session, str_ref name, List<Item> *item_list);
00411 
00412   static Create_func_export_set s_singleton;
00413 };
00414 
00415 
00416 class Create_func_field : public Create_native_func
00417 {
00418 public:
00419   virtual Item* create_native(Session *session, str_ref name, List<Item> *item_list);
00420 
00421   static Create_func_field s_singleton;
00422 };
00423 
00424 
00425 class Create_func_find_in_set : public Create_func_arg2
00426 {
00427 public:
00428   using Create_func_arg2::create;
00429 
00430   virtual Item* create(Session *session, Item *arg1, Item *arg2)
00431   {
00432     return new (session->mem) Item_func_find_in_set(arg1, arg2);
00433   }
00434 
00435   static Create_func_find_in_set s_singleton;
00436 };
00437 
00438 class Create_func_found_rows : public Create_func_arg0
00439 {
00440 public:
00441   using Create_func_arg0::create;
00442 
00443   virtual Item* create(Session *session)
00444   {
00445     return new (session->mem) Item_func_found_rows();
00446   }
00447 
00448   static Create_func_found_rows s_singleton;
00449 };
00450 
00451 
00452 class Create_func_from_days : public Create_func_arg1
00453 {
00454 public:
00455   using Create_func_arg1::create;
00456 
00457   virtual Item* create(Session *session, Item *arg1)
00458   {
00459     return new (session->mem) Item_func_from_days(arg1);
00460   }
00461 
00462   static Create_func_from_days s_singleton;
00463 };
00464 
00465 
00466 class Create_func_from_unixtime : public Create_native_func
00467 {
00468 public:
00469   virtual Item* create_native(Session *session, str_ref name, List<Item> *item_list);
00470 
00471   static Create_func_from_unixtime s_singleton;
00472 };
00473 
00474 
00475 class Create_func_greatest : public Create_native_func
00476 {
00477 public:
00478   virtual Item* create_native(Session *session, str_ref name, List<Item> *item_list);
00479 
00480   static Create_func_greatest s_singleton;
00481 };
00482 
00483 
00484 class Create_func_ifnull : public Create_func_arg2
00485 {
00486 public:
00487   using Create_func_arg2::create;
00488 
00489   virtual Item* create(Session *session, Item *arg1, Item *arg2)
00490   {
00491     return new (session->mem) Item_func_ifnull(arg1, arg2);
00492   }
00493 
00494   static Create_func_ifnull s_singleton;
00495 };
00496 
00497 
00498 class Create_func_instr : public Create_func_arg2
00499 {
00500 public:
00501   using Create_func_arg2::create;
00502 
00503   virtual Item* create(Session *session, Item *arg1, Item *arg2)
00504   {
00505     return new (session->mem) Item_func_locate(arg1, arg2);
00506   }
00507 
00508   static Create_func_instr s_singleton;
00509 };
00510 
00511 
00512 class Create_func_isnull : public Create_func_arg1
00513 {
00514 public:
00515   using Create_func_arg1::create;
00516 
00517   virtual Item* create(Session *session, Item *arg1)
00518   {
00519     return new (session->mem) Item_func_isnull(arg1);
00520   }
00521 
00522   static Create_func_isnull s_singleton;
00523 };
00524 
00525 
00526 class Create_func_last_day : public Create_func_arg1
00527 {
00528 public:
00529   using Create_func_arg1::create;
00530 
00531   virtual Item* create(Session *session, Item *arg1)
00532   {
00533     return new (session->mem) Item_func_last_day(arg1);
00534   }
00535 
00536   static Create_func_last_day s_singleton;
00537 };
00538 
00539 
00540 class Create_func_last_insert_id : public Create_native_func
00541 {
00542 public:
00543   virtual Item* create_native(Session *session, str_ref name, List<Item> *item_list);
00544 
00545   static Create_func_last_insert_id s_singleton;
00546 };
00547 
00548 
00549 class Create_func_lcase : public Create_func_arg1
00550 {
00551 public:
00552   using Create_func_arg1::create;
00553 
00554   virtual Item* create(Session *session, Item *arg1)
00555   {
00556     return new (session->mem) Item_func_lcase(arg1);
00557   }
00558 
00559   static Create_func_lcase s_singleton;
00560 };
00561 
00562 
00563 class Create_func_least : public Create_native_func
00564 {
00565 public:
00566   virtual Item* create_native(Session *session, str_ref name, List<Item> *item_list);
00567 
00568   static Create_func_least s_singleton;
00569 };
00570 
00571 class Create_func_load_file : public Create_func_arg1
00572 {
00573 public:
00574   using Create_func_arg1::create;
00575 
00576   virtual Item* create(Session *session, Item *arg1)
00577   {
00578     return new (session->mem) Item_load_file(*session, arg1);
00579   }
00580 
00581   static Create_func_load_file s_singleton;
00582 };
00583 
00584 class Create_func_locate : public Create_native_func
00585 {
00586 public:
00587   virtual Item* create_native(Session *session, str_ref name, List<Item> *item_list);
00588 
00589   static Create_func_locate s_singleton;
00590 };
00591 
00592 class Create_func_lpad : public Create_func_arg3
00593 {
00594 public:
00595   using Create_func_arg3::create;
00596 
00597   virtual Item* create(Session *session, Item *arg1, Item *arg2, Item *arg3)
00598   {
00599     return new (session->mem) Item_func_lpad(*session, arg1, arg2, arg3);
00600   }
00601 
00602   static Create_func_lpad s_singleton;
00603 };
00604 
00605 Create_func_lpad Create_func_lpad::s_singleton;
00606 
00607 class Create_func_ltrim : public Create_func_arg1
00608 {
00609 public:
00610   using Create_func_arg1::create;
00611 
00612   virtual Item* create(Session *session, Item *arg1)
00613   {
00614     return new (session->mem) Item_func_ltrim(arg1);
00615   }
00616 
00617   static Create_func_ltrim s_singleton;
00618 };
00619 
00620 Create_func_ltrim Create_func_ltrim::s_singleton;
00621 
00622 class Create_func_makedate : public Create_func_arg2
00623 {
00624 public:
00625   using Create_func_arg2::create;
00626 
00627   virtual Item* create(Session *session, Item *arg1, Item *arg2)
00628   {
00629     return new (session->mem) Item_func_makedate(arg1, arg2);
00630   }
00631 
00632   static Create_func_makedate s_singleton;
00633 };
00634 
00635 Create_func_makedate Create_func_makedate::s_singleton;
00636 
00637 class Create_func_make_set : public Create_native_func
00638 {
00639 public:
00640   virtual Item* create_native(Session *session, str_ref name, List<Item> *item_list);
00641 
00642   static Create_func_make_set s_singleton;
00643 };
00644 
00645 
00646 class Create_func_monthname : public Create_func_arg1
00647 {
00648 public:
00649   using Create_func_arg1::create;
00650 
00651   virtual Item* create(Session *session, Item *arg1)
00652   {
00653     return new (session->mem) Item_func_monthname(arg1);
00654   }
00655 
00656   static Create_func_monthname s_singleton;
00657 };
00658 
00659 Create_func_monthname Create_func_monthname::s_singleton;
00660 
00661 class Create_func_name_const : public Create_func_arg2
00662 {
00663 public:
00664   using Create_func_arg2::create;
00665 
00666   virtual Item* create(Session *session, Item *arg1, Item *arg2);
00667 
00668   static Create_func_name_const s_singleton;
00669 };
00670 
00671 
00672 class Create_func_nullif : public Create_func_arg2
00673 {
00674 public:
00675   using Create_func_arg2::create;
00676 
00677   virtual Item* create(Session *session, Item *arg1, Item *arg2)
00678   {
00679     return new (session->mem) Item_func_nullif(arg1, arg2);
00680   }
00681 
00682   static Create_func_nullif s_singleton;
00683 };
00684 
00685 Create_func_nullif Create_func_nullif::s_singleton;
00686 
00687 class Create_func_oct : public Create_func_arg1
00688 {
00689 public:
00690   using Create_func_arg1::create;
00691 
00692   virtual Item* create(Session *session, Item *arg1);
00693 
00694   static Create_func_oct s_singleton;
00695 };
00696 
00697 class Create_func_period_add : public Create_func_arg2
00698 {
00699 public:
00700   using Create_func_arg2::create;
00701 
00702   virtual Item* create(Session *session, Item *arg1, Item *arg2)
00703   {
00704     return new (session->mem) Item_func_period_add(arg1, arg2);
00705   }
00706 
00707   static Create_func_period_add s_singleton;
00708 };
00709 
00710 
00711 class Create_func_period_diff : public Create_func_arg2
00712 {
00713 public:
00714   using Create_func_arg2::create;
00715 
00716   virtual Item* create(Session *session, Item *arg1, Item *arg2)
00717   {
00718     return new (session->mem) Item_func_period_diff(arg1, arg2);
00719   }
00720 
00721   static Create_func_period_diff s_singleton;
00722 };
00723 
00724 
00725 class Create_func_pi : public Create_func_arg0
00726 {
00727 public:
00728   using Create_func_arg0::create;
00729 
00730   virtual Item* create(Session *session)
00731   {
00732     return new (session->mem) Item_static_float_func("pi()", M_PI, 6, 8);
00733   }
00734 
00735   static Create_func_pi s_singleton;
00736 };
00737 
00738 class Create_func_radians : public Create_func_arg1
00739 {
00740 public:
00741   using Create_func_arg1::create;
00742 
00743   virtual Item* create(Session *session, Item *arg1)
00744   {
00745     return new (session->mem) Item_func_units("radians", arg1, M_PI/180, 0.0);
00746   }
00747 
00748   static Create_func_radians s_singleton;
00749 };
00750 
00751 
00752 class Create_func_round : public Create_native_func
00753 {
00754 public:
00755   virtual Item* create_native(Session *session, str_ref name, List<Item> *item_list);
00756 
00757   static Create_func_round s_singleton;
00758 };
00759 
00760 
00761 class Create_func_row_count : public Create_func_arg0
00762 {
00763 public:
00764   using Create_func_arg0::create;
00765 
00766   virtual Item* create(Session *session)
00767   {
00768     return new (session->mem) Item_func_row_count();
00769   }
00770 
00771   static Create_func_row_count s_singleton;
00772 };
00773 
00774 Create_func_row_count Create_func_row_count::s_singleton;
00775 
00776 class Create_func_rpad : public Create_func_arg3
00777 {
00778 public:
00779   using Create_func_arg3::create;
00780 
00781   virtual Item* create(Session *session, Item *arg1, Item *arg2, Item *arg3)
00782   {
00783     return new (session->mem) Item_func_rpad(*session, arg1, arg2, arg3);
00784   }
00785 
00786   static Create_func_rpad s_singleton;
00787 };
00788 
00789 Create_func_rpad Create_func_rpad::s_singleton;
00790 
00791 class Create_func_rtrim : public Create_func_arg1
00792 {
00793 public:
00794   using Create_func_arg1::create;
00795 
00796   virtual Item* create(Session *session, Item *arg1)
00797   {
00798     return new (session->mem) Item_func_rtrim(arg1);
00799   }
00800 
00801   static Create_func_rtrim s_singleton;
00802 };
00803 
00804 Create_func_rtrim Create_func_rtrim::s_singleton;
00805 
00806 class Create_func_sign : public Create_func_arg1
00807 {
00808 public:
00809   using Create_func_arg1::create;
00810 
00811   virtual Item* create(Session *session, Item *arg1)
00812   {
00813     return new (session->mem) Item_func_sign(arg1);
00814   }
00815 
00816   static Create_func_sign s_singleton;
00817 };
00818 
00819 Create_func_sign Create_func_sign::s_singleton;
00820 
00821 class Create_func_space : public Create_func_arg1
00822 {
00823 public:
00824   using Create_func_arg1::create;
00825 
00826   virtual Item* create(Session *session, Item *arg1);
00827 
00828   static Create_func_space s_singleton;
00829 };
00830 
00831 class Create_func_strcmp : public Create_func_arg2
00832 {
00833 public:
00834   using Create_func_arg2::create;
00835 
00836   virtual Item* create(Session *session, Item *arg1, Item *arg2)
00837   {
00838     return new (session->mem) Item_func_strcmp(arg1, arg2);
00839   }
00840   
00841   static Create_func_strcmp s_singleton;
00842 };
00843 
00844 Create_func_strcmp Create_func_strcmp::s_singleton;
00845 
00846 class Create_func_tan : public Create_func_arg1
00847 {
00848 public:
00849   using Create_func_arg1::create;
00850 
00851   virtual Item* create(Session *session, Item *arg1)
00852   {
00853     return new (session->mem) Item_func_tan(arg1);
00854   }
00855 
00856   static Create_func_tan s_singleton;
00857 };
00858 
00859 Create_func_tan Create_func_tan::s_singleton;
00860 
00861 class Create_func_time_format : public Create_func_arg2
00862 {
00863 public:
00864   using Create_func_arg2::create;
00865 
00866   virtual Item* create(Session *session, Item *arg1, Item *arg2)
00867   {
00868     return new (session->mem) Item_func_date_format(arg1, arg2, 1);
00869   }
00870 
00871   static Create_func_time_format s_singleton;
00872 };
00873 
00874 Create_func_time_format Create_func_time_format::s_singleton;
00875 
00876 class Create_func_to_days : public Create_func_arg1
00877 {
00878 public:
00879   using Create_func_arg1::create;
00880 
00881   virtual Item* create(Session *session, Item *arg1)
00882   {
00883     return new (session->mem) Item_func_to_days(arg1);
00884   }
00885 
00886   static Create_func_to_days s_singleton;
00887 };
00888 
00889 Create_func_to_days Create_func_to_days::s_singleton;
00890 
00891 class Create_func_ucase : public Create_func_arg1
00892 {
00893 public:
00894   using Create_func_arg1::create;
00895 
00896   virtual Item* create(Session *session, Item *arg1)
00897   {
00898     return new (session->mem) Item_func_ucase(arg1);
00899   }
00900 
00901   static Create_func_ucase s_singleton;
00902 };
00903 
00904 Create_func_ucase Create_func_ucase::s_singleton;
00905 
00906 class Create_func_unix_timestamp : public Create_native_func
00907 {
00908 public:
00909   virtual Item* create_native(Session *session, str_ref name, List<Item> *item_list);
00910 
00911   static Create_func_unix_timestamp s_singleton;
00912 };
00913 
00914 
00915 class Create_func_weekday : public Create_func_arg1
00916 {
00917 public:
00918   using Create_func_arg1::create;
00919 
00920   virtual Item* create(Session *session, Item *arg1)
00921   {
00922     return new (session->mem) Item_func_weekday(arg1, 0);
00923   }
00924 
00925   static Create_func_weekday s_singleton;
00926 };
00927 
00928 Create_func_weekday Create_func_weekday::s_singleton;
00929 
00930 /*
00931 =============================================================================
00932   IMPLEMENTATION
00933 =============================================================================
00934 */
00935 
00936 Create_udf_func Create_udf_func::s_singleton;
00937 
00938 Item* Create_udf_func::create(Session *session, str_ref name, List<Item> *item_list)
00939 {
00940   return create(session, plugin::Function::get(to_string(name)), item_list);
00941 }
00942 
00943 Item* Create_udf_func::create(Session *session, const plugin::Function *udf, List<Item> *item_list)
00944 {
00945   assert(udf);
00946   Item_func* func= (*udf)(&session->mem);
00947   if (!func->check_argument_count(item_list ? item_list->size() : 0))
00948   {
00949     my_error(ER_WRONG_PARAMCOUNT_TO_FUNCTION, MYF(0), func->func_name());
00950     return NULL;
00951   }
00952   if (item_list)
00953     func->set_arguments(*item_list);
00954   return func;
00955 }
00956 
00957 Item* Create_func_arg0::create(Session *session, str_ref name, List<Item> *item_list)
00958 {
00959   if (not item_list || not item_list->size())
00960     return create(session);
00961   my_error(ER_WRONG_PARAMCOUNT_TO_FUNCTION, MYF(0), name.data());
00962   return NULL;
00963 }
00964 
00965 Item* Create_func_arg1::create(Session *session, str_ref name, List<Item> *item_list)
00966 {
00967   if (not item_list || item_list->size() != 1)
00968   {
00969     my_error(ER_WRONG_PARAMCOUNT_TO_FUNCTION, MYF(0), name.data());
00970     return NULL;
00971   }
00972 
00973   Item *param_1= item_list->pop();
00974   if (param_1->is_autogenerated_name)
00975     return create(session, param_1);
00976   my_error(ER_WRONG_PARAMETERS_TO_NATIVE_FCT, MYF(0), name.data());
00977   return NULL;
00978 }
00979 
00980 Item* Create_func_arg2::create(Session *session, str_ref name, List<Item> *item_list)
00981 {
00982   if (not item_list || item_list->size() != 2)
00983   {
00984     my_error(ER_WRONG_PARAMCOUNT_TO_FUNCTION, MYF(0), name.data());
00985     return NULL;
00986   }
00987 
00988   Item *param_1= item_list->pop();
00989   Item *param_2= item_list->pop();
00990 
00991   if (param_1->is_autogenerated_name
00992     && param_2->is_autogenerated_name)
00993   return create(session, param_1, param_2);
00994   my_error(ER_WRONG_PARAMETERS_TO_NATIVE_FCT, MYF(0), name.data());
00995   return NULL;
00996 }
00997 
00998 Item* Create_func_arg3::create(Session *session, str_ref name, List<Item> *item_list)
00999 {
01000   if (not item_list || item_list->size() != 3)
01001   {
01002     my_error(ER_WRONG_PARAMCOUNT_TO_FUNCTION, MYF(0), name.data());
01003     return NULL;
01004   }
01005 
01006   Item *param_1= item_list->pop();
01007   Item *param_2= item_list->pop();
01008   Item *param_3= item_list->pop();
01009 
01010   if (param_1->is_autogenerated_name
01011     && param_2->is_autogenerated_name
01012     && param_3->is_autogenerated_name)
01013     return create(session, param_1, param_2, param_3);
01014   my_error(ER_WRONG_PARAMETERS_TO_NATIVE_FCT, MYF(0), name.data());
01015   return NULL;
01016 }
01017 
01018 Create_func_bin Create_func_bin::s_singleton;
01019 
01020 Item* Create_func_bin::create(Session *session, Item *arg1)
01021 {
01022   Item *i10= new (session->mem) Item_int(10, 2);
01023   Item *i2= new (session->mem) Item_int(2, 1);
01024   return new (session->mem) Item_func_conv(arg1, i10, i2);
01025 }
01026 
01027 Create_func_concat Create_func_concat::s_singleton;
01028 
01029 Item* Create_func_concat::create_native(Session *session, str_ref name, List<Item> *item_list)
01030 {
01031   if (item_list && item_list->size() >= 1)
01032     return new (session->mem) Item_func_concat(*session, *item_list);
01033   my_error(ER_WRONG_PARAMCOUNT_TO_FUNCTION, MYF(0), name.data());
01034   return NULL;
01035 }
01036 
01037 Create_func_concat_ws Create_func_concat_ws::s_singleton;
01038 
01039 Item* Create_func_concat_ws::create_native(Session *session, str_ref name, List<Item> *item_list)
01040 {
01041   /* "WS" stands for "With Separator": this function takes 2+ arguments */
01042   if (item_list && item_list->size() >= 2)
01043     return new (session->mem) Item_func_concat_ws(*session, *item_list);
01044   my_error(ER_WRONG_PARAMCOUNT_TO_FUNCTION, MYF(0), name.data());
01045   return NULL;
01046 }
01047 
01048 Create_func_cot Create_func_cot::s_singleton;
01049 
01050 Item*
01051 Create_func_cot::create(Session *session, Item *arg1)
01052 {
01053   Item *i1= new (session->mem) Item_int("1", 1, 1);
01054   Item *i2= new (session->mem) Item_func_tan(arg1);
01055   return new (session->mem) Item_func_div(session, i1, i2);
01056 }
01057 
01058 Create_func_datediff Create_func_datediff::s_singleton;
01059 
01060 Item*
01061 Create_func_datediff::create(Session *session, Item *arg1, Item *arg2)
01062 {
01063   Item *i1= new (session->mem) Item_func_to_days(arg1);
01064   Item *i2= new (session->mem) Item_func_to_days(arg2);
01065   return new (session->mem) Item_func_minus(i1, i2);
01066 }
01067 
01068 Create_func_export_set Create_func_export_set::s_singleton;
01069 
01070 Item*
01071 Create_func_export_set::create_native(Session *session, str_ref name, List<Item> *item_list)
01072 {
01073   switch (item_list ? item_list->size() : 0) 
01074   {
01075   case 3:
01076     {
01077       Item *param_1= item_list->pop();
01078       Item *param_2= item_list->pop();
01079       Item *param_3= item_list->pop();
01080       return new (session->mem) Item_func_export_set(param_1, param_2, param_3);
01081     }
01082   case 4:
01083     {
01084       Item *param_1= item_list->pop();
01085       Item *param_2= item_list->pop();
01086       Item *param_3= item_list->pop();
01087       Item *param_4= item_list->pop();
01088       return new (session->mem) Item_func_export_set(param_1, param_2, param_3, param_4);
01089     }
01090   case 5:
01091     {
01092       Item *param_1= item_list->pop();
01093       Item *param_2= item_list->pop();
01094       Item *param_3= item_list->pop();
01095       Item *param_4= item_list->pop();
01096       Item *param_5= item_list->pop();
01097       return new (session->mem) Item_func_export_set(param_1, param_2, param_3, param_4, param_5);
01098     }
01099   }
01100   my_error(ER_WRONG_PARAMCOUNT_TO_FUNCTION, MYF(0), name.data());
01101   return NULL;
01102 }
01103 
01104 
01105 Create_func_field Create_func_field::s_singleton;
01106 
01107 Item* Create_func_field::create_native(Session *session, str_ref name, List<Item> *item_list)
01108 {
01109   if (item_list && item_list->size() >= 2)
01110     return new (session->mem) Item_func_field(*item_list);
01111   my_error(ER_WRONG_PARAMCOUNT_TO_FUNCTION, MYF(0), name.data());
01112   return NULL;
01113 }
01114 
01115 Create_func_find_in_set Create_func_find_in_set::s_singleton;
01116 Create_func_found_rows Create_func_found_rows::s_singleton;
01117 Create_func_from_days Create_func_from_days::s_singleton;
01118 Create_func_from_unixtime Create_func_from_unixtime::s_singleton;
01119 
01120 Item* Create_func_from_unixtime::create_native(Session *session, str_ref name, List<Item> *item_list)
01121 {
01122   switch (item_list ? item_list->size() : 0) 
01123   {
01124   case 1:
01125     {
01126       Item *param_1= item_list->pop();
01127       return new (session->mem) Item_func_from_unixtime(param_1);
01128     }
01129   case 2:
01130     {
01131       Item *param_1= item_list->pop();
01132       Item *param_2= item_list->pop();
01133       Item *ut= new (session->mem) Item_func_from_unixtime(param_1);
01134       return new (session->mem) Item_func_date_format(ut, param_2, 0);
01135     }
01136   }
01137   my_error(ER_WRONG_PARAMCOUNT_TO_FUNCTION, MYF(0), name.data());
01138   return NULL;
01139 }
01140 
01141 Create_func_greatest Create_func_greatest::s_singleton;
01142 
01143 Item* Create_func_greatest::create_native(Session *session, str_ref name, List<Item> *item_list)
01144 {
01145   if (item_list && item_list->size() >= 2)
01146   return new (session->mem) Item_func_max(*item_list);
01147   my_error(ER_WRONG_PARAMCOUNT_TO_FUNCTION, MYF(0), name.data());
01148   return NULL;
01149 }
01150 
01151 Create_func_ifnull Create_func_ifnull::s_singleton;
01152 Create_func_instr Create_func_instr::s_singleton;
01153 Create_func_isnull Create_func_isnull::s_singleton;
01154 Create_func_last_day Create_func_last_day::s_singleton;
01155 Create_func_last_insert_id Create_func_last_insert_id::s_singleton;
01156 
01157 Item* Create_func_last_insert_id::create_native(Session *session, str_ref name, List<Item> *item_list)
01158 {
01159   int arg_count= item_list ? item_list->size() : 0;
01160   switch (arg_count) 
01161   {
01162   case 0:
01163     {
01164       return new (session->mem) Item_func_last_insert_id();
01165     }
01166   case 1:
01167     {
01168       Item *param_1= item_list->pop();
01169       return new (session->mem) Item_func_last_insert_id(param_1);
01170     }
01171   }
01172   my_error(ER_WRONG_PARAMCOUNT_TO_FUNCTION, MYF(0), name.data());
01173   return NULL;
01174 }
01175 
01176 Create_func_lcase Create_func_lcase::s_singleton;
01177 Create_func_least Create_func_least::s_singleton;
01178 
01179 Item* Create_func_least::create_native(Session *session, str_ref name, List<Item> *item_list)
01180 {
01181   if (item_list && item_list->size() >= 2)
01182     return new (session->mem) Item_func_min(*item_list);
01183   my_error(ER_WRONG_PARAMCOUNT_TO_FUNCTION, MYF(0), name.data());
01184   return NULL;
01185 }
01186 
01187 Create_func_load_file Create_func_load_file::s_singleton;
01188 Create_func_locate Create_func_locate::s_singleton;
01189 
01190 Item* Create_func_locate::create_native(Session *session, str_ref name, List<Item> *item_list)
01191 {
01192   switch (item_list ? item_list->size() : 0) 
01193   {
01194   case 2:
01195     {
01196       Item *param_1= item_list->pop();
01197       Item *param_2= item_list->pop();
01198       /* Yes, parameters in that order : 2, 1 */
01199       return new (session->mem) Item_func_locate(param_2, param_1);
01200     }
01201   case 3:
01202     {
01203       Item *param_1= item_list->pop();
01204       Item *param_2= item_list->pop();
01205       Item *param_3= item_list->pop();
01206       /* Yes, parameters in that order : 2, 1, 3 */
01207       return new (session->mem) Item_func_locate(param_2, param_1, param_3);
01208     }
01209   }
01210   my_error(ER_WRONG_PARAMCOUNT_TO_FUNCTION, MYF(0), name.data());
01211   return NULL;
01212 }
01213 
01214 Create_func_make_set Create_func_make_set::s_singleton;
01215 
01216 Item* Create_func_make_set::create_native(Session *session_arg, str_ref name, List<Item> *item_list)
01217 {
01218   if (not item_list || item_list->size() < 2)
01219   {
01220     my_error(ER_WRONG_PARAMCOUNT_TO_FUNCTION, MYF(0), name.data());
01221     return NULL;
01222   }
01223 
01224   Item *param_1= item_list->pop();
01225   return new (session_arg->mem) Item_func_make_set(*session_arg, param_1, *item_list);
01226 }
01227 
01228 Create_func_oct Create_func_oct::s_singleton;
01229 
01230 Item* Create_func_oct::create(Session *session, Item *arg1)
01231 {
01232   Item *i10= new (session->mem) Item_int((int32_t) 10,2);
01233   Item *i8= new (session->mem) Item_int((int32_t) 8,1);
01234   return new (session->mem) Item_func_conv(arg1, i10, i8);
01235 }
01236 
01237 Create_func_period_add Create_func_period_add::s_singleton;
01238 Create_func_period_diff Create_func_period_diff::s_singleton;
01239 Create_func_pi Create_func_pi::s_singleton;
01240 Create_func_radians Create_func_radians::s_singleton;
01241 Create_func_round Create_func_round::s_singleton;
01242 
01243 Item* Create_func_round::create_native(Session *session, str_ref name, List<Item> *item_list)
01244 {
01245   switch (item_list ? item_list->size() : 0) 
01246   {
01247   case 1:
01248     {
01249       Item *param_1= item_list->pop();
01250       Item *i0 = new (session->mem) Item_int("0", 0, 1);
01251       return new (session->mem) Item_func_round(param_1, i0, 0);
01252     }
01253   case 2:
01254     {
01255       Item *param_1= item_list->pop();
01256       Item *param_2= item_list->pop();
01257       return new (session->mem) Item_func_round(param_1, param_2, 0);
01258     }
01259   }
01260   my_error(ER_WRONG_PARAMCOUNT_TO_FUNCTION, MYF(0), name.data());
01261   return NULL;
01262 }
01263 
01264 Create_func_space Create_func_space::s_singleton;
01265 
01266 Item* Create_func_space::create(Session *session, Item *arg1)
01267 {
01273   const charset_info_st * const cs= session->variables.getCollation();
01274   Item *sp;
01275 
01276   if (cs->mbminlen > 1)
01277   {
01278     sp= new (session->mem) Item_string(str_ref(""), cs, DERIVATION_COERCIBLE);
01279     sp->str_value.copy(" ", 1, cs);
01280   }
01281   else
01282   {
01283     sp= new (session->mem) Item_string(str_ref(" "), cs, DERIVATION_COERCIBLE);
01284   }
01285 
01286   return new (session->mem) Item_func_repeat(*session, sp, arg1);
01287 }
01288 
01289 Create_func_unix_timestamp Create_func_unix_timestamp::s_singleton;
01290 
01291 Item* Create_func_unix_timestamp::create_native(Session *session, str_ref name, List<Item> *item_list)
01292 {
01293   switch (item_list ? item_list->size() : 0) 
01294   {
01295   case 0:
01296     return new (session->mem) Item_func_unix_timestamp();
01297   case 1:
01298     return new (session->mem) Item_func_unix_timestamp(item_list->pop());
01299   }
01300   my_error(ER_WRONG_PARAMCOUNT_TO_FUNCTION, MYF(0), name.data());
01301   return NULL;
01302 }
01303 
01304 
01305 struct Native_func_registry
01306 {
01307   const char* name;
01308   Create_func *builder;
01309 };
01310 
01311 #define BUILDER(F) & F::s_singleton
01312 
01313 /*
01314   MySQL native functions.
01315   MAINTAINER:
01316   - Keep sorted for human lookup. At runtime, a hash table is used.
01317   - keep 1 line per entry, it makes grep | sort easier
01318 */
01319 
01320 static Native_func_registry func_array[] =
01321 {
01322   { "BIN", BUILDER(Create_func_bin)},
01323   { "CONCAT", BUILDER(Create_func_concat)},
01324   { "CONCAT_WS", BUILDER(Create_func_concat_ws)},
01325   { "CONV", BUILDER(Create_func_conv)},
01326   { "COT", BUILDER(Create_func_cot)},
01327   { "DATEDIFF", BUILDER(Create_func_datediff)},
01328   { "DATE_FORMAT", BUILDER(Create_func_date_format)},
01329   { "DAYNAME", BUILDER(Create_func_dayname)},
01330   { "DAYOFMONTH", BUILDER(Create_func_dayofmonth)},
01331   { "DAYOFWEEK", BUILDER(Create_func_dayofweek)},
01332   { "DAYOFYEAR", BUILDER(Create_func_dayofyear)},
01333   { "DEGREES", BUILDER(Create_func_degrees)},
01334   { "EXPORT_SET", BUILDER(Create_func_export_set)},
01335   { "FIELD", BUILDER(Create_func_field)},
01336   { "FIND_IN_SET", BUILDER(Create_func_find_in_set)},
01337   { "FOUND_ROWS", BUILDER(Create_func_found_rows)},
01338   { "FROM_DAYS", BUILDER(Create_func_from_days)},
01339   { "FROM_UNIXTIME", BUILDER(Create_func_from_unixtime)},
01340   { "GREATEST", BUILDER(Create_func_greatest)},
01341   { "IFNULL", BUILDER(Create_func_ifnull)},
01342   { "INSTR", BUILDER(Create_func_instr)},
01343   { "ISNULL", BUILDER(Create_func_isnull)},
01344   { "LAST_DAY", BUILDER(Create_func_last_day)},
01345   { "LAST_INSERT_ID", BUILDER(Create_func_last_insert_id)},
01346   { "LCASE", BUILDER(Create_func_lcase)},
01347   { "LEAST", BUILDER(Create_func_least)},
01348   { "LOAD_FILE", BUILDER(Create_func_load_file)},
01349   { "LOCATE", BUILDER(Create_func_locate)},
01350   { "LOWER", BUILDER(Create_func_lcase)},
01351   { "LPAD", BUILDER(Create_func_lpad)},
01352   { "LTRIM", BUILDER(Create_func_ltrim)},
01353   { "MAKEDATE", BUILDER(Create_func_makedate)},
01354   { "MAKE_SET", BUILDER(Create_func_make_set)},
01355   { "MONTHNAME", BUILDER(Create_func_monthname)},
01356   { "NULLIF", BUILDER(Create_func_nullif)},
01357   { "OCT", BUILDER(Create_func_oct)},
01358   { "PERIOD_ADD", BUILDER(Create_func_period_add)},
01359   { "PERIOD_DIFF", BUILDER(Create_func_period_diff)},
01360   { "PI", BUILDER(Create_func_pi)},
01361   { "RADIANS", BUILDER(Create_func_radians)},
01362   { "ROUND", BUILDER(Create_func_round)},
01363   { "ROW_COUNT", BUILDER(Create_func_row_count)},
01364   { "RPAD", BUILDER(Create_func_rpad)},
01365   { "RTRIM", BUILDER(Create_func_rtrim)},
01366   { "SIGN", BUILDER(Create_func_sign)},
01367   { "SPACE", BUILDER(Create_func_space)},
01368   { "STRCMP", BUILDER(Create_func_strcmp)},
01369   { "TAN", BUILDER(Create_func_tan)},
01370   { "TIME_FORMAT", BUILDER(Create_func_time_format)},
01371   { "TO_DAYS", BUILDER(Create_func_to_days)},
01372   { "UCASE", BUILDER(Create_func_ucase)},
01373   { "UNIX_TIMESTAMP", BUILDER(Create_func_unix_timestamp)},
01374   { "UPPER", BUILDER(Create_func_ucase)},
01375   { "WEEKDAY", BUILDER(Create_func_weekday)},
01376   { NULL, NULL}
01377 };
01378 
01379 /*
01380   Load the hash table for native functions.
01381   Note: this code is not thread safe, and is intended to be used at server
01382   startup only (before going multi-threaded)
01383 */
01384 
01385 void item_create_init()
01386 {
01387   for (Native_func_registry* func= func_array; func->builder; func++)
01388     FunctionContainer::getMutableMap()[func->name]= func->builder;
01389 }
01390 
01391 Create_func* find_native_function_builder(str_ref name)
01392 {
01393   return find_ptr2(FunctionContainer::getMap(), name.data());
01394 }
01395 
01396 Item* create_func_char_cast(Session *session, Item *a, int len, const charset_info_st* cs)
01397 {
01398   return new (session->mem) Item_char_typecast(a, len, cs ? cs : session->variables.getCollation());
01399 }
01400 
01401 Item* create_func_cast(Session *session, Item *a, Cast_target cast_type, const char *c_len, const char *c_dec, const charset_info_st * const cs)
01402 {
01403   switch (cast_type) 
01404   {
01405   case ITEM_CAST_SIGNED:
01406     return new (session->mem) function::cast::Signed(a);
01407   case ITEM_CAST_UNSIGNED:
01408     return new (session->mem) function::cast::Unsigned(a);
01409   case ITEM_CAST_BINARY:
01410     return new (session->mem) Item_func_binary(a);
01411   case ITEM_CAST_BOOLEAN:
01412     return new (session->mem) function::cast::Boolean(a);
01413   case ITEM_CAST_TIME:
01414     return new (session->mem) function::cast::Time(a);
01415   case ITEM_CAST_DATE:
01416     return new (session->mem) Item_date_typecast(a);
01417   case ITEM_CAST_DATETIME:
01418     return new (session->mem) Item_datetime_typecast(a);
01419   case ITEM_CAST_DECIMAL:
01420     {
01421       uint32_t len= c_len ? atoi(c_len) : 0;
01422       uint32_t dec= c_dec ? atoi(c_dec) : 0;
01423       class_decimal_trim(&len, &dec);
01424       if (len < dec)
01425       {
01426         my_error(ER_M_BIGGER_THAN_D, MYF(0), "");
01427         return 0;
01428       }
01429       if (len > DECIMAL_MAX_PRECISION)
01430       {
01431         my_error(ER_TOO_BIG_PRECISION, MYF(0), len, a->name, DECIMAL_MAX_PRECISION);
01432         return 0;
01433       }
01434       if (dec > DECIMAL_MAX_SCALE)
01435       {
01436         my_error(ER_TOO_BIG_SCALE, MYF(0), dec, a->name, DECIMAL_MAX_SCALE);
01437         return 0;
01438       }
01439       return new (session->mem) Item_decimal_typecast(a, len, dec);
01440     }
01441   case ITEM_CAST_CHAR:
01442     return create_func_char_cast(session, a, c_len ? atoi(c_len) : -1, cs);
01443   }
01444   return NULL;
01445 }
01446 
01447 } /* namespace drizzled */