File: myx_grt_mysql.c

package info (click to toggle)
mysql-admin 1.2.5rc-2
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 80,944 kB
  • ctags: 43,103
  • sloc: sql: 295,916; pascal: 256,535; cpp: 74,487; ansic: 68,881; objc: 26,417; sh: 16,867; yacc: 10,755; java: 9,917; xml: 8,453; php: 2,806; python: 2,068; makefile: 1,252; perl: 3
file content (232 lines) | stat: -rw-r--r-- 7,912 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
/* Copyright (C) 2004 MySQL AB

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */


#include "myx_grt_mysql.h"

// --------------------------------------------------------------------------

/**
 ****************************************************************************
 * @brief Creates an error GRT value with the error from the given MYSQL struct
 *
 *   Creates a GRT value of type MYX_GRT_DICT, that contains a error and a detail
 * error message of the MySQL error.
 *
 * @param message the error message
 * @param mysql MYSQL struct that caused the error
 * 
 * @return  A newly created dict value struct containing the error information.
 *****************************************************************************/
MYX_GRT_VALUE *make_return_value_mysql_error(MYSQL *mysql, const char *message, const char *details)
{
  MYX_GRT_VALUE *value;
  char *mysql_error= g_strdup_printf("MySQL Error %d: %s", myx_mysql_errno(mysql), myx_mysql_error(mysql));
  //char *msg= NULL;

  if (!details)
    value= make_return_value_error(message, mysql_error);
  else
  {
    char *detail_str= g_strdup_printf("%s" _br _br "%s", details, mysql_error);

    value= make_return_value_error(message, detail_str);
    g_free(detail_str);
  }
  g_free(mysql_error);

  return value;
}

/**
 ****************************************************************************
 * @brief Creates an error GRT value with the error from the given MYSQL struct
 *
 *   Creates a GRT value of type MYX_GRT_DICT, that contains a error and a detail
 * error message of the MySQL error. Then closes the connection to the MySQL server
 *
 * @param message the error message
 * @param mysql MYSQL struct that caused the error
 * 
 * @return  A newly created dict value struct containing the error information.
 *****************************************************************************/
MYX_GRT_VALUE *make_return_value_mysql_error_and_close(MYSQL *mysql, const char *message, const char *sql)
{
  MYX_GRT_VALUE *value= make_return_value_mysql_error(mysql, message, sql);

  myx_mysql_close(mysql);

  return value;
}


/**
 ****************************************************************************
 * @brief Sets the parameters in a MYX_USER_CONNECTION struct
 *
 *   Sets the parameters in a MYX_USER_CONNECTION struct given in a GRT value.
 *
 * @param value the connection information stored in a GRT value
 * @param conn the connection struct
 *
 *****************************************************************************/
void get_connection_info(MYX_GRT_VALUE *value, MYX_USER_CONNECTION *conn)
{
  MYX_GRT_VALUE *port;
  const char *param;

  // if this is a "db.mgmt.Connection" struct, the parameters are in the parameterValues item
  if (strcmp2(myx_grt_dict_struct_get_name(value), "db.mgmt.Connection") == 0)
    value= myx_grt_dict_item_get_value(value, "parameterValues");

  param= myx_grt_dict_item_get_as_string(value, "username");
  if (param)
    conn->username= g_strdup(param);
  else
    conn->username= g_strdup("root");

  param= myx_grt_dict_item_get_as_string(value, "password");
  if (param)
    conn->password= g_strdup(param);
  else
    conn->password= NULL;

  param= myx_grt_dict_item_get_as_string(value, "host");

  if (param)
    conn->hostname= g_strdup(param);
  else
    conn->hostname= g_strdup("localhost");


  port= myx_grt_dict_item_get_value(value, "port");
  if (port)
  {
    if (myx_grt_value_get_type(port) == MYX_INT_VALUE)
      conn->port= myx_grt_value_as_int(port);
    else if (myx_grt_value_get_type(port) == MYX_STRING_VALUE)
      conn->port= atoi(myx_grt_value_as_string(port));
    else
      conn->port= 3306;
  }
  else
    conn->port= 3306;
}


/**
 ****************************************************************************
 * @brief Connects to a MySQL server
 *
 *   Connects to a MySQL server using the given connection parameters defined
 * in a GRT value.
 *
 * @param param the connection information stored in a GRT value
 * @param retval contains the error GRT value on failure, NULL on success
 * 
 * @return  Returns the MYSQL struct if successful
 *****************************************************************************/
MYSQL *grt_mysql_connect(MYX_GRT_VALUE *param, MYX_GRT_VALUE **retval)
{
  MYX_USER_CONNECTION conn;
  MYX_GRT_VALUE *value;
  MYSQL *mysql;

  memset(&conn, 0, sizeof(MYX_USER_CONNECTION));

  if ((myx_grt_value_get_type(param) == MYX_LIST_VALUE) && (myx_grt_list_item_count(param) == 1))
  {
    value= myx_grt_list_item_get(param, 0);
  }
  else if (myx_grt_value_get_type(param) == MYX_DICT_VALUE)
  {
    value= param;
  }
  else
  {
    *retval= make_return_value_error("Bad parameter submitted.", 
      "The connection parameters have to be submitted as list or dict.");
    return NULL;
  }

  get_connection_info(value, &conn);

  mysql= myx_mysql_init();
  if (!mysql)
  {
    *retval= make_return_value_error("Out of memory.", 
      "The memory for the MySQL connection information could not be created.");
    return NULL;
  }

  DBUG("connecting to mysql");

  if (myx_connect_to_instance(&conn, mysql) < 0)
  {
    *retval= make_return_value_mysql_error_and_close(mysql,
      "Can't connect to server. Please check the connection parameters.", NULL);
    return NULL;
  }

  *retval= NULL;

  return mysql;
}

/**
 ****************************************************************************
 * @brief Executes the given SQL statement and stores the result set
 *
 *   Executes the given SQL statement and stores the result set.
 *
 * @param mysql the connection information stored in a GRT value
 * @param sql the SQL command to execute.
 * @param error_msg the message text that will be returned in case of error
 * 
 * @return Returns the error GRT value on failure, NULL on success
 *****************************************************************************/
MYX_GRT_VALUE * grt_mysql_execute(MYSQL *mysql, MYSQL_RES **res, const char *sql, char *error_msg)
{
  MYX_GRT_VALUE *error= NULL;

  // execute SQL
  if (myx_mysql_query(mysql, sql) || !(*res= mysql_store_result(mysql)))
    error= make_return_value_mysql_error(mysql, error_msg, sql);

  return error;
}

/**
 ****************************************************************************
 * @brief Executes the given SQL statement and stores the result set
 *
 *   Executes the given SQL statement and stores the result set. The sql text
 * is freed after the execution.
 *
 * @param mysql the connection information stored in a GRT value
 * @param sql the SQL command to execute. Will be freed with g_free()
 * @param error_msg the message text that will be returned in case of error
 * 
 * @return Returns the error GRT value on failure, NULL on success
 *****************************************************************************/
MYX_GRT_VALUE * grt_mysql_execute_and_free(MYSQL *mysql, MYSQL_RES **res, char *sql, char *error_msg)
{
  MYX_GRT_VALUE *error= grt_mysql_execute(mysql, res, sql, error_msg);

  g_free(sql);

  return error;
}