diff -dcrpN pgsql85dev.0orig/src/backend/parser/gram.y pgsql85dev.1dyncursor/src/backend/parser/gram.y
*** pgsql85dev.0orig/src/backend/parser/gram.y	2009-07-13 15:16:32.743838857 +0200
--- pgsql85dev.1dyncursor/src/backend/parser/gram.y	2009-07-14 15:51:32.679681966 +0200
*************** static TypeName *TableFuncTypeName(List 
*** 253,259 ****
  
  %type <str>		relation_name copy_file_name
  				database_name access_method_clause access_method attr_name
! 				index_name name file_name cluster_index_specification
  
  %type <list>	func_name handler_name qual_Op qual_all_Op subquery_Op
  				opt_class opt_validator validator_clause
--- 253,259 ----
  
  %type <str>		relation_name copy_file_name
  				database_name access_method_clause access_method attr_name
! 				index_name name cursor_name file_name cluster_index_specification
  
  %type <list>	func_name handler_name qual_Op qual_all_Op subquery_Op
  				opt_class opt_validator validator_clause
*************** reloption_elem:	
*** 1869,1875 ****
   *****************************************************************************/
  
  ClosePortalStmt:
! 			CLOSE name
  				{
  					ClosePortalStmt *n = makeNode(ClosePortalStmt);
  					n->portalname = $2;
--- 1869,1875 ----
   *****************************************************************************/
  
  ClosePortalStmt:
! 			CLOSE cursor_name
  				{
  					ClosePortalStmt *n = makeNode(ClosePortalStmt);
  					n->portalname = $2;
*************** comment_text:
*** 4062,4075 ****
   *
   *****************************************************************************/
  
! FetchStmt:	FETCH fetch_direction from_in name
  				{
  					FetchStmt *n = (FetchStmt *) $2;
  					n->portalname = $4;
  					n->ismove = FALSE;
  					$$ = (Node *)n;
  				}
! 			| FETCH name
  				{
  					FetchStmt *n = makeNode(FetchStmt);
  					n->direction = FETCH_FORWARD;
--- 4062,4093 ----
   *
   *****************************************************************************/
  
! FetchStmt:	FETCH BACKWARD from_in cursor_name
! 				{
! 					FetchStmt *n = makeNode(FetchStmt);
! 					n->portalname = $4;
! 					n->ismove = FALSE;
! 					n->direction = FETCH_BACKWARD;
! 					n->howMany = 1;
! 					$$ = (Node *)n;
! 				}
! 			| FETCH FORWARD from_in cursor_name
! 				{
! 					FetchStmt *n = makeNode(FetchStmt);
! 					n->portalname = $4;
! 					n->ismove = FALSE;
! 					n->direction = FETCH_FORWARD;
! 					n->howMany = 1;
! 					$$ = (Node *)n;
! 				}
! 			| FETCH fetch_direction from_in cursor_name
  				{
  					FetchStmt *n = (FetchStmt *) $2;
  					n->portalname = $4;
  					n->ismove = FALSE;
  					$$ = (Node *)n;
  				}
! 			| FETCH cursor_name
  				{
  					FetchStmt *n = makeNode(FetchStmt);
  					n->direction = FETCH_FORWARD;
*************** FetchStmt:	FETCH fetch_direction from_in
*** 4078,4091 ****
  					n->ismove = FALSE;
  					$$ = (Node *)n;
  				}
! 			| MOVE fetch_direction from_in name
  				{
  					FetchStmt *n = (FetchStmt *) $2;
  					n->portalname = $4;
  					n->ismove = TRUE;
  					$$ = (Node *)n;
  				}
! 			| MOVE name
  				{
  					FetchStmt *n = makeNode(FetchStmt);
  					n->direction = FETCH_FORWARD;
--- 4096,4127 ----
  					n->ismove = FALSE;
  					$$ = (Node *)n;
  				}
! 			| MOVE BACKWARD from_in cursor_name
! 				{
! 					FetchStmt *n = makeNode(FetchStmt);
! 					n->portalname = $4;
! 					n->ismove = TRUE;
! 					n->direction = FETCH_BACKWARD;
! 					n->howMany = 1;
! 					$$ = (Node *)n;
! 				}
! 			| MOVE FORWARD from_in cursor_name
! 				{
! 					FetchStmt *n = makeNode(FetchStmt);
! 					n->portalname = $4;
! 					n->ismove = TRUE;
! 					n->direction = FETCH_FORWARD;
! 					n->howMany = 1;
! 					$$ = (Node *)n;
! 				}
! 			| MOVE fetch_direction from_in cursor_name
  				{
  					FetchStmt *n = (FetchStmt *) $2;
  					n->portalname = $4;
  					n->ismove = TRUE;
  					$$ = (Node *)n;
  				}
! 			| MOVE cursor_name
  				{
  					FetchStmt *n = makeNode(FetchStmt);
  					n->direction = FETCH_FORWARD;
*************** fetch_direction:
*** 4160,4172 ****
  					n->howMany = FETCH_ALL;
  					$$ = (Node *)n;
  				}
- 			| FORWARD
- 				{
- 					FetchStmt *n = makeNode(FetchStmt);
- 					n->direction = FETCH_FORWARD;
- 					n->howMany = 1;
- 					$$ = (Node *)n;
- 				}
  			| FORWARD SignedIconst
  				{
  					FetchStmt *n = makeNode(FetchStmt);
--- 4196,4201 ----
*************** fetch_direction:
*** 4181,4193 ****
  					n->howMany = FETCH_ALL;
  					$$ = (Node *)n;
  				}
- 			| BACKWARD
- 				{
- 					FetchStmt *n = makeNode(FetchStmt);
- 					n->direction = FETCH_BACKWARD;
- 					n->howMany = 1;
- 					$$ = (Node *)n;
- 				}
  			| BACKWARD SignedIconst
  				{
  					FetchStmt *n = makeNode(FetchStmt);
--- 4210,4215 ----
*************** set_target_list:
*** 6776,6782 ****
   *				CURSOR STATEMENTS
   *
   *****************************************************************************/
! DeclareCursorStmt: DECLARE name cursor_options CURSOR opt_hold FOR SelectStmt
  				{
  					DeclareCursorStmt *n = makeNode(DeclareCursorStmt);
  					n->portalname = $2;
--- 6798,6804 ----
   *				CURSOR STATEMENTS
   *
   *****************************************************************************/
! DeclareCursorStmt: DECLARE cursor_name cursor_options CURSOR opt_hold FOR SelectStmt
  				{
  					DeclareCursorStmt *n = makeNode(DeclareCursorStmt);
  					n->portalname = $2;
*************** DeclareCursorStmt: DECLARE name cursor_o
*** 6787,6792 ****
--- 6809,6817 ----
  				}
  		;
  
+ cursor_name:	name						{ $$ = $1; }
+ 		;
+ 
  cursor_options: /*EMPTY*/					{ $$ = 0; }
  			| cursor_options NO SCROLL		{ $$ = $1 | CURSOR_OPT_NO_SCROLL; }
  			| cursor_options SCROLL			{ $$ = $1 | CURSOR_OPT_SCROLL; }
diff -dcrpN pgsql85dev.0orig/src/interfaces/ecpg/preproc/ecpg.addons pgsql85dev.1dyncursor/src/interfaces/ecpg/preproc/ecpg.addons
*** pgsql85dev.0orig/src/interfaces/ecpg/preproc/ecpg.addons	2009-01-30 17:28:46.000000000 +0100
--- pgsql85dev.1dyncursor/src/interfaces/ecpg/preproc/ecpg.addons	2009-07-14 16:19:41.370682220 +0200
*************** ECPG: fetch_directionBACKWARDSignedIcons
*** 221,226 ****
--- 221,235 ----
  			free($2);
  			$2 = make_str("$0");
  		}
+ ECPG: cursor_namename rule
+ 	| char_civar
+ 		{
+ 			char *curname = mm_alloc(strlen($1) + 2);
+ 			sprintf(curname, ":%s", $1);
+ 			free($1);
+ 			$1 = curname;
+ 			$$ = $1;
+ 		}
  ECPG: PrepareStmtPREPAREprepared_nameprep_type_clauseASPreparableStmt block
  	{
  		$$.name = $2;
*************** ECPG: PrepareStmtPREPAREprepared_namepre
*** 235,243 ****
  	}
  ECPG: ExecuteStmtEXECUTEprepared_nameexecute_param_clauseexecute_rest block
  	{ $$ = $2; }
! ECPG: DeclareCursorStmtDECLAREnamecursor_optionsCURSORopt_holdFORSelectStmt block
  	{
  		struct cursor *ptr, *this;
  
  		for (ptr = cur; ptr != NULL; ptr = ptr->next)
  		{
--- 244,253 ----
  	}
  ECPG: ExecuteStmtEXECUTEprepared_nameexecute_param_clauseexecute_rest block
  	{ $$ = $2; }
! ECPG: DeclareCursorStmtDECLAREcursor_namecursor_optionsCURSORopt_holdFORSelectStmt block
  	{
  		struct cursor *ptr, *this;
+ 		char *cursor_marker = $2[0] == ':' ? make_str("$0") : mm_strdup($2);
  
  		for (ptr = cur; ptr != NULL; ptr = ptr->next)
  		{
*************** ECPG: DeclareCursorStmtDECLAREnamecursor
*** 251,257 ****
  		this->name = $2;
  		this->connection = connection;
  		this->opened = false;
! 		this->command =  cat_str(7, make_str("declare"), mm_strdup($2), $3, make_str("cursor"), $5, make_str("for"), $7);
  		this->argsinsert = argsinsert;
  		this->argsresult = argsresult;
  		argsinsert = argsresult = NULL;
--- 261,267 ----
  		this->name = $2;
  		this->connection = connection;
  		this->opened = false;
! 		this->command =  cat_str(7, make_str("declare"), cursor_marker, $3, make_str("cursor"), $5, make_str("for"), $7);
  		this->argsinsert = argsinsert;
  		this->argsresult = argsresult;
  		argsinsert = argsresult = NULL;
*************** ECPG: DeclareCursorStmtDECLAREnamecursor
*** 262,267 ****
--- 272,282 ----
  		else
  			$$ = cat_str(3, make_str("/*"), mm_strdup(this->command), make_str("*/"));
  	}
+ ECPG: ClosePortalStmtCLOSEcursor_name block
+ 	{
+ 		char *cursor_marker = $2[0] == ':' ? make_str("$0") : $2;
+ 		$$ = cat2_str(make_str("close"), cursor_marker);
+ 	}
  ECPG: opt_hold block
  	{
  		if (compat == ECPG_COMPAT_INFORMIX_SE && autocommit == true)
*************** ECPG: VariableShowStmtSHOWALL block
*** 326,371 ****
  		mmerror(PARSE_ERROR, ET_ERROR, "SHOW ALL is not implemented");
  		$$ = EMPTY;
  	}
! ECPG: FetchStmtFETCHfetch_directionfrom_inname block 
  	{
  		add_additional_variables($4, false);
! 		$$ = cat_str(4, make_str("fetch"), $2, $3, $4);
  	}
! ECPG: FetchStmtFETCHname block
  	{
  		add_additional_variables($2, false);
! 		$$ = cat_str(2, make_str("fetch"), $2);
  	}
! ECPG: FetchStmtMOVEname rule
! 	| FETCH fetch_direction from_in name ecpg_into
  		{
  			add_additional_variables($4, false);
! 			$$ = cat_str(4, make_str("fetch"), $2, $3, $4);
  		}
! 	| FETCH fetch_direction name ecpg_into
  		{
  			add_additional_variables($3, false);
! 			$$ = cat_str(4, make_str("fetch"), $2, make_str("from"), $3);
  		}
! 	| FETCH from_in name ecpg_into
  		{
  			add_additional_variables($3, false);
! 			$$ = cat_str(3, make_str("fetch"), $2, $3);
  		}
! 	| FETCH name ecpg_into
  		{
  			add_additional_variables($2, false);
! 			$$ = cat2_str(make_str("fetch"), $2);
  		}
! 	| FETCH fetch_direction name
  		{
  			add_additional_variables($3, false);
! 			$$ = cat_str(4, make_str("fetch"), $2, make_str("from"), $3);
  		}
! 	| FETCH from_in name
  		{
  			add_additional_variables($3, false);
! 			$$ = cat_str(3, make_str("fetch"), $2, $3);
  		}
  ECPG: SpecialRuleRelationOLD addon
  		if (!QueryIsRule)
--- 341,442 ----
  		mmerror(PARSE_ERROR, ET_ERROR, "SHOW ALL is not implemented");
  		$$ = EMPTY;
  	}
! ECPG: FetchStmtFETCHBACKWARDfrom_incursor_name block 
  	{
+ 		char *cursor_marker = $4[0] == ':' ? make_str("$0") : $4;
  		add_additional_variables($4, false);
! 		$$ = cat_str(3, make_str("fetch backward"), $3, cursor_marker);
  	}
! ECPG: FetchStmtFETCHFORWARDfrom_incursor_name block
  	{
+ 		char *cursor_marker = $4[0] == ':' ? make_str("$0") : $4;
+ 		add_additional_variables($4, false);
+ 		$$ = cat_str(3, make_str("fetch forward"), $3, cursor_marker);
+ 	}
+ ECPG: FetchStmtFETCHfetch_directionfrom_incursor_name block
+ 	{
+ 		char *cursor_marker = $4[0] == ':' ? make_str("$0") : $4;
+ 		add_additional_variables($4, false);
+ 		$$ = cat_str(4, make_str("fetch"), $2, $3, cursor_marker);
+ 	}
+ ECPG: FetchStmtFETCHcursor_name block
+ 	{
+ 		char *cursor_marker = $2[0] == ':' ? make_str("$0") : $2;
  		add_additional_variables($2, false);
! 		$$ = cat_str(2, make_str("fetch"), cursor_marker);
  	}
! ECPG: FetchStmtMOVEcursor_name rule
! 	| FETCH BACKWARD from_in cursor_name ecpg_into
  		{
+ 			char *cursor_marker = $4[0] == ':' ? make_str("$0") : $4;
  			add_additional_variables($4, false);
! 			$$ = cat_str(3, make_str("fetch backward"), $3, cursor_marker);
  		}
! 	| FETCH FORWARD from_in cursor_name ecpg_into
  		{
+ 			char *cursor_marker = $4[0] == ':' ? make_str("$0") : $4;
+ 			add_additional_variables($4, false);
+ 			$$ = cat_str(3, make_str("fetch forward"), $3, cursor_marker);
+ 		}
+ 	| FETCH fetch_direction from_in cursor_name ecpg_into
+ 		{
+ 			char *cursor_marker = $4[0] == ':' ? make_str("$0") : $4;
+ 			add_additional_variables($4, false);
+ 			$$ = cat_str(4, make_str("fetch"), $2, $3, cursor_marker);
+ 		}
+ 	| FETCH BACKWARD cursor_name ecpg_into
+ 		{
+ 			char *cursor_marker = $3[0] == ':' ? make_str("$0") : $3;
  			add_additional_variables($3, false);
! 			$$ = cat2_str(make_str("fetch backward from"), cursor_marker);
  		}
! 	| FETCH FORWARD cursor_name ecpg_into
  		{
+ 			char *cursor_marker = $3[0] == ':' ? make_str("$0") : $3;
  			add_additional_variables($3, false);
! 			$$ = cat2_str(make_str("fetch forward from"), cursor_marker);
  		}
! 	| FETCH fetch_direction cursor_name ecpg_into
! 		{
! 			char *cursor_marker = $3[0] == ':' ? make_str("$0") : $3;
! 			add_additional_variables($3, false);
! 			$$ = cat_str(4, make_str("fetch"), $2, make_str("from"), cursor_marker);
! 		}
! 	| FETCH from_in cursor_name ecpg_into
! 		{
! 			char *cursor_marker = $3[0] == ':' ? make_str("$0") : $3;
! 			add_additional_variables($3, false);
! 			$$ = cat_str(3, make_str("fetch"), $2, cursor_marker);
! 		}
! 	| FETCH cursor_name ecpg_into
  		{
+ 			char *cursor_marker = $2[0] == ':' ? make_str("$0") : $2;
  			add_additional_variables($2, false);
! 			$$ = cat2_str(make_str("fetch"), cursor_marker);
  		}
! 	| FETCH BACKWARD cursor_name
  		{
+ 			char *cursor_marker = $3[0] == ':' ? make_str("$0") : $3;
  			add_additional_variables($3, false);
! 			$$ = cat2_str(make_str("fetch backward from"), cursor_marker);
  		}
! 	| FETCH FORWARD cursor_name
  		{
+ 			char *cursor_marker = $3[0] == ':' ? make_str("$0") : $3;
  			add_additional_variables($3, false);
! 			$$ = cat2_str(make_str("fetch forward from"), cursor_marker);
! 		}
! 	| FETCH fetch_direction cursor_name
! 		{
! 			char *cursor_marker = $3[0] == ':' ? make_str("$0") : $3;
! 			add_additional_variables($3, false);
! 			$$ = cat_str(4, make_str("fetch"), $2, make_str("from"), cursor_marker);
! 		}
! 	| FETCH from_in cursor_name
! 		{
! 			char *cursor_marker = $3[0] == ':' ? make_str("$0") : $3;
! 			add_additional_variables($3, false);
! 			$$ = cat_str(3, make_str("fetch"), $2, cursor_marker);
  		}
  ECPG: SpecialRuleRelationOLD addon
  		if (!QueryIsRule)
diff -dcrpN pgsql85dev.0orig/src/interfaces/ecpg/preproc/ecpg.trailer pgsql85dev.1dyncursor/src/interfaces/ecpg/preproc/ecpg.trailer
*** pgsql85dev.0orig/src/interfaces/ecpg/preproc/ecpg.trailer	2009-06-13 18:25:05.000000000 +0200
--- pgsql85dev.1dyncursor/src/interfaces/ecpg/preproc/ecpg.trailer	2009-07-14 16:36:00.932681716 +0200
*************** prepared_name: name             {
*** 284,292 ****
   * Declare a prepared cursor. The syntax is different from the standard
   * declare statement, so we create a new rule.
   */
! ECPGCursorStmt:  DECLARE name cursor_options CURSOR opt_hold FOR prepared_name
  		{
  			struct cursor *ptr, *this;
  			struct variable *thisquery = (struct variable *)mm_alloc(sizeof(struct variable));
  			const char *con = connection ? connection : "NULL";
  
--- 284,293 ----
   * Declare a prepared cursor. The syntax is different from the standard
   * declare statement, so we create a new rule.
   */
! ECPGCursorStmt:  DECLARE cursor_name cursor_options CURSOR opt_hold FOR prepared_name
  		{
  			struct cursor *ptr, *this;
+ 			char *cursor_marker = $2[0] == ':' ? make_str("$0") : mm_strdup($2);
  			struct variable *thisquery = (struct variable *)mm_alloc(sizeof(struct variable));
  			const char *con = connection ? connection : "NULL";
  
*************** ECPGCursorStmt:  DECLARE name cursor_opt
*** 303,309 ****
  			this->next = cur;
  			this->name = $2;
  			this->connection = connection;
! 			this->command =  cat_str(6, make_str("declare"), mm_strdup($2), $3, make_str("cursor"), $5, make_str("for $1"));
  			this->argsresult = NULL;
  
  			thisquery->type = &ecpg_query;
--- 304,310 ----
  			this->next = cur;
  			this->name = $2;
  			this->connection = connection;
! 			this->command =  cat_str(6, make_str("declare"), cursor_marker, $3, make_str("cursor"), $5, make_str("for $1"));
  			this->argsresult = NULL;
  
  			thisquery->type = &ecpg_query;
*************** ECPGCursorStmt:  DECLARE name cursor_opt
*** 313,318 ****
--- 314,325 ----
  			sprintf(thisquery->name, "ECPGprepared_statement(%s, %s, __LINE__)", con, $7);
  
  			this->argsinsert = NULL;
+ 			if ($2[0] == ':')
+ 			{
+ 				struct variable *var = find_variable($2 + 1);
+ 				remove_variable_from_list(&argsinsert, var);
+ 				add_variable_to_head(&(this->argsinsert), var, &no_indicator);
+ 			}
  			add_variable_to_head(&(this->argsinsert), thisquery, &no_indicator);
  
  			cur = this;
*************** ECPGFree:	SQL_FREE name	{ $$ = $2; }
*** 944,950 ****
  /*
   * open is an open cursor, at the moment this has to be removed
   */
! ECPGOpen: SQL_OPEN name opt_ecpg_using { $$ = $2; };
  
  opt_ecpg_using: /*EMPTY*/	{ $$ = EMPTY; }
  		| ecpg_using		{ $$ = $1; }
--- 951,966 ----
  /*
   * open is an open cursor, at the moment this has to be removed
   */
! ECPGOpen: SQL_OPEN cursor_name opt_ecpg_using
! 		{
! 			if ($2[0] == ':')
! 			{
! 				struct variable *var = find_variable($2 + 1);
! 				remove_variable_from_list(&argsinsert, var);
! 			}
! 			$$ = $2;
! 		}
! 		;
  
  opt_ecpg_using: /*EMPTY*/	{ $$ = EMPTY; }
  		| ecpg_using		{ $$ = $1; }
*************** civarind: cvariable indicator
*** 1768,1773 ****
--- 1784,1799 ----
  		}
  		;
  
+ char_civar: char_variable
+ 		{
+ 			char *qm; /* dummy questionmark, we have to pass the real variable name */
+ 			add_variable_to_head(&argsinsert, find_variable($1), &no_indicator);
+ 			qm = create_questionmarks($1, false);
+ 			free(qm);
+ 			$$ = $1;
+ 		}
+ 		;
+ 
  civar: cvariable
  		{
  			add_variable_to_head(&argsinsert, find_variable($1), &no_indicator);
diff -dcrpN pgsql85dev.0orig/src/interfaces/ecpg/preproc/ecpg.type pgsql85dev.1dyncursor/src/interfaces/ecpg/preproc/ecpg.type
*** pgsql85dev.0orig/src/interfaces/ecpg/preproc/ecpg.type	2008-11-14 11:03:33.000000000 +0100
--- pgsql85dev.1dyncursor/src/interfaces/ecpg/preproc/ecpg.type	2009-07-14 16:37:28.935681196 +0200
***************
*** 43,48 ****
--- 43,49 ----
  %type <str> c_term
  %type <str> c_thing
  %type <str> char_variable
+ %type <str> char_civar
  %type <str> civar
  %type <str> civarind
  %type <str> ColLabel
diff -dcrpN pgsql85dev.0orig/src/interfaces/ecpg/preproc/extern.h pgsql85dev.1dyncursor/src/interfaces/ecpg/preproc/extern.h
*** pgsql85dev.0orig/src/interfaces/ecpg/preproc/extern.h	2009-06-13 18:25:05.000000000 +0200
--- pgsql85dev.1dyncursor/src/interfaces/ecpg/preproc/extern.h	2009-07-14 16:37:58.314682948 +0200
*************** extern struct descriptor *lookup_descrip
*** 91,96 ****
--- 91,97 ----
  extern struct variable *descriptor_variable(const char *name, int input);
  extern void add_variable_to_head(struct arguments **, struct variable *, struct variable *);
  extern void add_variable_to_tail(struct arguments **, struct variable *, struct variable *);
+ extern void remove_variable_from_list(struct arguments ** list, struct variable * var);
  extern void dump_variables(struct arguments *, int);
  extern struct typedefs *get_typedef(char *);
  extern void adjust_array(enum ECPGttype, char **, char **, char *, char *, int, bool);
diff -dcrpN pgsql85dev.0orig/src/interfaces/ecpg/preproc/variable.c pgsql85dev.1dyncursor/src/interfaces/ecpg/preproc/variable.c
*** pgsql85dev.0orig/src/interfaces/ecpg/preproc/variable.c	2009-06-13 18:25:05.000000000 +0200
--- pgsql85dev.1dyncursor/src/interfaces/ecpg/preproc/variable.c	2009-07-14 16:40:53.576682685 +0200
*************** add_variable_to_tail(struct arguments **
*** 401,406 ****
--- 401,430 ----
  		*list = new;
  }
  
+ void
+ remove_variable_from_list(struct arguments ** list, struct variable * var)
+ {
+ 	struct arguments *p, *prev = NULL;
+ 	bool found = false;
+ 
+ 	for (p = *list; p; p = p->next)
+ 	{
+ 		if (p->variable == var)
+ 		{
+ 			found = true;
+ 			break;
+ 		}
+ 		prev = p;
+ 	}
+ 	if (found)
+ 	{
+ 		if (prev)
+ 			prev->next = p->next;
+ 		else
+ 			*list = p->next;
+ 	}
+ }
+ 
  /* Dump out a list of all the variable on this list.
     This is a recursive function that works from the end of the list and
     deletes the list as we go on.
