Advertisement
bueddl

mysql dynamic binding for stmt

Aug 25th, 2016
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.06 KB | None | 0 0
  1.  
  2. int _mysql_schema_stmt_bind_params(MYSQL_STMT *stmt, MYSQL_BIND *params, int n,
  3.   const char *typestr, va_list args)
  4. {
  5.   int i;
  6.  
  7.   memset(params, 0, sizeof(MYSQL_BIND) * n);
  8.  
  9.   for (i = 0; i < n && *typestr; ++i)
  10.   {
  11.     switch (*typestr++)
  12.     {
  13.     case 'C':
  14.       params[i].is_unsigned = 1;
  15.     case 'c':
  16.       params[i].buffer_type = MYSQL_TYPE_TINY;
  17.       params[i].buffer = va_arg(args, uint8_t*);
  18.       params[i].buffer_length = sizeof(uint8_t);
  19.       break;
  20.  
  21.     case 'S':
  22.       params[i].is_unsigned = 1;
  23.     case 's':
  24.       params[i].buffer_type = MYSQL_TYPE_SHORT;
  25.       params[i].buffer = va_arg(args, uint8_t*);
  26.       params[i].buffer_length = sizeof(uint8_t);
  27.       break;
  28.  
  29.     case 'L':
  30.       params[i].is_unsigned = 1;
  31.     case 'l':
  32.       params[i].buffer_type = MYSQL_TYPE_LONG;
  33.       params[i].buffer = va_arg(args, uint32_t*);
  34.       params[i].buffer_length = sizeof(uint32_t);
  35.       break;
  36.  
  37.     case 'Q':
  38.       params[i].is_unsigned = 1;
  39.     case 'q':
  40.       params[i].buffer_type = MYSQL_TYPE_LONGLONG;
  41.       params[i].buffer = va_arg(args, uint64_t*);
  42.       params[i].buffer_length = sizeof(uint64_t);
  43.       break;
  44.  
  45.     case 'f':
  46.       params[i].buffer_type = MYSQL_TYPE_FLOAT;
  47.       params[i].buffer = va_arg(args, float*);
  48.       params[i].buffer_length = sizeof(float);
  49.       break;
  50.  
  51.     case 'd':
  52.       params[i].buffer_type = MYSQL_TYPE_DOUBLE;
  53.       params[i].buffer = va_arg(args, double*);
  54.       params[i].buffer_length = sizeof(double);
  55.       break;
  56.  
  57.     case 'A':
  58.       params[i].buffer_type = MYSQL_TYPE_STRING;
  59.       params[i].buffer_length = va_arg(args, size_t);
  60.       params[i].buffer = va_arg(args, char*);
  61.         break;    
  62.  
  63.     case 'a':
  64.       params[i].buffer_type = MYSQL_TYPE_STRING;
  65.       params[i].buffer = va_arg(args, char*);
  66.       params[i].buffer_length = strlen(params[i].buffer);
  67.       break;
  68.  
  69.     case 'x':
  70.       params[i].buffer_type = MYSQL_TYPE_NULL;
  71.       break;
  72.  
  73.     default:
  74.       return 0;
  75.     }
  76.   }
  77.  
  78.   if (!mysql_stmt_bind_param(stmt, params))
  79.     return 0;
  80.  
  81.   return 1;
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement