]> granicus.if.org Git - postgresql/commitdiff
Made code in ecpg better readable.
authorMichael Meskes <meskes@postgresql.org>
Wed, 4 Jan 2012 09:01:14 +0000 (10:01 +0100)
committerMichael Meskes <meskes@postgresql.org>
Wed, 4 Jan 2012 13:55:02 +0000 (14:55 +0100)
src/interfaces/ecpg/preproc/ecpg.header
src/interfaces/ecpg/test/expected/preproc-outofscope.c

index 94c45c88c0e7e159a0475c1c05612ddf134ea2b6..88d9cf53c0c87a41f928c6b6bd8a2a419d640f32 100644 (file)
@@ -224,16 +224,16 @@ adjust_outofscope_cursor_vars(struct cursor *cur)
 {
        /* Informix accepts DECLARE with variables that are out of scope when OPEN is called.
         * For instance you can DECLARE a cursor in one function, and OPEN/FETCH/CLOSE
-        * it in other functions. This is very useful for e.g. event-driver programming,
+        * it in another functions. This is very useful for e.g. event-driver programming,
         * but may also lead to dangerous programming. The limitation when this is allowed
-        * and doesn's cause problems have to be documented, like the allocated variables
+        * and doesn't cause problems have to be documented, like the allocated variables
         * must not be realloc()'ed.
         *
         * We have to change the variables to our own struct and just store the pointer
         * instead of the variable. Do it only for local variables, not for globals.
         */
 
-       char *result = mm_strdup("");
+       char *result = EMPTY;
        int insert;
 
        for (insert = 1; insert >= 0; insert--)
@@ -247,13 +247,14 @@ adjust_outofscope_cursor_vars(struct cursor *cur)
 
                for (ptr = list; ptr != NULL; ptr = ptr->next)
                {
-                       char temp[20];
+                       char var_text[20];
                        char *original_var;
                        bool skip_set_var = false;
+                       bool var_ptr = false;
 
                        /* change variable name to "ECPGget_var(<counter>)" */
                        original_var = ptr->variable->name;
-                       sprintf(temp, "%d))", ecpg_internal_var);
+                       sprintf(var_text, "%d))", ecpg_internal_var);
 
                        /* Don't emit ECPGset_var() calls for global variables */
                        if (ptr->variable->brace_level == 0)
@@ -276,13 +277,12 @@ adjust_outofscope_cursor_vars(struct cursor *cur)
                                newvar = new_variable(cat_str(4, mm_strdup("("),
                                                                                          mm_strdup(ecpg_type_name(ptr->variable->type->u.element->type)),
                                                                                          mm_strdup(" *)(ECPGget_var("),
-                                                                                         mm_strdup(temp)),
+                                                                                         mm_strdup(var_text)),
                                                                          ECPGmake_array_type(ECPGmake_simple_type(ptr->variable->type->u.element->type,
                                                                                                                                                           mm_strdup("1"),
                                                                                                                                                           ptr->variable->type->u.element->counter),
                                                                                                                  ptr->variable->type->size),
                                                                          0);
-                               sprintf(temp, "%d, (", ecpg_internal_var++);
                        }
                        else if ((ptr->variable->type->type == ECPGt_varchar
                                          || ptr->variable->type->type == ECPGt_char
@@ -293,59 +293,57 @@ adjust_outofscope_cursor_vars(struct cursor *cur)
                                newvar = new_variable(cat_str(4, mm_strdup("("),
                                                                                          mm_strdup(ecpg_type_name(ptr->variable->type->type)),
                                                                                          mm_strdup(" *)(ECPGget_var("),
-                                                                                         mm_strdup(temp)),
+                                                                                         mm_strdup(var_text)),
                                                                          ECPGmake_simple_type(ptr->variable->type->type,
                                                                                                                   ptr->variable->type->size,
                                                                                                                   ptr->variable->type->counter),
                                                                          0);
                                if (ptr->variable->type->type == ECPGt_varchar)
-                                       sprintf(temp, "%d, &(", ecpg_internal_var++);
-                               else
-                                       sprintf(temp, "%d, (", ecpg_internal_var++);
+                                       var_ptr = true;
                        }
                        else if (ptr->variable->type->type == ECPGt_struct
                                         || ptr->variable->type->type == ECPGt_union)
                        {
-                               sprintf(temp, "%d)))", ecpg_internal_var);
-                               newvar = new_variable(cat_str(4, mm_strdup("(*("),
+                               newvar = new_variable(cat_str(5, mm_strdup("(*("),
                                                                                          mm_strdup(ptr->variable->type->type_name),
                                                                                          mm_strdup(" *)(ECPGget_var("),
-                                                                                         mm_strdup(temp)),
+                                                                                         mm_strdup(var_text),
+                                                                                         mm_strdup(")")),
                                                                          ECPGmake_struct_type(ptr->variable->type->u.members,
                                                                                                                   ptr->variable->type->type,
                                                                                                                   ptr->variable->type->type_name,
                                                                                                                   ptr->variable->type->struct_sizeof),
                                                                          0);
-                               sprintf(temp, "%d, &(", ecpg_internal_var++);
+                               var_ptr = true;
                        }
                        else if (ptr->variable->type->type == ECPGt_array)
                        {
                                if (ptr->variable->type->u.element->type == ECPGt_struct
                                        || ptr->variable->type->u.element->type == ECPGt_union)
                                {
-                                       sprintf(temp, "%d)))", ecpg_internal_var);
-                                       newvar = new_variable(cat_str(4, mm_strdup("(*("),
-                                                                                                 mm_strdup(ptr->variable->type->u.element->type_name),
-                                                                                                 mm_strdup(" *)(ECPGget_var("), mm_strdup(temp)),
+                                       newvar = new_variable(cat_str(5, mm_strdup("(*("),
+                                                                                         mm_strdup(ptr->variable->type->u.element->type_name),
+                                                                                         mm_strdup(" *)(ECPGget_var("),
+                                                                                         mm_strdup(var_text),
+                                                                                         mm_strdup(")")),
                                                                                  ECPGmake_struct_type(ptr->variable->type->u.element->u.members,
                                                                                                                           ptr->variable->type->u.element->type,
                                                                                                                           ptr->variable->type->u.element->type_name,
                                                                                                                           ptr->variable->type->u.element->struct_sizeof),
                                                                                  0);
-                                       sprintf(temp, "%d, (", ecpg_internal_var++);
                                }
                                else
                                {
                                        newvar = new_variable(cat_str(4, mm_strdup("("),
                                                                                                  mm_strdup(ecpg_type_name(ptr->variable->type->type)),
                                                                                                  mm_strdup(" *)(ECPGget_var("),
-                                                                                                 mm_strdup(temp)),
+                                                                                                 mm_strdup(var_text)),
                                                                                  ECPGmake_array_type(ECPGmake_simple_type(ptr->variable->type->u.element->type,
                                                                                                                                                                   ptr->variable->type->u.element->size,
                                                                                                                                                                   ptr->variable->type->u.element->counter),
                                                                                                                          ptr->variable->type->size),
                                                                                  0);
-                                       sprintf(temp, "%d, &(", ecpg_internal_var++);
+                                       var_ptr = true;
                                }
                        }
                        else
@@ -353,19 +351,22 @@ adjust_outofscope_cursor_vars(struct cursor *cur)
                                newvar = new_variable(cat_str(4, mm_strdup("*("),
                                                                                          mm_strdup(ecpg_type_name(ptr->variable->type->type)),
                                                                                          mm_strdup(" *)(ECPGget_var("),
-                                                                                         mm_strdup(temp)),
+                                                                                         mm_strdup(var_text)),
                                                                          ECPGmake_simple_type(ptr->variable->type->type,
                                                                                                                   ptr->variable->type->size,
                                                                                                                   ptr->variable->type->counter),
                                                                          0);
