~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/crc32/crc32udf.cc

  • Committer: Brian Aker
  • Date: 2010-01-22 00:53:13 UTC
  • Revision ID: brian@gaz-20100122005313-jmizcbcdi1lt4tcx
Revert db patch.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* Copyright (C) 2006 MySQL AB
2
 
 
3
 
   This program is free software; you can redistribute it and/or modify
4
 
   it under the terms of the GNU General Public License as published by
5
 
   the Free Software Foundation; version 2 of the License.
6
 
 
7
 
   This program is distributed in the hope that it will be useful,
8
 
   but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10
 
   GNU General Public License for more details.
11
 
 
12
 
   You should have received a copy of the GNU General Public License
13
 
   along with this program; if not, write to the Free Software
14
 
   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA */
15
 
 
16
 
#include <drizzled/common_includes.h>
17
 
#include <zlib.h>
18
 
 
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
 
 
57
 
 
58
 
static int crc32udf_plugin_init(void *p)
59
 
{
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;
72
 
 
73
 
  return 0;
74
 
}
75
 
 
76
 
static int crc32udf_plugin_deinit(void *p)
77
 
{
78
 
  udf_func *udff = (udf_func *) p;
79
 
  (void)udff;
80
 
  return 0;
81
 
}
82
 
 
83
 
mysql_declare_plugin(crc32)
84
 
{
85
 
  DRIZZLE_UDF_PLUGIN,
86
 
  "crc32",
87
 
  "1.0",
88
 
  "Stewart Smith",
89
 
  "UDF for computing CRC32",
90
 
  PLUGIN_LICENSE_GPL,
91
 
  crc32udf_plugin_init, /* Plugin Init */
92
 
  crc32udf_plugin_deinit, /* Plugin Deinit */
93
 
  NULL,   /* status variables */
94
 
  NULL,   /* system variables */
95
 
  NULL    /* config options */
96
 
}
97
 
mysql_declare_plugin_end;
 
1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
 
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
 
3
 *
 
4
 *  Copyright (C) 2008 Sun Microsystems
 
5
 *
 
6
 *  This program is free software; you can redistribute it and/or modify
 
7
 *  it under the terms of the GNU General Public License as published by
 
8
 *  the Free Software Foundation; version 2 of the License.
 
9
 *
 
10
 *  This program is distributed in the hope that it will be useful,
 
11
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
 *  GNU General Public License for more details.
 
14
 *
 
15
 *  You should have received a copy of the GNU General Public License
 
16
 *  along with this program; if not, write to the Free Software
 
17
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 
18
 */
 
19
 
 
20
#include "config.h"
 
21
#include <drizzled/plugin/function.h>
 
22
#include <drizzled/item/func.h>
 
23
#include <drizzled/algorithm/crc32.h>
 
24
 
 
25
#include <string>
 
26
 
 
27
using namespace std;
 
28
using namespace drizzled;
 
29
 
 
30
class Crc32Function :public Item_int_func
 
31
{
 
32
  String value;
 
33
public:
 
34
  int64_t val_int();
 
35
  
 
36
  Crc32Function() :Item_int_func() 
 
37
  { 
 
38
    unsigned_flag= true; 
 
39
  }
 
40
  
 
41
  const char *func_name() const 
 
42
  { 
 
43
    return "crc32"; 
 
44
  }
 
45
  
 
46
  void fix_length_and_dec() 
 
47
  { 
 
48
    max_length= 10; 
 
49
  }
 
50
  
 
51
  bool check_argument_count(int n) 
 
52
  { 
 
53
    return (n == 1); 
 
54
  }
 
55
};
 
56
 
 
57
int64_t Crc32Function::val_int()
 
58
{
 
59
  assert(fixed == true);
 
60
  String *res=args[0]->val_str(&value);
 
61
  
 
62
  if (res == NULL)
 
63
  {
 
64
    null_value= true;
 
65
    return 0;
 
66
  }
 
67
 
 
68
  null_value= false;
 
69
  return static_cast<int64_t>(drizzled::algorithm::crc32(res->ptr(), res->length()));
 
70
}
 
71
 
 
72
plugin::Create_function<Crc32Function> *crc32udf= NULL;
 
73
 
 
74
static int initialize(plugin::Registry &registry)
 
75
{
 
76
  crc32udf= new plugin::Create_function<Crc32Function>("crc32");
 
77
  registry.add(crc32udf);
 
78
  return 0;
 
79
}
 
80
 
 
81
static int finalize(plugin::Registry &registry)  
 
82
{
 
83
  registry.remove(crc32udf);
 
84
  delete crc32udf;
 
85
  return 0;
 
86
}
 
87
 
 
88
DRIZZLE_PLUGIN(initialize, finalize, NULL, NULL);