~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/crc32/crc32udf.cc

  • Committer: Monty Taylor
  • Date: 2008-09-14 22:10:23 UTC
  • mto: This revision was merged to the branch mainline in revision 388.
  • Revision ID: monty@inaugust.com-20080914221023-otz8vuui590zp5yf
Got rid of libsqlcommon and some surious defines.

Show diffs side-by-side

added added

removed removed

Lines of Context:
13
13
   along with this program; if not, write to the Free Software
14
14
   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA */
15
15
 
16
 
#include <config.h>
17
 
 
18
 
#include CSTDINT_H
19
16
#include <drizzled/common_includes.h>
20
 
#include <drizzled/item_func.h>
21
17
#include <zlib.h>
22
18
 
23
 
class Item_func_crc32 :public Item_int_func
24
 
{
25
 
  String value;
26
 
public:
27
 
  Item_func_crc32() :Item_int_func() { unsigned_flag= 1; }
28
 
  const char *func_name() const { return "crc32"; }
29
 
  void fix_length_and_dec() { max_length=10; }
30
 
  int64_t val_int();
31
 
};
32
 
 
33
 
int64_t Item_func_crc32::val_int()
34
 
{
35
 
  assert(fixed == 1);
36
 
  String *res=args[0]->val_str(&value);
37
 
  if (!res)
38
 
  {
39
 
    null_value=1;
40
 
    return 0; /* purecov: inspected */
41
 
  }
42
 
  null_value=0;
43
 
  return (int64_t) crc32(0L, (unsigned char*)res->ptr(), res->length());
44
 
}
45
 
 
46
 
Item_func* create_crc32udf_item(MEM_ROOT* m)
47
 
{
48
 
  return  new (m) Item_func_crc32();
49
 
}
50
 
 
51
 
static struct udf_func crc32udf = {
52
 
  { C_STRING_WITH_LEN("crc32") },
53
 
  create_crc32udf_item
54
 
};
 
19
bool udf_init_crc32udf(UDF_INIT *initid, UDF_ARGS *args, char *message)
 
20
{
 
21
  /* initid->ptr keeps state for between udf_init_foo and udf_deinit_foo */
 
22
  initid->ptr= NULL;
 
23
 
 
24
  if (args->arg_count != 1)
 
25
   {
 
26
      strcpy(message,"CRC32() requires one arguments");
 
27
      return 1;
 
28
   }
 
29
 
 
30
   if (args->arg_type[0] != STRING_RESULT)
 
31
   {
 
32
      strcpy(message,"CRC32() requires a string");
 
33
      return 1;
 
34
   }
 
35
 
 
36
  return 0;
 
37
}
 
38
 
 
39
long long udf_doit_crc32(UDF_INIT *initid, UDF_ARGS *args, char *result,
 
40
                         unsigned long *length, char *is_null, char *error)
 
41
{
 
42
  (void)initid;
 
43
  (void)result;
 
44
  (void)length;
 
45
  (void)is_null;
 
46
  (void)error;
 
47
  return (long long) crc32(0L, (uchar*)args->args[0], args->lengths[0]);
 
48
}
 
49
 
 
50
void udf_deinit_crc32udf(UDF_INIT *initid)
 
51
{
 
52
  (void)initid;
 
53
  /* if we allocated initid->ptr, free it here */
 
54
  return;
 
55
}
 
56
 
55
57
 
56
58
static int crc32udf_plugin_init(void *p)
57
59
{
58
 
  udf_func **f = (udf_func**) p;
59
 
 
60
 
  *f= &crc32udf;
 
60
  udf_func *udff= (udf_func *) p;
 
61
  static char crc32str[6];
 
62
 
 
63
  strcpy(crc32str,"crc32");
 
64
 
 
65
  udff->name.str= crc32str;
 
66
  udff->name.length= strlen("crc32");
 
67
  udff->type= UDFTYPE_FUNCTION;
 
68
  udff->returns= INT_RESULT;
 
69
  udff->func_init= udf_init_crc32udf;
 
70
  udff->func_deinit= udf_deinit_crc32udf;
 
71
  udff->func= (Udf_func_any) udf_doit_crc32;
61
72
 
62
73
  return 0;
63
74
}