-                               sprintf(temp, "%d, &(", ecpg_internal_var++);
+                               var_ptr = true;
                        }
 
-                       /* create call to "ECPGset_var(<counter>, <pointer>. <line number>)" */
+                       /* create call to "ECPGset_var(<counter>, <connection>, <pointer>. <line number>)" */
                        if (!skip_set_var)
+                       {
+                               sprintf(var_text, "%d, %s", ecpg_internal_var++, var_ptr ? "&(" : "(");
                                result = cat_str(5, result, mm_strdup("ECPGset_var("),
-                                                                mm_strdup(temp), mm_strdup(original_var),
+                                                                mm_strdup(var_text), mm_strdup(original_var),
                                                                 mm_strdup("), __LINE__);\n"));
+                       }
 
                        /* now the indicator if there is one and it's not a global variable */
                        if ((ptr->indicator->type->type == ECPGt_NO_INDICATOR) || (ptr->indicator->brace_level == 0))
@@ -376,50 +377,51 @@ adjust_outofscope_cursor_vars(struct cursor *cur)
                        {
                                /* change variable name to "ECPGget_var(<counter>)" */
                                original_var = ptr->indicator->name;
-                               sprintf(temp, "%d))", ecpg_internal_var);
+                               sprintf(var_text, "%d))", ecpg_internal_var);
+                               var_ptr = false;
 
                                if (ptr->indicator->type->type == ECPGt_struct
                                        || ptr->indicator->type->type == ECPGt_union)
                                {
-                                       sprintf(temp, "%d)))", ecpg_internal_var);
-                                       newind = new_variable(cat_str(4, mm_strdup("(*("),
-                                                                                                 mm_strdup(ptr->indicator->type->type_name),
-                                                                                                 mm_strdup(" *)(ECPGget_var("),
-                                                                                                 mm_strdup(temp)),
+                                       newind = new_variable(cat_str(5, mm_strdup("(*("),
+                                                                                         mm_strdup(ptr->indicator->type->type_name),
+                                                                                         mm_strdup(" *)(ECPGget_var("),
+                                                                                         mm_strdup(var_text),
+                                                                                         mm_strdup(")")),
                                                                                  ECPGmake_struct_type(ptr->indicator->type->u.members,
                                                                                                                           ptr->indicator->type->type,
                                                                                                                           ptr->indicator->type->type_name,
                                                                                                                           ptr->indicator->type->struct_sizeof),
                                                                                  0);
-                                       sprintf(temp, "%d, &(", ecpg_internal_var++);
+                                       var_ptr = true;
                                }
                                else if (ptr->indicator->type->type == ECPGt_array)
                                {
                                        if (ptr->indicator->type->u.element->type == ECPGt_struct
                                                || ptr->indicator->type->u.element->type == ECPGt_union)
                                        {
-                                               sprintf(temp, "%d)))", ecpg_internal_var);
-                                               newind = new_variable(cat_str(4, mm_strdup("(*("),
-                                                                                                         mm_strdup(ptr->indicator->type->u.element->type_name),
-                                                                                                         mm_strdup(" *)(ECPGget_var("), mm_strdup(temp)),
+                                               newind = new_variable(cat_str(5, mm_strdup("(*("),
+                                                                                         mm_strdup(ptr->indicator->type->u.element->type_name),
+                                                                                         mm_strdup(" *)(ECPGget_var("),
+                                                                                         mm_strdup(var_text),
+                                                                                         mm_strdup(")")),
                                                                                          ECPGmake_struct_type(ptr->indicator->type->u.element->u.members,
                                                                                                                                   ptr->indicator->type->u.element->type,
                                                                                                                                   ptr->indicator->type->u.element->type_name,
                                                                                                                                   ptr->indicator->type->u.element->struct_sizeof),
                                                                                          0);
-                                               sprintf(temp, "%d, (", ecpg_internal_var++);
                                        }
                                        else
                                        {
                                                newind = new_variable(cat_str(4, mm_strdup("("),
                                                                                                          mm_strdup(ecpg_type_name(ptr->indicator->type->u.element->type)),
-                                                                                                         mm_strdup(" *)(ECPGget_var("), mm_strdup(temp)),
+                                                                                                         mm_strdup(" *)(ECPGget_var("), mm_strdup(var_text)),
                                                                                          ECPGmake_array_type(ECPGmake_simple_type(ptr->indicator->type->u.element->type,
                                                                                                                                                                           ptr->indicator->type->u.element->size,
                                                                                                                                                                           ptr->indicator->type->u.element->counter),
                                                                                                                                  ptr->indicator->type->size),
                                                                                          0);
-                                               sprintf(temp, "%d, &(", ecpg_internal_var++);
+                                               var_ptr = true;
                                        }
                                }
                                else if (atoi(ptr->indicator->type->size) > 1)
@@ -427,29 +429,29 @@ adjust_outofscope_cursor_vars(struct cursor *cur)
                                        newind = new_variable(cat_str(4, mm_strdup("("),
                                                                                                  mm_strdup(ecpg_type_name(ptr->indicator->type->type)),
                                                                                                  mm_strdup(" *)(ECPGget_var("),
-                                                                                                 mm_strdup(temp)),
+                                                                                                 mm_strdup(var_text)),
                                                                                  ECPGmake_simple_type(ptr->indicator->type->type,
                                                                                                                           ptr->indicator->type->size,
                                                                                                                           ptr->variable->type->counter),
                                                                                  0);
-                                       sprintf(temp, "%d, (", ecpg_internal_var++);
                                }
                                else
                                {
                                        newind = new_variable(cat_str(4, mm_strdup("*("),
                                                                                                  mm_strdup(ecpg_type_name(ptr->indicator->type->type)),
                                                                                                  mm_strdup(" *)(ECPGget_var("),
-                                                                                                 mm_strdup(temp)),
+                                                                                                 mm_strdup(var_text)),
                                                                                  ECPGmake_simple_type(ptr->indicator->type->type,
                                                                                                                           ptr->indicator->type->size,
                                                                                                                           ptr->variable->type->counter),
                                                                                  0);
