⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 libmysql.c

📁 php-4.4.7学习linux时下载的源代码
💻 C
📖 第 1 页 / 共 5 页
字号:
      {	cur->data[field] = to;	if (len > end_to - to)	{	  free_rows(result);	  net->last_errno = CR_UNKNOWN_ERROR;	  strmov(net->last_error, ER(net->last_errno));	  DBUG_RETURN(0);	}	memcpy(to,(char*) cp,len); to[len]=0;	to+=len+1;	cp+=len;	if (mysql_fields)	{	  if (mysql_fields[field].max_length < len)	    mysql_fields[field].max_length=len;	}      }    }    cur->data[field]=to;			/* End of last field */    if ((pkt_len=net_safe_read(mysql)) == packet_error)    {      free_rows(result);      DBUG_RETURN(0);    }  }  *prev_ptr=0;					/* last pointer is null */  DBUG_PRINT("exit",("Got %d rows",result->rows));  DBUG_RETURN(result);}/*** Read one row. Uses packet buffer as storage for fields.** When next packet is read, the previous field values are destroyed*/static intread_one_row(MYSQL *mysql,uint fields,MYSQL_ROW row, ulong *lengths){  uint field;  ulong pkt_len,len;  uchar *pos,*prev_pos,*end_pos;  if ((pkt_len=(uint) net_safe_read(mysql)) == packet_error)    return -1;  if (pkt_len == 1 && mysql->net.read_pos[0] == 254)    return 1;				/* End of data */  prev_pos= 0;				/* allowed to write at packet[-1] */  pos=mysql->net.read_pos;  end_pos=pos+pkt_len;  for (field=0 ; field < fields ; field++)  {    if ((len=(ulong) net_field_length(&pos)) == NULL_LENGTH)    {						/* null field */      row[field] = 0;      *lengths++=0;    }    else    {      if (len > end_pos - pos)      {	mysql->net.last_errno=CR_UNKNOWN_ERROR;	strmov(mysql->net.last_error, ER(mysql->net.last_errno));	return -1;      }      row[field] = (char*) pos;      pos+=len;      *lengths++=len;    }    if (prev_pos)      *prev_pos=0;				/* Terminate prev field */    prev_pos=pos;  }  row[field]=(char*) prev_pos+1;		/* End of last field */  *prev_pos=0;					/* Terminate last field */  return 0;}/****************************************************************************** Init MySQL structure or allocate one****************************************************************************/MYSQL * STDCALLmysql_init(MYSQL *mysql){  mysql_once_init();  if (!mysql)  {    if (!(mysql=(MYSQL*) my_malloc(sizeof(*mysql),MYF(MY_WME | MY_ZEROFILL))))      return 0;    mysql->free_me=1;    mysql->net.vio = 0;  }  else    bzero((char*) (mysql),sizeof(*(mysql)));  mysql->options.connect_timeout=CONNECT_TIMEOUT;#if defined(SIGPIPE) && defined(THREAD) && !defined(__WIN__)  if (!((mysql)->client_flag & CLIENT_IGNORE_SIGPIPE))    (void) signal(SIGPIPE,pipe_sig_handler);#endif/*  Only enable LOAD DATA INFILE by default if configured with  --with-enabled-local-inflile*/#ifdef ENABLED_LOCAL_INFILE  mysql->options.client_flag|= CLIENT_LOCAL_FILES;#endif  return mysql;}static void mysql_once_init(){  if (!mysql_client_init)  {    mysql_client_init=1;    my_init();					/* Will init threads */    init_client_errs();    if (!mysql_port)    {      mysql_port = MYSQL_PORT;#ifndef MSDOS      {	struct servent *serv_ptr;	char	*env;	if ((serv_ptr = getservbyname("mysql", "tcp")))	  mysql_port = (uint) ntohs((ushort) serv_ptr->s_port);	if ((env = getenv("MYSQL_TCP_PORT")))	  mysql_port =(uint) atoi(env);      }#endif    }    if (!mysql_unix_port)    {      char *env;#ifdef __WIN__      mysql_unix_port = (char*) MYSQL_NAMEDPIPE;#else      mysql_unix_port = (char*) MYSQL_UNIX_ADDR;#endif      if ((env = getenv("MYSQL_UNIX_PORT")))	mysql_unix_port = env;    }    mysql_debug(NullS);#if defined(SIGPIPE) && !defined(THREAD) && !defined(__WIN__)    (void) signal(SIGPIPE,SIG_IGN);#endif  }#ifdef THREAD  else    my_thread_init();         /* Init if new thread */#endif}#ifdef HAVE_OPENSSL/**************************************************************************** Fill in SSL part of MYSQL structure and set 'use_ssl' flag.** NB! Errors are not reported until you do mysql_real_connect.**************************************************************************/int STDCALLmysql_ssl_set(MYSQL *mysql, const char *key, const char *cert,              const char *ca, const char *capath){  mysql->options.ssl_key = key==0 ? 0 : my_strdup(key,MYF(0));  mysql->options.ssl_cert = cert==0 ? 0 : my_strdup(cert,MYF(0));  mysql->options.ssl_ca = ca==0 ? 0 : my_strdup(ca,MYF(0));  mysql->options.ssl_capath = capath==0 ? 0 : my_strdup(capath,MYF(0));  mysql->options.use_ssl = true;  mysql->connector_fd = new_VioSSLConnectorFd(key, cert, ca, capath);  return 0;}/****************************************************************************************************************************************************/char * STDCALLmysql_ssl_cipher(MYSQL *mysql){  return (char *)mysql->net.vio->cipher_description();}/**************************************************************************** Free strings in the SSL structure and clear 'use_ssl' flag.** NB! Errors are not reported until you do mysql_real_connect.**************************************************************************/int STDCALLmysql_ssl_clear(MYSQL *mysql){  my_free(mysql->options.ssl_key, MYF(MY_ALLOW_ZERO_PTR));  my_free(mysql->options.ssl_cert, MYF(MY_ALLOW_ZERO_PTR));  my_free(mysql->options.ssl_ca, MYF(MY_ALLOW_ZERO_PTR));  my_free(mysql->options.ssl_capath, MYF(MY_ALLOW_ZERO_PTR));  mysql->options.ssl_key = 0;  mysql->options.ssl_cert = 0;  mysql->options.ssl_ca = 0;  mysql->options.ssl_capath = 0;  mysql->options.use_ssl = false;  mysql->connector_fd->delete();  mysql->connector_fd = 0;  return 0;}#endif /* HAVE_OPENSSL *//**************************************************************************** Connect to sql server** If host == 0 then use localhost**************************************************************************/MYSQL * STDCALLmysql_connect(MYSQL *mysql,const char *host,	      const char *user, const char *passwd){  MYSQL *res;  mysql=mysql_init(mysql);			/* Make it thread safe */  {    DBUG_ENTER("mysql_connect");    if (!(res=mysql_real_connect(mysql,host,user,passwd,NullS,0,NullS,0)))    {      if (mysql->free_me)	my_free((gptr) mysql,MYF(0));    }    DBUG_RETURN(res);  }}/*** Note that the mysql argument must be initialized with mysql_init()** before calling mysql_real_connect !*/MYSQL * STDCALLmysql_real_connect(MYSQL *mysql,const char *host, const char *user,		   const char *passwd, const char *db,		   uint port, const char *unix_socket,uint client_flag){  char		buff[NAME_LEN+USERNAME_LENGTH+100],charset_name_buff[16];  char		*end,*host_info,*charset_name;  my_socket	sock;  uint32	ip_addr;  struct	sockaddr_in sock_addr;  uint		pkt_length;  NET		*net= &mysql->net;#ifdef __WIN__  HANDLE	hPipe=INVALID_HANDLE_VALUE;#endif#ifdef HAVE_SYS_UN_H  struct	sockaddr_un UNIXaddr;#endif  init_sigpipe_variables  DBUG_ENTER("mysql_real_connect");  DBUG_PRINT("enter",("host: %s  db: %s  user: %s",		      host ? host : "(Null)",		      db ? db : "(Null)",		      user ? user : "(Null)"));  /* Don't give sigpipe errors if the client doesn't want them */  set_sigpipe(mysql);  net->vio = 0;				/* If something goes wrong */  /* use default options */  if (mysql->options.my_cnf_file || mysql->options.my_cnf_group)  {    mysql_read_default_options(&mysql->options,			       (mysql->options.my_cnf_file ?				mysql->options.my_cnf_file : "my"),			       mysql->options.my_cnf_group);    my_free(mysql->options.my_cnf_file,MYF(MY_ALLOW_ZERO_PTR));    my_free(mysql->options.my_cnf_group,MYF(MY_ALLOW_ZERO_PTR));    mysql->options.my_cnf_file=mysql->options.my_cnf_group=0;  }  /* Some empty-string-tests are done because of ODBC */  if (!host || !host[0])    host=mysql->options.host;  if (!user || !user[0])    user=mysql->options.user;  if (!passwd)  {    passwd=mysql->options.password;#ifndef DONT_USE_MYSQL_PWD    if (!passwd)      passwd=getenv("MYSQL_PWD");  /* get it from environment (haneke) */#endif  }  if (!db || !db[0])    db=mysql->options.db;  if (!port)    port=mysql->options.port;  if (!unix_socket)    unix_socket=mysql->options.unix_socket;  mysql->reconnect=1;			/* Reconnect as default */  mysql->server_status=SERVER_STATUS_AUTOCOMMIT;  /*  ** Grab a socket and connect it to the server  */#if defined(HAVE_SYS_UN_H)  if ((!host || !strcmp(host,LOCAL_HOST)) && (unix_socket || mysql_unix_port))  {    host=LOCAL_HOST;    if (!unix_socket)      unix_socket=mysql_unix_port;    host_info=(char*) ER(CR_LOCALHOST_CONNECTION);    DBUG_PRINT("info",("Using UNIX sock '%s'",unix_socket));    if ((sock = socket(AF_UNIX,SOCK_STREAM,0)) == SOCKET_ERROR)    {      net->last_errno=CR_SOCKET_CREATE_ERROR;      sprintf(net->last_error,ER(net->last_errno),socket_errno);      goto error;    }    net->vio = vio_new(sock, VIO_TYPE_SOCKET, TRUE);    bzero((char*) &UNIXaddr,sizeof(UNIXaddr));    UNIXaddr.sun_family = AF_UNIX;    strmake(UNIXaddr.sun_path, unix_socket, sizeof(UNIXaddr.sun_path) - 1);    if (connect2(sock,(struct sockaddr *) &UNIXaddr, sizeof(UNIXaddr),		 mysql->options.connect_timeout) <0)    {      DBUG_PRINT("error",("Got error %d on connect to local server",socket_errno));      net->last_errno=CR_CONNECTION_ERROR;      sprintf(net->last_error,ER(net->last_errno),unix_socket,socket_errno);      goto error;    }  }  else#elif defined(__WIN__)  {    if ((unix_socket ||	 !host && is_NT() ||	 host && !strcmp(host,LOCAL_HOST_NAMEDPIPE) ||	 mysql->options.named_pipe || !have_tcpip))    {      sock=0;      if ((hPipe=create_named_pipe(net, mysql->options.connect_timeout,				   (char**) &host, (char**) &unix_socket)) ==	  INVALID_HANDLE_VALUE)      {	DBUG_PRINT("error",		   ("host: '%s'  socket: '%s'  named_pipe: %d  have_tcpip: %d",		    host ? host : "<null>",		    unix_socket ? unix_socket : "<null>",		    (int) mysql->options.named_pipe,		    (int) have_tcpip));	if (mysql->options.named_pipe ||	    (host && !strcmp(host,LOCAL_HOST_NAMEDPIPE)) ||	    (unix_socket && !strcmp(unix_socket,MYSQL_NAMEDPIPE)))	  goto error;		/* User only requested named pipes */	/* Try also with TCP/IP */      }      else      {	net->vio=vio_new_win32pipe(hPipe);	sprintf(host_info=buff, ER(CR_NAMEDPIPE_CONNECTION), host,		unix_socket);      }    }  }  if (hPipe == INVALID_HANDLE_VALUE)#endif  {    unix_socket=0;				/* This is not used */    if (!port)      port=mysql_port;    if (!host)      host=LOCAL_HOST;    sprintf(host_info=buff,ER(CR_TCP_CONNECTION),host);    DBUG_PRINT("info",("Server name: '%s'.  TCP sock: %d", host,port));    /* _WIN64 ;  Assume that the (int) range is enough for socket() */    if ((sock = (my_socket) socket(AF_INET,SOCK_STREAM,0)) == SOCKET_ERROR)    {      net->last_errno=CR_IPSOCK_ERROR;      sprintf(net->last_error,ER(net->last_errno),socket_errno);      goto error;    }    net->vio = vio_new(sock,VIO_TYPE_TCPIP,FALSE);    bzero((char*) &sock_addr,sizeof(sock_addr));    sock_addr.sin_family = AF_INET;    /*    ** The server name may be a host name or IP address    */    if ((int) (ip_addr = inet_addr(host)) != (int) INADDR_NONE)    {      memcpy_fixed(&sock_addr.sin_addr,&ip_addr,sizeof(ip_addr));    }    else#if defined(HAVE_GETHOSTBYNAME_R) && defined(_REENTRANT) && defined(THREAD)    {      int tmp_errno;      struct hostent tmp_hostent,*hp;      char buff2[GETHOSTBYNAME_BUFF_SIZE];      hp = my_gethostbyname_r(host,&tmp_hostent,buff2,sizeof(buff2),			      &tmp_errno);      if (!hp)      {	net->last_errno=CR_UNKNOWN_HOST;	sprintf(net->last_error, ER(CR_UNKNOWN_HOST), host, tmp_errno);	goto error;      }      memcpy(&sock_addr.sin_addr,hp->h_addr, (size_t) hp->h_length);    }#else    {      struct hostent *hp;      if (!(hp=gethostbyname(host)))      {	net->last_errno=CR_UNKNOWN_HOST;	sprintf(net->last_error, ER(CR_UNKNOWN_HOST), host, socket_errno);	goto error;      }      memcpy(&sock_addr.sin_addr,hp->h_addr, (size_t) hp->h_length);    }#endif    sock_addr.sin_port = (ushort) htons((ushort) port);    if (connect2(sock,(struct sockaddr *) &sock_addr, sizeof(sock_addr),		 mysql->options.connect_timeout) <0)    {      DBUG_PRINT("error",("Got error %d on connect to '%s'",socket_errno,host));      net->last_errno= CR_CONN_HOST_ERROR;      sprintf(net->last_error ,ER(CR_CONN_HOST_ERROR), host, socket_errno);      goto error;    }  }  if (!net->vio || my_net_init(net, net->vio))  {    vio_delete(net->vio);    net->vio = 0;    net->last_errno=CR_OUT_OF_MEMORY;    strmov(net->last_error,ER(net->last_errno));    goto error;  }  vio_keepalive(net->vio,TRUE);  /* Get version info */  mysql->protocol_version= PROTOCOL_VERSION;	/* Assume this */  if (mysql->options.connect_timeout &&      vio_poll_read(net->vio, mysql->options.connect_timeout))  {    net->last_errno= CR_SERVER_LOST;    strmov(net->last_error,ER(net->last_errno));        goto error;  }  if ((pkt_length=net_safe_read(mysql)) == packet_error)    goto error;  /* Check if version of protocoll matches current one */  mysql->protocol_version= net->read_pos[0];  DBUG_DUMP("packet",(char*) net->read_pos,10);  DBUG_PRINT("info",("mysql protocol version %d, server=%d",		     PROTOCOL_VERSION, mysql->protocol_version));  if (mysql->protocol_version != PROTOCOL_VERSION &&

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -