libdrizzle Public API Documentation

examples/pipe_query.c
Go to the documentation of this file.
00001 /*
00002  * Drizzle Client & Protocol Library
00003  *
00004  * Copyright (C) 2008 Eric Day (eday@oddments.org)
00005  * All rights reserved.
00006  *
00007  * Use and distribution licensed under the BSD license.  See
00008  * the COPYING file in this directory for full text.
00009  */
00010 
00011 #include "config.h"
00012 
00013 #include <errno.h>
00014 #include <stdio.h>
00015 #include <stdlib.h>
00016 #include <string.h>
00017 #include <unistd.h>
00018 
00019 #include <libdrizzle/drizzle_client.h>
00020 
00021 #define BUFFER_CHUNK 8192
00022 
00023 int main(int argc, char *argv[])
00024 {
00025   int c;
00026   char *host= NULL;
00027   in_port_t port= 0;
00028   char *user= NULL;
00029   char *password= NULL;
00030   char *buffer= NULL;
00031   size_t buffer_size= 0;
00032   size_t buffer_total= 0;
00033   ssize_t read_size= 0;
00034   drizzle_st drizzle;
00035   drizzle_con_st con;
00036   drizzle_result_st result;
00037   drizzle_return_t ret;
00038   drizzle_field_t field;
00039   size_t offset;
00040   size_t size;
00041   size_t total;
00042 
00043   /* The docs say this might fail, so check for errors. */
00044   if (drizzle_create(&drizzle) == NULL)
00045   {
00046     printf("drizzle_create:failed\n");
00047     exit(1);
00048   }
00049 
00050   if (drizzle_con_create(&drizzle, &con) == NULL)
00051   {
00052     printf("drizzle_con_create:%s\n", drizzle_error(&drizzle));
00053     exit(1);
00054   }
00055 
00056   while ((c = getopt(argc, argv, "d:h:Hmp:P:u:")) != -1)
00057   {
00058     switch(c)
00059     {
00060     case 'd':
00061       drizzle_con_set_db(&con, optarg);
00062       break;
00063 
00064     case 'h':
00065       host= optarg;
00066       break;
00067 
00068     case 'm':
00069       drizzle_con_add_options(&con, DRIZZLE_CON_MYSQL);
00070       break;
00071 
00072     case 'p':
00073       password= optarg;
00074       break;
00075 
00076     case 'P':
00077       port= (in_port_t)atoi(optarg);
00078       break;
00079 
00080     case 'u':
00081       user= optarg;
00082       break;
00083 
00084     case 'H':
00085     default:
00086       printf("\nUsage: %s [options] [query]\n", argv[0]);
00087       printf("\t-d <db>       - Use <db> for the connection\n");
00088       printf("\t-h <host>     - Connect to <host>\n");
00089       printf("\t-H            - Print this help menu\n");
00090       printf("\t-m            - Use MySQL protocol\n");
00091       printf("\t-p <password> - Use <password> for authentication\n");
00092       printf("\t-P <port>     - Connect to <port>\n");
00093       printf("\t-u <user>     - Use <user> for authentication\n");
00094       exit(0);
00095     }
00096   }
00097 
00098   drizzle_con_set_tcp(&con, host, port);
00099   drizzle_con_set_auth(&con, user, password);
00100 
00101   do
00102   {
00103     if (read_size == -1)
00104     {
00105       printf("read:%d\n", errno);
00106       return 1;
00107     }
00108 
00109     buffer_size+= (size_t)read_size;
00110 
00111     buffer= realloc(buffer, buffer_size + BUFFER_CHUNK);
00112     if (buffer == NULL)
00113     {
00114       printf("realloc:%d\n", errno);
00115       return 1;
00116     }
00117 
00118     buffer_total= buffer_size + BUFFER_CHUNK;
00119   } while ((read_size= read(0, buffer + buffer_size, BUFFER_CHUNK)) != 0);
00120 
00121   (void)drizzle_query(&con, &result, buffer, buffer_size, &ret);
00122   if (ret != DRIZZLE_RETURN_OK)
00123   {
00124     printf("drizzle_query:%s\n", drizzle_error(&drizzle));
00125     return 1;
00126   }
00127 
00128   free(buffer);
00129 
00130   ret= drizzle_column_skip(&result);
00131   if (ret != DRIZZLE_RETURN_OK)
00132   {
00133     printf("drizzle_column_skip:%s\n", drizzle_error(&drizzle));
00134     return 1;
00135   }
00136 
00137   while (drizzle_row_read(&result, &ret) != 0 && ret == DRIZZLE_RETURN_OK)
00138   {
00139     while (1)
00140     {
00141       field= drizzle_field_read(&result, &offset, &size, &total, &ret);
00142       if (ret == DRIZZLE_RETURN_ROW_END)
00143         break;
00144       else if (ret != DRIZZLE_RETURN_OK)
00145       {
00146         printf("drizzle_field_read:%s\n", drizzle_error(&drizzle));
00147         return 1;
00148       }
00149 
00150       if (field == NULL)
00151         printf("NULL");
00152       else
00153         printf("%.*s", (int)size, field);
00154 
00155       if (offset + size == total)
00156         printf("\t");
00157     }
00158 
00159     printf("\n");
00160   }
00161 
00162   if (ret != DRIZZLE_RETURN_OK)
00163   {
00164     printf("drizzle_row_read:%s\n", drizzle_error(&drizzle));
00165     return 1;
00166   }
00167 
00168   drizzle_result_free(&result);
00169   drizzle_con_free(&con);
00170   drizzle_free(&drizzle);
00171 
00172   return 0;
00173 }