PostGIS  2.4.9dev-r@@SVN_REVISION@@

◆ escape_insert_string()

char * escape_insert_string ( char *  str)

Escape input string suitable for INSERT.

If no characters require escaping, simply return the input pointer. Otherwise return a new allocated string.

Definition at line 174 of file shp2pgsql-core.c.

Referenced by ShpLoaderGenerateSQLRowStatement().

175 {
176  /*
177  * Escape single quotes by adding a preceding single quote
178  *
179  * 1. find # of characters
180  * 2. make new string
181  */
182 
183  char *result;
184  char *ptr, *optr;
185  int toescape = 0;
186  size_t size;
187 
188  ptr = str;
189 
190  /* Count how many characters we need to escape so we know the size of the string we need to return */
191  while (*ptr)
192  {
193  if (*ptr == '\'')
194  toescape++;
195 
196  ptr++;
197  }
198 
199  /* If we don't have to escape anything, simply return the input pointer */
200  if (toescape == 0)
201  return str;
202 
203  size = ptr - str + toescape + 1;
204  result = calloc(1, size);
205  optr = result;
206  ptr = str;
207 
208  while (*ptr)
209  {
210  if (*ptr == '\'')
211  *optr++='\'';
212 
213  *optr++ = *ptr++;
214  }
215 
216  *optr='\0';
217 
218  return result;
219 }
Here is the caller graph for this function: