database.sarang.net
UserID
Passwd
Database
DBMS
ㆍMySQL
PostgreSQL
Firebird
Oracle
Informix
Sybase
MS-SQL
DB2
Cache
CUBRID
LDAP
ALTIBASE
Tibero
DB 문서들
스터디
Community
공지사항
자유게시판
구인|구직
DSN 갤러리
도움주신분들
Admin
운영게시판
최근게시물
MySQL Devel 21133 게시물 읽기
 News | Q&A | Columns | Tutorials | Devel | Files | Links
No. 21133
MySQL UDF -- square()
작성자
정재익(advance)
작성일
2004-02-24 14:35
조회수
11,397

Just an example of how to do the casting when working with integer values. Only one function, square(), which squares a given value. eg: SELECT square(2) -> 4

 

============

/*
   Copyright ?1998 by Nem W Schlecht
   This file is public domain and comes with NO WARRANTY of any kind

MySQL SQL code for creating/deleting this function in the 'mysql' database:

CREATE FUNCTION square RETURNS INTEGER SONAME "int_example.so";
DROP FUNCTION square;

Compile line:

CC -DMYSQL_SERVER -DDEFAULT_MYSQL_HOME="\"/local/mysql\""
-DDATADIR="\"/local/mysql/var\"" -DSHAREDIR="\"/local/mysql/share/mysql\""
-DHAVE_CONFIG_H -DUSE_ALARM_THREAD -O3 -DDBUG_OFF
-I/local/mysql/include/mysql -I/local/src/SQL/mysql-3.22.4-beta/include
-I/local/src/SQL/mysql-3.22.4-beta -shared -o int_example.so int_example.cc

*/

#include <global.h>
#include <my_sys.h>
#include <mysql.h>
#include <m_ctype.h>

#ifdef HAVE_DLOPEN

extern "C" {

// The initialization function proto.
my_bool square_init(UDF_INIT *initid, UDF_ARGS *args, char *message);

// The de-initialzition function proto.
void square_deinit(UDF_INIT *initid);

// The actual function proto.
longlong square(UDF_INIT *initid, UDF_ARGS *args, char *is_null, char *error);
}


//
// Initialization function - make sure correct number of arguments are passed
// and that the argument is an integer value
my_bool square_init(UDF_INIT *initid, UDF_ARGS *args, char *message)
{
  if (args->arg_count != 1 || args->arg_type[0] != INT_RESULT) {
    strcpy(message,"Wrong arguments to square");
    return 1;
  }

  initid->maybe_null = 0;
  return 0;
}

//
// De-initialization function - nothing spectial
void square_deinit(UDF_INIT *initid)
{
}

//
// 'square' function.  Returns the square of the integer passed to it in the
// args structure
long long square(UDF_INIT *initid, UDF_ARGS *args, char *is_null, char *error)
{
    int number = *((long long*) args->args[0]);
    return number*number;
}

#endif /* HAVE_DLOPEN */

[Top]
No.
제목
작성자
작성일
조회
23670MySQL UDF -- Longest common subsequence
정재익
2004-02-16
9685
23669MySQL UDF Token() - Tokenizing (Windows version) [1]
이노성
2004-03-08
10586
21136MySQL UDF -- Unix user interface
정재익
2004-02-24
12209
21133MySQL UDF -- square()
정재익
2004-02-24
11397
21132MySQL UDF -- soundex
정재익
2004-02-24
10499
21129MySQL UDF -- ip lookup
정재익
2004-02-24
10817
21128MySQL UDF -- unprefix
정재익
2004-02-24
10192
Valid XHTML 1.0!
All about the DATABASE... Copyleft 1999-2023 DSN, All rights reserved.
작업시간: 0.050초, 이곳 서비스는
	PostgreSQL v16.1로 자료를 관리합니다