diff --git a/.github/workflows/regression.yml b/.github/workflows/regression.yml index 22ac213..becbd2c 100644 --- a/.github/workflows/regression.yml +++ b/.github/workflows/regression.yml @@ -1,20 +1,23 @@ name: Integration Testing and Analysis +# on: +# pull_request: +# branches: +# - main +# - develop +# - version* +# push: +# branches: +# - main +# - develop +# - version* +# tags: +# - robot* +# - regression* +# - integration* + on: - pull_request: - branches: - - main - - develop - - version* - push: - branches: - - main - - develop - - version* - tags: - - robot* - - regression* - - integration* + workflow_dispatch: # Only manually triggered now env: GO_VERSION: '^1.22' diff --git a/.gitignore b/.gitignore index f5de2ed..9f68936 100644 --- a/.gitignore +++ b/.gitignore @@ -6,8 +6,10 @@ stackql*.zip stackql*.pkg stackql_history.txt stackql.log +stackql_server.log stackql-core/ stackql-any-sdk/ stackql-stackql-provider-registry/ .env nohup.out +ssl-test/ \ No newline at end of file diff --git a/Cargo.lock b/Cargo.lock index 6f1a675..1aa8075 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,21 +2,6 @@ # It is not intended for manual editing. version = 4 -[[package]] -name = "addr2line" -version = "0.24.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dfbe277e56a376000877090da837660b4427aad530e3028d44e0bffe4f89a1c1" -dependencies = [ - "gimli", -] - -[[package]] -name = "adler2" -version = "2.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "512761e0bb2578dd7380c6baaa0f4ce03e84f95e960231d1dec8bf4d7d6e2627" - [[package]] name = "aho-corasick" version = "1.1.3" @@ -26,94 +11,6 @@ dependencies = [ "memchr", ] -[[package]] -name = "anstream" -version = "0.6.18" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8acc5369981196006228e28809f761875c0327210a891e941f4c683b3a99529b" -dependencies = [ - "anstyle", - "anstyle-parse", - "anstyle-query", - "anstyle-wincon", - "colorchoice", - "is_terminal_polyfill", - "utf8parse", -] - -[[package]] -name = "anstyle" -version = "1.0.10" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "55cc3b69f167a1ef2e161439aa98aed94e6028e5f9a59be9a6ffb47aef1651f9" - -[[package]] -name = "anstyle-parse" -version = "0.2.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3b2d16507662817a6a20a9ea92df6652ee4f94f914589377d69f3b21bc5798a9" -dependencies = [ - "utf8parse", -] - -[[package]] -name = "anstyle-query" -version = "1.1.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "79947af37f4177cfead1110013d678905c37501914fba0efea834c3fe9a8d60c" -dependencies = [ - "windows-sys 0.59.0", -] - -[[package]] -name = "anstyle-wincon" -version = "3.0.7" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ca3534e77181a9cc07539ad51f2141fe32f6c3ffd4df76db8ad92346b003ae4e" -dependencies = [ - "anstyle", - "once_cell", - "windows-sys 0.59.0", -] - -[[package]] -name = "async-trait" -version = "0.1.88" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e539d3fca749fcee5236ab05e93a52867dd549cc157c8cb7f99595f3cedffdb5" -dependencies = [ - "proc-macro2", - "quote", - "syn 2.0.100", -] - -[[package]] -name = "autocfg" -version = "1.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ace50bade8e6234aa140d9a2f552bbee1db4d353f69b8217bc503490fc1a9f26" - -[[package]] -name = "backtrace" -version = "0.3.74" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8d82cb332cdfaed17ae235a638438ac4d4839913cc2af585c3c6746e8f8bee1a" -dependencies = [ - "addr2line", - "cfg-if", - "libc", - "miniz_oxide", - "object", - "rustc-demangle", - "windows-targets", -] - -[[package]] -name = "base64" -version = "0.22.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "72b3254f16251a8381aa12e40e3c4d2f0199f8c6508fbecb9d91f575e0fbb8c6" - [[package]] name = "bindgen" version = "0.64.0" @@ -148,33 +45,6 @@ version = "2.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5c8214115b7bf84099f1309324e63141d4c5d7cc26862f97a0a857dbefe165bd" -[[package]] -name = "block-buffer" -version = "0.10.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3078c7629b62d3f0439517fa394996acacc5cbc91c5a20d8c658e77abd503a71" -dependencies = [ - "generic-array", -] - -[[package]] -name = "bumpalo" -version = "3.17.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1628fb46dfa0b37568d12e5edd512553eccf6a22a78e8bde00bb4aed84d5bdbf" - -[[package]] -name = "byteorder" -version = "1.5.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b" - -[[package]] -name = "bytes" -version = "1.10.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d71b6127be86fdcfddb610f7182ac57211d4b18a3e9c82eb2d17662f2227ad6a" - [[package]] name = "cexpr" version = "0.6.0" @@ -202,54 +72,10 @@ dependencies = [ ] [[package]] -name = "client_test_harness" +name = "colorize" version = "0.1.0" -dependencies = [ - "env_logger", - "futures", - "libpq", - "libpq-sys", - "log", - "postgres", - "tokio", - "tokio-postgres", -] - -[[package]] -name = "colorchoice" -version = "1.0.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5b63caa9aa9397e2d9480a9b13673856c78d8ac123288526c37d7839f2a86990" - -[[package]] -name = "cpufeatures" -version = "0.2.17" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "59ed5838eebb26a2bb2e58f6d5b5316989ae9d08bab10e0e6d103e656d1b0280" -dependencies = [ - "libc", -] - -[[package]] -name = "crypto-common" -version = "0.1.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1bfb12502f3fc46cca1bb51ac28df9d618d813cdc3d2f25b9fe775a34af26bb3" -dependencies = [ - "generic-array", - "typenum", -] - -[[package]] -name = "digest" -version = "0.10.7" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9ed9a281f7bc9b7576e61468ba615a66a5c8cfdff42420a70aa82701a3b1e292" -dependencies = [ - "block-buffer", - "crypto-common", - "subtle", -] +checksum = "dc17e449bc7854c50b943d113a98bc0e01dc6585d2c66eaa09ca645ebd8a7e62" [[package]] name = "either" @@ -257,162 +83,29 @@ version = "1.15.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "48c757948c5ede0e46177b7add2e67155f70e33c07fea8284df6576da70b3719" -[[package]] -name = "env_filter" -version = "0.1.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "186e05a59d4c50738528153b83b0b0194d3a29507dfec16eccd4b342903397d0" -dependencies = [ - "log", - "regex", -] - [[package]] name = "env_logger" -version = "0.11.8" +version = "0.10.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "13c863f0904021b108aa8b2f55046443e6b1ebde8fd4a15c399893aae4fa069f" +checksum = "4cd405aab171cb85d6735e5c8d9db038c17d3ca007a4d2c25f337935c3d90580" dependencies = [ - "anstream", - "anstyle", - "env_filter", - "jiff", + "humantime", + "is-terminal", "log", + "regex", + "termcolor", ] [[package]] name = "errno" -version = "0.3.10" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "33d852cb9b869c2a9b3df2f71a3074817f01e1844f839a144f5fcef059a4eb5d" -dependencies = [ - "libc", - "windows-sys 0.59.0", -] - -[[package]] -name = "fallible-iterator" -version = "0.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4443176a9f2c162692bd3d352d745ef9413eec5782a80d8fd6f8a1ac692a07f7" - -[[package]] -name = "futures" -version = "0.3.31" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "65bc07b1a8bc7c85c5f2e110c476c7389b4554ba72af57d8445ea63a576b0876" -dependencies = [ - "futures-channel", - "futures-core", - "futures-executor", - "futures-io", - "futures-sink", - "futures-task", - "futures-util", -] - -[[package]] -name = "futures-channel" -version = "0.3.31" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2dff15bf788c671c1934e366d07e30c1814a8ef514e1af724a602e8a2fbe1b10" -dependencies = [ - "futures-core", - "futures-sink", -] - -[[package]] -name = "futures-core" -version = "0.3.31" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "05f29059c0c2090612e8d742178b0580d2dc940c837851ad723096f87af6663e" - -[[package]] -name = "futures-executor" -version = "0.3.31" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1e28d1d997f585e54aebc3f97d39e72338912123a67330d723fdbb564d646c9f" -dependencies = [ - "futures-core", - "futures-task", - "futures-util", -] - -[[package]] -name = "futures-io" -version = "0.3.31" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9e5c1b78ca4aae1ac06c48a526a655760685149f0d465d21f37abfe57ce075c6" - -[[package]] -name = "futures-macro" -version = "0.3.31" +version = "0.3.11" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "162ee34ebcb7c64a8abebc059ce0fee27c2262618d7b60ed8faf72fef13c3650" +checksum = "976dd42dc7e85965fe702eb8164f21f450704bdde31faefd6471dba214cb594e" dependencies = [ - "proc-macro2", - "quote", - "syn 2.0.100", -] - -[[package]] -name = "futures-sink" -version = "0.3.31" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e575fab7d1e0dcb8d0c7bcf9a63ee213816ab51902e6d244a95819acacf1d4f7" - -[[package]] -name = "futures-task" -version = "0.3.31" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f90f7dce0722e95104fcb095585910c0977252f286e354b5e3bd38902cd99988" - -[[package]] -name = "futures-util" -version = "0.3.31" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9fa08315bb612088cc391249efdc3bc77536f16c91f6cf495e6fbe85b20a4a81" -dependencies = [ - "futures-channel", - "futures-core", - "futures-io", - "futures-macro", - "futures-sink", - "futures-task", - "memchr", - "pin-project-lite", - "pin-utils", - "slab", -] - -[[package]] -name = "generic-array" -version = "0.14.7" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "85649ca51fd72272d7821adaf274ad91c288277713d9c18820d8499a7ff69e9a" -dependencies = [ - "typenum", - "version_check", -] - -[[package]] -name = "getrandom" -version = "0.3.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "73fea8450eea4bac3940448fb7ae50d91f034f941199fcd9d909a5a07aa455f0" -dependencies = [ - "cfg-if", "libc", - "r-efi", - "wasi 0.14.2+wasi-0.2.4", + "windows-sys", ] -[[package]] -name = "gimli" -version = "0.31.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "07e28edb80900c19c28f1072f2e8aeca7fa06b23cd4169cefe1af5aa3260783f" - [[package]] name = "glob" version = "0.3.2" @@ -420,13 +113,10 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a8d1add55171497b4705a648c6b583acafb01d58050a51727785f0b2c8e0a2b2" [[package]] -name = "hmac" -version = "0.12.1" +name = "hermit-abi" +version = "0.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6c49c37c09c17a53d937dfbb742eb3a961d65a994e6bcdcf37e7399d0cc8ab5e" -dependencies = [ - "digest", -] +checksum = "fbd780fe5cc30f81464441920d82ac8740e2e46b29a6fad543ddd075229ce37e" [[package]] name = "home" @@ -434,47 +124,24 @@ version = "0.5.11" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "589533453244b0995c858700322199b2becb13b627df2851f64a2775d024abcf" dependencies = [ - "windows-sys 0.59.0", -] - -[[package]] -name = "is_terminal_polyfill" -version = "1.70.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7943c866cc5cd64cbc25b2e01621d07fa8eb2a1a23160ee81ce38704e97b8ecf" - -[[package]] -name = "jiff" -version = "0.2.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c102670231191d07d37a35af3eb77f1f0dbf7a71be51a962dcd57ea607be7260" -dependencies = [ - "jiff-static", - "log", - "portable-atomic", - "portable-atomic-util", - "serde", + "windows-sys", ] [[package]] -name = "jiff-static" -version = "0.2.5" +name = "humantime" +version = "2.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4cdde31a9d349f1b1f51a0b3714a5940ac022976f4b49485fc04be052b183b4c" -dependencies = [ - "proc-macro2", - "quote", - "syn 2.0.100", -] +checksum = "9b112acc8b3adf4b107a8ec20977da0273a8c386765a3ec0229bd500a1443f9f" [[package]] -name = "js-sys" -version = "0.3.77" +name = "is-terminal" +version = "0.4.16" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1cfaf33c695fc6e08064efbc1f72ec937429614f25eef83af942d0e227c3a28f" +checksum = "e04d7f318608d35d4b61ddd75cbdaee86b023ebe2bd5a66ee0915f0bf93095a9" dependencies = [ - "once_cell", - "wasm-bindgen", + "hermit-abi", + "libc", + "windows-sys", ] [[package]] @@ -507,9 +174,9 @@ dependencies = [ [[package]] name = "libpq" -version = "5.0.2" +version = "4.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "457febd48c79e1c69729a1a706144943c3ae0185cb7317efcb90a1a8f843994d" +checksum = "57eb9f8893722a29eab34ec11b42a0455abf265162871cf5d6fa4f04842b8fc5" dependencies = [ "bitflags 2.9.0", "libc", @@ -535,32 +202,12 @@ version = "0.4.15" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d26c52dbd32dccf2d10cac7725f8eae5296885fb5703b261f7d0a0739ec807ab" -[[package]] -name = "lock_api" -version = "0.4.12" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "07af8b9cdd281b7915f413fa73f29ebd5d55d0d3f0155584dade1ff18cea1b17" -dependencies = [ - "autocfg", - "scopeguard", -] - [[package]] name = "log" version = "0.4.27" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "13dc2df351e3202783a1fe0d44375f7295ffb4049267b0f3018346dc122a1d94" -[[package]] -name = "md-5" -version = "0.10.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d89e7ee0cfbedfc4da3340218492196241d89eefb6dab27de5df917a6d2e78cf" -dependencies = [ - "cfg-if", - "digest", -] - [[package]] name = "memchr" version = "2.7.4" @@ -573,26 +220,6 @@ version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "68354c5c6bd36d73ff3feceb05efa59b6acb7626617f4962be322a825e61f79a" -[[package]] -name = "miniz_oxide" -version = "0.8.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8e3e04debbb59698c15bacbb6d93584a8c0ca9cc3213cb423d31f760d8843ce5" -dependencies = [ - "adler2", -] - -[[package]] -name = "mio" -version = "1.0.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2886843bf800fba2e3377cff24abf6379b4c4d5c6681eaf9ea5b0d15090450bd" -dependencies = [ - "libc", - "wasi 0.11.0+wasi-snapshot-preview1", - "windows-sys 0.52.0", -] - [[package]] name = "nom" version = "7.1.3" @@ -603,44 +230,12 @@ dependencies = [ "minimal-lexical", ] -[[package]] -name = "object" -version = "0.36.7" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "62948e14d923ea95ea2c7c86c71013138b66525b86bdc08d2dcc262bdb497b87" -dependencies = [ - "memchr", -] - [[package]] name = "once_cell" version = "1.21.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "42f5e15c9953c5e4ccceeb2e7382a716482c34515315f7b03532b8b4e8393d2d" -[[package]] -name = "parking_lot" -version = "0.12.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f1bf18183cf54e8d6059647fc3063646a1801cf30896933ec2311622cc4b9a27" -dependencies = [ - "lock_api", - "parking_lot_core", -] - -[[package]] -name = "parking_lot_core" -version = "0.9.10" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1e401f977ab385c9e4e3ab30627d6f26d00e2c73eef317493c4ec6d468726cf8" -dependencies = [ - "cfg-if", - "libc", - "redox_syscall", - "smallvec", - "windows-targets", -] - [[package]] name = "peeking_take_while" version = "0.1.2" @@ -648,114 +243,23 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "19b17cddbe7ec3f8bc800887bab5e717348c95ea2ca0b1bf0837fb964dc67099" [[package]] -name = "percent-encoding" -version = "2.3.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e3148f5046208a5d56bcfc03053e3ca6334e51da8dfb19b6cdc8b306fae3283e" - -[[package]] -name = "phf" -version = "0.11.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1fd6780a80ae0c52cc120a26a1a42c1ae51b247a253e4e06113d23d2c2edd078" -dependencies = [ - "phf_shared", -] - -[[package]] -name = "phf_shared" -version = "0.11.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "67eabc2ef2a60eb7faa00097bd1ffdb5bd28e62bf39990626a582201b7a754e5" +name = "pgwire-lite" +version = "0.1.0" dependencies = [ - "siphasher", + "colorize", + "env_logger", + "lazy_static", + "libpq", + "libpq-sys", + "log", ] -[[package]] -name = "pin-project-lite" -version = "0.2.16" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3b3cff922bd51709b605d9ead9aa71031d81447142d828eb4a6eba76fe619f9b" - -[[package]] -name = "pin-utils" -version = "0.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184" - [[package]] name = "pkg-config" version = "0.3.32" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7edddbd0b52d732b21ad9a5fab5c704c14cd949e5e9a1ec5929a24fded1b904c" -[[package]] -name = "portable-atomic" -version = "1.11.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "350e9b48cbc6b0e028b0473b114454c6316e57336ee184ceab6e53f72c178b3e" - -[[package]] -name = "portable-atomic-util" -version = "0.2.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d8a2f0d8d040d7848a709caf78912debcc3f33ee4b3cac47d73d1e1069e83507" -dependencies = [ - "portable-atomic", -] - -[[package]] -name = "postgres" -version = "0.19.10" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "363e6dfbdd780d3aa3597b6eb430db76bb315fa9bad7fae595bb8def808b8470" -dependencies = [ - "bytes", - "fallible-iterator", - "futures-util", - "log", - "tokio", - "tokio-postgres", -] - -[[package]] -name = "postgres-protocol" -version = "0.6.8" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "76ff0abab4a9b844b93ef7b81f1efc0a366062aaef2cd702c76256b5dc075c54" -dependencies = [ - "base64", - "byteorder", - "bytes", - "fallible-iterator", - "hmac", - "md-5", - "memchr", - "rand", - "sha2", - "stringprep", -] - -[[package]] -name = "postgres-types" -version = "0.2.9" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "613283563cd90e1dfc3518d548caee47e0e725455ed619881f5cf21f36de4b48" -dependencies = [ - "bytes", - "fallible-iterator", - "postgres-protocol", -] - -[[package]] -name = "ppv-lite86" -version = "0.2.21" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "85eae3c4ed2f50dcfe72643da4befc30deadb458a9b590d720cde2f2b1e97da9" -dependencies = [ - "zerocopy", -] - [[package]] name = "proc-macro2" version = "1.0.94" @@ -774,51 +278,6 @@ dependencies = [ "proc-macro2", ] -[[package]] -name = "r-efi" -version = "5.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "74765f6d916ee2faa39bc8e68e4f3ed8949b48cccdac59983d287a7cb71ce9c5" - -[[package]] -name = "rand" -version = "0.9.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3779b94aeb87e8bd4e834cee3650289ee9e0d5677f976ecdb6d219e5f4f6cd94" -dependencies = [ - "rand_chacha", - "rand_core", - "zerocopy", -] - -[[package]] -name = "rand_chacha" -version = "0.9.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d3022b5f1df60f26e1ffddd6c66e8aa15de382ae63b3a0c1bfc0e4d3e3f325cb" -dependencies = [ - "ppv-lite86", - "rand_core", -] - -[[package]] -name = "rand_core" -version = "0.9.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "99d9a13982dcf210057a8a78572b2217b667c3beacbf3a0d8b454f6f82837d38" -dependencies = [ - "getrandom", -] - -[[package]] -name = "redox_syscall" -version = "0.5.10" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0b8c0c260b63a8219631167be35e6a988e9554dbd323f8bd08439c8ed1302bd1" -dependencies = [ - "bitflags 2.9.0", -] - [[package]] name = "regex" version = "1.11.1" @@ -848,12 +307,6 @@ version = "0.8.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2b15c43186be67a4fd63bee50d0303afffcef381492ebe2c5d87f324e1b8815c" -[[package]] -name = "rustc-demangle" -version = "0.1.24" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "719b953e2095829ee67db738b3bfa9fa368c94900df327b3f07fe6e794d2fe1f" - [[package]] name = "rustc-hash" version = "1.1.0" @@ -870,44 +323,7 @@ dependencies = [ "errno", "libc", "linux-raw-sys", - "windows-sys 0.59.0", -] - -[[package]] -name = "scopeguard" -version = "1.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49" - -[[package]] -name = "serde" -version = "1.0.219" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5f0e2c6ed6606019b4e29e69dbaba95b11854410e5347d525002456dbbb786b6" -dependencies = [ - "serde_derive", -] - -[[package]] -name = "serde_derive" -version = "1.0.219" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5b0276cf7f2c73365f7157c8123c21cd9a50fbbd844757af28ca1f5925fc2a00" -dependencies = [ - "proc-macro2", - "quote", - "syn 2.0.100", -] - -[[package]] -name = "sha2" -version = "0.10.8" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "793db75ad2bcafc3ffa7c68b215fee268f537982cd901d132f89c6343f3a3dc8" -dependencies = [ - "cfg-if", - "cpufeatures", - "digest", + "windows-sys", ] [[package]] @@ -916,63 +332,6 @@ version = "1.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64" -[[package]] -name = "signal-hook-registry" -version = "1.4.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a9e9e0b4211b72e7b8b6e85c807d36c212bdb33ea8587f7569562a84df5465b1" -dependencies = [ - "libc", -] - -[[package]] -name = "siphasher" -version = "1.0.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "56199f7ddabf13fe5074ce809e7d3f42b42ae711800501b5b16ea82ad029c39d" - -[[package]] -name = "slab" -version = "0.4.9" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8f92a496fb766b417c996b9c5e57daf2f7ad3b0bebe1ccfca4856390e3d3bb67" -dependencies = [ - "autocfg", -] - -[[package]] -name = "smallvec" -version = "1.14.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7fcf8323ef1faaee30a44a340193b1ac6814fd9b7b4e88e9d4519a3e4abe1cfd" - -[[package]] -name = "socket2" -version = "0.5.9" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4f5fd57c80058a56cf5c777ab8a126398ece8e442983605d280a44ce79d0edef" -dependencies = [ - "libc", - "windows-sys 0.52.0", -] - -[[package]] -name = "stringprep" -version = "0.1.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7b4df3d392d81bd458a8a621b8bffbd2302a12ffe288a9d931670948749463b1" -dependencies = [ - "unicode-bidi", - "unicode-normalization", - "unicode-properties", -] - -[[package]] -name = "subtle" -version = "2.6.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "13c2bddecc57b384dee18652358fb23172facb8a2c51ccc10d74c157bdea3292" - [[package]] name = "syn" version = "1.0.109" @@ -996,247 +355,46 @@ dependencies = [ ] [[package]] -name = "thiserror" -version = "2.0.12" +name = "termcolor" +version = "1.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "567b8a2dae586314f7be2a752ec7474332959c6460e02bde30d702a66d488708" +checksum = "06794f8f6c5c898b3275aebefa6b8a1cb24cd2c6c79397ab15774837a0bc5755" dependencies = [ - "thiserror-impl", + "winapi-util", ] [[package]] -name = "thiserror-impl" -version = "2.0.12" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7f7cf42b4507d8ea322120659672cf1b9dbb93f8f2d4ecfd6e51350ff5b17a1d" -dependencies = [ - "proc-macro2", - "quote", - "syn 2.0.100", -] - -[[package]] -name = "tinyvec" -version = "1.9.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "09b3661f17e86524eccd4371ab0429194e0d7c008abb45f7a7495b1719463c71" -dependencies = [ - "tinyvec_macros", -] - -[[package]] -name = "tinyvec_macros" -version = "0.1.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1f3ccbac311fea05f86f61904b462b55fb3df8837a366dfc601a0161d0532f20" - -[[package]] -name = "tokio" -version = "1.44.1" +name = "thiserror" +version = "1.0.69" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f382da615b842244d4b8738c82ed1275e6c5dd90c459a30941cd07080b06c91a" +checksum = "b6aaf5339b578ea85b50e080feb250a3e8ae8cfcdff9a461c9ec2904bc923f52" dependencies = [ - "backtrace", - "bytes", - "libc", - "mio", - "parking_lot", - "pin-project-lite", - "signal-hook-registry", - "socket2", - "tokio-macros", - "windows-sys 0.52.0", + "thiserror-impl", ] [[package]] -name = "tokio-macros" -version = "2.5.0" +name = "thiserror-impl" +version = "1.0.69" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6e06d43f1345a3bcd39f6a56dbb7dcab2ba47e68e8ac134855e7e2bdbaf8cab8" +checksum = "4fee6c4efc90059e10f81e6d42c60a18f76588c3d74cb83a0b242a2b6c7504c1" dependencies = [ "proc-macro2", "quote", "syn 2.0.100", ] -[[package]] -name = "tokio-postgres" -version = "0.7.13" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6c95d533c83082bb6490e0189acaa0bbeef9084e60471b696ca6988cd0541fb0" -dependencies = [ - "async-trait", - "byteorder", - "bytes", - "fallible-iterator", - "futures-channel", - "futures-util", - "log", - "parking_lot", - "percent-encoding", - "phf", - "pin-project-lite", - "postgres-protocol", - "postgres-types", - "rand", - "socket2", - "tokio", - "tokio-util", - "whoami", -] - -[[package]] -name = "tokio-util" -version = "0.7.14" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6b9590b93e6fcc1739458317cccd391ad3955e2bde8913edf6f95f9e65a8f034" -dependencies = [ - "bytes", - "futures-core", - "futures-sink", - "pin-project-lite", - "tokio", -] - -[[package]] -name = "typenum" -version = "1.18.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1dccffe3ce07af9386bfd29e80c0ab1a8205a2fc34e4bcd40364df902cfa8f3f" - -[[package]] -name = "unicode-bidi" -version = "0.3.18" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5c1cb5db39152898a79168971543b1cb5020dff7fe43c8dc468b0885f5e29df5" - [[package]] name = "unicode-ident" version = "1.0.18" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5a5f39404a5da50712a4c1eecf25e90dd62b613502b7e925fd4e4d19b5c96512" -[[package]] -name = "unicode-normalization" -version = "0.1.24" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5033c97c4262335cded6d6fc3e5c18ab755e1a3dc96376350f3d8e9f009ad956" -dependencies = [ - "tinyvec", -] - -[[package]] -name = "unicode-properties" -version = "0.1.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e70f2a8b45122e719eb623c01822704c4e0907e7e426a05927e1a1cfff5b75d0" - -[[package]] -name = "utf8parse" -version = "0.2.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821" - [[package]] name = "vcpkg" version = "0.2.15" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "accd4ea62f7bb7a82fe23066fb0957d48ef677f6eeb8215f372f52e48bb32426" -[[package]] -name = "version_check" -version = "0.9.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0b928f33d975fc6ad9f86c8f283853ad26bdd5b10b7f1542aa2fa15e2289105a" - -[[package]] -name = "wasi" -version = "0.11.0+wasi-snapshot-preview1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" - -[[package]] -name = "wasi" -version = "0.14.2+wasi-0.2.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9683f9a5a998d873c0d21fcbe3c083009670149a8fab228644b8bd36b2c48cb3" -dependencies = [ - "wit-bindgen-rt", -] - -[[package]] -name = "wasite" -version = "0.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b8dad83b4f25e74f184f64c43b150b91efe7647395b42289f38e50566d82855b" - -[[package]] -name = "wasm-bindgen" -version = "0.2.100" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1edc8929d7499fc4e8f0be2262a241556cfc54a0bea223790e71446f2aab1ef5" -dependencies = [ - "cfg-if", - "once_cell", - "wasm-bindgen-macro", -] - -[[package]] -name = "wasm-bindgen-backend" -version = "0.2.100" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2f0a0651a5c2bc21487bde11ee802ccaf4c51935d0d3d42a6101f98161700bc6" -dependencies = [ - "bumpalo", - "log", - "proc-macro2", - "quote", - "syn 2.0.100", - "wasm-bindgen-shared", -] - -[[package]] -name = "wasm-bindgen-macro" -version = "0.2.100" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7fe63fc6d09ed3792bd0897b314f53de8e16568c2b3f7982f468c0bf9bd0b407" -dependencies = [ - "quote", - "wasm-bindgen-macro-support", -] - -[[package]] -name = "wasm-bindgen-macro-support" -version = "0.2.100" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8ae87ea40c9f689fc23f209965b6fb8a99ad69aeeb0231408be24920604395de" -dependencies = [ - "proc-macro2", - "quote", - "syn 2.0.100", - "wasm-bindgen-backend", - "wasm-bindgen-shared", -] - -[[package]] -name = "wasm-bindgen-shared" -version = "0.2.100" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1a05d73b933a847d6cccdda8f838a22ff101ad9bf93e33684f39c1f5f0eece3d" -dependencies = [ - "unicode-ident", -] - -[[package]] -name = "web-sys" -version = "0.3.77" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "33b6dd2ef9186f1f2072e409e99cd22a975331a6b3591b12c764e0e55c60d5d2" -dependencies = [ - "js-sys", - "wasm-bindgen", -] - [[package]] name = "which" version = "4.4.2" @@ -1250,23 +408,12 @@ dependencies = [ ] [[package]] -name = "whoami" -version = "1.6.0" +name = "winapi-util" +version = "0.1.9" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6994d13118ab492c3c80c1f81928718159254c53c472bf9ce36f8dae4add02a7" +checksum = "cf221c93e13a30d793f7645a0e7762c55d169dbb0a49671918a2319d289b10bb" dependencies = [ - "redox_syscall", - "wasite", - "web-sys", -] - -[[package]] -name = "windows-sys" -version = "0.52.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d" -dependencies = [ - "windows-targets", + "windows-sys", ] [[package]] @@ -1341,32 +488,3 @@ name = "windows_x86_64_msvc" version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" - -[[package]] -name = "wit-bindgen-rt" -version = "0.39.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6f42320e61fe2cfd34354ecb597f86f413484a798ba44a8ca1165c58d42da6c1" -dependencies = [ - "bitflags 2.9.0", -] - -[[package]] -name = "zerocopy" -version = "0.8.24" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2586fea28e186957ef732a5f8b3be2da217d65c5969d4b1e17f973ebbe876879" -dependencies = [ - "zerocopy-derive", -] - -[[package]] -name = "zerocopy-derive" -version = "0.8.24" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a996a8f63c5c4448cd959ac1bab0aaa3306ccfd060472f85943ee0750f0169be" -dependencies = [ - "proc-macro2", - "quote", - "syn 2.0.100", -] diff --git a/Cargo.toml b/Cargo.toml index 2d393b5..86ffcaa 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,14 +1,25 @@ [package] -name = "client_test_harness" +name = "pgwire-lite" version = "0.1.0" edition = "2021" +description = "A library for connecting to a StackQL server using the PostgreSQL wire protocol" +license = "MIT" +authors = ["krimmer@stackql.io","javen@stackql.io"] +repository = "https://github.com/stackql/pgwire-lite-rs" +readme = "README.md" +keywords = ["stackql", "postgres", "pgwire", "database", "sql"] +categories = ["database", "api-bindings"] [dependencies] -tokio = { version = "1.44", features = ["full"] } -postgres = { version = "0.19.10" } -libpq = "5.0.2" +libpq = "4.1.0" libpq-sys = "0.8.0" -tokio-postgres = "0.7" -futures = "0.3" -env_logger = "0.11" log = "0.4" + +[dev-dependencies] +lazy_static = "1.4.0" +colorize = "0.1.0" +env_logger = "0.10" + +[[example]] +name = "simple_query" +path = "examples/simple_query.rs" \ No newline at end of file diff --git a/README.md b/README.md index a083e30..30ecfd8 100644 --- a/README.md +++ b/README.md @@ -1,30 +1,132 @@ # pgwire-lite-rs -## Testing +[![Crates.io](https://img.shields.io/crates/v/pgwire-lite.svg)](https://crates.io/crates/pgwire-lite) +[![Documentation](https://docs.rs/pgwire-lite/badge.svg)](https://docs.rs/pgwire-lite) +[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT) -## Robot testing +A lightweight PostgreSQL wire protocol client library for Rust. -Per [`.github/workflows/regression.yml`](/.github/workflows/regression.yml). +## Overview -### vscode debug +**pgwire-lite** provides a simple, efficient interface for executing queries against PostgreSQL-compatible servers, including StackQL and other wire-protocol compatible services. -- Config in `launch.json` is dependent on [the `CodeLLDB` extension](https://marketplace.visualstudio.com/items?itemName=vadimcn.vscode-lldb). -- Also installed [the `rust-analyzer` extension](https://marketplace.visualstudio.com/items?itemName=rust-lang.rust-analyzer). +This crate was created for applications that need a robust, well-tested connection to PostgreSQL-compatible servers without the overhead of a full-featured ORM. -### Manual Dream Run +## Features -Build with `cargo build --release --bin client_test_harness`. +- **Simple API** - Straightforward query execution with minimal boilerplate +- **Robust Error Handling** - Comprehensive error information with configurable verbosity +- **Flexible Value Types** - Easy type conversion between PostgreSQL and Rust types +- **SSL/TLS Support** - Secure connections with TLS and certificate validation +- **Detailed Results** - Full access to all aspects of query results including notices +- **libpq Foundation** - Built on the stable, production-tested libpq C library -Then, presuming you have an appropriate `stackql` server running, the output of running `target/release/client_test_harness "SELECT repo, count(*) as has_starred FROM github.activity.repo_stargazers WHERE owner = 'stackql' and repo in ('stackql', 'stackql-deploy') and login = 'generalkroll0' GROUP BY repo;" "host=localhost port=5444"`, once github reaches rate limit: +## Installation -```log -Query did some non-notify thing. ---- Notice 1 --- -sqlstate: 01000 -detail: http response status code: 403, response body: {"message":"API rate limit exceeded for 110.144.44.79. (But here's the good news: Authenticated requests get a higher rate limit. Check out the documentation for more details.) If you reach out to GitHub Support for help, please include the request ID E19A:36FB4A:0813:0A62:67F1D9DD and timestamp 2025-04-06 01:33:17 UTC.","documentation_url":"https://docs.github.com/rest/overview/rate-limits-for-the-rest-api","status":"403"} -http response status code: 403, response body: {"message":"API rate limit exceeded for 110.144.44.79. (But here's the good news: Authenticated requests get a higher rate limit. Check out the documentation for more details.) If you reach out to GitHub Support for help, please include the request ID E19B:5D7C:12EAF10:17BA794:67F1D9DD and timestamp 2025-04-06 01:33:17 UTC.","documentation_url":"https://docs.github.com/rest/overview/rate-limits-for-the-rest-api","status":"403"} +Add this to your `Cargo.toml`: -message: a notice level event has occurred -severity: NOTICE -Query executed successfully. -``` \ No newline at end of file +```toml +[dependencies] +pgwire-lite = "0.1.0" +``` + +## Quick Start + +The following example connects to a local [**stackql**](https://github.com/stackql/stackql) server used to query cloud providers. + +```rust +use pgwire_lite::PgwireLite; + +fn main() -> Result<(), Box> { + // Connect to a StackQL server + let client = PgwireLite::new("localhost", 5444, false, "verbose")?; + + // Pull a provider registry + client.query("REGISTRY PULL aws")?; + + // Query AWS resources using SQL + let result = client.query( + "SELECT region, name, instance_type + FROM aws.ec2.instances + WHERE region = 'us-east-1'" + )?; + + // Process the results + println!("Found {} EC2 instances:", result.row_count); + for row in &result.rows { + println!( + "Instance: {} ({}), Type: {}", + row.get("name").unwrap(), + row.get("region").unwrap(), + row.get("instance_type").unwrap() + ); + } + + Ok(()) +} +``` +## Error Handling + +**pgwire-lite** provides detailed error information and configurable verbosity: + +```rust +use pgwire_lite::PgwireLite; + +fn main() { + // Set verbosity to "verbose" for maximum error detail + let client = PgwireLite::new("localhost", 5432, false, "verbose") + .expect("Failed to create client"); + + match client.query("SELECT * FROM nonexistent_table") { + Ok(result) => { + println!("Query succeeded with {} rows", result.row_count); + }, + Err(e) => { + eprintln!("Query failed: {}", e); + // Detailed error with context, hint, line numbers, etc. + } + } +} +``` + +## TLS/SSL Support + +Secure your connections with TLS: + +```rust +use pgwire_lite::PgwireLite; +use std::env; + +fn main() -> Result<(), Box> { + // Set environment variables for TLS certificates + env::set_var("PGSSLMODE", "verify-full"); + env::set_var("PGSSLCERT", "/path/to/client-cert.pem"); + env::set_var("PGSSLKEY", "/path/to/client-key.pem"); + env::set_var("PGSSLROOTCERT", "/path/to/server-ca.pem"); + + // Create a client with TLS enabled + let client = PgwireLite::new("db.example.com", 5432, true, "default")?; + + // Execute queries over a secure connection + let result = client.query("SELECT 1 as secure_conn_example")?; + + Ok(()) +} +``` + +## Documentation + +For more detailed usage examples and API documentation, please visit [docs.rs/pgwire-lite](https://docs.rs/pgwire-lite). + +## License + +This project is licensed under the MIT License - see the LICENSE file for details. + +## Contributing + +Contributions are welcome! Please feel free to submit a Pull Request. + +## Acknowledgments + +- [libpq](https://www.postgresql.org/docs/current/libpq.html) C library +- [stackql](https://github.com/stackql/stackql) \ No newline at end of file diff --git a/examples/simple_query.rs b/examples/simple_query.rs new file mode 100644 index 0000000..b56204f --- /dev/null +++ b/examples/simple_query.rs @@ -0,0 +1,140 @@ +// example/simple_query.rs + +use colorize::AnsiColor; +use pgwire_lite::{PgwireLite, Value}; + +fn print_heading(title: &str) { + let title_owned = title.to_string(); // Convert &str to String + println!("{}", title_owned.blue().bold()); +} + +// Pretty print a row with formatting +fn print_row(row: &std::collections::HashMap, index: usize) { + if index == 0 { + // Print header + println!("Row {}: {{", index); + } else { + println!("\nRow {}: {{", index); + } + + for (key, value) in row { + println!( + " {}: {}", + key.clone().green(), + format!("{}", value).yellow() + ); + } + println!("}}"); +} + +fn execute_query(conn: &PgwireLite, query: &str) { + match conn.query(query) { + Ok(result) => { + println!(); + + println!("Elapsed time: {} ms", result.elapsed_time_ms); + + println!("Result status: {:?}", result.status); + + println!( + "{} columns, {} rows, {} notices", + result.col_count, result.row_count, result.notice_count + ); + + if !result.column_names.is_empty() { + println!("Column names: {:?}", result.column_names); + } + + if !result.rows.is_empty() { + println!("Data:"); + for (i, row) in result.rows.iter().enumerate() { + print_row(row, i); + } + } + + if !result.notices.is_empty() { + println!("Notices (detail):"); + for notice in result.notices.iter() { + if let Some(detail) = notice.fields.get("detail") { + println!("{}", detail); + } + } + } + println!(); + } + Err(e) => eprintln!("Error: {}", e), + } +} + +fn main() -> Result<(), Box> { + env_logger::init(); + + // Create a connection configuration + let conn = PgwireLite::new("localhost", 5444, false, "verbose")?; + + println!(); + println!("libpq version: {}", conn.libpq_version()); + println!("Verbosity set to: {}", conn.verbosity()); + println!(); + + // + // registry list example + // + print_heading("REGISTRY LIST example"); + execute_query(&conn, "REGISTRY LIST aws"); + + // + // registry pull example + // + print_heading("REGISTRY PULL example"); + execute_query(&conn, "REGISTRY PULL homebrew"); + + // + // simple select with one row + // + print_heading("Literal SELECT example (one row)"); + execute_query(&conn, "SELECT 1 as col_name"); + + // + // simple select with no rows + // + print_heading("Literal SELECT example (no rows)"); + execute_query(&conn, "SELECT 1 as col_name WHERE 1=0"); + + // + // failed command - handle expected error + // + print_heading("Failed command example"); + execute_query(&conn, "NOTACOMMAND"); + + // Use the same connection object for the remaining queries + // This is the critical test - can we continue using the same connection + // after a syntax error, just like psql does? + + // + // stackql provider select, multiple rows + // + print_heading("StackQL SELECT example (multiple rows)"); + execute_query( + &conn, + "SELECT * FROM homebrew.formula.vw_usage_metrics WHERE formula_name = 'stackql'", + ); + + // + // Still using the same connection + // stackql provider select, provider error, no rows + // + print_heading("StackQL SELECT example with provider error and no rows"); + execute_query(&conn, "SELECT region, function_name FROM aws.lambda.functions WHERE region = 'us-east-1' AND data__Identifier = 'fred'"); + + // + // another stackql provider select, multiple rows + // + print_heading("StackQL SELECT example (multiple rows)"); + execute_query( + &conn, + "SELECT * FROM homebrew.formula.vw_usage_metrics WHERE formula_name = 'stackql'", + ); + + Ok(()) +} diff --git a/examples/simple_query_with_mtls.rs b/examples/simple_query_with_mtls.rs new file mode 100644 index 0000000..9fe52e5 --- /dev/null +++ b/examples/simple_query_with_mtls.rs @@ -0,0 +1,42 @@ +// examples/simple_query_with_mtls.rs + +use pgwire_lite::PgwireLite; +use std::env; +use std::path::PathBuf; + +fn main() { + // Get the home directory and properly construct paths + let home_dir = env::var("HOME").expect("Could not find HOME environment variable"); + let ssl_dir = PathBuf::from(&home_dir).join("ssl-test"); + + // Set environment variables with absolute paths + env::set_var("PGSSLMODE", "verify-full"); + env::set_var( + "PGSSLCERT", + ssl_dir + .join("client_cert.pem") + .to_string_lossy() + .to_string(), + ); + env::set_var( + "PGSSLKEY", + ssl_dir.join("client_key.pem").to_string_lossy().to_string(), + ); + env::set_var( + "PGSSLROOTCERT", + ssl_dir + .join("server_cert.pem") + .to_string_lossy() + .to_string(), + ); + + // Create the connection with SSL enabled + let conn = + PgwireLite::new("localhost", 5444, true, "verbose").expect("Failed to create connection"); + + // Try to execute a simple query + match conn.query("SELECT 1 as col_name") { + Ok(result) => println!("Query result (TLS): {:?}", result), + Err(e) => eprintln!("Error: {}", e), + } +} diff --git a/src/connection.rs b/src/connection.rs new file mode 100644 index 0000000..d0cee46 --- /dev/null +++ b/src/connection.rs @@ -0,0 +1,372 @@ +// src/connection.rs + +use std::collections::HashMap; +use std::ffi::{c_void, CStr}; +use std::sync::Arc; +use std::time::Instant; + +use log::debug; + +use libpq::Connection; +use libpq_sys::ExecStatusType::{PGRES_COMMAND_OK, PGRES_TUPLES_OK}; +use libpq_sys::{ + PGContextVisibility, PQclear, PQconsumeInput, PQfname, PQgetResult, PQgetvalue, PQlibVersion, + PQnfields, PQntuples, PQresultStatus, PQresultVerboseErrorMessage, PQsendQuery, + PQsetErrorVerbosity, PQsetNoticeReceiver, +}; + +use crate::notices::{notice_receiver, Notice, NoticeStorage, Verbosity}; +use crate::value::Value; + +/// Main client for interacting with PostgreSQL-compatible servers. +/// +/// This struct provides the core functionality for establishing connections +/// and executing queries against a PostgreSQL-compatible server. +pub struct PgwireLite { + hostname: String, + port: u16, + use_tls: bool, + verbosity: Verbosity, + notices: NoticeStorage, +} + +/// Contains the complete result of a query execution. +/// +/// This struct provides access to all aspects of a query result, +/// including rows, columns, notices, and execution statistics. +#[derive(Debug)] +pub struct QueryResult { + /// Rows returned by the query, represented as maps of column names to values. + pub rows: Vec>, + + /// Names of the columns in the result set. + pub column_names: Vec, + + /// Notices generated during query execution. + pub notices: Vec, + + /// Number of rows in the result set. + pub row_count: i32, + + /// Number of columns in the result set. + pub col_count: i32, + + /// Number of notices generated during query execution. + pub notice_count: usize, + + /// Status of the query execution. + pub status: libpq_sys::ExecStatusType, + + /// Elapsed time for the query execution in milliseconds. + pub elapsed_time_ms: u64, +} + +// Helper function to safely clear a PGresult and log it +fn clear_pg_result(result: *mut libpq_sys::PGresult) { + if !result.is_null() { + unsafe { + debug!("Clearing PGresult at {:p}", result); + PQclear(result); + debug!("PGresult cleared successfully"); + } + } +} + +impl PgwireLite { + /// Creates a new PgwireLite client with the specified connection parameters. + /// + /// # Arguments + /// + /// * `hostname` - The hostname or IP address of the PostgreSQL server + /// * `port` - The port number the PostgreSQL server is listening on + /// * `use_tls` - Whether to use TLS encryption for the connection + /// * `verbosity` - Error/notice verbosity level, one of: "terse", "default", "verbose", "sqlstate" + /// + /// # Returns + /// + /// A Result containing the new PgwireLite instance or an error + /// + /// # Example + /// + /// ``` + /// use pgwire_lite::PgwireLite; + /// + /// let client = PgwireLite::new("localhost", 5432, false, "default") + /// .expect("Failed to create client"); + /// ``` + pub fn new( + hostname: &str, + port: u16, + use_tls: bool, + verbosity: &str, + ) -> Result> { + let verbosity_val = match verbosity.to_lowercase().as_str() { + "default" => Verbosity::Default, + "verbose" => Verbosity::Verbose, + "terse" => Verbosity::Terse, + "sqlstate" => Verbosity::Sqlstate, + "" => Verbosity::Default, + _ => Verbosity::Default, + }; + + // Set the log filter level based on verbosity + match verbosity_val { + Verbosity::Terse => log::set_max_level(log::LevelFilter::Warn), + Verbosity::Default => log::set_max_level(log::LevelFilter::Info), + Verbosity::Verbose => log::set_max_level(log::LevelFilter::Debug), + Verbosity::Sqlstate => log::set_max_level(log::LevelFilter::Debug), + } + + let notices = Arc::new(std::sync::Mutex::new(Vec::new())); + + Ok(PgwireLite { + hostname: hostname.to_string(), + port, + use_tls, + verbosity: verbosity_val, + notices, + }) + } + + /// Returns the version of the underlying libpq library. + /// + /// # Returns + /// + /// A string representing the version in the format "major.minor.patch" + pub fn libpq_version(&self) -> String { + let version = unsafe { PQlibVersion() }; + let major = version / 10000; + let minor = (version / 100) % 100; + let patch = version % 100; + format!("{}.{}.{}", major, minor, patch) + } + + /// Returns the current verbosity setting. + /// + /// # Returns + /// + /// A string representation of the current verbosity level + pub fn verbosity(&self) -> String { + format!("{:?}", self.verbosity) + } + + // Helper method to consume any pending results + fn consume_pending_results(conn: &Connection) { + debug!("Consuming pending results"); + unsafe { + // First make sure we've read all data available from the server + PQconsumeInput(conn.into()); + + // Then clear any pending results + loop { + let result = PQgetResult(conn.into()); + if result.is_null() { + break; + } + clear_pg_result(result); + } + } + } + + /// Executes a SQL query and returns the results. + /// + /// This method creates a fresh connection for each query, executes the query, + /// and processes the results. It handles all aspects of connection management + /// and error handling. + /// + /// # Arguments + /// + /// * `query` - The SQL query to execute + /// + /// # Returns + /// + /// A Result containing a QueryResult with the query results or an error + /// + /// # Example + /// + /// ``` + /// use pgwire_lite::PgwireLite; + /// + /// let client = PgwireLite::new("localhost", 5444, false, "default") + /// .expect("Failed to create client"); + /// + /// let result = client.query("SELECT 1 as value") + /// .expect("Query failed"); + /// + /// println!("Number of rows: {}", result.row_count); + /// ``` + pub fn query(&self, query: &str) -> Result> { + // Clear any previous notices + debug!("Clearing previous notices"); + if let Ok(mut notices) = self.notices.lock() { + notices.clear(); + } + + let start_time = Instant::now(); + + // Create a connection string + let conn_str = format!( + "host={} port={} sslmode={} application_name=pgwire-lite-client connect_timeout=10 client_encoding=UTF8", + self.hostname, + self.port, + if self.use_tls { "require" } else { "disable" } + ); + debug!("Establishing connection using: {}", conn_str); + + // Create a fresh connection for this query + let conn = Connection::new(&conn_str)?; + + // Apply the desired verbosity level + debug!("Setting error verbosity to: {:?}", self.verbosity); + unsafe { + PQsetErrorVerbosity((&conn).into(), self.verbosity.into()); + } + + // Set up notice receiver for the connection + debug!("Setting up notice receiver"); + let notices_ptr = Arc::into_raw(self.notices.clone()) as *mut c_void; + unsafe { + PQsetNoticeReceiver((&conn).into(), Some(notice_receiver), notices_ptr); + } + + // add ; to `query` if it doesn't end with one + let query = if query.ends_with(';') { + query.to_string() + } else { + format!("{};", query) + }; + + // Use PQsendQuery + debug!("Sending query: {}", query); + let send_success = unsafe { PQsendQuery((&conn).into(), query.as_ptr() as *const i8) }; + if send_success == 0 { + // If send failed, return the error + return Err( + format!("Error: {}", conn.error_message().unwrap_or("Unknown error")).into(), + ); + } + + // Process the result + debug!("Processing the result"); + let result = unsafe { PQgetResult((&conn).into()) }; + + if result.is_null() { + return Err("No result returned".into()); + } + + let status = unsafe { PQresultStatus(result) }; + + if status != PGRES_TUPLES_OK && status != PGRES_COMMAND_OK { + // Try to get a detailed error message + let error_msg_ptr = unsafe { + PQresultVerboseErrorMessage( + result, + self.verbosity.into(), + PGContextVisibility::PQSHOW_CONTEXT_ALWAYS, + ) + }; + + let error_msg = if !error_msg_ptr.is_null() { + // Convert the C string to a Rust string + let msg = unsafe { CStr::from_ptr(error_msg_ptr).to_string_lossy().into_owned() }; + // Free the C string allocated by PQresultVerboseErrorMessage + unsafe { libpq_sys::PQfreemem(error_msg_ptr as *mut _) }; + msg + } else { + // Fallback to the standard connection error message if verbose message is not available + conn.error_message().unwrap_or("Unknown error").to_string() + }; + + clear_pg_result(result); + + // Clear any pending results + Self::consume_pending_results(&conn); + + return Err(format!("{}", error_msg.trim_end()).into()); + } + + // Get column information + debug!("Getting column count"); + let col_count = unsafe { PQnfields(result) }; + + // Create a vector to store column names + debug!("Getting column names"); + let mut column_names = Vec::with_capacity(col_count as usize); + for col_index in 0..col_count { + let col_name_ptr = unsafe { PQfname(result, col_index) }; + if !col_name_ptr.is_null() { + let col_name = + unsafe { CStr::from_ptr(col_name_ptr).to_string_lossy().into_owned() }; + column_names.push(col_name); + } else { + column_names.push(String::from("(unknown)")); + } + } + + // Initialize row_count here + debug!("Getting row count"); + let row_count = if status == PGRES_TUPLES_OK { + unsafe { PQntuples(result) } + } else { + 0 + }; + + // Create the rows vector + let mut rows = Vec::new(); + + // Get row data if available + if status == PGRES_TUPLES_OK { + debug!("Processing rows"); + + // Process each row + for row_index in 0..row_count { + let mut row_data = HashMap::new(); + + // Process each column in the row + for col_index in 0..col_count { + let value_ptr = unsafe { PQgetvalue(result, row_index, col_index) }; + let value = if !value_ptr.is_null() { + let string_value = + unsafe { CStr::from_ptr(value_ptr).to_string_lossy().into_owned() }; + Value::String(string_value) + } else { + Value::Null + }; + + // Insert value into the row map using the column name as key + row_data.insert(column_names[col_index as usize].clone(), value); + } + + rows.push(row_data); + } + } + debug!("Rows processed: {}", rows.len()); + + clear_pg_result(result); + + // Check for any remaining results and clear them + Self::consume_pending_results(&conn); + + // Get the notices that were collected during the query + debug!("Collecting notices"); + let notices = if let Ok(mut lock) = self.notices.lock() { + lock.drain(..).collect() + } else { + Vec::new() + }; + let notice_count = notices.len(); + + let elapsed_time_ms = start_time.elapsed().as_millis() as u64; + + Ok(QueryResult { + rows, + column_names, + notices, + row_count, + col_count, + notice_count, + status, + elapsed_time_ms, + }) + } +} diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..4bd3799 --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,60 @@ +// src/lib.rs + +//! # PgWire Lite +//! +//! A lightweight PostgreSQL wire protocol client library built on top of libpq. +//! +//! This crate provides a simple, efficient interface for executing queries against +//! PostgreSQL-compatible servers, including StackQL and similar services. +//! +//! ## Features +//! +//! - Built on the robust libpq C library +//! - Simple API for query execution +//! - Comprehensive error handling with configurable verbosity +//! - Support for SSL/TLS connections +//! - Detailed query result information including notices +//! +//! ## Example +//! +//! ```rust +//! use pgwire_lite::PgwireLite; +//! +//! fn main() -> Result<(), Box> { +//! // Create a connection to PGWire protocol server (like a local StackQL server) +//! let conn = PgwireLite::new("localhost", 5444, false, "default")?; +//! +//! // Execute a multi-line query using a raw string +//! let result = conn.query(r#" +//! SELECT region, instance_type, COUNT(*) as num_instances +//! FROM aws.ec2.instances +//! WHERE region = 'us-east-1' +//! GROUP BY instance_type +//! "#)?; +//! +//! // Process the result +//! println!("Instance types in us-east-1:"); +//! for row in &result.rows { +//! println!( +//! " {}: {} instances", +//! row.get("instance_type").unwrap(), +//! row.get("num_instances").unwrap() +//! ); +//! } +//! +//! Ok(()) +//! } +//! ``` + +pub mod connection; +pub mod notices; +pub mod value; + +// Re-export types from the connection module +pub use connection::{PgwireLite, QueryResult}; + +// Re-export types from the notices module +pub use notices::{Notice, Verbosity}; + +// Re-export the Value type +pub use value::Value; diff --git a/src/main.rs b/src/main.rs deleted file mode 100644 index 5425de7..0000000 --- a/src/main.rs +++ /dev/null @@ -1,151 +0,0 @@ -use postgres::{Client, NoTls}; -use postgres::SimpleQueryMessage::{Row, CommandComplete}; - -use libpq; -use libpq_sys; - -use std::env; - - -use std::collections::HashMap; -use std::ffi::{CStr, c_void}; -use std::os::raw::c_char; -use std::process::exit; -use std::sync::{Arc, Mutex}; - -use libpq_sys::{ - PGconn, PGresult, PQresultErrorField, - PG_DIAG_SEVERITY, PG_DIAG_SQLSTATE, PG_DIAG_MESSAGE_PRIMARY, - PG_DIAG_MESSAGE_DETAIL, PG_DIAG_MESSAGE_HINT, PG_DIAG_STATEMENT_POSITION, - PG_DIAG_INTERNAL_POSITION, PG_DIAG_INTERNAL_QUERY, - PG_DIAG_CONTEXT, PG_DIAG_SOURCE_FILE, - PG_DIAG_SOURCE_LINE, PG_DIAG_SOURCE_FUNCTION, -}; - - - -#[derive(Debug)] -pub struct Notice { - pub fields: HashMap<&'static str, String>, -} - -type SharedNotices = Arc>>; - -/// Custom notice receiver function. -extern "C" fn notice_receiver(arg: *mut c_void, result: *const PGresult) { - if result.is_null() || arg.is_null() { - return; - } - - let field_kinds = [ - (PG_DIAG_SEVERITY, "severity"), - (PG_DIAG_SQLSTATE, "sqlstate"), - (PG_DIAG_MESSAGE_PRIMARY, "message"), - (PG_DIAG_MESSAGE_DETAIL, "detail"), - (PG_DIAG_MESSAGE_HINT, "hint"), - (PG_DIAG_STATEMENT_POSITION, "statement_position"), - (PG_DIAG_INTERNAL_POSITION, "internal_position"), - (PG_DIAG_INTERNAL_QUERY, "internal_query"), - (PG_DIAG_CONTEXT, "context"), - (PG_DIAG_SOURCE_FILE, "source_file"), - (PG_DIAG_SOURCE_LINE, "source_line"), - (PG_DIAG_SOURCE_FUNCTION, "source_function"), - ]; - - let mut notice = Notice { - fields: HashMap::new(), - }; - - for (code, label) in field_kinds.iter() { - let val_ptr = unsafe { PQresultErrorField(result, *code as i32) }; - if !val_ptr.is_null() { - let val = unsafe { CStr::from_ptr(val_ptr) }.to_string_lossy().into_owned(); - notice.fields.insert(*label, val); - } - } - - let shared = unsafe { &*(arg as *const Mutex>) }; - if let Ok(mut vec) = shared.lock() { - vec.push(notice); - } -} - -fn pq_query(conn: &libpq::Connection, query: &str, notices: SharedNotices) -> Result<(), Box> { - // Capture notice count before the query - let old_len = { - let lock = notices.lock().unwrap(); - lock.len() - }; - - // Run the query - let res = conn.exec(query); - let res_status = res.status(); - - if res_status == libpq::Status::NonFatalError { - println!("Query notify in effect: {:?}", conn.error_message()); - } else { - println!("Query did some non-notify thing."); - } - - // Extract and print only the new notices - let mut locked = notices.lock().unwrap(); - let new_notices = locked.split_off(old_len); - if new_notices.is_empty() { - println!("No notices captured."); - } else { - for (i, notice) in new_notices.iter().enumerate() { - println!("--- Notice {} ---", i + 1); - for (k, v) in ¬ice.fields { - println!("{}: {}", k, v); - } - } - } - - Ok(()) -} - -fn pq_main(query_str: &str, dsn: &str) -> Result<(), Box> { - let conninfo = dsn.to_string(); - // let query_str = "\ - // SELECT repo, count(*) as has_starred \ - // FROM github.activity.repo_stargazers \ - // WHERE owner = 'stackql' and repo in ('stackql', 'stackql-deploy') \ - // and login = 'generalkroll0' \ - // GROUP BY repo;\ - // "; - - // Create shared notice storage - let notices: SharedNotices = Arc::new(Mutex::new(Vec::new())); - let notices_raw_ptr = Arc::into_raw(notices.clone()) as *mut c_void; - - let conn = libpq::Connection::new(&conninfo)?; - unsafe { - libpq_sys::PQsetNoticeReceiver((&conn).into(), Some(notice_receiver), notices_raw_ptr); - } - - // Execute the query - pq_query(&conn, query_str, notices)?; - - // Manually drop libpq's reference to avoid leak - unsafe { - let _ = Arc::from_raw(notices_raw_ptr as *const Mutex>); - } - - Ok(()) -} - -fn main() { - let all_args = env::args().collect::>(); - if all_args.len() < 3 { - println!("Need to at least supply query argument and dsn."); - exit(1); - } - let query = &all_args[1]; - let dsn = &all_args[2]; - if let Err(e) = pq_main(&query, &dsn) { - eprintln!("Error: {}", e); - exit(1); - } - println!("Query executed successfully."); - exit(0); -} diff --git a/src/notices.rs b/src/notices.rs new file mode 100644 index 0000000..4c54d1a --- /dev/null +++ b/src/notices.rs @@ -0,0 +1,141 @@ +// src/notices.rs + +use std::collections::HashMap; +use std::ffi::{c_void, CStr}; +use std::sync::{Arc, Mutex}; + +use libpq_sys::{ + PGVerbosity, PGresult, PQresultErrorField, PG_DIAG_COLUMN_NAME, PG_DIAG_CONSTRAINT_NAME, + PG_DIAG_CONTEXT, PG_DIAG_DATATYPE_NAME, PG_DIAG_INTERNAL_POSITION, PG_DIAG_INTERNAL_QUERY, + PG_DIAG_MESSAGE_DETAIL, PG_DIAG_MESSAGE_HINT, PG_DIAG_MESSAGE_PRIMARY, PG_DIAG_SCHEMA_NAME, + PG_DIAG_SEVERITY, PG_DIAG_SEVERITY_NONLOCALIZED, PG_DIAG_SOURCE_FILE, PG_DIAG_SOURCE_FUNCTION, + PG_DIAG_SOURCE_LINE, PG_DIAG_SQLSTATE, PG_DIAG_STATEMENT_POSITION, PG_DIAG_TABLE_NAME, +}; + +/// Error/notice verbosity level for PostgreSQL connections. +/// +/// Controls the amount of detail included in error and notice messages. +#[derive(Debug, Clone, Copy)] +pub enum Verbosity { + Terse, + Default, + Verbose, + Sqlstate, +} + +impl From for PGVerbosity { + fn from(verbosity: Verbosity) -> Self { + match verbosity { + Verbosity::Terse => PGVerbosity::PQERRORS_TERSE, + Verbosity::Default => PGVerbosity::PQERRORS_DEFAULT, + Verbosity::Verbose => PGVerbosity::PQERRORS_VERBOSE, + Verbosity::Sqlstate => PGVerbosity::PQERRORS_SQLSTATE, + } + } +} + +/// Represents a notice or warning message from PostgreSQL. +/// +/// Notices are informational messages that don't cause a query to fail +/// but provide important context about the execution. +#[derive(Debug, Clone)] +pub struct Notice { + /// A map of field identifiers to their values + /// + /// Common fields include: + /// - "severity": The severity level (e.g., "NOTICE", "WARNING") + /// - "message": The primary message text + /// - "detail": Additional detail about the problem + /// - "hint": Suggestion on how to fix the problem + pub fields: HashMap<&'static str, String>, +} + +/// Thread-safe storage for collected notices +pub type NoticeStorage = Arc>>; + +/// C callback function that receives notices from PostgreSQL. +/// +/// This function is called by libpq whenever a notice is generated. +/// It extracts the relevant fields based on the verbosity level and +/// stores them in the shared notice storage. +/// +/// # Safety +/// +/// This function is called directly by C code and must follow C calling conventions. +/// It carefully handles null pointers and performs proper memory management. +pub extern "C" fn notice_receiver(arg: *mut c_void, result: *const PGresult) { + if result.is_null() || arg.is_null() { + return; + } + + let shared_notices = unsafe { &*(arg as *const Mutex>) }; + + // Retrieve verbosity level from the connection + let verbosity = match shared_notices.lock() { + Ok(notices) => notices + .get(0) + .map(|_| Verbosity::Verbose) + .unwrap_or(Verbosity::Default), + Err(_) => Verbosity::Default, + }; + + let field_kinds: Vec<(i32, &'static str)> = match verbosity { + Verbosity::Terse => vec![ + (PG_DIAG_SEVERITY as i32, "severity"), + (PG_DIAG_MESSAGE_PRIMARY as i32, "message"), + (PG_DIAG_SQLSTATE as i32, "sqlstate"), + ], + Verbosity::Default => vec![ + (PG_DIAG_SEVERITY as i32, "severity"), + (PG_DIAG_SQLSTATE as i32, "sqlstate"), + (PG_DIAG_MESSAGE_PRIMARY as i32, "message"), + (PG_DIAG_MESSAGE_DETAIL as i32, "detail"), + (PG_DIAG_MESSAGE_HINT as i32, "hint"), + ], + Verbosity::Verbose => vec![ + (PG_DIAG_SEVERITY as i32, "severity"), + ( + PG_DIAG_SEVERITY_NONLOCALIZED as i32, + "severity_nonlocalized", + ), + (PG_DIAG_SQLSTATE as i32, "sqlstate"), + (PG_DIAG_MESSAGE_PRIMARY as i32, "message"), + (PG_DIAG_MESSAGE_DETAIL as i32, "detail"), + (PG_DIAG_MESSAGE_HINT as i32, "hint"), + (PG_DIAG_STATEMENT_POSITION as i32, "statement_position"), + (PG_DIAG_INTERNAL_POSITION as i32, "internal_position"), + (PG_DIAG_INTERNAL_QUERY as i32, "internal_query"), + (PG_DIAG_CONTEXT as i32, "context"), + (PG_DIAG_SCHEMA_NAME as i32, "schema_name"), + (PG_DIAG_TABLE_NAME as i32, "table_name"), + (PG_DIAG_COLUMN_NAME as i32, "column_name"), + (PG_DIAG_DATATYPE_NAME as i32, "datatype_name"), + (PG_DIAG_CONSTRAINT_NAME as i32, "constraint_name"), + (PG_DIAG_SOURCE_FILE as i32, "source_file"), + (PG_DIAG_SOURCE_LINE as i32, "source_line"), + (PG_DIAG_SOURCE_FUNCTION as i32, "source_function"), + ], + Verbosity::Sqlstate => vec![ + (PG_DIAG_SEVERITY as i32, "severity"), + (PG_DIAG_SQLSTATE as i32, "sqlstate"), + ], + }; + + let mut notice = Notice { + fields: HashMap::new(), + }; + + for (code, label) in &field_kinds { + let val_ptr = unsafe { PQresultErrorField(result, *code) }; + if !val_ptr.is_null() { + let val = unsafe { CStr::from_ptr(val_ptr) } + .to_string_lossy() + .into_owned(); + notice.fields.insert(*label, val); + } + } + + if let Ok(mut vec) = shared_notices.lock() { + vec.push(notice); + } +} diff --git a/src/value.rs b/src/value.rs new file mode 100644 index 0000000..86f5082 --- /dev/null +++ b/src/value.rs @@ -0,0 +1,221 @@ +// src/value.rs + +use std::fmt; + +/// Represents a value from a PostgreSQL query result. +/// +/// This enum provides type-safe access to various PostgreSQL data types +/// and includes conversion methods for common Rust types. +#[derive(Debug, Clone)] +pub enum Value { + Null, + Bool(bool), + Integer(i64), + Float(f64), + String(String), + Bytes(Vec), +} + +impl fmt::Display for Value { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + Value::Null => write!(f, "NULL"), + Value::Bool(b) => write!(f, "{}", b), + Value::Integer(i) => write!(f, "{}", i), + Value::Float(fl) => write!(f, "{}", fl), + Value::String(s) => write!(f, "{}", s), + Value::Bytes(b) => write!(f, "{:?}", b), + } + } +} + +// Default implementation for Value is Null +impl Default for Value { + fn default() -> Self { + Value::Null + } +} + +// Implement From traits for common types +impl From for Value { + fn from(s: String) -> Self { + Value::String(s) + } +} + +impl From<&str> for Value { + fn from(s: &str) -> Self { + Value::String(s.to_string()) + } +} + +impl From for Value { + fn from(b: bool) -> Self { + Value::Bool(b) + } +} + +impl From for Value { + fn from(i: i64) -> Self { + Value::Integer(i) + } +} + +impl From for Value { + fn from(i: i32) -> Self { + Value::Integer(i as i64) + } +} + +impl From for Value { + fn from(f: f64) -> Self { + Value::Float(f) + } +} + +impl From> for Value { + fn from(b: Vec) -> Self { + Value::Bytes(b) + } +} + +// Try-conversion traits for getting values out +impl Value { + /// Try to get the value as a string reference. + /// + /// Returns `None` if the value is not a String. + /// + /// # Example + /// + /// ``` + /// use pgwire_lite::Value; + /// + /// let val = Value::from("hello"); + /// assert_eq!(val.as_str(), Some("hello")); + /// + /// let val = Value::Integer(42); + /// assert_eq!(val.as_str(), None); + /// ``` + pub fn as_str(&self) -> Option<&str> { + match self { + Value::String(s) => Some(s), + _ => None, + } + } + + /// Try to get the value as a boolean. + /// + /// Returns `Some(bool)` if the value is a boolean or a string that can be + /// interpreted as a boolean (e.g., "true", "yes", "1"). + /// Returns `None` for other types or strings that cannot be parsed as booleans. + /// + /// # Example + /// + /// ``` + /// use pgwire_lite::Value; + /// + /// let val = Value::Bool(true); + /// assert_eq!(val.as_bool(), Some(true)); + /// + /// let val = Value::from("yes"); + /// assert_eq!(val.as_bool(), Some(true)); + /// + /// let val = Value::from("0"); + /// assert_eq!(val.as_bool(), Some(false)); + /// + /// let val = Value::from("invalid"); + /// assert_eq!(val.as_bool(), None); + /// ``` + pub fn as_bool(&self) -> Option { + match self { + Value::Bool(b) => Some(*b), + Value::String(s) => match s.to_lowercase().as_str() { + "true" | "t" | "yes" | "y" | "1" => Some(true), + "false" | "f" | "no" | "n" | "0" => Some(false), + _ => None, + }, + _ => None, + } + } + + /// Try to get the value as a 64-bit signed integer. + /// + /// Returns `Some(i64)` if the value is an integer, a float that can be + /// converted to an integer, or a string that can be parsed as an integer. + /// Returns `None` for other types or values that cannot be converted. + /// + /// # Example + /// + /// ``` + /// use pgwire_lite::Value; + /// + /// let val = Value::Integer(42); + /// assert_eq!(val.as_i64(), Some(42)); + /// + /// let val = Value::Float(42.0); + /// assert_eq!(val.as_i64(), Some(42)); + /// + /// let val = Value::from("42"); + /// assert_eq!(val.as_i64(), Some(42)); + /// + /// let val = Value::from("invalid"); + /// assert_eq!(val.as_i64(), None); + /// ``` + pub fn as_i64(&self) -> Option { + match self { + Value::Integer(i) => Some(*i), + Value::Float(f) => Some(*f as i64), + Value::String(s) => s.parse::().ok(), + _ => None, + } + } + + /// Try to get the value as a 64-bit floating point number. + /// + /// Returns `Some(f64)` if the value is a float, an integer, or a string + /// that can be parsed as a float. + /// Returns `None` for other types or values that cannot be converted. + /// + /// # Example + /// + /// ``` + /// use pgwire_lite::Value; + /// + /// let val = Value::Float(3.14); + /// assert_eq!(val.as_f64(), Some(3.14)); + /// + /// let val = Value::Integer(42); + /// assert_eq!(val.as_f64(), Some(42.0)); + /// + /// let val = Value::from("3.14"); + /// assert_eq!(val.as_f64(), Some(3.14)); + /// + /// let val = Value::from("invalid"); + /// assert_eq!(val.as_f64(), None); + /// ``` + pub fn as_f64(&self) -> Option { + match self { + Value::Float(f) => Some(*f), + Value::Integer(i) => Some(*i as f64), + Value::String(s) => s.parse::().ok(), + _ => None, + } + } + + /// Check if the value is NULL. + /// + /// # Example + /// + /// ``` + /// use pgwire_lite::Value; + /// + /// let val = Value::Null; + /// assert!(val.is_null()); + /// + /// let val = Value::Integer(42); + /// assert!(!val.is_null()); + /// ``` + pub fn is_null(&self) -> bool { + matches!(self, Value::Null) + } +} diff --git a/ssl/openssl.cnf b/ssl/openssl.cnf new file mode 100644 index 0000000..44d8988 --- /dev/null +++ b/ssl/openssl.cnf @@ -0,0 +1,24 @@ +[ req ] +default_bits = 4096 +default_md = sha256 +prompt = no +encrypt_key = no +distinguished_name = pg_server +# stackql_ca +[ pg_ca ] +countryName = "AU" # C= +stateOrProvinceName = "VIC" # ST= +localityName = "Melbourne" # L= +organizationName = "StackQL" # O= +organizationalUnitName = "Core Functions" # OU= +commonName = "pg_ca" # CN= +emailAddress = "krimmer@stackql.io" # CN/emailAddress= +# stackql_server +[ pg_server ] +countryName = "AU" # C= +stateOrProvinceName = "VIC" # ST= +localityName = "Melbourne" # L= +organizationName = "StackQL" # O= +organizationalUnitName = "Core Functions" # OU= +commonName = "127.0.0.1" # CN= +emailAddress = "krimmer@stackql.io" # CN/emailAddress= \ No newline at end of file diff --git a/start-secure-server.sh b/start-secure-server.sh new file mode 100644 index 0000000..cc5e415 --- /dev/null +++ b/start-secure-server.sh @@ -0,0 +1,24 @@ +#!/bin/bash + +# Ensure any existing server is stopped +./stop-server.sh + +# Generate server and client certificates +mkdir -p ~/ssl-test +rm -rf ~/ssl-test/* +openssl req -x509 -keyout ~/ssl-test/server_key.pem -out ~/ssl-test/server_cert.pem -config ./ssl/openssl.cnf -days 365 >/dev/null 2>&1 +openssl req -x509 -keyout ~/ssl-test/client_key.pem -out ~/ssl-test/client_cert.pem -config ./ssl/openssl.cnf -days 365 >/dev/null 2>&1 +chmod 400 ~/ssl-test/client_key.pem + +# Export environment variables for secure server +export PGPORT=5444 +export PGSSLCERT=~/ssl-test/client_cert.pem +export PGSSLKEY=~/ssl-test/client_key.pem +export PGSSLROOTCERT=~/ssl-test/server_cert.pem +export PGSSLSRVKEY=~/ssl-test/server_key.pem +export CLIENT_CERT=$(base64 -w 0 ~/ssl-test/client_cert.pem) +export PGSSLMODE=require + +# Start the secure PostgreSQL server using stackql +nohup ./stackql srv --pgsrv.address=0.0.0.0 --pgsrv.port=$PGPORT --pgsrv.tls='{ "keyFilePath": "'${PGSSLSRVKEY}'", "certFilePath": "'${PGSSLROOTCERT}'", "clientCAs": [ "'${CLIENT_CERT}'" ] }' >/dev/null 2>&1 & +echo "secure server started on port $PGPORT with TLS" \ No newline at end of file diff --git a/start-server.sh b/start-server.sh new file mode 100644 index 0000000..04a74d9 --- /dev/null +++ b/start-server.sh @@ -0,0 +1,19 @@ +check_and_stop_server() { + # Check if stackql server is running using pgrep + SERVER_PID=$(pgrep -f "stackql") + + if [ -n "$SERVER_PID" ]; then + echo "stackql server is running with PID $SERVER_PID, stopping it..." + ./stop-server.sh + else + echo "no running stackql server found." + fi +} + +check_and_stop_server + +PGPORT=5444 +echo "starting local stackql server on port $PGPORT" +nohup ./stackql --pgsrv.address=0.0.0.0 --pgsrv.port=$PGPORT srv & +sleep 5 +echo "stackql server started" diff --git a/stop-server.sh b/stop-server.sh new file mode 100644 index 0000000..4ae8906 --- /dev/null +++ b/stop-server.sh @@ -0,0 +1,12 @@ +#!/bin/bash + +# Check if stackql server is running and kill the process +SERVER_PID=$(pgrep -f "stackql") + +if [ -n "$SERVER_PID" ]; then + echo "stopping stackql server with PID $SERVER_PID..." + kill -9 $SERVER_PID + echo "stackql server stopped." +else + echo "no running stackql server found." +fi diff --git a/tests/integration.rs b/tests/integration.rs new file mode 100644 index 0000000..5ee28f3 --- /dev/null +++ b/tests/integration.rs @@ -0,0 +1,278 @@ +// tests/integration.rs + +#[cfg(test)] +mod integration_tests { + use colorize::AnsiColor; + use lazy_static::lazy_static; + use libpq_sys::ExecStatusType; + use pgwire_lite::{PgwireLite, QueryResult, Value}; + use std::collections::HashMap; + use std::sync::{Arc, Mutex, Once}; + + // Setup static connection that will be shared across tests + lazy_static! { + static ref CONNECTION: Arc>> = Arc::new(Mutex::new(None)); + static ref INIT: Once = Once::new(); + } + + // Initialize connection once for all tests + fn setup_connection() -> Arc>> { + INIT.call_once(|| { + let conn = PgwireLite::new("localhost", 5444, false, "verbose") + .expect("Failed to create connection"); + println!("\nConnection created successfully"); + println!("libpq version: {}", conn.libpq_version()); + println!("Verbosity set to: {}", conn.verbosity()); + + *CONNECTION.lock().unwrap() = Some(conn); + }); + + CONNECTION.clone() + } + + // Helper function to validate query results + fn validate_result(result: &QueryResult, expected_col_count: i32, min_row_count: i32) { + assert!( + result.elapsed_time_ms > 0, + "Elapsed time should be greater than 0" + ); + + if expected_col_count > 0 { + assert_eq!(result.status, ExecStatusType::PGRES_TUPLES_OK); + assert_eq!( + result.col_count, expected_col_count, + "Column count mismatch" + ); + assert_eq!( + result.column_names.len() as i32, + expected_col_count, + "Column names length mismatch" + ); + } else { + assert_eq!(result.status, ExecStatusType::PGRES_COMMAND_OK); + } + + assert!( + result.row_count >= min_row_count, + "Row count should be at least {}", + min_row_count + ); + } + + // Helper to check if a row contains expected column names + fn validate_row_has_columns(row: &HashMap, expected_columns: &[&str]) { + for col in expected_columns { + assert!( + row.contains_key(&col.to_string()), + "Row should contain column '{}'", + col + ); + } + } + + // Test 1: Registry List + #[test] + fn test_registry_list() { + let conn_mutex = setup_connection(); + let conn_guard = conn_mutex.lock().unwrap(); + let conn = conn_guard.as_ref().unwrap(); + + println!("\n{}", "REGISTRY LIST example".blue().bold()); + let result = conn + .query("REGISTRY LIST aws") + .expect("REGISTRY LIST should succeed"); + validate_result(&result, 2, 1); + assert!(result.column_names.contains(&"provider".to_string())); + assert!(result.column_names.contains(&"versions".to_string())); + + // Validate at least the first row has proper content + if !result.rows.is_empty() { + let row = &result.rows[0]; + validate_row_has_columns(row, &["provider", "versions"]); + assert_eq!( + row.get("provider").unwrap().to_string(), + "aws", + "Provider should be aws" + ); + // Just check that versions is non-empty, as it may change over time + assert!( + !row.get("versions").unwrap().to_string().is_empty(), + "Versions should not be empty" + ); + } + } + + // Test 2: Registry Pull + #[test] + fn test_registry_pull() { + let conn_mutex = setup_connection(); + let conn_guard = conn_mutex.lock().unwrap(); + let conn = conn_guard.as_ref().unwrap(); + + println!("\n{}", "REGISTRY PULL example".blue().bold()); + let result = conn + .query("REGISTRY PULL homebrew") + .expect("REGISTRY PULL should succeed"); + validate_result(&result, 0, 0); + } + + // Test 3: Simple SELECT with one row + #[test] + fn test_simple_select_one_row() { + let conn_mutex = setup_connection(); + let conn_guard = conn_mutex.lock().unwrap(); + let conn = conn_guard.as_ref().unwrap(); + + println!("\n{}", "Literal SELECT example (one row)".blue().bold()); + let result = conn + .query("SELECT 1 as col_name") + .expect("Simple SELECT should succeed"); + validate_result(&result, 1, 1); + assert_eq!(result.column_names[0], "col_name"); + assert_eq!(result.rows[0].get("col_name").unwrap().to_string(), "1"); + } + + // Test 4: Simple SELECT with no rows + #[test] + fn test_simple_select_no_rows() { + let conn_mutex = setup_connection(); + let conn_guard = conn_mutex.lock().unwrap(); + let conn = conn_guard.as_ref().unwrap(); + + println!("\n{}", "Literal SELECT example (no rows)".blue().bold()); + let result = conn + .query("SELECT 1 as col_name WHERE 1=0") + .expect("Simple SELECT with no rows should succeed"); + validate_result(&result, 1, 0); + assert_eq!(result.column_names[0], "col_name"); + assert!(result.rows.is_empty()); + } + + // Test 5: Failed command - Expected to error + #[test] + fn test_failed_command() { + let conn_mutex = setup_connection(); + let conn_guard = conn_mutex.lock().unwrap(); + let conn = conn_guard.as_ref().unwrap(); + + println!("\n{}", "Failed command example".blue().bold()); + let result = conn.query("NOTACOMMAND"); + assert!(result.is_err(), "Invalid command should return an error"); + let error_message = result.unwrap_err().to_string(); + assert!( + error_message.contains("syntax error"), + "Error should contain 'syntax error'" + ); + } + + // Test 6: StackQL provider SELECT after error + #[test] + fn test_stackql_select_after_error() { + let conn_mutex = setup_connection(); + let conn_guard = conn_mutex.lock().unwrap(); + let conn = conn_guard.as_ref().unwrap(); + + // First try a failing command (to verify we can recover) + let _ = conn.query("NOTACOMMAND"); + + println!( + "\n{}", + "StackQL SELECT example (multiple rows)".blue().bold() + ); + let result = conn + .query("SELECT * FROM homebrew.formula.vw_usage_metrics WHERE formula_name = 'stackql'") + .expect("StackQL query should succeed after error"); + validate_result(&result, 7, 1); + + let expected_columns = [ + "formula_name", + "installs_30d", + "installs_90d", + "installs_365d", + "install_on_requests_30d", + "install_on_requests_90d", + "install_on_requests_365d", + ]; + + assert!( + result + .column_names + .iter() + .all(|col| expected_columns.contains(&col.as_str())), + "All expected columns should be present" + ); + + if !result.rows.is_empty() { + let row = &result.rows[0]; + validate_row_has_columns(row, &expected_columns); + assert_eq!( + row.get("formula_name").unwrap().to_string(), + "stackql", + "Formula name should be stackql" + ); + } + } + + // Test 7: StackQL provider SELECT with provider error + #[test] + fn test_stackql_select_with_provider_error() { + let conn_mutex = setup_connection(); + let conn_guard = conn_mutex.lock().unwrap(); + let conn = conn_guard.as_ref().unwrap(); + + println!( + "\n{}", + "StackQL SELECT example with provider error and no rows" + .blue() + .bold() + ); + let result = conn.query("SELECT region, function_name FROM aws.lambda.functions WHERE region = 'us-east-1' AND data__Identifier = 'fred'") + .expect("Query with provider error should succeed at connection level"); + validate_result(&result, 2, 0); + assert!( + result.notice_count > 0, + "Should have notices for provider error" + ); + + // Check that notices contain error information + let has_error_notice = result.notices.iter().any(|notice| { + notice + .fields + .get("detail") + .map(|detail| { + detail.contains("UnrecognizedClientException") || detail.contains("400") + }) + .unwrap_or(false) + }); + assert!(has_error_notice, "Should have notice with error details"); + } + + // Test 8: Final StackQL SELECT to ensure connection still works + #[test] + fn test_final_stackql_select() { + let conn_mutex = setup_connection(); + let conn_guard = conn_mutex.lock().unwrap(); + let conn = conn_guard.as_ref().unwrap(); + + println!( + "\n{}", + "Final StackQL SELECT example (multiple rows)".blue().bold() + ); + let result = conn + .query("SELECT * FROM homebrew.formula.vw_usage_metrics WHERE formula_name = 'stackql'") + .expect("Final StackQL query should succeed"); + validate_result(&result, 7, 1); + + // Verify we have results with the expected stackql formula + if !result.rows.is_empty() { + let row = &result.rows[0]; + assert_eq!( + row.get("formula_name").unwrap().to_string(), + "stackql", + "Formula name should be stackql" + ); + } + + println!("\nAll tests completed successfully!"); + } +}