e576bb2002-10-11Martin Nilsson /* || This file is part of Pike. For copyright information see COPYRIGHT. || Pike is distributed under GPL, LGPL and MPL. See the file COPYING || for more information. */
1b10db2002-10-08Martin Nilsson 
ab6aec1997-02-11Fredrik Hübinette (Hubbe) /* * SQL database functionality for Pike * * Henrik Grubbström 1996-12-21 */
b788cd1998-07-04Henrik Grubbström (Grubba) /* Master Pike headerfile */ #include "global.h"
ab6aec1997-02-11Fredrik Hübinette (Hubbe) #ifdef HAVE_CONFIG_H #include "config.h" #endif /* HAVE_CONFIG_H */ #ifdef HAVE_MYSQL /* * Includes */
9a09222003-04-30Henrik Grubbström (Grubba) #ifdef HAVE_WINSOCK2_H #include <winsock2.h> #elif defined(HAVE_WINSOCK_H)
bfbae31998-09-01Fredrik Hübinette (Hubbe) #include <winsock.h> #endif
ab6aec1997-02-11Fredrik Hübinette (Hubbe) /* From the mysql-dist */ /* Workaround for versions prior to 3.20.0 not beeing protected for * multiple inclusion. */ #ifndef _mysql_h #ifdef HAVE_MYSQL_H #include <mysql.h> #else #ifdef HAVE_MYSQL_MYSQL_H #include <mysql/mysql.h> #else
def05c2002-01-27Martin Stjernholm #ifndef DISABLE_BINARY
a4a1722000-12-05Per Hedbor #error Need mysql.h header-file
def05c2002-01-27Martin Stjernholm #endif
ab6aec1997-02-11Fredrik Hübinette (Hubbe) #endif /* HAVE_MYSQL_MYSQL_H */ #endif /* HAVE_MYSQL_H */ #ifndef _mysql_h #define _mysql_h #endif #endif
390bf62000-01-03Dan Nelson #ifdef HAVE_ERRMSG_H #include <errmsg.h> #else /* !HAVE_ERRMSG_H */ #ifdef HAVE_MYSQL_ERRMSG_H #include <mysql/errmsg.h> #endif /* HAVE_MYSQL_ERRMSG_H */ #endif /* HAVE_ERRMGS_H */
ab6aec1997-02-11Fredrik Hübinette (Hubbe) #endif /* HAVE_MYSQL */ /* From the Pike-dist */
51ef5c2002-10-21Marcus Comstedt #include "module.h"
08885f1997-04-20Henrik Grubbström (Grubba) #include "svalue.h" #include "object.h" #include "stralloc.h" #include "interpret.h"
b2d3e42000-12-01Fredrik Hübinette (Hubbe) #include "pike_error.h"
08885f1997-04-20Henrik Grubbström (Grubba) #include "threads.h" #include "program.h" #include "operators.h" #include "builtin_functions.h"
159ce91999-11-16Henrik Grubbström (Grubba) #include "fd_control.h"
de9cfa2003-02-07Marcus Agehall #include "mapping.h"
0ba1be2004-03-06Martin Nilsson #include "bignum.h"
440cf92005-11-16Henrik Grubbström (Grubba) #include "module_support.h"
ab6aec1997-02-11Fredrik Hübinette (Hubbe)  /* System includes */ #ifdef HAVE_STRING_H #include <string.h> #endif #ifdef HAVE_MEMORY_H #include <memory.h> #endif
1b8ceb2011-08-25Henrik Grubbström (Grubba) #ifdef HAVE_POLL_H #include <poll.h> #endif
6ad2372002-05-11Martin Nilsson #define sp Pike_sp
ab6aec1997-02-11Fredrik Hübinette (Hubbe) #ifdef HAVE_MYSQL /* Local includes */ #include "precompiled_mysql.h" /* * Globals */
0be0372001-02-10Henrik Grubbström (Grubba) /*! @module Mysql *! *! This module enables access to the Mysql database from within Pike. *!
322be02014-08-25Per Hedbor  *! @note *! You typically don't want to access this module directly, instead *! use @[Sql.Sql()] with an "mysql://" URL.
0be0372001-02-10Henrik Grubbström (Grubba)  *! *! @seealso
2a3a822002-11-18Martin Nilsson  *! @[Mysql.mysql], @[Mysql.mysql_result], @[Sql.Sql]
0be0372001-02-10Henrik Grubbström (Grubba)  */ /*! @class mysql *!
322be02014-08-25Per Hedbor  *! Low level interface to the Mysql database.
0be0372001-02-10Henrik Grubbström (Grubba)  *! *! This class enables access to the Mysql database from within Pike. *! *! @seealso
230dd22002-11-26Henrik Grubbström (Grubba)  *! @[Mysql.mysql_result], @[Sql.Sql]
0be0372001-02-10Henrik Grubbström (Grubba)  */
ab6aec1997-02-11Fredrik Hübinette (Hubbe) 
50e5682013-11-02Arne Goedeke struct program * mysql_error_program = NULL;
ab6aec1997-02-11Fredrik Hübinette (Hubbe) 
7314db2015-06-10Henrik Grubbström (Grubba) #ifndef HAVE_MYSQL_REAL_CONNECT
31ba6a1998-07-02Henrik Grubbström (Grubba) #if defined(HAVE_MYSQL_PORT) || defined(HAVE_MYSQL_UNIX_PORT)
5722651998-01-25Henrik Grubbström (Grubba) #ifdef HAVE_MYSQL_PORT extern unsigned int mysql_port;
31ba6a1998-07-02Henrik Grubbström (Grubba) #endif /* HAVE_MYSQL_PORT */ #ifdef HAVE_MYSQL_UNIX_PORT extern char *mysql_unix_port; #endif /* HAVE_MYSQL_UNIX_PORT */
1caf511998-02-08Henrik Grubbström (Grubba) #ifdef _REENTRANT static MUTEX_T stupid_port_lock;
5722651998-01-25Henrik Grubbström (Grubba) #define STUPID_PORT_INIT() mt_init(&stupid_port_lock) #define STUPID_PORT_LOCK() mt_lock(&stupid_port_lock) #define STUPID_PORT_UNLOCK() mt_unlock(&stupid_port_lock) #define STUPID_PORT_DESTROY() mt_destroy(&stupid_port_lock)
1caf511998-02-08Henrik Grubbström (Grubba) #else /* !_REENTRANT */ #define STUPID_PORT_INIT() #define STUPID_PORT_LOCK() #define STUPID_PORT_UNLOCK() #define STUPID_PORT_DESTROY() #endif /* _REENTRANT */
5722651998-01-25Henrik Grubbström (Grubba) #endif /* HAVE_MYSQL_PORT */
7314db2015-06-10Henrik Grubbström (Grubba) #endif /* !HAVE_MYSQL_REAL_CONNECT */
5722651998-01-25Henrik Grubbström (Grubba) 
1caf511998-02-08Henrik Grubbström (Grubba) #ifdef _REENTRANT #define MYSQL_LOCK (&(PIKE_MYSQL->lock)) #define INIT_MYSQL_LOCK() mt_init(MYSQL_LOCK) #define DESTROY_MYSQL_LOCK() mt_destroy(MYSQL_LOCK) #define MYSQL_ALLOW() do { MUTEX_T *__l = MYSQL_LOCK; THREADS_ALLOW(); mt_lock(__l); #define MYSQL_DISALLOW() mt_unlock(__l); THREADS_DISALLOW(); } while(0) #else /* !_REENTRANT */ #define INIT_MYSQL_LOCK() #define DESTROY_MYSQL_LOCK() #define MYSQL_ALLOW() #define MYSQL_DISALLOW() #endif /* _REENTRANT */
51f9c62003-10-13Martin Stjernholm #define CHECK_8BIT_NONBINARY_STRING(FUNC, ARG) do { \
017b572011-10-28Henrik Grubbström (Grubba)  if (TYPEOF(sp[ARG-1-args]) != T_STRING || \
51f9c62003-10-13Martin Stjernholm  sp[ARG-1-args].u.string->size_shift || \
2dd2ee2005-11-12Martin Nilsson  string_has_null(sp[ARG-1-args].u.string)) \
de22f72014-08-25Martin Nilsson  SIMPLE_BAD_ARG_ERROR (FUNC, ARG, "string(1..255)"); \
51f9c62003-10-13Martin Stjernholm  } while (0) #define CHECK_8BIT_STRING(FUNC, ARG) do { \
017b572011-10-28Henrik Grubbström (Grubba)  if (TYPEOF(sp[ARG-1-args]) != T_STRING || \
51f9c62003-10-13Martin Stjernholm  sp[ARG-1-args].u.string->size_shift) \
de22f72014-08-25Martin Nilsson  SIMPLE_BAD_ARG_ERROR (FUNC, ARG, "string(8bit)"); \
51f9c62003-10-13Martin Stjernholm  } while (0)
6ac0282015-06-09Henrik Grubbström (Grubba) #undef PIKE_MYSQL
1caf511998-02-08Henrik Grubbström (Grubba) 
6ac0282015-06-09Henrik Grubbström (Grubba) DECLARATIONS;
ab6aec1997-02-11Fredrik Hübinette (Hubbe) 
6ac0282015-06-09Henrik Grubbström (Grubba) PIKECLASS mysql { #ifdef PIKE_THREADS CVAR PIKE_MUTEX_T lock; #endif /* PIKE_THREADS */ CVAR MYSQL *mysql; CVAR struct pike_string *host, *database, *user, *password; /* Reconnect */ CVAR struct mapping *options; CVAR struct pike_string *conn_charset; #define PIKE_MYSQL THIS_MYSQL_MYSQL /* * Functions */
ab6aec1997-02-11Fredrik Hübinette (Hubbe) 
6ac0282015-06-09Henrik Grubbström (Grubba)  /* * State maintenance */ INIT
ab6aec1997-02-11Fredrik Hübinette (Hubbe) {
1caf511998-02-08Henrik Grubbström (Grubba)  INIT_MYSQL_LOCK();
479f1a2008-08-20Henrik Grubbström (Grubba)  PIKE_MYSQL->mysql = mysql_init(NULL);
0ebd442008-01-26Martin Stjernholm  if (!PIKE_MYSQL->mysql) Pike_error ("Out of memory when initializing mysql connection.\n");
ab6aec1997-02-11Fredrik Hübinette (Hubbe) }
6ac0282015-06-09Henrik Grubbström (Grubba) EXIT
ab6aec1997-02-11Fredrik Hübinette (Hubbe) {
6e3b222000-08-04Henrik Grubbström (Grubba)  MYSQL *mysql = PIKE_MYSQL->mysql;
ab6aec1997-02-11Fredrik Hübinette (Hubbe) 
6e3b222000-08-04Henrik Grubbström (Grubba)  PIKE_MYSQL->mysql = NULL;
ab6aec1997-02-11Fredrik Hübinette (Hubbe) 
8426b71997-06-30Henrik Grubbström (Grubba)  if (PIKE_MYSQL->password) { free_string(PIKE_MYSQL->password); PIKE_MYSQL->password = NULL; } if (PIKE_MYSQL->user) { free_string(PIKE_MYSQL->user); PIKE_MYSQL->user = NULL; } if (PIKE_MYSQL->database) { free_string(PIKE_MYSQL->database); PIKE_MYSQL->database = NULL; } if (PIKE_MYSQL->host) { free_string(PIKE_MYSQL->host); PIKE_MYSQL->host = NULL; }
ebf6542006-08-12Martin Stjernholm  if (PIKE_MYSQL->options) { free_mapping (PIKE_MYSQL->options); PIKE_MYSQL->options = NULL; }
aa16eb2006-08-12Martin Stjernholm  if (PIKE_MYSQL->conn_charset) { free_string (PIKE_MYSQL->conn_charset); PIKE_MYSQL->conn_charset = NULL; }
8426b71997-06-30Henrik Grubbström (Grubba) 
1caf511998-02-08Henrik Grubbström (Grubba)  MYSQL_ALLOW();
ab6aec1997-02-11Fredrik Hübinette (Hubbe) 
6e3b222000-08-04Henrik Grubbström (Grubba)  if (mysql) {
479f1a2008-08-20Henrik Grubbström (Grubba)  mysql_close(mysql);
6e3b222000-08-04Henrik Grubbström (Grubba)  }
ab6aec1997-02-11Fredrik Hübinette (Hubbe) 
1caf511998-02-08Henrik Grubbström (Grubba)  MYSQL_DISALLOW(); DESTROY_MYSQL_LOCK();
ab6aec1997-02-11Fredrik Hübinette (Hubbe) }
6ac0282015-06-09Henrik Grubbström (Grubba)  EXTRA
0569022008-01-30Henrik Grubbström (Grubba) {
6ac0282015-06-09Henrik Grubbström (Grubba) #ifndef USE_OLD_FUNCTIONS add_integer_constant( "MYSQL_NO_ADD_DROP_DB", 1, 0 ); #endif add_integer_constant( "CLIENT_COMPRESS", CLIENT_COMPRESS, 0); add_integer_constant( "CLIENT_FOUND_ROWS", CLIENT_FOUND_ROWS, 0); add_integer_constant( "CLIENT_IGNORE_SPACE", CLIENT_IGNORE_SPACE, 0); #ifdef CLIENT_INTERACTIVE add_integer_constant( "CLIENT_INTERACTIVE", CLIENT_INTERACTIVE, 0); #endif add_integer_constant( "CLIENT_NO_SCHEMA", CLIENT_NO_SCHEMA, 0); add_integer_constant( "CLIENT_ODBC", CLIENT_ODBC, 0); #ifdef HAVE_MYSQL_SSL add_integer_constant( "CLIENT_SSL", CLIENT_SSL, 0); #endif #ifdef HAVE_MYSQL_FIELD_CHARSETNR add_integer_constant ("HAVE_MYSQL_FIELD_CHARSETNR", 1, 0); #endif } void pike_mysql_set_ssl(struct mapping *options) {
de9cfa2003-02-07Marcus Agehall #ifdef HAVE_MYSQL_SSL char *ssl_key = NULL; char *ssl_cert = NULL; char *ssl_ca = NULL; char *ssl_capath = NULL; char *ssl_cipher = NULL; struct svalue *val = NULL;
0569022008-01-30Henrik Grubbström (Grubba) #endif /* HAVE_MYSQL_SSL */
de9cfa2003-02-07Marcus Agehall 
37f08d2008-05-24Henrik Grubbström (Grubba)  /* For some reason, we may get here without an options mapping. */ if(!options) return;
0569022008-01-30Henrik Grubbström (Grubba)  #ifdef HAVE_MYSQL_SSL
1ce51b2003-02-07Martin Nilsson  if ((val = simple_mapping_string_lookup(options, "ssl_key")) &&
017b572011-10-28Henrik Grubbström (Grubba)  (TYPEOF(*val) == T_STRING) &&
de9cfa2003-02-07Marcus Agehall  (!val->u.string->size_shift)) ssl_key = val->u.string->str;
1ce51b2003-02-07Martin Nilsson  if ((val = simple_mapping_string_lookup(options, "ssl_cert")) &&
017b572011-10-28Henrik Grubbström (Grubba)  (TYPEOF(*val) == T_STRING) &&
de9cfa2003-02-07Marcus Agehall  (!val->u.string->size_shift)) ssl_cert = val->u.string->str; if ((val = simple_mapping_string_lookup(options, "ssl_ca")) &&
017b572011-10-28Henrik Grubbström (Grubba)  (TYPEOF(*val) == T_STRING) &&
de9cfa2003-02-07Marcus Agehall  (!val->u.string->size_shift)) ssl_ca = val->u.string->str;
1ce51b2003-02-07Martin Nilsson  if ((val = simple_mapping_string_lookup(options, "ssl_capath")) &&
017b572011-10-28Henrik Grubbström (Grubba)  (TYPEOF(*val) == T_STRING) &&
de9cfa2003-02-07Marcus Agehall  (!val->u.string->size_shift)) ssl_capath = val->u.string->str;
1ce51b2003-02-07Martin Nilsson  if ((val = simple_mapping_string_lookup(options, "ssl_cipher")) &&
017b572011-10-28Henrik Grubbström (Grubba)  (TYPEOF(*val) == T_STRING) &&
de9cfa2003-02-07Marcus Agehall  (!val->u.string->size_shift)) ssl_cipher = val->u.string->str; if (ssl_key || ssl_cert || ssl_ca || ssl_capath || ssl_cipher) mysql_ssl_set(PIKE_MYSQL->mysql, ssl_key, ssl_cert, ssl_ca, ssl_capath, ssl_cipher); #endif /* HAVE_MYSQL_SSL */
6ac0282015-06-09Henrik Grubbström (Grubba)  }
ab6aec1997-02-11Fredrik Hübinette (Hubbe) 
4996ea2002-03-18Henrik Grubbström (Grubba) static void pike_mysql_set_options(struct mapping *options) { struct svalue *val; #ifdef HAVE_MYSQL_OPTIONS
bf7b382011-08-18Henrik Grubbström (Grubba)  if ((val = simple_mapping_string_lookup(options, "reconnect"))) { my_bool reconnectp = 0; if (!SAFE_IS_ZERO(val)) reconnectp = 1;
55ba452011-08-25Henrik Grubbström (Grubba) #ifdef HAVE_MYSQL_OPT_RECONNECT
bf7b382011-08-18Henrik Grubbström (Grubba)  mysql_options(PIKE_MYSQL->mysql, MYSQL_OPT_RECONNECT, &reconnectp);
55ba452011-08-25Henrik Grubbström (Grubba) #else PIKE_MYSQL->mysql->reconnect = reconnectp;
bf7b382011-08-18Henrik Grubbström (Grubba) #endif
55ba452011-08-25Henrik Grubbström (Grubba)  }
438bdc2003-02-26Marcus Agehall #ifdef HAVE_MYSQL_READ_DEFAULT_FILE
4996ea2002-03-18Henrik Grubbström (Grubba)  if ((val = simple_mapping_string_lookup(options, "mysql_config_file")) &&
017b572011-10-28Henrik Grubbström (Grubba)  (TYPEOF(*val) == T_STRING) && (!val->u.string->size_shift)) {
4996ea2002-03-18Henrik Grubbström (Grubba)  mysql_options(PIKE_MYSQL->mysql, MYSQL_READ_DEFAULT_FILE, val->u.string->str); } #endif /* MYSQL_READ_DEFAULT_FILE */
438bdc2003-02-26Marcus Agehall #ifdef HAVE_MYSQL_READ_DEFAULT_GROUP if ((val = simple_mapping_string_lookup(options, "mysql_group")) &&
017b572011-10-28Henrik Grubbström (Grubba)  (TYPEOF(*val) == T_STRING) && (!val->u.string->size_shift)) {
438bdc2003-02-26Marcus Agehall  mysql_options(PIKE_MYSQL->mysql, MYSQL_READ_DEFAULT_GROUP, val->u.string->str); }
4996ea2002-03-18Henrik Grubbström (Grubba) #endif /* MYSQL_READ_DEFAULT_GROUP */
438bdc2003-02-26Marcus Agehall #ifdef HAVE_MYSQL_INIT_COMMAND
4996ea2002-03-18Henrik Grubbström (Grubba)  if ((val = simple_mapping_string_lookup(options, "init_command")) &&
017b572011-10-28Henrik Grubbström (Grubba)  (TYPEOF(*val) == T_STRING) && (!val->u.string->size_shift)) {
4996ea2002-03-18Henrik Grubbström (Grubba)  mysql_options(PIKE_MYSQL->mysql, MYSQL_INIT_COMMAND, val->u.string->str); } #endif /* MYSQL_INIT_COMMAND */
438bdc2003-02-26Marcus Agehall #ifdef HAVE_MYSQL_OPT_NAMED_PIPE
4996ea2002-03-18Henrik Grubbström (Grubba)  if ((val = simple_mapping_string_lookup(options, "mysql_named_pipe")) &&
017b572011-10-28Henrik Grubbström (Grubba)  (TYPEOF(*val) == T_INT) && (val->u.integer)) {
4996ea2002-03-18Henrik Grubbström (Grubba)  mysql_options(PIKE_MYSQL->mysql, MYSQL_OPT_NAMED_PIPE, NULL); } #endif /* MYSQL_OPT_NAMED_PIPE */
438bdc2003-02-26Marcus Agehall #ifdef HAVE_MYSQL_OPT_CONNECT_TIMEOUT
4996ea2002-03-18Henrik Grubbström (Grubba)  if ((val = simple_mapping_string_lookup(options, "timeout")) &&
017b572011-10-28Henrik Grubbström (Grubba)  (TYPEOF(*val) == T_INT)) {
4996ea2002-03-18Henrik Grubbström (Grubba)  unsigned int timeout = (unsigned int)val->u.integer; mysql_options(PIKE_MYSQL->mysql, MYSQL_OPT_CONNECT_TIMEOUT, (char *)&timeout); } #endif /* MYSQL_OPT_CONNECT_TIMEOUT */
438bdc2003-02-26Marcus Agehall #ifdef HAVE_MYSQL_OPT_COMPRESS
4996ea2002-03-18Henrik Grubbström (Grubba)  if ((val = simple_mapping_string_lookup(options, "compress")) &&
017b572011-10-28Henrik Grubbström (Grubba)  (TYPEOF(*val) == T_INT) && (val->u.integer)) {
4996ea2002-03-18Henrik Grubbström (Grubba)  mysql_options(PIKE_MYSQL->mysql, MYSQL_OPT_COMPRESS, NULL); } #endif /* MYSQL_OPT_COMPRESS */
438bdc2003-02-26Marcus Agehall #ifdef HAVE_MYSQL_OPT_LOCAL_INFILE
4996ea2002-03-18Henrik Grubbström (Grubba)  if ((val = simple_mapping_string_lookup(options, "mysql_local_infile")) &&
017b572011-10-28Henrik Grubbström (Grubba)  (TYPEOF(*val) == T_INT)) {
4996ea2002-03-18Henrik Grubbström (Grubba)  unsigned int allowed = (unsigned int)val->u.integer;
0ebf002005-08-09Henrik Grubbström (Grubba)  mysql_options(PIKE_MYSQL->mysql, MYSQL_OPT_LOCAL_INFILE,
4996ea2002-03-18Henrik Grubbström (Grubba)  (char *)&allowed); } else { /* Default to not allowed */ unsigned int allowed = 0;
0ebf002005-08-09Henrik Grubbström (Grubba)  mysql_options(PIKE_MYSQL->mysql, MYSQL_OPT_LOCAL_INFILE,
13670c2015-05-25Martin Nilsson  (char *)&allowed);
4996ea2002-03-18Henrik Grubbström (Grubba)  } #endif /* MYSQL_OPT_LOCAL_INFILE */
438bdc2003-02-26Marcus Agehall #ifdef HAVE_MYSQL_SET_CHARSET_DIR
4996ea2002-03-18Henrik Grubbström (Grubba)  if ((val = simple_mapping_string_lookup(options, "mysql_charset_dir")) &&
017b572011-10-28Henrik Grubbström (Grubba)  (TYPEOF(*val) == T_STRING) && (!val->u.string->size_shift)) {
4996ea2002-03-18Henrik Grubbström (Grubba)  mysql_options(PIKE_MYSQL->mysql, MYSQL_SET_CHARSET_DIR, val->u.string->str); } #endif /* MYSQL_SET_CHARSET_DIR */
2f9a7e2009-11-15Martin Stjernholm #endif /* HAVE_MYSQL_OPTIONS */
4996ea2002-03-18Henrik Grubbström (Grubba)  if ((val = simple_mapping_string_lookup(options, "mysql_charset_name")) &&
017b572011-10-28Henrik Grubbström (Grubba)  (TYPEOF(*val) == T_STRING) && (!val->u.string->size_shift) &&
2f9a7e2009-11-15Martin Stjernholm  !string_has_null (val->u.string)) {
8106342011-08-23Henrik Grubbström (Grubba) #if defined (HAVE_MYSQL_OPTIONS) && defined (HAVE_MYSQL_SET_CHARSET_NAME) mysql_options (PIKE_MYSQL->mysql, MYSQL_SET_CHARSET_NAME, val->u.string->str); #endif
2f9a7e2009-11-15Martin Stjernholm  if (PIKE_MYSQL->conn_charset) free (PIKE_MYSQL->conn_charset); copy_shared_string (PIKE_MYSQL->conn_charset, val->u.string);
4996ea2002-03-18Henrik Grubbström (Grubba)  }
2f9a7e2009-11-15Martin Stjernholm } static void low_query(INT32 args, char *name, int flags);
474ba12012-01-07Martin Stjernholm static void connection_set_charset (struct pike_string *charset)
2f9a7e2009-11-15Martin Stjernholm { int res; MYSQL *mysql = PIKE_MYSQL->mysql;
8106342011-08-23Henrik Grubbström (Grubba)  #if defined (HAVE_MYSQL_OPTIONS) && defined (HAVE_MYSQL_SET_CHARSET_NAME) /* Update the default charset for the connection, * so that it will be restored on reconnect. */ mysql_options (mysql, MYSQL_SET_CHARSET_NAME, charset->str); #endif #ifdef HAVE_MYSQL_SET_CHARACTER_SET
2f9a7e2009-11-15Martin Stjernholm  MYSQL_ALLOW(); res = mysql_set_character_set (mysql, charset->str); MYSQL_DISALLOW(); if (res) { const char *err; MYSQL_ALLOW(); err = mysql_error(mysql); MYSQL_DISALLOW(); Pike_error("Setting the charset failed: %s\n", err); } #else /* Old libs (< 4.1.13) doesn't support changing the connection * charset. We emulate it by setting the charset ourselves. Note * that this doesn't work with mysql_real_escape_string, but that * function isn't used. */
7cf0162014-05-22Per Hedbor  push_text ("SET NAMES '");
8106342011-08-23Henrik Grubbström (Grubba)  ref_push_string (charset); /* FIXME: Quote? */
7cf0162014-05-22Per Hedbor  push_text ("'");
2f9a7e2009-11-15Martin Stjernholm  f_add (3); low_query (1, "set_charset", PIKE_MYSQL_FLAG_STORE_RESULT); pop_stack(); #endif
4996ea2002-03-18Henrik Grubbström (Grubba) }
8426b71997-06-30Henrik Grubbström (Grubba) 
13ce622006-08-12Martin Stjernholm static void pike_mysql_reconnect (int reconnect)
8426b71997-06-30Henrik Grubbström (Grubba) {
6e3b222000-08-04Henrik Grubbström (Grubba)  MYSQL *mysql = PIKE_MYSQL->mysql;
8426b71997-06-30Henrik Grubbström (Grubba)  MYSQL *socket; char *host = NULL; char *database = NULL; char *user = NULL; char *password = NULL;
31ba6a1998-07-02Henrik Grubbström (Grubba)  char *hostptr = NULL;
5722651998-01-25Henrik Grubbström (Grubba)  char *portptr = NULL;
31ba6a1998-07-02Henrik Grubbström (Grubba)  char *saved_unix_port = NULL;
5722651998-01-25Henrik Grubbström (Grubba)  unsigned int port = 0; unsigned int saved_port = 0;
de9cfa2003-02-07Marcus Agehall  unsigned int options = 0; struct svalue *val;
8426b71997-06-30Henrik Grubbström (Grubba)  if (PIKE_MYSQL->host) {
31ba6a1998-07-02Henrik Grubbström (Grubba)  hostptr = strdup(PIKE_MYSQL->host->str); if (!hostptr) {
b2d3e42000-12-01Fredrik Hübinette (Hubbe)  Pike_error("Mysql.mysql(): Out of memory!\n");
5722651998-01-25Henrik Grubbström (Grubba)  }
31ba6a1998-07-02Henrik Grubbström (Grubba)  if ((portptr = strchr(hostptr, ':')) && (*portptr == ':')) {
5722651998-01-25Henrik Grubbström (Grubba)  *portptr = 0; portptr++; port = (unsigned int) atoi(portptr); }
31ba6a1998-07-02Henrik Grubbström (Grubba)  if (*hostptr) { host = hostptr; }
8426b71997-06-30Henrik Grubbström (Grubba)  } if (PIKE_MYSQL->database) { database = PIKE_MYSQL->database->str; } if (PIKE_MYSQL->user) { user = PIKE_MYSQL->user->str; } if (PIKE_MYSQL->password) { password = PIKE_MYSQL->password->str; }
001e992008-03-21Henrik Grubbström (Grubba)  if (PIKE_MYSQL->options && (val = simple_mapping_string_lookup(PIKE_MYSQL->options,
9b52352003-02-07Henrik Grubbström (Grubba)  "connect_options")) &&
017b572011-10-28Henrik Grubbström (Grubba)  (TYPEOF(*val) == T_INT) && (val->u.integer)) {
9b52352003-02-07Henrik Grubbström (Grubba)  options = (unsigned int)val->u.integer; }
bf7b382011-08-18Henrik Grubbström (Grubba) #if defined(HAVE_MYSQL_OPTIONS) && defined(HAVE_MYSQL_OPT_RECONNECT) { /* Disable the automatic reconnect. */ my_bool reconnectp = 0;
2ed6422012-03-07Tobias S. Josefowitz  if (PIKE_MYSQL->options && (val = simple_mapping_string_lookup(PIKE_MYSQL->options, "reconnect"))) if (!SAFE_IS_ZERO(val)) reconnectp = 1;
bf7b382011-08-18Henrik Grubbström (Grubba)  mysql_options(mysql, MYSQL_OPT_RECONNECT, &reconnectp); } #endif
2f9a7e2009-11-15Martin Stjernholm #if defined (HAVE_MYSQL_OPTIONS) && defined (HAVE_MYSQL_SET_CHARSET_NAME)
aa16eb2006-08-12Martin Stjernholm  if (PIKE_MYSQL->conn_charset) mysql_options (mysql, MYSQL_SET_CHARSET_NAME, PIKE_MYSQL->conn_charset->str);
2f9a7e2009-11-15Martin Stjernholm #define RECONNECT_CHARSET_IS_SET
aa16eb2006-08-12Martin Stjernholm #endif
1caf511998-02-08Henrik Grubbström (Grubba)  MYSQL_ALLOW();
8426b71997-06-30Henrik Grubbström (Grubba) 
68b2d62011-08-17Henrik Grubbström (Grubba) #ifdef HAVE_MYSQL_REAL_CONNECT socket = mysql_real_connect(mysql, host, user, password, NULL, port, portptr, options); #else
7314db2015-06-10Henrik Grubbström (Grubba) #if defined(HAVE_MYSQL_PORT) || defined(HAVE_MYSQL_UNIX_PORT) STUPID_PORT_LOCK(); #endif /* HAVE_MYSQL_PORT || HAVE_MYSQL_UNIX_PORT */
5722651998-01-25Henrik Grubbström (Grubba)  #ifdef HAVE_MYSQL_PORT if (port) { saved_port = mysql_port; mysql_port = port; } #endif /* HAVE_MYSQL_PORT */
31ba6a1998-07-02Henrik Grubbström (Grubba) #ifdef HAVE_MYSQL_UNIX_PORT if (portptr) { saved_unix_port = mysql_unix_port; mysql_unix_port = portptr; } #endif /* HAVE_MYSQL_UNIX_PORT */
5722651998-01-25Henrik Grubbström (Grubba) 
8426b71997-06-30Henrik Grubbström (Grubba)  socket = mysql_connect(mysql, host, user, password);
5722651998-01-25Henrik Grubbström (Grubba) #ifdef HAVE_MYSQL_PORT
86d0a11998-03-08Henrik Grubbström (Grubba)  if (port) { mysql_port = saved_port; }
5722651998-01-25Henrik Grubbström (Grubba) #endif /* HAVE_MYSQL_PORT */
31ba6a1998-07-02Henrik Grubbström (Grubba) #ifdef HAVE_MYSQL_UNIX_PORT if (portptr) { mysql_unix_port = saved_unix_port; } #endif /* HAVE_MYSQL_UNIX_PORT */
68b2d62011-08-17Henrik Grubbström (Grubba) 
31ba6a1998-07-02Henrik Grubbström (Grubba) #if defined(HAVE_MYSQL_PORT) || defined(HAVE_MYSQL_UNIX_PORT) STUPID_PORT_UNLOCK(); #endif /* HAVE_MYSQL_PORT || MAVE_MYSQL_UNIX_PORT*/
7314db2015-06-10Henrik Grubbström (Grubba) #endif /* HAVE_MYSQL_REAL_CONNECT */
5722651998-01-25Henrik Grubbström (Grubba) 
b51e512011-08-30Henrik Grubbström (Grubba)  MYSQL_DISALLOW();
55ba452011-08-25Henrik Grubbström (Grubba) #ifndef HAVE_MYSQL_OPT_RECONNECT /* Note: In Mysql 3.22 the reconnect flag is always set by * mysql_real_connect(), so we need to reset it here. */
bf7b382011-08-18Henrik Grubbström (Grubba)  mysql->reconnect = 0;
55ba452011-08-25Henrik Grubbström (Grubba)  if (PIKE_MYSQL->options && (val = simple_mapping_string_lookup(PIKE_MYSQL->options, "reconnect"))) { my_bool reconnectp = 0; if (!SAFE_IS_ZERO(val)) reconnectp = 1; PIKE_MYSQL->mysql->reconnect = reconnectp; }
bf7b382011-08-18Henrik Grubbström (Grubba) #endif
31ba6a1998-07-02Henrik Grubbström (Grubba)  if (hostptr) {
5722651998-01-25Henrik Grubbström (Grubba)  /* No longer needed */
31ba6a1998-07-02Henrik Grubbström (Grubba)  free(hostptr);
5722651998-01-25Henrik Grubbström (Grubba)  }
13670c2015-05-25Martin Nilsson 
479f1a2008-08-20Henrik Grubbström (Grubba)  if (!socket) {
13ce622006-08-12Martin Stjernholm  const char *err; MYSQL_ALLOW(); err = mysql_error (mysql); MYSQL_DISALLOW(); Pike_error("Mysql.mysql(): Couldn't %s to SQL-server: %s\n", reconnect ? "reconnect" : "connect", err);
8426b71997-06-30Henrik Grubbström (Grubba)  }
79aad21999-10-19Henrik Grubbström (Grubba) 
479f1a2008-08-20Henrik Grubbström (Grubba)  if (mysql->net.fd >= 0) {
79aad21999-10-19Henrik Grubbström (Grubba)  /* Make sure the fd gets closed on exec. */
479f1a2008-08-20Henrik Grubbström (Grubba)  set_close_on_exec(mysql->net.fd, 1);
79aad21999-10-19Henrik Grubbström (Grubba)  }
8426b71997-06-30Henrik Grubbström (Grubba)  if (database) { int tmp;
1caf511998-02-08Henrik Grubbström (Grubba)  MYSQL_ALLOW();
8426b71997-06-30Henrik Grubbström (Grubba) 
479f1a2008-08-20Henrik Grubbström (Grubba)  tmp = mysql_select_db(mysql, database);
8426b71997-06-30Henrik Grubbström (Grubba) 
1caf511998-02-08Henrik Grubbström (Grubba)  MYSQL_DISALLOW();
8426b71997-06-30Henrik Grubbström (Grubba) 
5244cd2003-12-22Dan Nelson  if (tmp) {
e941661998-05-15Henrik Grubbström (Grubba)  if (strlen(database) < 1024) {
b2d3e42000-12-01Fredrik Hübinette (Hubbe)  Pike_error("Mysql.mysql(): Couldn't select database \"%s\"\n", database);
e941661998-05-15Henrik Grubbström (Grubba)  } else {
b2d3e42000-12-01Fredrik Hübinette (Hubbe)  Pike_error("Mysql.mysql(): Couldn't select database\n");
e941661998-05-15Henrik Grubbström (Grubba)  }
8426b71997-06-30Henrik Grubbström (Grubba)  } }
2f9a7e2009-11-15Martin Stjernholm  #ifndef RECONNECT_CHARSET_IS_SET if (PIKE_MYSQL->conn_charset)
474ba12012-01-07Martin Stjernholm  connection_set_charset (PIKE_MYSQL->conn_charset);
2f9a7e2009-11-15Martin Stjernholm #endif
8426b71997-06-30Henrik Grubbström (Grubba) }
ab6aec1997-02-11Fredrik Hübinette (Hubbe) /* * Methods */
0be0372001-02-10Henrik Grubbström (Grubba) /*! @decl void create() *! @decl void create(string host) *! @decl void create(string host, string database) *! @decl void create(string host, string database, string user) *! @decl void create(string host, string database, string user, @ *! string password)
4996ea2002-03-18Henrik Grubbström (Grubba)  *! @decl void create(string host, string database, string user, @ *! string password, mapping(string:string|int) options)
0be0372001-02-10Henrik Grubbström (Grubba)  *! *! Connect to a Mysql database. *! *! To access the Mysql database, you must first connect to it. This is *! done with this function. *!
4996ea2002-03-18Henrik Grubbström (Grubba)  *! @param host
cbe8c92003-04-07Martin Nilsson  *! If you give no argument, or give @expr{""@} as @[host] it will connect *! with a UNIX-domain socket, which can be a big performance gain.
4996ea2002-03-18Henrik Grubbström (Grubba)  *! *! @param options *! This optional mapping can contain zero or more of the following *! parameters: *! *! @mapping *! @member string "init_command" *! Command to execute on connect. *! *! @member int "timeout" *! Timeout in seconds. *! *! @member int(0..1) "compress" *! Enable compressed protocol. *! *! @member string "mysql_config_file"
cbe8c92003-04-07Martin Nilsson  *! Change config file from @expr{"my.cnf"@}.
4996ea2002-03-18Henrik Grubbström (Grubba)  *! *! @member string "mysql_group" *! Specify additional group to read from config file. *! *! @member int(0..1) "mysql_named_pipe" *! Use named pipe to connect to server. *! *! @member int(0..1) "mysql_local_infile" *! Enable use of LOCAL INFILE (security). *! *! @member string "mysql_charset_dir" *! Change charset directory. *! *! @member string "mysql_charset_name"
aa16eb2006-08-12Martin Stjernholm  *! Set connection charset - see @[set_charset] for details. The *! default is @expr{"latin1"@}. As opposed to @[set_charset], *! this way of specifying the connection charset doesn't *! require MySQL 4.1.0. *! *! @member int "unicode_decode_mode" *! Enable unicode decode mode for the connection if nonzero. In *! this mode non-binary string results are automatically *! converted to (possibly wide) unicode strings. An error is *! thrown if the server doesn't support this. See *! @[set_unicode_decode_mode].
de9cfa2003-02-07Marcus Agehall  *!
1ce51b2003-02-07Martin Nilsson  *! @member string "ssl_key"
de9cfa2003-02-07Marcus Agehall  *! Path to SSL-key for use in SSL-communication. *!
1ce51b2003-02-07Martin Nilsson  *! @member string "ssl_cert"
de9cfa2003-02-07Marcus Agehall  *! Path to SSL-cert for use in SSL-communication. *!
1ce51b2003-02-07Martin Nilsson  *! @member string "ssl_ca"
de9cfa2003-02-07Marcus Agehall  *! Path to SSL-CA for use in SSL-communication. *!
1ce51b2003-02-07Martin Nilsson  *! @member string "ssl_capath"
de9cfa2003-02-07Marcus Agehall  *! Path to SSL-CAPATH for use in SSL-communication. *!
1ce51b2003-02-07Martin Nilsson  *! @member string "ssl_cipher" *! FIXME *!
de9cfa2003-02-07Marcus Agehall  *! @member int "connect_options" *! Options used when connecting to the server. See mysql documentation *! for more information. *!
4996ea2002-03-18Henrik Grubbström (Grubba)  *! @endmapping *! *! @note *! Some options may not be implemented. Unimplemented options are *! silently ignored.
de9cfa2003-02-07Marcus Agehall  *!
c648222006-08-12Martin Stjernholm  *! @note *! To use SSL-connections, set the SSL-parameters correctly. They correspond
cbe8c92003-04-07Martin Nilsson  *! to the parameters given to the mysql-client with the same name so make *! sure that the mysql-client works with SSL and set these parameters to *! the same values and everything should work. If SSL-options are loaded *! from a config-file, one may set the connect_options to include *! CLIENT_SSL.
c648222006-08-12Martin Stjernholm  *! *! @note *! If Pike has been built with an old MySQL client lib then it *! might not be possible to specify some charsets that the server *! supports with the @expr{"mysql_charset_name"@} option. In such *! cases it's possible that @[set_charset] works better (provided *! the server is 4.1 or newer).
0be0372001-02-10Henrik Grubbström (Grubba)  */
6ac0282015-06-09Henrik Grubbström (Grubba) PIKEFUN void create(string(8bit)|void host, string(8bit)|void database, string(8bit)|void user, string(8bit)|void password, mapping|void options)
ab6aec1997-02-11Fredrik Hübinette (Hubbe) {
0ebf002005-08-09Henrik Grubbström (Grubba) #if defined(HAVE_MYSQL_OPTIONS) && defined(HAVE_MYSQL_OPT_LOCAL_INFILE)
4996ea2002-03-18Henrik Grubbström (Grubba)  /* Default to not allowed */ unsigned int allowed = 0;
0ebf002005-08-09Henrik Grubbström (Grubba)  mysql_options(PIKE_MYSQL->mysql, MYSQL_OPT_LOCAL_INFILE,
13670c2015-05-25Martin Nilsson  (char *)&allowed);
0ebf002005-08-09Henrik Grubbström (Grubba) #endif /* HAVE_MYSQL_OPTIONS && HAVE_MYSQL_OPT_LOCAL_INFILE */
4996ea2002-03-18Henrik Grubbström (Grubba) 
ab6aec1997-02-11Fredrik Hübinette (Hubbe)  if (args >= 1) {
de22f72014-08-25Martin Nilsson  CHECK_8BIT_NONBINARY_STRING ("create", 1);
6ac0282015-06-09Henrik Grubbström (Grubba)  if (host && host->len) { add_ref(PIKE_MYSQL->host = host);
ab6aec1997-02-11Fredrik Hübinette (Hubbe)  } if (args >= 2) {
de22f72014-08-25Martin Nilsson  CHECK_8BIT_NONBINARY_STRING ("create", 2);
ab6aec1997-02-11Fredrik Hübinette (Hubbe)  if (sp[1-args].u.string->len) {
6ac0282015-06-09Henrik Grubbström (Grubba)  add_ref(PIKE_MYSQL->database = database);
ab6aec1997-02-11Fredrik Hübinette (Hubbe)  }
13670c2015-05-25Martin Nilsson 
ab6aec1997-02-11Fredrik Hübinette (Hubbe)  if (args >= 3) {
de22f72014-08-25Martin Nilsson  CHECK_8BIT_NONBINARY_STRING ("create", 3);
ab6aec1997-02-11Fredrik Hübinette (Hubbe)  if (sp[2-args].u.string->len) {
6ac0282015-06-09Henrik Grubbström (Grubba)  add_ref(PIKE_MYSQL->user = user);
ab6aec1997-02-11Fredrik Hübinette (Hubbe)  }
13670c2015-05-25Martin Nilsson 
ab6aec1997-02-11Fredrik Hübinette (Hubbe)  if (args >= 4) {
de22f72014-08-25Martin Nilsson  CHECK_8BIT_NONBINARY_STRING ("create", 4);
ab6aec1997-02-11Fredrik Hübinette (Hubbe)  if (sp[3-args].u.string->len) {
6ac0282015-06-09Henrik Grubbström (Grubba)  add_ref(PIKE_MYSQL->password = password);
ab6aec1997-02-11Fredrik Hübinette (Hubbe)  }
4996ea2002-03-18Henrik Grubbström (Grubba)  if (args >= 5) {
0a5ec32012-04-12Henrik Grubbström (Grubba)  if (TYPEOF(sp[4-args]) != T_MAPPING){ if (!UNSAFE_IS_ZERO(sp + 4 - args)) {
de22f72014-08-25Martin Nilsson  SIMPLE_BAD_ARG_ERROR ("create", 5, "mapping(string:mixed)");
0a5ec32012-04-12Henrik Grubbström (Grubba)  } } else {
6ac0282015-06-09Henrik Grubbström (Grubba)  add_ref(PIKE_MYSQL->options = options); pike_mysql_set_options(options);
4996ea2002-03-18Henrik Grubbström (Grubba)  } }
ab6aec1997-02-11Fredrik Hübinette (Hubbe)  } } } }
de9cfa2003-02-07Marcus Agehall  pike_mysql_set_ssl(PIKE_MYSQL->options);
8106342011-08-23Henrik Grubbström (Grubba)  if (!PIKE_MYSQL->conn_charset) { /* Backward compat with Mysql 4.0 and earlier. */
6ac0282015-06-09Henrik Grubbström (Grubba)  PIKE_MYSQL->conn_charset = MK_STRING("latin1");
8106342011-08-23Henrik Grubbström (Grubba)  }
13ce622006-08-12Martin Stjernholm  pike_mysql_reconnect (0);
aa16eb2006-08-12Martin Stjernholm 
15caf42006-08-31Martin Stjernholm #ifdef HAVE_MYSQL_CHARACTER_SET_NAME
2f9a7e2009-11-15Martin Stjernholm  if (!PIKE_MYSQL->conn_charset) {
479f1a2008-08-20Henrik Grubbström (Grubba)  const char *charset = mysql_character_set_name (PIKE_MYSQL->mysql);
2f9a7e2009-11-15Martin Stjernholm  if (charset) /* Just paranoia; mysql_character_set_name should * always return a string. */
aa16eb2006-08-12Martin Stjernholm  PIKE_MYSQL->conn_charset = make_shared_string (charset); } #endif
b8edb52006-08-12Martin Stjernholm  pop_n_elems(args);
ab6aec1997-02-11Fredrik Hübinette (Hubbe) }
65a0fc2002-04-11Johan Sundström /*! @decl string _sprintf(int type, void|mapping flags) */
6ac0282015-06-09Henrik Grubbström (Grubba) PIKEFUN string _sprintf(int type, void|mapping flags) flags ID_PROTECTED;
65a0fc2002-04-11Johan Sundström { pop_n_elems(args); switch( type ) { case 'O': {
479f1a2008-08-20Henrik Grubbström (Grubba)  MYSQL *mysql = PIKE_MYSQL->mysql;
954c6d2006-08-12Martin Stjernholm 
479f1a2008-08-20Henrik Grubbström (Grubba)  if (mysql) {
954c6d2006-08-12Martin Stjernholm  const char *info;
d0ba932009-10-09Martin Stjernholm  /* _sprintf functions must not hang. mysql_get_host_info is * safe to execute anyway, because it only returns a field in * the MYSQL struct. */ /* MYSQL_ALLOW(); */
479f1a2008-08-20Henrik Grubbström (Grubba)  info = mysql_get_host_info(mysql);
d0ba932009-10-09Martin Stjernholm  /* MYSQL_DISALLOW(); */
954c6d2006-08-12Martin Stjernholm  push_text("mysql(/*%s%s*/)");
2092782008-08-28Jonas Wallden  push_text(info ? info : "");
de9cfa2003-02-07Marcus Agehall #ifdef HAVE_MYSQL_SSL
954c6d2006-08-12Martin Stjernholm  if (PIKE_MYSQL->mysql->options.use_ssl) { push_text(", SSL"); } else push_empty_string();
de9cfa2003-02-07Marcus Agehall #else
954c6d2006-08-12Martin Stjernholm  push_empty_string();
de9cfa2003-02-07Marcus Agehall #endif /* HAVE_MYSQL_SSL */
954c6d2006-08-12Martin Stjernholm  f_sprintf(3); } else
7cf0162014-05-22Per Hedbor  push_text ("mysql()");
de9cfa2003-02-07Marcus Agehall 
65a0fc2002-04-11Johan Sundström  return; } case 't': {
6ac0282015-06-09Henrik Grubbström (Grubba)  ref_push_string(MK_STRING("mysql"));
65a0fc2002-04-11Johan Sundström  return; } }
074dd12011-10-22Henrik Grubbström (Grubba)  push_undefined();
65a0fc2002-04-11Johan Sundström }
1b8ceb2011-08-25Henrik Grubbström (Grubba) /*! @decl int is_open() *! *! Returns true if the connection seems to be open. *! *! @note *! This function only checks that there's an *! open connection, and that the other end hasn't *! closed it yet. No data is sent over the *! connection. *! *! For a more reliable check of whether the connection *! is alive, please use @[ping()]. *! *! @seealso *! @[ping()] */
6ac0282015-06-09Henrik Grubbström (Grubba) PIKEFUN int is_open()
1b8ceb2011-08-25Henrik Grubbström (Grubba) { int fd = PIKE_MYSQL->mysql->net.fd; pop_n_elems(args); if ( #ifdef HAVE_MYSQL_FIELD_NET_VIO !(PIKE_MYSQL->mysql->net.vio) || #endif (fd < 0)) { push_int(0); } else { #ifdef HAVE_POLL struct pollfd fds[1]; fds->fd = fd; fds->events = POLLOUT|POLLHUP; fds->revents = 0; if ((poll(fds, 1, 0) == 1) && (fds->revents & POLLOUT)) { push_int(1); } else { /* POLLHUP or POLLERR or timeout */ push_int(0); } #else /* FIXME: Use select(). */ push_int(1); #endif } }
a847602011-08-17Henrik Grubbström (Grubba) /*! @decl int ping() *! *! Check whether the connection is alive. *! *! @returns *! Returns one of the following: *! @int *! @value 0 *! Everything ok. *! @value 1 *! The connection reconnected automatically. *! @value -1 *! The server has gone away, and the connection is dead. *! @endint
1b8ceb2011-08-25Henrik Grubbström (Grubba)  *! *! @seealso *! @[is_open()]
a847602011-08-17Henrik Grubbström (Grubba)  */
6ac0282015-06-09Henrik Grubbström (Grubba) PIKEFUN int ping()
a847602011-08-17Henrik Grubbström (Grubba) { MYSQL *mysql = PIKE_MYSQL->mysql; unsigned long orig_id = mysql_thread_id(mysql); int err; MYSQL_ALLOW(); err = mysql_ping(mysql); MYSQL_DISALLOW(); pop_n_elems(args); if (err) { push_int(-1); } else if (orig_id != mysql_thread_id(mysql)) { push_int(1); } else { push_int(0); } }
0be0372001-02-10Henrik Grubbström (Grubba) /*! @decl int affected_rows() *! *! Returns the number of rows affected by the last query. */
6ac0282015-06-09Henrik Grubbström (Grubba) PIKEFUN int affected_rows()
ab6aec1997-02-11Fredrik Hübinette (Hubbe) {
479f1a2008-08-20Henrik Grubbström (Grubba)  MYSQL *mysql;
4d4cc22001-09-24Henrik Grubbström (Grubba)  INT64 count;
1caf511998-02-08Henrik Grubbström (Grubba) 
ab6aec1997-02-11Fredrik Hübinette (Hubbe)  pop_n_elems(args);
479f1a2008-08-20Henrik Grubbström (Grubba)  mysql = PIKE_MYSQL->mysql;
1caf511998-02-08Henrik Grubbström (Grubba)  MYSQL_ALLOW();
479f1a2008-08-20Henrik Grubbström (Grubba)  count = mysql_affected_rows(mysql);
1caf511998-02-08Henrik Grubbström (Grubba)  MYSQL_DISALLOW();
4d4cc22001-09-24Henrik Grubbström (Grubba)  push_int64(count);
ab6aec1997-02-11Fredrik Hübinette (Hubbe) }
0be0372001-02-10Henrik Grubbström (Grubba) /*! @decl int insert_id() *! *! Returns the id of the last INSERT query into a table with *! an AUTO INCREMENT field. */
6ac0282015-06-09Henrik Grubbström (Grubba) PIKEFUN int insert_id()
ab6aec1997-02-11Fredrik Hübinette (Hubbe) {
479f1a2008-08-20Henrik Grubbström (Grubba)  MYSQL *mysql;
4d4cc22001-09-24Henrik Grubbström (Grubba)  INT64 id;
1caf511998-02-08Henrik Grubbström (Grubba) 
ab6aec1997-02-11Fredrik Hübinette (Hubbe)  pop_n_elems(args);
1caf511998-02-08Henrik Grubbström (Grubba) 
479f1a2008-08-20Henrik Grubbström (Grubba)  mysql = PIKE_MYSQL->mysql;
1caf511998-02-08Henrik Grubbström (Grubba)  MYSQL_ALLOW();
479f1a2008-08-20Henrik Grubbström (Grubba)  id = mysql_insert_id(mysql);
1caf511998-02-08Henrik Grubbström (Grubba)  MYSQL_DISALLOW();
4d4cc22001-09-24Henrik Grubbström (Grubba)  push_int64(id);
ab6aec1997-02-11Fredrik Hübinette (Hubbe) }
cd1da52001-04-28Henrik Grubbström (Grubba) /*! @decl string error()
0be0372001-02-10Henrik Grubbström (Grubba)  *! *! Returns a string describing the last error from the Mysql-server. *!
cbe8c92003-04-07Martin Nilsson  *! Returns @expr{0@} (zero) if there was no error.
0be0372001-02-10Henrik Grubbström (Grubba)  */
6ac0282015-06-09Henrik Grubbström (Grubba) PIKEFUN string error()
ab6aec1997-02-11Fredrik Hübinette (Hubbe) {
479f1a2008-08-20Henrik Grubbström (Grubba)  MYSQL *mysql;
066ff12004-03-19Martin Nilsson  const char *error_msg;
8b84621997-11-05Henrik Grubbström (Grubba) 
479f1a2008-08-20Henrik Grubbström (Grubba)  mysql = PIKE_MYSQL->mysql;
1caf511998-02-08Henrik Grubbström (Grubba)  MYSQL_ALLOW();
479f1a2008-08-20Henrik Grubbström (Grubba)  error_msg = mysql_error(mysql);
1caf511998-02-08Henrik Grubbström (Grubba)  MYSQL_DISALLOW();
ab6aec1997-02-11Fredrik Hübinette (Hubbe)  pop_n_elems(args);
8b84621997-11-05Henrik Grubbström (Grubba) 
ab6aec1997-02-11Fredrik Hübinette (Hubbe)  if (error_msg && *error_msg) { push_text(error_msg); } else { push_int(0); } }
1b02b52013-10-13Arne Goedeke /*! @decl int errno() *! *! Returns an error code describing the last error from the Mysql-server. *! *! Returns @expr{0@} (zero) if there was no error. */
6ac0282015-06-09Henrik Grubbström (Grubba) PIKEFUN int errno()
1b02b52013-10-13Arne Goedeke { MYSQL *mysql; unsigned int errnum; mysql = PIKE_MYSQL->mysql; MYSQL_ALLOW(); errnum = mysql_errno(mysql); MYSQL_DISALLOW(); pop_n_elems(args); push_int(errnum); }
506e342013-10-13Arne Goedeke /*! @decl string sqlstate() *! *! Returns the SQLSTATE error code describing the last error. *! *! The value @expr{"000000"@} means 'no error'. The SQLSTATE error codes are *! described in ANSI SQL. */ #ifdef HAVE_MYSQL_SQLSTATE
6ac0282015-06-09Henrik Grubbström (Grubba) PIKEFUN string sqlstate()
506e342013-10-13Arne Goedeke { MYSQL *mysql; const char *error_msg; mysql = PIKE_MYSQL->mysql; MYSQL_ALLOW(); error_msg = mysql_sqlstate(mysql); MYSQL_DISALLOW(); pop_n_elems(args); push_text(error_msg); } #endif
0be0372001-02-10Henrik Grubbström (Grubba) /*! @decl void select_db(string database) *! *! Select database. *! *! The Mysql-server can hold several databases. You select which one *! you want to access with this function. *! *! @seealso *! @[create()], @[create_db()], @[drop_db()] */
6ac0282015-06-09Henrik Grubbström (Grubba) PIKEFUN void select_db(string(8bit) database)
ab6aec1997-02-11Fredrik Hübinette (Hubbe) {
479f1a2008-08-20Henrik Grubbström (Grubba)  MYSQL *mysql = PIKE_MYSQL->mysql;
b72ff21997-11-03Henrik Grubbström (Grubba)  int tmp = -1;
ab6aec1997-02-11Fredrik Hübinette (Hubbe) 
de22f72014-08-25Martin Nilsson  CHECK_8BIT_NONBINARY_STRING ("select_db", 1);
ab6aec1997-02-11Fredrik Hübinette (Hubbe) 
479f1a2008-08-20Henrik Grubbström (Grubba)  if (mysql) {
1caf511998-02-08Henrik Grubbström (Grubba)  MYSQL_ALLOW();
ab6aec1997-02-11Fredrik Hübinette (Hubbe) 
6ac0282015-06-09Henrik Grubbström (Grubba)  tmp = mysql_select_db(mysql, database->str);
ab6aec1997-02-11Fredrik Hübinette (Hubbe) 
1caf511998-02-08Henrik Grubbström (Grubba)  MYSQL_DISALLOW();
b444f51997-11-02Henrik Grubbström (Grubba)  }
8426b71997-06-30Henrik Grubbström (Grubba) 
5244cd2003-12-22Dan Nelson  if (tmp) {
066ff12004-03-19Martin Nilsson  const char *err;
1caf511998-02-08Henrik Grubbström (Grubba)  MYSQL_ALLOW();
479f1a2008-08-20Henrik Grubbström (Grubba)  err = mysql_error(mysql);
1caf511998-02-08Henrik Grubbström (Grubba)  MYSQL_DISALLOW();
51f9c62003-10-13Martin Stjernholm  Pike_error("Mysql.mysql->select_db(): Couldn't select database \"%s\" (%s)\n",
6ac0282015-06-09Henrik Grubbström (Grubba)  database->str, err);
8426b71997-06-30Henrik Grubbström (Grubba)  } if (PIKE_MYSQL->database) { free_string(PIKE_MYSQL->database);
ab6aec1997-02-11Fredrik Hübinette (Hubbe)  }
6ac0282015-06-09Henrik Grubbström (Grubba)  add_ref(PIKE_MYSQL->database = database);
ab6aec1997-02-11Fredrik Hübinette (Hubbe)  pop_n_elems(args); }
df71022005-11-17Henrik Grubbström (Grubba) static void low_query(INT32 args, char *name, int flags)
ab6aec1997-02-11Fredrik Hübinette (Hubbe) {
479f1a2008-08-20Henrik Grubbström (Grubba)  MYSQL *mysql = PIKE_MYSQL->mysql;
6d5a8a1999-12-22Henrik Grubbström (Grubba)  MYSQL_RES *result = NULL;
ab6aec1997-02-11Fredrik Hübinette (Hubbe)  char *query; int qlen;
b72ff21997-11-03Henrik Grubbström (Grubba)  int tmp = -1;
ab6aec1997-02-11Fredrik Hübinette (Hubbe)  if (!args) {
43ed242005-04-02Martin Nilsson  SIMPLE_TOO_FEW_ARGS_ERROR (name, 1);
ab6aec1997-02-11Fredrik Hübinette (Hubbe)  }
51f9c62003-10-13Martin Stjernholm #ifdef HAVE_MYSQL_REAL_QUERY
43ed242005-04-02Martin Nilsson  CHECK_8BIT_STRING (name, 1);
51f9c62003-10-13Martin Stjernholm #else
43ed242005-04-02Martin Nilsson  CHECK_8BIT_NONBINARY_STRING (name, 1);
51f9c62003-10-13Martin Stjernholm #endif
ab6aec1997-02-11Fredrik Hübinette (Hubbe)  query = sp[-args].u.string->str; qlen = sp[-args].u.string->len;
6b30de2010-10-28Henrik Grubbström (Grubba)  /* NB: mysql_real_query() and mysql_query() call mysql_read_query_result() * which may have inlined a call to send_file_to_server(), which * allocates a buffer of IO_SIZE*15 (ie 0xf000) bytes on the stack. */ check_c_stack(0x10000);
479f1a2008-08-20Henrik Grubbström (Grubba)  if (mysql) {
1caf511998-02-08Henrik Grubbström (Grubba)  MYSQL_ALLOW();
ab6aec1997-02-11Fredrik Hübinette (Hubbe)  #ifdef HAVE_MYSQL_REAL_QUERY
479f1a2008-08-20Henrik Grubbström (Grubba)  tmp = mysql_real_query(mysql, query, qlen);
ab6aec1997-02-11Fredrik Hübinette (Hubbe) #else
479f1a2008-08-20Henrik Grubbström (Grubba)  tmp = mysql_query(mysql, query);
ab6aec1997-02-11Fredrik Hübinette (Hubbe) #endif /* HAVE_MYSQL_REAL_QUERY */
5244cd2003-12-22Dan Nelson  if (!tmp) {
df71022005-11-17Henrik Grubbström (Grubba)  if (flags & PIKE_MYSQL_FLAG_STORE_RESULT) {
479f1a2008-08-20Henrik Grubbström (Grubba)  result = mysql_store_result(mysql);
df71022005-11-17Henrik Grubbström (Grubba)  } else {
479f1a2008-08-20Henrik Grubbström (Grubba)  result = mysql_use_result(mysql);
df71022005-11-17Henrik Grubbström (Grubba)  }
6d5a8a1999-12-22Henrik Grubbström (Grubba)  }
1caf511998-02-08Henrik Grubbström (Grubba)  MYSQL_DISALLOW();
b444f51997-11-02Henrik Grubbström (Grubba)  }
8426b71997-06-30Henrik Grubbström (Grubba) 
5244cd2003-12-22Dan Nelson  if (tmp) { const char *err;
1caf511998-02-08Henrik Grubbström (Grubba)  MYSQL_ALLOW();
479f1a2008-08-20Henrik Grubbström (Grubba)  err = mysql_error(mysql);
1caf511998-02-08Henrik Grubbström (Grubba)  MYSQL_DISALLOW();
cce6aa1998-03-16Henrik Grubbström (Grubba)  if (sp[-args].u.string->len <= 512) {
43ed242005-04-02Martin Nilsson  Pike_error("%s(): Query \"%s\" failed (%s)\n", name, query, err);
cce6aa1998-03-16Henrik Grubbström (Grubba)  } else {
43ed242005-04-02Martin Nilsson  Pike_error("%s(): Query failed (%s)\n", name, err);
cce6aa1998-03-16Henrik Grubbström (Grubba)  }
ab6aec1997-02-11Fredrik Hübinette (Hubbe)  } pop_n_elems(args);
ad843e2001-09-06Henrik Grubbström (Grubba)  if (!(result)) {
1caf511998-02-08Henrik Grubbström (Grubba)  int err; MYSQL_ALLOW();
967e712000-12-07Henrik Grubbström (Grubba)  err = ( #ifdef mysql_field_count
479f1a2008-08-20Henrik Grubbström (Grubba)  mysql_field_count(mysql)
967e712000-12-07Henrik Grubbström (Grubba) #else /* !mysql_field_count */ #ifdef mysql_num_fields
479f1a2008-08-20Henrik Grubbström (Grubba)  mysql_num_fields(mysql)
967e712000-12-07Henrik Grubbström (Grubba) #else /* !mysql_num_fields */
479f1a2008-08-20Henrik Grubbström (Grubba)  mysql->field_count
967e712000-12-07Henrik Grubbström (Grubba) #endif /* mysql_num_fields */ #endif /* mysql_field_count */
479f1a2008-08-20Henrik Grubbström (Grubba)  && mysql_error(mysql)[0]);
1caf511998-02-08Henrik Grubbström (Grubba)  MYSQL_DISALLOW(); if (err) {
13ce622006-08-12Martin Stjernholm  const char *msg; MYSQL_ALLOW();
479f1a2008-08-20Henrik Grubbström (Grubba)  msg = mysql_error(mysql);
13ce622006-08-12Martin Stjernholm  MYSQL_DISALLOW();
43ed242005-04-02Martin Nilsson  Pike_error("%s(): Couldn't create result for query (%s)\n",
13ce622006-08-12Martin Stjernholm  name, msg);
ab6aec1997-02-11Fredrik Hübinette (Hubbe)  } /* query was INSERT or similar - return 0 */ push_int(0); } else { /* Return the result-object */
0160732015-06-09Henrik Grubbström (Grubba)  push_object(make_mysql_result(Pike_fp->current_object, result, flags));
ab6aec1997-02-11Fredrik Hübinette (Hubbe)  } }
43ed242005-04-02Martin Nilsson 
6ac0282015-06-09Henrik Grubbström (Grubba) PIKEFUN object big_query(string query)
43ed242005-04-02Martin Nilsson {
df71022005-11-17Henrik Grubbström (Grubba)  low_query(args, "big_query", PIKE_MYSQL_FLAG_STORE_RESULT);
43ed242005-04-02Martin Nilsson }
6ac0282015-06-09Henrik Grubbström (Grubba) PIKEFUN object streaming_query(string query)
43ed242005-04-02Martin Nilsson {
df71022005-11-17Henrik Grubbström (Grubba)  low_query(args, "streaming_query", 0);
43ed242005-04-02Martin Nilsson }
6ac0282015-06-09Henrik Grubbström (Grubba) PIKEFUN object big_typed_query(string query)
abb51b2009-11-05Henrik Grubbström (Grubba) { low_query(args, "big_typed_query", PIKE_MYSQL_FLAG_STORE_RESULT | PIKE_MYSQL_FLAG_TYPED_RESULT); }
6ac0282015-06-09Henrik Grubbström (Grubba) PIKEFUN object streaming_typed_query(string query)
abb51b2009-11-05Henrik Grubbström (Grubba) { low_query(args, "streaming_typed_query", PIKE_MYSQL_FLAG_TYPED_RESULT); }
43ed242005-04-02Martin Nilsson 
0be0372001-02-10Henrik Grubbström (Grubba) /*! @decl void create_db(string database) *! *! Create a new database *! *! This function creates a new database named @[database] *! in the Mysql-server. *! *! @seealso *! @[select_db()], @[drop_db()] */
4c14422002-01-02Per Hedbor #ifdef USE_OLD_FUNCTIONS
6ac0282015-06-09Henrik Grubbström (Grubba) PIKEFUN void create_db(string(8bit) database)
ab6aec1997-02-11Fredrik Hübinette (Hubbe) {
479f1a2008-08-20Henrik Grubbström (Grubba)  MYSQL *mysql = PIKE_MYSQL->mysql;
b72ff21997-11-03Henrik Grubbström (Grubba)  int tmp = -1;
ab6aec1997-02-11Fredrik Hübinette (Hubbe) 
de22f72014-08-25Martin Nilsson  CHECK_8BIT_NONBINARY_STRING ("create_db", 1);
6ac0282015-06-09Henrik Grubbström (Grubba)  if (database->len > 127) { if (database->len < 1024) {
b2d3e42000-12-01Fredrik Hübinette (Hubbe)  Pike_error("Database name \"%s\" is too long (max 127 characters)\n",
6ac0282015-06-09Henrik Grubbström (Grubba)  database->str);
e941661998-05-15Henrik Grubbström (Grubba)  } else {
530bd72001-09-23Henrik Grubbström (Grubba)  Pike_error("Database name (length %ld) is too long (max 127 characters)\n",
6ac0282015-06-09Henrik Grubbström (Grubba)  DO_NOT_WARN((long)database->len));
e941661998-05-15Henrik Grubbström (Grubba)  }
ab6aec1997-02-11Fredrik Hübinette (Hubbe)  }
479f1a2008-08-20Henrik Grubbström (Grubba)  if (mysql) {
1caf511998-02-08Henrik Grubbström (Grubba)  MYSQL_ALLOW();
ab6aec1997-02-11Fredrik Hübinette (Hubbe) 
6ac0282015-06-09Henrik Grubbström (Grubba)  tmp = mysql_create_db(mysql, database->str);
1caf511998-02-08Henrik Grubbström (Grubba)  MYSQL_DISALLOW();
b444f51997-11-02Henrik Grubbström (Grubba)  }
8426b71997-06-30Henrik Grubbström (Grubba) 
5244cd2003-12-22Dan Nelson  if (tmp) {
51f9c62003-10-13Martin Stjernholm  Pike_error("Mysql.mysql->create_db(): Creation of database \"%s\" failed\n",
6ac0282015-06-09Henrik Grubbström (Grubba)  database->str);
ab6aec1997-02-11Fredrik Hübinette (Hubbe)  } pop_n_elems(args); }
0be0372001-02-10Henrik Grubbström (Grubba) /*! @decl void drop_db(string database) *! *! Drop a database *! *! This function drops the database named @[database] from the Mysql-server. *! *! @seealso *! @[create_db()], @[select_db()] */
6ac0282015-06-09Henrik Grubbström (Grubba) PIKEFUN void drop_db(string(8bit) database)
ab6aec1997-02-11Fredrik Hübinette (Hubbe) {
479f1a2008-08-20Henrik Grubbström (Grubba)  MYSQL *mysql = PIKE_MYSQL->mysql;
b72ff21997-11-03Henrik Grubbström (Grubba)  int tmp = -1;
ab6aec1997-02-11Fredrik Hübinette (Hubbe) 
de22f72014-08-25Martin Nilsson  CHECK_8BIT_NONBINARY_STRING ("drop_db", 1);
6ac0282015-06-09Henrik Grubbström (Grubba)  if (database->len > 127) { if (database->len < 1024) {
b2d3e42000-12-01Fredrik Hübinette (Hubbe)  Pike_error("Database name \"%s\" is too long (max 127 characters)\n",
6ac0282015-06-09Henrik Grubbström (Grubba)  database->str);
e941661998-05-15Henrik Grubbström (Grubba)  } else {
530bd72001-09-23Henrik Grubbström (Grubba)  Pike_error("Database name (length %ld) is too long (max 127 characters)\n",
6ac0282015-06-09Henrik Grubbström (Grubba)  DO_NOT_WARN((long)database->len));
e941661998-05-15Henrik Grubbström (Grubba)  }
ab6aec1997-02-11Fredrik Hübinette (Hubbe)  }
479f1a2008-08-20Henrik Grubbström (Grubba)  if (mysql) {
1caf511998-02-08Henrik Grubbström (Grubba)  MYSQL_ALLOW();
ab6aec1997-02-11Fredrik Hübinette (Hubbe) 
6ac0282015-06-09Henrik Grubbström (Grubba)  tmp = mysql_drop_db(mysql, database->str);
ab6aec1997-02-11Fredrik Hübinette (Hubbe) 
1caf511998-02-08Henrik Grubbström (Grubba)  MYSQL_DISALLOW();
b444f51997-11-02Henrik Grubbström (Grubba)  }
8426b71997-06-30Henrik Grubbström (Grubba) 
5244cd2003-12-22Dan Nelson  if (tmp) {
51f9c62003-10-13Martin Stjernholm  Pike_error("Mysql.mysql->drop_db(): Drop of database \"%s\" failed\n",
6ac0282015-06-09Henrik Grubbström (Grubba)  database->str);
ab6aec1997-02-11Fredrik Hübinette (Hubbe)  } pop_n_elems(args); }
4c14422002-01-02Per Hedbor #endif
ab6aec1997-02-11Fredrik Hübinette (Hubbe) 
0be0372001-02-10Henrik Grubbström (Grubba) /*! @decl void shutdown() *! *! Shutdown the Mysql-server *! *! This function shuts down a running Mysql-server. *! *! @seealso *! @[reload()] */
6ac0282015-06-09Henrik Grubbström (Grubba) PIKEFUN void shutdown()
ab6aec1997-02-11Fredrik Hübinette (Hubbe) {
479f1a2008-08-20Henrik Grubbström (Grubba)  MYSQL *mysql = PIKE_MYSQL->mysql;
b72ff21997-11-03Henrik Grubbström (Grubba)  int tmp = -1;
ab6aec1997-02-11Fredrik Hübinette (Hubbe) 
479f1a2008-08-20Henrik Grubbström (Grubba)  if (mysql) {
1caf511998-02-08Henrik Grubbström (Grubba)  MYSQL_ALLOW();
13670c2015-05-25Martin Nilsson 
137fe02004-07-14Henrik Grubbström (Grubba) #ifdef HAVE_SHUTDOWN_DEFAULT
8953592004-07-13Dan Nelson  /* Mysql 4.1.3 added an extra shutdown_level argument. */
479f1a2008-08-20Henrik Grubbström (Grubba)  tmp = mysql_shutdown(mysql, SHUTDOWN_DEFAULT);
137fe02004-07-14Henrik Grubbström (Grubba) #else /* !HAVE_SHUTDOWN_DEFAULT */
479f1a2008-08-20Henrik Grubbström (Grubba)  tmp = mysql_shutdown(mysql);
137fe02004-07-14Henrik Grubbström (Grubba) #endif /* HAVE_SHUTDOWN_DEFAULT */
ab6aec1997-02-11Fredrik Hübinette (Hubbe) 
1caf511998-02-08Henrik Grubbström (Grubba)  MYSQL_DISALLOW();
b444f51997-11-02Henrik Grubbström (Grubba)  }
8426b71997-06-30Henrik Grubbström (Grubba) 
5244cd2003-12-22Dan Nelson  if (tmp) {
51f9c62003-10-13Martin Stjernholm  Pike_error("Mysql.mysql->shutdown(): Shutdown failed\n");
ab6aec1997-02-11Fredrik Hübinette (Hubbe)  } pop_n_elems(args); }
0be0372001-02-10Henrik Grubbström (Grubba) /*! @decl void reload() *! *! Reload security tables *! *! This function causes the Mysql-server to reload its access tables. *! *! @seealso *! @[shutdown()] */
6ac0282015-06-09Henrik Grubbström (Grubba) PIKEFUN void reload()
ab6aec1997-02-11Fredrik Hübinette (Hubbe) {
479f1a2008-08-20Henrik Grubbström (Grubba)  MYSQL *mysql = PIKE_MYSQL->mysql;
b72ff21997-11-03Henrik Grubbström (Grubba)  int tmp = -1;
ab6aec1997-02-11Fredrik Hübinette (Hubbe) 
479f1a2008-08-20Henrik Grubbström (Grubba)  if (mysql) {
1caf511998-02-08Henrik Grubbström (Grubba)  MYSQL_ALLOW();
ab6aec1997-02-11Fredrik Hübinette (Hubbe) 
479f1a2008-08-20Henrik Grubbström (Grubba)  tmp = mysql_reload(mysql);
ab6aec1997-02-11Fredrik Hübinette (Hubbe) 
1caf511998-02-08Henrik Grubbström (Grubba)  MYSQL_DISALLOW();
b444f51997-11-02Henrik Grubbström (Grubba)  }
8426b71997-06-30Henrik Grubbström (Grubba) 
5244cd2003-12-22Dan Nelson  if (tmp) {
51f9c62003-10-13Martin Stjernholm  Pike_error("Mysql.mysql->reload(): Reload failed\n");
ab6aec1997-02-11Fredrik Hübinette (Hubbe)  } pop_n_elems(args); }
0be0372001-02-10Henrik Grubbström (Grubba) /*! @decl string statistics() *! *! Some Mysql-server statistics *! *! This function returns some server statistics. *! *! @seealso
e9a4ab2011-09-06Arne Goedeke  *! @[server_info()], @[host_info()], @[protocol_info()], @[info()]
0be0372001-02-10Henrik Grubbström (Grubba)  */
6ac0282015-06-09Henrik Grubbström (Grubba) PIKEFUN string statistics()
ab6aec1997-02-11Fredrik Hübinette (Hubbe) {
479f1a2008-08-20Henrik Grubbström (Grubba)  MYSQL *mysql = PIKE_MYSQL->mysql;
066ff12004-03-19Martin Nilsson  const char *stats;
ab6aec1997-02-11Fredrik Hübinette (Hubbe)  pop_n_elems(args);
1caf511998-02-08Henrik Grubbström (Grubba)  MYSQL_ALLOW();
ab6aec1997-02-11Fredrik Hübinette (Hubbe) 
479f1a2008-08-20Henrik Grubbström (Grubba)  stats = mysql_stat(mysql);
ab6aec1997-02-11Fredrik Hübinette (Hubbe) 
1caf511998-02-08Henrik Grubbström (Grubba)  MYSQL_DISALLOW();
ab6aec1997-02-11Fredrik Hübinette (Hubbe)  push_text(stats); }
0be0372001-02-10Henrik Grubbström (Grubba) /*! @decl string server_info() *! *! Get the version number of the Mysql-server. *! *! @seealso
e9a4ab2011-09-06Arne Goedeke  *! @[statistics()], @[host_info()], @[protocol_info()], @[info()]
0be0372001-02-10Henrik Grubbström (Grubba)  */
6ac0282015-06-09Henrik Grubbström (Grubba) PIKEFUN string server_info()
ab6aec1997-02-11Fredrik Hübinette (Hubbe) {
479f1a2008-08-20Henrik Grubbström (Grubba)  MYSQL *mysql = PIKE_MYSQL->mysql;
5244cd2003-12-22Dan Nelson  const char *info;
ab6aec1997-02-11Fredrik Hübinette (Hubbe)  pop_n_elems(args); push_text("mysql/");
1caf511998-02-08Henrik Grubbström (Grubba)  MYSQL_ALLOW();
ab6aec1997-02-11Fredrik Hübinette (Hubbe) 
479f1a2008-08-20Henrik Grubbström (Grubba)  info = mysql_get_server_info(mysql);
ab6aec1997-02-11Fredrik Hübinette (Hubbe) 
1caf511998-02-08Henrik Grubbström (Grubba)  MYSQL_DISALLOW();
ab6aec1997-02-11Fredrik Hübinette (Hubbe)  push_text(info); f_add(2); }
0be0372001-02-10Henrik Grubbström (Grubba) /*! @decl string host_info() *! *! Get information about the Mysql-server connection *! *! @seealso
e9a4ab2011-09-06Arne Goedeke  *! @[statistics()], @[server_info()], @[protocol_info()], @[info()]
0be0372001-02-10Henrik Grubbström (Grubba)  */
6ac0282015-06-09Henrik Grubbström (Grubba) PIKEFUN string host_info()
ab6aec1997-02-11Fredrik Hübinette (Hubbe) {
479f1a2008-08-20Henrik Grubbström (Grubba)  MYSQL *mysql;
5244cd2003-12-22Dan Nelson  const char *info;
1caf511998-02-08Henrik Grubbström (Grubba) 
479f1a2008-08-20Henrik Grubbström (Grubba)  mysql = PIKE_MYSQL->mysql;
1caf511998-02-08Henrik Grubbström (Grubba) 
ab6aec1997-02-11Fredrik Hübinette (Hubbe)  pop_n_elems(args);
1caf511998-02-08Henrik Grubbström (Grubba)  MYSQL_ALLOW();
479f1a2008-08-20Henrik Grubbström (Grubba)  info = mysql_get_host_info(mysql);
1caf511998-02-08Henrik Grubbström (Grubba)  MYSQL_DISALLOW(); push_text(info);
ab6aec1997-02-11Fredrik Hübinette (Hubbe) }
e9a4ab2011-09-06Arne Goedeke /*! @decl string info() *! *! Get information about the most recently executed statement. *! *! @seealso *! @[statistics()], @[server_info()], @[protocol_info()], @[host_info()] */
6ac0282015-06-09Henrik Grubbström (Grubba) PIKEFUN string info() {
e9a4ab2011-09-06Arne Goedeke  MYSQL *mysql; const char *info; pop_n_elems(args); if (!PIKE_MYSQL->mysql) { pike_mysql_reconnect (1); push_undefined(); return; } mysql = PIKE_MYSQL->mysql; MYSQL_ALLOW(); info = mysql_info(mysql); MYSQL_DISALLOW(); if (info) push_text(info); else push_undefined(); }
0be0372001-02-10Henrik Grubbström (Grubba) /*! @decl int protocol_info() *! *! Give the Mysql protocol version *! *! This function returns the version number of the protocol the *! Mysql-server uses. *! *! @seealso *! @[statistics()], @[server_info()], @[host_info()] */
6ac0282015-06-09Henrik Grubbström (Grubba) PIKEFUN int protocol_info()
ab6aec1997-02-11Fredrik Hübinette (Hubbe) {
479f1a2008-08-20Henrik Grubbström (Grubba)  MYSQL *mysql;
1caf511998-02-08Henrik Grubbström (Grubba)  int prot;
ab6aec1997-02-11Fredrik Hübinette (Hubbe)  pop_n_elems(args);
479f1a2008-08-20Henrik Grubbström (Grubba)  mysql = PIKE_MYSQL->mysql;
1caf511998-02-08Henrik Grubbström (Grubba)  MYSQL_ALLOW();
479f1a2008-08-20Henrik Grubbström (Grubba)  prot = mysql_get_proto_info(mysql);
1caf511998-02-08Henrik Grubbström (Grubba)  MYSQL_DISALLOW(); push_int(prot);
ab6aec1997-02-11Fredrik Hübinette (Hubbe) }
f3ddd82003-12-16Johan Sundström /*! @decl object(Mysql.mysql_result) list_dbs() *! @decl object(Mysql.mysql_result) list_dbs(string wild)
0be0372001-02-10Henrik Grubbström (Grubba)  *! *! List databases *! *! Returns a table containing the names of all databases in the *! Mysql-server. If the argument @[wild] is specified, only those matching *! it will be returned. *! *! @seealso *! @[list_tables()], @[list_fields()], @[list_processes()],
230dd22002-11-26Henrik Grubbström (Grubba)  *! @[Mysql.mysql_result]
0be0372001-02-10Henrik Grubbström (Grubba)  */
6ac0282015-06-09Henrik Grubbström (Grubba) PIKEFUN object list_dbs(string|void wild)
ab6aec1997-02-11Fredrik Hübinette (Hubbe) {
479f1a2008-08-20Henrik Grubbström (Grubba)  MYSQL *mysql = PIKE_MYSQL->mysql;
8a81341997-11-05Henrik Wallin  MYSQL_RES *result = NULL;
ab6aec1997-02-11Fredrik Hübinette (Hubbe) 
7ed1632015-06-10Henrik Grubbström (Grubba)  if (wild) {
51f9c62003-10-13Martin Stjernholm  CHECK_8BIT_NONBINARY_STRING ("Mysql.mysql->list_dbs", 1);
6ac0282015-06-09Henrik Grubbström (Grubba)  if (wild->len > 80) { if (wild->len < 1024) {
b2d3e42000-12-01Fredrik Hübinette (Hubbe)  Pike_error("Wildcard \"%s\" is too long (max 80 characters)\n",
6ac0282015-06-09Henrik Grubbström (Grubba)  wild->str);
e941661998-05-15Henrik Grubbström (Grubba)  } else {
530bd72001-09-23Henrik Grubbström (Grubba)  Pike_error("Wildcard (length %ld) is too long (max 80 characters)\n",
6ac0282015-06-09Henrik Grubbström (Grubba)  DO_NOT_WARN((long)wild->len));
e941661998-05-15Henrik Grubbström (Grubba)  }
ab6aec1997-02-11Fredrik Hübinette (Hubbe)  } }
479f1a2008-08-20Henrik Grubbström (Grubba)  if (mysql) {
1caf511998-02-08Henrik Grubbström (Grubba)  MYSQL_ALLOW();
ab6aec1997-02-11Fredrik Hübinette (Hubbe) 
6ac0282015-06-09Henrik Grubbström (Grubba)  result = mysql_list_dbs(mysql, wild?wild->str:NULL);
ab6aec1997-02-11Fredrik Hübinette (Hubbe) 
1caf511998-02-08Henrik Grubbström (Grubba)  MYSQL_DISALLOW();
b444f51997-11-02Henrik Grubbström (Grubba)  }
8426b71997-06-30Henrik Grubbström (Grubba) 
ad843e2001-09-06Henrik Grubbström (Grubba)  if (!result) {
5244cd2003-12-22Dan Nelson  const char *err;
1caf511998-02-08Henrik Grubbström (Grubba)  MYSQL_ALLOW();
479f1a2008-08-20Henrik Grubbström (Grubba)  err = mysql_error(mysql);
1caf511998-02-08Henrik Grubbström (Grubba)  MYSQL_DISALLOW();
51f9c62003-10-13Martin Stjernholm  Pike_error("Mysql.mysql->list_dbs(): Cannot list databases: %s\n", err);
ab6aec1997-02-11Fredrik Hübinette (Hubbe)  } pop_n_elems(args);
0160732015-06-09Henrik Grubbström (Grubba)  /* Return the result-object */ push_object(make_mysql_result(Pike_fp->current_object, result, 0));
ab6aec1997-02-11Fredrik Hübinette (Hubbe) }
f3ddd82003-12-16Johan Sundström /*! @decl object(Mysql.mysql_result) list_tables() *! @decl object(Mysql.mysql_result) list_tables(string wild)
0be0372001-02-10Henrik Grubbström (Grubba)  *! *! List tables in the current database *! *! Returns a table containing the names of all tables in the current *! database. If the argument @[wild] is given, only those matching it *! will be returned. *! *! @seealso *! @[list_dbs()], @[list_fields()], @[list_processes()],
230dd22002-11-26Henrik Grubbström (Grubba)  *! @[Mysql.mysql_result]
0be0372001-02-10Henrik Grubbström (Grubba)  */
6ac0282015-06-09Henrik Grubbström (Grubba) PIKEFUN object list_tables(string|void wild)
ab6aec1997-02-11Fredrik Hübinette (Hubbe) {
479f1a2008-08-20Henrik Grubbström (Grubba)  MYSQL *mysql = PIKE_MYSQL->mysql;
8a81341997-11-05Henrik Wallin  MYSQL_RES *result = NULL;
ab6aec1997-02-11Fredrik Hübinette (Hubbe) 
7ed1632015-06-10Henrik Grubbström (Grubba)  if (wild) {
51f9c62003-10-13Martin Stjernholm  CHECK_8BIT_NONBINARY_STRING ("Mysql.mysql->list_tables", 1);
6ac0282015-06-09Henrik Grubbström (Grubba)  if (wild->len > 80) { if (wild->len < 1024) {
b2d3e42000-12-01Fredrik Hübinette (Hubbe)  Pike_error("Wildcard \"%s\" is too long (max 80 characters)\n",
6ac0282015-06-09Henrik Grubbström (Grubba)  wild->str);
e941661998-05-15Henrik Grubbström (Grubba)  } else {
530bd72001-09-23Henrik Grubbström (Grubba)  Pike_error("Wildcard (length %ld) is too long (max 80 characters)\n",
6ac0282015-06-09Henrik Grubbström (Grubba)  DO_NOT_WARN((long)wild->len));
e941661998-05-15Henrik Grubbström (Grubba)  }
ab6aec1997-02-11Fredrik Hübinette (Hubbe)  } }
479f1a2008-08-20Henrik Grubbström (Grubba)  if (mysql) {
1caf511998-02-08Henrik Grubbström (Grubba)  MYSQL_ALLOW();
ab6aec1997-02-11Fredrik Hübinette (Hubbe) 
6ac0282015-06-09Henrik Grubbström (Grubba)  result = mysql_list_tables(mysql, wild?wild->str:NULL);
ab6aec1997-02-11Fredrik Hübinette (Hubbe) 
1caf511998-02-08Henrik Grubbström (Grubba)  MYSQL_DISALLOW();
b444f51997-11-02Henrik Grubbström (Grubba)  }
8426b71997-06-30Henrik Grubbström (Grubba) 
ad843e2001-09-06Henrik Grubbström (Grubba)  if (!result) {
5244cd2003-12-22Dan Nelson  const char *err;
1caf511998-02-08Henrik Grubbström (Grubba)  MYSQL_ALLOW();
479f1a2008-08-20Henrik Grubbström (Grubba)  err = mysql_error(mysql);
1caf511998-02-08Henrik Grubbström (Grubba)  MYSQL_DISALLOW();
ef01522013-04-10Arne Goedeke  Pike_error("Mysql.mysql->list_tables(): Cannot list tables: %s\n", err);
ab6aec1997-02-11Fredrik Hübinette (Hubbe)  } pop_n_elems(args);
0160732015-06-09Henrik Grubbström (Grubba)  /* Return the result-object */ push_object(make_mysql_result(Pike_fp->current_object, result, 0));
ab6aec1997-02-11Fredrik Hübinette (Hubbe) }
0be0372001-02-10Henrik Grubbström (Grubba) /*! @decl array(int|mapping(string:mixed)) list_fields(string table) *! @decl array(int|mapping(string:mixed)) list_fields(string table, @ *! string wild) *! *! List all fields. *! *! Returns an array of mappings with information about the fields in the
f9e08b2001-10-28Martin Nilsson  *! table named @[table]. If the argument @[wild] is given, only those
0be0372001-02-10Henrik Grubbström (Grubba)  *! fields matching it will be returned *! *! The mappings contain the following entries: *! @mapping *! @member string "name" *! The name of the field. *! @member string "table" *! The name of the table. *! @member string "default" *! The default value for the field. *! @member string "type" *! The SQL type of the field. *! @member int "length"
3a1f7a2009-07-24Martin Stjernholm  *! The length of the longest possible value that can be stored in *! the field. Note that this measures the display length in *! string form.
0be0372001-02-10Henrik Grubbström (Grubba)  *! @member multiset(string) "flags" *! Some flags. *! @member int decimals *! The number of decimalplaces. *! @endmapping *! *! The type of the field can be any of:
cbe8c92003-04-07Martin Nilsson  *! @expr{"decimal"@}, @expr{"char"@}, @expr{"short"@}, @expr{"long"@}, *! @expr{"float"@}, @expr{"double"@}, @expr{"null"@}, @expr{"time"@}, *! @expr{"longlong"@}, @expr{"int24"@}, @expr{"tiny blob"@}, *! @expr{"medium blob"@}, *! @expr{"long blob"@}, @expr{"var string"@}, @expr{"string"@} or *! @expr{"unknown"@}.
0be0372001-02-10Henrik Grubbström (Grubba)  *! *! The flags multiset can contain any of: *! @multiset *! @index "primary_key" *! This field is part of the primary key for this table.
8c061a2014-09-12Arne Goedeke  *! @index "unique" *! This field is part of a unique key for this table. *! @index "multiple_key" *! This field is part of a nonunique key for this table.
0be0372001-02-10Henrik Grubbström (Grubba)  *! @index "not_null"
8c061a2014-09-12Arne Goedeke  *! This field cannot be NULL.
0be0372001-02-10Henrik Grubbström (Grubba)  *! @index "blob"
8c061a2014-09-12Arne Goedeke  *! This field is a BLOB or TEXT.
aad6e62011-06-05Arne Goedeke  *! @index "auto_increment"
8c061a2014-09-12Arne Goedeke  *! This field has the AUTO_INCREMENT attribute. *! @index "zerofill" *! This Field has the ZEROFILL attribute. *! @index "binary" *! This Field has the BINARY attribute. *! @index "enum" *! This Field is an ENUM. *! @index "set" *! This Field is a SET. *! @index "unsigned" *! This Field has the UNSIGNED attribute. *! @index "numeric" *! This Field is numeric.
0be0372001-02-10Henrik Grubbström (Grubba)  *! @endmultiset *! *! @note
cf87632001-04-28Henrik Grubbström (Grubba)  *! Michael Widenius recomends use of the following query instead:
0be0372001-02-10Henrik Grubbström (Grubba)  *! @tt{show fields in 'table' like "wild"@}. *! *! @seealso
230dd22002-11-26Henrik Grubbström (Grubba)  *! @[list_dbs()], @[list_tables()], @[list_processes()], *! @[Mysql.mysql_result.fetch_field()]
0be0372001-02-10Henrik Grubbström (Grubba)  */
6ac0282015-06-09Henrik Grubbström (Grubba) PIKEFUN array(int|mapping(string:mixed)) list_fields(string(8bit) table, string(8bit)|void wild)
ab6aec1997-02-11Fredrik Hübinette (Hubbe) {
479f1a2008-08-20Henrik Grubbström (Grubba)  MYSQL *mysql = PIKE_MYSQL->mysql;
8a81341997-11-05Henrik Wallin  MYSQL_RES *result = NULL;
ab6aec1997-02-11Fredrik Hübinette (Hubbe)  MYSQL_FIELD *field; int i = 0;
51f9c62003-10-13Martin Stjernholm 
de22f72014-08-25Martin Nilsson  CHECK_8BIT_NONBINARY_STRING ("list_fields", 1);
6ac0282015-06-09Henrik Grubbström (Grubba)  if (table->len > 125) { if (table->len < 1024) {
b2d3e42000-12-01Fredrik Hübinette (Hubbe)  Pike_error("Table name \"%s\" is too long (max 125 characters)\n",
6ac0282015-06-09Henrik Grubbström (Grubba)  table->str);
e941661998-05-15Henrik Grubbström (Grubba)  } else {
530bd72001-09-23Henrik Grubbström (Grubba)  Pike_error("Table name (length %ld) is too long (max 125 characters)\n",
6ac0282015-06-09Henrik Grubbström (Grubba)  DO_NOT_WARN((long)table->len));
e941661998-05-15Henrik Grubbström (Grubba)  }
ab6aec1997-02-11Fredrik Hübinette (Hubbe)  }
7ed1632015-06-10Henrik Grubbström (Grubba)  if (wild) {
51f9c62003-10-13Martin Stjernholm  CHECK_8BIT_NONBINARY_STRING ("Mysql.mysql->list_fields", 2);
6ac0282015-06-09Henrik Grubbström (Grubba)  if (wild->len + table->len > 125) {
e941661998-05-15Henrik Grubbström (Grubba)  /* The length of the table name has already been checked. */
6ac0282015-06-09Henrik Grubbström (Grubba)  if (wild->len < 1024) {
b2d3e42000-12-01Fredrik Hübinette (Hubbe)  Pike_error("Wildcard \"%s\" + table name \"%s\" is too long "
6ac0282015-06-09Henrik Grubbström (Grubba)  "(max 125 characters)\n", wild->str, table->str);
e941661998-05-15Henrik Grubbström (Grubba)  } else {
530bd72001-09-23Henrik Grubbström (Grubba)  Pike_error("Wildcard (length %ld) + table name \"%s\" is too long " "(max 125 characters)\n",
6ac0282015-06-09Henrik Grubbström (Grubba)  DO_NOT_WARN((long)wild->len), table->str);
e941661998-05-15Henrik Grubbström (Grubba)  }
ab6aec1997-02-11Fredrik Hübinette (Hubbe)  } }
479f1a2008-08-20Henrik Grubbström (Grubba)  if (mysql) {
1caf511998-02-08Henrik Grubbström (Grubba)  MYSQL_ALLOW();
ab6aec1997-02-11Fredrik Hübinette (Hubbe) 
6ac0282015-06-09Henrik Grubbström (Grubba)  result = mysql_list_fields(mysql, table->str, wild?wild->str:NULL);
ab6aec1997-02-11Fredrik Hübinette (Hubbe) 
1caf511998-02-08Henrik Grubbström (Grubba)  MYSQL_DISALLOW();
b444f51997-11-02Henrik Grubbström (Grubba)  }
8426b71997-06-30Henrik Grubbström (Grubba)  if (!result) {
5244cd2003-12-22Dan Nelson  const char *err;
1caf511998-02-08Henrik Grubbström (Grubba)  MYSQL_ALLOW();
479f1a2008-08-20Henrik Grubbström (Grubba)  err = mysql_error(mysql);
1caf511998-02-08Henrik Grubbström (Grubba)  MYSQL_DISALLOW();
ef01522013-04-10Arne Goedeke  Pike_error("Mysql.mysql->list_fields(): Cannot list fields: %s\n", err);
ab6aec1997-02-11Fredrik Hübinette (Hubbe)  } pop_n_elems(args);
1caf511998-02-08Henrik Grubbström (Grubba)  /* FIXME: Should have MYSQL_{DIS,}ALLOW() here */
ab6aec1997-02-11Fredrik Hübinette (Hubbe)  while ((field = mysql_fetch_field(result))) { mysqlmod_parse_field(field, 1); i++; } f_aggregate(i); }
f3ddd82003-12-16Johan Sundström /*! @decl object(Mysql.mysql_result) list_processes()
0be0372001-02-10Henrik Grubbström (Grubba)  *! *! List all processes in the Mysql-server *! *! Returns a table containing the names of all processes in the *! Mysql-server. *! *! @seealso *! @[list_dbs()], @[list_tables()], @[list_fields()], @[Mysql.mysql_result] */
6ac0282015-06-09Henrik Grubbström (Grubba) PIKEFUN object list_processes()
ab6aec1997-02-11Fredrik Hübinette (Hubbe) {
479f1a2008-08-20Henrik Grubbström (Grubba)  MYSQL *mysql = PIKE_MYSQL->mysql;
8a81341997-11-05Henrik Wallin  MYSQL_RES *result = NULL;
ab6aec1997-02-11Fredrik Hübinette (Hubbe)  pop_n_elems(args);
479f1a2008-08-20Henrik Grubbström (Grubba)  if (mysql) {
1caf511998-02-08Henrik Grubbström (Grubba)  MYSQL_ALLOW();
ab6aec1997-02-11Fredrik Hübinette (Hubbe) 
479f1a2008-08-20Henrik Grubbström (Grubba)  result = mysql_list_processes(mysql);
ab6aec1997-02-11Fredrik Hübinette (Hubbe) 
1caf511998-02-08Henrik Grubbström (Grubba)  MYSQL_DISALLOW();
b444f51997-11-02Henrik Grubbström (Grubba)  }
8426b71997-06-30Henrik Grubbström (Grubba) 
ad843e2001-09-06Henrik Grubbström (Grubba)  if (!result) {
5244cd2003-12-22Dan Nelson  const char *err;
1caf511998-02-08Henrik Grubbström (Grubba)  MYSQL_ALLOW();
479f1a2008-08-20Henrik Grubbström (Grubba)  err = mysql_error(mysql);
1caf511998-02-08Henrik Grubbström (Grubba)  MYSQL_DISALLOW();
ef01522013-04-10Arne Goedeke  Pike_error("Mysql.mysql->list_processes(): Cannot list processes: %s\n", err);
ab6aec1997-02-11Fredrik Hübinette (Hubbe)  }
0160732015-06-09Henrik Grubbström (Grubba)  /* Return the result-object */ push_object(make_mysql_result(Pike_fp->current_object, result, 0));
ab6aec1997-02-11Fredrik Hübinette (Hubbe) }
0be0372001-02-10Henrik Grubbström (Grubba) /*! @decl int binary_data() *! *! Inform if this version of @[Mysql.mysql] supports binary data *! *! This function returns non-zero if binary data can be reliably stored *! and retreived with this version of the mysql-module. *! *! Usually, there is no problem storing binary data in mysql-tables,
cbe8c92003-04-07Martin Nilsson  *! but data containing @expr{'\0'@} (NUL) couldn't be fetched with old
0be0372001-02-10Henrik Grubbström (Grubba)  *! versions (prior to 3.20.5) of the mysql-library.
ab6aec1997-02-11Fredrik Hübinette (Hubbe)  */
6ac0282015-06-09Henrik Grubbström (Grubba) PIKEFUN int binary_data()
ab6aec1997-02-11Fredrik Hübinette (Hubbe) { pop_n_elems(args); #ifdef HAVE_MYSQL_FETCH_LENGTHS push_int(1); #else push_int(0); #endif /* HAVE_MYSQL_FETCH_LENGTHS */ }
6ac0282015-06-09Henrik Grubbström (Grubba) PIKEFUN void set_charset(string(8bit) charset)
aa16eb2006-08-12Martin Stjernholm { if (string_has_null (charset)) SIMPLE_ARG_ERROR ("set_charset", 0, "The charset name cannot contain a NUL character.");
474ba12012-01-07Martin Stjernholm  connection_set_charset (charset);
aa16eb2006-08-12Martin Stjernholm  if (PIKE_MYSQL->conn_charset) free_string (PIKE_MYSQL->conn_charset); copy_shared_string (PIKE_MYSQL->conn_charset, charset); pop_n_elems (args); }
6ac0282015-06-09Henrik Grubbström (Grubba) PIKEFUN string(8bit) get_charset()
aa16eb2006-08-12Martin Stjernholm { pop_n_elems (args); if (PIKE_MYSQL->conn_charset) ref_push_string (PIKE_MYSQL->conn_charset); else
7cf0162014-05-22Per Hedbor  push_text ("latin1");
aa16eb2006-08-12Martin Stjernholm }
6ac0282015-06-09Henrik Grubbström (Grubba) PIKEFUN int(0..1) _can_send_as_latin1(string str)
aa16eb2006-08-12Martin Stjernholm /* Helper function to detect if a string can be sent in the latin1 * encoding. */ { ptrdiff_t i; int res; if (str->size_shift) res = 0; else { /* Have to go through the string to check that it doesn't contain * any of those pesky chars in the 0x80..0x9f range that MySQL has * remapped in latin1. */ /* This check could be made tighter by ignoring chars in strings * with introducers. */ res = 1; for (i = str->len; i--;) { int chr = STR0 (str)[i]; if (chr >= 0x80 && chr <= 0x9f && chr != 0x81 && chr != 0x8d && chr != 0x8f && chr != 0x90 && chr != 0x9d) { res = 0; break; } } } pop_stack(); push_int (res); }
6ac0282015-06-09Henrik Grubbström (Grubba) }
0be0372001-02-10Henrik Grubbström (Grubba) /*! @endclass */
2dfcdb2013-02-27Henrik Grubbström (Grubba) /*! @decl string client_info() *! *! Get some information about the Mysql-server client library. *! *! @seealso *! @[mysql()->statistics()], @[mysql()->server_info()], *! @[mysql()->protocol_info()], @[mysql()->info()] */
6ac0282015-06-09Henrik Grubbström (Grubba) PIKEFUN string(8bit) client_info()
2dfcdb2013-02-27Henrik Grubbström (Grubba) { pop_n_elems(args); #ifndef MYSQL_COMPILATION_COMMENT #define MYSQL_COMPILATION_COMMENT "MySQL (Copyright Abandoned)" #endif push_text(MYSQL_COMPILATION_COMMENT "/" MYSQL_SERVER_VERSION); }
0be0372001-02-10Henrik Grubbström (Grubba) /*! @endmodule */
ab6aec1997-02-11Fredrik Hübinette (Hubbe) #endif /* HAVE_MYSQL */ /* * Module linkage */
51ef5c2002-10-21Marcus Comstedt PIKE_MODULE_INIT
ab6aec1997-02-11Fredrik Hübinette (Hubbe) {
44384f2015-06-09Henrik Grubbström (Grubba) #ifdef HAVE_MYSQL
6ac0282015-06-09Henrik Grubbström (Grubba)  INIT;
ab6aec1997-02-11Fredrik Hübinette (Hubbe) 
1b02b52013-10-13Arne Goedeke #ifdef HAVE_MYSQL_MYSQLD_ERNAME_H { struct mysqld_ername { const char * msg; unsigned int code; }; unsigned int i; static const struct mysqld_ername list[] = { #include <mysql/mysqld_ername.h> }; const unsigned int n = sizeof(list)/sizeof(list[0]); start_new_program(); for (i = 0; i < n; i++) { add_integer_constant(list[i].msg, list[i].code, 0); }
50e5682013-11-02Arne Goedeke  mysql_error_program = end_program(); add_program_constant("error", mysql_error_program, 0);
1b02b52013-10-13Arne Goedeke  } #endif
d526a02015-06-14Henrik Grubbström (Grubba) #ifndef HAVE_MYSQL_REAL_CONNECT
5722651998-01-25Henrik Grubbström (Grubba) #ifdef HAVE_MYSQL_PORT STUPID_PORT_INIT(); #endif /* HAVE_MYSQL_PORT */
d526a02015-06-14Henrik Grubbström (Grubba) #endif /* !HAVE_MYSQL_REAL_CONNECT */
5722651998-01-25Henrik Grubbström (Grubba) 
ab6aec1997-02-11Fredrik Hübinette (Hubbe)  init_mysql_res_programs();
44384f2015-06-09Henrik Grubbström (Grubba) #else HIDE_MODULE();
07e1b72015-06-09Henrik Grubbström (Grubba) #endif /* HAVE_MYSQL */
ab6aec1997-02-11Fredrik Hübinette (Hubbe) }
51ef5c2002-10-21Marcus Comstedt PIKE_MODULE_EXIT
ab6aec1997-02-11Fredrik Hübinette (Hubbe) { #ifdef HAVE_MYSQL exit_mysql_res();
d526a02015-06-14Henrik Grubbström (Grubba) #ifndef HAVE_MYSQL_REAL_CONNECT
5722651998-01-25Henrik Grubbström (Grubba) #ifdef HAVE_MYSQL_PORT STUPID_PORT_DESTROY(); #endif /* HAVE_MYSQL_PORT */
d526a02015-06-14Henrik Grubbström (Grubba) #endif /* !HAVE_MYSQL_REAL_CONNECT */
5722651998-01-25Henrik Grubbström (Grubba) 
50e5682013-11-02Arne Goedeke #ifdef HAVE_MYSQL_MYSQLD_ERNAME_H if (mysql_error_program) { free_program(mysql_error_program); mysql_error_program = NULL; } #endif
6ac0282015-06-09Henrik Grubbström (Grubba)  EXIT;
44384f2015-06-09Henrik Grubbström (Grubba) #endif /* HAVE_MYSQL */
ab6aec1997-02-11Fredrik Hübinette (Hubbe) }