-                                       sprintf(temp, "%d, &(", ecpg_internal_var++);
+                                       var_ptr = true;
                                }
 
                                /* create call to "ECPGset_var(<counter>, <pointer>. <line number>)" */
+                               sprintf(var_text, "%d, %s", ecpg_internal_var++, var_ptr ? "&(" : "(");
                                result = cat_str(5, result, mm_strdup("ECPGset_var("),
-                                                                mm_strdup(temp), mm_strdup(original_var),
+                                                                mm_strdup(var_text), mm_strdup(original_var),
                                                                 mm_strdup("), __LINE__);\n"));
                        }
 
index a30b7215ae4fe63ed4fbe5a053b9abc41a54c932..125d7d8513e7c7a949bcd8d99238500efd9a91df 100644 (file)
@@ -202,16 +202,16 @@ static void
 open_cur1(void)
 {
        { ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_normal, "declare mycur cursor for select * from a1", ECPGt_EOIT, 
-       ECPGt_int,&((*( MYTYPE  *)(ECPGget_var( 0))).id),(long)1,(long)1,sizeof(int), 
-       ECPGt_int,&((*( MYNULLTYPE  *)(ECPGget_var( 1))).id),(long)1,(long)1,sizeof(int), 
-       ECPGt_char,&((*( MYTYPE  *)(ECPGget_var( 0))).t),(long)64,(long)1,(64)*sizeof(char), 
-       ECPGt_int,&((*( MYNULLTYPE  *)(ECPGget_var( 1))).t),(long)1,(long)1,sizeof(int), 
-       ECPGt_double,&((*( MYTYPE  *)(ECPGget_var( 0))).d1),(long)1,(long)1,sizeof(double), 
-       ECPGt_int,&((*( MYNULLTYPE  *)(ECPGget_var( 1))).d1),(long)1,(long)1,sizeof(int), 
-       ECPGt_double,&((*( MYTYPE  *)(ECPGget_var( 0))).d2),(long)1,(long)1,sizeof(double), 
-       ECPGt_int,&((*( MYNULLTYPE  *)(ECPGget_var( 1))).d2),(long)1,(long)1,sizeof(int), 
-       ECPGt_char,&((*( MYTYPE  *)(ECPGget_var( 0))).c),(long)30,(long)1,(30)*sizeof(char), 
-       ECPGt_int,&((*( MYNULLTYPE  *)(ECPGget_var( 1))).c),(long)1,(long)1,sizeof(int), ECPGt_EORT);
+       ECPGt_int,&((*( MYTYPE  *)(ECPGget_var( 0)) ).id),(long)1,(long)1,sizeof(int), 
+       ECPGt_int,&((*( MYNULLTYPE  *)(ECPGget_var( 1)) ).id),(long)1,(long)1,sizeof(int), 
+       ECPGt_char,&((*( MYTYPE  *)(ECPGget_var( 0)) ).t),(long)64,(long)1,(64)*sizeof(char), 
+       ECPGt_int,&((*( MYNULLTYPE  *)(ECPGget_var( 1)) ).t),(long)1,(long)1,sizeof(int), 
+       ECPGt_double,&((*( MYTYPE  *)(ECPGget_var( 0)) ).d1),(long)1,(long)1,sizeof(double), 
+       ECPGt_int,&((*( MYNULLTYPE  *)(ECPGget_var( 1)) ).d1),(long)1,(long)1,sizeof(int), 
+       ECPGt_double,&((*( MYTYPE  *)(ECPGget_var( 0)) ).d2),(long)1,(long)1,sizeof(double), 
+       ECPGt_int,&((*( MYNULLTYPE  *)(ECPGget_var( 1)) ).d2),(long)1,(long)1,sizeof(int), 
+       ECPGt_char,&((*( MYTYPE  *)(ECPGget_var( 0)) ).c),(long)30,(long)1,(30)*sizeof(char), 
+       ECPGt_int,&((*( MYNULLTYPE  *)(ECPGget_var( 1)) ).c),(long)1,(long)1,sizeof(int), ECPGt_EORT);
 #line 40 "outofscope.pgc"
 
 if (sqlca.sqlcode < 0) exit (1);}
