~drizzle-trunk/drizzle/development

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/* Copyright (C) 2006 MySQL AB

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; version 2 of the License.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA */

#include <drizzled/mysql_priv.h>
#include <stdlib.h>
#include <ctype.h>
#include <drizzled/plugin.h>

bool udf_init_hello_world(UDF_INIT *initid, UDF_ARGS *args, char *message)
{
  /* this is how to fail */
  if (args->arg_count != 0)  {
    strncpy(message, "Too many arguments", MYSQL_ERRMSG_SIZE);
    return 1;
  }

  /* initid->ptr keeps state for between udf_init_foo and udf_deinit_foo */
  initid->ptr= NULL;

  return 0;
}

char *udf_doit_hello_world(UDF_INIT *initid, UDF_ARGS *args, char *result,
			   unsigned long *length, char *is_null, char *error)
{
  /* We don't use these, so void them out */
  (void)initid;
  (void)args;

  /* "result" is preallocated 255 bytes for me, if i want to use it */
  strncpy(result, "Hello, world!", 255);

  /* if set to 255 or less, MySQL treats the result as a varchar
     if set to greater than 255, MySQL treats the result as a blob */
  *length= strlen("Hello, world!");

  /* is_null is already zero, this is a demonstration */
  *is_null= 0;

  /* error is already zero, this is a demonstration */
  *error= 0;

  return result;
}

void udf_deinit_hello_world(UDF_INIT *initid)
{
  /* We don't use this, so void it out */
  (void)initid;

  /* if we allocated initid->ptr, free it here */
  return;
}


static int hello_world_plugin_init(void *p)
{
  udf_func *udff= (udf_func *) p;

  udff->name.str= (char *)"hello_world";
  udff->name.length= strlen("hello_world");
  udff->type= UDFTYPE_FUNCTION;
  udff->returns= STRING_RESULT;
  udff->func_init= udf_init_hello_world;
  udff->func_deinit= udf_deinit_hello_world;
  udff->func= (Udf_func_any) udf_doit_hello_world;

  return 0;
}

static int hello_world_plugin_deinit(void *p)
{
  /* We don't use this, so void it out */
  (void)p;

  /* There is nothing to de-init here, but if 
   * something is needed from the udf_func, it 
   * can be had from p with:
   *   udf_func *udff = (udf_func *) p;
   */

  return 0;
}

mysql_declare_plugin(hello_world)
{
  MYSQL_UDF_PLUGIN,
  "hello_world",
  "1.0",
  "Mark Atwood",
  "Hello, world!",
  PLUGIN_LICENSE_GPL,
  hello_world_plugin_init, /* Plugin Init */
  hello_world_plugin_deinit, /* Plugin Deinit */
  NULL,   /* status variables */
  NULL,   /* system variables */
  NULL    /* config options */
}
mysql_declare_plugin_end;