~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/compress/compressudf.cc

  • Committer: Monty Taylor
  • Date: 2008-10-23 00:05:28 UTC
  • Revision ID: monty@inaugust.com-20081023000528-grdvrd8c4058nutm
Moved my_handler to myisam, which is where it actually belongs.

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
 
#include <drizzled/plugin/function.h>
18
 
#include <drizzled/item/func.h>
19
 
#include <drizzled/function/str/strfunc.h>
20
 
#include <drizzled/error.h>
21
 
#include <drizzled/sql_error.h>
22
 
#include <drizzled/current_session.h>
 
16
#include <drizzled/common_includes.h>
 
17
#include <drizzled/item_func.h>
 
18
#include <drizzled/item_strfunc.h>
 
19
#include <drizzled/drizzled_error_messages.h>
23
20
#include <zlib.h>
24
 
#include "plugin/compression/compress.h"
25
 
 
26
 
#include <string>
27
 
 
28
 
using namespace std;
29
 
using namespace drizzled;
 
21
 
 
22
class Item_func_compress: public Item_str_func
 
23
{
 
24
  String buffer;
 
25
public:
 
26
  Item_func_compress():Item_str_func(){}
 
27
  void fix_length_and_dec(){max_length= (args[0]->max_length*120)/100+12;}
 
28
  const char *func_name() const{return "compress";}
 
29
  String *val_str(String *) ;
 
30
};
30
31
 
31
32
String *Item_func_compress::val_str(String *str)
32
33
{
57
58
  new_size= res->length() + res->length() / 5 + 12;
58
59
 
59
60
  // Check new_size overflow: new_size <= res->length()
60
 
  if (((uint32_t) (new_size+5) <= res->length()) ||
 
61
  if (((uint32_t) (new_size+5) <= res->length()) || 
61
62
      buffer.realloc((uint32_t) new_size + 4 + 1))
62
63
  {
63
64
    null_value= 1;
68
69
 
69
70
  // As far as we have checked res->is_empty() we can use ptr()
70
71
  if ((err= compress(body, &new_size,
71
 
                     (const Bytef*)res->ptr(), res->length())) != Z_OK)
 
72
                     (const Bytef*)res->ptr(), res->length())) != Z_OK)
72
73
  {
73
74
    code= err==Z_MEM_ERROR ? ER_ZLIB_Z_MEM_ERROR : ER_ZLIB_Z_BUF_ERROR;
74
 
    push_warning(current_session, DRIZZLE_ERROR::WARN_LEVEL_ERROR,
75
 
                 code, ER(code));
 
75
    push_warning(current_session, DRIZZLE_ERROR::WARN_LEVEL_ERROR, code, ER(code));
76
76
    null_value= 1;
77
77
    return 0;
78
78
  }
93
93
}
94
94
 
95
95
 
 
96
Item_func* create_compressudf_item(MEM_ROOT* m)
 
97
{
 
98
  return  new (m) Item_func_compress();
 
99
}
 
100
 
 
101
static struct udf_func compressudf = {
 
102
  { C_STRING_WITH_LEN("compress") },
 
103
  create_compressudf_item
 
104
};
 
105
 
 
106
static int compressudf_plugin_init(void *p)
 
107
{
 
108
  udf_func **f = (udf_func**) p;
 
109
 
 
110
  *f= &compressudf;
 
111
 
 
112
  return 0;
 
113
}
 
114
 
 
115
static int compressudf_plugin_deinit(void *p)
 
116
{
 
117
  udf_func *udff = (udf_func *) p;
 
118
  (void)udff;
 
119
  return 0;
 
120
}
 
121
 
 
122
mysql_declare_plugin(compress)
 
123
{
 
124
  DRIZZLE_UDF_PLUGIN,
 
125
  "compress",
 
126
  "1.0",
 
127
  "Stewart Smith",
 
128
  "UDF for compress()",
 
129
  PLUGIN_LICENSE_GPL,
 
130
  compressudf_plugin_init, /* Plugin Init */
 
131
  compressudf_plugin_deinit, /* Plugin Deinit */
 
132
  NULL,   /* status variables */
 
133
  NULL,   /* system variables */
 
134
  NULL    /* config options */
 
135
}
 
136
mysql_declare_plugin_end;