~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/hello_world/hello_world.cc

  • Committer: Stewart Smith
  • Date: 2008-09-25 08:57:27 UTC
  • mto: This revision was merged to the branch mainline in revision 408.
  • Revision ID: stewart@flamingspork.com-20080925085727-lwemz5gl6tvrp9ku
UDFs are now normal Item_func objects. Simplifies handling them a lot.

Show diffs side-by-side

added added

removed removed

Lines of Context:
14
14
   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA */
15
15
 
16
16
#include <drizzled/common_includes.h>
 
17
#include <drizzled/item_func.h>
 
18
#include <drizzled/item_strfunc.h>
17
19
 
18
 
bool udf_init_hello_world(UDF_INIT *initid, UDF_ARGS *args, char *message)
 
20
class Item_func_hello_world : public Item_str_func
19
21
{
20
 
  /* this is how to fail */
21
 
  if (args->arg_count != 0)  {
22
 
    strncpy(message, "Too many arguments", DRIZZLE_ERRMSG_SIZE);
23
 
    return 1;
 
22
public:
 
23
  const char *func_name() const { return "hello_world"; }
 
24
  String *val_str(String* s) {
 
25
    s->set(STRING_WITH_LEN("Hello World!"),system_charset_info);
 
26
    return s;
 
27
  };
 
28
  void fix_length_and_dec() {
 
29
    max_length=strlen("Hello World!");
24
30
  }
25
 
 
26
 
  /* initid->ptr keeps state for between udf_init_foo and udf_deinit_foo */
27
 
  initid->ptr= NULL;
28
 
 
29
 
  return 0;
30
 
}
31
 
 
32
 
char *udf_doit_hello_world(UDF_INIT *initid, UDF_ARGS *args, char *result,
33
 
                           unsigned long *length, char *is_null, char *error)
34
 
{
35
 
  /* We don't use these, so void them out */
36
 
  (void)initid;
37
 
  (void)args;
38
 
 
39
 
  /* "result" is preallocated 255 bytes for me, if i want to use it */
40
 
  strncpy(result, "Hello, world!", 255);
41
 
 
42
 
  /* if set to 255 or less, MySQL treats the result as a varchar
43
 
     if set to greater than 255, MySQL treats the result as a blob */
44
 
  *length= strlen("Hello, world!");
45
 
 
46
 
  /* is_null is already zero, this is a demonstration */
47
 
  *is_null= 0;
48
 
 
49
 
  /* error is already zero, this is a demonstration */
50
 
  *error= 0;
51
 
 
52
 
  return result;
53
 
}
54
 
 
55
 
void udf_deinit_hello_world(UDF_INIT *initid)
56
 
{
57
 
  /* We don't use this, so void it out */
58
 
  (void)initid;
59
 
 
60
 
  /* if we allocated initid->ptr, free it here */
61
 
  return;
62
 
}
63
 
 
 
31
};
 
32
 
 
33
Item_func* create_hello_world_udf_item(MEM_ROOT* m)
 
34
{
 
35
  return  new (m) Item_func_hello_world();
 
36
}
 
37
 
 
38
static struct udf_func hello_world_udf = {
 
39
  { C_STRING_WITH_LEN("hello_world") },
 
40
  create_hello_world_udf_item
 
41
};
64
42
 
65
43
static int hello_world_plugin_init(void *p)
66
44
{
67
 
  udf_func *udff= (udf_func *) p;
 
45
  udf_func **f = (udf_func**) p;
68
46
 
69
 
  udff->name.str= (char *)"hello_world";
70
 
  udff->name.length= strlen("hello_world");
71
 
  udff->type= UDFTYPE_FUNCTION;
72
 
  udff->returns= STRING_RESULT;
73
 
  udff->func_init= udf_init_hello_world;
74
 
  udff->func_deinit= udf_deinit_hello_world;
75
 
  udff->func= (Udf_func_any) udf_doit_hello_world;
 
47
  *f= &hello_world_udf;
76
48
 
77
49
  return 0;
78
50
}
79
51
 
80
52
static int hello_world_plugin_deinit(void *p)
81
53
{
82
 
  /* We don't use this, so void it out */
83
 
  (void)p;
84
 
 
85
 
  /* There is nothing to de-init here, but if 
86
 
   * something is needed from the udf_func, it 
87
 
   * can be had from p with:
88
 
   *   udf_func *udff = (udf_func *) p;
89
 
   */
90
 
 
 
54
  udf_func *udff = (udf_func *) p;
 
55
  (void)udff;
91
56
  return 0;
92
57
}
93
58
 
 
59
 
94
60
mysql_declare_plugin(hello_world)
95
61
{
96
62
  DRIZZLE_UDF_PLUGIN,