Beispiel #1
0
/*
** Return TRUE if the line typed in is an SQL command terminator other
** than a semi-colon.  The SQL Server style "go" command is understood
** as is the Oracle "/".
*/
static int _is_command_terminator(const char *zLine){
  extern int sqliteStrNICmp(const char*,const char*,int);
  while( isspace(*zLine) ){ zLine++; };
  if( zLine[0]=='/' && _all_whitespace(&zLine[1]) ) return 1;  /* Oracle */
  if( sqliteStrNICmp(zLine,"go",2)==0 && _all_whitespace(&zLine[2]) ){
    return 1;  /* SQL Server */
  }
  return 0;
}
Beispiel #2
0
/*
** Return TRUE if the line typed in is an SQL command terminator other
** than a semi-colon.  The SQL Server style "go" command is understood
** as is the Oracle "/".
*/
static int _is_command_terminator(const char *zLine){
	while( isspace(*(unsigned char*)zLine) ){ zLine++; };
	if( zLine[0]=='/' && _all_whitespace(&zLine[1]) ){
		return 1;  /* Oracle */
	}
	if( tolower(zLine[0])=='g' && tolower(zLine[1])=='o'
		&& _all_whitespace(&zLine[2]) ){
			return 1;  /* SQL Server */
	}
	return 0;
}
static void process_input(sqlite3 * db, FILE *in, int * lineErr){
  char *zLine;
  char *zSql = 0;
  char * zErrMsg = 0;
  int nSql = 0;
  int rc;
  while((zLine = sqlbrowser_getline(in))!=0 ){
    if( (zSql==0 || zSql[0]==0) && _all_whitespace(zLine) ) continue;
    (*lineErr)++;
    if( zSql==0 ){
      int i;
      for(i=0; zLine[i] && isspace(zLine[i]); i++){}
      if( zLine[i]!=0 ){
        nSql = strlen(zLine);
        zSql = malloc( nSql+1 );
        strcpy(zSql, zLine);
      }
    }else{
      int len = strlen(zLine);
      zSql = realloc( zSql, nSql + len + 2 );
      /*if( zSql==0 ){
        fprintf(stderr,"%s: out of memory!\n", Argv0);
        exit(1);
      }*/
      strcpy(&zSql[nSql++], "\n");
      strcpy(&zSql[nSql], zLine);
      nSql += len;
    }
    free(zLine);
    if( zSql && _ends_with_semicolon(zSql, nSql) && sqlite3_complete(zSql) ){
      rc = sqlite3_exec(db, zSql, NULL, NULL, &zErrMsg);//&zErrMsg
      if( rc || zErrMsg ){
        //if( in!=0 && !p->echoOn ) printf("%s\n",zSql);
        if( zErrMsg!=0 ){
          /*printf("SQL error: %s\n", zErrMsg);*/
          free(zErrMsg);
          zErrMsg = 0;
          if( zSql ){
            free(zSql);
           }
           return;
        }/*else{
          printf("SQL error: %s\n", sqlite3_error_string(rc));
        }*/
      }
      free(zSql);
      zSql = 0;
      nSql = 0;
    }
  }
  if( zSql ){
    /*if( !_all_whitespace(zSql) ) printf("Incomplete SQL: %s\n", zSql);*/
    free(zSql);
  }
  /*normal exit, clear err*/
  *lineErr = 0;
}
Beispiel #4
0
/*
** Read input from *in and process it.  If *in==0 then input
** is interactive - the user is typing it it.  Otherwise, input
** is coming from a file or device.  A prompt is issued and history
** is saved only if input is interactive.  An interrupt signal will
** cause this routine to exit immediately, unless input is interactive.
*/
static void process_input(struct callback_data *p, FILE *in){
  char *zLine;
  char *zSql = 0;
  int nSql = 0;
  char *zErrMsg;
  int rc;
  while( fflush(p->out), (zLine = one_input_line(zSql, in))!=0 ){
    if( seenInterrupt ){
      if( in!=0 ) break;
      seenInterrupt = 0;
    }
    if( p->echoOn ) printf("%s\n", zLine);
    if( (zSql==0 || zSql[0]==0) && _all_whitespace(zLine) ) continue;
    if( zLine && zLine[0]=='.' && nSql==0 ){
      int rc = do_meta_command(zLine, p);
      free(zLine);
      if( rc ) break;
      continue;
    }
    if( _is_command_terminator(zLine) ){
      strcpy(zLine,";");
    }
    if( zSql==0 ){
      int i;
      for(i=0; zLine[i] && isspace(zLine[i]); i++){}
      if( zLine[i]!=0 ){
        nSql = strlen(zLine);
        zSql = malloc( nSql+1 );
        strcpy(zSql, zLine);
      }
    }else{
      int len = strlen(zLine);
      zSql = realloc( zSql, nSql + len + 2 );
      if( zSql==0 ){
        fprintf(stderr,"%s: out of memory!\n", Argv0);
        exit(1);
      }
      strcpy(&zSql[nSql++], "\n");
      strcpy(&zSql[nSql], zLine);
      nSql += len;
    }
    free(zLine);
    if( zSql && _ends_with_semicolon(zSql, nSql) && sqlite_complete(zSql) ){
      p->cnt = 0;
      open_db(p);
      rc = sqlite_exec(p->db, zSql, callback, p, &zErrMsg);
      if( rc || zErrMsg ){
        if( in!=0 && !p->echoOn ) printf("%s\n",zSql);
        if( zErrMsg!=0 ){
          printf("SQL error: %s\n", zErrMsg);
          sqlite_freemem(zErrMsg);
          zErrMsg = 0;
        }else{
          printf("SQL error: %s\n", sqlite_error_string(rc));
        }
      }
      free(zSql);
      zSql = 0;
      nSql = 0;
    }
  }
  if( zSql ){
    if( !_all_whitespace(zSql) ) printf("Incomplete SQL: %s\n", zSql);
    free(zSql);
  }
}
Beispiel #5
0
/*
** Read input from *in and process it.  If *in==0 then input
** is interactive - the user is typing it it.  Otherwise, input
** is coming from a file or device.  A prompt is issued and history
** is saved only if input is interactive.  An interrupt signal will
** cause this routine to exit immediately, unless input is interactive.
**
** Return the number of errors.
*/
static int process_input(struct callback_data *p, FILE *in){
	char *zLine = 0;
	char *zSql = 0;
	int nSql = 0;
	int nSqlPrior = 0;
	char *zErrMsg;
	int rc;
	int errCnt = 0;
	int lineno = 0;
	int startline = 0;

	while( errCnt==0 || !bail_on_error || (in==0 && stdin_is_interactive) ){
		//fflush(p->out);
		free(zLine);
		zLine = one_input_line(in);
		if( zLine==0 ){
			break;  /* We have reached EOF */
		}
		if( seenInterrupt ){
			if( in!=0 ) break;
			seenInterrupt = 0;
		}
		lineno++;
		if( (zSql==0 || zSql[0]==0) && _all_whitespace(zLine) ) continue;
		if( zLine && zLine[0]=='.' && nSql==0 ){
			continue;
		}
		if( _is_command_terminator(zLine) && _is_complete(zSql, nSql) ){
			memcpy(zLine,";",2);
		}
		nSqlPrior = nSql;
		if( zSql==0 ){
			int i;
			for(i=0; zLine[i] && isspace((unsigned char)zLine[i]); i++){}
			if( zLine[i]!=0 ){
				nSql = strlen30(zLine);
				zSql = (char*)malloc( nSql+3 );
				if( zSql==0 ){
					fprintf(stderr, "Error: out of memory\n");
					exit(1);
				}
				memcpy(zSql, zLine, nSql+1);
				startline = lineno;
			}
		}else{
			int len = strlen30(zLine);
			zSql = (char*)realloc( zSql, nSql + len + 4 );
			if( zSql==0 ){
				fprintf(stderr,"Error: out of memory\n");
				exit(1);
			}
			zSql[nSql++] = '\n';
			memcpy(&zSql[nSql], zLine, len+1);
			nSql += len;
		}
		if( zSql && _contains_semicolon(&zSql[nSqlPrior], nSql-nSqlPrior)
			&& sqlite3_complete(zSql) ){
				p->cnt = 0;
				//
				rc = shell_exec(p->db, zSql, shell_callback, p, &zErrMsg);

				if( rc || zErrMsg ){
					char zPrefix[100];
					if( in!=0 || !stdin_is_interactive ){
						sqlite3_snprintf(sizeof(zPrefix), zPrefix, 
							"Error: near line %d:", startline);
					}else{
						sqlite3_snprintf(sizeof(zPrefix), zPrefix, "Error:");
					}
					if( zErrMsg!=0 ){
						fprintf(stderr, "%s %s\n", zPrefix, zErrMsg);
						sqlite3_free(zErrMsg);
						zErrMsg = 0;
					}else{
						fprintf(stderr, "%s %s\n", zPrefix, sqlite3_errmsg(p->db));
					}
					errCnt++;
				}
				free(zSql);
				zSql = 0;
				nSql = 0;
		}
	}
	if( zSql ){
		if( !_all_whitespace(zSql) ){
			fprintf(stderr, "Error: incomplete SQL: %s\n", zSql);
		}
		free(zSql);
	}
	free(zLine);
	return errCnt;
}