1996-07-09 08:22:35 +02:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
|
|
|
* psql.c--
|
|
|
|
* an interactive front-end to postgres95
|
|
|
|
*
|
|
|
|
* Copyright (c) 1996, Regents of the University of California
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* IDENTIFICATION
|
1996-07-23 05:03:43 +02:00
|
|
|
* $Header: /cvsroot/pgsql/src/bin/psql/Attic/psql.c,v 1.6 1996/07/23 03:03:43 scrappy Exp $
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <fcntl.h>
|
1996-07-23 05:03:43 +02:00
|
|
|
|
1996-07-09 08:22:35 +02:00
|
|
|
#include "libpq-fe.h"
|
|
|
|
#include "stringutils.h"
|
|
|
|
|
|
|
|
#include "psqlHelp.h"
|
|
|
|
|
|
|
|
#ifdef NOREADLINE
|
|
|
|
extern char *readline(char *); /* in rlstubs.c */
|
|
|
|
#else
|
|
|
|
/* from the GNU readline library */
|
|
|
|
#ifdef OLD_READLINE
|
|
|
|
#include "readline.h"
|
|
|
|
#include "history.h"
|
|
|
|
#else
|
|
|
|
#include <readline/readline.h>
|
1996-07-23 05:03:43 +02:00
|
|
|
#include <history.h>
|
1996-07-09 08:22:35 +02:00
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define MAX_QUERY_BUFFER 20000
|
1996-07-23 05:03:43 +02:00
|
|
|
#define MAX_FIELD_SEP_LENGTH 40
|
1996-07-09 08:22:35 +02:00
|
|
|
|
|
|
|
#define COPYBUFSIZ 8192
|
|
|
|
|
1996-07-23 05:03:43 +02:00
|
|
|
#define DEFAULT_FIELD_SEP " "
|
1996-07-09 08:22:35 +02:00
|
|
|
#define DEFAULT_EDITOR "vi"
|
|
|
|
#define DEFAULT_SHELL "/bin/sh"
|
|
|
|
|
|
|
|
typedef struct _psqlSettings {
|
1996-07-23 05:03:43 +02:00
|
|
|
int echoQuery; /* if 1, echo the query before sending it */
|
|
|
|
int quiet; /* run quietly, no messages, no promt */
|
|
|
|
int singleStep; /* if 1, prompt for each query */
|
|
|
|
int singleLineMode; /* if 1, query terminated by newline */
|
|
|
|
int useReadline; /* use the readline routines or not */
|
|
|
|
int printHeader; /* print output field headers or not */
|
|
|
|
int fillAlign; /* fill align the fields */
|
|
|
|
FILE *queryFout; /* where to send the query results */
|
|
|
|
char fieldSep[MAX_FIELD_SEP_LENGTH]; /* field separator */
|
1996-07-09 08:22:35 +02:00
|
|
|
} PsqlSettings;
|
|
|
|
|
|
|
|
/* declarations for functions in this file */
|
|
|
|
static void usage(char* progname);
|
|
|
|
static void slashUsage();
|
1996-07-23 05:03:43 +02:00
|
|
|
static void handleCopyOut(PGresult *res, int quiet);
|
|
|
|
static void handleCopyIn(PGresult *res, int quiet);
|
|
|
|
static int tableList(PGconn* conn, int deep_tablelist);
|
|
|
|
static int tableDesc(PGconn* conn, char* table);
|
1996-07-09 08:22:35 +02:00
|
|
|
|
|
|
|
char* gets_noreadline(char* prompt, FILE* source);
|
|
|
|
char* gets_readline(char* prompt, FILE* source);
|
|
|
|
char* gets_fromFile(char* prompt, FILE* source);
|
1996-07-23 05:03:43 +02:00
|
|
|
int listAllDbs(PGconn *db, PsqlSettings *settings);
|
|
|
|
int SendQuery(PGconn* db, char* query, PsqlSettings *settings);
|
|
|
|
int HandleSlashCmds(PGconn** db_ptr,
|
1996-07-09 08:22:35 +02:00
|
|
|
char *line,
|
1996-07-23 05:03:43 +02:00
|
|
|
char** prompt_ptr,
|
|
|
|
char *query,
|
|
|
|
PsqlSettings *settings);
|
|
|
|
int MainLoop(PGconn** db_ptr, FILE *source, PsqlSettings *settings);
|
|
|
|
FILE* setFout(char *fname);
|
1996-07-09 08:22:35 +02:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* usage
|
|
|
|
* print out usage for command line arguments
|
|
|
|
*/
|
|
|
|
|
|
|
|
static void
|
|
|
|
usage(char* progname)
|
|
|
|
{
|
|
|
|
fprintf(stderr,"Usage: %s [options] [dbname]\n",progname);
|
|
|
|
fprintf(stderr,"\t -a authsvc set authentication service\n");
|
1996-07-23 05:03:43 +02:00
|
|
|
fprintf(stderr,"\t -A turn off fill-justification when printing out attributes\n");
|
1996-07-09 08:22:35 +02:00
|
|
|
fprintf(stderr,"\t -c query run single query (slash commands too)\n");
|
|
|
|
fprintf(stderr,"\t -d dbName specify database name\n");
|
|
|
|
fprintf(stderr,"\t -e echo the query sent to the backend\n");
|
|
|
|
fprintf(stderr,"\t -f filename use file as a source of queries\n");
|
|
|
|
fprintf(stderr,"\t -F sep set the field separator (default is " ")\n");
|
1996-07-23 05:03:43 +02:00
|
|
|
fprintf(stderr,"\t -h help information\n");
|
|
|
|
fprintf(stderr,"\t -H host set database server host\n");
|
1996-07-09 08:22:35 +02:00
|
|
|
fprintf(stderr,"\t -l list available databases\n");
|
|
|
|
fprintf(stderr,"\t -n don't use readline library\n");
|
1996-07-23 05:03:43 +02:00
|
|
|
fprintf(stderr,"\t -o filename send output to filename\n");
|
1996-07-09 08:22:35 +02:00
|
|
|
fprintf(stderr,"\t -p port set port number\n");
|
|
|
|
fprintf(stderr,"\t -q run quietly (no messages, no prompts)\n");
|
|
|
|
fprintf(stderr,"\t -s single step mode (prompts for each query)\n");
|
|
|
|
fprintf(stderr,"\t -S single line mode (i.e. query terminated by newline)\n");
|
1996-07-23 05:03:43 +02:00
|
|
|
fprintf(stderr,"\t -T turn off printing of attribute names\n");
|
1996-07-09 08:22:35 +02:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* slashUsage
|
|
|
|
* print out usage for the backslash commands
|
|
|
|
*/
|
|
|
|
|
|
|
|
static void
|
1996-07-23 05:03:43 +02:00
|
|
|
slashUsage()
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1996-07-23 05:03:43 +02:00
|
|
|
fprintf(stderr,"\t \\a -- toggle fill-justification of display of attributes\n");
|
1996-07-09 08:22:35 +02:00
|
|
|
fprintf(stderr,"\t \\d [<table>] -- list tables in database or columns in <table>\n");
|
|
|
|
fprintf(stderr,"\t \\d * -- list tables in database and columns in all tables\n");
|
|
|
|
fprintf(stderr,"\t \\e [<fname>] -- edit the current query buffer or <fname>\n");
|
1996-07-23 05:03:43 +02:00
|
|
|
fprintf(stderr,"\t \\f <sep> -- change field separator\n");
|
|
|
|
fprintf(stderr,"\t \\g -- query to backend\n");
|
|
|
|
fprintf(stderr,"\t \\h <command> -- help on syntax of sql commands\n");
|
1996-07-09 08:22:35 +02:00
|
|
|
fprintf(stderr,"\t \\h * -- complete description of all sql commands\n");
|
1996-07-23 05:03:43 +02:00
|
|
|
fprintf(stderr,"\t \\g -- send query to backend\n");
|
|
|
|
fprintf(stderr,"\t \\i <fname> -- read queries from filename\n");
|
1996-07-09 08:22:35 +02:00
|
|
|
fprintf(stderr,"\t \\l -- list all databases\n");
|
1996-07-23 05:03:43 +02:00
|
|
|
fprintf(stderr,"\t \\o [<fname>] -- send query results file named <fname> or stdout\n");
|
1996-07-09 08:22:35 +02:00
|
|
|
fprintf(stderr,"\t \\p -- print the current query buffer\n");
|
|
|
|
fprintf(stderr,"\t \\q -- quit\n");
|
1996-07-23 05:03:43 +02:00
|
|
|
fprintf(stderr,"\t \\s [<fname>] -- save or print history\n");
|
|
|
|
fprintf(stderr,"\t \\t -- toggle output field headers (defaults to on)\n");
|
|
|
|
fprintf(stderr,"\t \\! [<cmd>] -- shell escape\n");
|
1996-07-09 08:22:35 +02:00
|
|
|
fprintf(stderr,"\t \\? -- help\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* listAllDbs
|
|
|
|
*
|
|
|
|
* list all the databases in the system
|
|
|
|
* returns 0 if all went well
|
|
|
|
*
|
|
|
|
*
|
|
|
|
*/
|
1996-07-23 05:03:43 +02:00
|
|
|
int
|
|
|
|
listAllDbs(PGconn *db, PsqlSettings *settings)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
|
|
|
PGresult *results;
|
|
|
|
char* query = "select * from pg_database;";
|
|
|
|
|
1996-07-23 05:03:43 +02:00
|
|
|
results = PQexec(db, query);
|
|
|
|
if (results == NULL) {
|
|
|
|
fprintf(stderr,"%s", PQerrorMessage(db));
|
1996-07-09 08:22:35 +02:00
|
|
|
return 1;
|
1996-07-23 05:03:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (PQresultStatus(results) != PGRES_TUPLES_OK)
|
|
|
|
{
|
|
|
|
fprintf(stderr,"Unexpected error from executing: %s\n", query);
|
|
|
|
return 2;
|
|
|
|
}
|
1996-07-09 08:22:35 +02:00
|
|
|
else
|
|
|
|
{
|
1996-07-23 05:03:43 +02:00
|
|
|
PQdisplayTuples(results,
|
|
|
|
settings->queryFout,
|
|
|
|
settings->fillAlign,
|
|
|
|
settings->fieldSep,
|
|
|
|
settings->printHeader,
|
|
|
|
settings->quiet);
|
1996-07-09 08:22:35 +02:00
|
|
|
PQclear(results);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
1996-07-23 05:03:43 +02:00
|
|
|
* tableList (PGconn* conn)
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
|
|
|
* List The Database Tables
|
|
|
|
* returns 0 if all went well
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
int
|
1996-07-23 05:03:43 +02:00
|
|
|
tableList (PGconn* conn, int deep_tablelist)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
|
|
|
char listbuf[256];
|
|
|
|
int nColumns;
|
|
|
|
int i;
|
1996-07-23 05:03:43 +02:00
|
|
|
char* ru;
|
1996-07-09 08:22:35 +02:00
|
|
|
char* rk;
|
|
|
|
char* rr;
|
|
|
|
|
|
|
|
PGresult* res;
|
|
|
|
|
|
|
|
listbuf[0] = '\0';
|
|
|
|
strcat(listbuf,"SELECT usename, relname, relkind, relhasrules");
|
|
|
|
strcat(listbuf," FROM pg_class, pg_user ");
|
|
|
|
strcat(listbuf,"WHERE ( relkind = 'r' OR relkind = 'i') ");
|
|
|
|
strcat(listbuf," and relname !~ '^pg_'");
|
1996-07-20 10:44:45 +02:00
|
|
|
strcat(listbuf," and relname !~ '^Inv[0-9]+'");
|
1996-07-09 08:22:35 +02:00
|
|
|
/* the usesysid = relowner won't work on stock 1.0 dbs, need to
|
|
|
|
add in the int4oideq function */
|
|
|
|
strcat(listbuf," and usesysid = relowner");
|
|
|
|
strcat(listbuf," ORDER BY relname ");
|
1996-07-23 05:03:43 +02:00
|
|
|
res = PQexec(conn,listbuf);
|
|
|
|
if (res == NULL) {
|
|
|
|
fprintf(stderr,"%s", PQerrorMessage(conn));
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((PQresultStatus(res) != PGRES_TUPLES_OK) || (PQntuples(res) <= 0)) {
|
|
|
|
fprintf(stderr,"No tables found in database %s.\n", PQdb(conn));
|
|
|
|
PQclear(res);
|
|
|
|
return (-1);
|
|
|
|
}
|
1996-07-09 08:22:35 +02:00
|
|
|
|
|
|
|
/* first, print out the attribute names */
|
|
|
|
nColumns = PQntuples(res);
|
|
|
|
if (nColumns > 0)
|
|
|
|
{
|
|
|
|
if ( deep_tablelist ) {
|
|
|
|
/* describe everything here */
|
|
|
|
char **table;
|
|
|
|
table = (char**)malloc(nColumns * sizeof(char*));
|
|
|
|
if ( table == NULL )
|
|
|
|
perror("malloc");
|
|
|
|
|
|
|
|
/* load table table*/
|
|
|
|
for (i=0; i < nColumns; i++) {
|
|
|
|
table[i] = (char *) malloc(PQgetlength(res,i,1) * sizeof(char) + 1);
|
|
|
|
if ( table[i] == NULL )
|
|
|
|
perror("malloc");
|
|
|
|
strcpy(table[i],PQgetvalue(res,i,1));
|
|
|
|
}
|
|
|
|
|
|
|
|
PQclear(res);
|
|
|
|
for (i=0; i < nColumns; i++) {
|
1996-07-23 05:03:43 +02:00
|
|
|
tableDesc(conn,table[i]);
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
free(table);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* Display the information */
|
|
|
|
|
1996-07-23 05:03:43 +02:00
|
|
|
printf ("\nDatabase = %s\n", PQdb(conn));
|
1996-07-09 08:22:35 +02:00
|
|
|
printf (" +------------------+----------------------------------+----------+\n");
|
|
|
|
printf (" | Owner | Relation | Type |\n");
|
|
|
|
printf (" +------------------+----------------------------------+----------+\n");
|
|
|
|
|
|
|
|
/* next, print out the instances */
|
|
|
|
for (i=0; i < PQntuples(res); i++) {
|
|
|
|
printf (" | %-16.16s", PQgetvalue(res,i,0));
|
|
|
|
printf (" | %-32.32s | ", PQgetvalue(res,i,1));
|
|
|
|
rk = PQgetvalue(res,i,2);
|
|
|
|
rr = PQgetvalue(res,i,3);
|
|
|
|
if (strcmp(rk, "r") == 0)
|
|
|
|
printf ("%-8.8s |", (rr[0] == 't') ? "view?" : "table" );
|
|
|
|
else
|
|
|
|
printf ("%-8.8s |", "index");
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
printf (" +------------------+----------------------------------+----------+\n");
|
|
|
|
PQclear(res);
|
|
|
|
}
|
|
|
|
return (0);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
fprintf (stderr, "Couldn't find any tables!\n");
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
1996-07-23 05:03:43 +02:00
|
|
|
* Describe a table (PGconn* conn, char* table)
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
|
|
|
* Describe the columns in a database table.
|
|
|
|
* returns 0 if all went well
|
|
|
|
*
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
int
|
1996-07-23 05:03:43 +02:00
|
|
|
tableDesc (PGconn* conn, char* table)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
|
|
|
char descbuf[256];
|
|
|
|
int nColumns;
|
|
|
|
char *rtype;
|
|
|
|
int i;
|
|
|
|
int rsize;
|
|
|
|
|
|
|
|
PGresult* res;
|
|
|
|
|
|
|
|
/* Build the query */
|
|
|
|
|
|
|
|
descbuf[0] = '\0';
|
|
|
|
strcat(descbuf,"SELECT a.attnum, a.attname, t.typname, a.attlen");
|
|
|
|
strcat(descbuf," FROM pg_class c, pg_attribute a, pg_type t ");
|
|
|
|
strcat(descbuf," WHERE c.relname = '");
|
|
|
|
strcat(descbuf,table);
|
|
|
|
strcat(descbuf,"'");
|
|
|
|
strcat(descbuf," and a.attnum > 0 ");
|
|
|
|
strcat(descbuf," and a.attrelid = c.oid ");
|
|
|
|
strcat(descbuf," and a.atttypid = t.oid ");
|
|
|
|
strcat(descbuf," ORDER BY attnum ");
|
1996-07-23 05:03:43 +02:00
|
|
|
res = PQexec(conn,descbuf);
|
|
|
|
if (res == NULL) {
|
|
|
|
fprintf(stderr,"%s", PQerrorMessage(conn));
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
if ((PQresultStatus(res) != PGRES_TUPLES_OK) || (PQntuples(res) <= 0)) {
|
|
|
|
fprintf(stderr,"Couldn't find table %s!\n", table);
|
|
|
|
PQclear(res);
|
|
|
|
return (-1);
|
|
|
|
}
|
1996-07-09 08:22:35 +02:00
|
|
|
/* first, print out the attribute names */
|
|
|
|
nColumns = PQntuples(res);
|
|
|
|
if (nColumns > 0)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
** Display the information
|
|
|
|
*/
|
|
|
|
|
|
|
|
printf ("\nTable = %s\n", table);
|
1996-07-23 05:03:43 +02:00
|
|
|
printf ("+----------------------------------+----------------------------------+-------+\n");
|
|
|
|
printf ("| Field | Type | Length|\n");
|
|
|
|
printf ("+----------------------------------+----------------------------------+-------+\n");
|
1996-07-09 08:22:35 +02:00
|
|
|
|
|
|
|
/* next, print out the instances */
|
|
|
|
for (i=0; i < PQntuples(res); i++) {
|
|
|
|
printf ("| %-32.32s | ", PQgetvalue(res,i,1));
|
|
|
|
rtype = PQgetvalue(res,i,2);
|
|
|
|
rsize = atoi(PQgetvalue(res,i,3));
|
|
|
|
if (strcmp(rtype, "text") == 0) {
|
|
|
|
printf ("%-32.32s |", rtype);
|
1996-07-23 05:03:43 +02:00
|
|
|
printf ("%-6s |", "var" );
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
else if (strcmp(rtype, "bpchar") == 0) {
|
|
|
|
printf ("%-32.32s |", "char");
|
1996-07-23 05:03:43 +02:00
|
|
|
printf ("%-6i |", rsize > 0 ? rsize - 4 : 0 );
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
else if (strcmp(rtype, "varchar") == 0) {
|
|
|
|
printf ("%-32.32s |", rtype);
|
1996-07-23 05:03:43 +02:00
|
|
|
printf ("%-6i |", rsize > 0 ? rsize - 4 : 0 );
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* array types start with an underscore */
|
|
|
|
if (rtype[0] != '_')
|
|
|
|
printf ("%-32.32s |", rtype);
|
|
|
|
else {
|
|
|
|
char *newname;
|
|
|
|
newname = malloc(strlen(rtype) + 2);
|
|
|
|
strcpy(newname, rtype+1);
|
|
|
|
strcat(newname, "[]");
|
|
|
|
printf ("%-32.32s |", newname);
|
|
|
|
free(newname);
|
|
|
|
}
|
|
|
|
if (rsize > 0)
|
1996-07-23 05:03:43 +02:00
|
|
|
printf ("%-6i |", rsize);
|
1996-07-09 08:22:35 +02:00
|
|
|
else
|
1996-07-23 05:03:43 +02:00
|
|
|
printf ("%-6s |", "var");
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
printf("\n");
|
|
|
|
}
|
1996-07-23 05:03:43 +02:00
|
|
|
printf ("+----------------------------------+----------------------------------+-------+\n");
|
1996-07-09 08:22:35 +02:00
|
|
|
|
|
|
|
PQclear(res);
|
|
|
|
return (0);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
fprintf (stderr, "Couldn't find table %s!\n", table);
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
typedef char* (*READ_ROUTINE)(char* prompt, FILE* source);
|
|
|
|
|
|
|
|
/* gets_noreadline prompt source
|
|
|
|
gets a line of input without calling readline, the source is ignored
|
|
|
|
*/
|
|
|
|
char*
|
|
|
|
gets_noreadline(char* prompt, FILE* source)
|
|
|
|
{
|
|
|
|
fputs(prompt, stdout);
|
|
|
|
fflush(stdout);
|
|
|
|
return(gets_fromFile(prompt,stdin));
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* gets_readline prompt source
|
|
|
|
* the routine to get input from GNU readline(), the source is ignored
|
|
|
|
* the prompt argument is used as the prompting string
|
|
|
|
*/
|
|
|
|
char*
|
|
|
|
gets_readline(char* prompt, FILE* source)
|
|
|
|
{
|
|
|
|
return (readline(prompt));
|
|
|
|
}
|
|
|
|
|
1996-07-23 05:03:43 +02:00
|
|
|
|
1996-07-09 08:22:35 +02:00
|
|
|
/*
|
|
|
|
* gets_fromFile prompt source
|
|
|
|
*
|
|
|
|
* the routine to read from a file, the prompt argument is ignored
|
|
|
|
* the source argument is a FILE*
|
|
|
|
*/
|
|
|
|
char*
|
|
|
|
gets_fromFile(char* prompt, FILE* source)
|
|
|
|
{
|
|
|
|
char* line;
|
|
|
|
int len;
|
|
|
|
|
|
|
|
line = malloc(MAX_QUERY_BUFFER+1);
|
|
|
|
|
|
|
|
/* read up to MAX_QUERY_BUFFER characters */
|
|
|
|
if (fgets(line, MAX_QUERY_BUFFER, source) == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
line[MAX_QUERY_BUFFER-1] = '\0';
|
|
|
|
len = strlen(line);
|
|
|
|
if (len == MAX_QUERY_BUFFER)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "line read exceeds maximum length. Truncating at %d\n", MAX_QUERY_BUFFER);
|
|
|
|
}
|
|
|
|
|
|
|
|
return line;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
1996-07-23 05:03:43 +02:00
|
|
|
* SendQuery:
|
|
|
|
SendQuery: send the query string to the backend
|
|
|
|
*
|
1996-07-09 08:22:35 +02:00
|
|
|
* return 0 if the query executed successfully
|
|
|
|
* returns 1 otherwise
|
|
|
|
*/
|
|
|
|
int
|
1996-07-23 05:03:43 +02:00
|
|
|
SendQuery(PGconn* db, char* query, PsqlSettings *settings)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
|
|
|
PGresult* results;
|
|
|
|
PGnotify* notify;
|
|
|
|
int status = 0;
|
|
|
|
|
|
|
|
if (settings->singleStep)
|
|
|
|
fprintf(stdout, "\n*******************************************************************************\n");
|
|
|
|
|
|
|
|
if (settings->echoQuery || settings->singleStep) {
|
|
|
|
fprintf(stderr,"QUERY: %s\n",query);
|
|
|
|
fflush(stderr);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (settings->singleStep) {
|
|
|
|
fprintf(stdout, "\n*******************************************************************************\n");
|
|
|
|
fflush(stdout);
|
|
|
|
printf("\npress return to continue ..\n");
|
|
|
|
gets_fromFile("",stdin);
|
|
|
|
}
|
|
|
|
|
1996-07-23 05:03:43 +02:00
|
|
|
results = PQexec(db, query);
|
1996-07-09 08:22:35 +02:00
|
|
|
if (results == NULL) {
|
1996-07-23 05:03:43 +02:00
|
|
|
fprintf(stderr,"%s",PQerrorMessage(db));
|
1996-07-09 08:22:35 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (PQresultStatus(results)) {
|
|
|
|
case PGRES_TUPLES_OK:
|
1996-07-23 05:03:43 +02:00
|
|
|
PQdisplayTuples(results,
|
|
|
|
settings->queryFout,
|
|
|
|
settings->fillAlign,
|
|
|
|
settings->fieldSep,
|
|
|
|
settings->printHeader,
|
|
|
|
settings->quiet);
|
1996-07-09 08:22:35 +02:00
|
|
|
PQclear(results);
|
|
|
|
break;
|
|
|
|
case PGRES_EMPTY_QUERY:
|
|
|
|
/* do nothing */
|
|
|
|
break;
|
|
|
|
case PGRES_COMMAND_OK:
|
|
|
|
if (!settings->quiet)
|
1996-07-23 05:03:43 +02:00
|
|
|
fprintf(stdout,"%s\n",PQcmdStatus(results));
|
1996-07-09 08:22:35 +02:00
|
|
|
break;
|
|
|
|
case PGRES_COPY_OUT:
|
|
|
|
handleCopyOut(results, settings->quiet);
|
|
|
|
break;
|
|
|
|
case PGRES_COPY_IN:
|
|
|
|
handleCopyIn(results, settings->quiet);
|
|
|
|
break;
|
|
|
|
case PGRES_NONFATAL_ERROR:
|
|
|
|
case PGRES_FATAL_ERROR:
|
|
|
|
case PGRES_BAD_RESPONSE:
|
|
|
|
status = 1;
|
1996-07-23 05:03:43 +02:00
|
|
|
fprintf(stderr,"%s",PQerrorMessage(db));
|
1996-07-09 08:22:35 +02:00
|
|
|
break;
|
1996-07-23 05:03:43 +02:00
|
|
|
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* check for asynchronous returns */
|
1996-07-23 05:03:43 +02:00
|
|
|
notify = PQnotifies(db);
|
1996-07-09 08:22:35 +02:00
|
|
|
if (notify) {
|
|
|
|
fprintf(stderr,"ASYNC NOTIFY of '%s' from backend pid '%d' received\n",
|
|
|
|
notify->relname, notify->be_pid);
|
|
|
|
free(notify);
|
|
|
|
}
|
|
|
|
|
|
|
|
return status;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
HandleSlashCmds:
|
|
|
|
|
|
|
|
Handles all the different commands that start with \
|
|
|
|
db_ptr is a pointer to the TgDb* structure
|
|
|
|
line is the current input line
|
|
|
|
prompt_ptr is a pointer to the prompt string,
|
|
|
|
a pointer is used because the prompt can be used with
|
|
|
|
a connection to a new database
|
|
|
|
returns a status:
|
|
|
|
0 - send currently constructed query to backend (i.e. we got a \g)
|
|
|
|
1 - skip processing of this line, continue building up query
|
|
|
|
2 - terminate processing of this query entirely
|
|
|
|
*/
|
|
|
|
int
|
1996-07-23 05:03:43 +02:00
|
|
|
HandleSlashCmds(PGconn** db_ptr,
|
1996-07-09 08:22:35 +02:00
|
|
|
char* line,
|
1996-07-23 05:03:43 +02:00
|
|
|
char** prompt_ptr,
|
|
|
|
char *query,
|
|
|
|
PsqlSettings *settings)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1996-07-23 05:03:43 +02:00
|
|
|
int status = 0;
|
|
|
|
PGconn* db = *db_ptr;
|
|
|
|
char* dbname = PQdb(db);
|
1996-07-09 08:22:35 +02:00
|
|
|
char *optarg = NULL;
|
|
|
|
int len;
|
|
|
|
|
|
|
|
len = strlen(line);
|
|
|
|
if (len > 2)
|
|
|
|
optarg = leftTrim(line+2);
|
|
|
|
switch (line[1])
|
|
|
|
{
|
1996-07-23 05:03:43 +02:00
|
|
|
case 'a': /* toggles to fill fields on output */
|
|
|
|
if (settings->fillAlign)
|
|
|
|
settings->fillAlign = 0;
|
|
|
|
else
|
|
|
|
settings->fillAlign = 1;
|
|
|
|
if (!settings->quiet)
|
|
|
|
fprintf(stderr,"turning %s fill-justification\n",
|
|
|
|
(settings->fillAlign) ? "on" : "off" );
|
1996-07-18 07:48:57 +02:00
|
|
|
break;
|
1996-07-23 05:03:43 +02:00
|
|
|
case 'c': /* \c means connect to new database */
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
|
|
|
if (!optarg) {
|
|
|
|
fprintf(stderr,"\\c must be followed by a database name\n");
|
1996-07-23 05:03:43 +02:00
|
|
|
status = 1;
|
1996-07-09 08:22:35 +02:00
|
|
|
break;
|
|
|
|
}
|
1996-07-23 05:03:43 +02:00
|
|
|
if (strcmp(optarg, dbname) == 0) {
|
|
|
|
fprintf(stderr,"already connected to %s\n", dbname);
|
|
|
|
status = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
PGconn *olddb;
|
1996-07-09 08:22:35 +02:00
|
|
|
|
1996-07-23 05:03:43 +02:00
|
|
|
printf("closing connection to database:%s\n", dbname);
|
|
|
|
olddb = db;
|
|
|
|
db = PQsetdb(PQhost(olddb), PQport(olddb), NULL, NULL, optarg);
|
|
|
|
*db_ptr = db;
|
1996-07-09 08:22:35 +02:00
|
|
|
printf("connecting to new database: %s\n", optarg);
|
1996-07-23 05:03:43 +02:00
|
|
|
if (PQstatus(db) == CONNECTION_BAD) {
|
|
|
|
fprintf(stderr,"%s\n", PQerrorMessage(db));
|
1996-07-09 08:22:35 +02:00
|
|
|
printf("reconnecting to %s\n", dbname);
|
1996-07-23 05:03:43 +02:00
|
|
|
db = PQsetdb(PQhost(olddb), PQport(olddb),
|
1996-07-09 08:22:35 +02:00
|
|
|
NULL, NULL, dbname);
|
1996-07-23 05:03:43 +02:00
|
|
|
*db_ptr = db;
|
|
|
|
if (PQstatus(db) == CONNECTION_BAD) {
|
1996-07-09 08:22:35 +02:00
|
|
|
fprintf(stderr,
|
1996-07-23 05:03:43 +02:00
|
|
|
"could not reconnect to %s. exiting\n", dbname);
|
1996-07-09 08:22:35 +02:00
|
|
|
exit(2);
|
|
|
|
}
|
1996-07-23 05:03:43 +02:00
|
|
|
status = 1;
|
1996-07-09 08:22:35 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
PQfinish(olddb);
|
1996-07-23 05:03:43 +02:00
|
|
|
free(*prompt_ptr);
|
|
|
|
*prompt_ptr = malloc(strlen(optarg) + 10);
|
|
|
|
sprintf(*prompt_ptr,"%s=> ", optarg);
|
|
|
|
status = 1;
|
1996-07-09 08:22:35 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'd': /* \d describe tables or columns in a table */
|
1996-07-23 05:03:43 +02:00
|
|
|
{
|
1996-07-09 08:22:35 +02:00
|
|
|
if (!optarg) {
|
1996-07-23 05:03:43 +02:00
|
|
|
tableList(db,0);
|
|
|
|
status = 1;
|
1996-07-09 08:22:35 +02:00
|
|
|
break;
|
1996-07-23 05:03:43 +02:00
|
|
|
}
|
|
|
|
if ( strcmp(optarg,"*") == 0 ) {
|
|
|
|
tableList(db, 0);
|
|
|
|
tableList(db, 1);
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
else {
|
1996-07-23 05:03:43 +02:00
|
|
|
tableDesc(db,optarg);
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
1996-07-23 05:03:43 +02:00
|
|
|
status = 1;
|
1996-07-09 08:22:35 +02:00
|
|
|
break;
|
1996-07-23 05:03:43 +02:00
|
|
|
}
|
1996-07-09 08:22:35 +02:00
|
|
|
case 'e':
|
|
|
|
{
|
1996-07-23 05:03:43 +02:00
|
|
|
char s[256];
|
1996-07-09 08:22:35 +02:00
|
|
|
int fd;
|
1996-07-18 07:48:57 +02:00
|
|
|
int ql = strlen(query);
|
1996-07-23 05:03:43 +02:00
|
|
|
int f_arg = 0;
|
|
|
|
int cc;
|
1996-07-09 08:22:35 +02:00
|
|
|
if (optarg)
|
1996-07-23 05:03:43 +02:00
|
|
|
{
|
|
|
|
f_arg = 1;
|
|
|
|
strcpy(s, optarg);
|
|
|
|
}
|
1996-07-09 08:22:35 +02:00
|
|
|
else
|
1996-07-23 05:03:43 +02:00
|
|
|
{
|
|
|
|
sprintf(s, "/tmp/psql.%d.%d", geteuid(), getpid());
|
|
|
|
unlink(s);
|
1996-07-09 08:22:35 +02:00
|
|
|
if (ql)
|
|
|
|
{
|
1996-07-23 05:03:43 +02:00
|
|
|
if ((fd=open(s, O_EXCL|O_CREAT|O_WRONLY, 0600))==-1)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1996-07-23 05:03:43 +02:00
|
|
|
perror(s);
|
1996-07-09 08:22:35 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (query[ql-1]!='\n')
|
|
|
|
strcat(query, "\n");
|
|
|
|
if (write(fd, query, ql)!=ql)
|
|
|
|
{
|
1996-07-23 05:03:43 +02:00
|
|
|
perror(s);
|
1996-07-09 08:22:35 +02:00
|
|
|
close(fd);
|
1996-07-23 05:03:43 +02:00
|
|
|
unlink(s);
|
1996-07-09 08:22:35 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
close(fd);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
{
|
1996-07-23 05:03:43 +02:00
|
|
|
char sys[256];
|
|
|
|
char *editorName;
|
|
|
|
editorName = getenv("EDITOR");
|
|
|
|
if (editorName == NULL)
|
|
|
|
editorName = DEFAULT_EDITOR;
|
|
|
|
sprintf(sys, "exec %s %s", editorName, s);
|
|
|
|
system(sys);
|
|
|
|
}
|
|
|
|
if ((fd=open(s, O_RDONLY))==-1)
|
|
|
|
{
|
|
|
|
if (!f_arg)
|
|
|
|
unlink(s);
|
1996-07-09 08:22:35 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
if ((cc=read(fd, query, MAX_QUERY_BUFFER))==-1)
|
|
|
|
{
|
1996-07-23 05:03:43 +02:00
|
|
|
perror(s);
|
1996-07-09 08:22:35 +02:00
|
|
|
close(fd);
|
1996-07-23 05:03:43 +02:00
|
|
|
if (!f_arg)
|
|
|
|
unlink(s);
|
1996-07-09 08:22:35 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
query[cc]='\0';
|
|
|
|
close(fd);
|
1996-07-23 05:03:43 +02:00
|
|
|
if (!f_arg)
|
|
|
|
unlink(s);
|
1996-07-09 08:22:35 +02:00
|
|
|
rightTrim(query);
|
|
|
|
if (query[strlen(query)-1]==';')
|
|
|
|
return 0;
|
|
|
|
break;
|
|
|
|
}
|
1996-07-23 05:03:43 +02:00
|
|
|
case 'f':
|
|
|
|
if (optarg)
|
|
|
|
strcpy(settings->fieldSep,optarg);
|
|
|
|
else
|
|
|
|
strcpy(settings->fieldSep,DEFAULT_FIELD_SEP);
|
|
|
|
break;
|
1996-07-18 07:48:57 +02:00
|
|
|
case 'g': /* \g means send query */
|
|
|
|
status = 0;
|
1996-07-23 05:03:43 +02:00
|
|
|
break;
|
|
|
|
case 'i': /* \i is include file */
|
|
|
|
{
|
|
|
|
FILE* fd;
|
|
|
|
|
|
|
|
if (!optarg) {
|
|
|
|
fprintf(stderr,"\\i must be followed by a file name\n");
|
|
|
|
status = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( (fd = fopen(optarg, "r")) == NULL)
|
|
|
|
{
|
|
|
|
fprintf(stderr,"file named %s could not be opened\n",optarg);
|
|
|
|
status = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
MainLoop(&db, fd, settings);
|
|
|
|
fclose(fd);
|
|
|
|
status = 1;
|
|
|
|
break;
|
|
|
|
}
|
1996-07-09 08:22:35 +02:00
|
|
|
case 'h':
|
|
|
|
{
|
|
|
|
char* cmd;
|
|
|
|
int i, numCmds;
|
|
|
|
int all_help = 0;
|
|
|
|
|
|
|
|
if (!optarg) {
|
|
|
|
printf("type \\h <cmd> where <cmd> is one of the following:\n");
|
|
|
|
i = 0;
|
|
|
|
while (QL_HELP[i].cmd != NULL)
|
|
|
|
{
|
|
|
|
printf("\t%s\n", QL_HELP[i].cmd);
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
printf("type \\h * for a complete description of all commands\n");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
cmd = optarg;
|
|
|
|
|
|
|
|
numCmds = 0;
|
|
|
|
while (QL_HELP[numCmds++].cmd != NULL);
|
|
|
|
|
|
|
|
numCmds = numCmds - 1;
|
|
|
|
|
1996-07-23 05:03:43 +02:00
|
|
|
if ( strcmp(cmd,"*") == 0 ) {
|
1996-07-09 08:22:35 +02:00
|
|
|
all_help=1;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i=0; i<numCmds;i++) {
|
|
|
|
if (strcmp(QL_HELP[i].cmd, cmd) == 0 || all_help) {
|
|
|
|
printf("Command: %s\n",QL_HELP[i].cmd);
|
|
|
|
printf("Description: %s\n", QL_HELP[i].help);
|
|
|
|
printf("Syntax:\n");
|
|
|
|
printf("%s\n", QL_HELP[i].syntax);
|
|
|
|
if ( all_help ) {
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (i == numCmds && ! all_help)
|
|
|
|
printf("command not found, try \\h with no arguments to see available help\n");
|
|
|
|
}
|
1996-07-23 05:03:43 +02:00
|
|
|
status = 1;
|
1996-07-09 08:22:35 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'l': /* \l is list database */
|
1996-07-23 05:03:43 +02:00
|
|
|
listAllDbs(db,settings);
|
|
|
|
status = 1;
|
1996-07-09 08:22:35 +02:00
|
|
|
break;
|
|
|
|
case 'o':
|
1996-07-23 05:03:43 +02:00
|
|
|
settings->queryFout = setFout(optarg);
|
1996-07-09 08:22:35 +02:00
|
|
|
break;
|
|
|
|
case 'p':
|
|
|
|
if (query) {
|
|
|
|
fputs(query, stdout);
|
|
|
|
fputc('\n', stdout);
|
1996-07-23 05:03:43 +02:00
|
|
|
status = 1;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'q': /* \q is quit */
|
|
|
|
status = 2;
|
|
|
|
break;
|
1996-07-18 07:48:57 +02:00
|
|
|
case 'r':
|
1996-07-23 05:03:43 +02:00
|
|
|
query[0] = '\0';
|
|
|
|
status = 1;
|
|
|
|
break;
|
|
|
|
case 's': /* \s is save history to a file */
|
|
|
|
{
|
|
|
|
char* fname;
|
|
|
|
|
|
|
|
if (!optarg) {
|
|
|
|
fprintf(stderr,"\\s must be followed by a file name\n");
|
|
|
|
status = 1;
|
|
|
|
break;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
1996-07-23 05:03:43 +02:00
|
|
|
|
|
|
|
fname = optarg;
|
|
|
|
if (write_history(fname) != 0)
|
|
|
|
{
|
|
|
|
fprintf(stderr,"cannot write history to %s\n",fname);
|
|
|
|
}
|
|
|
|
status = 1;
|
1996-07-09 08:22:35 +02:00
|
|
|
break;
|
|
|
|
}
|
1996-07-23 05:03:43 +02:00
|
|
|
case 't':
|
|
|
|
if ( settings->printHeader )
|
|
|
|
settings->printHeader = 0;
|
|
|
|
else
|
|
|
|
settings->printHeader = 1;
|
1996-07-18 07:48:57 +02:00
|
|
|
if (!settings->quiet)
|
1996-07-23 05:03:43 +02:00
|
|
|
fprintf(stderr,"turning %s printing of field headers\n",
|
|
|
|
(settings->printHeader) ? "on" : "off" );
|
|
|
|
break;
|
1996-07-18 07:48:57 +02:00
|
|
|
case '!':
|
|
|
|
if (!optarg) {
|
1996-07-23 05:03:43 +02:00
|
|
|
char sys[256];
|
|
|
|
char *shellName;
|
|
|
|
shellName = getenv("SHELL");
|
|
|
|
if (shellName == NULL)
|
|
|
|
shellName = DEFAULT_SHELL;
|
|
|
|
sprintf(sys,"exec %s", shellName);
|
|
|
|
system(sys);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
system(optarg);
|
1996-07-09 08:22:35 +02:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
case '?': /* \? is help */
|
1996-07-23 05:03:43 +02:00
|
|
|
slashUsage();
|
|
|
|
status = 1;
|
1996-07-09 08:22:35 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
MainLoop: main processing loop for reading lines of input
|
|
|
|
and sending them to the backend
|
|
|
|
|
|
|
|
this loop is re-entrant. May be called by \i command
|
|
|
|
which reads input from a file
|
|
|
|
|
|
|
|
*db_ptr must be initialized and set
|
|
|
|
*/
|
|
|
|
int
|
1996-07-23 05:03:43 +02:00
|
|
|
MainLoop(PGconn** db_ptr,
|
|
|
|
FILE* source,
|
|
|
|
PsqlSettings *settings)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1996-07-23 05:03:43 +02:00
|
|
|
char* prompt; /* readline prompt */
|
1996-07-09 08:22:35 +02:00
|
|
|
char* line; /* line of input*/
|
|
|
|
int len; /* length of the line */
|
|
|
|
char query[MAX_QUERY_BUFFER]; /* multi-line query storage */
|
1996-07-23 05:03:43 +02:00
|
|
|
PGconn* db = *db_ptr;
|
|
|
|
char* dbname = PQdb(db);
|
1996-07-09 08:22:35 +02:00
|
|
|
int exitStatus = 0;
|
1996-07-23 05:03:43 +02:00
|
|
|
|
1996-07-09 08:22:35 +02:00
|
|
|
int slashCmdStatus = 0;
|
|
|
|
/* slashCmdStatus can be:
|
|
|
|
0 - send currently constructed query to backend (i.e. we got a \g)
|
|
|
|
1 - skip processing of this line, continue building up query
|
|
|
|
2 - terminate processing of this query entirely
|
|
|
|
*/
|
|
|
|
|
1996-07-23 05:03:43 +02:00
|
|
|
int send_query = 0;
|
|
|
|
int interactive;
|
1996-07-09 08:22:35 +02:00
|
|
|
READ_ROUTINE GetNextLine;
|
|
|
|
|
1996-07-23 05:03:43 +02:00
|
|
|
interactive = (source == stdin);
|
|
|
|
|
1996-07-09 08:22:35 +02:00
|
|
|
if (interactive) {
|
1996-07-23 05:03:43 +02:00
|
|
|
prompt = malloc(strlen(dbname) + 10);
|
1996-07-09 08:22:35 +02:00
|
|
|
if (settings->quiet)
|
1996-07-23 05:03:43 +02:00
|
|
|
prompt[0] = '\0';
|
1996-07-09 08:22:35 +02:00
|
|
|
else
|
1996-07-23 05:03:43 +02:00
|
|
|
sprintf(prompt,"%s=> ", dbname);
|
1996-07-09 08:22:35 +02:00
|
|
|
if (settings->useReadline) {
|
|
|
|
using_history();
|
|
|
|
GetNextLine = gets_readline;
|
|
|
|
} else
|
|
|
|
GetNextLine = gets_noreadline;
|
|
|
|
|
|
|
|
}
|
|
|
|
else
|
|
|
|
GetNextLine = gets_fromFile;
|
|
|
|
|
|
|
|
query[0] = '\0';
|
|
|
|
|
|
|
|
/* main loop for getting queries and executing them */
|
1996-07-23 05:03:43 +02:00
|
|
|
while ((line = GetNextLine(prompt, source)) != NULL)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
|
|
|
exitStatus = 0;
|
|
|
|
line = rightTrim(line); /* remove whitespaces on the right, incl. \n's */
|
|
|
|
|
|
|
|
if (line[0] == '\0') {
|
|
|
|
free(line);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* filter out comment lines that begin with --,
|
|
|
|
this could be incorrect if -- is part of a quoted string.
|
|
|
|
But we won't go through the trouble of detecting that. If you have
|
|
|
|
-- in your quoted string, be careful and don't start a line with it*/
|
|
|
|
if (line[0] == '-' && line[1] == '-') {
|
|
|
|
if (settings->singleStep) /* in single step mode, show comments */
|
|
|
|
fprintf(stdout,"%s\n",line);
|
|
|
|
free(line);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
len = strlen(line);
|
|
|
|
|
|
|
|
if (interactive && settings->useReadline)
|
|
|
|
add_history(line); /* save non-empty lines in history */
|
|
|
|
|
|
|
|
/* do the query immediately if we are doing single line queries
|
|
|
|
or if the last character is a semicolon */
|
1996-07-23 05:03:43 +02:00
|
|
|
send_query = settings->singleLineMode || (line[len-1] == ';') ;
|
1996-07-09 08:22:35 +02:00
|
|
|
|
|
|
|
/* normally, \ commands have to be start the line,
|
|
|
|
but for backwards compatibility with monitor,
|
|
|
|
check for \g at the end of line */
|
1996-07-23 05:03:43 +02:00
|
|
|
if (len > 2 && !send_query)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
|
|
|
if (line[len-1]=='g' && line[len-2]=='\\')
|
|
|
|
{
|
1996-07-23 05:03:43 +02:00
|
|
|
send_query = 1;
|
1996-07-09 08:22:35 +02:00
|
|
|
line[len-2]='\0';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* slash commands have to be on their own line */
|
|
|
|
if (line[0] == '\\') {
|
1996-07-23 05:03:43 +02:00
|
|
|
slashCmdStatus = HandleSlashCmds(db_ptr,
|
1996-07-09 08:22:35 +02:00
|
|
|
line,
|
1996-07-23 05:03:43 +02:00
|
|
|
&prompt,
|
|
|
|
query,
|
|
|
|
settings);
|
|
|
|
db = *db_ptr; /* in case \c changed the database */
|
1996-07-09 08:22:35 +02:00
|
|
|
if (slashCmdStatus == 1)
|
|
|
|
continue;
|
|
|
|
if (slashCmdStatus == 2)
|
|
|
|
break;
|
|
|
|
if (slashCmdStatus == 0)
|
1996-07-23 05:03:43 +02:00
|
|
|
send_query = 1;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
if (strlen(query) + len > MAX_QUERY_BUFFER)
|
|
|
|
{
|
|
|
|
fprintf(stderr,"query buffer max length of %d exceeded\n",MAX_QUERY_BUFFER);
|
|
|
|
fprintf(stderr,"query line ignored\n");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
if (query[0]!='\0') {
|
|
|
|
strcat(query,"\n");
|
|
|
|
strcat(query,line);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
strcpy(query,line);
|
|
|
|
|
1996-07-23 05:03:43 +02:00
|
|
|
if (send_query && query[0] != '\0')
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
|
|
|
/* echo the line read from the file,
|
|
|
|
unless we are in single_step mode, because single_step mode
|
|
|
|
will echo anyway */
|
1996-07-23 05:03:43 +02:00
|
|
|
if (!interactive && !settings->singleStep)
|
|
|
|
fprintf(stderr,"%s\n",query);
|
1996-07-09 08:22:35 +02:00
|
|
|
|
1996-07-23 05:03:43 +02:00
|
|
|
exitStatus = SendQuery(db, query, settings);
|
|
|
|
query[0] = '\0';
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
free(line); /* free storage malloc'd by GetNextLine */
|
|
|
|
} /* while */
|
1996-07-23 05:03:43 +02:00
|
|
|
return exitStatus;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char** argv)
|
|
|
|
{
|
|
|
|
extern char* optarg;
|
1996-07-23 05:03:43 +02:00
|
|
|
extern int optind, opterr;
|
1996-07-09 08:22:35 +02:00
|
|
|
|
1996-07-23 05:03:43 +02:00
|
|
|
PGconn *db;
|
1996-07-09 08:22:35 +02:00
|
|
|
char* dbname = NULL;
|
|
|
|
char* host = NULL;
|
|
|
|
char* port = NULL;
|
|
|
|
char* qfilename = NULL;
|
|
|
|
char errbuf[ERROR_MSG_LENGTH];
|
|
|
|
|
|
|
|
PsqlSettings settings;
|
|
|
|
|
|
|
|
char* singleQuery = NULL;
|
|
|
|
|
1996-07-23 05:03:43 +02:00
|
|
|
int listDatabases = 0 ;
|
1996-07-09 08:22:35 +02:00
|
|
|
int exitStatus = 0;
|
1996-07-23 05:03:43 +02:00
|
|
|
int singleSlashCmd = 0;
|
1996-07-09 08:22:35 +02:00
|
|
|
int c;
|
|
|
|
|
1996-07-23 05:03:43 +02:00
|
|
|
|
|
|
|
#ifdef NOREADLINE
|
|
|
|
settings.useReadline = 0;
|
|
|
|
#else
|
|
|
|
settings.useReadline = 1;
|
1996-07-18 07:48:57 +02:00
|
|
|
#endif
|
1996-07-09 08:22:35 +02:00
|
|
|
|
1996-07-23 05:03:43 +02:00
|
|
|
settings.quiet = 0;
|
|
|
|
settings.fillAlign = 1;
|
|
|
|
settings.printHeader = 1;
|
|
|
|
settings.echoQuery = 0;
|
|
|
|
settings.singleStep = 0;
|
|
|
|
settings.singleLineMode = 0;
|
|
|
|
settings.queryFout = stdout;
|
|
|
|
strcpy(settings.fieldSep, DEFAULT_FIELD_SEP);
|
|
|
|
|
|
|
|
while ((c = getopt(argc, argv, "Aa:c:d:ef:F:lhH:nso:p:qST")) != EOF) {
|
1996-07-09 08:22:35 +02:00
|
|
|
switch (c) {
|
|
|
|
case 'A':
|
1996-07-23 05:03:43 +02:00
|
|
|
settings.fillAlign = 0;
|
1996-07-09 08:22:35 +02:00
|
|
|
break;
|
|
|
|
case 'a':
|
|
|
|
fe_setauthsvc(optarg, errbuf);
|
|
|
|
break;
|
|
|
|
case 'c':
|
|
|
|
singleQuery = optarg;
|
|
|
|
if ( singleQuery[0] == '\\' ) {
|
|
|
|
singleSlashCmd=1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'd':
|
|
|
|
dbname = optarg;
|
|
|
|
break;
|
|
|
|
case 'e':
|
|
|
|
settings.echoQuery = 1;
|
|
|
|
break;
|
|
|
|
case 'f':
|
|
|
|
qfilename = optarg;
|
|
|
|
break;
|
|
|
|
case 'F':
|
1996-07-23 05:03:43 +02:00
|
|
|
strncpy(settings.fieldSep,optarg,MAX_FIELD_SEP_LENGTH);
|
|
|
|
break;
|
1996-07-09 08:22:35 +02:00
|
|
|
case 'l':
|
|
|
|
listDatabases = 1;
|
|
|
|
break;
|
1996-07-18 07:48:57 +02:00
|
|
|
case 'H':
|
1996-07-23 05:03:43 +02:00
|
|
|
host = optarg;
|
1996-07-18 07:48:57 +02:00
|
|
|
break;
|
1996-07-09 08:22:35 +02:00
|
|
|
case 'n':
|
1996-07-23 05:03:43 +02:00
|
|
|
settings.useReadline = 0;
|
|
|
|
break;
|
1996-07-09 08:22:35 +02:00
|
|
|
case 'o':
|
1996-07-23 05:03:43 +02:00
|
|
|
settings.queryFout = setFout(optarg);
|
|
|
|
break;
|
1996-07-09 08:22:35 +02:00
|
|
|
case 'p':
|
|
|
|
port = optarg;
|
|
|
|
break;
|
|
|
|
case 'q':
|
|
|
|
settings.quiet = 1;
|
|
|
|
break;
|
|
|
|
case 's':
|
|
|
|
settings.singleStep = 1;
|
|
|
|
break;
|
|
|
|
case 'S':
|
|
|
|
settings.singleLineMode = 1;
|
|
|
|
break;
|
|
|
|
case 'T':
|
1996-07-23 05:03:43 +02:00
|
|
|
settings.printHeader = 0;
|
|
|
|
break;
|
|
|
|
case 'h':
|
1996-07-09 08:22:35 +02:00
|
|
|
default:
|
|
|
|
usage(argv[0]);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* if we still have an argument, use it as the database name */
|
|
|
|
if (argc - optind == 1)
|
|
|
|
dbname = argv[optind];
|
|
|
|
|
|
|
|
if (listDatabases)
|
|
|
|
dbname = "template1";
|
|
|
|
|
1996-07-23 05:03:43 +02:00
|
|
|
db = PQsetdb(host, port, NULL, NULL, dbname);
|
|
|
|
dbname = PQdb(db);
|
1996-07-09 08:22:35 +02:00
|
|
|
|
1996-07-23 05:03:43 +02:00
|
|
|
if (PQstatus(db) == CONNECTION_BAD) {
|
1996-07-09 08:22:35 +02:00
|
|
|
fprintf(stderr,"Connection to database '%s' failed.\n", dbname);
|
1996-07-23 05:03:43 +02:00
|
|
|
fprintf(stderr,"%s",PQerrorMessage(db));
|
1996-07-09 08:22:35 +02:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
if (listDatabases) {
|
1996-07-23 05:03:43 +02:00
|
|
|
exit(listAllDbs(db,&settings));
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!settings.quiet && !singleQuery && !qfilename) {
|
|
|
|
printf("Welcome to the POSTGRES95 interactive sql monitor:\n");
|
|
|
|
printf(" Please read the file COPYRIGHT for copyright terms of POSTGRES95\n\n");
|
|
|
|
printf(" type \\? for help on slash commands\n");
|
|
|
|
printf(" type \\q to quit\n");
|
|
|
|
printf(" type \\g or terminate with semicolon to execute query\n");
|
|
|
|
printf(" You are currently connected to the database: %s\n\n", dbname);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (qfilename || singleSlashCmd) {
|
|
|
|
/* read in a file full of queries instead of reading in queries
|
|
|
|
interactively */
|
|
|
|
char *line;
|
1996-07-23 05:03:43 +02:00
|
|
|
char prompt[100];
|
1996-07-09 08:22:35 +02:00
|
|
|
|
|
|
|
if ( singleSlashCmd ) {
|
|
|
|
/* Not really a query, but "Do what I mean, not what I say." */
|
|
|
|
line = singleQuery;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
line = malloc(strlen(qfilename) + 5);
|
|
|
|
sprintf(line,"\\i %s", qfilename);
|
|
|
|
}
|
1996-07-23 05:03:43 +02:00
|
|
|
HandleSlashCmds(&db, line, (char**)prompt, "", &settings);
|
1996-07-09 08:22:35 +02:00
|
|
|
|
|
|
|
} else {
|
|
|
|
if (singleQuery) {
|
1996-07-23 05:03:43 +02:00
|
|
|
exitStatus = SendQuery(db, singleQuery, &settings);
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
else
|
1996-07-23 05:03:43 +02:00
|
|
|
exitStatus = MainLoop(&db, stdin, &settings);
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
1996-07-23 05:03:43 +02:00
|
|
|
PQfinish(db);
|
1996-07-09 08:22:35 +02:00
|
|
|
|
|
|
|
return exitStatus;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
1996-07-23 05:03:43 +02:00
|
|
|
handleCopyOut(PGresult *res, int quiet)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
|
|
|
bool copydone = false;
|
|
|
|
char copybuf[COPYBUFSIZ];
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (!quiet)
|
|
|
|
fprintf(stdout, "Copy command returns...\n");
|
|
|
|
|
|
|
|
while (!copydone) {
|
|
|
|
ret = PQgetline(res->conn, copybuf, COPYBUFSIZ);
|
|
|
|
|
|
|
|
if (copybuf[0] == '.' && copybuf[1] =='\0') {
|
|
|
|
copydone = true; /* don't print this... */
|
|
|
|
} else {
|
|
|
|
fputs(copybuf, stdout);
|
|
|
|
switch (ret) {
|
|
|
|
case EOF:
|
|
|
|
copydone = true;
|
|
|
|
/*FALLTHROUGH*/
|
|
|
|
case 0:
|
|
|
|
fputc('\n', stdout);
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fflush(stdout);
|
|
|
|
PQendcopy(res->conn);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
1996-07-23 05:03:43 +02:00
|
|
|
handleCopyIn(PGresult *res, int quiet)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
|
|
|
bool copydone = false;
|
|
|
|
bool firstload;
|
|
|
|
bool linedone;
|
|
|
|
char copybuf[COPYBUFSIZ];
|
|
|
|
char *s;
|
|
|
|
int buflen;
|
|
|
|
int c;
|
|
|
|
|
|
|
|
if (!quiet) {
|
|
|
|
fputs("Enter info followed by a newline\n", stdout);
|
|
|
|
fputs("End with a dot on a line by itself.\n", stdout);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* eat extra newline still in input buffer
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
fflush(stdin);
|
|
|
|
if ((c = getc(stdin)) != '\n' && c != EOF) {
|
|
|
|
(void) ungetc(c, stdin);
|
|
|
|
}
|
|
|
|
|
|
|
|
while (!copydone) { /* for each input line ... */
|
|
|
|
if (!quiet) {
|
|
|
|
fputs(">> ", stdout);
|
|
|
|
fflush(stdout);
|
|
|
|
}
|
|
|
|
firstload = true;
|
|
|
|
linedone = false;
|
|
|
|
while (!linedone) { /* for each buffer ... */
|
|
|
|
s = copybuf;
|
|
|
|
buflen = COPYBUFSIZ;
|
|
|
|
for (; buflen > 1 &&
|
|
|
|
!(linedone = (c = getc(stdin)) == '\n' || c == EOF);
|
|
|
|
--buflen) {
|
|
|
|
*s++ = c;
|
|
|
|
}
|
|
|
|
if (c == EOF) {
|
|
|
|
/* reading from stdin, but from a file */
|
|
|
|
PQputline(res->conn, ".");
|
|
|
|
copydone = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
*s = '\0';
|
|
|
|
PQputline(res->conn, copybuf);
|
|
|
|
if (firstload) {
|
|
|
|
if (!strcmp(copybuf, ".")) {
|
|
|
|
copydone = true;
|
|
|
|
}
|
|
|
|
firstload = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
PQputline(res->conn, "\n");
|
|
|
|
}
|
|
|
|
PQendcopy(res->conn);
|
|
|
|
}
|
|
|
|
|
1996-07-23 05:03:43 +02:00
|
|
|
|
1996-07-09 08:22:35 +02:00
|
|
|
/* try to open fname and return a FILE*,
|
|
|
|
if it fails, use stdout, instead */
|
|
|
|
FILE*
|
1996-07-23 05:03:43 +02:00
|
|
|
setFout(char *fname)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1996-07-23 05:03:43 +02:00
|
|
|
FILE *queryFout;
|
|
|
|
|
|
|
|
if (!fname)
|
|
|
|
queryFout = stdout;
|
|
|
|
else {
|
|
|
|
queryFout = fopen(fname, "w");
|
|
|
|
if (!queryFout) {
|
|
|
|
perror(fname);
|
|
|
|
queryFout = stdout;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
1996-07-23 05:03:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return queryFout;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
1996-07-23 05:03:43 +02:00
|
|
|
|