Back to home page

Enduro/X

 
 

    


0001 /**
0002  * @brief Parse XA Configuration
0003  *
0004  * @file pgconfig.c
0005  */
0006 /* -----------------------------------------------------------------------------
0007  * Enduro/X Middleware Platform for Distributed Transaction Processing
0008  * Copyright (C) 2009-2016, ATR Baltic, Ltd. All Rights Reserved.
0009  * Copyright (C) 2017-2023, Mavimax, Ltd. All Rights Reserved.
0010  * This software is released under one of the following licenses:
0011  * AGPL (with Java and Go exceptions) or Mavimax's license for commercial use.
0012  * See LICENSE file for full text.
0013  * -----------------------------------------------------------------------------
0014  * AGPL license:
0015  *
0016  * This program is free software; you can redistribute it and/or modify it under
0017  * the terms of the GNU Affero General Public License, version 3 as published
0018  * by the Free Software Foundation;
0019  *
0020  * This program is distributed in the hope that it will be useful, but WITHOUT ANY
0021  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
0022  * PARTICULAR PURPOSE. See the GNU Affero General Public License, version 3
0023  * for more details.
0024  *
0025  * You should have received a copy of the GNU Affero General Public License along 
0026  * with this program; if not, write to the Free Software Foundation, Inc.,
0027  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
0028  *
0029  * -----------------------------------------------------------------------------
0030  * A commercial use license is available from Mavimax, Ltd
0031  * contact@mavimax.com
0032  * -----------------------------------------------------------------------------
0033  */
0034 #include <stdlib.h>
0035 #include <string.h>
0036 #include <memory.h>
0037 #include <ctype.h>
0038 #include <errno.h>
0039 
0040 #include <userlog.h>
0041 
0042 #include <ndrstandard.h>
0043 #include <ndebug.h>
0044 
0045 #include <exparson.h>
0046 #include <pgxa.h>
0047 
0048 /*------------------------------Externs---------------------------------------*/
0049 /*------------------------------Macros----------------------------------------*/
0050 
0051 /* values from ecpglib/extern.h */
0052 #define NDRX_ECPG_COMPAT_PGSQL              0
0053 #define NDRX_ECPG_COMPAT_INFORMIX           1
0054 #define NDRX_ECPG_COMPAT_INFORMIX_SE        1
0055 
0056 #define CONF_URL        1
0057 #define CONF_USER       2
0058 #define CONF_PASS       3
0059 #define CONF_COMPAT     4
0060         
0061 /*------------------------------Enums-----------------------------------------*/
0062 /*------------------------------Typedefs--------------------------------------*/
0063 /*------------------------------Globals---------------------------------------*/
0064 /*------------------------------Statics---------------------------------------*/
0065 /*------------------------------Prototypes------------------------------------*/
0066 
0067 /**
0068  * Parse XA Config.
0069  * JSON to parse is following:
0070  * { "url":"unix:postgresql://sql.mydomain.com:5432/mydb", "user":"test", 
0071  *  "password":"test1", "compat":"INFORMIX|INFORMIX_SE|PGSQL"}
0072  * @param[in] buffer JSON buffer
0073  * @param[out] conndata connection data
0074  */
0075 expublic int ndrx_pg_xa_cfgparse(char *buffer, ndrx_pgconnect_t *conndata)
0076 {
0077     int ret = EXSUCCEED;
0078     EXJSON_Value *root_value=NULL;
0079     EXJSON_Object *root_object;
0080     size_t i, cnt, j, n;
0081     char *name;
0082     char    *str_val;
0083     int typ;
0084     int param;
0085     
0086     NDRX_LOG(log_debug, "Parsing buffer: [%s]", buffer);
0087 
0088     root_value = exjson_parse_string_with_comments(buffer);
0089 
0090     if (exjson_value_get_type(root_value) != EXJSONObject)
0091     {
0092         NDRX_LOG(log_debug, "Failed to parse root element");
0093         EXFAIL_OUT(ret);
0094         goto out;
0095     }
0096     
0097     memset(conndata, 0, sizeof(ndrx_pgconnect_t));
0098     
0099     root_object = exjson_value_get_object(root_value);
0100 
0101     cnt = exjson_object_get_count(root_object);
0102     NDRX_LOG(log_debug, "cnt = %d", cnt);
0103     
0104     for (i=0; i<cnt; i++)
0105     {
0106         name = (char *)exjson_object_get_name(root_object, i);
0107 
0108         if (
0109                 ((param=CONF_URL) && 0==strcmp(name, "url")) ||
0110                 ((param=CONF_USER) && 0==strcmp(name, "user")) ||
0111                 ((param=CONF_PASS) && 0==strcmp(name, "password")) ||
0112                 ((param=CONF_COMPAT) && 0==strcmp(name, "compat"))
0113             )
0114         {
0115             
0116             typ = exjson_value_get_type(exjson_object_get_value_at(root_object, i));
0117 
0118             if (EXJSONString!=typ)
0119             {
0120                 NDRX_LOG(log_error, "Invalid type %d for `%s', must be string", 
0121                         typ, name);
0122                 EXFAIL_OUT(ret);
0123             }
0124             
0125             str_val = (char *)exjson_object_get_string(root_object, name);
0126             
0127             NDRX_LOG(log_debug, "Got [%s] = [%s]", name, str_val);
0128             
0129             switch (param)
0130             {
0131                 case CONF_URL:
0132                     NDRX_STRCPY_SAFE(conndata->url, str_val);
0133                     break;
0134                 case CONF_USER:
0135                     NDRX_STRCPY_SAFE(conndata->user, str_val);
0136                     break;
0137                 case CONF_PASS:
0138                     NDRX_STRCPY_SAFE(conndata->password, str_val);
0139                     break;
0140                 case CONF_COMPAT:
0141                     
0142                     if (0==strncmp(str_val, "INFORMIX", 8))
0143                     {
0144                         if (0==strcmp(str_val, "INFORMIX"))
0145                         {
0146                             conndata->c = NDRX_ECPG_COMPAT_INFORMIX;
0147                         }
0148                         else
0149                         {
0150                             conndata->c = NDRX_ECPG_COMPAT_INFORMIX_SE;
0151                         }
0152                     }
0153                     else if (0!=strcmp(str_val, "PGSQL"))
0154                     {
0155                         NDRX_LOG(log_error, "Invalid compat node [%s]", str_val);
0156                         EXFAIL_OUT(ret);
0157                     }
0158                     
0159                     break;
0160             }
0161         } 
0162         else
0163         {
0164             NDRX_LOG(log_warn, "Skipping [%s] - unsupported", name);
0165         }
0166     } /* for root object elements */
0167     
0168 out:
0169     /* cleanup code */
0170     if (NULL != root_value)
0171     {
0172         exjson_value_free(root_value);
0173     }
0174     
0175     return ret;
0176 }
0177 
0178 /* vim: set ts=4 sw=4 et smartindent: */