From 971961f232c90b86365bbef0e665b0c482b02dfd Mon Sep 17 00:00:00 2001 From: Brian Boru Date: Sat, 13 Aug 2022 19:25:13 +0300 Subject: [PATCH] fix --- Jamrules.configure | 12 +++++++-- include/libpgcli/pgconn.h | 2 +- util/pgsql.c | 63 +++++++++++++++++++++++++---------------------- 3 files changed, 45 insertions(+), 32 deletions(-) diff --git a/Jamrules.configure b/Jamrules.configure index a4543f2..33aa447 100644 --- a/Jamrules.configure +++ b/Jamrules.configure @@ -127,9 +127,17 @@ rule -configure- { if [ -configure-test-gmp- ] == 1 { -configure-add-line- "CFLAGS.all += -DHAVE_GMP ;" ; -configure-add-line- "LINKLIBS.all += -lgmp ;" ; - Command "sed -e \"s/@prefix/\\$(pp)/\" -e \"s/@cflags/-DHAVE_GMP/\" -e \"s/@arch_lib/\\$(pl)/g\" ./libpgcli.pc.in > ./libpgcli.pc" ; + if $(ARCH_LIB) { + Command "sed -e \"s/@prefix/\\$(pp)/\" -e \"s/@cflags/-DHAVE_GMP/\" -e \"s/@arch_lib/\\$(pl)/g\" ./libpgcli.pc.in > ./libpgcli.pc" ; + } else { + Command "sed -e \"s/@prefix/\\$(pp)/\" -e \"s/@cflags/-DHAVE_GMP/\" -e \"s/@arch_lib//g\" ./libpgcli.pc.in > ./libpgcli.pc" ; + } } else { - Command "sed -e \"s/@prefix/\\$(pp)/\" -e \"s/@cflags//\" -e \"s/@arch_lib/\\$(pl)/g\" ./libpgcli.pc.in > ./libpgcli.pc" ; + if $(ARCH_LIB) { + Command "sed -e \"s/@prefix/\\$(pp)/\" -e \"s/@cflags//\" -e \"s/@arch_lib/\\$(pl)/g\" ./libpgcli.pc.in > ./libpgcli.pc" ; + } else { + Command "sed -e \"s/@prefix/\\$(pp)/\" -e \"s/@cflags//\" -e \"s/@arch_lib//g\" ./libpgcli.pc.in > ./libpgcli.pc" ; + } } if $(BUILDDEBUG) { if $(CC) == "gcc" { diff --git a/include/libpgcli/pgconn.h b/include/libpgcli/pgconn.h index 4e68a04..28b655c 100644 --- a/include/libpgcli/pgconn.h +++ b/include/libpgcli/pgconn.h @@ -111,7 +111,7 @@ void pg_close (pgconn_t *conn); void pg_disconnect(pgconn_t *conn); static inline int pg_nrecs (pgconn_t *conn) { return PG_NRECS(conn); }; -static inline int pg_nflds (pgconn_t *conn) { return PG_NFLDS(conn); }; +static inline int pg_nflds (pgconn_t *conn) { return conn->rowdesc ? PG_NFLDS(conn) : 0; }; static inline int pg_isnull (pgconn_t *conn, int row, int col) { return PG_ISNULL(conn, row, col); }; #define pg_getdate(C,R,F) pg_geti32(C,R,F) diff --git a/util/pgsql.c b/util/pgsql.c index f02f4b8..dcbdbf9 100644 --- a/util/pgsql.c +++ b/util/pgsql.c @@ -34,38 +34,41 @@ static void exec_sql (pgconn_t *conn, const char *sql, size_t sql_len) { print_error(conn); return; } - int *lens = malloc(sizeof(int) * pg_nflds(conn)); - for (int i = 0; i < pg_nflds(conn); ++i) - lens[i] = sizeof(NULL_STR)-1; - for (int i = 0; i < pg_nrecs(conn); ++i) { - for (int j = 0; j < pg_nflds(conn); ++j) { - if (!pg_isnull(conn, i, j)) { - strptr_t s = pg_getstr(conn, i, j); - if (lens[j] < s.len) lens[j] = s.len; - free(s.ptr); + if (pg_nflds(conn) > 0) { + int *lens = malloc(sizeof(int) * pg_nflds(conn)); + for (int i = 0; i < pg_nflds(conn); ++i) + lens[i] = sizeof(NULL_STR)-1; + for (int i = 0; i < pg_nrecs(conn); ++i) { + for (int j = 0; j < pg_nflds(conn); ++j) { + if (!pg_isnull(conn, i, j)) { + strptr_t s = pg_getstr(conn, i, j); + if (lens[j] < s.len) lens[j] = s.len; + free(s.ptr); + } } } - } - for (int i = 0; i < pg_nrecs(conn); ++i) { - for (int j = 0; j < pg_nflds(conn); ++j) { - str_t *str; - if (pg_isnull(conn, i, j)) - str = mkstr(CONST_STR_LEN(NULL_STR), 0); - else { - strptr_t s = pg_getstr(conn, i, j); - str = mkstr(s.ptr, s.len, 0); - free(s.ptr); + for (int i = 0; i < pg_nrecs(conn); ++i) { + for (int j = 0; j < pg_nflds(conn); ++j) { + str_t *str; + if (pg_isnull(conn, i, j)) + str = mkstr(CONST_STR_LEN(NULL_STR), 0); + else { + strptr_t s = pg_getstr(conn, i, j); + str = mkstr(s.ptr, s.len, 0); + free(s.ptr); + } + strpad(&str, lens[j], ' ', STR_LEFT); + if (0 == j) + printf("|%s|", str->ptr); + else + printf("%s|", str->ptr); + free(str); } - strpad(&str, lens[j], ' ', STR_LEFT); - if (0 == j) - printf("|%s|", str->ptr); - else - printf("%s|", str->ptr); - free(str); + printf("\n"); } - printf("\n"); + free(lens); } - free(lens); + printf("%s\n", conn->complete->tag); pg_close(conn); } @@ -73,7 +76,6 @@ int main (int argc, const char *argv[]) { if (argc < 2 || argc > 3) usage(argv[0]); pgconn_t *conn = pg_connect(argv[1]); if (0 == print_error(conn) && PG_IDLE == pg_ready(conn)) { - print_server_state(conn); int is_quit = 0; if (3 == argc) exec_sql(conn, argv[2], strlen(argv[2])); @@ -84,9 +86,12 @@ int main (int argc, const char *argv[]) { if (sql) strnadd(&sql, line, strlen(line)); else { - if (!strcmp(line, "quit")) { + if (!strcmp(line, "quit") || !strcmp(line, "exit")) { is_quit = 1; break; + } else if (!strcmp(line, "server_state")) { + print_server_state(conn); + continue; } sql = mkstr(line, strlen(line), 0); } -- 2.11.4.GIT