@@ -226,16 +226,16 @@ static void
 get_record1(void)
 {
        { ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_normal, "fetch mycur", ECPGt_EOIT, 
-       ECPGt_int,&((*( MYTYPE  *)(ECPGget_var( 0))).id),(long)1,(long)1,sizeof(int), 
-       ECPGt_int,&((*( MYNULLTYPE  *)(ECPGget_var( 1))).id),(long)1,(long)1,sizeof(int), 
-       ECPGt_char,&((*( MYTYPE  *)(ECPGget_var( 0))).t),(long)64,(long)1,(64)*sizeof(char), 
-       ECPGt_int,&((*( MYNULLTYPE  *)(ECPGget_var( 1))).t),(long)1,(long)1,sizeof(int), 
-       ECPGt_double,&((*( MYTYPE  *)(ECPGget_var( 0))).d1),(long)1,(long)1,sizeof(double), 
-       ECPGt_int,&((*( MYNULLTYPE  *)(ECPGget_var( 1))).d1),(long)1,(long)1,sizeof(int), 
-       ECPGt_double,&((*( MYTYPE  *)(ECPGget_var( 0))).d2),(long)1,(long)1,sizeof(double), 
-       ECPGt_int,&((*( MYNULLTYPE  *)(ECPGget_var( 1))).d2),(long)1,(long)1,sizeof(int), 
-       ECPGt_char,&((*( MYTYPE  *)(ECPGget_var( 0))).c),(long)30,(long)1,(30)*sizeof(char), 
-       ECPGt_int,&((*( MYNULLTYPE  *)(ECPGget_var( 1))).c),(long)1,(long)1,sizeof(int), ECPGt_EORT);
+       ECPGt_int,&((*( MYTYPE  *)(ECPGget_var( 0)) ).id),(long)1,(long)1,sizeof(int), 
+       ECPGt_int,&((*( MYNULLTYPE  *)(ECPGget_var( 1)) ).id),(long)1,(long)1,sizeof(int), 
+       ECPGt_char,&((*( MYTYPE  *)(ECPGget_var( 0)) ).t),(long)64,(long)1,(64)*sizeof(char), 
+       ECPGt_int,&((*( MYNULLTYPE  *)(ECPGget_var( 1)) ).t),(long)1,(long)1,sizeof(int), 
+       ECPGt_double,&((*( MYTYPE  *)(ECPGget_var( 0)) ).d1),(long)1,(long)1,sizeof(double), 
+       ECPGt_int,&((*( MYNULLTYPE  *)(ECPGget_var( 1)) ).d1),(long)1,(long)1,sizeof(int), 
+       ECPGt_double,&((*( MYTYPE  *)(ECPGget_var( 0)) ).d2),(long)1,(long)1,sizeof(double), 
+       ECPGt_int,&((*( MYNULLTYPE  *)(ECPGget_var( 1)) ).d2),(long)1,(long)1,sizeof(int), 
+       ECPGt_char,&((*( MYTYPE  *)(ECPGget_var( 0)) ).c),(long)30,(long)1,(30)*sizeof(char), 
+       ECPGt_int,&((*( MYNULLTYPE  *)(ECPGget_var( 1)) ).c),(long)1,(long)1,sizeof(int), ECPGt_EORT);
 #line 49 "outofscope.pgc"
 
 if (sqlca.sqlcode < 0) exit (1);}