"Fossies" - the Fresh Open Source Software Archive

Member "gmysqlcc-0.3.0/src/gmlc_mysql_query.c" (15 Feb 2008, 24191 Bytes) of package /linux/privat/old/gmysqlcc-0.3.0.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 /*
    2  *  This program is free software; you can redistribute it and/or modify
    3  *  it under the terms of the GNU General Public License as published by
    4  *  the Free Software Foundation; either version 2 of the License, or
    5  *  (at your option) any later version.
    6  *
    7  *  This program is distributed in the hope that it will be useful,
    8  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
    9  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   10  *  GNU General Public License for more details.
   11  *
   12  *  You should have received a copy of the GNU General Public License
   13  *  along with this program; if not, write to the Free Software
   14  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
   15  */
   16 
   17 #include "gmlc_mysql_query.h"
   18 
   19 #include <stdlib.h>
   20 #include <string.h>
   21 
   22 static void gmlc_mysql_query_finalize (GmlcMysqlQuery * pGmlcMysqlQry);
   23 static void gmlc_mysql_query_set_property (GObject * object, guint prop_id, const GValue * value, GParamSpec * pspec);
   24 static void gmlc_mysql_query_get_property (GObject * object, guint prop_id, GValue * value, GParamSpec * pspec);
   25 
   26 static gboolean gmlc_mysql_query_connect(GmlcMysqlQuery * pGmlcMysqlQry);
   27 
   28 void gmlc_mysql_query_read_error(GmlcMysqlQuery * pGmlcMysqlQry, gboolean bClearOnly);
   29 
   30 
   31 enum {
   32     PROP_0,
   33     PROP_RAW_HEADERS,
   34     PROP_SERVER,
   35     PROP_QUERY,
   36     PROP_SRV_CHARSET,
   37     PROP_DB_NAME,
   38     PROP_ERR_MSG,
   39     PROP_ABS_TABLE_NAME,
   40     PROP_TABLE_NAME,
   41     PROP_NB_FIELD,
   42     PROP_EDIT_RESULT,
   43     PROP_ERR_CODE,
   44     PROP_CAN_EDIT
   45 };
   46 
   47 G_DEFINE_TYPE (GmlcMysqlQuery, gmlc_mysql_query, G_TYPE_OBJECT);
   48 
   49 gboolean gmlc_tools_query_is_read_query(const gchar * pcQuery, gsize szQuery) {
   50     GString * pstrWord = NULL;
   51     gchar * pcQueryUp = NULL, * pcTmp = NULL, * pcPrevTmp = NULL;
   52     gunichar ucChar = 0;
   53     gsize szPos = 0;
   54     gboolean bReturn = TRUE;
   55     gboolean bEndLoop, bFirstWord, bInString;
   56     
   57     pcQueryUp = g_utf8_strup(pcQuery, szQuery);
   58     
   59     if (pcQueryUp == NULL) {
   60         return FALSE;
   61     }
   62     
   63     if (!g_utf8_validate(pcQueryUp, szQuery, NULL)) {
   64         return FALSE;
   65     }
   66     
   67     /*g_print("Test query (%d) : '%s'\n", szQuery, pcQueryUp);*/
   68     
   69     pcTmp = pcQueryUp;
   70     pstrWord = g_string_sized_new(16);
   71     bFirstWord = TRUE;
   72     bEndLoop = FALSE;
   73     bInString = FALSE;
   74     szPos = 0;
   75     
   76     while (!bEndLoop && szPos < szQuery) {
   77         ucChar = g_utf8_get_char(pcTmp);
   78         
   79         /*g_print("Current (%d/%d) '%c' : '%s'\n", szPos, szQuery, *pcTmp, pcTmp);*/
   80         
   81         if (bFirstWord) {
   82             if (!g_unichar_isspace(ucChar)) {
   83                 g_string_append_unichar(pstrWord, ucChar);
   84             } else if (g_unichar_isspace(ucChar) && pstrWord->len == 0) {
   85                 /* Do nothing ! */
   86             } else { /* Check word */
   87                 gboolean bReadCmd = FALSE;
   88                 
   89                 /*g_print("**** Checked first word : '%s' : ", pstrWord->str);*/
   90                 
   91                 if (g_ascii_strncasecmp(pstrWord->str, "SELECT", 6) == 0) {
   92                     bReadCmd = TRUE;
   93                 } else if (g_ascii_strncasecmp(pstrWord->str, "SHOW", 4) == 0) {
   94                     bReadCmd = TRUE;
   95                 } else if (g_ascii_strncasecmp(pstrWord->str, "EXPLAIN", 7) == 0) {
   96                     bReadCmd = TRUE;
   97                 } else if (g_ascii_strncasecmp(pstrWord->str, "DESCRIBE", 8) == 0) {
   98                     bReadCmd = TRUE;
   99                 } else if (g_ascii_strncasecmp(pstrWord->str, "#", 1) == 0) { /* Comments */
  100                     bReadCmd = TRUE;
  101                     while (*pcTmp != '\n') {
  102                         pcTmp = g_utf8_next_char(pcTmp);
  103                         szPos ++;
  104                     }
  105                 }
  106                 
  107                 if (!bReadCmd) {
  108                     bReturn = FALSE;
  109                     bEndLoop = TRUE;
  110                 }
  111                 
  112                 /*g_print("- Word status : %s - Global status : %s ****\n", ((bReadCmd) ? "True" : "False"), ((bReturn) ? "True" : "False"));*/
  113                 
  114                 bFirstWord = FALSE;
  115             }
  116         } else if ((*pcTmp) == ';' && !bInString) {
  117             g_string_set_size(pstrWord, 0);
  118             bFirstWord = TRUE;
  119         } else if (((*pcTmp) == '\'' || (*pcTmp) == '"') && (*pcPrevTmp) != '\\') {
  120             bInString = !bInString;
  121             /*g_print("In a string : %s\n", ((bInString) ? "True" : "False"));*/
  122         }
  123         
  124         pcPrevTmp = pcTmp;
  125         pcTmp = g_utf8_next_char(pcTmp);
  126         szPos ++;
  127     }
  128     
  129     /*g_print("Returned value : %s\n", ((bReturn) ? "True" : "False"));*/
  130     
  131     g_free (pcQueryUp);
  132     return bReturn;
  133 }
  134 
  135 gchar * gmlc_tools_query_get_one_result(MYSQL * pMysqlLink, const gchar * pcCharset, const gchar * pcQuery, const gint iIdxField) {
  136     MYSQL_RES * pMysqlResult = NULL;
  137     MYSQL_ROW pCurrRow = NULL;
  138     GError * oErr = NULL;
  139     gulong * arlLengths = NULL;
  140     gchar * pcRet = NULL, * pcConvQuery = NULL;
  141     gint iErrCode = 0, iNbrField = 0;
  142     
  143     if (pMysqlLink == NULL) {
  144         return NULL;
  145     }
  146     
  147     pcConvQuery = g_convert(pcQuery, -1, pcCharset, "UTF-8", NULL, NULL, &oErr);
  148     if (pcConvQuery == NULL && oErr != NULL) {
  149         g_printerr("gmlc_tools_query_get_one_result - g_convert - Failed to convert query\nQuery : '%s'\nError: (%d) %s\n", pcQuery, oErr->code, oErr->message);
  150     }
  151     
  152     iErrCode = mysql_real_query(pMysqlLink, pcConvQuery, strlen(pcConvQuery));
  153     
  154     if (iErrCode != 0) {
  155         g_printerr("gmlc_tools_query_get_one_result - mysql_real_query - Failed to query\nQuery : '%s'\nError: (%d - %d) %s\n", pcQuery, iErrCode, mysql_errno(pMysqlLink), mysql_error(pMysqlLink));
  156         return NULL;
  157     }
  158     
  159     iNbrField = mysql_field_count(pMysqlLink);
  160     
  161     if (iErrCode != 0 && iNbrField < 0) {
  162         g_printerr("gmlc_tools_query_get_one_result - mysql_field_count - Failed to query\nQuery : '%s'\nError: (%d - %d) %s\n", pcQuery, iErrCode, mysql_errno(pMysqlLink), mysql_error(pMysqlLink));
  163         return NULL;
  164     } else if (iErrCode == 0 && iNbrField == 0) {
  165         /*mysql_qry->editResult = mysql_affected_rows(mysql_qry->mysql_link);*/
  166         return NULL;
  167     }
  168     
  169     pMysqlResult = mysql_use_result(pMysqlLink);
  170     
  171     if (pMysqlResult == (MYSQL_RES *)NULL) {
  172         g_printerr("gmlc_tools_query_get_one_result - mysql_use_result - Failed to use results\nQuery : '%s'\nError: (%d) %s\n", pcQuery, mysql_errno(pMysqlLink), mysql_error(pMysqlLink));
  173         return NULL;
  174     }
  175     
  176     pCurrRow = mysql_fetch_row(pMysqlResult);
  177     if (pCurrRow == NULL) {
  178         g_printerr("gmlc_tools_query_get_one_result - mysql_fetch_row - Failed to use results\nQuery : '%s'\nError: (%d) %s\n", pcQuery, mysql_errno(pMysqlLink), mysql_error(pMysqlLink));
  179         mysql_free_result(pMysqlResult);
  180         return NULL;
  181     }
  182     arlLengths = mysql_fetch_lengths(pMysqlResult);
  183     
  184     pcRet = g_convert(pCurrRow[iIdxField], arlLengths[iIdxField], "UTF-8", pcCharset, NULL, NULL, &oErr);
  185     
  186     if (pcRet == NULL && oErr != NULL) {
  187         g_printerr("gmlc_tools_query_get_one_result - g_convert - Failed to convert result \nQuery : '%s'\nError: (%d) %s\n", pcQuery, oErr->code, oErr->message);
  188     }
  189     
  190     mysql_free_result(pMysqlResult);
  191     
  192     return pcRet;
  193 }
  194 
  195 static void gmlc_mysql_query_class_init (GmlcMysqlQueryClass * pClass) {
  196     GObjectClass *pObjClass = G_OBJECT_CLASS(pClass);
  197     
  198     pObjClass->finalize = (GObjectFinalizeFunc) gmlc_mysql_query_finalize;
  199     
  200     pObjClass->get_property = gmlc_mysql_query_get_property;
  201     pObjClass->set_property = gmlc_mysql_query_set_property;
  202     
  203     g_object_class_install_property(pObjClass, PROP_SERVER, 
  204         g_param_spec_object("server", "Server object", "Server object", GMLC_MYSQL_TYPE_SERVER, G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
  205     g_object_class_install_property(pObjClass, PROP_QUERY, 
  206         g_param_spec_string("query", "Query string", "Sql Query string", "", G_PARAM_READABLE));
  207     g_object_class_install_property(pObjClass, PROP_SRV_CHARSET, 
  208         g_param_spec_string("srv_charset", "Server charset", "Charset used on server", "", G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
  209     g_object_class_install_property(pObjClass, PROP_DB_NAME, 
  210         g_param_spec_string("db_name", "Database name", "Name of the database", "", G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
  211     g_object_class_install_property(pObjClass, PROP_ERR_MSG, 
  212         g_param_spec_string("err_msg", "Error message", "Error message", "", G_PARAM_READABLE));
  213     g_object_class_install_property(pObjClass, PROP_ERR_CODE, 
  214         g_param_spec_int("err_code", "Error code", "Munerical code of error", 0, 65535, 0, G_PARAM_READABLE));
  215     
  216 }
  217 
  218 static void gmlc_mysql_query_init (GmlcMysqlQuery * pGmlcMysqlQry) {
  219     pGmlcMysqlQry->pGmlcMysqlSrv = NULL;/* r-c */
  220     pGmlcMysqlQry->pMysqlLink = NULL;   /* --- */
  221     pGmlcMysqlQry->pMysqlResult = NULL; /* --- */
  222     pGmlcMysqlQry->pcQuery = NULL;      /* r-- */
  223     pGmlcMysqlQry->pcDbName = NULL;     /* r-c */
  224     pGmlcMysqlQry->pcErrMsg = NULL;     /* r-- */
  225     pGmlcMysqlQry->iErrCode = 0;        /* r-- */
  226     pGmlcMysqlQry->iEditResult = 0;     /* r-- */
  227     pGmlcMysqlQry->bNoRecord = FALSE;   /* r-- */
  228     pGmlcMysqlQry->lVersion = 0;        /* r-- */
  229     pGmlcMysqlQry->pcSrvCharset = NULL; /* r-c */
  230     pGmlcMysqlQry->pMysqlHeaders = NULL; /* --- */
  231     pGmlcMysqlQry->arMysqlHeaders = NULL;
  232 }
  233 
  234 static void gmlc_mysql_query_finalize (GmlcMysqlQuery * pGmlcMysqlQry) {
  235     
  236     /* Close DB connection */
  237     if (pGmlcMysqlQry->pMysqlLink != NULL) {
  238         mysql_close(pGmlcMysqlQry->pMysqlLink);
  239     }
  240     
  241     /* Release pointers */
  242     gmlc_mysql_query_read_error(pGmlcMysqlQry, TRUE);
  243     
  244     g_free(pGmlcMysqlQry->pcDbName);
  245     g_free(pGmlcMysqlQry->pcSrvCharset);
  246     g_free(pGmlcMysqlQry->pcQuery);
  247 }
  248 
  249 static void gmlc_mysql_query_set_property (GObject * object, guint prop_id, const GValue * value, GParamSpec * pspec) {
  250     GmlcMysqlQuery * pGmlcMysqlQry = GMLC_MYSQL_QUERY(object);
  251     
  252     switch (prop_id) {
  253         case PROP_SERVER :
  254             pGmlcMysqlQry->pGmlcMysqlSrv = g_value_get_object(value);
  255             break;
  256         case PROP_SRV_CHARSET:
  257             /*g_free(pGmlcMysqlQry->pcSrvCharset);
  258             
  259             pGmlcMysqlQry->pcSrvCharset = g_value_dup_string(value);*/
  260             break;
  261         case PROP_DB_NAME :
  262             g_free(pGmlcMysqlQry->pcDbName);
  263             
  264             pGmlcMysqlQry->pcDbName = g_value_dup_string(value);
  265             break;
  266         default:
  267             G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
  268             break;
  269     }
  270 }
  271 
  272 static void gmlc_mysql_query_get_property (GObject * object, guint prop_id, GValue * value, GParamSpec * pspec) {
  273     GmlcMysqlQuery * pGmlcMysqlQry = GMLC_MYSQL_QUERY(object);
  274     UNUSED_VAR(pGmlcMysqlQry);
  275     
  276     switch (prop_id) {
  277     default:
  278             G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
  279             break;
  280     }
  281 }
  282 
  283 GmlcMysqlQuery * gmlc_mysql_query_new (GmlcMysqlServer * pGmlcMysqlSrv, const gchar * pcDbName) {
  284     GmlcMysqlQuery * pGmlcMysqlQry = NULL;
  285     
  286     pGmlcMysqlQry = GMLC_MYSQL_QUERY(g_object_new (GMLC_MYSQL_TYPE_QUERY, "server", GMLC_MYSQL_SERVER(pGmlcMysqlSrv), "db_name", pcDbName, NULL));
  287     
  288     gmlc_mysql_query_get_version(pGmlcMysqlQry);
  289     //gmlc_mysql_query_get_current_charset(pGmlcMysqlQry);
  290     
  291     return pGmlcMysqlQry;
  292 }
  293 
  294 gchar * gmlc_mysql_query_static_get_one_result(GmlcMysqlServer * pGmlcMysqlSrv, const gchar * pcDbName, const gchar * pcQuery, const gint iIdxField) {
  295     GmlcMysqlQuery * pGmlcMysqlQry = NULL;
  296     gchar * pcRetValue = NULL;
  297     
  298     pGmlcMysqlQry = gmlc_mysql_query_new(pGmlcMysqlSrv, pcDbName);
  299     
  300     g_return_val_if_fail(pGmlcMysqlQry != NULL, NULL);
  301     
  302     if (pGmlcMysqlQry->pMysqlLink == NULL) {
  303         gmlc_mysql_query_connect(pGmlcMysqlQry);
  304     }
  305     
  306     g_return_val_if_fail(pGmlcMysqlQry->pMysqlLink != NULL, NULL);
  307     
  308     pcRetValue = gmlc_tools_query_get_one_result(pGmlcMysqlQry->pMysqlLink, pGmlcMysqlQry->pcSrvCharset, pcQuery, iIdxField);
  309     
  310     g_object_unref(pGmlcMysqlQry);
  311     
  312     return pcRetValue;
  313 }
  314 
  315 static gboolean gmlc_mysql_query_connect(GmlcMysqlQuery * pGmlcMysqlQry) {
  316     gint iRet = 0;
  317     
  318     g_return_val_if_fail(pGmlcMysqlQry != NULL, FALSE);
  319     
  320     if (pGmlcMysqlQry->pMysqlLink != NULL) {
  321         return TRUE;
  322     }
  323     
  324     pGmlcMysqlQry->pMysqlLink = mysql_init(NULL);
  325     if (pGmlcMysqlQry->pMysqlLink == NULL) {
  326         gmlc_mysql_query_read_error(pGmlcMysqlQry, TRUE);
  327         pGmlcMysqlQry->iErrCode = -1;
  328         pGmlcMysqlQry->pcErrMsg = g_strdup("**** Can't prepare mysql connection ****");
  329         g_printerr("Failed to connect to database: Error (%d) : %s\n", pGmlcMysqlQry->iErrCode, pGmlcMysqlQry->pcErrMsg);
  330         return FALSE;
  331     }
  332 
  333     if (!mysql_real_connect(pGmlcMysqlQry->pMysqlLink, pGmlcMysqlQry->pGmlcMysqlSrv->pcHost, 
  334             pGmlcMysqlQry->pGmlcMysqlSrv->pcLogin, pGmlcMysqlQry->pGmlcMysqlSrv->pcPassword, 
  335             pGmlcMysqlQry->pcDbName, pGmlcMysqlQry->pGmlcMysqlSrv->iPort, 
  336             pGmlcMysqlQry->pGmlcMysqlSrv->pcLocalSock, CLIENT_MULTI_STATEMENTS)
  337             ) {
  338         gmlc_mysql_query_read_error(pGmlcMysqlQry, FALSE);
  339         pGmlcMysqlQry->pMysqlLink = NULL;
  340         g_printerr("Failed to connect to database: Error (%d) : %s\n", pGmlcMysqlQry->iErrCode, pGmlcMysqlQry->pcErrMsg);
  341         return FALSE;
  342     }
  343     
  344     pGmlcMysqlQry->pcSrvCharset = NULL;
  345     iRet = mysql_set_character_set(pGmlcMysqlQry->pMysqlLink, "utf8");
  346     if (iRet != 0) {
  347         g_printerr("Failed to set the connection's caracter set to 'utf8' - '%d'\n", iRet);
  348         gmlc_mysql_query_get_current_charset(pGmlcMysqlQry);
  349     } else {
  350         MY_CHARSET_INFO cs;
  351         mysql_get_character_set_info(pGmlcMysqlQry->pMysqlLink, &cs);
  352         g_print("collation name: %s\n", cs.csname);
  353         pGmlcMysqlQry->pcSrvCharset = g_strdup(cs.csname);
  354     }
  355     
  356     g_print("Charset : '%s'\n", pGmlcMysqlQry->pcSrvCharset);
  357     
  358     return TRUE;
  359 }
  360 
  361 gboolean gmlc_mysql_query_execute(GmlcMysqlQuery * pGmlcMysqlQry, const gchar * pcQuery, gsize szQuery, gboolean bForceWrite) {
  362     GError * oErr = NULL;
  363     gchar * pcTmp = NULL;
  364     gsize szTmp = 0;
  365     gint iErrCode = 0;
  366     gboolean bIsReadQuery = FALSE;
  367     
  368     g_return_val_if_fail(pGmlcMysqlQry != NULL, FALSE);
  369     
  370     if (pGmlcMysqlQry->pcSrvCharset != NULL) {
  371         pcTmp = g_convert(pcQuery, szQuery, pGmlcMysqlQry->pcSrvCharset, "UTF-8", NULL, &szTmp, &oErr);
  372         if (pcTmp == NULL && oErr != NULL) {
  373             g_printerr("gmlc_mysql_query_execute - g_convert - Failed to convert query\nQuery : '%s'\nError: (%d) %s\n", pcQuery, oErr->code, oErr->message);
  374             return FALSE;
  375         }
  376     } else {
  377         pcTmp = g_strndup(pcQuery, szQuery);
  378         szTmp = szQuery;
  379     }
  380     
  381     g_free(pGmlcMysqlQry->pcQuery);
  382     pGmlcMysqlQry->pcQuery = pcTmp;
  383     pGmlcMysqlQry->szQuery = szTmp;
  384     
  385     
  386     gmlc_mysql_query_connect(pGmlcMysqlQry);
  387 
  388     if (pGmlcMysqlQry->pMysqlLink == NULL) {
  389         return FALSE;
  390     }
  391     
  392     bIsReadQuery = gmlc_tools_query_is_read_query(pGmlcMysqlQry->pcQuery, pGmlcMysqlQry->szQuery);
  393     
  394     if (pGmlcMysqlQry->pGmlcMysqlSrv->bReadOnly && !bIsReadQuery) {
  395         gmlc_mysql_query_read_error(pGmlcMysqlQry, TRUE);
  396         pGmlcMysqlQry->iErrCode = -1000;
  397         pGmlcMysqlQry->pcErrMsg = g_strdup("gmysqlcc : Server in Read-only mode");
  398         return FALSE;
  399     }
  400     
  401     if (pGmlcMysqlQry->pGmlcMysqlSrv->bWriteWarning && !bIsReadQuery) {
  402         if (!bForceWrite) {
  403             gmlc_mysql_query_read_error(pGmlcMysqlQry, TRUE);
  404             pGmlcMysqlQry->iErrCode = -1001;
  405             pGmlcMysqlQry->pcErrMsg = g_strdup("gmysqlcc : Server in write warning mode");
  406             return FALSE;
  407         }
  408     }
  409     
  410     iErrCode = mysql_real_query(pGmlcMysqlQry->pMysqlLink, pcQuery, szQuery);
  411     
  412     if (iErrCode != 0) {
  413         gmlc_mysql_query_read_error(pGmlcMysqlQry, FALSE);
  414         g_printerr("gmlc_mysql_query_execute - mysql_real_query - Failed to query\nQuery : '%s'\nError: (%d - %d) %s\n", pGmlcMysqlQry->pcQuery, iErrCode, pGmlcMysqlQry->iErrCode, pGmlcMysqlQry->pcErrMsg);
  415         return FALSE;
  416     }
  417     
  418     pGmlcMysqlQry->iNbField = mysql_field_count(pGmlcMysqlQry->pMysqlLink);
  419 
  420     if (iErrCode != 0 && pGmlcMysqlQry->iNbField < 0) {
  421         gmlc_mysql_query_read_error(pGmlcMysqlQry, FALSE);
  422         g_printerr("gmlc_mysql_query_execute - mysql_field_count - Failed to query : '%s'\nError: (%d - %d) %s\n", pGmlcMysqlQry->pcQuery, iErrCode, pGmlcMysqlQry->iErrCode, pGmlcMysqlQry->pcErrMsg);
  423         return FALSE;
  424     } else if (iErrCode == 0 && pGmlcMysqlQry->iNbField == 0) {
  425         pGmlcMysqlQry->iEditResult = mysql_affected_rows(pGmlcMysqlQry->pMysqlLink);
  426         pGmlcMysqlQry->bNoRecord = TRUE;
  427         gmlc_mysql_query_read_error(pGmlcMysqlQry, TRUE);
  428         return TRUE;
  429     }
  430     
  431     return TRUE;
  432 }
  433 
  434 gboolean gmlc_mysql_query_have_record(GmlcMysqlQuery * pGmlcMysqlQry) {
  435     
  436     g_return_val_if_fail(pGmlcMysqlQry != NULL, FALSE);
  437     
  438     return (!pGmlcMysqlQry->bNoRecord);
  439 }
  440 
  441 GArray * gmlc_mysql_query_next_record(GmlcMysqlQuery * pGmlcMysqlQry) {
  442     GArray * arRecordValue = NULL;
  443     GError * oErr = NULL;
  444     MYSQL_ROW pCurrRow;
  445     gchar * pcConverted = NULL;
  446     gulong * arlSizes = NULL;
  447     gsize szConverted = 0;
  448     int i = 0;
  449         
  450     g_return_val_if_fail(pGmlcMysqlQry != NULL, FALSE);
  451     
  452     if (pGmlcMysqlQry->pMysqlResult == NULL) {
  453         if (pGmlcMysqlQry->bNoRecord) {
  454             gmlc_mysql_query_read_error(pGmlcMysqlQry, TRUE);
  455             return NULL;
  456         }
  457         
  458         pGmlcMysqlQry->pMysqlResult = mysql_use_result(pGmlcMysqlQry->pMysqlLink);
  459         
  460         if (pGmlcMysqlQry->pMysqlResult == NULL) {
  461             gmlc_mysql_query_read_error(pGmlcMysqlQry, FALSE);
  462             g_printerr("gmlc_mysql_query_next_record - mysql_use_result - Failed\nQuery : '%s'\nError: (%d) %s\n", pGmlcMysqlQry->pcQuery, pGmlcMysqlQry->iErrCode, pGmlcMysqlQry->pcErrMsg);
  463             return NULL;
  464         }
  465         
  466         pGmlcMysqlQry->pMysqlHeaders = mysql_fetch_fields(pGmlcMysqlQry->pMysqlResult);
  467         if (pGmlcMysqlQry->pMysqlHeaders == NULL) {
  468             gmlc_mysql_query_read_error(pGmlcMysqlQry, FALSE);
  469             g_printerr("gmlc_mysql_query_execute - mysql_fetch_fields - Failed to query : '%s'\nError: (%d) %s\n", pGmlcMysqlQry->pcQuery, pGmlcMysqlQry->iErrCode, pGmlcMysqlQry->pcErrMsg);
  470         }
  471         
  472         pGmlcMysqlQry->bNoRecord = FALSE;
  473     }
  474     
  475     pCurrRow = mysql_fetch_row(pGmlcMysqlQry->pMysqlResult);
  476     if (pCurrRow == NULL) {
  477         gmlc_mysql_query_read_error(pGmlcMysqlQry, FALSE);
  478         if (pGmlcMysqlQry->iErrCode != 0) {
  479             g_printerr("gmlc_mysql_query_next_record - mysql_fetch_row - Failed\nQuery : '%s'\nError: (%d) %s\n", pGmlcMysqlQry->pcQuery, pGmlcMysqlQry->iErrCode, pGmlcMysqlQry->pcErrMsg);
  480         }
  481         return NULL;
  482     }
  483     
  484     arlSizes = mysql_fetch_lengths(pGmlcMysqlQry->pMysqlResult);
  485     if (pCurrRow == NULL) {
  486         gmlc_mysql_query_read_error(pGmlcMysqlQry, FALSE);
  487         g_printerr("gmlc_mysql_query_next_record - mysql_fetch_lengths - Failed\nQuery : '%s'\nError: (%d) %s\n", pGmlcMysqlQry->pcQuery, pGmlcMysqlQry->iErrCode, pGmlcMysqlQry->pcErrMsg);
  488         return NULL;
  489     }
  490     
  491     arRecordValue = g_array_sized_new (FALSE, TRUE, sizeof (gchar *), pGmlcMysqlQry->iNbField);
  492     
  493     for(i = 0; i < pGmlcMysqlQry->iNbField; i++) {
  494         if (pCurrRow[i] != NULL) {
  495             pcConverted = NULL;
  496             if (pGmlcMysqlQry->pcSrvCharset != NULL && pGmlcMysqlQry->pMysqlHeaders[i].charsetnr != 63) {
  497                 pcConverted = g_convert(pCurrRow[i], arlSizes[i], "utf8", pGmlcMysqlQry->pcSrvCharset, NULL, &szConverted, &oErr);
  498                 g_print("Convert (%d - %s -> utf8) : '%s' -> '%s'\n", pGmlcMysqlQry->pMysqlHeaders[i].charsetnr, pGmlcMysqlQry->pcSrvCharset, pCurrRow[i], pcConverted);
  499                 if (pcConverted == NULL) {
  500                     pcConverted = g_strdup("Binary BLOB");
  501                 }
  502             } else {
  503                 pcConverted = g_strndup(pCurrRow[i], arlSizes[i]);
  504                 g_print("NO Convert (%d) : '%s' -> '%s'\n", pGmlcMysqlQry->pMysqlHeaders[i].charsetnr, pCurrRow[i], pcConverted);
  505             }
  506             
  507         } else {
  508             pcConverted = g_strdup("NULL");
  509         }
  510         
  511         g_array_append_val (arRecordValue, pcConverted);
  512     }
  513     
  514     return arRecordValue;
  515 }
  516 
  517 void gmlc_mysql_query_free_record_content(GArray * arRow) {
  518     gint i;
  519     
  520     for (i = 0; i < arRow->len; i++) {
  521         g_free(g_array_index(arRow, gchar *, i));
  522     }
  523     
  524     g_array_free(arRow, TRUE);
  525 }
  526 
  527 GArray * gmlc_mysql_query_get_headers(GmlcMysqlQuery * pGmlcMysqlQry) {
  528     MYSQL_FIELD * pField = NULL;
  529     GArray * arHeaders = NULL;
  530     GError * oErr = NULL;
  531     gchar * pcTmp = NULL;
  532     gsize szTmp = 0;
  533     int i = 0;
  534     
  535     g_return_val_if_fail(pGmlcMysqlQry != NULL, NULL);
  536     
  537     if (pGmlcMysqlQry->iNbField == 0) {
  538         return NULL;
  539     }
  540     
  541     if (pGmlcMysqlQry->pMysqlHeaders == NULL) {
  542         return NULL;
  543     }
  544     
  545     if (pGmlcMysqlQry->arMysqlHeaders == NULL) {
  546         pGmlcMysqlQry->arMysqlHeaders = g_array_sized_new (FALSE, TRUE, sizeof (char *), pGmlcMysqlQry->iNbField);
  547         
  548         for(i = 0; i < pGmlcMysqlQry->iNbField; i++) {
  549             pField = &pGmlcMysqlQry->pMysqlHeaders[i];
  550             if (pGmlcMysqlQry->pcSrvCharset != NULL) {
  551                 pcTmp = g_convert(pField->name, strlen(pField->name), "UTF-8", pGmlcMysqlQry->pcSrvCharset, NULL, &szTmp, &oErr);
  552                 if (pcTmp == NULL && oErr != NULL) {
  553                     g_printerr("gmlc_mysql_query_get_headers - g_convert - Failed to convert header\nHeader : '%s'\nError: (%d) %s\n", pField->name, oErr->code, oErr->message);
  554                     pcTmp = g_strdup(pField->name);
  555                 }
  556             } else {
  557                 pcTmp = g_strdup(pField->name);
  558             }
  559             
  560             g_array_append_val (pGmlcMysqlQry->arMysqlHeaders, pcTmp);
  561         }
  562     }
  563     
  564     arHeaders = g_array_sized_new (FALSE, TRUE, sizeof (char *), pGmlcMysqlQry->iNbField);
  565     
  566     for(i = 0; i < pGmlcMysqlQry->arMysqlHeaders->len; i++) {
  567         pcTmp = g_array_index(pGmlcMysqlQry->arMysqlHeaders, gchar *, i);
  568         g_array_append_val (arHeaders, pcTmp);
  569     }
  570     
  571     return arHeaders;
  572 }
  573 
  574 gboolean gmlc_mysql_query_have_more_result(GmlcMysqlQuery * pGmlcMysqlQry) {
  575     
  576     g_return_val_if_fail(pGmlcMysqlQry != NULL, FALSE);
  577     
  578     return mysql_more_results(pGmlcMysqlQry->pMysqlLink);
  579 }
  580 
  581 gboolean gmlc_mysql_query_goto_next_result(GmlcMysqlQuery * pGmlcMysqlQry) {
  582     gint iNextRes = 0;
  583     
  584     g_return_val_if_fail(pGmlcMysqlQry != NULL, FALSE);
  585     
  586     gmlc_mysql_query_free_result(pGmlcMysqlQry);
  587     
  588     iNextRes = mysql_next_result(pGmlcMysqlQry->pMysqlLink);
  589     
  590     if (iNextRes < 0) {
  591         pGmlcMysqlQry->iEditResult = 0;
  592         pGmlcMysqlQry->bNoRecord = TRUE;
  593         gmlc_mysql_query_read_error(pGmlcMysqlQry, TRUE);
  594         return FALSE;
  595     }
  596     
  597     if (iNextRes > 0) {
  598         gmlc_mysql_query_read_error(pGmlcMysqlQry, FALSE);
  599         g_printerr("gmlc_mysql_query_goto_next_result - mysql_next_result - Failed\nQuery : '%s'\nError: (%d) %s\n", pGmlcMysqlQry->pcQuery, pGmlcMysqlQry->iErrCode, pGmlcMysqlQry->pcErrMsg);
  600         return FALSE;
  601     }
  602 
  603     pGmlcMysqlQry->iNbField = mysql_field_count(pGmlcMysqlQry->pMysqlLink);
  604 
  605     if (iNextRes > 0 && pGmlcMysqlQry->iNbField < 0) {
  606         gmlc_mysql_query_read_error(pGmlcMysqlQry, FALSE);
  607         g_printerr("gmlc_mysql_query_goto_next_result - mysql_field_count - Failed to query : '%s'\nError: (%d - %d) %s\n", pGmlcMysqlQry->pcQuery, iNextRes, pGmlcMysqlQry->iErrCode, pGmlcMysqlQry->pcErrMsg);
  608         return FALSE;
  609     } else if (iNextRes == 0 && pGmlcMysqlQry->iNbField == 0) {
  610         pGmlcMysqlQry->iEditResult = mysql_affected_rows(pGmlcMysqlQry->pMysqlLink);
  611         pGmlcMysqlQry->bNoRecord = TRUE;
  612         gmlc_mysql_query_read_error(pGmlcMysqlQry, TRUE);
  613         return TRUE;
  614     }
  615     
  616     return (iNextRes == 0) ? TRUE : FALSE ;
  617 }
  618 
  619 gboolean gmlc_mysql_query_free_result (GmlcMysqlQuery * pGmlcMysqlQry) {
  620     gchar * pcTmp = NULL;
  621     gint i = 0;
  622     
  623     g_return_val_if_fail(pGmlcMysqlQry != NULL, FALSE);
  624     
  625     if (pGmlcMysqlQry->pMysqlResult != NULL || (pGmlcMysqlQry->pMysqlResult == NULL && pGmlcMysqlQry->bNoRecord == TRUE)) {
  626         mysql_free_result(pGmlcMysqlQry->pMysqlResult);
  627         
  628         if (pGmlcMysqlQry->arMysqlHeaders != NULL) {
  629             for (i = 0; i < pGmlcMysqlQry->arMysqlHeaders->len; i ++) {
  630                 pcTmp = g_array_index(pGmlcMysqlQry->arMysqlHeaders, gchar *, i);
  631                 g_free(pcTmp);
  632             }
  633             g_array_free(pGmlcMysqlQry->arMysqlHeaders, TRUE);
  634         }
  635         
  636         pGmlcMysqlQry->pMysqlResult = NULL;
  637         pGmlcMysqlQry->pMysqlHeaders = NULL;
  638         pGmlcMysqlQry->arMysqlHeaders = NULL;
  639         pGmlcMysqlQry->iNbField = 0;
  640         pGmlcMysqlQry->bNoRecord = FALSE;
  641         pGmlcMysqlQry->iEditResult = 0;
  642         gmlc_mysql_query_read_error(pGmlcMysqlQry, TRUE);
  643     }
  644     
  645     return TRUE;
  646 }
  647 
  648 gulong gmlc_mysql_query_get_version(GmlcMysqlQuery * pGmlcMysqlQry) {
  649     
  650     g_return_val_if_fail(pGmlcMysqlQry != NULL, 0);
  651     
  652     if (pGmlcMysqlQry->lVersion != 0) {
  653         return pGmlcMysqlQry->lVersion;
  654     }
  655     
  656     if (pGmlcMysqlQry->pMysqlLink == NULL) {
  657         gmlc_mysql_query_connect(pGmlcMysqlQry);
  658     }
  659     
  660     if (pGmlcMysqlQry->pMysqlLink == NULL) {
  661         return 0;
  662     }
  663     
  664     pGmlcMysqlQry->lVersion = mysql_get_server_version(pGmlcMysqlQry->pMysqlLink);
  665     
  666     return pGmlcMysqlQry->lVersion;
  667 }
  668 
  669 gchar * gmlc_mysql_query_get_current_charset(GmlcMysqlQuery * pGmlcMysqlQry) {
  670     gchar * pcCharset = NULL;
  671     
  672     g_return_val_if_fail(pGmlcMysqlQry != NULL, NULL);
  673     
  674     if (pGmlcMysqlQry->pMysqlLink == NULL) {
  675         gmlc_mysql_query_connect(pGmlcMysqlQry);
  676     }
  677     
  678     if (pGmlcMysqlQry->pMysqlLink == NULL) {
  679         return NULL;
  680     }
  681     
  682     if (pGmlcMysqlQry->lVersion >= 40100) {
  683         pcCharset = gmlc_tools_query_get_one_result(pGmlcMysqlQry->pMysqlLink, "ISO-8859-1", "SHOW VARIABLES LIKE 'character_set_results'", 1);
  684     
  685         if (pcCharset != NULL) {
  686             g_free(pGmlcMysqlQry->pcSrvCharset);
  687             pGmlcMysqlQry->pcSrvCharset = pcCharset;
  688         } 
  689     } else {
  690         pcCharset = gmlc_tools_query_get_one_result(pGmlcMysqlQry->pMysqlLink, "ISO-8859-1", "SHOW VARIABLES LIKE 'character_set'", 1);
  691     
  692         if (pcCharset != NULL) {
  693             g_free(pGmlcMysqlQry->pcSrvCharset);
  694             pGmlcMysqlQry->pcSrvCharset = pcCharset;
  695         } 
  696     }
  697     
  698     return pGmlcMysqlQry->pcSrvCharset;
  699 }
  700 
  701 void gmlc_mysql_query_read_error(GmlcMysqlQuery * pGmlcMysqlQry, gboolean bClearOnly) {
  702     
  703     g_return_if_fail(pGmlcMysqlQry != NULL);
  704     
  705     if (pGmlcMysqlQry->iErrCode < 0) {
  706         g_free(pGmlcMysqlQry->pcErrMsg);
  707     }
  708     
  709     if (bClearOnly) {
  710         pGmlcMysqlQry->iErrCode = 0;
  711         pGmlcMysqlQry->pcErrMsg = NULL;
  712     } else {
  713         pGmlcMysqlQry->iErrCode = mysql_errno(pGmlcMysqlQry->pMysqlLink);
  714         pGmlcMysqlQry->pcErrMsg = (gchar *)mysql_error(pGmlcMysqlQry->pMysqlLink);
  715     }
  716 
  717 }